diff --git a/nimbus/beacon/payload_conv.nim b/nimbus/beacon/payload_conv.nim index d2c82aad8..2c2c1a5a7 100644 --- a/nimbus/beacon/payload_conv.nim +++ b/nimbus/beacon/payload_conv.nim @@ -35,6 +35,13 @@ func txRoot(list: openArray[Web3Tx]): common.Hash256 {.noSideEffect.}: calcTxRoot(ethTxs(list)) +func requestsRoot(p: ExecutionPayload): Opt[common.Hash256] + {.gcsafe, raises:[].} = + {.noSideEffect.}: + let reqs = ethRequests(p) + if reqs.isNone: Opt.none(common.Hash256) + else: Opt.some(calcRequestsRoot reqs.get) + # ------------------------------------------------------------------------------ # Public functions # ------------------------------------------------------------------------------ @@ -59,7 +66,10 @@ func executionPayload*(blk: EthBlock): ExecutionPayload = transactions : w3Txs blk.txs, withdrawals : w3Withdrawals blk.withdrawals, blobGasUsed : w3Qty blk.header.blobGasUsed, - excessBlobGas: w3Qty blk.header.excessBlobGas + excessBlobGas: w3Qty blk.header.excessBlobGas, + depositRequests: w3DepositRequests blk.requests, + withdrawalRequests: w3WithdrawalRequests blk.requests, + consolidationRequests: w3ConsolidationRequests blk.requests, ) func executionPayloadV1V2*(blk: EthBlock): ExecutionPayloadV1OrV2 = @@ -104,7 +114,8 @@ func blockHeader*(p: ExecutionPayload, withdrawalsRoot: wdRoot p.withdrawals, blobGasUsed : u64(p.blobGasUsed), excessBlobGas : u64(p.excessBlobGas), - parentBeaconBlockRoot: beaconRoot + parentBeaconBlockRoot: beaconRoot, + requestsRoot : requestsRoot(p), ) func blockBody*(p: ExecutionPayload): @@ -113,6 +124,7 @@ func blockBody*(p: ExecutionPayload): uncles : @[], transactions: ethTxs p.transactions, withdrawals : ethWithdrawals p.withdrawals, + requests : ethRequests(p), ) func ethBlock*(p: ExecutionPayload, @@ -122,5 +134,6 @@ func ethBlock*(p: ExecutionPayload, header : blockHeader(p, beaconRoot), uncles : @[], transactions: ethTxs p.transactions, - withdrawals: ethWithdrawals p.withdrawals, + withdrawals : ethWithdrawals p.withdrawals, + requests : ethRequests(p), ) diff --git a/nimbus/beacon/web3_eth_conv.nim b/nimbus/beacon/web3_eth_conv.nim index d512dfa0c..573286c6e 100644 --- a/nimbus/beacon/web3_eth_conv.nim +++ b/nimbus/beacon/web3_eth_conv.nim @@ -12,6 +12,7 @@ import web3/primitives as web3types, web3/eth_api_types, web3/engine_api_types, + web3/execution_types, eth/common/eth_types_rlp, stew/byteutils, ../utils/utils @@ -293,3 +294,112 @@ proc w3AccessList*(list: openArray[AccessPair]): seq[AccessTuple] = result = newSeqOfCap[AccessTuple](list.len) for x in list: result.add w3AccessTuple(x) + +func w3DepositRequest*(x: DepositRequest): DepositRequestV1 = + DepositRequestV1( + pubkey: FixedBytes[48](x.pubkey), + withdrawalCredentials: FixedBytes[32](x.withdrawalCredentials), + amount: w3Qty x.amount, + signature: FixedBytes[96](x.signature), + index: w3Qty x.index, + ) + +func w3DepositRequests*(reqs: Opt[seq[Request]]): Opt[seq[DepositRequestV1]] = + if reqs.isNone: + return Opt.none(seq[DepositRequestV1]) + + var res: seq[DepositRequestV1] + for req in reqs.get: + if req.requestType == DepositRequestType: + res.add w3DepositRequest req.deposit + + ok(res) + +func w3WithdrawalRequest*(x: WithdrawalRequest): WithdrawalRequestV1 = + WithdrawalRequestV1( + sourceAddress: w3Addr x.sourceAddress, + validatorPubkey: FixedBytes[48](x.validatorPubkey), + amount: w3Qty x.amount, + ) + +func w3WithdrawalRequests*(reqs: Opt[seq[Request]]): Opt[seq[WithdrawalRequestV1]] = + if reqs.isNone: + return Opt.none(seq[WithdrawalRequestV1]) + + var res: seq[WithdrawalRequestV1] + for req in reqs.get: + if req.requestType == WithdrawalRequestType: + res.add w3WithdrawalRequest req.withdrawal + + ok(res) + +func w3ConsolidationRequest*(x: ConsolidationRequest): ConsolidationRequestV1 = + ConsolidationRequestV1( + sourceAddress: w3Addr x.sourceAddress, + sourcePubkey: FixedBytes[48](x.sourcePubkey), + targetPubkey: FixedBytes[48](x.targetPubkey), + ) + +func w3ConsolidationRequests*(reqs: Opt[seq[Request]]): Opt[seq[ConsolidationRequestV1]] = + if reqs.isNone: + return Opt.none(seq[ConsolidationRequestV1]) + + var res: seq[ConsolidationRequestV1] + for req in reqs.get: + if req.requestType == ConsolidationRequestType: + res.add w3ConsolidationRequest req.consolidation + + ok(res) + +func ethRequest*(x: DepositRequestV1): Request = + Request( + requestType: DepositRequestType, + deposit: DepositRequest( + pubkey: x.pubkey.bytes, + withdrawalCredentials: x.withdrawalCredentials.bytes, + amount: uint64 x.amount, + signature: x.signature.bytes, + index: uint64 x.index, + ) + ) + +func ethRequest*(x: WithdrawalRequestV1): Request = + Request( + requestType: WithdrawalRequestType, + withdrawal: WithDrawalRequest( + sourceAddress: ethAddr x.sourceAddress, + validatorPubkey: x.validatorPubkey.bytes, + amount: uint64 x.amount, + ) + ) + +func ethRequest*(x: ConsolidationRequestV1): Request = + Request( + requestType: ConsolidationRequestType, + consolidation: ConsolidationRequest( + sourceAddress: ethAddr x.sourceAddress, + sourcePubkey: x.sourcePubkey.bytes, + targetPubkey: x.targetPubkey.bytes, + ) + ) + +func ethRequests*(p: ExecutionPayload): Opt[seq[Request]] = + if p.depositRequests.isNone and + p.withdrawalRequests.isNone and + p.consolidationRequests.isNone: + return Opt.none(seq[Request]) + + var res: seq[Request] + if p.depositRequests.isSome: + for x in p.depositRequests.get: + res.add ethRequest(x) + + if p.withdrawalRequests.isSome: + for x in p.withdrawalRequests.get: + res.add ethRequest(x) + + if p.consolidationRequests.isSome: + for x in p.consolidationRequests.get: + res.add ethRequest(x) + + ok(res) diff --git a/nimbus/utils/utils.nim b/nimbus/utils/utils.nim index 7715778b0..02dd11558 100644 --- a/nimbus/utils/utils.nim +++ b/nimbus/utils/utils.nim @@ -36,6 +36,9 @@ template calcWithdrawalsRoot*(withdrawals: openArray[Withdrawal]): Hash256 = template calcReceiptsRoot*(receipts: openArray[Receipt]): Hash256 = calcRootHash(receipts) +template calcRequestsRoot*(requests: openArray[Request]): Hash256 = + calcRootHash(requests) + func sumHash*(hashes: varargs[Hash256]): Hash256 = var ctx: sha256 ctx.init() diff --git a/vendor/nim-eth b/vendor/nim-eth index 11eafac0f..59715353d 160000 --- a/vendor/nim-eth +++ b/vendor/nim-eth @@ -1 +1 @@ -Subproject commit 11eafac0f086510f3d2fbd54573b4a1521bfbbf5 +Subproject commit 59715353dbe73c49fbcde58e720387efd70d5684