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?
diff --git a/src/net_processing.cpp b/src/net_processing.cpp
index 614a3e0791..022f2e0574 100644
--- a/src/net_processing.cpp
+++ b/src/net_processing.cpp
@@ -2301,10 +2301,7 @@ void PeerManagerImpl::ProcessOrphanTx(std::set<uint256>& orphan_work_set)
break;
}
}
- CChainState& active_chainstate = m_chainman.ActiveChainstate();
- CCoinsViewCache& active_coins_tip = active_chainstate.CoinsTip();
- assert(std::addressof(::ChainstateActive().CoinsTip()) == std::addressof(active_coins_tip));
- m_mempool.check(&active_coins_tip, active_chainstate.m_blockman.GetSpendHeight(active_coins_tip));
+ m_mempool.check(m_chainman.ActiveChainstate());
}
/**
@@ -3265,10 +3262,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
const TxValidationState& state = result.m_state;
if (result.m_result_type == MempoolAcceptResult::ResultType::VALID) {
- CChainState& active_chainstate = m_chainman.ActiveChainstate();
- CCoinsViewCache& active_coins_tip = active_chainstate.CoinsTip();
- assert(std::addressof(::ChainstateActive().CoinsTip()) == std::addressof(active_coins_tip));
- m_mempool.check(&active_coins_tip, active_chainstate.m_blockman.GetSpendHeight(active_coins_tip));
+ m_mempool.check(m_chainman.ActiveChainstate());
// As this version of the transaction was acceptable, we can forget about any
// requests for it.
diff --git a/src/txmempool.cpp b/src/txmempool.cpp
index 4df7017c47..05902a55c3 100644
--- a/src/txmempool.cpp
+++ b/src/txmempool.cpp
@@ -619,7 +619,7 @@ static void CheckInputsAndUpdateCoins(const CTransaction& tx, CCoinsViewCache& m
UpdateCoins(tx, mempoolDuplicate, std::numeric_limits<int>::max());
}
-void CTxMemPool::check(const CCoinsViewCache *pcoins, const int64_t spendheight) const
+void CTxMemPool::check(CChainState& active_chainstate) const
{
if (m_check_ratio == 0) return;
@@ -633,7 +633,10 @@ void CTxMemPool::check(const CCoinsViewCache *pcoins, const int64_t spendheight)
CAmount check_total_fee{0};
uint64_t innerUsage = 0;
- CCoinsViewCache mempoolDuplicate(const_cast<CCoinsViewCache*>(pcoins));
+ CCoinsViewCache& active_coins_tip = active_chainstate.CoinsTip();
+ assert(std::addressof(::ChainstateActive().CoinsTip()) == std::addressof(active_coins_tip)); // TODO: REVIEW-ONLY, REMOVE IN FUTURE COMMIT
+ CCoinsViewCache mempoolDuplicate(const_cast<CCoinsViewCache*>(&active_coins_tip));
+ const int64_t spendheight = active_chainstate.m_chain.Height() + 1;
assert(g_chainman.m_blockman.GetSpendHeight(mempoolDuplicate) == spendheight); // TODO: REVIEW-ONLY, REMOVE IN FUTURE COMMIT
std::list<const CTxMemPoolEntry*> waitingOnDependants;
@@ -655,7 +658,7 @@ void CTxMemPool::check(const CCoinsViewCache *pcoins, const int64_t spendheight)
fDependsWait = true;
setParentCheck.insert(*it2);
} else {
- assert(pcoins->HaveCoin(txin.prevout));
+ assert(active_coins_tip.HaveCoin(txin.prevout));
}
// Check whether its inputs are marked in mapNextTx.
auto it3 = mapNextTx.find(txin.prevout);
diff --git a/src/txmempool.h b/src/txmempool.h
index dd82d61fc6..001d856e43 100644
--- a/src/txmempool.h
+++ b/src/txmempool.h
@@ -604,7 +604,7 @@ public:
* all inputs are in the mapNextTx array). If sanity-checking is turned off,
* check does nothing.
*/
- void check(const CCoinsViewCache *pcoins, const int64_t spendheight) const EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
+ void check(CChainState& active_chainstate) const EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
// addUnchecked must updated state for all ancestors of a given transaction,
// to track size/count of descendant transactions. First version of
diff --git a/src/validation.cpp b/src/validation.cpp
index 325edc5b6a..ac79dce52e 100644
--- a/src/validation.cpp
+++ b/src/validation.cpp
@@ -2801,8 +2801,7 @@ bool CChainState::ActivateBestChainStep(BlockValidationState& state, const CChai
UpdateMempoolForReorg(*this, m_mempool, disconnectpool, true);
}
- CCoinsViewCache& active_coins_tip = CoinsTip();
- m_mempool.check(&active_coins_tip, m_blockman.GetSpendHeight(active_coins_tip));
+ m_mempool.check(*this);
CheckForkWarningConditions();