Skip to content

Commit

Permalink
validation: Pass in chainstate to UpdateMempoolForReorg
Browse files Browse the repository at this point in the history
Summary:
This is a backport of [[bitcoin/bitcoin#20750 | core#20750]] [16/17]
bitcoin/bitcoin@0a9a24d

Depends on D11202

Test Plan: `ninja all check-all`

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D11203
  • Loading branch information
dongcarl authored and PiRK committed Mar 17, 2022
1 parent 4e68502 commit 1e0222e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
16 changes: 9 additions & 7 deletions src/txmempool.cpp
Expand Up @@ -1400,11 +1400,13 @@ void DisconnectedBlockTransactions::importMempool(CTxMemPool &pool) {
}
}

void DisconnectedBlockTransactions::updateMempoolForReorg(const Config &config,
bool fAddToMempool,
CTxMemPool &pool) {
void DisconnectedBlockTransactions::updateMempoolForReorg(
const Config &config, CChainState &active_chainstate, bool fAddToMempool,
CTxMemPool &pool) {
AssertLockHeld(cs_main);
AssertLockHeld(pool.cs);
assert(std::addressof(::ChainstateActive()) ==
std::addressof(active_chainstate));
std::vector<TxId> txidsUpdate;

// disconnectpool's insertion_order index sorts the entries from oldest to
Expand All @@ -1418,8 +1420,8 @@ void DisconnectedBlockTransactions::updateMempoolForReorg(const Config &config,
// ignore validation errors in resurrected transactions
TxValidationState stateDummy;
if (!fAddToMempool || tx->IsCoinBase() ||
!AcceptToMemoryPool(::ChainstateActive(), config, pool, stateDummy,
tx, true /* bypass_limits */)) {
!AcceptToMemoryPool(active_chainstate, config, pool, stateDummy, tx,
true /* bypass_limits */)) {
// If the transaction doesn't make it in to the mempool, remove any
// transactions that depend on it (which would now be orphans).
pool.removeRecursive(*tx, MemPoolRemovalReason::REORG);
Expand All @@ -1438,11 +1440,11 @@ void DisconnectedBlockTransactions::updateMempoolForReorg(const Config &config,
pool.UpdateTransactionsFromBlock(txidsUpdate);

// We also need to remove any now-immature transactions
pool.removeForReorg(config, ::ChainstateActive(),
pool.removeForReorg(config, active_chainstate,
STANDARD_LOCKTIME_VERIFY_FLAGS);

// Re-limit mempool size, in case we added any transactions
pool.LimitSize(::ChainstateActive().CoinsTip(),
pool.LimitSize(active_chainstate.CoinsTip(),
gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) *
1000000,
std::chrono::hours{
Expand Down
5 changes: 3 additions & 2 deletions src/txmempool.h
Expand Up @@ -1079,8 +1079,9 @@ class DisconnectedBlockTransactions {
* Passing fAddToMempool=false will skip trying to add the transactions
* back, and instead just erase from the mempool as needed.
*/
void updateMempoolForReorg(const Config &config, bool fAddToMempool,
CTxMemPool &pool)
void updateMempoolForReorg(const Config &config,
CChainState &active_chainstate,
bool fAddToMempool, CTxMemPool &pool)
EXCLUSIVE_LOCKS_REQUIRED(cs_main, pool.cs);
};

Expand Down
13 changes: 8 additions & 5 deletions src/validation.cpp
Expand Up @@ -2890,7 +2890,8 @@ bool CChainState::ActivateBestChainStep(
if (!DisconnectTip(config.GetChainParams(), state, &disconnectpool)) {
// This is likely a fatal error, but keep the mempool consistent,
// just in case. Only remove from the mempool in this case.
disconnectpool.updateMempoolForReorg(config, false, m_mempool);
disconnectpool.updateMempoolForReorg(config, ::ChainstateActive(),
false, m_mempool);

// If we're unable to disconnect a block during normal operation,
// then that is a failure of our local system -- we should abort
Expand Down Expand Up @@ -2943,7 +2944,8 @@ bool CChainState::ActivateBestChainStep(
// A system error occurred (disk space, database error, ...).
// Make the mempool consistent with the current tip, just in
// case any observers try to use it before shutdown.
disconnectpool.updateMempoolForReorg(config, false, m_mempool);
disconnectpool.updateMempoolForReorg(
config, ::ChainstateActive(), false, m_mempool);
return false;
} else {
PruneBlockIndexCandidates();
Expand All @@ -2965,7 +2967,8 @@ bool CChainState::ActivateBestChainStep(
// effect.
LogPrint(BCLog::MEMPOOL, "Updating mempool due to reorganization or "
"rules upgrade/downgrade\n");
disconnectpool.updateMempoolForReorg(config, true, m_mempool);
disconnectpool.updateMempoolForReorg(config, ::ChainstateActive(), true,
m_mempool);
}

m_mempool.check(&CoinsTip());
Expand Down Expand Up @@ -3282,8 +3285,8 @@ bool CChainState::UnwindBlock(const Config &config, BlockValidationState &state,
// and we're not doing a very deep invalidation (in which case
// keeping the mempool up to date is probably futile anyway).
disconnectpool.updateMempoolForReorg(
config, /* fAddToMempool = */ (++disconnected <= 10) && ret,
m_mempool);
config, ::ChainstateActive(),
/* fAddToMempool = */ (++disconnected <= 10) && ret, m_mempool);

if (!ret) {
return false;
Expand Down

0 comments on commit 1e0222e

Please sign in to comment.