aboutsummaryrefslogtreecommitdiff
path: root/src/validation.cpp
diff options
context:
space:
mode:
authorglozow <gloriajzhao@gmail.com>2021-09-29 20:52:04 +0100
committerglozow <gloriajzhao@gmail.com>2021-11-30 12:21:56 +0000
commita64078e38563ef3ac5e5ec20c07569441c87eeee (patch)
tree3a4968f341333b819884b28e94f74007fd24c1fe /src/validation.cpp
parent64e4963c635ec3a73a5fa3f32f6ec08e70609f60 (diff)
downloadbitcoin-a64078e38563ef3ac5e5ec20c07569441c87eeee.tar.xz
Break validation <-> txmempool circular dependency
No behavior change. Parameterize removeForReorg using a CChain and callable that encapsulates validation logic. The mempool shouldn't need to know a bunch of details about coinbase maturity and lock finality. Instead, just pass in a callable function that says true/false. Breaks circular dependency by removing txmempool's dependency on validation.
Diffstat (limited to 'src/validation.cpp')
-rw-r--r--src/validation.cpp33
1 files changed, 32 insertions, 1 deletions
diff --git a/src/validation.cpp b/src/validation.cpp
index 322e77afe6..714eab1f77 100644
--- a/src/validation.cpp
+++ b/src/validation.cpp
@@ -350,8 +350,39 @@ void CChainState::MaybeUpdateMempoolForReorg(
// the disconnectpool that were added back and cleans up the mempool state.
m_mempool->UpdateTransactionsFromBlock(vHashUpdate);
+ const auto check_final_and_mature = [this, flags=STANDARD_LOCKTIME_VERIFY_FLAGS](CTxMemPool::txiter it)
+ EXCLUSIVE_LOCKS_REQUIRED(m_mempool->cs, ::cs_main) {
+ bool should_remove = false;
+ AssertLockHeld(m_mempool->cs);
+ AssertLockHeld(::cs_main);
+ const CTransaction& tx = it->GetTx();
+ LockPoints lp = it->GetLockPoints();
+ bool validLP = TestLockPointValidity(m_chain, &lp);
+ CCoinsViewMemPool view_mempool(&CoinsTip(), *m_mempool);
+ if (!CheckFinalTx(m_chain.Tip(), tx, flags)
+ || !CheckSequenceLocks(m_chain.Tip(), view_mempool, tx, flags, &lp, validLP)) {
+ // Note if CheckSequenceLocks fails the LockPoints may still be invalid
+ // So it's critical that we remove the tx and not depend on the LockPoints.
+ should_remove = true;
+ } else if (it->GetSpendsCoinbase()) {
+ for (const CTxIn& txin : tx.vin) {
+ auto it2 = m_mempool->mapTx.find(txin.prevout.hash);
+ if (it2 != m_mempool->mapTx.end())
+ continue;
+ const Coin &coin = CoinsTip().AccessCoin(txin.prevout);
+ assert(!coin.IsSpent());
+ unsigned int nMemPoolHeight = m_chain.Tip()->nHeight + 1;
+ if (coin.IsSpent() || (coin.IsCoinBase() && ((signed long)nMemPoolHeight) - coin.nHeight < COINBASE_MATURITY)) {
+ should_remove = true;
+ break;
+ }
+ }
+ }
+ return should_remove;
+ };
+
// We also need to remove any now-immature transactions
- m_mempool->removeForReorg(*this, STANDARD_LOCKTIME_VERIFY_FLAGS);
+ m_mempool->removeForReorg(m_chain, check_final_and_mature);
// Re-limit mempool size, in case we added any transactions
LimitMempoolSize(
*m_mempool,