Skip to content

Commit

Permalink
fill in transactions root and KZG commitments from deneb.BlindedBeaco…
Browse files Browse the repository at this point in the history
…nBlock in state transition (#5553)
  • Loading branch information
tersec authored Nov 2, 2023
1 parent 657de27 commit ab89e96
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
16 changes: 11 additions & 5 deletions beacon_chain/spec/state_transition.nim
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,8 @@ proc makeBeaconBlock*(
# removed if we don't use invalid signatures there
verificationFlags: UpdateFlags,
transactions_root: Opt[Eth2Digest],
execution_payload_root: Opt[Eth2Digest]):
execution_payload_root: Opt[Eth2Digest],
kzg_commitments: Opt[KzgCommitments]):
Result[ForkedBeaconBlock, cstring] =
## Create a block for the given state. The latest block applied to it will
## be used for the parent_root value, and the slot will be take from
Expand All @@ -428,7 +429,7 @@ proc makeBeaconBlock*(
rollback(state)
return err(res.error())

# Override for MEV
# Override for Builder API
if transactions_root.isSome and execution_payload_root.isSome:
withState(state):
when consensusFork < ConsensusFork.Capella:
Expand All @@ -455,6 +456,9 @@ proc makeBeaconBlock*(
execution_payload_root.get,
hash_tree_root(validator_changes.bls_to_execution_changes)])
elif consensusFork == ConsensusFork.Deneb:
forkyState.data.latest_execution_payload_header.transactions_root =
transactions_root.get

when executionPayload is deneb.ExecutionPayloadForSigning:
# https://github.com/ethereum/consensus-specs/blob/v1.4.0-beta.1/specs/deneb/beacon-chain.md#beaconblockbody
forkyState.data.latest_block_header.body_root = hash_tree_root(
Expand All @@ -469,7 +473,7 @@ proc makeBeaconBlock*(
hash_tree_root(sync_aggregate),
execution_payload_root.get,
hash_tree_root(validator_changes.bls_to_execution_changes),
hash_tree_root(executionPayload.kzgs)
hash_tree_root(kzg_commitments.get)
])
else:
raiseAssert "Attempt to use non-Deneb payload with post-Deneb state"
Expand Down Expand Up @@ -517,7 +521,8 @@ proc makeBeaconBlock*(
attestations, deposits, validator_changes, sync_aggregate,
executionPayload, rollback, cache,
verificationFlags = {}, transactions_root = Opt.none Eth2Digest,
execution_payload_root = Opt.none Eth2Digest)
execution_payload_root = Opt.none Eth2Digest,
kzg_commitments = Opt.none KzgCommitments)

proc makeBeaconBlock*(
cfg: RuntimeConfig, state: var ForkedHashedBeaconState,
Expand All @@ -536,4 +541,5 @@ proc makeBeaconBlock*(
executionPayload, rollback, cache,
verificationFlags = verificationFlags,
transactions_root = Opt.none Eth2Digest,
execution_payload_root = Opt.none Eth2Digest)
execution_payload_root = Opt.none Eth2Digest,
kzg_commitments = Opt.none KzgCommitments)
18 changes: 13 additions & 5 deletions beacon_chain/validators/beacon_validators.nim
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,8 @@ proc makeBeaconBlockForHeadAndSlot*(
execution_payload: Opt[PayloadType],
transactions_root: Opt[Eth2Digest],
execution_payload_root: Opt[Eth2Digest],
withdrawals_root: Opt[Eth2Digest]):
withdrawals_root: Opt[Eth2Digest],
kzg_commitments: Opt[KzgCommitments]):
Future[ForkedBlockResult] {.async.} =
# Advance state to the slot that we're proposing for
var cache = StateCache()
Expand Down Expand Up @@ -528,7 +529,8 @@ proc makeBeaconBlockForHeadAndSlot*(
cache,
verificationFlags = {},
transactions_root = transactions_root,
execution_payload_root = execution_payload_root).mapErr do (error: cstring) -> string:
execution_payload_root = execution_payload_root,
kzg_commitments = kzg_commitments).mapErr do (error: cstring) -> string:
# This is almost certainly a bug, but it's complex enough that there's a
# small risk it might happen even when most proposals succeed - thus we
# log instead of asserting
Expand Down Expand Up @@ -560,7 +562,8 @@ proc makeBeaconBlockForHeadAndSlot*(
execution_payload = Opt.none(PayloadType),
transactions_root = Opt.none(Eth2Digest),
execution_payload_root = Opt.none(Eth2Digest),
withdrawals_root = Opt.none(Eth2Digest))
withdrawals_root = Opt.none(Eth2Digest),
kzg_commitments = Opt.none(KzgCommitments))

proc getBlindedExecutionPayload[
EPH: capella.ExecutionPayloadHeader |
Expand Down Expand Up @@ -846,6 +849,7 @@ proc getBlindedBlockParts[
template actualEPH: untyped = executionPayloadHeader.get.blindedBlckPart
let withdrawals_root =
Opt.some executionPayloadHeader.get.blindedBlckPart.withdrawals_root
const kzg_commitments = Opt.none KzgCommitments

var shimExecutionPayload: PayloadType
copyFields(
Expand All @@ -855,7 +859,10 @@ proc getBlindedBlockParts[
type PayloadType = deneb.ExecutionPayloadForSigning
template actualEPH: untyped =
executionPayloadHeader.get.blindedBlckPart.execution_payload_header
let withdrawals_root = Opt.some actualEPH.withdrawals_root
let
withdrawals_root = Opt.some actualEPH.withdrawals_root
kzg_commitments = Opt.some(
executionPayloadHeader.get.blindedBlckPart.blinded_blobs_bundle.commitments)

var shimExecutionPayload: PayloadType
type DenebEPH =
Expand All @@ -870,7 +877,8 @@ proc getBlindedBlockParts[
execution_payload = Opt.some shimExecutionPayload,
transactions_root = Opt.some actualEPH.transactions_root,
execution_payload_root = Opt.some hash_tree_root(actualEPH),
withdrawals_root = withdrawals_root)
withdrawals_root = withdrawals_root,
kzg_commitments = kzg_commitments)

if newBlock.isErr():
# Haven't committed to the MEV block, so allow EL fallback.
Expand Down

0 comments on commit ab89e96

Please sign in to comment.