Skip to content

Commit

Permalink
validation: Pass in chainstate to CTxMemPool::removeForReorg
Browse files Browse the repository at this point in the history
Summary:
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.

This is a backport of [[bitcoin/bitcoin#20750 | core#20750]] [15/17]
bitcoin/bitcoin@7142018

Depends on D11201

Test Plan: `ninja all check-all`

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D11202
  • Loading branch information
dongcarl authored and PiRK committed Mar 17, 2022
1 parent a53d338 commit 4e68502
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
22 changes: 12 additions & 10 deletions src/txmempool.cpp
Expand Up @@ -571,8 +571,7 @@ void CTxMemPool::removeRecursive(const CTransaction &origTx,
}

void CTxMemPool::removeForReorg(const Config &config,
const CCoinsViewCache *pcoins,
unsigned int nMemPoolHeight, int flags) {
CChainState &active_chainstate, int flags) {
// Remove transactions spending a coinbase which are now immature and
// no-longer-final transactions.
AssertLockHeld(cs);
Expand All @@ -581,13 +580,15 @@ void CTxMemPool::removeForReorg(const Config &config,
it != mapTx.end(); it++) {
const CTransaction &tx = it->GetTx();
LockPoints lp = it->GetLockPoints();
bool validLP = TestLockPointValidity(::ChainActive(), &lp);
assert(std::addressof(::ChainstateActive()) ==
std::addressof(active_chainstate));
bool validLP = TestLockPointValidity(active_chainstate.m_chain, &lp);

TxValidationState state;
if (!ContextualCheckTransactionForCurrentBlock(
::ChainActive().Tip(), config.GetChainParams().GetConsensus(),
tx, state, flags) ||
!CheckSequenceLocks(::ChainstateActive(), *this, tx, flags, &lp,
active_chainstate.m_chain.Tip(),
config.GetChainParams().GetConsensus(), tx, state, flags) ||
!CheckSequenceLocks(active_chainstate, *this, 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
Expand All @@ -601,11 +602,13 @@ void CTxMemPool::removeForReorg(const Config &config,
continue;
}

const Coin &coin = pcoins->AccessCoin(txin.prevout);
const Coin &coin =
active_chainstate.CoinsTip().AccessCoin(txin.prevout);
if (m_check_ratio != 0) {
assert(!coin.IsSpent());
}

unsigned int nMemPoolHeight =
active_chainstate.m_chain.Tip()->nHeight + 1;
if (coin.IsSpent() ||
(coin.IsCoinBase() &&
int64_t(nMemPoolHeight) - coin.GetHeight() <
Expand Down Expand Up @@ -1435,8 +1438,7 @@ void DisconnectedBlockTransactions::updateMempoolForReorg(const Config &config,
pool.UpdateTransactionsFromBlock(txidsUpdate);

// We also need to remove any now-immature transactions
pool.removeForReorg(config, &::ChainstateActive().CoinsTip(),
::ChainActive().Tip()->nHeight + 1,
pool.removeForReorg(config, ::ChainstateActive(),
STANDARD_LOCKTIME_VERIFY_FLAGS);

// Re-limit mempool size, in case we added any transactions
Expand Down
6 changes: 3 additions & 3 deletions src/txmempool.h
Expand Up @@ -28,6 +28,7 @@
#include <vector>

class CBlockIndex;
class CChainState;
class Config;

extern RecursiveMutex cs_main;
Expand Down Expand Up @@ -642,9 +643,8 @@ class CTxMemPool {

void removeRecursive(const CTransaction &tx, MemPoolRemovalReason reason)
EXCLUSIVE_LOCKS_REQUIRED(cs);
void removeForReorg(const Config &config, const CCoinsViewCache *pcoins,
unsigned int nMemPoolHeight, int flags)
EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main);
void removeForReorg(const Config &config, CChainState &active_chainstate,
int flags) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main);
void removeConflicts(const CTransaction &tx) EXCLUSIVE_LOCKS_REQUIRED(cs);
void removeForBlock(const std::vector<CTransactionRef> &vtx,
unsigned int nBlockHeight) EXCLUSIVE_LOCKS_REQUIRED(cs);
Expand Down

0 comments on commit 4e68502

Please sign in to comment.