Skip to content

Commit 84d921e

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#23465: Remove CTxMemPool params from ATMP
f1f10c0 Remove CTxMemPool params from ATMP (lsilva01) Pull request description: Remove `CTxMemPool` parameter from `AcceptToMemoryPool` function, as suggested in bitcoin/bitcoin#23437 (comment) . This requires that `CChainState` has access to `MockedTxPool` in `tx_pool.cpp` as mentioned bitcoin/bitcoin#23173 (comment). So the `MockedTxPool` is attributed to `CChainState::m_mempool` before calling `AcceptToMemoryPool`. Requires #23437. ACKs for top commit: jnewbery: utACK f1f10c0 MarcoFalke: review ACK f1f10c0 🔙 Tree-SHA512: 2a4885f4645014fc1fa98bb1090f13721c1a0796bc0021b9cb43bc8cc13920b6eaf057d1f5ed796e0a110e7813e41fe0196334ce7c80d1231fc057a9a3bdf349
2 parents 099325a + f1f10c0 commit 84d921e

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).
@@ -1094,11 +1094,14 @@ PackageMempoolAcceptResult MemPoolAccept::AcceptMultipleTransactions(const std::
10941094

10951095
} // anon namespace
10961096

1097-
MempoolAcceptResult AcceptToMemoryPool(CTxMemPool& pool, CChainState& active_chainstate, const CTransactionRef& tx,
1097+
MempoolAcceptResult AcceptToMemoryPool(CChainState& active_chainstate, const CTransactionRef& tx,
10981098
int64_t accept_time, bool bypass_limits, bool test_accept)
10991099
EXCLUSIVE_LOCKS_REQUIRED(cs_main)
11001100
{
11011101
const CChainParams& chainparams{active_chainstate.m_params};
1102+
assert(active_chainstate.GetMempool() != nullptr);
1103+
CTxMemPool& pool{*active_chainstate.GetMempool()};
1104+
11021105
std::vector<COutPoint> coins_to_uncache;
11031106
auto args = MemPoolAccept::ATMPArgs::SingleAccept(chainparams, accept_time, bypass_limits, coins_to_uncache, test_accept);
11041107
const MempoolAcceptResult result = MemPoolAccept(pool, active_chainstate).AcceptSingleTransaction(tx, args);
@@ -3496,13 +3499,13 @@ bool ChainstateManager::ProcessNewBlock(const CChainParams& chainparams, const s
34963499
MempoolAcceptResult ChainstateManager::ProcessTransaction(const CTransactionRef& tx, bool test_accept)
34973500
{
34983501
CChainState& active_chainstate = ActiveChainstate();
3499-
if (!active_chainstate.m_mempool) {
3502+
if (!active_chainstate.GetMempool()) {
35003503
TxValidationState state;
35013504
state.Invalid(TxValidationResult::TX_NO_MEMPOOL, "no-mempool");
35023505
return MempoolAcceptResult::Failure(state);
35033506
}
3504-
auto result = AcceptToMemoryPool(*active_chainstate.m_mempool, active_chainstate, tx, GetTime(), /* bypass_limits= */ false, test_accept);
3505-
active_chainstate.m_mempool->check(active_chainstate.CoinsTip(), active_chainstate.m_chain.Height() + 1);
3507+
auto result = AcceptToMemoryPool(active_chainstate, tx, GetTime(), /*bypass_limits=*/ false, test_accept);
3508+
active_chainstate.GetMempool()->check(active_chainstate.CoinsTip(), active_chainstate.m_chain.Height() + 1);
35063509
return result;
35073510
}
35083511

@@ -4568,8 +4571,8 @@ bool LoadMempool(CTxMemPool& pool, CChainState& active_chainstate, FopenFn mocka
45684571
}
45694572
if (nTime > nNow - nExpiryTimeout) {
45704573
LOCK(cs_main);
4571-
if (AcceptToMemoryPool(pool, active_chainstate, tx, nTime, /* bypass_limits= */ false,
4572-
/* test_accept= */ false).m_result_type == MempoolAcceptResult::ResultType::VALID) {
4574+
const auto& accepted = AcceptToMemoryPool(active_chainstate, tx, nTime, /*bypass_limits=*/false, /*test_accept=*/false);
4575+
if (accepted.m_result_type == MempoolAcceptResult::ResultType::VALID) {
45734576
++count;
45744577
} else {
45754578
// 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

@@ -649,6 +647,12 @@ class CChainState
649647
return m_coins_views->m_dbview;
650648
}
651649

650+
//! @returns A pointer to the mempool.
651+
CTxMemPool* GetMempool()
652+
{
653+
return m_mempool;
654+
}
655+
652656
//! @returns A reference to a wrapped view of the in-memory UTXO set that
653657
//! handles disk read errors gracefully.
654658
CCoinsViewErrorCatcher& CoinsErrorCatcher() EXCLUSIVE_LOCKS_REQUIRED(cs_main)

0 commit comments

Comments
 (0)