Skip to content

Commit

Permalink
extent BeaconStateFork enum (#4396)
Browse files Browse the repository at this point in the history
  • Loading branch information
tersec authored Dec 7, 2022
1 parent d30cb8b commit 2932d3b
Show file tree
Hide file tree
Showing 24 changed files with 231 additions and 38 deletions.
7 changes: 5 additions & 2 deletions ConsensusSpecPreset-mainnet.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,9 @@ ConsensusSpecPreset-mainnet
+ Light client - Single merkle proof - mainnet/capella/light_client/single_merkle_proof/Beac OK
+ Light client - Single merkle proof - mainnet/capella/light_client/single_merkle_proof/Beac OK
+ Light client - Single merkle proof - mainnet/capella/light_client/single_merkle_proof/Beac OK
+ Light client - Single merkle proof - mainnet/eip4844/light_client/single_merkle_proof/Beac OK
+ Light client - Single merkle proof - mainnet/eip4844/light_client/single_merkle_proof/Beac OK
+ Light client - Single merkle proof - mainnet/eip4844/light_client/single_merkle_proof/Beac OK
+ Slots - double_empty_epoch OK
+ Slots - empty_epoch OK
+ Slots - over_epoch_boundary OK
Expand Down Expand Up @@ -647,7 +650,7 @@ ConsensusSpecPreset-mainnet
+ fork_random_low_balances OK
+ fork_random_misc_balances OK
```
OK: 636/644 Fail: 0/644 Skip: 8/644
OK: 639/647 Fail: 0/647 Skip: 8/647
## Attestation
```diff
+ [Invalid] EF - Altair - Operations - Attestation - after_epoch_slots OK
Expand Down Expand Up @@ -2322,4 +2325,4 @@ OK: 63/63 Fail: 0/63 Skip: 0/63
OK: 51/51 Fail: 0/51 Skip: 0/51

---TOTAL---
OK: 2028/2036 Fail: 0/2036 Skip: 8/2036
OK: 2031/2039 Fail: 0/2039 Skip: 8/2039
7 changes: 5 additions & 2 deletions ConsensusSpecPreset-minimal.md
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,9 @@ ConsensusSpecPreset-minimal
+ Light client - Single merkle proof - minimal/capella/light_client/single_merkle_proof/Beac OK
+ Light client - Single merkle proof - minimal/capella/light_client/single_merkle_proof/Beac OK
+ Light client - Single merkle proof - minimal/capella/light_client/single_merkle_proof/Beac OK
+ Light client - Single merkle proof - minimal/eip4844/light_client/single_merkle_proof/Beac OK
+ Light client - Single merkle proof - minimal/eip4844/light_client/single_merkle_proof/Beac OK
+ Light client - Single merkle proof - minimal/eip4844/light_client/single_merkle_proof/Beac OK
+ Light client - Sync - minimal/altair/light_client/sync/pyspec_tests/advance_finality_witho OK
+ Light client - Sync - minimal/altair/light_client/sync/pyspec_tests/light_client_sync OK
+ Light client - Sync - minimal/altair/light_client/sync/pyspec_tests/supply_sync_committee_ OK
Expand Down Expand Up @@ -729,7 +732,7 @@ ConsensusSpecPreset-minimal
+ fork_random_low_balances OK
+ fork_random_misc_balances OK
```
OK: 718/726 Fail: 0/726 Skip: 8/726
OK: 721/729 Fail: 0/729 Skip: 8/729
## Attestation
```diff
+ [Invalid] EF - Altair - Operations - Attestation - after_epoch_slots OK
Expand Down Expand Up @@ -2482,4 +2485,4 @@ OK: 68/68 Fail: 0/68 Skip: 0/68
OK: 52/52 Fail: 0/52 Skip: 0/52

---TOTAL---
OK: 2172/2180 Fail: 0/2180 Skip: 8/2180
OK: 2175/2183 Fail: 0/2183 Skip: 8/2183
17 changes: 12 additions & 5 deletions beacon_chain/beacon_chain_db.nim
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,8 @@ proc new*(T: type BeaconChainDB,
kvStore db.openKvStore("state_no_validators2").expectDb(),
kvStore db.openKvStore("altair_state_no_validators").expectDb(),
kvStore db.openKvStore("bellatrix_state_no_validators").expectDb(),
kvStore db.openKvStore("capella_state_no_validators").expectDb()]
kvStore db.openKvStore("capella_state_no_validators").expectDb(),
kvStore db.openKvStore("eip4844_state_no_validators").expectDb()]

stateDiffs = kvStore db.openKvStore("state_diffs").expectDb()
summaries = kvStore db.openKvStore("beacon_block_summaries", true).expectDb()
Expand Down Expand Up @@ -759,7 +760,8 @@ proc putBlock*(
proc putBlock*(
db: BeaconChainDB,
value: bellatrix.TrustedSignedBeaconBlock |
capella.TrustedSignedBeaconBlock) =
capella.TrustedSignedBeaconBlock |
eip4844.TrustedSignedBeaconBlock) =
db.withManyWrites:
db.blocks[type(value).toFork].putSZSSZ(value.root.data, value)
db.putBeaconBlockSummary(value.root, value.message.toBeaconBlockSummary())
Expand Down Expand Up @@ -794,6 +796,10 @@ template toBeaconStateNoImmutableValidators(state: capella.BeaconState):
CapellaBeaconStateNoImmutableValidators =
isomorphicCast[CapellaBeaconStateNoImmutableValidators](state)

template toBeaconStateNoImmutableValidators(state: eip4844.BeaconState):
EIP4844BeaconStateNoImmutableValidators =
isomorphicCast[EIP4844BeaconStateNoImmutableValidators](state)

proc putState*(
db: BeaconChainDB, key: Eth2Digest,
value: phase0.BeaconState | altair.BeaconState) =
Expand All @@ -803,7 +809,7 @@ proc putState*(

proc putState*(
db: BeaconChainDB, key: Eth2Digest,
value: bellatrix.BeaconState | capella.BeaconState) =
value: bellatrix.BeaconState | capella.BeaconState | eip4844.BeaconState) =
db.updateImmutableValidators(value.validators.asSeq())
db.statesNoVal[type(value).toFork()].putSZSSZ(
key.data, toBeaconStateNoImmutableValidators(value))
Expand Down Expand Up @@ -1105,7 +1111,8 @@ proc getStateOnlyMutableValidators(
proc getStateOnlyMutableValidators(
immutableValidators: openArray[ImmutableValidatorData2],
store: KvStoreRef, key: openArray[byte],
output: var (bellatrix.BeaconState | capella.BeaconState),
output: var (bellatrix.BeaconState | capella.BeaconState |
eip4844.BeaconState),
rollback: RollbackProc): bool =
## Load state into `output` - BeaconState is large so we want to avoid
## re-allocating it if possible
Expand Down Expand Up @@ -1197,7 +1204,7 @@ proc getState*(
proc getState*(
db: BeaconChainDB, key: Eth2Digest,
output: var (altair.BeaconState | bellatrix.BeaconState |
capella.BeaconState),
capella.BeaconState | eip4844.BeaconState),
rollback: RollbackProc): bool =
## Load state into `output` - BeaconState is large so we want to avoid
## re-allocating it if possible
Expand Down
67 changes: 67 additions & 0 deletions beacon_chain/beacon_chain_db_immutable.nim
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import
./spec/[eth2_ssz_serialization, eth2_merkleization]

from ./spec/datatypes/capella import ExecutionPayloadHeader, Withdrawal
from ./spec/datatypes/eip4844 import ExecutionPayloadHeader

type
# https://github.com/ethereum/consensus-specs/blob/v1.3.0-alpha.1/specs/phase0/beacon-chain.md#beaconstate
Expand Down Expand Up @@ -186,6 +187,7 @@ type
latest_execution_payload_header*: bellatrix.ExecutionPayloadHeader # [New in Bellatrix]

# https://github.com/ethereum/consensus-specs/blob/v1.3.0-alpha.1/specs/capella/beacon-chain.md#beaconstate
# with indirect changes via ExecutionPayload
# Memory-representation-equivalent to a Capella BeaconState for in-place SSZ
# reading and writing
CapellaBeaconStateNoImmutableValidators* = object
Expand Down Expand Up @@ -248,3 +250,68 @@ type
# Withdrawals
next_withdrawal_index*: WithdrawalIndex # [New in Capella]
next_withdrawal_validator_index*: uint64 # [New in Capella]

# https://github.com/ethereum/consensus-specs/blob/v1.3.0-alpha.1/specs/capella/beacon-chain.md#beaconstate
# with indirect changes via ExecutionPayloadHeader
# Memory-representation-equivalent to a Capella BeaconState for in-place SSZ
# reading and writing
EIP4844BeaconStateNoImmutableValidators* = object
# Versioning
genesis_time*: uint64
genesis_validators_root*: Eth2Digest
slot*: Slot
fork*: Fork

# History
latest_block_header*: BeaconBlockHeader
## `latest_block_header.state_root == ZERO_HASH` temporarily

block_roots*: HashArray[Limit SLOTS_PER_HISTORICAL_ROOT, Eth2Digest]
## Needed to process attestations, older to newer

state_roots*: HashArray[Limit SLOTS_PER_HISTORICAL_ROOT, Eth2Digest]
historical_roots*: HashList[Eth2Digest, Limit HISTORICAL_ROOTS_LIMIT]

# Eth1
eth1_data*: Eth1Data
eth1_data_votes*:
HashList[Eth1Data, Limit(EPOCHS_PER_ETH1_VOTING_PERIOD * SLOTS_PER_EPOCH)]
eth1_deposit_index*: uint64

# Registry
validators*: HashList[ValidatorStatus, Limit VALIDATOR_REGISTRY_LIMIT]
balances*: HashList[Gwei, Limit VALIDATOR_REGISTRY_LIMIT]

# Randomness
randao_mixes*: HashArray[Limit EPOCHS_PER_HISTORICAL_VECTOR, Eth2Digest]

# Slashings
slashings*: HashArray[Limit EPOCHS_PER_SLASHINGS_VECTOR, Gwei]
## Per-epoch sums of slashed effective balances

# Participation
previous_epoch_participation*: EpochParticipationFlags
current_epoch_participation*: EpochParticipationFlags

# Finality
justification_bits*: JustificationBits

previous_justified_checkpoint*: Checkpoint
## Previous epoch snapshot

current_justified_checkpoint*: Checkpoint
finalized_checkpoint*: Checkpoint

# Inactivity
inactivity_scores*: HashList[uint64, Limit VALIDATOR_REGISTRY_LIMIT]

# Light client sync committees
current_sync_committee*: SyncCommittee
next_sync_committee*: SyncCommittee

# Execution
latest_execution_payload_header*: eip4844.ExecutionPayloadHeader

# Withdrawals
next_withdrawal_index*: WithdrawalIndex
next_withdrawal_validator_index*: uint64
16 changes: 13 additions & 3 deletions beacon_chain/consensus_object_pools/attestation_pool.nim
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ proc init*(T: type AttestationPool, dag: ChainDAGRef,
var unrealized: FinalityCheckpoints
if enableTestFeatures in dag.updateFlags and blckRef == dag.head:
unrealized = withState(dag.headState):
when stateFork >= BeaconStateFork.Altair:
when stateFork == BeaconStateFork.EIP4844:
raiseAssert $eip4844ImplementationMissing & ": attestation_pool.nim:init"
elif stateFork >= BeaconStateFork.Altair:
forkyState.data.compute_unrealized_finality()
else:
var cache: StateCache
Expand Down Expand Up @@ -558,6 +560,8 @@ proc check_attestation_compatible*(

ok()

from ../spec/datatypes/eip4844 import HashedBeaconState

proc getAttestationsForBlock*(pool: var AttestationPool,
state: ForkyHashedBeaconState,
cache: var StateCache): seq[Attestation] =
Expand All @@ -584,6 +588,10 @@ proc getAttestationsForBlock*(pool: var AttestationPool,
elif state is altair.HashedBeaconState or state is bellatrix.HashedBeaconState or
state is capella.HashedBeaconState:
AttestationCache.init(state, cache)
elif state is eip4844.HashedBeaconState:
if true:
raiseAssert $eip4844ImplementationMissing & ": attestation_pool.nim:getAttestationsForBlock"
default(AttestationCache)
else:
static: doAssert false

Expand Down Expand Up @@ -643,8 +651,10 @@ proc getAttestationsForBlock*(pool: var AttestationPool,
var
prevEpoch = state.data.get_previous_epoch()
prevEpochSpace =
when state is altair.HashedBeaconState or state is bellatrix.HashedBeaconState or
state is capella.HashedBeaconState:
when state is altair.HashedBeaconState or
state is bellatrix.HashedBeaconState or
state is capella.HashedBeaconState or
state is eip4844.HashedBeaconState:
MAX_ATTESTATIONS
elif state is phase0.HashedBeaconState:
state.data.previous_epoch_attestations.maxLen -
Expand Down
6 changes: 4 additions & 2 deletions beacon_chain/consensus_object_pools/block_clearance.nim
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,10 @@ proc addResolvedHeadBlock(
var unrealized: FinalityCheckpoints
if enableTestFeatures in dag.updateFlags:
unrealized = withState(state):
static: doAssert high(BeaconStateFork) == BeaconStateFork.Capella
when stateFork >= BeaconStateFork.Altair:
static: doAssert high(BeaconStateFork) == BeaconStateFork.EIP4844
when stateFork == BeaconStateFork.EIP4844:
raiseAssert $eip4844ImplementationMissing & ": block_clearance.nim:addResolvedHeadBlock"
elif stateFork >= BeaconStateFork.Altair:
forkyState.data.compute_unrealized_finality()
else:
forkyState.data.compute_unrealized_finality(cache)
Expand Down
1 change: 1 addition & 0 deletions beacon_chain/consensus_object_pools/blockchain_dag.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,7 @@ proc init*(T: type ChainDAGRef, cfg: RuntimeConfig, db: BeaconChainDB,
of BeaconStateFork.Altair: altairFork(cfg)
of BeaconStateFork.Bellatrix: bellatrixFork(cfg)
of BeaconStateFork.Capella: capellaFork(cfg)
of BeaconStateFork.EIP4844: eip4844Fork(cfg)
stateFork = getStateField(dag.headState, fork)

if stateFork != configFork:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,20 @@ import
"."/[block_pools_types, blockchain_dag]

from ../spec/datatypes/capella import TrustedSignedBeaconBlock
from ../spec/datatypes/eip4844 import
HashedBeaconState, TrustedSignedBeaconBlock

logScope: topics = "chaindag_lc"

type
HashedBeaconStateWithSyncCommittee =
eip4844.HashedBeaconState |
capella.HashedBeaconState |
bellatrix.HashedBeaconState |
altair.HashedBeaconState

TrustedSignedBeaconBlockWithSyncAggregate =
eip4844.TrustedSignedBeaconBlock |
capella.TrustedSignedBeaconBlock |
bellatrix.TrustedSignedBeaconBlock |
altair.TrustedSignedBeaconBlock
Expand Down Expand Up @@ -218,6 +222,7 @@ proc initLightClientBootstrapForPeriod(
res

from ../spec/datatypes/capella import asSigned
from ../spec/datatypes/eip4844 import asSigned

proc initLightClientUpdateForPeriod(
dag: ChainDAGRef, period: SyncCommitteePeriod): Opt[void] =
Expand Down
2 changes: 2 additions & 0 deletions beacon_chain/era_db.nim
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,8 @@ iterator getBlockIds*(
state = nil # No `return` in iterators
of BeaconStateFork.Capella:
raiseAssert $capellaImplementationMissing
of BeaconStateFork.EIP4844:
raiseAssert $eip4844ImplementationMissing

if state == nil:
break
Expand Down
7 changes: 6 additions & 1 deletion beacon_chain/nimbus_beacon_node.nim
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,8 @@ func forkDigests(node: BeaconNode): auto =
node.dag.forkDigests.phase0,
node.dag.forkDigests.altair,
node.dag.forkDigests.bellatrix,
node.dag.forkDigests.capella]
node.dag.forkDigests.capella,
node.dag.forkDigests.eip4844]
forkDigestsArray

# https://github.com/ethereum/consensus-specs/blob/v1.3.0-alpha.1/specs/phase0/validator.md#phase-0-attestation-subnet-stability
Expand Down Expand Up @@ -1050,20 +1051,24 @@ proc updateGossipStatus(node: BeaconNode, slot: Slot) {.async.} =

let forkDigests = node.forkDigests()

discard $eip4844ImplementationMissing & "nimbus_beacon_node.nim:updateGossipStatus check EIP4844 removeMessageHandlers"
const removeMessageHandlers: array[BeaconStateFork, auto] = [
removePhase0MessageHandlers,
removeAltairMessageHandlers,
removeAltairMessageHandlers, # with different forkDigest
removeCapellaMessageHandlers,
removeCapellaMessageHandlers
]

for gossipFork in oldGossipForks:
removeMessageHandlers[gossipFork](node, forkDigests[gossipFork])

discard $eip4844ImplementationMissing & "nimbus_beacon_node.nim:updateGossipStatus check EIP4844 message addMessageHandlers"
const addMessageHandlers: array[BeaconStateFork, auto] = [
addPhase0MessageHandlers,
addAltairMessageHandlers,
addAltairMessageHandlers, # with different forkDigest
addCapellaMessageHandlers,
addCapellaMessageHandlers
]

Expand Down
2 changes: 2 additions & 0 deletions beacon_chain/rpc/rest_beacon_api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,8 @@ proc installBeaconApiHandlers*(router: var RestRouter, node: BeaconNode) =
return RestApiResponse.jsonError(Http400, BlockIncorrectFork)

case currentEpochFork
of BeaconStateFork.EIP4844:
return RestApiResponse.jsonError(Http500, $eip4844ImplementationMissing)
of BeaconStateFork.Capella:
return RestApiResponse.jsonError(Http500, $capellaImplementationMissing)
of BeaconStateFork.Bellatrix:
Expand Down
2 changes: 2 additions & 0 deletions beacon_chain/rpc/rest_utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,8 @@ proc getStateOptimistic*(node: BeaconNode,
case state.kind
of BeaconStateFork.Phase0, BeaconStateFork.Altair:
some[bool](false)
of BeaconStateFork.EIP4844:
raiseAssert $eip4844ImplementationMissing & "rest_utils.nim:getStateOptimistic"
of BeaconStateFork.Bellatrix, BeaconStateFork.Capella:
# A state is optimistic iff the block which created it is
withState(state):
Expand Down
11 changes: 11 additions & 0 deletions beacon_chain/spec/beaconstate.nim
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,17 @@ func get_initial_beacon_block*(state: bellatrix.HashedBeaconState):
bellatrix.TrustedSignedBeaconBlock(
message: message, root: hash_tree_root(message))

func get_initial_beacon_block*(state: eip4844.HashedBeaconState):
eip4844.TrustedSignedBeaconBlock =
# The genesis block is implicitly trusted
let message = eip4844.TrustedBeaconBlock(
slot: state.data.slot,
state_root: state.root)
# parent_root, randao_reveal, eth1_data, signature, and body automatically
# initialized to default values.
eip4844.TrustedSignedBeaconBlock(
message: message, root: hash_tree_root(message))

# https://github.com/ethereum/consensus-specs/blob/v1.1.7/specs/merge/beacon-chain.md#testing
func get_initial_beacon_block*(state: capella.HashedBeaconState):
capella.TrustedSignedBeaconBlock =
Expand Down
Loading

0 comments on commit 2932d3b

Please sign in to comment.