Skip to content

Commit

Permalink
fix Electra light client objects; use version-2-0 for Nim again (#6222)
Browse files Browse the repository at this point in the history
  • Loading branch information
tersec authored Apr 19, 2024
1 parent 2ec82fe commit 41f8400
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 14 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
48 changes: 39 additions & 9 deletions beacon_chain/spec/datatypes/electra.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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 |
Expand Down
5 changes: 3 additions & 2 deletions beacon_chain/spec/eth2_merkleization.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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] =
Expand Down
9 changes: 8 additions & 1 deletion beacon_chain/spec/eth2_ssz_serialization.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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) =
Expand All @@ -78,4 +85,4 @@ proc fromSszBytes*(
let
key = ValidatorPubKey.fromSszBytes(bytes)

HashedValidatorPubKey.init(key)
HashedValidatorPubKey.init(key)
1 change: 1 addition & 0 deletions beacon_chain/spec/forks.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
7 changes: 7 additions & 0 deletions beacon_chain/sync/sync_protocol.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 41f8400

Please sign in to comment.