From 0a5d9ee0278139ea0b7708ef94ebeaef18d65b32 Mon Sep 17 00:00:00 2001 From: Etan Kissling Date: Thu, 7 Dec 2023 18:10:22 +0100 Subject: [PATCH] use `PayloadAttributesV3` in `nimbus_light_client` for Deneb (#5654) * use `PayloadAttributesV3` in `nimbus_light_client` for Deneb From Deneb onward, `forkchoiceUpdated` requires `PayloadAttributesV3`. In `nimbus_light_client` we still used `PayloadAttributesV2`. Also clean up two other locations that were already correctly using `PayloadAttributesV3`, to reduce code duplication. * fix letter case --- .../consensus_manager.nim | 21 +++++++------------ .../gossip_processing/block_processor.nim | 20 +++++------------- beacon_chain/nimbus_light_client.nim | 20 ++---------------- beacon_chain/spec/forks.nim | 11 ++++++++++ 4 files changed, 26 insertions(+), 46 deletions(-) diff --git a/beacon_chain/consensus_object_pools/consensus_manager.nim b/beacon_chain/consensus_object_pools/consensus_manager.nim index 2c1ab3851..c72e46caa 100644 --- a/beacon_chain/consensus_object_pools/consensus_manager.nim +++ b/beacon_chain/consensus_object_pools/consensus_manager.nim @@ -168,19 +168,14 @@ proc updateExecutionClientHead(self: ref ConsensusManager, payloadAttributes = none attributes) # Can't use dag.head here because it hasn't been updated yet - let (payloadExecutionStatus, _) = - case self.dag.cfg.consensusForkAtEpoch(newHead.blck.bid.slot.epoch) - of ConsensusFork.Deneb: - callForkchoiceUpdated(PayloadAttributesV3) - of ConsensusFork.Capella: - # https://github.com/ethereum/execution-apis/blob/v1.0.0-beta.3/src/engine/shanghai.md#specification-1 - # Consensus layer client MUST call this method instead of - # `engine_forkchoiceUpdatedV1` under any of the following conditions: - # `headBlockHash` references a block which `timestamp` is greater or - # equal to the Shanghai timestamp - callForkchoiceUpdated(PayloadAttributesV2) - of ConsensusFork.Phase0, ConsensusFork.Altair, ConsensusFork.Bellatrix: - callForkchoiceUpdated(PayloadAttributesV1) + let + consensusFork = + self.dag.cfg.consensusForkAtEpoch(newHead.blck.bid.slot.epoch) + (payloadExecutionStatus, _) = withConsensusFork(consensusFork): + when consensusFork >= ConsensusFork.Bellatrix: + callForkchoiceUpdated(consensusFork.PayloadAttributes) + else: + callForkchoiceUpdated(PayloadAttributesV1) case payloadExecutionStatus of PayloadExecutionStatus.valid: diff --git a/beacon_chain/gossip_processing/block_processor.nim b/beacon_chain/gossip_processing/block_processor.nim index de6a9743c..e1176c8e4 100644 --- a/beacon_chain/gossip_processing/block_processor.nim +++ b/beacon_chain/gossip_processing/block_processor.nim @@ -642,21 +642,11 @@ proc storeBlock( finalizedBlockHash = newHead.get.finalizedExecutionPayloadHash, payloadAttributes = none attributes) - case self.consensusManager.dag.cfg.consensusForkAtEpoch( - newHead.get.blck.bid.slot.epoch) - of ConsensusFork.Deneb: - callForkchoiceUpdated(PayloadAttributesV3) - of ConsensusFork.Capella: - # https://github.com/ethereum/execution-apis/blob/v1.0.0-beta.3/src/engine/shanghai.md#specification-1 - # Consensus layer client MUST call this method instead of - # `engine_forkchoiceUpdatedV1` under any of the following conditions: - # `headBlockHash` references a block which `timestamp` is greater or - # equal to the Shanghai timestamp - callForkchoiceUpdated(PayloadAttributesV2) - of ConsensusFork.Bellatrix: - callForkchoiceUpdated(PayloadAttributesV1) - of ConsensusFork.Phase0, ConsensusFork.Altair: - discard + let consensusFork = self.consensusManager.dag.cfg.consensusForkAtEpoch( + newHead.get.blck.bid.slot.epoch) + withConsensusFork(consensusFork): + when consensusFork >= ConsensusFork.Bellatrix: + callForkchoiceUpdated(consensusFork.PayloadAttributes) else: let headExecutionPayloadHash = diff --git a/beacon_chain/nimbus_light_client.nim b/beacon_chain/nimbus_light_client.nim index 59812bd14..cdf33d90d 100644 --- a/beacon_chain/nimbus_light_client.nim +++ b/beacon_chain/nimbus_light_client.nim @@ -109,12 +109,7 @@ programMain: opt = signedBlock.toBlockId(), wallSlot = getBeaconTime().slotOrZero withBlck(signedBlock): - when consensusFork >= ConsensusFork.Capella: - # https://github.com/ethereum/execution-apis/blob/v1.0.0-beta.3/src/engine/shanghai.md#specification-1 - # Consensus layer client MUST call this method instead of - # `engine_forkchoiceUpdatedV1` under any of the following conditions: - # `headBlockHash` references a block which `timestamp` is greater or - # equal to the Shanghai timestamp + when consensusFork >= ConsensusFork.Bellatrix: if forkyBlck.message.is_execution_block: template payload(): auto = forkyBlck.message.body.execution_payload @@ -124,18 +119,7 @@ programMain: headBlockHash = payload.block_hash, safeBlockHash = payload.block_hash, # stub value finalizedBlockHash = ZERO_HASH, - payloadAttributes = none PayloadAttributesV2) - elif consensusFork >= ConsensusFork.Bellatrix: - if forkyBlck.message.is_execution_block: - template payload(): auto = forkyBlck.message.body.execution_payload - - if elManager != nil and not payload.block_hash.isZero: - discard await elManager.newExecutionPayload(forkyBlck.message) - discard await elManager.forkchoiceUpdated( - headBlockHash = payload.block_hash, - safeBlockHash = payload.block_hash, # stub value - finalizedBlockHash = ZERO_HASH, - payloadAttributes = none PayloadAttributesV1) + payloadAttributes = none(consensusFork.PayloadAttributes)) else: discard optimisticProcessor = initOptimisticProcessor( getBeaconTime, optimisticHandler) diff --git a/beacon_chain/spec/forks.nim b/beacon_chain/spec/forks.nim index 6e75841cc..13d4a7279 100644 --- a/beacon_chain/spec/forks.nim +++ b/beacon_chain/spec/forks.nim @@ -531,6 +531,17 @@ template BlindedBlockContents*( else: {.error: "BlindedBlockContents does not support " & $kind.} +template PayloadAttributes*( + kind: static ConsensusFork): auto = + when kind >= ConsensusFork.Deneb: + typedesc[PayloadAttributesV3] + elif kind >= ConsensusFork.Capella: + typedesc[PayloadAttributesV2] + elif kind >= ConsensusFork.Bellatrix: + typedesc[PayloadAttributesV1] + else: + {.error: "PayloadAttributes does not support " & $kind.} + # TODO when https://github.com/nim-lang/Nim/issues/21086 fixed, use return type # `ref T` func new*(T: type ForkedHashedBeaconState, data: phase0.BeaconState):