Skip to content

Commit

Permalink
feat: (light client)add new consensus types for Electra (#14527)
Browse files Browse the repository at this point in the history
* add `LightClientBootstrapElectra` to proto

* add `LightClientUpdateElectra` to proto

* implement `bootstrapElectra`

* add ssz support for `LightClientBootstrapElectra`

* remove unused type

* update `CHANGELOG.md`

* implement `updateElectra`

* refactor: remove `CurrentSyncCommitteeBranchElectra()` from `LightClientBootstrap`

* remove `NewWrappedHeaderElectra`

* Update consensus-types/light-client/bootstrap.go

Co-authored-by: Radosław Kapka <[email protected]>

* Update consensus-types/light-client/update.go

Co-authored-by: Radosław Kapka <[email protected]>

* add `CurrentSyncCommitteeBranchElectra()` to `LightClientBootstrap`

* add `NextSyncCommitteeBranchElectra()` to `LightClientUpdate`

* revert changes to unrelated pb/ssz files

* Revert "revert changes to unrelated pb/ssz files"

This reverts commit 5ceaaf5.

* more refactors

* even more refactors

---------

Co-authored-by: Radosław Kapka <[email protected]>
  • Loading branch information
rupam-04 and rkapka authored Oct 15, 2024
1 parent 7238848 commit dc91c96
Show file tree
Hide file tree
Showing 8 changed files with 1,102 additions and 244 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ The format is based on Keep a Changelog, and this project adheres to Semantic Ve
- Add Bellatrix tests for light client functions.
- Add Discovery Rebooter Feature.
- Added GetBlockAttestationsV2 endpoint.
- Light client support: Consensus types for Electra

### Changed

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 @@ -9,6 +9,7 @@ import (

type LightClientExecutionBranch = [fieldparams.ExecutionBranchDepth][fieldparams.RootLength]byte
type LightClientSyncCommitteeBranch = [fieldparams.SyncCommitteeBranchDepth][fieldparams.RootLength]byte
type LightClientSyncCommitteeBranchElectra = [fieldparams.SyncCommitteeBranchDepthElectra][fieldparams.RootLength]byte
type LightClientFinalityBranch = [fieldparams.FinalityBranchDepth][fieldparams.RootLength]byte

type LightClientHeader interface {
Expand All @@ -24,15 +25,17 @@ type LightClientBootstrap interface {
Version() int
Header() LightClientHeader
CurrentSyncCommittee() *pb.SyncCommittee
CurrentSyncCommitteeBranch() LightClientSyncCommitteeBranch
CurrentSyncCommitteeBranch() (LightClientSyncCommitteeBranch, error)
CurrentSyncCommitteeBranchElectra() (LightClientSyncCommitteeBranchElectra, error)
}

type LightClientUpdate interface {
ssz.Marshaler
Version() int
AttestedHeader() LightClientHeader
NextSyncCommittee() *pb.SyncCommittee
NextSyncCommitteeBranch() LightClientSyncCommitteeBranch
NextSyncCommitteeBranch() (LightClientSyncCommitteeBranch, error)
NextSyncCommitteeBranchElectra() (LightClientSyncCommitteeBranchElectra, error)
FinalizedHeader() LightClientHeader
FinalityBranch() LightClientFinalityBranch
SyncAggregate() *pb.SyncAggregate
Expand Down
90 changes: 84 additions & 6 deletions consensus-types/light-client/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ func NewWrappedBootstrap(m proto.Message) (interfaces.LightClientBootstrap, erro
return NewWrappedBootstrapCapella(t)
case *pb.LightClientBootstrapDeneb:
return NewWrappedBootstrapDeneb(t)
case *pb.LightClientBootstrapElectra:
return NewWrappedBootstrapElectra(t)
default:
return nil, fmt.Errorf("cannot construct light client bootstrap from type %T", t)
}
Expand Down Expand Up @@ -83,8 +85,12 @@ func (h *bootstrapAltair) CurrentSyncCommittee() *pb.SyncCommittee {
return h.p.CurrentSyncCommittee
}

func (h *bootstrapAltair) CurrentSyncCommitteeBranch() interfaces.LightClientSyncCommitteeBranch {
return h.currentSyncCommitteeBranch
func (h *bootstrapAltair) CurrentSyncCommitteeBranch() (interfaces.LightClientSyncCommitteeBranch, error) {
return h.currentSyncCommitteeBranch, nil
}

func (h *bootstrapAltair) CurrentSyncCommitteeBranchElectra() (interfaces.LightClientSyncCommitteeBranchElectra, error) {
return [6][32]byte{}, consensustypes.ErrNotSupported("CurrentSyncCommitteeBranchElectra", version.Altair)
}

type bootstrapCapella struct {
Expand Down Expand Up @@ -143,8 +149,12 @@ func (h *bootstrapCapella) CurrentSyncCommittee() *pb.SyncCommittee {
return h.p.CurrentSyncCommittee
}

func (h *bootstrapCapella) CurrentSyncCommitteeBranch() interfaces.LightClientSyncCommitteeBranch {
return h.currentSyncCommitteeBranch
func (h *bootstrapCapella) CurrentSyncCommitteeBranch() (interfaces.LightClientSyncCommitteeBranch, error) {
return h.currentSyncCommitteeBranch, nil
}

func (h *bootstrapCapella) CurrentSyncCommitteeBranchElectra() (interfaces.LightClientSyncCommitteeBranchElectra, error) {
return [6][32]byte{}, consensustypes.ErrNotSupported("CurrentSyncCommitteeBranchElectra", version.Capella)
}

type bootstrapDeneb struct {
Expand Down Expand Up @@ -203,6 +213,74 @@ func (h *bootstrapDeneb) CurrentSyncCommittee() *pb.SyncCommittee {
return h.p.CurrentSyncCommittee
}

func (h *bootstrapDeneb) CurrentSyncCommitteeBranch() interfaces.LightClientSyncCommitteeBranch {
return h.currentSyncCommitteeBranch
func (h *bootstrapDeneb) CurrentSyncCommitteeBranch() (interfaces.LightClientSyncCommitteeBranch, error) {
return h.currentSyncCommitteeBranch, nil
}

func (h *bootstrapDeneb) CurrentSyncCommitteeBranchElectra() (interfaces.LightClientSyncCommitteeBranchElectra, error) {
return [6][32]byte{}, consensustypes.ErrNotSupported("CurrentSyncCommitteeBranchElectra", version.Deneb)
}

type bootstrapElectra struct {
p *pb.LightClientBootstrapElectra
header interfaces.LightClientHeader
currentSyncCommitteeBranch interfaces.LightClientSyncCommitteeBranchElectra
}

var _ interfaces.LightClientBootstrap = &bootstrapElectra{}

func NewWrappedBootstrapElectra(p *pb.LightClientBootstrapElectra) (interfaces.LightClientBootstrap, error) {
if p == nil {
return nil, consensustypes.ErrNilObjectWrapped
}
header, err := NewWrappedHeaderDeneb(p.Header)
if err != nil {
return nil, err
}
branch, err := createBranch[interfaces.LightClientSyncCommitteeBranchElectra](
"sync committee",
p.CurrentSyncCommitteeBranch,
fieldparams.SyncCommitteeBranchDepthElectra,
)
if err != nil {
return nil, err
}

return &bootstrapElectra{
p: p,
header: header,
currentSyncCommitteeBranch: branch,
}, nil
}

func (h *bootstrapElectra) MarshalSSZTo(dst []byte) ([]byte, error) {
return h.p.MarshalSSZTo(dst)
}

func (h *bootstrapElectra) MarshalSSZ() ([]byte, error) {
return h.p.MarshalSSZ()
}

func (h *bootstrapElectra) SizeSSZ() int {
return h.p.SizeSSZ()
}

func (h *bootstrapElectra) Version() int {
return version.Electra
}

func (h *bootstrapElectra) Header() interfaces.LightClientHeader {
return h.header
}

func (h *bootstrapElectra) CurrentSyncCommittee() *pb.SyncCommittee {
return h.p.CurrentSyncCommittee
}

func (h *bootstrapElectra) CurrentSyncCommitteeBranch() (interfaces.LightClientSyncCommitteeBranch, error) {
return [5][32]byte{}, consensustypes.ErrNotSupported("CurrentSyncCommitteeBranch", version.Electra)
}

func (h *bootstrapElectra) CurrentSyncCommitteeBranchElectra() (interfaces.LightClientSyncCommitteeBranchElectra, error) {
return h.currentSyncCommitteeBranch, nil
}
120 changes: 114 additions & 6 deletions consensus-types/light-client/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,12 @@ func (u *updateAltair) NextSyncCommittee() *pb.SyncCommittee {
return u.p.NextSyncCommittee
}

func (u *updateAltair) NextSyncCommitteeBranch() interfaces.LightClientSyncCommitteeBranch {
return u.nextSyncCommitteeBranch
func (u *updateAltair) NextSyncCommitteeBranch() (interfaces.LightClientSyncCommitteeBranch, error) {
return u.nextSyncCommitteeBranch, nil
}

func (u *updateAltair) NextSyncCommitteeBranchElectra() (interfaces.LightClientSyncCommitteeBranchElectra, error) {
return [6][32]byte{}, consensustypes.ErrNotSupported("NextSyncCommitteeBranchElectra", version.Altair)
}

func (u *updateAltair) FinalizedHeader() interfaces.LightClientHeader {
Expand Down Expand Up @@ -192,8 +196,12 @@ func (u *updateCapella) NextSyncCommittee() *pb.SyncCommittee {
return u.p.NextSyncCommittee
}

func (u *updateCapella) NextSyncCommitteeBranch() interfaces.LightClientSyncCommitteeBranch {
return u.nextSyncCommitteeBranch
func (u *updateCapella) NextSyncCommitteeBranch() (interfaces.LightClientSyncCommitteeBranch, error) {
return u.nextSyncCommitteeBranch, nil
}

func (u *updateCapella) NextSyncCommitteeBranchElectra() (interfaces.LightClientSyncCommitteeBranchElectra, error) {
return [6][32]byte{}, consensustypes.ErrNotSupported("NextSyncCommitteeBranchElectra", version.Capella)
}

func (u *updateCapella) FinalizedHeader() interfaces.LightClientHeader {
Expand Down Expand Up @@ -284,8 +292,12 @@ func (u *updateDeneb) NextSyncCommittee() *pb.SyncCommittee {
return u.p.NextSyncCommittee
}

func (u *updateDeneb) NextSyncCommitteeBranch() interfaces.LightClientSyncCommitteeBranch {
return u.nextSyncCommitteeBranch
func (u *updateDeneb) NextSyncCommitteeBranch() (interfaces.LightClientSyncCommitteeBranch, error) {
return u.nextSyncCommitteeBranch, nil
}

func (u *updateDeneb) NextSyncCommitteeBranchElectra() (interfaces.LightClientSyncCommitteeBranchElectra, error) {
return [6][32]byte{}, consensustypes.ErrNotSupported("NextSyncCommitteeBranchElectra", version.Deneb)
}

func (u *updateDeneb) FinalizedHeader() interfaces.LightClientHeader {
Expand All @@ -303,3 +315,99 @@ func (u *updateDeneb) SyncAggregate() *pb.SyncAggregate {
func (u *updateDeneb) SignatureSlot() primitives.Slot {
return u.p.SignatureSlot
}

type updateElectra struct {
p *pb.LightClientUpdateElectra
attestedHeader interfaces.LightClientHeader
nextSyncCommitteeBranch interfaces.LightClientSyncCommitteeBranchElectra
finalizedHeader interfaces.LightClientHeader
finalityBranch interfaces.LightClientFinalityBranch
}

var _ interfaces.LightClientUpdate = &updateElectra{}

func NewWrappedUpdateElectra(p *pb.LightClientUpdateElectra) (interfaces.LightClientUpdate, error) {
if p == nil {
return nil, consensustypes.ErrNilObjectWrapped
}
attestedHeader, err := NewWrappedHeaderDeneb(p.AttestedHeader)
if err != nil {
return nil, err
}
finalizedHeader, err := NewWrappedHeaderDeneb(p.FinalizedHeader)
if err != nil {
return nil, err
}
scBranch, err := createBranch[interfaces.LightClientSyncCommitteeBranchElectra](
"sync committee",
p.NextSyncCommitteeBranch,
fieldparams.SyncCommitteeBranchDepthElectra,
)
if err != nil {
return nil, err
}
finalityBranch, err := createBranch[interfaces.LightClientFinalityBranch](
"finality",
p.FinalityBranch,
fieldparams.FinalityBranchDepth,
)
if err != nil {
return nil, err
}

return &updateElectra{
p: p,
attestedHeader: attestedHeader,
nextSyncCommitteeBranch: scBranch,
finalizedHeader: finalizedHeader,
finalityBranch: finalityBranch,
}, nil
}

func (u *updateElectra) MarshalSSZTo(dst []byte) ([]byte, error) {
return u.p.MarshalSSZTo(dst)
}

func (u *updateElectra) MarshalSSZ() ([]byte, error) {
return u.p.MarshalSSZ()
}

func (u *updateElectra) SizeSSZ() int {
return u.p.SizeSSZ()
}

func (u *updateElectra) Version() int {
return version.Electra
}

func (u *updateElectra) AttestedHeader() interfaces.LightClientHeader {
return u.attestedHeader
}

func (u *updateElectra) NextSyncCommittee() *pb.SyncCommittee {
return u.p.NextSyncCommittee
}

func (u *updateElectra) NextSyncCommitteeBranch() (interfaces.LightClientSyncCommitteeBranch, error) {
return [5][32]byte{}, consensustypes.ErrNotSupported("NextSyncCommitteeBranch", version.Electra)
}

func (u *updateElectra) NextSyncCommitteeBranchElectra() (interfaces.LightClientSyncCommitteeBranchElectra, error) {
return u.nextSyncCommitteeBranch, nil
}

func (u *updateElectra) FinalizedHeader() interfaces.LightClientHeader {
return u.finalizedHeader
}

func (u *updateElectra) FinalityBranch() interfaces.LightClientFinalityBranch {
return u.finalityBranch
}

func (u *updateElectra) SyncAggregate() *pb.SyncAggregate {
return u.p.SyncAggregate
}

func (u *updateElectra) SignatureSlot() primitives.Slot {
return u.p.SignatureSlot
}
2 changes: 2 additions & 0 deletions proto/prysm/v1alpha1/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ ssz_electra_objs = [
"BlindedBeaconBlockElectra",
"Consolidation",
"IndexedAttestationElectra",
"LightClientBootstrapElectra",
"LightClientUpdateElectra",
"PendingDeposit",
"PendingDeposits",
"PendingConsolidation",
Expand Down
Loading

0 comments on commit dc91c96

Please sign in to comment.