Skip to content

Commit 828bb77

Browse files
author
MarcoFalke
committed
Merge #20750: [Bundle 2/n] Prune g_chainman usage in mempool-related validation functions
e8ae1db style-only: Make AcceptToMemoryPool signature readable (Carl Dong) 8f5c100 style-only: Make CheckSequenceLock signature readable (Carl Dong) 8c82481 validation: Use *this in CChainState::LoadMempool (Carl Dong) 0a9a24d validation: Pass in chainstate to UpdateMempoolForReorg (Carl Dong) 7142018 validation: Pass in chainstate to CTxMemPool::removeForReorg (Carl Dong) 71734c6 validation: Pass in chain to ::TestLockPointValidity (Carl Dong) 120aaba tree-wide: Fix erroneous AcceptToMemoryPool replacements (Carl Dong) 417dafc validation: Remove old AcceptToMemoryPool w/o chainstate param (Carl Dong) 3704433 scripted-diff: Invoke ::AcceptToMemoryPool with chainstate (Carl Dong) 229bc37 validation: Pass in chainstate to ::AcceptToMemoryPool (Carl Dong) d0da7ea validation: Pass in chainstate to ::LoadMempool (Carl Dong) 3a205c4 validation: Pass in chainstate to AcceptToMemoryPoolWithTime (Carl Dong) d8a8163 validation: Add chainstate member to MemPoolAccept (Carl Dong) 4c15942 validation: Pass in chainstate to ::CheckSequenceLocks (Carl Dong) 577b774 validation: Remove old CheckFinalTx w/o chain tip param (Carl Dong) 7031cf8 scripted-diff: Invoke ::CheckFinalTx with chain tip (Carl Dong) d015eaa validation: Pass in chain tip to ::CheckFinalTx (Carl Dong) 252b489 validation: Pass in coins tip to CheckInputsFromMempoolAndCache (Carl Dong) 73a6d2b validation: Pass in chainstate to IsCurrentForFeeEstimation (Carl Dong) d1f932b validation: Pass in coins cache to ::LimitMempoolSize (Carl Dong) Pull request description: Overall PR: #20158 (tree-wide: De-globalize ChainstateManager) Note to reviewers: 1. This bundle may _apparently_ introduce usage of `g_chainman` or `::Chain(state|)Active()` globals, but these are resolved later on in the overall PR. [Commits of overall PR](https://github.com/bitcoin/bitcoin/pull/20158/commits) 2. There may be seemingly obvious local references to `ChainstateManager` or other validation objects which are not being used in callers of the current function in question, this is done intentionally to **_keep each commit centered around one function/method_** to ease review and to make the overall change systematic. We don't assume anything about our callers. Rest assured that once we are considering that particular caller in later commits, we will use the obvious local references. [Commits of overall PR](https://github.com/bitcoin/bitcoin/pull/20158/commits) 3. When changing a function/method that has many callers (e.g. `LookupBlockIndex` with 55 callers), it is sometimes easier (and less error-prone) to use a scripted-diff. When doing so, there will be 3 commits in sequence so that every commit compiles like so: 1. Add `new_function`, make `old_function` a wrapper of `new_function`, divert all calls to `old_function` to `new_function` **in the local module only** 2. Scripted-diff to divert all calls to `old_function` to `new_function` **in the rest of the codebase** 3. Remove `old_function` ACKs for top commit: glozow: reACK bitcoin/bitcoin@e8ae1db via `git range-diff 15f0042...e8ae1db`, only change is fixing ATMP call from conflict MarcoFalke: ACK e8ae1db 📣 Tree-SHA512: 6af50f04940a69c5c3d3796a24f32f963fa02503cdc1155cc11fff832a99172b407cd163a19793080a5af98580f051b48195b62ec4a797ba2763b4883174153d
2 parents c46fe4d + e8ae1db commit 828bb77

14 files changed

+114
-74
lines changed

src/bench/block_assemble.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ static void AssembleBlock(benchmark::Bench& bench)
4848
LOCK(::cs_main); // Required for ::AcceptToMemoryPool.
4949

5050
for (const auto& txr : txs) {
51-
const MempoolAcceptResult res = ::AcceptToMemoryPool(*test_setup.m_node.mempool, txr, false /* bypass_limits */);
51+
const MempoolAcceptResult res = ::AcceptToMemoryPool(::ChainstateActive(), *test_setup.m_node.mempool, txr, false /* bypass_limits */);
5252
assert(res.m_result_type == MempoolAcceptResult::ResultType::VALID);
5353
}
5454
}

src/net_processing.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2236,7 +2236,7 @@ void PeerManagerImpl::ProcessOrphanTx(std::set<uint256>& orphan_work_set)
22362236
if (orphan_it == mapOrphanTransactions.end()) continue;
22372237

22382238
const CTransactionRef porphanTx = orphan_it->second.tx;
2239-
const MempoolAcceptResult result = AcceptToMemoryPool(m_mempool, porphanTx, false /* bypass_limits */);
2239+
const MempoolAcceptResult result = AcceptToMemoryPool(::ChainstateActive(), m_mempool, porphanTx, false /* bypass_limits */);
22402240
const TxValidationState& state = result.m_state;
22412241

22422242
if (result.m_result_type == MempoolAcceptResult::ResultType::VALID) {
@@ -3258,7 +3258,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
32583258
return;
32593259
}
32603260

3261-
const MempoolAcceptResult result = AcceptToMemoryPool(m_mempool, ptx, false /* bypass_limits */);
3261+
const MempoolAcceptResult result = AcceptToMemoryPool(::ChainstateActive(), m_mempool, ptx, false /* bypass_limits */);
32623262
const TxValidationState& state = result.m_state;
32633263

32643264
if (result.m_result_type == MempoolAcceptResult::ResultType::VALID) {

src/node/interfaces.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ class ChainImpl : public Chain
441441
bool checkFinalTx(const CTransaction& tx) override
442442
{
443443
LOCK(cs_main);
444-
return CheckFinalTx(tx);
444+
return CheckFinalTx(::ChainActive().Tip(), tx);
445445
}
446446
Optional<int> findLocatorFork(const CBlockLocator& locator) override
447447
{

src/node/transaction.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ TransactionError BroadcastTransaction(NodeContext& node, const CTransactionRef t
5353
if (max_tx_fee > 0) {
5454
// First, call ATMP with test_accept and check the fee. If ATMP
5555
// fails here, return error immediately.
56-
const MempoolAcceptResult result = AcceptToMemoryPool(*node.mempool, tx, false /* bypass_limits */,
56+
const MempoolAcceptResult result = AcceptToMemoryPool(::ChainstateActive(), *node.mempool, tx, false /* bypass_limits */,
5757
true /* test_accept */);
5858
if (result.m_result_type != MempoolAcceptResult::ResultType::VALID) {
5959
return HandleATMPError(result.m_state, err_string);
@@ -62,7 +62,7 @@ TransactionError BroadcastTransaction(NodeContext& node, const CTransactionRef t
6262
}
6363
}
6464
// Try to submit the transaction to the mempool.
65-
const MempoolAcceptResult result = AcceptToMemoryPool(*node.mempool, tx, false /* bypass_limits */,
65+
const MempoolAcceptResult result = AcceptToMemoryPool(::ChainstateActive(), *node.mempool, tx, false /* bypass_limits */,
6666
false /* test_accept */);
6767
if (result.m_result_type != MempoolAcceptResult::ResultType::VALID) {
6868
return HandleATMPError(result.m_state, err_string);

src/rpc/rawtransaction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,7 @@ static RPCHelpMan testmempoolaccept()
946946
result_0.pushKV("txid", tx->GetHash().GetHex());
947947
result_0.pushKV("wtxid", tx->GetWitnessHash().GetHex());
948948

949-
const MempoolAcceptResult accept_result = WITH_LOCK(cs_main, return AcceptToMemoryPool(mempool, std::move(tx),
949+
const MempoolAcceptResult accept_result = WITH_LOCK(cs_main, return AcceptToMemoryPool(::ChainstateActive(), mempool, std::move(tx),
950950
false /* bypass_limits */, /* test_accept */ true));
951951

952952
// Only return the fee and vsize if the transaction would pass ATMP.

src/test/miner_tests.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ struct MinerTestingSetup : public TestingSetup {
2828
void TestPackageSelection(const CChainParams& chainparams, const CScript& scriptPubKey, const std::vector<CTransactionRef>& txFirst) EXCLUSIVE_LOCKS_REQUIRED(::cs_main, m_node.mempool->cs);
2929
bool TestSequenceLocks(const CTransaction& tx, int flags) EXCLUSIVE_LOCKS_REQUIRED(::cs_main, m_node.mempool->cs)
3030
{
31-
return CheckSequenceLocks(*m_node.mempool, tx, flags);
31+
return CheckSequenceLocks(::ChainstateActive(), *m_node.mempool, tx, flags);
3232
}
3333
BlockAssembler AssemblerForTest(const CChainParams& params);
3434
};
@@ -437,7 +437,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
437437
tx.nLockTime = 0;
438438
hash = tx.GetHash();
439439
m_node.mempool->addUnchecked(entry.Fee(HIGHFEE).Time(GetTime()).SpendsCoinbase(true).FromTx(tx));
440-
BOOST_CHECK(CheckFinalTx(CTransaction(tx), flags)); // Locktime passes
440+
BOOST_CHECK(CheckFinalTx(::ChainActive().Tip(), CTransaction(tx), flags)); // Locktime passes
441441
BOOST_CHECK(!TestSequenceLocks(CTransaction(tx), flags)); // Sequence locks fail
442442
BOOST_CHECK(SequenceLocks(CTransaction(tx), flags, prevheights, CreateBlockIndex(::ChainActive().Tip()->nHeight + 2))); // Sequence locks pass on 2nd block
443443

@@ -447,7 +447,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
447447
prevheights[0] = baseheight + 2;
448448
hash = tx.GetHash();
449449
m_node.mempool->addUnchecked(entry.Time(GetTime()).FromTx(tx));
450-
BOOST_CHECK(CheckFinalTx(CTransaction(tx), flags)); // Locktime passes
450+
BOOST_CHECK(CheckFinalTx(::ChainActive().Tip(), CTransaction(tx), flags)); // Locktime passes
451451
BOOST_CHECK(!TestSequenceLocks(CTransaction(tx), flags)); // Sequence locks fail
452452

453453
for (int i = 0; i < CBlockIndex::nMedianTimeSpan; i++)
@@ -463,7 +463,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
463463
tx.nLockTime = ::ChainActive().Tip()->nHeight + 1;
464464
hash = tx.GetHash();
465465
m_node.mempool->addUnchecked(entry.Time(GetTime()).FromTx(tx));
466-
BOOST_CHECK(!CheckFinalTx(CTransaction(tx), flags)); // Locktime fails
466+
BOOST_CHECK(!CheckFinalTx(::ChainActive().Tip(), CTransaction(tx), flags)); // Locktime fails
467467
BOOST_CHECK(TestSequenceLocks(CTransaction(tx), flags)); // Sequence locks pass
468468
BOOST_CHECK(IsFinalTx(CTransaction(tx), ::ChainActive().Tip()->nHeight + 2, ::ChainActive().Tip()->GetMedianTimePast())); // Locktime passes on 2nd block
469469

@@ -474,7 +474,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
474474
prevheights[0] = baseheight + 4;
475475
hash = tx.GetHash();
476476
m_node.mempool->addUnchecked(entry.Time(GetTime()).FromTx(tx));
477-
BOOST_CHECK(!CheckFinalTx(CTransaction(tx), flags)); // Locktime fails
477+
BOOST_CHECK(!CheckFinalTx(::ChainActive().Tip(), CTransaction(tx), flags)); // Locktime fails
478478
BOOST_CHECK(TestSequenceLocks(CTransaction(tx), flags)); // Sequence locks pass
479479
BOOST_CHECK(IsFinalTx(CTransaction(tx), ::ChainActive().Tip()->nHeight + 2, ::ChainActive().Tip()->GetMedianTimePast() + 1)); // Locktime passes 1 second later
480480

@@ -483,7 +483,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
483483
prevheights[0] = ::ChainActive().Tip()->nHeight + 1;
484484
tx.nLockTime = 0;
485485
tx.vin[0].nSequence = 0;
486-
BOOST_CHECK(CheckFinalTx(CTransaction(tx), flags)); // Locktime passes
486+
BOOST_CHECK(CheckFinalTx(::ChainActive().Tip(), CTransaction(tx), flags)); // Locktime passes
487487
BOOST_CHECK(TestSequenceLocks(CTransaction(tx), flags)); // Sequence locks pass
488488
tx.vin[0].nSequence = 1;
489489
BOOST_CHECK(!TestSequenceLocks(CTransaction(tx), flags)); // Sequence locks fail

src/test/txvalidation_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ BOOST_FIXTURE_TEST_CASE(tx_mempool_reject_coinbase, TestChain100Setup)
3333
LOCK(cs_main);
3434

3535
unsigned int initialPoolSize = m_node.mempool->size();
36-
const MempoolAcceptResult result = AcceptToMemoryPool(*m_node.mempool, MakeTransactionRef(coinbaseTx),
36+
const MempoolAcceptResult result = AcceptToMemoryPool(::ChainstateActive(), *m_node.mempool, MakeTransactionRef(coinbaseTx),
3737
true /* bypass_limits */);
3838

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

src/test/txvalidationcache_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ BOOST_FIXTURE_TEST_CASE(tx_mempool_block_doublespend, TestChain100Setup)
2828
const auto ToMemPool = [this](const CMutableTransaction& tx) {
2929
LOCK(cs_main);
3030

31-
const MempoolAcceptResult result = AcceptToMemoryPool(*m_node.mempool, MakeTransactionRef(tx),
31+
const MempoolAcceptResult result = AcceptToMemoryPool(::ChainstateActive(), *m_node.mempool, MakeTransactionRef(tx),
3232
true /* bypass_limits */);
3333
return result.m_result_type == MempoolAcceptResult::ResultType::VALID;
3434
};

src/test/util/setup_common.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ CMutableTransaction TestChain100Setup::CreateValidMempoolTransaction(CTransactio
303303
// Add transaction to the mempool
304304
{
305305
LOCK(cs_main);
306-
const MempoolAcceptResult result = AcceptToMemoryPool(*m_node.mempool.get(), MakeTransactionRef(mempool_txn), /* bypass_limits */ false);
306+
const MempoolAcceptResult result = AcceptToMemoryPool(::ChainstateActive(), *m_node.mempool.get(), MakeTransactionRef(mempool_txn), /* bypass_limits */ false);
307307
assert(result.m_result_type == MempoolAcceptResult::ResultType::VALID);
308308
}
309309

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.mempool, tx, false /* bypass_limits */);
276+
const MempoolAcceptResult result = AcceptToMemoryPool(::ChainstateActive(), *m_node.mempool, tx, false /* bypass_limits */);
277277
BOOST_REQUIRE(result.m_result_type == MempoolAcceptResult::ResultType::VALID);
278278
}
279279
}

0 commit comments

Comments
 (0)