diff --git a/src/test/mempool_tests.cpp b/src/test/mempool_tests.cpp --- a/src/test/mempool_tests.cpp +++ b/src/test/mempool_tests.cpp @@ -875,6 +875,7 @@ {disconnectedTxnsInOrder, disconnectedTxnsMixedOrder, disconnectedTxnsInvertedOrder}) { for (auto &unconfTxns : {unconfTxnsInOrder, unconfTxnsOutOfOrder}) { + CTxMemPool testPool; // addForBlock inserts disconnectTxns in disconnectPool. They // simulate transactions that were once confirmed in a block std::vector vtx; @@ -882,14 +883,12 @@ vtx.push_back(MakeTransactionRef(*tx)); } DisconnectedBlockTransactions disconnectPool; - disconnectPool.addForBlock(vtx); + disconnectPool.addForBlock(vtx, testPool); CheckDisconnectPoolOrder(disconnectPool, correctlyOrderedIds, disconnectedTxns.size()); // If the mempool is empty, importMempool doesn't change // disconnectPool - CTxMemPool testPool; - disconnectPool.importMempool(testPool); CheckDisconnectPoolOrder(disconnectPool, correctlyOrderedIds, disconnectedTxns.size()); diff --git a/src/txmempool.h b/src/txmempool.h --- a/src/txmempool.h +++ b/src/txmempool.h @@ -925,7 +925,7 @@ // Add entries for a block while reconstructing the topological ordering so // they can be added back to the mempool simply. - void addForBlock(const std::vector &vtx); + void addForBlock(const std::vector &vtx, CTxMemPool &pool); // Remove entries based on txid_index, and update memory usage. void removeForBlock(const std::vector &vtx) { @@ -969,7 +969,8 @@ * Passing fAddToMempool=false will skip trying to add the transactions * back, and instead just erase from the mempool as needed. */ - void updateMempoolForReorg(const Config &config, bool fAddToMempool); + void updateMempoolForReorg(const Config &config, bool fAddToMempool, + CTxMemPool &pool); }; #endif // BITCOIN_TXMEMPOOL_H diff --git a/src/txmempool.cpp b/src/txmempool.cpp --- a/src/txmempool.cpp +++ b/src/txmempool.cpp @@ -636,7 +636,7 @@ LOCK(cs); DisconnectedBlockTransactions disconnectpool; - disconnectpool.addForBlock(vtx); + disconnectpool.addForBlock(vtx, *this); std::vector entries; for (const CTransactionRef &tx : @@ -1284,7 +1284,7 @@ static const size_t MAX_DISCONNECTED_TX_POOL_SIZE = 20 * DEFAULT_MAX_BLOCK_SIZE; void DisconnectedBlockTransactions::addForBlock( - const std::vector &vtx) { + const std::vector &vtx, CTxMemPool &pool) { for (const auto &tx : reverse_iterate(vtx)) { // If we already added it, just skip. auto it = queuedTx.find(tx->GetId()); @@ -1336,7 +1336,7 @@ // Drop the earliest entry, and remove its children from the // mempool. auto it = queuedTx.get().begin(); - g_mempool.removeRecursive(**it, MemPoolRemovalReason::REORG); + pool.removeRecursive(**it, MemPoolRemovalReason::REORG); removeEntry(it); } } @@ -1364,7 +1364,7 @@ // Use addForBlocks to sort the transactions and then splice them in front // of queuedTx DisconnectedBlockTransactions orderedTxnPool; - orderedTxnPool.addForBlock(vtx); + orderedTxnPool.addForBlock(vtx, pool); cachedInnerUsage += orderedTxnPool.cachedInnerUsage; queuedTx.get().splice( queuedTx.get().begin(), @@ -1379,7 +1379,8 @@ } void DisconnectedBlockTransactions::updateMempoolForReorg(const Config &config, - bool fAddToMempool) { + bool fAddToMempool, + CTxMemPool &pool) { AssertLockHeld(cs_main); std::vector txidsUpdate; @@ -1394,13 +1395,13 @@ // ignore validation errors in resurrected transactions TxValidationState stateDummy; if (!fAddToMempool || tx->IsCoinBase() || - !AcceptToMemoryPool(config, g_mempool, stateDummy, tx, + !AcceptToMemoryPool(config, pool, stateDummy, tx, true /* bypass_limits */, Amount::zero() /* nAbsurdFee */)) { // If the transaction doesn't make it in to the mempool, remove any // transactions that depend on it (which would now be orphans). - g_mempool.removeRecursive(*tx, MemPoolRemovalReason::REORG); - } else if (g_mempool.exists(tx->GetId())) { + pool.removeRecursive(*tx, MemPoolRemovalReason::REORG); + } else if (pool.exists(tx->GetId())) { txidsUpdate.push_back(tx->GetId()); } } @@ -1412,16 +1413,16 @@ // previously-confirmed transactions back to the mempool. // UpdateTransactionsFromBlock finds descendants of any transactions in the // disconnectpool that were added back and cleans up the mempool state. - g_mempool.UpdateTransactionsFromBlock(txidsUpdate); + pool.UpdateTransactionsFromBlock(txidsUpdate); // We also need to remove any now-immature transactions - g_mempool.removeForReorg(config, pcoinsTip.get(), - ::ChainActive().Tip()->nHeight + 1, - STANDARD_LOCKTIME_VERIFY_FLAGS); + pool.removeForReorg(config, pcoinsTip.get(), + ::ChainActive().Tip()->nHeight + 1, + STANDARD_LOCKTIME_VERIFY_FLAGS); // Re-limit mempool size, in case we added any transactions - g_mempool.LimitSize( - gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000, - std::chrono::hours{ - gArgs.GetArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY)}); + pool.LimitSize(gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * + 1000000, + std::chrono::hours{ + gArgs.GetArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY)}); } diff --git a/src/validation.cpp b/src/validation.cpp --- a/src/validation.cpp +++ b/src/validation.cpp @@ -2157,7 +2157,7 @@ } if (disconnectpool) { - disconnectpool->addForBlock(block.vtx); + disconnectpool->addForBlock(block.vtx, g_mempool); } // If the tip is finalized, then undo it. @@ -2659,7 +2659,7 @@ if (!DisconnectTip(config.GetChainParams(), state, &disconnectpool)) { // This is likely a fatal error, but keep the mempool consistent, // just in case. Only remove from the mempool in this case. - disconnectpool.updateMempoolForReorg(config, false); + disconnectpool.updateMempoolForReorg(config, false, g_mempool); // If we're unable to disconnect a block during normal operation, // then that is a failure of our local system -- we should abort @@ -2712,7 +2712,7 @@ // A system error occurred (disk space, database error, ...). // Make the mempool consistent with the current tip, just in // case any observers try to use it before shutdown. - disconnectpool.updateMempoolForReorg(config, false); + disconnectpool.updateMempoolForReorg(config, false, g_mempool); return false; } else { PruneBlockIndexCandidates(); @@ -2734,7 +2734,7 @@ // effect. LogPrint(BCLog::MEMPOOL, "Updating mempool due to reorganization or " "rules upgrade/downgrade\n"); - disconnectpool.updateMempoolForReorg(config, true); + disconnectpool.updateMempoolForReorg(config, true, g_mempool); } g_mempool.check(pcoinsTip.get()); @@ -3047,7 +3047,8 @@ // and we're not doing a very deep invalidation (in which case // keeping the mempool up to date is probably futile anyway). disconnectpool.updateMempoolForReorg( - config, /* fAddToMempool = */ (++disconnected <= 10) && ret); + config, /* fAddToMempool = */ (++disconnected <= 10) && ret, + g_mempool); if (!ret) { return false;