diff --git a/nimbus/utils/tx_pool.nim b/nimbus/utils/tx_pool.nim index eb8d9c334..328a21c55 100644 --- a/nimbus/utils/tx_pool.nim +++ b/nimbus/utils/tx_pool.nim @@ -600,6 +600,10 @@ proc jobDeltaTxsHead*(xp: TxPoolRef; newHead: BlockHeader): bool # Re-inject transactions, do that via job queue if 0 < changes.addTxs.len: + debug "queuing delta txs", + mode = "inject", + num = changes.addTxs.len + discard xp.job(TxJobDataRef( kind: txJobAddTxs, addTxsArgs: ( @@ -608,6 +612,10 @@ proc jobDeltaTxsHead*(xp: TxPoolRef; newHead: BlockHeader): bool # Delete already *mined* transactions if 0 < changes.remTxs.len: + debug "queuing delta txs", + mode = "remove", + num = changes.remTxs.len + discard xp.job(TxJobDataRef( kind: txJobDelItemIDs, delItemIDsArgs: ( diff --git a/nimbus/utils/tx_pool/tx_tasks/tx_head.nim b/nimbus/utils/tx_pool/tx_tasks/tx_head.nim index e0ba7646d..ce50fd691 100644 --- a/nimbus/utils/tx_pool/tx_tasks/tx_head.nim +++ b/nimbus/utils/tx_pool/tx_tasks/tx_head.nim @@ -164,8 +164,10 @@ proc headDiff*(xp: TxPoolRef; # | | # new << current (step back this one) # - # preserve transactions on the upper branch block numbers - # between #new..#current to be re-inserted into the pool + # + preserve transactions on the upper branch blocks, + # + # + txs of blocks with numbers between #new..#current need to be + # re-inserted into the pool # while newHead.blockNumber < curBranchHead.blockNumber: xp.insert(txDiffs, curBranchHash) @@ -190,11 +192,13 @@ proc headDiff*(xp: TxPoolRef; # | | # current << new (step back this one) # - # preserve transactions on the upper branch block numbers - # between #current..#new to be deleted from the pool + # + preserve some transactions on the upper branch blocks, + # + # + txs of blocks with numbers between #current..#new need to be + # deleted from the pool (as they are on the block chain, now) # while curHead.blockNumber < newBranchHead.blockNumber: - xp.remove(txDiffs, curBranchHash) + xp.remove(txDiffs, newBranchHash) let tmpHead = newBranchHead # cache value for error logging tmpHash = newBranchHash diff --git a/tests/test_txpool.nim b/tests/test_txpool.nim index 40462029a..8c0393587 100644 --- a/tests/test_txpool.nim +++ b/tests/test_txpool.nim @@ -905,8 +905,9 @@ proc txPoolMain*(noisy = defined(debug)) = noisy.runTxLoader noisy.runTxPoolTests noisy.runTxPackerTests - test_txpool2.runTxPoolCliqueTest() - test_txpool2.runTxPoolPosTest() + runTxPoolCliqueTest() + runTxPoolPosTest() + noisy.runTxHeadDelta when isMainModule: const @@ -922,8 +923,9 @@ when isMainModule: noisy.runTxPoolTests noisy.runTxPackerTests - #runTxPoolCliqueTest() - #runTxPoolPosTest() + runTxPoolCliqueTest() + runTxPoolPosTest() + noisy.runTxHeadDelta #noisy.runTxLoader(dir = ".") #noisy.runTxPoolTests diff --git a/tests/test_txpool2.nim b/tests/test_txpool2.nim index 386e6d07f..24f85cfd7 100644 --- a/tests/test_txpool2.nim +++ b/tests/test_txpool2.nim @@ -6,7 +6,7 @@ import ../nimbus/p2p/chain, ../nimbus/p2p/clique/[clique_sealer, clique_desc], ../nimbus/[chain_config, config, genesis, transaction, constants], - ../nimbus/utils/[tx_pool], + ../nimbus/utils/tx_pool, ./test_txpool/helpers, ./macro_assembler @@ -234,11 +234,11 @@ proc runTxPoolPosTest*() = #runTxPoolPosTest() -proc runTxMissingTxNoce*(noisy = true) = +proc runTxHeadDelta*(noisy = true) = ## see github.com/status-im/nimbus-eth1/issues/1031 - suite "TxPool: Issue #1031": - test "Reproducing issue #1031": + suite "TxPool: Synthesising blocks (covers issue #1031)": + test "Packing and adding multiple blocks to chain": var env = initEnv(some(100.u256)) xp = env.xp @@ -266,9 +266,10 @@ proc runTxMissingTxNoce*(noisy = true) = xp.jobCommit() - noisy.say "***", "stats", + noisy.say "***", "txDB", &" n={n}", - &" pending/staged/packed:total/disposed={xp.nItems.pp}" + # pending/staged/packed : total/disposed + &" stats={xp.nItems.pp}" xp.prevRandao = prevRandao var blk = xp.ethBlock() @@ -285,15 +286,27 @@ proc runTxMissingTxNoce*(noisy = true) = transactions: blk.txs, uncles: blk.uncles) - let rr = chain.persistBlocks([blk.header], [body]) - check rr.isOk + # Commit to block chain + check chain.persistBlocks([blk.header], [body]).isOk - # PoS block canonical head must be explicitly set using setHead + # PoS block canonical head must be explicitly set using setHead. The + # function `persistHeaderToDb()` used in `persistBlocks()` does not + # reliably do so due to scoring. chainDB.setHead(blk.header) - discard xp.jobDeltaTxsHead(blk.header) + # Synchronise TxPool against new chain head, register txs differences. + # In this particular case, these differences will simply flush the + # packer bucket. + check xp.jobDeltaTxsHead(blk.header) + check xp.nJobs == 1 + + # Move TxPool chain head to new chain head and apply delta jobs xp.head = blk.header xp.jobCommit() + check xp.nItems.staged == 0 + check xp.nItems.packed == 0 + + setErrorLevel() # in case we set trace level check chainDB.currentBlock == 10.toBlockNumber head = chainDB.getBlockHeader(chainDB.currentBlock) @@ -311,7 +324,7 @@ when isMainModule: setErrorLevel() # mute logger - runTxPoolCliqueTest() - runTxPoolPosTest() + #runTxPoolCliqueTest() + #runTxPoolPosTest() - true.runTxMissingTxNoce + true.runTxHeadDelta