Skip to content

Commit 8bdab4d

Browse files
committed
merge bitcoin#23437: AcceptToMemoryPool
1 parent 1f4e8a0 commit 8bdab4d

File tree

3 files changed

+27
-29
lines changed

3 files changed

+27
-29
lines changed

src/test/fuzz/tx_pool.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ void Finish(FuzzedDataProvider& fuzzed_data_provider, MockedTxPool& tx_pool, con
8686
options.nBlockMaxSize = fuzzed_data_provider.ConsumeIntegralInRange(0U, MaxBlockSize(true));
8787
options.blockMinFeeRate = CFeeRate{ConsumeMoney(fuzzed_data_provider, /* max */ COIN)};
8888

89-
auto assembler = BlockAssembler{chainstate, node, *static_cast<CTxMemPool*>(&tx_pool), ::Params(), options};
89+
auto assembler = BlockAssembler{chainstate, node, *static_cast<CTxMemPool*>(&tx_pool), chainstate.m_params, options};
9090
auto block_template = assembler.CreateNewBlock(CScript{} << OP_TRUE);
9191
Assert(block_template->block.vtx.size() >= 1);
9292
}
@@ -222,13 +222,13 @@ FUZZ_TARGET_INIT(tx_pool_standard, initialize_tx_pool)
222222
// Make sure ProcessNewPackage on one transaction works and always fully validates the transaction.
223223
// The result is not guaranteed to be the same as what is returned by ATMP.
224224
const auto result_package = WITH_LOCK(::cs_main,
225-
return ProcessNewPackage(node.chainman->ActiveChainstate(), tx_pool, {tx}, true));
225+
return ProcessNewPackage(chainstate, tx_pool, {tx}, true));
226226
auto it = result_package.m_tx_results.find(tx->GetHash());
227227
Assert(it != result_package.m_tx_results.end());
228228
Assert(it->second.m_result_type == MempoolAcceptResult::ResultType::VALID ||
229229
it->second.m_result_type == MempoolAcceptResult::ResultType::INVALID);
230230

231-
const auto res = WITH_LOCK(::cs_main, return AcceptToMemoryPool(chainstate, tx_pool, tx, bypass_limits));
231+
const auto res = WITH_LOCK(::cs_main, return AcceptToMemoryPool(tx_pool, chainstate, tx, GetTime(), bypass_limits, /* test_accept= */ false));
232232
const bool accepted = res.m_result_type == MempoolAcceptResult::ResultType::VALID;
233233
SyncWithValidationInterfaceQueue();
234234
UnregisterSharedValidationInterface(txr);
@@ -328,7 +328,7 @@ FUZZ_TARGET_INIT(tx_pool, initialize_tx_pool)
328328
const auto tx = MakeTransactionRef(mut_tx);
329329
const bool bypass_limits = fuzzed_data_provider.ConsumeBool();
330330
::fRequireStandard = fuzzed_data_provider.ConsumeBool();
331-
const auto res = WITH_LOCK(::cs_main, return AcceptToMemoryPool(node.chainman->ActiveChainstate(), tx_pool, tx, bypass_limits));
331+
const auto res = WITH_LOCK(::cs_main, return AcceptToMemoryPool(tx_pool, chainstate, tx, GetTime(), bypass_limits, /* test_accept= */ false));
332332
const bool accepted = res.m_result_type == MempoolAcceptResult::ResultType::VALID;
333333
if (accepted) {
334334
txids.push_back(tx->GetHash());

src/validation.cpp

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,9 @@ void CChainState::MaybeUpdateMempoolForReorg(
354354
while (it != disconnectpool.queuedTx.get<insertion_order>().rend()) {
355355
// ignore validation errors in resurrected transactions
356356
if (!fAddToMempool || (*it)->IsCoinBase() ||
357-
AcceptToMemoryPool(*this, *m_mempool, *it, true /* bypass_limits */).m_result_type != MempoolAcceptResult::ResultType::VALID) {
357+
AcceptToMemoryPool(*m_mempool, *this, *it, GetTime(),
358+
/* bypass_limits= */true, /* test_accept= */ false).m_result_type !=
359+
MempoolAcceptResult::ResultType::VALID) {
358360
// If the transaction doesn't make it in to the mempool, remove any
359361
// transactions that depend on it (which would now be orphans).
360362
m_mempool->removeRecursive(**it, MemPoolRemovalReason::REORG);
@@ -978,16 +980,13 @@ PackageMempoolAcceptResult MemPoolAccept::AcceptMultipleTransactions(const std::
978980

979981
} // anon namespace
980982

981-
/** (try to) add transaction to memory pool with a specified acceptance time **/
982-
static MempoolAcceptResult AcceptToMemoryPoolWithTime(const CChainParams& chainparams, CTxMemPool& pool, CChainState& active_chainstate,
983-
const CTransactionRef &tx, int64_t nAcceptTime,
984-
bool bypass_limits, bool test_accept)
983+
MempoolAcceptResult AcceptToMemoryPool(CTxMemPool& pool, CChainState& active_chainstate, const CTransactionRef& tx,
984+
int64_t accept_time, bool bypass_limits, bool test_accept)
985985
EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
986986
{
987-
AssertLockHeld(::cs_main);
987+
const CChainParams& chainparams{active_chainstate.m_params};
988988
std::vector<COutPoint> coins_to_uncache;
989-
MemPoolAccept::ATMPArgs args { chainparams, nAcceptTime, bypass_limits, coins_to_uncache, test_accept };
990-
989+
MemPoolAccept::ATMPArgs args { chainparams, accept_time, bypass_limits, coins_to_uncache, test_accept };
991990
const MempoolAcceptResult result = MemPoolAccept(pool, active_chainstate).AcceptSingleTransaction(tx, args);
992991
if (result.m_result_type != MempoolAcceptResult::ResultType::VALID || test_accept) {
993992
if (result.m_result_type != MempoolAcceptResult::ResultType::VALID) {
@@ -1008,11 +1007,6 @@ static MempoolAcceptResult AcceptToMemoryPoolWithTime(const CChainParams& chainp
10081007
return result;
10091008
}
10101009

1011-
MempoolAcceptResult AcceptToMemoryPool(CChainState& active_chainstate, CTxMemPool& pool, const CTransactionRef &tx, bool bypass_limits, bool test_accept)
1012-
{
1013-
return AcceptToMemoryPoolWithTime(Params(), pool, active_chainstate, tx, GetTime(), bypass_limits, test_accept);
1014-
}
1015-
10161010
PackageMempoolAcceptResult ProcessNewPackage(CChainState& active_chainstate, CTxMemPool& pool,
10171011
const Package& package, bool test_accept)
10181012
{
@@ -1232,12 +1226,12 @@ CChainState::CChainState(CTxMemPool* mempool,
12321226
const std::unique_ptr<llmq::CInstantSendManager>& isman,
12331227
std::optional<uint256> from_snapshot_blockhash)
12341228
: m_mempool(mempool),
1235-
m_params(::Params()),
12361229
m_chain_helper(chain_helper),
12371230
m_clhandler(clhandler),
12381231
m_isman(isman),
12391232
m_evoDb(evoDb),
12401233
m_blockman(blockman),
1234+
m_params(::Params()),
12411235
m_chainman(chainman),
12421236
m_from_snapshot_blockhash(from_snapshot_blockhash) {}
12431237

@@ -4001,7 +3995,7 @@ MempoolAcceptResult ChainstateManager::ProcessTransaction(const CTransactionRef&
40013995
state.Invalid(TxValidationResult::TX_NO_MEMPOOL, "no-mempool");
40023996
return MempoolAcceptResult::Failure(state);
40033997
}
4004-
auto result = AcceptToMemoryPool(active_chainstate, *active_chainstate.m_mempool, tx, bypass_limits, test_accept);
3998+
auto result = AcceptToMemoryPool(*active_chainstate.m_mempool, active_chainstate, tx, GetTime(), bypass_limits, test_accept);
40053999
active_chainstate.m_mempool->check(active_chainstate.CoinsTip(), active_chainstate.m_chain.Height() + 1);
40064000
return result;
40074001
}
@@ -4966,7 +4960,6 @@ static const uint64_t MEMPOOL_DUMP_VERSION = 1;
49664960

49674961
bool LoadMempool(CTxMemPool& pool, CChainState& active_chainstate, FopenFn mockable_fopen_function)
49684962
{
4969-
const CChainParams& chainparams = Params();
49704963
int64_t nExpiryTimeout = gArgs.GetArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY) * 60 * 60;
49714964
FILE* filestr{mockable_fopen_function(gArgs.GetDataDirNet() / "mempool.dat", "rb")};
49724965
CAutoFile file(filestr, SER_DISK, CLIENT_VERSION);
@@ -5005,8 +4998,8 @@ bool LoadMempool(CTxMemPool& pool, CChainState& active_chainstate, FopenFn mocka
50054998
}
50064999
if (nTime > nNow - nExpiryTimeout) {
50075000
LOCK(cs_main);
5008-
if (AcceptToMemoryPoolWithTime(chainparams, pool, active_chainstate, tx, nTime, false /* bypass_limits */,
5009-
false /* test_accept */).m_result_type == MempoolAcceptResult::ResultType::VALID) {
5001+
if (AcceptToMemoryPool(pool, active_chainstate, tx, nTime, /* bypass_limits= */ false,
5002+
/* test_accept= */ false).m_result_type == MempoolAcceptResult::ResultType::VALID) {
50105003
++count;
50115004
} else {
50125005
// mempool may contain the transaction already, e.g. from

src/validation.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -240,19 +240,23 @@ struct PackageMempoolAcceptResult
240240
};
241241

242242
/**
243-
* Try to add a transaction to the mempool. This is an internal function and is
244-
* exposed only for testing. Client code should use ChainstateManager::ProcessTransaction()
243+
* Try to add a transaction to the mempool. This is an internal function and is exposed only for testing.
244+
* Client code should use ChainstateManager::ProcessTransaction()
245245
*
246-
* @param[in] active_chainstate Reference to the active chainstate.
247246
* @param[in] pool Reference to the node's mempool.
247+
* @param[in] active_chainstate Reference to the active chainstate.
248248
* @param[in] tx The transaction to submit for mempool acceptance.
249+
* @param[in] accept_time The timestamp for adding the transaction to the mempool. Usually
250+
* the current system time, but may be different.
251+
* It is also used to determine when the entry expires.
249252
* @param[in] bypass_limits When true, don't enforce mempool fee and capacity limits.
250253
* @param[in] test_accept When true, run validation checks but don't submit to mempool.
251254
*
252255
* @returns a MempoolAcceptResult indicating whether the transaction was accepted/rejected with reason.
253256
*/
254-
MempoolAcceptResult AcceptToMemoryPool(CChainState& active_chainstate, CTxMemPool& pool, const CTransactionRef& tx,
255-
bool bypass_limits, bool test_accept=false) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
257+
MempoolAcceptResult AcceptToMemoryPool(CTxMemPool& pool, CChainState& active_chainstate, const CTransactionRef& tx,
258+
int64_t accept_time, bool bypass_limits, bool test_accept)
259+
EXCLUSIVE_LOCKS_REQUIRED(cs_main);
256260

257261
/**
258262
* Atomically test acceptance of a package. If the package only contains one tx, package rules still
@@ -486,8 +490,6 @@ class CChainState
486490
//! Only the active chainstate has a mempool.
487491
CTxMemPool* m_mempool;
488492

489-
const CChainParams& m_params;
490-
491493
//! Manages the UTXO set, which is a reflection of the contents of `m_chain`.
492494
std::unique_ptr<CoinsViews> m_coins_views;
493495

@@ -502,6 +504,9 @@ class CChainState
502504
//! CChainState instances.
503505
BlockManager& m_blockman;
504506

507+
/** Chain parameters for this chainstate */
508+
const CChainParams& m_params;
509+
505510
//! The chainstate manager that owns this chainstate. The reference is
506511
//! necessary so that this instance can check whether it is the active
507512
//! chainstate within deeply nested method calls.

0 commit comments

Comments
 (0)