Skip to content

Commit 7142018

Browse files
committed
validation: Pass in chainstate to CTxMemPool::removeForReorg
Several other parameters are now redundant since they can be safely obtained from the chainstate given that ::cs_main is locked. These are now removed.
1 parent 71734c6 commit 7142018

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

src/txmempool.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -503,16 +503,17 @@ void CTxMemPool::removeRecursive(const CTransaction &origTx, MemPoolRemovalReaso
503503
RemoveStaged(setAllRemoves, false, reason);
504504
}
505505

506-
void CTxMemPool::removeForReorg(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight, int flags)
506+
void CTxMemPool::removeForReorg(CChainState& active_chainstate, int flags)
507507
{
508508
// Remove transactions spending a coinbase which are now immature and no-longer-final transactions
509509
AssertLockHeld(cs);
510510
setEntries txToRemove;
511511
for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) {
512512
const CTransaction& tx = it->GetTx();
513513
LockPoints lp = it->GetLockPoints();
514-
bool validLP = TestLockPointValidity(::ChainActive(), &lp);
515-
if (!CheckFinalTx(::ChainActive().Tip(), tx, flags) || !CheckSequenceLocks(::ChainstateActive(), *this, tx, flags, &lp, validLP)) {
514+
assert(std::addressof(::ChainstateActive()) == std::addressof(active_chainstate));
515+
bool validLP = TestLockPointValidity(active_chainstate.m_chain, &lp);
516+
if (!CheckFinalTx(active_chainstate.m_chain.Tip(), tx, flags) || !CheckSequenceLocks(active_chainstate, *this, tx, flags, &lp, validLP)) {
516517
// Note if CheckSequenceLocks fails the LockPoints may still be invalid
517518
// So it's critical that we remove the tx and not depend on the LockPoints.
518519
txToRemove.insert(it);
@@ -521,8 +522,9 @@ void CTxMemPool::removeForReorg(const CCoinsViewCache *pcoins, unsigned int nMem
521522
indexed_transaction_set::const_iterator it2 = mapTx.find(txin.prevout.hash);
522523
if (it2 != mapTx.end())
523524
continue;
524-
const Coin &coin = pcoins->AccessCoin(txin.prevout);
525+
const Coin &coin = active_chainstate.CoinsTip().AccessCoin(txin.prevout);
525526
if (m_check_ratio != 0) assert(!coin.IsSpent());
527+
unsigned int nMemPoolHeight = active_chainstate.m_chain.Tip()->nHeight + 1;
526528
if (coin.IsSpent() || (coin.IsCoinBase() && ((signed long)nMemPoolHeight) - coin.nHeight < COINBASE_MATURITY)) {
527529
txToRemove.insert(it);
528530
break;

src/txmempool.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <boost/multi_index/sequenced_index.hpp>
3030

3131
class CBlockIndex;
32+
class CChainState;
3233
extern RecursiveMutex cs_main;
3334

3435
/** Fake height value used in Coin to signify they are only in the memory pool (since 0.8) */
@@ -616,7 +617,7 @@ class CTxMemPool
616617
void addUnchecked(const CTxMemPoolEntry& entry, setEntries& setAncestors, bool validFeeEstimate = true) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main);
617618

618619
void removeRecursive(const CTransaction& tx, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs);
619-
void removeForReorg(const CCoinsViewCache* pcoins, unsigned int nMemPoolHeight, int flags) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main);
620+
void removeForReorg(CChainState& active_chainstate, int flags) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main);
620621
void removeConflicts(const CTransaction& tx) EXCLUSIVE_LOCKS_REQUIRED(cs);
621622
void removeForBlock(const std::vector<CTransactionRef>& vtx, unsigned int nBlockHeight) EXCLUSIVE_LOCKS_REQUIRED(cs);
622623

src/validation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ static void UpdateMempoolForReorg(CTxMemPool& mempool, DisconnectedBlockTransact
406406
mempool.UpdateTransactionsFromBlock(vHashUpdate);
407407

408408
// We also need to remove any now-immature transactions
409-
mempool.removeForReorg(&::ChainstateActive().CoinsTip(), ::ChainActive().Tip()->nHeight + 1, STANDARD_LOCKTIME_VERIFY_FLAGS);
409+
mempool.removeForReorg(::ChainstateActive(), STANDARD_LOCKTIME_VERIFY_FLAGS);
410410
// Re-limit mempool size, in case we added any transactions
411411
LimitMempoolSize(mempool, ::ChainstateActive().CoinsTip(), gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000, std::chrono::hours{gArgs.GetArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY)});
412412
}

0 commit comments

Comments
 (0)