Skip to content

Commit

Permalink
init fulu 2
Browse files Browse the repository at this point in the history
  • Loading branch information
agnxsh committed Oct 25, 2024
1 parent ad95af3 commit aa8c51a
Show file tree
Hide file tree
Showing 31 changed files with 432 additions and 172 deletions.
7 changes: 5 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 Expand Up @@ -1029,6 +1030,8 @@ proc getElectraAttestationsForBlock*(
else:
default(seq[electra.Attestation])



func bestValidation(
aggregates: openArray[Phase0Validation | ElectraValidation]): (int, int) =
# Look for best validation based on number of votes in the aggregate
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
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 @@ -294,10 +294,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

HeadChangeInfoObject* = object
slot*: Slot
Expand Down Expand Up @@ -329,6 +331,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
2 changes: 1 addition & 1 deletion beacon_chain/consensus_object_pools/blockchain_dag.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2427,7 +2427,7 @@ proc updateHead*(
if dag.vanityLogs.onUpgradeToElectra != nil:
dag.vanityLogs.onUpgradeToElectra()
of ConsensusFork.Fulu:
nil
discard nil

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
13 changes: 10 additions & 3 deletions beacon_chain/el/el_manager.nim
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,9 @@ template EngineApiResponseType*(T: type deneb.ExecutionPayloadForSigning): type
template EngineApiResponseType*(T: type electra.ExecutionPayloadForSigning): type =
engine_api.GetPayloadV4Response

template EngineApiResponseType*(T: type fulu.ExecutionPayloadForSigning): type =
engine_api.GetPayloadV4Response

template toEngineWithdrawals*(withdrawals: seq[capella.Withdrawal]): seq[WithdrawalV1] =
mapIt(withdrawals, toEngineWithdrawal(it))

Expand Down Expand Up @@ -720,8 +723,12 @@ proc getPayload*(
requests.filterIt(not(it.finished())).mapIt(it.cancelAndWait())
await noCancel allFutures(pending)

if bestPayloadIdx.isSome():
return ok(requests[bestPayloadIdx.get()].value().asConsensusType)
when PayloadType.kind == ConsensusFork.Fulu:
if bestPayloadIdx.isSome():
return ok(requests[bestPayloadIdx.get()].value().asConsensusTypeFulu)
else:
if bestPayloadIdx.isSome():
return ok(requests[bestPayloadIdx.get()].value().asConsensusType)

if timeoutExceeded:
break
Expand Down Expand Up @@ -987,7 +994,7 @@ proc sendNewPayload*(
let
requests = m.elConnections.mapIt:
let req =
when typeof(blck).kind == ConsensusFork.Electra:
when typeof(blck).kind >= ConsensusFork.Electra:
# https://github.com/ethereum/execution-apis/blob/4140e528360fea53c34a766d86a000c6c039100e/src/engine/prague.md#engine_newpayloadv4
let versioned_hashes = mapIt(
blck.body.blob_kzg_commitments,
Expand Down
53 changes: 51 additions & 2 deletions beacon_chain/el/engine_api_conversions.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import
kzg4844/[kzg_abi, kzg],
../spec/datatypes/[bellatrix, capella, deneb, electra],
../spec/datatypes/[bellatrix, capella, deneb, electra, fulu],
web3/[engine_api, engine_api_types]

from std/sequtils import mapIt
Expand Down Expand Up @@ -156,6 +156,33 @@ func asElectraConsensusPayload(rpcExecutionPayload: ExecutionPayloadV3):
blob_gas_used: rpcExecutionPayload.blobGasUsed.uint64,
excess_blob_gas: rpcExecutionPayload.excessBlobGas.uint64)

func asFuluConsensusPayload(rpcExecutionPayload: ExecutionPayloadV3):
fulu.ExecutionPayload =
template getTransaction(tt: TypedTransaction): bellatrix.Transaction =
bellatrix.Transaction.init(tt.distinctBase)

fulu.ExecutionPayload(
parent_hash: rpcExecutionPayload.parentHash.asEth2Digest,
feeRecipient:
ExecutionAddress(data: rpcExecutionPayload.feeRecipient.distinctBase),
state_root: rpcExecutionPayload.stateRoot.asEth2Digest,
receipts_root: rpcExecutionPayload.receiptsRoot.asEth2Digest,
logs_bloom: BloomLogs(data: rpcExecutionPayload.logsBloom.distinctBase),
prev_randao: rpcExecutionPayload.prevRandao.asEth2Digest,
block_number: rpcExecutionPayload.blockNumber.uint64,
gas_limit: rpcExecutionPayload.gasLimit.uint64,
gas_used: rpcExecutionPayload.gasUsed.uint64,
timestamp: rpcExecutionPayload.timestamp.uint64,
extra_data: List[byte, MAX_EXTRA_DATA_BYTES].init(rpcExecutionPayload.extraData.data),
base_fee_per_gas: rpcExecutionPayload.baseFeePerGas,
block_hash: rpcExecutionPayload.blockHash.asEth2Digest,
transactions: List[bellatrix.Transaction, MAX_TRANSACTIONS_PER_PAYLOAD].init(
mapIt(rpcExecutionPayload.transactions, it.getTransaction)),
withdrawals: List[capella.Withdrawal, MAX_WITHDRAWALS_PER_PAYLOAD].init(
mapIt(rpcExecutionPayload.withdrawals, it.asConsensusWithdrawal)),
blob_gas_used: rpcExecutionPayload.blobGasUsed.uint64,
excess_blob_gas: rpcExecutionPayload.excessBlobGas.uint64)

func asConsensusType*(payload: engine_api.GetPayloadV3Response):
deneb.ExecutionPayloadForSigning =
deneb.ExecutionPayloadForSigning(
Expand Down Expand Up @@ -196,6 +223,27 @@ func asConsensusType*(
payload.blobsBundle.blobs.mapIt(it.data))),
executionRequests: payload.executionRequests)

func asConsensusTypeFulu*(
payload: GetPayloadV4Response):
fulu.ExecutionPayloadForSigning =
fulu.ExecutionPayloadForSigning(
executionPayload: payload.executionPayload.asFuluConsensusPayload,
blockValue: payload.blockValue,
# TODO
# The `mapIt` calls below are necessary only because we use different distinct
# types for KZG commitments and Blobs in the `web3` and the `deneb` spec types.
# Both are defined as `array[N, byte]` under the hood.
blobsBundle: deneb.BlobsBundle(
commitments: KzgCommitments.init(
payload.blobsBundle.commitments.mapIt(
kzg_abi.KzgCommitment(bytes: it.data))),
proofs: KzgProofs.init(
payload.blobsBundle.proofs.mapIt(
kzg_abi.KzgProof(bytes: it.data))),
blobs: Blobs.init(
payload.blobsBundle.blobs.mapIt(it.data))),
executionRequests: payload.executionRequests)

func asEngineExecutionPayload*(blockBody: bellatrix.BeaconBlockBody):
ExecutionPayloadV1 =
template executionPayload(): untyped = blockBody.execution_payload
Expand Down Expand Up @@ -252,7 +300,8 @@ func asEngineExecutionPayload*(blockBody: capella.BeaconBlockBody):
withdrawals: mapIt(executionPayload.withdrawals, it.toEngineWithdrawal))

func asEngineExecutionPayload*(
blockBody: deneb.BeaconBlockBody | electra.BeaconBlockBody):
blockBody: deneb.BeaconBlockBody | electra.BeaconBlockBody |
fulu.BeaconBlockBody):
ExecutionPayloadV3 =
template executionPayload(): untyped = blockBody.execution_payload

Expand Down
8 changes: 5 additions & 3 deletions beacon_chain/gossip_processing/block_processor.nim
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,8 @@ proc newExecutionPayload*(
proc getExecutionValidity(
elManager: ELManager,
blck: bellatrix.SignedBeaconBlock | capella.SignedBeaconBlock |
deneb.SignedBeaconBlock | electra.SignedBeaconBlock,
deneb.SignedBeaconBlock | electra.SignedBeaconBlock |
fulu.SignedBeaconBlock,
deadlineObj: DeadlineObject,
maxRetriesCount: int
): Future[NewPayloadStatus] {.async: (raises: [CancelledError]).} =
Expand Down Expand Up @@ -371,7 +372,8 @@ proc getExecutionValidity(

proc checkBloblessSignature(
self: BlockProcessor,
signed_beacon_block: deneb.SignedBeaconBlock | electra.SignedBeaconBlock):
signed_beacon_block: deneb.SignedBeaconBlock | electra.SignedBeaconBlock |
fulu.SignedBeaconBlock):
Result[void, cstring] =
let dag = self.consensusManager.dag
let parent = dag.getBlockRef(signed_beacon_block.message.parent_root).valueOr:
Expand Down Expand Up @@ -757,7 +759,7 @@ proc storeBlock(
template callForkChoiceUpdated: auto =
case self.consensusManager.dag.cfg.consensusForkAtEpoch(
newHead.get.blck.bid.slot.epoch)
of ConsensusFork.Deneb, ConsensusFork.Electra:
of ConsensusFork.Deneb, ConsensusFork.Electra, ConsensusFork.Fulu:
# https://github.com/ethereum/execution-apis/blob/90a46e9137c89d58e818e62fa33a0347bba50085/src/engine/prague.md
# does not define any new forkchoiceUpdated, so reuse V3 from Dencun
callExpectValidFCU(payloadAttributeType = PayloadAttributesV3)
Expand Down
3 changes: 2 additions & 1 deletion beacon_chain/gossip_processing/gossip_validation.nim
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,8 @@ template validateBeaconBlockBellatrix(
bellatrix.SignedBeaconBlock |
capella.SignedBeaconBlock |
deneb.SignedBeaconBlock |
electra.SignedBeaconBlock,
electra.SignedBeaconBlock |
fulu.SignedBeaconBlock,
parent: BlockRef): untyped =
# If the execution is enabled for the block -- i.e.
# is_execution_enabled(state, block.body) then validate the following:
Expand Down
9 changes: 8 additions & 1 deletion beacon_chain/networking/eth2_network.nim
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,8 @@ template gossipMaxSize(T: untyped): uint32 =
when isFixedSize(T):
fixedPortionSize(T).uint32
elif T is bellatrix.SignedBeaconBlock or T is capella.SignedBeaconBlock or
T is deneb.SignedBeaconBlock or T is electra.SignedBeaconBlock:
T is deneb.SignedBeaconBlock or T is electra.SignedBeaconBlock or
T is fulu.SignedBeaconBlock:
GOSSIP_MAX_SIZE
# TODO https://github.com/status-im/nim-ssz-serialization/issues/20 for
# Attestation, AttesterSlashing, and SignedAggregateAndProof, which all
Expand Down Expand Up @@ -2684,6 +2685,12 @@ proc broadcastBeaconBlock*(
let topic = getBeaconBlocksTopic(node.forkDigests.electra)
node.broadcast(topic, blck)

proc broadcastBeaconBlock*(
node: Eth2Node, blck: fulu.SignedBeaconBlock):
Future[SendResult] {.async: (raises: [CancelledError], raw: true).} =
let topic = getBeaconBlocksTopic(node.forkDigests.fulu)
node.broadcast(topic, blck)

proc broadcastBlobSidecar*(
node: Eth2Node, subnet_id: BlobId, blob: deneb.BlobSidecar):
Future[SendResult] {.async: (raises: [CancelledError], raw: true).} =
Expand Down
22 changes: 17 additions & 5 deletions beacon_chain/nimbus_beacon_node.nim
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ func getVanityLogs(stdoutKind: StdoutLogKind): VanityLogs =

func getVanityMascot(consensusFork: ConsensusFork): string =
case consensusFork
of ConsensusFork.Fulu:
"not decided yet?"
of ConsensusFork.Electra:
"🦒"
of ConsensusFork.Deneb:
Expand Down Expand Up @@ -661,7 +663,7 @@ proc init*(T: type BeaconNode,
exitQueue: newAsyncEventQueue[SignedVoluntaryExit](),
blsToExecQueue: newAsyncEventQueue[SignedBLSToExecutionChange](),
propSlashQueue: newAsyncEventQueue[ProposerSlashing](),
attSlashQueue: newAsyncEventQueue[AttesterSlashing](),
attSlashQueue: newAsyncEventQueue[phase0.AttesterSlashing](),
blobSidecarQueue: newAsyncEventQueue[BlobSidecarInfoObject](),
finalQueue: newAsyncEventQueue[FinalizationInfoObject](),
reorgQueue: newAsyncEventQueue[ReorgInfoObject](),
Expand Down Expand Up @@ -1007,7 +1009,8 @@ func forkDigests(node: BeaconNode): auto =
node.dag.forkDigests.bellatrix,
node.dag.forkDigests.capella,
node.dag.forkDigests.deneb,
node.dag.forkDigests.electra]
node.dag.forkDigests.electra,
node.dag.forkDigests.fulu]
forkDigestsArray

# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.5/specs/phase0/p2p-interface.md#attestation-subnet-subscription
Expand Down Expand Up @@ -1199,6 +1202,10 @@ proc addElectraMessageHandlers(
node: BeaconNode, forkDigest: ForkDigest, slot: Slot) =
node.addDenebMessageHandlers(forkDigest, slot)

proc addFuluMessageHandlers(
node: BeaconNode, forkDigest: ForkDigest, slot: Slot) =
node.addElectraMessageHandlers(forkDigest, slot)

proc removeAltairMessageHandlers(node: BeaconNode, forkDigest: ForkDigest) =
node.removePhase0MessageHandlers(forkDigest)

Expand All @@ -1222,6 +1229,9 @@ proc removeDenebMessageHandlers(node: BeaconNode, forkDigest: ForkDigest) =
proc removeElectraMessageHandlers(node: BeaconNode, forkDigest: ForkDigest) =
node.removeDenebMessageHandlers(forkDigest)

proc removeFuluMessageHandlers(node: BeaconNode, forkDigest: ForkDigest) =
node.removeElectraMessageHandlers(forkDigest)

proc updateSyncCommitteeTopics(node: BeaconNode, slot: Slot) =
template lastSyncUpdate: untyped =
node.consensusManager[].actionTracker.lastSyncUpdate
Expand Down Expand Up @@ -1372,7 +1382,7 @@ proc updateGossipStatus(node: BeaconNode, slot: Slot) {.async.} =
TOPIC_SUBSCRIBE_THRESHOLD_SLOTS = 64
HYSTERESIS_BUFFER = 16

static: doAssert high(ConsensusFork) == ConsensusFork.Electra
static: doAssert high(ConsensusFork) == ConsensusFork.Fulu

let
head = node.dag.head
Expand Down Expand Up @@ -1451,7 +1461,8 @@ proc updateGossipStatus(node: BeaconNode, slot: Slot) {.async.} =
removeAltairMessageHandlers, # bellatrix (altair handlers, different forkDigest)
removeCapellaMessageHandlers,
removeDenebMessageHandlers,
removeElectraMessageHandlers
removeElectraMessageHandlers,
removeFuluMessageHandlers
]

for gossipFork in oldGossipForks:
Expand All @@ -1463,7 +1474,8 @@ proc updateGossipStatus(node: BeaconNode, slot: Slot) {.async.} =
addAltairMessageHandlers, # bellatrix (altair handlers, different forkDigest)
addCapellaMessageHandlers,
addDenebMessageHandlers,
addElectraMessageHandlers
addElectraMessageHandlers,
addFuluMessageHandlers
]

for gossipFork in newGossipForks:
Expand Down
4 changes: 2 additions & 2 deletions beacon_chain/rpc/rest_beacon_api.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1455,7 +1455,7 @@ proc installBeaconApiHandlers*(router: var RestRouter, node: BeaconNode) =
case consensusVersion.get():
of ConsensusFork.Phase0 .. ConsensusFork.Deneb:
decodeAttestations(phase0.Attestation)
of ConsensusFork.Electra:
of ConsensusFork.Electra .. ConsensusFork.Fulu:
decodeAttestations(electra.Attestation)

let failures =
Expand Down Expand Up @@ -1555,7 +1555,7 @@ proc installBeaconApiHandlers*(router: var RestRouter, node: BeaconNode) =
case consensusVersion.get():
of ConsensusFork.Phase0 .. ConsensusFork.Deneb:
decodeAttesterSlashing(phase0.AttesterSlashing)
of ConsensusFork.Electra:
of ConsensusFork.Electra .. ConsensusFork.Fulu:
decodeAttesterSlashing(electra.AttesterSlashing)

# https://ethereum.github.io/beacon-APIs/#/Beacon/getPoolProposerSlashings
Expand Down
Loading

0 comments on commit aa8c51a

Please sign in to comment.