Skip to content

Commit 41f8400

Browse files
authored
fix Electra light client objects; use version-2-0 for Nim again (#6222)
1 parent 2ec82fe commit 41f8400

File tree

6 files changed

+60
-14
lines changed

6 files changed

+60
-14
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
cpu: amd64
3434
- os: windows
3535
cpu: amd64
36-
branch: [~, upstream/version-1-6, v2.0.4]
36+
branch: [~, upstream/version-1-6, upstream/version-2-0]
3737
exclude:
3838
- target:
3939
os: macos
@@ -47,7 +47,7 @@ jobs:
4747
include:
4848
- branch: upstream/version-1-6
4949
branch-short: version-1-6
50-
- branch: v2.0.4
50+
- branch: upstream/version-2-0
5151
branch-short: version-2-0
5252
nimflags-extra: --mm:refc
5353
- target:

beacon_chain/spec/datatypes/electra.nim

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,36 @@
1818
import
1919
std/typetraits,
2020
chronicles,
21-
stew/byteutils,
2221
json_serialization,
2322
ssz_serialization/[merkleization, proofs],
2423
ssz_serialization/types as sszTypes,
2524
../digest,
26-
"."/[base, phase0, altair, bellatrix, capella]
25+
"."/[base, phase0]
2726

2827
from kzg4844 import KzgCommitment, KzgProof
28+
from stew/bitops2 import log2trunc
29+
from stew/byteutils import to0xHex
30+
from ./altair import
31+
EpochParticipationFlags, InactivityScores, SyncAggregate, SyncCommittee,
32+
TrustedSyncAggregate
33+
from ./bellatrix import BloomLogs, ExecutionAddress, Transaction
34+
from ./capella import
35+
HistoricalSummary, SignedBLSToExecutionChangeList, Withdrawal
2936
from ./deneb import Blobs, BlobsBundle, KzgCommitments, KzgProofs
3037

3138
export json_serialization, base, kzg4844
3239

40+
const
41+
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.5/specs/altair/light-client/sync-protocol.md#constants
42+
# All of these indices are rooted in `BeaconState`.
43+
# The first member (`genesis_time`) is 64, subsequent members +1 each.
44+
# If there are ever more than 64 members in `BeaconState`, indices change!
45+
# `FINALIZED_ROOT_GINDEX` is one layer deeper, i.e., `84 * 2 + 1`.
46+
# https://github.com/ethereum/consensus-specs/blob/v1.4.0/ssz/merkle-proofs.md
47+
FINALIZED_ROOT_GINDEX = 169.GeneralizedIndex # finalized_checkpoint > root
48+
CURRENT_SYNC_COMMITTEE_GINDEX = 86.GeneralizedIndex # current_sync_committee
49+
NEXT_SYNC_COMMITTEE_GINDEX = 87.GeneralizedIndex # next_sync_committee
50+
3351
type
3452
# https://github.com/ethereum/consensus-specs/blob/94a0b6c581f2809aa8aca4ef7ee6fbb63f9d74e9/specs/electra/beacon-chain.md#depositreceipt
3553
DepositReceipt* = object
@@ -75,7 +93,7 @@ type
7593
blockValue*: Wei
7694
blobsBundle*: BlobsBundle
7795

78-
# https://github.com/ethereum/consensus-specs/blob/82133085a1295e93394ebdf71df8f2f6e0962588/specs/electra/beacon-chain.md#executionpayloadheader
96+
# https://github.com/ethereum/consensus-specs/blob/v1.5.0-alpha.0/specs/electra/beacon-chain.md#executionpayloadheader
7997
ExecutionPayloadHeader* = object
8098
# Execution block header fields
8199
parent_hash*: Eth2Digest
@@ -141,12 +159,21 @@ type
141159
source_index*: uint64
142160
target_index*: uint64
143161

162+
FinalityBranch =
163+
array[log2trunc(FINALIZED_ROOT_GINDEX), Eth2Digest]
164+
165+
CurrentSyncCommitteeBranch =
166+
array[log2trunc(CURRENT_SYNC_COMMITTEE_GINDEX), Eth2Digest]
167+
168+
NextSyncCommitteeBranch =
169+
array[log2trunc(NEXT_SYNC_COMMITTEE_GINDEX), Eth2Digest]
170+
144171
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.5/specs/capella/light-client/sync-protocol.md#modified-lightclientheader
145172
LightClientHeader* = object
146173
beacon*: BeaconBlockHeader
147174
## Beacon block header
148175

149-
execution*: ExecutionPayloadHeader
176+
execution*: electra.ExecutionPayloadHeader
150177
## Execution payload header corresponding to `beacon.body_root` (from Capella onward)
151178
execution_branch*: capella.ExecutionBranch
152179

@@ -157,7 +184,7 @@ type
157184

158185
current_sync_committee*: SyncCommittee
159186
## Current sync committee corresponding to `header.beacon.state_root`
160-
current_sync_committee_branch*: altair.CurrentSyncCommitteeBranch
187+
current_sync_committee_branch*: CurrentSyncCommitteeBranch
161188

162189
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.5/specs/altair/light-client/sync-protocol.md#lightclientupdate
163190
LightClientUpdate* = object
@@ -167,11 +194,11 @@ type
167194
next_sync_committee*: SyncCommittee
168195
## Next sync committee corresponding to
169196
## `attested_header.beacon.state_root`
170-
next_sync_committee_branch*: altair.NextSyncCommitteeBranch
197+
next_sync_committee_branch*: NextSyncCommitteeBranch
171198

172199
# Finalized header corresponding to `attested_header.beacon.state_root`
173200
finalized_header*: LightClientHeader
174-
finality_branch*: altair.FinalityBranch
201+
finality_branch*: FinalityBranch
175202

176203
sync_aggregate*: SyncAggregate
177204
## Sync committee aggregate signature
@@ -185,7 +212,7 @@ type
185212

186213
# Finalized header corresponding to `attested_header.beacon.state_root`
187214
finalized_header*: LightClientHeader
188-
finality_branch*: altair.FinalityBranch
215+
finality_branch*: FinalityBranch
189216

190217
# Sync committee aggregate signature
191218
sync_aggregate*: SyncAggregate
@@ -405,7 +432,7 @@ type
405432
attester_slashings*:
406433
List[AttesterSlashing, Limit MAX_ATTESTER_SLASHINGS_ELECTRA]
407434
## [Modified in Electra:EIP7549]
408-
attestations*: List[Attestation, Limit MAX_ATTESTATIONS_ELECTRA]
435+
attestations*: List[phase0.Attestation, Limit MAX_ATTESTATIONS_ELECTRA]
409436
## [Modified in Electra:EIP7549]
410437
deposits*: List[Deposit, Limit MAX_DEPOSITS]
411438
voluntary_exits*: List[SignedVoluntaryExit, Limit MAX_VOLUNTARY_EXITS]
@@ -527,6 +554,9 @@ type
527554

528555
root* {.dontSerialize.}: Eth2Digest # cached root of signed beacon block
529556

557+
ElectraCommitteeValidatorsBits* =
558+
BitList[Limit MAX_VALIDATORS_PER_COMMITTEE * MAX_COMMITTEES_PER_SLOT]
559+
530560
SomeSignedBeaconBlock* =
531561
SignedBeaconBlock |
532562
SigVerifiedSignedBeaconBlock |

beacon_chain/spec/eth2_merkleization.nim

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ from ./datatypes/altair import HashedBeaconState, SignedBeaconBlock
2121
from ./datatypes/bellatrix import HashedBeaconState, SignedBeaconBlock
2222
from ./datatypes/capella import HashedBeaconState, SignedBeaconBlock
2323
from ./datatypes/deneb import HashedBeaconState, SignedBeaconBlock
24+
from ./datatypes/electra import HashedBeaconState, SignedBeaconBlock
2425

2526
export ssz_codec, merkleization, proofs
2627

@@ -32,13 +33,13 @@ type
3233
func hash_tree_root*(
3334
x: phase0.HashedBeaconState | altair.HashedBeaconState |
3435
bellatrix.HashedBeaconState | capella.HashedBeaconState |
35-
deneb.HashedBeaconState) {.
36+
deneb.HashedBeaconState | electra.SignedBeaconBlock) {.
3637
error: "HashedBeaconState should not be hashed".}
3738

3839
func hash_tree_root*(
3940
x: phase0.SignedBeaconBlock | altair.SignedBeaconBlock |
4041
bellatrix.SignedBeaconBlock | capella.SignedBeaconBlock |
41-
deneb.SignedBeaconBlock) {.
42+
deneb.SignedBeaconBlock | electra.SignedBeaconBlock) {.
4243
error: "SignedBeaconBlock should not be hashed".}
4344

4445
func depositCountBytes*(x: uint64): array[32, byte] =

beacon_chain/spec/eth2_ssz_serialization.nim

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import
1717
./eth2_merkleization
1818

1919
from ./datatypes/deneb import SignedBeaconBlock, TrustedSignedBeaconBlock
20+
from ./datatypes/electra import SignedBeaconBlock, TrustedSignedBeaconBlock
2021

2122
export phase0, altair, ssz_codec, ssz_serialization, eth2_merkleization
2223

@@ -60,6 +61,12 @@ template readSszBytes*(
6061
template readSszBytes*(
6162
data: openArray[byte], val: var deneb.TrustedSignedBeaconBlock, updateRoot = true) =
6263
readAndUpdateRoot(data, val, updateRoot)
64+
template readSszBytes*(
65+
data: openArray[byte], val: var electra.SignedBeaconBlock, updateRoot = true) =
66+
readAndUpdateRoot(data, val, updateRoot)
67+
template readSszBytes*(
68+
data: openArray[byte], val: var electra.TrustedSignedBeaconBlock, updateRoot = true) =
69+
readAndUpdateRoot(data, val, updateRoot)
6370

6471
template readSszBytes*(
6572
data: openArray[byte], val: var auto, updateRoot: bool) =
@@ -78,4 +85,4 @@ proc fromSszBytes*(
7885
let
7986
key = ValidatorPubKey.fromSszBytes(bytes)
8087

81-
HashedValidatorPubKey.init(key)
88+
HashedValidatorPubKey.init(key)

beacon_chain/spec/forks.nim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,7 @@ template PayloadAttributes*(
617617
static: doAssert ConsensusFork.high == ConsensusFork.Electra,
618618
"eth2_merkleization has been checked and `hash_tree_root` is up to date"
619619

620+
# TODO are these used?
620621
# TODO when https://github.com/nim-lang/Nim/issues/21086 fixed, use return type
621622
# `ref T`
622623
func new*(T: type ForkedHashedBeaconState, data: phase0.BeaconState):

beacon_chain/sync/sync_protocol.nim

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ proc readChunkPayload*(
4949
except CatchableError:
5050
return neterr UnexpectedEOF
5151

52+
static: doAssert ConsensusFork.high == ConsensusFork.Electra
5253
if contextBytes == peer.network.forkDigests.phase0:
5354
let res = await readChunkPayload(conn, peer, phase0.SignedBeaconBlock)
5455
if res.isOk:
@@ -79,6 +80,12 @@ proc readChunkPayload*(
7980
return ok newClone(ForkedSignedBeaconBlock.init(res.get))
8081
else:
8182
return err(res.error)
83+
elif contextBytes == peer.network.forkDigests.electra:
84+
let res = await readChunkPayload(conn, peer, electra.SignedBeaconBlock)
85+
if res.isOk:
86+
return ok newClone(ForkedSignedBeaconBlock.init(res.get))
87+
else:
88+
return err(res.error)
8289
else:
8390
return neterr InvalidContextBytes
8491

0 commit comments

Comments
 (0)