I’m not sure I can easily convince myself that moving this check into ATMP
is completely safe. However, I do see how this is ugly.
Do you think the following patch would be an acceptable solution?
0diff --git a/src/net_processing.cpp b/src/net_processing.cpp
1index 614a3e0791..022f2e0574 100644
2--- a/src/net_processing.cpp
3+++ b/src/net_processing.cpp
4@@ -2301,10 +2301,7 @@ void PeerManagerImpl::ProcessOrphanTx(std::set<uint256>& orphan_work_set)
5 break;
6 }
7 }
8- CChainState& active_chainstate = m_chainman.ActiveChainstate();
9- CCoinsViewCache& active_coins_tip = active_chainstate.CoinsTip();
10- assert(std::addressof(::ChainstateActive().CoinsTip()) == std::addressof(active_coins_tip));
11- m_mempool.check(&active_coins_tip, active_chainstate.m_blockman.GetSpendHeight(active_coins_tip));
12+ m_mempool.check(m_chainman.ActiveChainstate());
13 }
14
15 /**
16@@ -3265,10 +3262,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
17 const TxValidationState& state = result.m_state;
18
19 if (result.m_result_type == MempoolAcceptResult::ResultType::VALID) {
20- CChainState& active_chainstate = m_chainman.ActiveChainstate();
21- CCoinsViewCache& active_coins_tip = active_chainstate.CoinsTip();
22- assert(std::addressof(::ChainstateActive().CoinsTip()) == std::addressof(active_coins_tip));
23- m_mempool.check(&active_coins_tip, active_chainstate.m_blockman.GetSpendHeight(active_coins_tip));
24+ m_mempool.check(m_chainman.ActiveChainstate());
25
26 // As this version of the transaction was acceptable, we can forget about any
27 // requests for it.
28diff --git a/src/txmempool.cpp b/src/txmempool.cpp
29index 4df7017c47..05902a55c3 100644
30--- a/src/txmempool.cpp
31+++ b/src/txmempool.cpp
32@@ -619,7 +619,7 @@ static void CheckInputsAndUpdateCoins(const CTransaction& tx, CCoinsViewCache& m
33 UpdateCoins(tx, mempoolDuplicate, std::numeric_limits<int>::max());
34 }
35
36-void CTxMemPool::check(const CCoinsViewCache *pcoins, const int64_t spendheight) const
37+void CTxMemPool::check(CChainState& active_chainstate) const
38 {
39 if (m_check_ratio == 0) return;
40
41@@ -633,7 +633,10 @@ void CTxMemPool::check(const CCoinsViewCache *pcoins, const int64_t spendheight)
42 CAmount check_total_fee{0};
43 uint64_t innerUsage = 0;
44
45- CCoinsViewCache mempoolDuplicate(const_cast<CCoinsViewCache*>(pcoins));
46+ CCoinsViewCache& active_coins_tip = active_chainstate.CoinsTip();
47+ assert(std::addressof(::ChainstateActive().CoinsTip()) == std::addressof(active_coins_tip)); // TODO: REVIEW-ONLY, REMOVE IN FUTURE COMMIT
48+ CCoinsViewCache mempoolDuplicate(const_cast<CCoinsViewCache*>(&active_coins_tip));
49+ const int64_t spendheight = active_chainstate.m_chain.Height() + 1;
50 assert(g_chainman.m_blockman.GetSpendHeight(mempoolDuplicate) == spendheight); // TODO: REVIEW-ONLY, REMOVE IN FUTURE COMMIT
51
52 std::list<const CTxMemPoolEntry*> waitingOnDependants;
53@@ -655,7 +658,7 @@ void CTxMemPool::check(const CCoinsViewCache *pcoins, const int64_t spendheight)
54 fDependsWait = true;
55 setParentCheck.insert(*it2);
56 } else {
57- assert(pcoins->HaveCoin(txin.prevout));
58+ assert(active_coins_tip.HaveCoin(txin.prevout));
59 }
60 // Check whether its inputs are marked in mapNextTx.
61 auto it3 = mapNextTx.find(txin.prevout);
62diff --git a/src/txmempool.h b/src/txmempool.h
63index dd82d61fc6..001d856e43 100644
64--- a/src/txmempool.h
65+++ b/src/txmempool.h
66@@ -604,7 +604,7 @@ public:
67 * all inputs are in the mapNextTx array). If sanity-checking is turned off,
68 * check does nothing.
69 */
70- void check(const CCoinsViewCache *pcoins, const int64_t spendheight) const EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
71+ void check(CChainState& active_chainstate) const EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
72
73 // addUnchecked must updated state for all ancestors of a given transaction,
74 // to track size/count of descendant transactions. First version of
75diff --git a/src/validation.cpp b/src/validation.cpp
76index 325edc5b6a..ac79dce52e 100644
77--- a/src/validation.cpp
78+++ b/src/validation.cpp
79@@ -2801,8 +2801,7 @@ bool CChainState::ActivateBestChainStep(BlockValidationState& state, const CChai
80 UpdateMempoolForReorg(*this, m_mempool, disconnectpool, true);
81 }
82
83- CCoinsViewCache& active_coins_tip = CoinsTip();
84- m_mempool.check(&active_coins_tip, m_blockman.GetSpendHeight(active_coins_tip));
85+ m_mempool.check(*this);
86
87 CheckForkWarningConditions();
88