@@ -372,6 +372,19 @@ func compute_timestamp_at_slot*(state: ForkyBeaconState, slot: Slot): uint64 =
372372 let slots_since_genesis = slot - GENESIS_SLOT
373373 state.genesis_time + slots_since_genesis * SECONDS_PER_SLOT
374374
375+ proc computeTransactionsTrieRoot * (
376+ payload: bellatrix.ExecutionPayload | capella.ExecutionPayload ): Hash256 =
377+ if payload.transactions.len == 0 :
378+ return EMPTY_ROOT_HASH
379+
380+ var tr = initHexaryTrie (newMemoryDB ())
381+ for i, transaction in payload.transactions:
382+ try :
383+ tr.put (rlp.encode (i), distinctBase (transaction)) # Already RLP encoded
384+ except RlpError as exc:
385+ doAssert false , " HexaryTrie.put failed: " & $ exc.msg
386+ tr.rootHash ()
387+
375388func gweiToWei * (gwei: Gwei ): UInt256 =
376389 gwei.u256 * 1_000_000_000 .u256
377390
@@ -397,18 +410,15 @@ proc computeWithdrawalsTrieRoot*(
397410 doAssert false , " HexaryTrie.put failed: " & $ exc.msg
398411 tr.rootHash ()
399412
400- proc emptyPayloadToBlockHeader * (
413+ proc payloadToBlockHeader * (
401414 payload: bellatrix.ExecutionPayload | capella.ExecutionPayload
402415): ExecutionBlockHeader =
403416 static : # `GasInt` is signed. We only use it for hashing.
404417 doAssert sizeof (GasInt ) == sizeof (payload.gas_limit)
405418 doAssert sizeof (GasInt ) == sizeof (payload.gas_used)
406419
407- # # This function assumes that the payload is empty!
408- doAssert payload.transactions.len == 0
409-
410420 let
411- txRoot = EMPTY_ROOT_HASH
421+ txRoot = payload. computeTransactionsTrieRoot ()
412422 withdrawalsRoot =
413423 when payload is bellatrix.ExecutionPayload :
414424 none (Hash256 )
@@ -434,7 +444,12 @@ proc emptyPayloadToBlockHeader*(
434444 fee : some payload.base_fee_per_gas,
435445 withdrawalsRoot: withdrawalsRoot)
436446
437- func build_empty_execution_payload * (
447+ proc compute_execution_block_hash * (
448+ payload: bellatrix.ExecutionPayload | capella.ExecutionPayload
449+ ): Eth2Digest =
450+ rlpHash payloadToBlockHeader (payload)
451+
452+ proc build_empty_execution_payload * (
438453 state: bellatrix.BeaconState ,
439454 feeRecipient: Eth1Address ): bellatrix.ExecutionPayload =
440455 # # Assuming a pre-state of the same slot, build a valid ExecutionPayload
@@ -459,7 +474,7 @@ func build_empty_execution_payload*(
459474 timestamp: timestamp,
460475 base_fee_per_gas: base_fee)
461476
462- payload.block_hash = rlpHash emptyPayloadToBlockHeader ( payload)
477+ payload.block_hash = payload. compute_execution_block_hash ( )
463478
464479 payload
465480
@@ -491,6 +506,6 @@ proc build_empty_execution_payload*(
491506 for withdrawal in expectedWithdrawals:
492507 doAssert payload.withdrawals.add withdrawal
493508
494- payload.block_hash = rlpHash emptyPayloadToBlockHeader ( payload)
509+ payload.block_hash = payload. compute_execution_block_hash ( )
495510
496511 payload
0 commit comments