Skip to content

Commit

Permalink
Light Client consensus types (#14518)
Browse files Browse the repository at this point in the history
* protos/SSZ

* interfaces

* types

* cleanup/dedup

* changelog

* use createBranch in headers

* error handling

---------

Co-authored-by: rkapka <radoslaw.kapka@gmail.com>
  • Loading branch information
Inspector-Butters and rkapka authored Oct 8, 2024
1 parent cfbfccb commit 7fc5c71
Showing 21 changed files with 5,564 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@ The format is based on Keep a Changelog, and this project adheres to Semantic Ve
- Light client support: Add light client database changes.
- Light client support: Implement capella and deneb changes.
- Light client support: Implement `BlockToLightClientHeader` function.
- Light client support: Consensus types.
- GetBeaconStateV2: add Electra case.
- Implement [consensus-specs/3875](https://github.com/ethereum/consensus-specs/pull/3875)
- Tests to ensure sepolia config matches the official upstream yaml
4 changes: 2 additions & 2 deletions beacon-chain/core/light-client/lightclient.go
Original file line number Diff line number Diff line change
@@ -243,8 +243,8 @@ func createDefaultLightClientUpdate() (*ethpbv2.LightClientUpdate, error) {
Pubkeys: pubKeys,
AggregatePubkey: make([]byte, fieldparams.BLSPubkeyLength),
}
nextSyncCommitteeBranch := make([][]byte, fieldparams.NextSyncCommitteeBranchDepth)
for i := 0; i < fieldparams.NextSyncCommitteeBranchDepth; i++ {
nextSyncCommitteeBranch := make([][]byte, fieldparams.SyncCommitteeBranchDepth)
for i := 0; i < fieldparams.SyncCommitteeBranchDepth; i++ {
nextSyncCommitteeBranch[i] = make([]byte, fieldparams.RootLength)
}
executionBranch := make([][]byte, executionBranchNumOfLeaves)
8 changes: 4 additions & 4 deletions beacon-chain/rpc/eth/light-client/helpers.go
Original file line number Diff line number Diff line change
@@ -91,7 +91,7 @@ func createLightClientBootstrapAltair(ctx context.Context, state state.BeaconSta
return nil, errors.Wrap(err, "could not get current sync committee proof")
}

branch := make([]string, fieldparams.NextSyncCommitteeBranchDepth)
branch := make([]string, fieldparams.SyncCommitteeBranchDepth)
for i, proof := range currentSyncCommitteeProof {
branch[i] = hexutil.Encode(proof)
}
@@ -159,7 +159,7 @@ func createLightClientBootstrapCapella(ctx context.Context, state state.BeaconSt
return nil, errors.Wrap(err, "could not get current sync committee proof")
}

branch := make([]string, fieldparams.NextSyncCommitteeBranchDepth)
branch := make([]string, fieldparams.SyncCommitteeBranchDepth)
for i, proof := range currentSyncCommitteeProof {
branch[i] = hexutil.Encode(proof)
}
@@ -227,7 +227,7 @@ func createLightClientBootstrapDeneb(ctx context.Context, state state.BeaconStat
return nil, errors.Wrap(err, "could not get current sync committee proof")
}

branch := make([]string, fieldparams.NextSyncCommitteeBranchDepth)
branch := make([]string, fieldparams.SyncCommitteeBranchDepth)
for i, proof := range currentSyncCommitteeProof {
branch[i] = hexutil.Encode(proof)
}
@@ -288,7 +288,7 @@ func newLightClientOptimisticUpdateFromBeaconState(
}

func IsSyncCommitteeUpdate(update *v2.LightClientUpdate) bool {
nextSyncCommitteeBranch := make([][]byte, fieldparams.NextSyncCommitteeBranchDepth)
nextSyncCommitteeBranch := make([][]byte, fieldparams.SyncCommitteeBranchDepth)
return !reflect.DeepEqual(update.NextSyncCommitteeBranch, nextSyncCommitteeBranch)
}

6 changes: 3 additions & 3 deletions beacon-chain/rpc/eth/light-client/helpers_test.go
Original file line number Diff line number Diff line change
@@ -13,7 +13,7 @@ import (

// When the update has relevant sync committee
func createNonEmptySyncCommitteeBranch() [][]byte {
res := make([][]byte, fieldparams.NextSyncCommitteeBranchDepth)
res := make([][]byte, fieldparams.SyncCommitteeBranchDepth)
res[0] = []byte("xyz")
return res
}
@@ -101,7 +101,7 @@ func TestIsBetterUpdate(t *testing.T) {
}},
},
},
NextSyncCommitteeBranch: make([][]byte, fieldparams.NextSyncCommitteeBranchDepth),
NextSyncCommitteeBranch: make([][]byte, fieldparams.SyncCommitteeBranchDepth),
SignatureSlot: 9999,
},
newUpdate: &ethpbv2.LightClientUpdate{
@@ -147,7 +147,7 @@ func TestIsBetterUpdate(t *testing.T) {
}},
},
},
NextSyncCommitteeBranch: make([][]byte, fieldparams.NextSyncCommitteeBranchDepth),
NextSyncCommitteeBranch: make([][]byte, fieldparams.SyncCommitteeBranchDepth),
SignatureSlot: 9999,
},
expectedResult: false,
4 changes: 3 additions & 1 deletion config/fieldparams/mainnet.go
Original file line number Diff line number Diff line change
@@ -33,7 +33,9 @@ const (
BlobSize = 131072 // defined to match blob.size in bazel ssz codegen
BlobSidecarSize = 131928 // defined to match blob sidecar size in bazel ssz codegen
KzgCommitmentInclusionProofDepth = 17 // Merkle proof depth for blob_kzg_commitments list item
NextSyncCommitteeBranchDepth = 5 // NextSyncCommitteeBranchDepth defines the depth of the next sync committee branch.
ExecutionBranchDepth = 4 // ExecutionBranchDepth defines the number of leaves in a merkle proof of the execution payload header.
SyncCommitteeBranchDepth = 5 // SyncCommitteeBranchDepth 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.
PendingBalanceDepositsLimit = 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.
4 changes: 3 additions & 1 deletion config/fieldparams/minimal.go
Original file line number Diff line number Diff line change
@@ -33,7 +33,9 @@ const (
BlobSize = 131072 // defined to match blob.size in bazel ssz codegen
BlobSidecarSize = 131928 // defined to match blob sidecar size in bazel ssz codegen
KzgCommitmentInclusionProofDepth = 17 // Merkle proof depth for blob_kzg_commitments list item
NextSyncCommitteeBranchDepth = 5 // NextSyncCommitteeBranchDepth defines the depth of the next sync committee branch.
ExecutionBranchDepth = 4 // ExecutionBranchDepth defines the number of leaves in a merkle proof of the execution payload header.
SyncCommitteeBranchDepth = 5 // SyncCommitteeBranchDepth 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.
PendingBalanceDepositsLimit = 134217728 // Maximum number of pending balance deposits in the beacon state.
PendingPartialWithdrawalsLimit = 64 // Maximum number of pending partial withdrawals in the beacon state.
PendingConsolidationsLimit = 64 // Maximum number of pending consolidations in the beacon state.
1 change: 1 addition & 0 deletions consensus-types/interfaces/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ go_library(
srcs = [
"beacon_block.go",
"error.go",
"light_client.go",
"utils.go",
"validator.go",
],
58 changes: 58 additions & 0 deletions consensus-types/interfaces/light_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package interfaces

import (
ssz "github.com/prysmaticlabs/fastssz"
fieldparams "github.com/prysmaticlabs/prysm/v5/config/fieldparams"
"github.com/prysmaticlabs/prysm/v5/consensus-types/primitives"
pb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
)

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

type LightClientHeader interface {
ssz.Marshaler
Version() int
Beacon() *pb.BeaconBlockHeader
Execution() (ExecutionData, error)
ExecutionBranch() (LightClientExecutionBranch, error)
}

type LightClientBootstrap interface {
ssz.Marshaler
Version() int
Header() LightClientHeader
CurrentSyncCommittee() *pb.SyncCommittee
CurrentSyncCommitteeBranch() LightClientSyncCommitteeBranch
}

type LightClientUpdate interface {
ssz.Marshaler
Version() int
AttestedHeader() LightClientHeader
NextSyncCommittee() *pb.SyncCommittee
NextSyncCommitteeBranch() LightClientSyncCommitteeBranch
FinalizedHeader() LightClientHeader
FinalityBranch() LightClientFinalityBranch
SyncAggregate() *pb.SyncAggregate
SignatureSlot() primitives.Slot
}

type LightClientFinalityUpdate interface {
ssz.Marshaler
Version() int
AttestedHeader() LightClientHeader
FinalizedHeader() LightClientHeader
FinalityBranch() LightClientFinalityBranch
SyncAggregate() *pb.SyncAggregate
SignatureSlot() primitives.Slot
}

type LightClientOptimisticUpdate interface {
ssz.Marshaler
Version() int
AttestedHeader() LightClientHeader
SyncAggregate() *pb.SyncAggregate
SignatureSlot() primitives.Slot
}
26 changes: 26 additions & 0 deletions consensus-types/light-client/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
load("@prysm//tools/go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = [
"bootstrap.go",
"finality_update.go",
"header.go",
"helpers.go",
"optimistic_update.go",
"update.go",
],
importpath = "github.com/prysmaticlabs/prysm/v5/consensus-types/light-client",
visibility = ["//visibility:public"],
deps = [
"//config/fieldparams: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",
"@org_golang_google_protobuf//proto:go_default_library",
],
)
Loading

0 comments on commit 7fc5c71

Please sign in to comment.