Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Light Client: Electra finality branch #14597

Merged
merged 4 commits into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions api/server/structs/conversions_lightclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,39 @@ func LightClientUpdateFromConsensus(update interfaces.LightClientUpdate) (*Light
if err != nil {
return nil, errors.Wrap(err, "could not marshal finalized light client header")
}
finalityBranch := update.FinalityBranch()

var scBranch [][32]byte
var finalityBranch [][32]byte
if update.Version() >= version.Electra {
b, err := update.NextSyncCommitteeBranchElectra()
scb, err := update.NextSyncCommitteeBranchElectra()
if err != nil {
return nil, err
}
scBranch = b[:]
scBranch = scb[:]
fb, err := update.FinalityBranchElectra()
if err != nil {
return nil, err
}
finalityBranch = fb[:]
} else {
b, err := update.NextSyncCommitteeBranch()
Copy link
Contributor

@rupam-04 rupam-04 Oct 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
b, err := update.NextSyncCommitteeBranch()
scb, err := update.NextSyncCommitteeBranch()

Lets have this for consistency since you used scb before too

if err != nil {
return nil, err
}
scBranch = b[:]
fb, err := update.FinalityBranch()
if err != nil {
return nil, err
}
finalityBranch = fb[:]
}

return &LightClientUpdate{
AttestedHeader: attestedHeader,
NextSyncCommittee: SyncCommitteeFromConsensus(update.NextSyncCommittee()),
NextSyncCommitteeBranch: branchToJSON(scBranch),
FinalizedHeader: finalizedHeader,
FinalityBranch: branchToJSON(finalityBranch[:]),
FinalityBranch: branchToJSON(finalityBranch),
SyncAggregate: SyncAggregateFromConsensus(update.SyncAggregate()),
SignatureSlot: fmt.Sprintf("%d", update.SignatureSlot()),
}, nil
Expand All @@ -57,12 +67,26 @@ func LightClientFinalityUpdateFromConsensus(update interfaces.LightClientFinalit
if err != nil {
return nil, errors.Wrap(err, "could not marshal finalized light client header")
}
finalityBranch := update.FinalityBranch()

var finalityBranch [][32]byte
if update.Version() >= version.Electra {
b, err := update.FinalityBranchElectra()
if err != nil {
return nil, err
}
finalityBranch = b[:]
} else {
b, err := update.FinalityBranch()
if err != nil {
return nil, err
}
finalityBranch = b[:]
}

return &LightClientFinalityUpdate{
AttestedHeader: attestedHeader,
FinalizedHeader: finalizedHeader,
FinalityBranch: branchToJSON(finalityBranch[:]),
FinalityBranch: branchToJSON(finalityBranch),
SyncAggregate: SyncAggregateFromConsensus(update.SyncAggregate()),
SignatureSlot: fmt.Sprintf("%d", update.SignatureSlot()),
}, nil
Expand Down
49 changes: 32 additions & 17 deletions beacon-chain/core/light-client/lightclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ import (
"google.golang.org/protobuf/proto"
)

const (
FinalityBranchNumOfLeaves = 6
)

func NewLightClientFinalityUpdateFromBeaconState(
ctx context.Context,
currentSlot primitives.Slot,
Expand Down Expand Up @@ -159,7 +155,7 @@ func NewLightClientUpdateFromBeaconState(
updateAttestedPeriod := slots.SyncCommitteePeriod(slots.ToEpoch(attestedBlock.Block().Slot()))

// update = LightClientUpdate()
result, err := CreateDefaultLightClientUpdate(currentSlot)
result, err := CreateDefaultLightClientUpdate(currentSlot, attestedBlock)
if err != nil {
return nil, errors.Wrap(err, "could not create default light client update")
}
Expand Down Expand Up @@ -243,7 +239,7 @@ func NewLightClientUpdateFromBeaconState(
return result, nil
}

func CreateDefaultLightClientUpdate(currentSlot primitives.Slot) (interfaces.LightClientUpdate, error) {
func CreateDefaultLightClientUpdate(currentSlot primitives.Slot, attestedBlock interfaces.ReadOnlySignedBeaconBlock) (interfaces.LightClientUpdate, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can pass attestedState here for lesser conflicts with #14585

currentEpoch := slots.ToEpoch(currentSlot)

syncCommitteeSize := params.BeaconConfig().SyncCommitteeSize
Expand All @@ -270,8 +266,14 @@ func CreateDefaultLightClientUpdate(currentSlot primitives.Slot) (interfaces.Lig
for i := 0; i < fieldparams.ExecutionBranchDepth; i++ {
executionBranch[i] = make([]byte, 32)
}
finalityBranch := make([][]byte, fieldparams.FinalityBranchDepth)
for i := 0; i < fieldparams.FinalityBranchDepth; i++ {

var finalityBranch [][]byte
if attestedBlock.Version() >= version.Electra {
finalityBranch = make([][]byte, fieldparams.FinalityBranchDepthElectra)
} else {
finalityBranch = make([][]byte, fieldparams.FinalityBranchDepth)
}
for i := 0; i < len(finalityBranch); i++ {
finalityBranch[i] = make([]byte, 32)
}

Expand Down Expand Up @@ -308,15 +310,28 @@ func CreateDefaultLightClientUpdate(currentSlot primitives.Slot) (interfaces.Lig
FinalityBranch: finalityBranch,
}
} else {
m = &pb.LightClientUpdateElectra{
AttestedHeader: &pb.LightClientHeaderDeneb{
Beacon: &pb.BeaconBlockHeader{},
Execution: &enginev1.ExecutionPayloadHeaderDeneb{},
ExecutionBranch: executionBranch,
},
NextSyncCommittee: nextSyncCommittee,
NextSyncCommitteeBranch: nextSyncCommitteeBranch,
FinalityBranch: finalityBranch,
if attestedBlock.Version() >= version.Electra {
m = &pb.LightClientUpdateElectra{
AttestedHeader: &pb.LightClientHeaderDeneb{
Beacon: &pb.BeaconBlockHeader{},
Execution: &enginev1.ExecutionPayloadHeaderDeneb{},
ExecutionBranch: executionBranch,
},
NextSyncCommittee: nextSyncCommittee,
NextSyncCommitteeBranch: nextSyncCommitteeBranch,
FinalityBranch: finalityBranch,
}
} else {
m = &pb.LightClientUpdateDeneb{
AttestedHeader: &pb.LightClientHeaderDeneb{
Beacon: &pb.BeaconBlockHeader{},
Execution: &enginev1.ExecutionPayloadHeaderDeneb{},
ExecutionBranch: executionBranch,
},
NextSyncCommittee: nextSyncCommittee,
NextSyncCommitteeBranch: nextSyncCommitteeBranch,
FinalityBranch: finalityBranch,
}
}
}

Expand Down
30 changes: 24 additions & 6 deletions beacon-chain/rpc/eth/light-client/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ import (
"fmt"
"reflect"

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/pkg/errors"
fieldparams "github.com/prysmaticlabs/prysm/v5/config/fieldparams"
"github.com/prysmaticlabs/prysm/v5/config/params"
"github.com/prysmaticlabs/prysm/v5/consensus-types/primitives"

lightclient "github.com/prysmaticlabs/prysm/v5/beacon-chain/core/light-client"
"github.com/prysmaticlabs/prysm/v5/runtime/version"

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/prysmaticlabs/prysm/v5/api/server/structs"
"github.com/prysmaticlabs/prysm/v5/beacon-chain/state"
"github.com/prysmaticlabs/prysm/v5/config/params"
"github.com/prysmaticlabs/prysm/v5/consensus-types/interfaces"
"github.com/prysmaticlabs/prysm/v5/time/slots"
)
Expand Down Expand Up @@ -147,8 +147,20 @@ func HasRelevantSyncCommittee(update interfaces.LightClientUpdate) (bool, error)
return !reflect.DeepEqual(branch, interfaces.LightClientSyncCommitteeBranch{}), nil
}

func HasFinality(update interfaces.LightClientUpdate) bool {
return !reflect.DeepEqual(update.FinalityBranch(), interfaces.LightClientFinalityBranch{})
func HasFinality(update interfaces.LightClientUpdate) (bool, error) {
if update.Version() >= version.Electra {
b, err := update.FinalityBranchElectra()
if err != nil {
return false, err
}
return !reflect.DeepEqual(b, interfaces.LightClientFinalityBranchElectra{}), nil
}

b, err := update.FinalityBranch()
if err != nil {
return false, err
}
return !reflect.DeepEqual(b, interfaces.LightClientFinalityBranch{}), nil
}

func IsBetterUpdate(newUpdate, oldUpdate interfaces.LightClientUpdate) (bool, error) {
Expand Down Expand Up @@ -187,8 +199,14 @@ func IsBetterUpdate(newUpdate, oldUpdate interfaces.LightClientUpdate) (bool, er
}

// Compare indication of any finality
newHasFinality := HasFinality(newUpdate)
oldHasFinality := HasFinality(oldUpdate)
newHasFinality, err := HasFinality(newUpdate)
if err != nil {
return false, err
}
oldHasFinality, err := HasFinality(oldUpdate)
if err != nil {
return false, err
}
if newHasFinality != oldHasFinality {
return newHasFinality, nil
}
Expand Down
1 change: 1 addition & 0 deletions config/fieldparams/mainnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const (
SyncCommitteeBranchDepth = 5 // SyncCommitteeBranchDepth defines the number of leaves in a merkle proof of a sync committee.
SyncCommitteeBranchDepthElectra = 6 // SyncCommitteeBranchDepthElectra defines the number of leaves in a merkle proof of a sync committee.
FinalityBranchDepth = 6 // FinalityBranchDepth defines the number of leaves in a merkle proof of the finalized checkpoint root.
FinalityBranchDepthElectra = 7 // FinalityBranchDepthElectra defines the number of leaves in a merkle proof of the finalized checkpoint root.
PendingDepositsLimit = 134217728 // Maximum number of pending balance deposits in the beacon state.
PendingPartialWithdrawalsLimit = 134217728 // Maximum number of pending partial withdrawals in the beacon state.
PendingConsolidationsLimit = 262144 // Maximum number of pending consolidations in the beacon state.
Expand Down
7 changes: 5 additions & 2 deletions consensus-types/interfaces/light_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type LightClientExecutionBranch = [fieldparams.ExecutionBranchDepth][fieldparams
type LightClientSyncCommitteeBranch = [fieldparams.SyncCommitteeBranchDepth][fieldparams.RootLength]byte
type LightClientSyncCommitteeBranchElectra = [fieldparams.SyncCommitteeBranchDepthElectra][fieldparams.RootLength]byte
type LightClientFinalityBranch = [fieldparams.FinalityBranchDepth][fieldparams.RootLength]byte
type LightClientFinalityBranchElectra = [fieldparams.FinalityBranchDepthElectra][fieldparams.RootLength]byte

type LightClientHeader interface {
ssz.Marshaler
Expand Down Expand Up @@ -45,7 +46,8 @@ type LightClientUpdate interface {
SetNextSyncCommitteeBranchElectra(branch [][]byte) error
FinalizedHeader() LightClientHeader
SetFinalizedHeader(header LightClientHeader) error
FinalityBranch() LightClientFinalityBranch
FinalityBranch() (LightClientFinalityBranch, error)
FinalityBranchElectra() (LightClientFinalityBranchElectra, error)
SetFinalityBranch(branch [][]byte) error
SyncAggregate() *pb.SyncAggregate
SetSyncAggregate(sa *pb.SyncAggregate)
Expand All @@ -59,7 +61,8 @@ type LightClientFinalityUpdate interface {
Version() int
AttestedHeader() LightClientHeader
FinalizedHeader() LightClientHeader
FinalityBranch() LightClientFinalityBranch
FinalityBranch() (LightClientFinalityBranch, error)
FinalityBranchElectra() (LightClientFinalityBranchElectra, error)
SyncAggregate() *pb.SyncAggregate
SignatureSlot() primitives.Slot
}
Expand Down
2 changes: 2 additions & 0 deletions consensus-types/light-client/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//config/fieldparams:go_default_library",
"//config/params:go_default_library",
"//consensus-types:go_default_library",
"//consensus-types/blocks:go_default_library",
"//consensus-types/interfaces:go_default_library",
"//consensus-types/primitives:go_default_library",
"//encoding/bytesutil:go_default_library",
"//proto/prysm/v1alpha1:go_default_library",
"//runtime/version:go_default_library",
"//time/slots:go_default_library",
"@org_golang_google_protobuf//proto:go_default_library",
],
)
8 changes: 4 additions & 4 deletions consensus-types/light-client/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func NewWrappedBootstrapAltair(p *pb.LightClientBootstrapAltair) (interfaces.Lig
if p == nil {
return nil, consensustypes.ErrNilObjectWrapped
}
header, err := NewWrappedHeaderAltair(p.Header)
header, err := NewWrappedHeader(p.Header)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -105,7 +105,7 @@ func NewWrappedBootstrapCapella(p *pb.LightClientBootstrapCapella) (interfaces.L
if p == nil {
return nil, consensustypes.ErrNilObjectWrapped
}
header, err := NewWrappedHeaderCapella(p.Header)
header, err := NewWrappedHeader(p.Header)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -169,7 +169,7 @@ func NewWrappedBootstrapDeneb(p *pb.LightClientBootstrapDeneb) (interfaces.Light
if p == nil {
return nil, consensustypes.ErrNilObjectWrapped
}
header, err := NewWrappedHeaderDeneb(p.Header)
header, err := NewWrappedHeader(p.Header)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -233,7 +233,7 @@ func NewWrappedBootstrapElectra(p *pb.LightClientBootstrapElectra) (interfaces.L
if p == nil {
return nil, consensustypes.ErrNilObjectWrapped
}
header, err := NewWrappedHeaderDeneb(p.Header)
header, err := NewWrappedHeader(p.Header)
if err != nil {
return nil, err
}
Expand Down
Loading
Loading