Skip to content

Commit f1f10c0

Browse files
lsilva01jnewberyjonatack
committed
Remove CTxMemPool params from ATMP
Co-authored-by: John Newbery <[email protected]> Co-authored-by: Jon Atack <[email protected]>
1 parent df562d6 commit f1f10c0

File tree

3 files changed

+33
-15
lines changed

3 files changed

+33
-15
lines changed

src/test/fuzz/tx_pool.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ struct MockedTxPool : public CTxMemPool {
2929
}
3030
};
3131

32+
class DummyChainState final : public CChainState
33+
{
34+
public:
35+
void SetMempool(CTxMemPool* mempool)
36+
{
37+
m_mempool = mempool;
38+
}
39+
};
40+
3241
void initialize_tx_pool()
3342
{
3443
static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
@@ -114,7 +123,7 @@ FUZZ_TARGET_INIT(tx_pool_standard, initialize_tx_pool)
114123
{
115124
FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
116125
const auto& node = g_setup->m_node;
117-
auto& chainstate = node.chainman->ActiveChainstate();
126+
auto& chainstate{static_cast<DummyChainState&>(node.chainman->ActiveChainstate())};
118127

119128
MockTime(fuzzed_data_provider, chainstate);
120129
SetMempoolConstraints(*node.args, fuzzed_data_provider);
@@ -134,6 +143,8 @@ FUZZ_TARGET_INIT(tx_pool_standard, initialize_tx_pool)
134143
CTxMemPool tx_pool_{/*estimator=*/nullptr, /*check_ratio=*/1};
135144
MockedTxPool& tx_pool = *static_cast<MockedTxPool*>(&tx_pool_);
136145

146+
chainstate.SetMempool(&tx_pool);
147+
137148
// Helper to query an amount
138149
const CCoinsViewMemPool amount_view{WITH_LOCK(::cs_main, return &chainstate.CoinsTip()), tx_pool};
139150
const auto GetAmount = [&](const COutPoint& outpoint) {
@@ -230,7 +241,7 @@ FUZZ_TARGET_INIT(tx_pool_standard, initialize_tx_pool)
230241
Assert(it->second.m_result_type == MempoolAcceptResult::ResultType::VALID ||
231242
it->second.m_result_type == MempoolAcceptResult::ResultType::INVALID);
232243

233-
const auto res = WITH_LOCK(::cs_main, return AcceptToMemoryPool(tx_pool, chainstate, tx, GetTime(), bypass_limits, /* test_accept= */ false));
244+
const auto res = WITH_LOCK(::cs_main, return AcceptToMemoryPool(chainstate, tx, GetTime(), bypass_limits, /*test_accept=*/false));
234245
const bool accepted = res.m_result_type == MempoolAcceptResult::ResultType::VALID;
235246
SyncWithValidationInterfaceQueue();
236247
UnregisterSharedValidationInterface(txr);
@@ -330,7 +341,7 @@ FUZZ_TARGET_INIT(tx_pool, initialize_tx_pool)
330341
const auto tx = MakeTransactionRef(mut_tx);
331342
const bool bypass_limits = fuzzed_data_provider.ConsumeBool();
332343
::fRequireStandard = fuzzed_data_provider.ConsumeBool();
333-
const auto res = WITH_LOCK(::cs_main, return AcceptToMemoryPool(tx_pool, chainstate, tx, GetTime(), bypass_limits, /* test_accept= */ false));
344+
const auto res = WITH_LOCK(::cs_main, return AcceptToMemoryPool(chainstate, tx, GetTime(), bypass_limits, /*test_accept=*/false));
334345
const bool accepted = res.m_result_type == MempoolAcceptResult::ResultType::VALID;
335346
if (accepted) {
336347
txids.push_back(tx->GetHash());

src/validation.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,8 @@ void CChainState::MaybeUpdateMempoolForReorg(
331331
while (it != disconnectpool.queuedTx.get<insertion_order>().rend()) {
332332
// ignore validation errors in resurrected transactions
333333
if (!fAddToMempool || (*it)->IsCoinBase() ||
334-
AcceptToMemoryPool(*m_mempool, *this, *it, GetTime(),
335-
/* bypass_limits= */true, /* test_accept= */ false).m_result_type !=
334+
AcceptToMemoryPool(*this, *it, GetTime(),
335+
/*bypass_limits=*/true, /*test_accept=*/false).m_result_type !=
336336
MempoolAcceptResult::ResultType::VALID) {
337337
// If the transaction doesn't make it in to the mempool, remove any
338338
// transactions that depend on it (which would now be orphans).
@@ -1091,11 +1091,14 @@ PackageMempoolAcceptResult MemPoolAccept::AcceptMultipleTransactions(const std::
10911091

10921092
} // anon namespace
10931093

1094-
MempoolAcceptResult AcceptToMemoryPool(CTxMemPool& pool, CChainState& active_chainstate, const CTransactionRef& tx,
1094+
MempoolAcceptResult AcceptToMemoryPool(CChainState& active_chainstate, const CTransactionRef& tx,
10951095
int64_t accept_time, bool bypass_limits, bool test_accept)
10961096
EXCLUSIVE_LOCKS_REQUIRED(cs_main)
10971097
{
10981098
const CChainParams& chainparams{active_chainstate.m_params};
1099+
assert(active_chainstate.GetMempool() != nullptr);
1100+
CTxMemPool& pool{*active_chainstate.GetMempool()};
1101+
10991102
std::vector<COutPoint> coins_to_uncache;
11001103
auto args = MemPoolAccept::ATMPArgs::SingleAccept(chainparams, accept_time, bypass_limits, coins_to_uncache, test_accept);
11011104
const MempoolAcceptResult result = MemPoolAccept(pool, active_chainstate).AcceptSingleTransaction(tx, args);
@@ -3500,13 +3503,13 @@ bool ChainstateManager::ProcessNewBlock(const CChainParams& chainparams, const s
35003503
MempoolAcceptResult ChainstateManager::ProcessTransaction(const CTransactionRef& tx, bool test_accept)
35013504
{
35023505
CChainState& active_chainstate = ActiveChainstate();
3503-
if (!active_chainstate.m_mempool) {
3506+
if (!active_chainstate.GetMempool()) {
35043507
TxValidationState state;
35053508
state.Invalid(TxValidationResult::TX_NO_MEMPOOL, "no-mempool");
35063509
return MempoolAcceptResult::Failure(state);
35073510
}
3508-
auto result = AcceptToMemoryPool(*active_chainstate.m_mempool, active_chainstate, tx, GetTime(), /* bypass_limits= */ false, test_accept);
3509-
active_chainstate.m_mempool->check(active_chainstate.CoinsTip(), active_chainstate.m_chain.Height() + 1);
3511+
auto result = AcceptToMemoryPool(active_chainstate, tx, GetTime(), /*bypass_limits=*/ false, test_accept);
3512+
active_chainstate.GetMempool()->check(active_chainstate.CoinsTip(), active_chainstate.m_chain.Height() + 1);
35103513
return result;
35113514
}
35123515

@@ -4572,8 +4575,8 @@ bool LoadMempool(CTxMemPool& pool, CChainState& active_chainstate, FopenFn mocka
45724575
}
45734576
if (nTime > nNow - nExpiryTimeout) {
45744577
LOCK(cs_main);
4575-
if (AcceptToMemoryPool(pool, active_chainstate, tx, nTime, /* bypass_limits= */ false,
4576-
/* test_accept= */ false).m_result_type == MempoolAcceptResult::ResultType::VALID) {
4578+
const auto& accepted = AcceptToMemoryPool(active_chainstate, tx, nTime, /*bypass_limits=*/false, /*test_accept=*/false);
4579+
if (accepted.m_result_type == MempoolAcceptResult::ResultType::VALID) {
45774580
++count;
45784581
} else {
45794582
// mempool may contain the transaction already, e.g. from

src/validation.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,18 +211,16 @@ struct PackageMempoolAcceptResult
211211
* Try to add a transaction to the mempool. This is an internal function and is exposed only for testing.
212212
* Client code should use ChainstateManager::ProcessTransaction()
213213
*
214-
* @param[in] pool Reference to the node's mempool.
215214
* @param[in] active_chainstate Reference to the active chainstate.
216215
* @param[in] tx The transaction to submit for mempool acceptance.
217-
* @param[in] accept_time The timestamp for adding the transaction to the mempool. Usually
218-
* the current system time, but may be different.
216+
* @param[in] accept_time The timestamp for adding the transaction to the mempool.
219217
* It is also used to determine when the entry expires.
220218
* @param[in] bypass_limits When true, don't enforce mempool fee and capacity limits.
221219
* @param[in] test_accept When true, run validation checks but don't submit to mempool.
222220
*
223221
* @returns a MempoolAcceptResult indicating whether the transaction was accepted/rejected with reason.
224222
*/
225-
MempoolAcceptResult AcceptToMemoryPool(CTxMemPool& pool, CChainState& active_chainstate, const CTransactionRef& tx,
223+
MempoolAcceptResult AcceptToMemoryPool(CChainState& active_chainstate, const CTransactionRef& tx,
226224
int64_t accept_time, bool bypass_limits, bool test_accept)
227225
EXCLUSIVE_LOCKS_REQUIRED(cs_main);
228226

@@ -656,6 +654,12 @@ class CChainState
656654
return m_coins_views->m_dbview;
657655
}
658656

657+
//! @returns A pointer to the mempool.
658+
CTxMemPool* GetMempool()
659+
{
660+
return m_mempool;
661+
}
662+
659663
//! @returns A reference to a wrapped view of the in-memory UTXO set that
660664
//! handles disk read errors gracefully.
661665
CCoinsViewErrorCatcher& CoinsErrorCatcher() EXCLUSIVE_LOCKS_REQUIRED(cs_main)

0 commit comments

Comments
 (0)