Skip to content

Commit

Permalink
fulu
Browse files Browse the repository at this point in the history
  • Loading branch information
agnxsh committed Oct 30, 2024
1 parent 18409a6 commit 3240299
Show file tree
Hide file tree
Showing 47 changed files with 2,156 additions and 313 deletions.
35 changes: 24 additions & 11 deletions beacon_chain/beacon_chain_db.nim
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,8 @@ proc new*(T: type BeaconChainDB,
kvStore db.openKvStore("bellatrix_blocks").expectDb(),
kvStore db.openKvStore("capella_blocks").expectDb(),
kvStore db.openKvStore("deneb_blocks").expectDb(),
kvStore db.openKvStore("electra_blocks").expectDb()]
kvStore db.openKvStore("electra_blocks").expectDb(),
kvStore db.openKvStore("fulu_blocks").expectDb()]

stateRoots = kvStore db.openKvStore("state_roots", true).expectDb()

Expand All @@ -524,7 +525,8 @@ proc new*(T: type BeaconChainDB,
kvStore db.openKvStore("bellatrix_state_no_validators").expectDb(),
kvStore db.openKvStore("capella_state_no_validator_pubkeys").expectDb(),
kvStore db.openKvStore("deneb_state_no_validator_pubkeys").expectDb(),
kvStore db.openKvStore("electra_state_no_validator_pubkeys").expectDb()]
kvStore db.openKvStore("electra_state_no_validator_pubkeys").expectDb(),
kvStore db.openKvStore("fulu_state_no_validator_pubkeys").expectDb()]

stateDiffs = kvStore db.openKvStore("state_diffs").expectDb()
summaries = kvStore db.openKvStore("beacon_block_summaries", true).expectDb()
Expand Down Expand Up @@ -805,7 +807,7 @@ proc putBlock*(
db: BeaconChainDB,
value: bellatrix.TrustedSignedBeaconBlock |
capella.TrustedSignedBeaconBlock | deneb.TrustedSignedBeaconBlock |
electra.TrustedSignedBeaconBlock) =
electra.TrustedSignedBeaconBlock | fulu.TrustedSignedBeaconBlock) =
db.withManyWrites:
db.blocks[type(value).kind].putSZSSZ(value.root.data, value)
db.putBeaconBlockSummary(value.root, value.message.toBeaconBlockSummary())
Expand Down Expand Up @@ -859,6 +861,10 @@ template toBeaconStateNoImmutableValidators(state: electra.BeaconState):
ElectraBeaconStateNoImmutableValidators =
isomorphicCast[ElectraBeaconStateNoImmutableValidators](state)

template toBeaconStateNoImmutableValidators(state: fulu.BeaconState):
FuluBeaconStateNoImmutableValidators =
isomorphicCast[FuluBeaconStateNoImmutableValidators](state)

proc putState*(
db: BeaconChainDB, key: Eth2Digest,
value: phase0.BeaconState | altair.BeaconState) =
Expand All @@ -869,7 +875,7 @@ proc putState*(
proc putState*(
db: BeaconChainDB, key: Eth2Digest,
value: bellatrix.BeaconState | capella.BeaconState | deneb.BeaconState |
electra.BeaconState) =
electra.BeaconState | fulu.BeaconState) =
db.updateImmutableValidators(value.validators.asSeq())
db.statesNoVal[type(value).kind].putSZSSZ(
key.data, toBeaconStateNoImmutableValidators(value))
Expand Down Expand Up @@ -998,7 +1004,8 @@ proc getBlock*(

proc getBlock*[
X: bellatrix.TrustedSignedBeaconBlock | capella.TrustedSignedBeaconBlock |
deneb.TrustedSignedBeaconBlock | electra.TrustedSignedBeaconBlock](
deneb.TrustedSignedBeaconBlock | electra.TrustedSignedBeaconBlock |
fulu.TrustedSignedBeaconBlock](
db: BeaconChainDB, key: Eth2Digest,
T: type X): Opt[T] =
# We only store blocks that we trust in the database
Expand Down Expand Up @@ -1053,7 +1060,8 @@ proc getBlockSSZ*(

proc getBlockSSZ*[
X: bellatrix.TrustedSignedBeaconBlock | capella.TrustedSignedBeaconBlock |
deneb.TrustedSignedBeaconBlock | electra.TrustedSignedBeaconBlock](
deneb.TrustedSignedBeaconBlock | electra.TrustedSignedBeaconBlock |
fulu.TrustedSignedBeaconBlock](
db: BeaconChainDB, key: Eth2Digest, data: var seq[byte], T: type X): bool =
let dataPtr = addr data # Short-lived
var success = true
Expand Down Expand Up @@ -1102,7 +1110,8 @@ proc getBlockSZ*(

proc getBlockSZ*[
X: bellatrix.TrustedSignedBeaconBlock | capella.TrustedSignedBeaconBlock |
deneb.TrustedSignedBeaconBlock | electra.TrustedSignedBeaconBlock](
deneb.TrustedSignedBeaconBlock | electra.TrustedSignedBeaconBlock |
fulu.TrustedSignedBeaconBlock](
db: BeaconChainDB, key: Eth2Digest, data: var seq[byte], T: type X): bool =
let dataPtr = addr data # Short-lived
func decode(data: openArray[byte]) =
Expand Down Expand Up @@ -1200,7 +1209,8 @@ proc getStateOnlyMutableValidators(
proc getStateOnlyMutableValidators(
immutableValidators: openArray[ImmutableValidatorData2],
store: KvStoreRef, key: openArray[byte],
output: var (capella.BeaconState | deneb.BeaconState | electra.BeaconState),
output: var (capella.BeaconState | deneb.BeaconState | electra.BeaconState |
fulu.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 @@ -1285,7 +1295,8 @@ proc getState*(
proc getState*(
db: BeaconChainDB, key: Eth2Digest,
output: var (altair.BeaconState | bellatrix.BeaconState |
capella.BeaconState | deneb.BeaconState | electra.BeaconState),
capella.BeaconState | deneb.BeaconState | electra.BeaconState |
fulu.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 @@ -1365,7 +1376,7 @@ proc containsBlock*(
proc containsBlock*[
X: altair.TrustedSignedBeaconBlock | bellatrix.TrustedSignedBeaconBlock |
capella.TrustedSignedBeaconBlock | deneb.TrustedSignedBeaconBlock |
electra.TrustedSignedBeaconBlock](
electra.TrustedSignedBeaconBlock | fulu.TrustedSignedBeaconBlock](
db: BeaconChainDB, key: Eth2Digest, T: type X): bool =
db.blocks[X.kind].contains(key.data).expectDb()

Expand Down Expand Up @@ -1506,7 +1517,7 @@ iterator getAncestorSummaries*(db: BeaconChainDB, root: Eth2Digest):

# Backwards compat for reading old databases, or those that for whatever
# reason lost a summary along the way..
static: doAssert ConsensusFork.high == ConsensusFork.Electra
static: doAssert ConsensusFork.high == ConsensusFork.Fulu
while true:
if db.v0.backend.getSnappySSZ(
subkey(BeaconBlockSummary, res.root), res.summary) == GetResult.found:
Expand All @@ -1523,6 +1534,8 @@ iterator getAncestorSummaries*(db: BeaconChainDB, root: Eth2Digest):
res.summary = blck.get().message.toBeaconBlockSummary()
elif (let blck = db.getBlock(res.root, electra.TrustedSignedBeaconBlock); blck.isSome()):
res.summary = blck.get().message.toBeaconBlockSummary()
elif (let blck = db.getBlock(res.root, fulu.TrustedSignedBeaconBlock); blck.isSome()):
res.summary = blck.get().message.toBeaconBlockSummary()
else:
break

Expand Down
86 changes: 86 additions & 0 deletions beacon_chain/beacon_chain_db_immutable.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ from ./spec/datatypes/deneb import ExecutionPayloadHeader
from ./spec/datatypes/electra import
ExecutionPayloadHeader, PendingConsolidation, PendingDeposit,
PendingPartialWithdrawal
from ./spec/datatypes/fulu import
ExecutionPayloadHeader

type
# https://github.com/ethereum/consensus-specs/blob/v1.4.0/specs/phase0/beacon-chain.md#beaconstate
Expand Down Expand Up @@ -416,3 +418,87 @@ type
pending_consolidations*:
HashList[PendingConsolidation, Limit PENDING_CONSOLIDATIONS_LIMIT]
## [New in Electra:EIP7251]

# Memory-representation-equivalent to a Fulu BeaconState for in-place SSZ
# reading and writing
FuluBeaconStateNoImmutableValidators* = 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]
## Frozen in Capella, replaced by historical_summaries

# 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[ValidatorStatusCapella, 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
## Bit set for every recent justified epoch

previous_justified_checkpoint*: Checkpoint
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*: fulu.ExecutionPayloadHeader

# Withdrawals
next_withdrawal_index*: WithdrawalIndex
next_withdrawal_validator_index*: uint64

# Deep history valid from Capella onwards
historical_summaries*:
HashList[HistoricalSummary, Limit HISTORICAL_ROOTS_LIMIT]

deposit_requests_start_index*: uint64 # [New in Electra:EIP6110]
deposit_balance_to_consume*: Gwei # [New in Electra:EIP7251]
exit_balance_to_consume*: Gwei # [New in Electra:EIP7251]
earliest_exit_epoch*: Epoch # [New in Electra:EIP7251]
consolidation_balance_to_consume*: Gwei # [New in Electra:EIP7251]
earliest_consolidation_epoch*: Epoch # [New in Electra:EIP7251]
pending_deposits*: HashList[PendingDeposit, Limit PENDING_DEPOSITS_LIMIT]
## [New in Electra:EIP7251]

# [New in Electra:EIP7251]
pending_partial_withdrawals*:
HashList[PendingPartialWithdrawal, Limit PENDING_PARTIAL_WITHDRAWALS_LIMIT]
pending_consolidations*:
HashList[PendingConsolidation, Limit PENDING_CONSOLIDATIONS_LIMIT]
## [New in Electra:EIP7251]
2 changes: 2 additions & 0 deletions beacon_chain/beacon_chain_file.nim
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ func getBlobForkCode(fork: ConsensusFork): uint64 =
uint64(MaxForksCount)
of ConsensusFork.Electra:
uint64(MaxForksCount) + uint64(fork) - uint64(ConsensusFork.Deneb)
of ConsensusFork.Fulu:
uint64(MaxForksCount) + uint64(fork) - uint64(ConsensusFork.Electra)
of ConsensusFork.Phase0 .. ConsensusFork.Capella:
raiseAssert "Blobs are not supported for the fork"

Expand Down
5 changes: 3 additions & 2 deletions beacon_chain/consensus_object_pools/attestation_pool.nim
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ func init(
T: type AttestationCache,
state: altair.HashedBeaconState | bellatrix.HashedBeaconState |
capella.HashedBeaconState | deneb.HashedBeaconState |
electra.HashedBeaconState,
electra.HashedBeaconState | fulu.HashedBeaconState,
cache: var StateCache): T =
# Load attestations that are scheduled for being given rewards for
let
Expand Down Expand Up @@ -861,7 +861,8 @@ proc getAttestationsForBlock*(pool: var AttestationPool,
default(seq[phase0.Attestation])

proc getElectraAttestationsForBlock*(
pool: var AttestationPool, state: electra.HashedBeaconState,
pool: var AttestationPool,
state: electra.HashedBeaconState | fulu.HashedBeaconState,
cache: var StateCache): seq[electra.Attestation] =
let newBlockSlot = state.data.slot.uint64

Expand Down
11 changes: 8 additions & 3 deletions beacon_chain/consensus_object_pools/blob_quarantine.nim
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ func hasBlob*(

func popBlobs*(
quarantine: var BlobQuarantine, digest: Eth2Digest,
blck: deneb.SignedBeaconBlock | electra.SignedBeaconBlock):
blck: deneb.SignedBeaconBlock | electra.SignedBeaconBlock |
fulu.SignedBeaconBlock):
seq[ref BlobSidecar] =
var r: seq[ref BlobSidecar] = @[]
for idx, kzg_commitment in blck.message.body.blob_kzg_commitments:
Expand All @@ -82,14 +83,18 @@ func popBlobs*(
r

func hasBlobs*(quarantine: BlobQuarantine,
blck: deneb.SignedBeaconBlock | electra.SignedBeaconBlock): bool =
blck: deneb.SignedBeaconBlock | electra.SignedBeaconBlock |
fulu.SignedBeaconBlock): bool =
# Having a fulu SignedBeaconBlock is incorrect atm, but
# shall be fixed once data columns are rebased to fulu
for idx, kzg_commitment in blck.message.body.blob_kzg_commitments:
if (blck.root, BlobIndex idx, kzg_commitment) notin quarantine.blobs:
return false
true

func blobFetchRecord*(quarantine: BlobQuarantine,
blck: deneb.SignedBeaconBlock | electra.SignedBeaconBlock): BlobFetchRecord =
blck: deneb.SignedBeaconBlock | electra.SignedBeaconBlock |
fulu.SignedBeaconBlock): BlobFetchRecord =
var indices: seq[BlobIndex]
for i in 0..<len(blck.message.body.blob_kzg_commitments):
let idx = BlobIndex(i)
Expand Down
4 changes: 3 additions & 1 deletion beacon_chain/consensus_object_pools/block_clearance.nim
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,9 @@ proc addBackfillBlock*(
ok()

template BlockAdded(kind: static ConsensusFork): untyped =
when kind == ConsensusFork.Electra:
when kind == ConsensusFork.Fulu:
OnFuluBlockAdded
elif kind == ConsensusFork.Electra:
OnElectraBlockAdded
elif kind == ConsensusFork.Deneb:
OnDenebBlockAdded
Expand Down
3 changes: 2 additions & 1 deletion beacon_chain/consensus_object_pools/block_dag.nim
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ func init*(
blck: bellatrix.SomeBeaconBlock | bellatrix.TrustedBeaconBlock |
capella.SomeBeaconBlock | capella.TrustedBeaconBlock |
deneb.SomeBeaconBlock | deneb.TrustedBeaconBlock |
electra.SomeBeaconBlock | electra.TrustedBeaconBlock): BlockRef =
electra.SomeBeaconBlock | electra.TrustedBeaconBlock |
fulu.SomeBeaconBlock | fulu.TrustedBeaconBlock): BlockRef =
BlockRef.init(
root, Opt.some blck.body.execution_payload.block_hash,
executionValid =
Expand Down
6 changes: 5 additions & 1 deletion beacon_chain/consensus_object_pools/block_pools_types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,12 @@ type
OnCapellaBlockAdded* = OnBlockAdded[capella.TrustedSignedBeaconBlock]
OnDenebBlockAdded* = OnBlockAdded[deneb.TrustedSignedBeaconBlock]
OnElectraBlockAdded* = OnBlockAdded[electra.TrustedSignedBeaconBlock]
OnFuluBlockAdded* = OnBlockAdded[fulu.TrustedSignedBeaconBlock]

OnForkyBlockAdded* =
OnPhase0BlockAdded | OnAltairBlockAdded | OnBellatrixBlockAdded |
OnCapellaBlockAdded | OnDenebBlockAdded | OnElectraBlockAdded
OnCapellaBlockAdded | OnDenebBlockAdded | OnElectraBlockAdded |
OnFuluBlockAdded

OnForkedBlockAdded* = proc(
blckRef: BlockRef, blck: ForkedTrustedSignedBeaconBlock, epochRef: EpochRef,
Expand Down Expand Up @@ -340,6 +342,8 @@ type
optimistic* {.serializedFieldName: "execution_optimistic".}: Option[bool]

template OnBlockAddedCallback*(kind: static ConsensusFork): auto =
when kind == ConsensusFork.Fulu:
typedesc[OnFuluBlockAdded]
when kind == ConsensusFork.Electra:
typedesc[OnElectraBlockAdded]
elif kind == ConsensusFork.Deneb:
Expand Down
3 changes: 2 additions & 1 deletion beacon_chain/consensus_object_pools/block_quarantine.nim
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,8 @@ iterator pop*(quarantine: var Quarantine, root: Eth2Digest):

proc addBlobless*(
quarantine: var Quarantine, finalizedSlot: Slot,
signedBlock: deneb.SignedBeaconBlock | electra.SignedBeaconBlock): bool =
signedBlock: deneb.SignedBeaconBlock | electra.SignedBeaconBlock |
fulu.SignedBeaconBlock): bool =

if not isViable(finalizedSlot, signedBlock.message.slot):
quarantine.addUnviable(signedBlock.root)
Expand Down
16 changes: 14 additions & 2 deletions beacon_chain/consensus_object_pools/blockchain_dag.nim
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,11 @@ proc getForkedBlock*(db: BeaconChainDB, root: Eth2Digest):
Opt[ForkedTrustedSignedBeaconBlock] =
# When we only have a digest, we don't know which fork it's from so we try
# them one by one - this should be used sparingly
static: doAssert high(ConsensusFork) == ConsensusFork.Electra
if (let blck = db.getBlock(root, electra.TrustedSignedBeaconBlock);
static: doAssert high(ConsensusFork) == ConsensusFork.Fulu
if (let blck = db.getBlock(root, fulu.TrustedSignedBeaconBlock);
blck.isSome()):
ok(ForkedTrustedSignedBeaconBlock.init(blck.get()))
elif (let blck = db.getBlock(root, electra.TrustedSignedBeaconBlock);
blck.isSome()):
ok(ForkedTrustedSignedBeaconBlock.init(blck.get()))
elif (let blck = db.getBlock(root, deneb.TrustedSignedBeaconBlock);
Expand Down Expand Up @@ -1010,6 +1013,12 @@ proc applyBlock(
? state_transition(
dag.cfg, state, data, cache, info,
updateFlags + {slotProcessed}, noRollback)
of ConsensusFork.Fulu:
let data = getBlock(dag, bid, fulu.TrustedSignedBeaconBlock).valueOr:
return err("Block load failed")
? state_transition(
dag.cfg, state, data, cache, info,
updateFlags + {slotProcessed}, noRollback)

ok()

Expand Down Expand Up @@ -1171,6 +1180,7 @@ proc init*(T: type ChainDAGRef, cfg: RuntimeConfig, db: BeaconChainDB,
of ConsensusFork.Capella: capellaFork(cfg)
of ConsensusFork.Deneb: denebFork(cfg)
of ConsensusFork.Electra: electraFork(cfg)
of ConsensusFork.Fulu: fuluFork(cfg)
stateFork = getStateField(dag.headState, fork)

# Here, we check only the `current_version` field because the spec
Expand Down Expand Up @@ -2421,6 +2431,8 @@ proc updateHead*(
of ConsensusFork.Electra:
if dag.vanityLogs.onUpgradeToElectra != nil:
dag.vanityLogs.onUpgradeToElectra()
of ConsensusFork.Fulu:
discard

if dag.vanityLogs.onKnownBlsToExecutionChange != nil and
checkBlsToExecutionChanges(
Expand Down
2 changes: 1 addition & 1 deletion beacon_chain/consensus_object_pools/consensus_manager.nim
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ proc runProposalForkchoiceUpdated*(
payloadAttributes = Opt.some fcPayloadAttributes)
debug "Fork-choice updated for proposal", status

static: doAssert high(ConsensusFork) == ConsensusFork.Electra
static: doAssert high(ConsensusFork) == ConsensusFork.Fulu
when consensusFork >= ConsensusFork.Deneb:
# https://github.com/ethereum/execution-apis/blob/90a46e9137c89d58e818e62fa33a0347bba50085/src/engine/prague.md
# does not define any new forkchoiceUpdated, so reuse V3 from Dencun
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import
std/tables,
../spec/datatypes/eip7594,
../spec/datatypes/fulu,
../spec/helpers

from std/sequtils import mapIt
Expand Down
Loading

0 comments on commit 3240299

Please sign in to comment.