From 0c1236756dd373c73ad7d3efbdc84397667b7515 Mon Sep 17 00:00:00 2001 From: jangko Date: Wed, 16 Aug 2023 20:05:43 +0700 Subject: [PATCH] EIP-4895: add withdrawal processing in txpool --- nimbus/core/executor/process_block.nim | 9 +-------- nimbus/core/tx_pool/tx_chain.nim | 2 +- nimbus/core/tx_pool/tx_tasks/tx_packer.nim | 7 ++++++- nimbus/utils/utils.nim | 12 ++++++++---- 4 files changed, 16 insertions(+), 14 deletions(-) diff --git a/nimbus/core/executor/process_block.nim b/nimbus/core/executor/process_block.nim index 80fa7b85e..3eee76278 100644 --- a/nimbus/core/executor/process_block.nim +++ b/nimbus/core/executor/process_block.nim @@ -9,7 +9,7 @@ # according to those terms. import - math, + ../../utils/utils, ../../common/common, ../../constants, ../../db/accounts_cache, @@ -28,13 +28,6 @@ import {.push raises: [].} -# ------------------------------------------------------------------------------ -# Private functions -# ------------------------------------------------------------------------------ - -func gwei(n: uint64): UInt256 = - n.u256 * (10 ^ 9).u256 - # Factored this out of procBlkPreamble so that it can be used directly for # stateless execution of specific transactions. proc processTransactions*(vmState: BaseVMState; diff --git a/nimbus/core/tx_pool/tx_chain.nim b/nimbus/core/tx_pool/tx_chain.nim index ffa6ad51a..d81cc6a2c 100644 --- a/nimbus/core/tx_pool/tx_chain.nim +++ b/nimbus/core/tx_pool/tx_chain.nim @@ -311,7 +311,7 @@ proc vmState*(dh: TxChainRef): BaseVMState = proc withdrawals*(dh: TxChainRef): seq[Withdrawal] = ## Getter, `BaseVmState` descriptor based on the current insertion point. - result = system.move(dh.withdrawals) + dh.withdrawals # ------------------------------------------------------------------------------ # Public functions, setters diff --git a/nimbus/core/tx_pool/tx_tasks/tx_packer.nim b/nimbus/core/tx_pool/tx_tasks/tx_packer.nim index f601b643b..6536b7196 100644 --- a/nimbus/core/tx_pool/tx_tasks/tx_packer.nim +++ b/nimbus/core/tx_pool/tx_tasks/tx_packer.nim @@ -20,6 +20,7 @@ import stew/sorted_set, ../../../db/[accounts_cache, core_db], ../../../common/common, + ../../../utils/utils, "../.."/[dao, executor, validate, eip4844], ../../../transaction/call_evm, ../../../transaction, @@ -92,7 +93,6 @@ proc runTx(pst: TxPackerStateRef; item: TxItemRef): GasInt pst.cleanState = false doAssert 0 <= result - proc runTxCommit(pst: TxPackerStateRef; item: TxItemRef; gasBurned: GasInt) {.gcsafe,raises: [CatchableError].} = ## Book keeping after executing argument `item` transaction in the VM. The @@ -219,6 +219,11 @@ proc vmExecCommit(pst: TxPackerStateRef) xp = pst.xp vmState = xp.chain.vmState + # EIP-4895 + if xp.chain.nextFork >= FkShanghai: + for withdrawal in xp.chain.withdrawals: + vmState.stateDB.addBalance(withdrawal.address, withdrawal.amount.gwei) + # EIP-3675: no reward for miner in POA/POS if vmState.com.consensus == ConsensusType.POW: let diff --git a/nimbus/utils/utils.nim b/nimbus/utils/utils.nim index b71efebed..8ac6c1963 100644 --- a/nimbus/utils/utils.nim +++ b/nimbus/utils/utils.nim @@ -1,4 +1,5 @@ import + std/math, eth/[rlp, common/eth_types_rlp], stew/byteutils, ../db/core_db @@ -65,8 +66,8 @@ proc short*(h: Hash256): string = bytes[^3..^1] = h.data[^3..^1] bytes.toHex -proc decompose*(rlp: var Rlp, - header: var BlockHeader, +proc decompose*(rlp: var Rlp, + header: var BlockHeader, body: var BlockBody) {.gcsafe, raises: [RlpError].} = var blk = rlp.read(EthBlock) header = system.move(blk.header) @@ -74,8 +75,11 @@ proc decompose*(rlp: var Rlp, body.uncles = system.move(blk.uncles) body.withdrawals = system.move(blk.withdrawals) -proc decompose*(rlpBytes: openArray[byte], - header: var BlockHeader, +proc decompose*(rlpBytes: openArray[byte], + header: var BlockHeader, body: var BlockBody) {.gcsafe, raises: [RlpError].} = var rlp = rlpFromBytes(rlpBytes) rlp.decompose(header, body) + +func gwei*(n: uint64): UInt256 = + n.u256 * (10 ^ 9).u256