Skip to content

Commit 2c64270

Browse files
committed
[refactor] Don't call AcceptToMemoryPool() from outside validation.cpp
1 parent 92a3aee commit 2c64270

9 files changed

+28
-24
lines changed

src/bench/block_assemble.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ static void AssembleBlock(benchmark::Bench& bench)
3434
txs.at(b) = MakeTransactionRef(tx);
3535
}
3636
{
37-
LOCK(::cs_main); // Required for ::AcceptToMemoryPool.
37+
LOCK(::cs_main);
3838

3939
for (const auto& txr : txs) {
40-
const MempoolAcceptResult res = ::AcceptToMemoryPool(test_setup->m_node.chainman->ActiveChainstate(), *test_setup->m_node.mempool, txr, false /* bypass_limits */);
40+
const MempoolAcceptResult res = test_setup->m_node.chainman->ProcessTransaction(txr);
4141
assert(res.m_result_type == MempoolAcceptResult::ResultType::VALID);
4242
}
4343
}

src/net_processing.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -461,9 +461,9 @@ class PeerManagerImpl final : public PeerManager
461461
bool AlreadyHaveTx(const GenTxid& gtxid) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
462462

463463
/**
464-
* Filter for transactions that were recently rejected by
465-
* AcceptToMemoryPool. These are not rerequested until the chain tip
466-
* changes, at which point the entire filter is reset.
464+
* Filter for transactions that were recently rejected by the mempool.
465+
* These are not rerequested until the chain tip changes, at which point
466+
* the entire filter is reset.
467467
*
468468
* Without this filter we'd be re-requesting txs from each of our peers,
469469
* increasing bandwidth consumption considerably. For instance, with 100
@@ -2243,7 +2243,7 @@ void PeerManagerImpl::ProcessOrphanTx(std::set<uint256>& orphan_work_set)
22432243
const auto [porphanTx, from_peer] = m_orphanage.GetTx(orphanHash);
22442244
if (porphanTx == nullptr) continue;
22452245

2246-
const MempoolAcceptResult result = AcceptToMemoryPool(m_chainman.ActiveChainstate(), m_mempool, porphanTx, false /* bypass_limits */);
2246+
const MempoolAcceptResult result = m_chainman.ProcessTransaction(porphanTx);
22472247
const TxValidationState& state = result.m_state;
22482248

22492249
if (result.m_result_type == MempoolAcceptResult::ResultType::VALID) {
@@ -3260,7 +3260,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
32603260
return;
32613261
}
32623262

3263-
const MempoolAcceptResult result = AcceptToMemoryPool(m_chainman.ActiveChainstate(), m_mempool, ptx, false /* bypass_limits */);
3263+
const MempoolAcceptResult result = m_chainman.ProcessTransaction(ptx);
32643264
const TxValidationState& state = result.m_state;
32653265

32663266
if (result.m_result_type == MempoolAcceptResult::ResultType::VALID) {
@@ -3384,8 +3384,8 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
33843384
}
33853385

33863386
// If a tx has been detected by m_recent_rejects, we will have reached
3387-
// this point and the tx will have been ignored. Because we haven't run
3388-
// the tx through AcceptToMemoryPool, we won't have computed a DoS
3387+
// this point and the tx will have been ignored. Because we haven't
3388+
// submitted the tx to our mempool, we won't have computed a DoS
33893389
// score for it or determined exactly why we consider it invalid.
33903390
//
33913391
// This means we won't penalize any peer subsequently relaying a DoSy

src/node/transaction.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,15 @@ TransactionError BroadcastTransaction(NodeContext& node, const CTransactionRef t
7070
if (max_tx_fee > 0) {
7171
// First, call ATMP with test_accept and check the fee. If ATMP
7272
// fails here, return error immediately.
73-
const MempoolAcceptResult result = AcceptToMemoryPool(node.chainman->ActiveChainstate(), *node.mempool, tx, false /* bypass_limits */,
74-
true /* test_accept */);
73+
const MempoolAcceptResult result = node.chainman->ProcessTransaction(tx, /*test_accept=*/ true);
7574
if (result.m_result_type != MempoolAcceptResult::ResultType::VALID) {
7675
return HandleATMPError(result.m_state, err_string);
7776
} else if (result.m_base_fees.value() > max_tx_fee) {
7877
return TransactionError::MAX_FEE_EXCEEDED;
7978
}
8079
}
8180
// Try to submit the transaction to the mempool.
82-
const MempoolAcceptResult result = AcceptToMemoryPool(node.chainman->ActiveChainstate(), *node.mempool, tx, false /* bypass_limits */,
83-
false /* test_accept */);
81+
const MempoolAcceptResult result = node.chainman->ProcessTransaction(tx, /*test_accept=*/ false);
8482
if (result.m_result_type != MempoolAcceptResult::ResultType::VALID) {
8583
return HandleATMPError(result.m_state, err_string);
8684
}

src/rpc/rawtransaction.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -946,12 +946,13 @@ static RPCHelpMan testmempoolaccept()
946946

947947
NodeContext& node = EnsureAnyNodeContext(request.context);
948948
CTxMemPool& mempool = EnsureMemPool(node);
949-
CChainState& chainstate = EnsureChainman(node).ActiveChainstate();
949+
ChainstateManager& chainman = EnsureChainman(node);
950+
CChainState& chainstate = chainman.ActiveChainstate();
950951
const PackageMempoolAcceptResult package_result = [&] {
951952
LOCK(::cs_main);
952953
if (txns.size() > 1) return ProcessNewPackage(chainstate, mempool, txns, /* test_accept */ true);
953954
return PackageMempoolAcceptResult(txns[0]->GetWitnessHash(),
954-
AcceptToMemoryPool(chainstate, mempool, txns[0], /* bypass_limits */ false, /* test_accept*/ true));
955+
chainman.ProcessTransaction(txns[0], /*test_accept=*/ true));
955956
}();
956957

957958
UniValue rpc_result(UniValue::VARR);

src/test/txvalidation_tests.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ BOOST_FIXTURE_TEST_CASE(tx_mempool_reject_coinbase, TestChain100Setup)
3737
LOCK(cs_main);
3838

3939
unsigned int initialPoolSize = m_node.mempool->size();
40-
const MempoolAcceptResult result = AcceptToMemoryPool(m_node.chainman->ActiveChainstate(), *m_node.mempool, MakeTransactionRef(coinbaseTx),
41-
false /* bypass_limits */);
40+
const MempoolAcceptResult result = m_node.chainman->ProcessTransaction(MakeTransactionRef(coinbaseTx));
4241

4342
BOOST_CHECK(result.m_result_type == MempoolAcceptResult::ResultType::INVALID);
4443

src/test/txvalidationcache_tests.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ BOOST_FIXTURE_TEST_CASE(tx_mempool_block_doublespend, Dersig100Setup)
3636
const auto ToMemPool = [this](const CMutableTransaction& tx) {
3737
LOCK(cs_main);
3838

39-
const MempoolAcceptResult result = AcceptToMemoryPool(m_node.chainman->ActiveChainstate(), *m_node.mempool, MakeTransactionRef(tx),
40-
false /* bypass_limits */);
39+
const MempoolAcceptResult result = m_node.chainman->ProcessTransaction(MakeTransactionRef(tx));
4140
return result.m_result_type == MempoolAcceptResult::ResultType::VALID;
4241
};
4342

src/test/util/setup_common.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ CMutableTransaction TestChain100Setup::CreateValidMempoolTransaction(CTransactio
315315
// If submit=true, add transaction to the mempool.
316316
if (submit) {
317317
LOCK(cs_main);
318-
const MempoolAcceptResult result = AcceptToMemoryPool(m_node.chainman->ActiveChainstate(), *m_node.mempool.get(), MakeTransactionRef(mempool_txn), /* bypass_limits */ false);
318+
const MempoolAcceptResult result = m_node.chainman->ProcessTransaction(MakeTransactionRef(mempool_txn));
319319
assert(result.m_result_type == MempoolAcceptResult::ResultType::VALID);
320320
}
321321

src/test/validation_block_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ BOOST_AUTO_TEST_CASE(mempool_locks_reorg)
273273
{
274274
LOCK(cs_main);
275275
for (const auto& tx : txs) {
276-
const MempoolAcceptResult result = AcceptToMemoryPool(m_node.chainman->ActiveChainstate(), *m_node.mempool, tx, false /* bypass_limits */);
276+
const MempoolAcceptResult result = m_node.chainman->ProcessTransaction(tx);
277277
BOOST_REQUIRE(result.m_result_type == MempoolAcceptResult::ResultType::VALID);
278278
}
279279
}

src/validation.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,16 @@ struct PackageMempoolAcceptResult
206206
};
207207

208208
/**
209-
* (Try to) add a transaction to the memory pool.
210-
* @param[in] bypass_limits When true, don't enforce mempool fee limits.
211-
* @param[in] test_accept When true, run validation checks but don't submit to mempool.
209+
* Try to add a transaction to the mempool. This is an internal function and is
210+
* exposed only for testing. Client code should use ChainstateManager::ProcessTransaction()
211+
*
212+
* @param[in] active_chainstate Reference to the active chainstate.
213+
* @param[in] pool Reference to the node's mempool.
214+
* @param[in] tx The transaction to submit for mempool acceptance.
215+
* @param[in] bypass_limits When true, don't enforce mempool fee and capacity limits.
216+
* @param[in] test_accept When true, run validation checks but don't submit to mempool.
217+
*
218+
* @returns a MempoolAcceptResult indicating whether the transaction was accepted/rejected with reason.
212219
*/
213220
MempoolAcceptResult AcceptToMemoryPool(CChainState& active_chainstate, CTxMemPool& pool, const CTransactionRef& tx,
214221
bool bypass_limits, bool test_accept=false) EXCLUSIVE_LOCKS_REQUIRED(cs_main);

0 commit comments

Comments
 (0)