From f02c20c1f931b38058792edbaae0e0a8e5a8aaf7 Mon Sep 17 00:00:00 2001 From: jangko Date: Tue, 25 Jul 2023 12:28:17 +0700 Subject: [PATCH] fix EIP-4844: txpool missing dataGasUsed when generate block header --- nimbus/core/tx_pool/tx_tasks/tx_packer.nim | 1 + tests/test_txpool.nim | 1 + tests/test_txpool2.nim | 80 +++++++++++++++++++--- 3 files changed, 73 insertions(+), 9 deletions(-) diff --git a/nimbus/core/tx_pool/tx_tasks/tx_packer.nim b/nimbus/core/tx_pool/tx_tasks/tx_packer.nim index 030ac4f1b..d8ca5436d 100644 --- a/nimbus/core/tx_pool/tx_tasks/tx_packer.nim +++ b/nimbus/core/tx_pool/tx_tasks/tx_packer.nim @@ -253,6 +253,7 @@ proc vmExecCommit(pst: TxPackerStateRef) # EIP-4844 let excessDataGas = calcExcessDataGas(vmState.parent) xp.chain.excessDataGas = some(excessDataGas) + xp.chain.dataGasUsed = some(pst.dataGasUsed) proc balanceDelta: UInt256 = let postBalance = vmState.readOnlyStateDB.getBalance(xp.chain.feeRecipient) diff --git a/tests/test_txpool.nim b/tests/test_txpool.nim index 4b8e73c18..17a68890e 100644 --- a/tests/test_txpool.nim +++ b/tests/test_txpool.nim @@ -900,6 +900,7 @@ when isMainModule: runTxPoolCliqueTest() runTxPoolPosTest() + runTxPoolBlobhashTest() noisy.runTxHeadDelta #noisy.runTxLoader(dir = ".") diff --git a/tests/test_txpool2.nim b/tests/test_txpool2.nim index 09ddb95ce..55597fe4e 100644 --- a/tests/test_txpool2.nim +++ b/tests/test_txpool2.nim @@ -70,7 +70,7 @@ proc makeTx*(t: var TestEnv, recipient: EthAddress, amount: UInt256, payload: op inc t.nonce signTransaction(tx, t.vaultKey, t.chainId, eip155 = true) -proc initEnv(ttd: Option[UInt256] = none(UInt256)): TestEnv = +proc initEnv(envFork: HardFork): TestEnv = var conf = makeConfig(@[ "--engine-signer:658bdf435d810c91414ec09147daa6db62406379", @@ -81,8 +81,14 @@ proc initEnv(ttd: Option[UInt256] = none(UInt256)): TestEnv = code: contractCode ) - if ttd.isSome: - conf.networkParams.config.terminalTotalDifficulty = ttd + if envFork >= MergeFork: + conf.networkParams.config.terminalTotalDifficulty = some(100.u256) + + if envFork >= Shanghai: + conf.networkParams.config.shanghaiTime = some(0.fromUnix) + + if envFork >= Cancun: + conf.networkParams.config.cancunTime = some(0.fromUnix) let com = CommonRef.new( @@ -112,7 +118,7 @@ const proc runTxPoolCliqueTest*() = var - env = initEnv() + env = initEnv(London) var tx = env.makeTx(recipient, amount) @@ -173,7 +179,6 @@ proc runTxPoolCliqueTest*() = check rr == ValidationResult.OK test "Do not kick the signer out of list": - let timestamp = blk.header.timestamp check xp.smartHead(blk.header) let tx = env.makeTx(recipient, amount) @@ -207,7 +212,7 @@ proc runTxPoolCliqueTest*() = proc runTxPoolPosTest*() = var - env = initEnv(some(100.u256)) + env = initEnv(MergeFork) var tx = env.makeTx(recipient, amount) @@ -260,13 +265,69 @@ proc runTxPoolPosTest*() = let bal = sdb.getBalance(feeRecipient) check not bal.isZero +proc runTxPoolBlobhashTest*() = + var + env = initEnv(Cancun) + + var + tx = env.makeTx(recipient, amount) + xp = env.xp + com = env.com + chain = env.chain + body: BlockBody + blk: EthBlock + + suite "Test TxPool with blobhash block": + test "TxPool addLocal": + let res = xp.addLocal(tx, force = true) + check res.isOk + if res.isErr: + debugEcho res.error + return + + test "TxPool jobCommit": + check xp.nItems.total == 1 + + test "TxPool ethBlock": + com.pos.prevRandao = prevRandao + com.pos.feeRecipient = feeRecipient + com.pos.timestamp = getTime() + + blk = xp.ethBlock() + + check com.isBlockAfterTtd(blk.header) + + body = BlockBody( + transactions: blk.txs, + uncles: blk.uncles, + withdrawals: some[seq[Withdrawal]](@[]) + ) + check blk.txs.len == 1 + + test "Blobhash persistBlocks": + let rr = chain.persistBlocks([blk.header], [body]) + check rr == ValidationResult.OK + + test "validate TxPool prevRandao setter": + var sdb = newAccountStateDB(com.db.db, blk.header.stateRoot, pruneTrie = false) + let (val, ok) = sdb.getStorage(recipient, slot) + let randao = Hash256(data: val.toBytesBE) + check ok + check randao == prevRandao + + test "feeRecipient rewarded": + check blk.header.coinbase == feeRecipient + var sdb = newAccountStateDB(com.db.db, blk.header.stateRoot, pruneTrie = false) + let bal = sdb.getBalance(feeRecipient) + check not bal.isZero + proc runTxHeadDelta*(noisy = true) = ## see github.com/status-im/nimbus-eth1/issues/1031 suite "TxPool: Synthesising blocks (covers issue #1031)": test "Packing and adding multiple blocks to chain": var - env = initEnv(some(100.u256)) + env = initEnv(MergeFork) xp = env.xp com = env.com chain = env.chain @@ -336,7 +397,8 @@ when isMainModule: setErrorLevel() # mute logger runTxPoolCliqueTest() - #runTxPoolPosTest() - #noisy.runTxHeadDelta + runTxPoolPosTest() + runTxPoolBlobhashTest() + noisy.runTxHeadDelta # End