Skip to content

Commit 78bdd7b

Browse files
jamesobjnewbery
authored andcommitted
refactor: move UpdateMempoolForReorg into CChainState
Allows fewer arguments and simplification of call sites. Co-authored-by: John Newbery <[email protected]>
1 parent 44bedb8 commit 78bdd7b

File tree

2 files changed

+42
-31
lines changed

2 files changed

+42
-31
lines changed

src/validation.cpp

+25-31
Original file line numberDiff line numberDiff line change
@@ -329,24 +329,14 @@ static bool IsCurrentForFeeEstimation(CChainState& active_chainstate) EXCLUSIVE_
329329
return true;
330330
}
331331

332-
/**
333-
* Make mempool consistent after a reorg, by re-adding or recursively erasing
334-
* disconnected block transactions from the mempool, and also removing any
335-
* other transactions from the mempool that are no longer valid given the new
336-
* tip/height.
337-
*
338-
* Note: we assume that disconnectpool only contains transactions that are NOT
339-
* confirmed in the current chain nor already in the mempool (otherwise,
340-
* in-mempool descendants of such transactions would be removed).
341-
*
342-
* Passing fAddToMempool=false will skip trying to add the transactions back,
343-
* and instead just erase from the mempool as needed.
344-
*/
345-
346-
static void UpdateMempoolForReorg(CChainState& active_chainstate, CTxMemPool& mempool, DisconnectedBlockTransactions& disconnectpool, bool fAddToMempool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, mempool.cs)
332+
void CChainState::MaybeUpdateMempoolForReorg(
333+
DisconnectedBlockTransactions& disconnectpool,
334+
bool fAddToMempool)
347335
{
336+
if (!m_mempool) return;
337+
348338
AssertLockHeld(cs_main);
349-
AssertLockHeld(mempool.cs);
339+
AssertLockHeld(m_mempool->cs);
350340
std::vector<uint256> vHashUpdate;
351341
// disconnectpool's insertion_order index sorts the entries from
352342
// oldest to newest, but the oldest entry will be the last tx from the
@@ -358,11 +348,13 @@ static void UpdateMempoolForReorg(CChainState& active_chainstate, CTxMemPool& me
358348
while (it != disconnectpool.queuedTx.get<insertion_order>().rend()) {
359349
// ignore validation errors in resurrected transactions
360350
if (!fAddToMempool || (*it)->IsCoinBase() ||
361-
AcceptToMemoryPool(active_chainstate, mempool, *it, true /* bypass_limits */).m_result_type != MempoolAcceptResult::ResultType::VALID) {
351+
AcceptToMemoryPool(
352+
*this, *m_mempool, *it, true /* bypass_limits */).m_result_type !=
353+
MempoolAcceptResult::ResultType::VALID) {
362354
// If the transaction doesn't make it in to the mempool, remove any
363355
// transactions that depend on it (which would now be orphans).
364-
mempool.removeRecursive(**it, MemPoolRemovalReason::REORG);
365-
} else if (mempool.exists((*it)->GetHash())) {
356+
m_mempool->removeRecursive(**it, MemPoolRemovalReason::REORG);
357+
} else if (m_mempool->exists((*it)->GetHash())) {
366358
vHashUpdate.push_back((*it)->GetHash());
367359
}
368360
++it;
@@ -373,12 +365,16 @@ static void UpdateMempoolForReorg(CChainState& active_chainstate, CTxMemPool& me
373365
// previously-confirmed transactions back to the mempool.
374366
// UpdateTransactionsFromBlock finds descendants of any transactions in
375367
// the disconnectpool that were added back and cleans up the mempool state.
376-
mempool.UpdateTransactionsFromBlock(vHashUpdate);
368+
m_mempool->UpdateTransactionsFromBlock(vHashUpdate);
377369

378370
// We also need to remove any now-immature transactions
379-
mempool.removeForReorg(active_chainstate, STANDARD_LOCKTIME_VERIFY_FLAGS);
371+
m_mempool->removeForReorg(*this, STANDARD_LOCKTIME_VERIFY_FLAGS);
380372
// Re-limit mempool size, in case we added any transactions
381-
LimitMempoolSize(mempool, active_chainstate.CoinsTip(), gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000, std::chrono::hours{gArgs.GetArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY)});
373+
LimitMempoolSize(
374+
*m_mempool,
375+
this->CoinsTip(),
376+
gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000,
377+
std::chrono::hours{gArgs.GetArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY)});
382378
}
383379

384380
/**
@@ -2251,7 +2247,7 @@ static void UpdateTip(CTxMemPool* mempool, const CBlockIndex* pindexNew, const C
22512247
/** Disconnect m_chain's tip.
22522248
* After calling, the mempool will be in an inconsistent state, with
22532249
* transactions from disconnected blocks being added to disconnectpool. You
2254-
* should make the mempool consistent again by calling UpdateMempoolForReorg.
2250+
* should make the mempool consistent again by calling MaybeUpdateMempoolForReorg.
22552251
* with cs_main held.
22562252
*
22572253
* If disconnectpool is nullptr, then no disconnected transactions are added to
@@ -2516,7 +2512,7 @@ bool CChainState::ActivateBestChainStep(BlockValidationState& state, CBlockIndex
25162512
if (!DisconnectTip(state, &disconnectpool)) {
25172513
// This is likely a fatal error, but keep the mempool consistent,
25182514
// just in case. Only remove from the mempool in this case.
2519-
if (m_mempool) UpdateMempoolForReorg(*this, *m_mempool, disconnectpool, false);
2515+
MaybeUpdateMempoolForReorg(disconnectpool, false);
25202516

25212517
// If we're unable to disconnect a block during normal operation,
25222518
// then that is a failure of our local system -- we should abort
@@ -2560,7 +2556,7 @@ bool CChainState::ActivateBestChainStep(BlockValidationState& state, CBlockIndex
25602556
// A system error occurred (disk space, database error, ...).
25612557
// Make the mempool consistent with the current tip, just in case
25622558
// any observers try to use it before shutdown.
2563-
if (m_mempool) UpdateMempoolForReorg(*this, *m_mempool, disconnectpool, false);
2559+
MaybeUpdateMempoolForReorg(disconnectpool, false);
25642560
return false;
25652561
}
25662562
} else {
@@ -2574,10 +2570,10 @@ bool CChainState::ActivateBestChainStep(BlockValidationState& state, CBlockIndex
25742570
}
25752571
}
25762572

2577-
if (fBlocksDisconnected && m_mempool) {
2573+
if (fBlocksDisconnected) {
25782574
// If any blocks were disconnected, disconnectpool may be non empty. Add
25792575
// any disconnected transactions back to the mempool.
2580-
UpdateMempoolForReorg(*this, *m_mempool, disconnectpool, true);
2576+
MaybeUpdateMempoolForReorg(disconnectpool, true);
25812577
}
25822578
if (m_mempool) m_mempool->check(*this);
25832579

@@ -2802,7 +2798,7 @@ bool CChainState::InvalidateBlock(BlockValidationState& state, CBlockIndex* pind
28022798
LimitValidationInterfaceQueue();
28032799

28042800
LOCK(cs_main);
2805-
// Lock for as long as disconnectpool is in scope to make sure UpdateMempoolForReorg is
2801+
// Lock for as long as disconnectpool is in scope to make sure MaybeUpdateMempoolForReorg is
28062802
// called after DisconnectTip without unlocking in between
28072803
LOCK(MempoolMutex());
28082804
if (!m_chain.Contains(pindex)) break;
@@ -2818,9 +2814,7 @@ bool CChainState::InvalidateBlock(BlockValidationState& state, CBlockIndex* pind
28182814
// transactions back to the mempool if disconnecting was successful,
28192815
// and we're not doing a very deep invalidation (in which case
28202816
// keeping the mempool up to date is probably futile anyway).
2821-
if (m_mempool) {
2822-
UpdateMempoolForReorg(*this, *m_mempool, disconnectpool, /* fAddToMempool = */ (++disconnected <= 10) && ret);
2823-
}
2817+
MaybeUpdateMempoolForReorg(disconnectpool, /* fAddToMempool = */ (++disconnected <= 10) && ret);
28242818
if (!ret) return false;
28252819
assert(invalid_walk_tip->pprev == m_chain.Tip());
28262820

src/validation.h

+17
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,23 @@ class CChainState
808808
return m_mempool ? &m_mempool->cs : nullptr;
809809
}
810810

811+
/**
812+
* Make mempool consistent after a reorg, by re-adding or recursively erasing
813+
* disconnected block transactions from the mempool, and also removing any
814+
* other transactions from the mempool that are no longer valid given the new
815+
* tip/height.
816+
*
817+
* Note: we assume that disconnectpool only contains transactions that are NOT
818+
* confirmed in the current chain nor already in the mempool (otherwise,
819+
* in-mempool descendants of such transactions would be removed).
820+
*
821+
* Passing fAddToMempool=false will skip trying to add the transactions back,
822+
* and instead just erase from the mempool as needed.
823+
*/
824+
void MaybeUpdateMempoolForReorg(
825+
DisconnectedBlockTransactions& disconnectpool,
826+
bool fAddToMempool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_mempool->cs);
827+
811828
friend ChainstateManager;
812829
};
813830

0 commit comments

Comments
 (0)