From 41f8400f97eff981d671239b52ac860f718efd7d Mon Sep 17 00:00:00 2001 From: tersec Date: Fri, 19 Apr 2024 19:20:45 +0000 Subject: [PATCH] fix Electra light client objects; use version-2-0 for Nim again (#6222) --- .github/workflows/ci.yml | 4 +- beacon_chain/spec/datatypes/electra.nim | 48 ++++++++++++++++---- beacon_chain/spec/eth2_merkleization.nim | 5 +- beacon_chain/spec/eth2_ssz_serialization.nim | 9 +++- beacon_chain/spec/forks.nim | 1 + beacon_chain/sync/sync_protocol.nim | 7 +++ 6 files changed, 60 insertions(+), 14 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index abccf8ba32..6cbd43d88f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -33,7 +33,7 @@ jobs: cpu: amd64 - os: windows cpu: amd64 - branch: [~, upstream/version-1-6, v2.0.4] + branch: [~, upstream/version-1-6, upstream/version-2-0] exclude: - target: os: macos @@ -47,7 +47,7 @@ jobs: include: - branch: upstream/version-1-6 branch-short: version-1-6 - - branch: v2.0.4 + - branch: upstream/version-2-0 branch-short: version-2-0 nimflags-extra: --mm:refc - target: diff --git a/beacon_chain/spec/datatypes/electra.nim b/beacon_chain/spec/datatypes/electra.nim index 7877b4d40d..27a1ef6491 100644 --- a/beacon_chain/spec/datatypes/electra.nim +++ b/beacon_chain/spec/datatypes/electra.nim @@ -18,18 +18,36 @@ import std/typetraits, chronicles, - stew/byteutils, json_serialization, ssz_serialization/[merkleization, proofs], ssz_serialization/types as sszTypes, ../digest, - "."/[base, phase0, altair, bellatrix, capella] + "."/[base, phase0] from kzg4844 import KzgCommitment, KzgProof +from stew/bitops2 import log2trunc +from stew/byteutils import to0xHex +from ./altair import + EpochParticipationFlags, InactivityScores, SyncAggregate, SyncCommittee, + TrustedSyncAggregate +from ./bellatrix import BloomLogs, ExecutionAddress, Transaction +from ./capella import + HistoricalSummary, SignedBLSToExecutionChangeList, Withdrawal from ./deneb import Blobs, BlobsBundle, KzgCommitments, KzgProofs export json_serialization, base, kzg4844 +const + # https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.5/specs/altair/light-client/sync-protocol.md#constants + # All of these indices are rooted in `BeaconState`. + # The first member (`genesis_time`) is 64, subsequent members +1 each. + # If there are ever more than 64 members in `BeaconState`, indices change! + # `FINALIZED_ROOT_GINDEX` is one layer deeper, i.e., `84 * 2 + 1`. + # https://github.com/ethereum/consensus-specs/blob/v1.4.0/ssz/merkle-proofs.md + FINALIZED_ROOT_GINDEX = 169.GeneralizedIndex # finalized_checkpoint > root + CURRENT_SYNC_COMMITTEE_GINDEX = 86.GeneralizedIndex # current_sync_committee + NEXT_SYNC_COMMITTEE_GINDEX = 87.GeneralizedIndex # next_sync_committee + type # https://github.com/ethereum/consensus-specs/blob/94a0b6c581f2809aa8aca4ef7ee6fbb63f9d74e9/specs/electra/beacon-chain.md#depositreceipt DepositReceipt* = object @@ -75,7 +93,7 @@ type blockValue*: Wei blobsBundle*: BlobsBundle - # https://github.com/ethereum/consensus-specs/blob/82133085a1295e93394ebdf71df8f2f6e0962588/specs/electra/beacon-chain.md#executionpayloadheader + # https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.0/specs/electra/beacon-chain.md#executionpayloadheader ExecutionPayloadHeader* = object # Execution block header fields parent_hash*: Eth2Digest @@ -141,12 +159,21 @@ type source_index*: uint64 target_index*: uint64 + FinalityBranch = + array[log2trunc(FINALIZED_ROOT_GINDEX), Eth2Digest] + + CurrentSyncCommitteeBranch = + array[log2trunc(CURRENT_SYNC_COMMITTEE_GINDEX), Eth2Digest] + + NextSyncCommitteeBranch = + array[log2trunc(NEXT_SYNC_COMMITTEE_GINDEX), Eth2Digest] + # https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.5/specs/capella/light-client/sync-protocol.md#modified-lightclientheader LightClientHeader* = object beacon*: BeaconBlockHeader ## Beacon block header - execution*: ExecutionPayloadHeader + execution*: electra.ExecutionPayloadHeader ## Execution payload header corresponding to `beacon.body_root` (from Capella onward) execution_branch*: capella.ExecutionBranch @@ -157,7 +184,7 @@ type current_sync_committee*: SyncCommittee ## Current sync committee corresponding to `header.beacon.state_root` - current_sync_committee_branch*: altair.CurrentSyncCommitteeBranch + current_sync_committee_branch*: CurrentSyncCommitteeBranch # https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.5/specs/altair/light-client/sync-protocol.md#lightclientupdate LightClientUpdate* = object @@ -167,11 +194,11 @@ type next_sync_committee*: SyncCommittee ## Next sync committee corresponding to ## `attested_header.beacon.state_root` - next_sync_committee_branch*: altair.NextSyncCommitteeBranch + next_sync_committee_branch*: NextSyncCommitteeBranch # Finalized header corresponding to `attested_header.beacon.state_root` finalized_header*: LightClientHeader - finality_branch*: altair.FinalityBranch + finality_branch*: FinalityBranch sync_aggregate*: SyncAggregate ## Sync committee aggregate signature @@ -185,7 +212,7 @@ type # Finalized header corresponding to `attested_header.beacon.state_root` finalized_header*: LightClientHeader - finality_branch*: altair.FinalityBranch + finality_branch*: FinalityBranch # Sync committee aggregate signature sync_aggregate*: SyncAggregate @@ -405,7 +432,7 @@ type attester_slashings*: List[AttesterSlashing, Limit MAX_ATTESTER_SLASHINGS_ELECTRA] ## [Modified in Electra:EIP7549] - attestations*: List[Attestation, Limit MAX_ATTESTATIONS_ELECTRA] + attestations*: List[phase0.Attestation, Limit MAX_ATTESTATIONS_ELECTRA] ## [Modified in Electra:EIP7549] deposits*: List[Deposit, Limit MAX_DEPOSITS] voluntary_exits*: List[SignedVoluntaryExit, Limit MAX_VOLUNTARY_EXITS] @@ -527,6 +554,9 @@ type root* {.dontSerialize.}: Eth2Digest # cached root of signed beacon block + ElectraCommitteeValidatorsBits* = + BitList[Limit MAX_VALIDATORS_PER_COMMITTEE * MAX_COMMITTEES_PER_SLOT] + SomeSignedBeaconBlock* = SignedBeaconBlock | SigVerifiedSignedBeaconBlock | diff --git a/beacon_chain/spec/eth2_merkleization.nim b/beacon_chain/spec/eth2_merkleization.nim index 4dd0b1950f..6ae66c8337 100644 --- a/beacon_chain/spec/eth2_merkleization.nim +++ b/beacon_chain/spec/eth2_merkleization.nim @@ -21,6 +21,7 @@ from ./datatypes/altair import HashedBeaconState, SignedBeaconBlock from ./datatypes/bellatrix import HashedBeaconState, SignedBeaconBlock from ./datatypes/capella import HashedBeaconState, SignedBeaconBlock from ./datatypes/deneb import HashedBeaconState, SignedBeaconBlock +from ./datatypes/electra import HashedBeaconState, SignedBeaconBlock export ssz_codec, merkleization, proofs @@ -32,13 +33,13 @@ type func hash_tree_root*( x: phase0.HashedBeaconState | altair.HashedBeaconState | bellatrix.HashedBeaconState | capella.HashedBeaconState | - deneb.HashedBeaconState) {. + deneb.HashedBeaconState | electra.SignedBeaconBlock) {. error: "HashedBeaconState should not be hashed".} func hash_tree_root*( x: phase0.SignedBeaconBlock | altair.SignedBeaconBlock | bellatrix.SignedBeaconBlock | capella.SignedBeaconBlock | - deneb.SignedBeaconBlock) {. + deneb.SignedBeaconBlock | electra.SignedBeaconBlock) {. error: "SignedBeaconBlock should not be hashed".} func depositCountBytes*(x: uint64): array[32, byte] = diff --git a/beacon_chain/spec/eth2_ssz_serialization.nim b/beacon_chain/spec/eth2_ssz_serialization.nim index 737f2ff2a7..a3d3a1f8f5 100644 --- a/beacon_chain/spec/eth2_ssz_serialization.nim +++ b/beacon_chain/spec/eth2_ssz_serialization.nim @@ -17,6 +17,7 @@ import ./eth2_merkleization from ./datatypes/deneb import SignedBeaconBlock, TrustedSignedBeaconBlock +from ./datatypes/electra import SignedBeaconBlock, TrustedSignedBeaconBlock export phase0, altair, ssz_codec, ssz_serialization, eth2_merkleization @@ -60,6 +61,12 @@ template readSszBytes*( template readSszBytes*( data: openArray[byte], val: var deneb.TrustedSignedBeaconBlock, updateRoot = true) = readAndUpdateRoot(data, val, updateRoot) +template readSszBytes*( + data: openArray[byte], val: var electra.SignedBeaconBlock, updateRoot = true) = + readAndUpdateRoot(data, val, updateRoot) +template readSszBytes*( + data: openArray[byte], val: var electra.TrustedSignedBeaconBlock, updateRoot = true) = + readAndUpdateRoot(data, val, updateRoot) template readSszBytes*( data: openArray[byte], val: var auto, updateRoot: bool) = @@ -78,4 +85,4 @@ proc fromSszBytes*( let key = ValidatorPubKey.fromSszBytes(bytes) - HashedValidatorPubKey.init(key) \ No newline at end of file + HashedValidatorPubKey.init(key) diff --git a/beacon_chain/spec/forks.nim b/beacon_chain/spec/forks.nim index be127f6190..25d081736d 100644 --- a/beacon_chain/spec/forks.nim +++ b/beacon_chain/spec/forks.nim @@ -617,6 +617,7 @@ template PayloadAttributes*( static: doAssert ConsensusFork.high == ConsensusFork.Electra, "eth2_merkleization has been checked and `hash_tree_root` is up to date" +# TODO are these used? # TODO when https://github.com/nim-lang/Nim/issues/21086 fixed, use return type # `ref T` func new*(T: type ForkedHashedBeaconState, data: phase0.BeaconState): diff --git a/beacon_chain/sync/sync_protocol.nim b/beacon_chain/sync/sync_protocol.nim index 7c71b419ee..e744d3b2f5 100644 --- a/beacon_chain/sync/sync_protocol.nim +++ b/beacon_chain/sync/sync_protocol.nim @@ -49,6 +49,7 @@ proc readChunkPayload*( except CatchableError: return neterr UnexpectedEOF + static: doAssert ConsensusFork.high == ConsensusFork.Electra if contextBytes == peer.network.forkDigests.phase0: let res = await readChunkPayload(conn, peer, phase0.SignedBeaconBlock) if res.isOk: @@ -79,6 +80,12 @@ proc readChunkPayload*( return ok newClone(ForkedSignedBeaconBlock.init(res.get)) else: return err(res.error) + elif contextBytes == peer.network.forkDigests.electra: + let res = await readChunkPayload(conn, peer, electra.SignedBeaconBlock) + if res.isOk: + return ok newClone(ForkedSignedBeaconBlock.init(res.get)) + else: + return err(res.error) else: return neterr InvalidContextBytes