@@ -370,9 +370,10 @@ static bool IsCurrentForFeeEstimation() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
370370 * and instead just erase from the mempool as needed.
371371 */
372372
373- static void UpdateMempoolForReorg (DisconnectedBlockTransactions& disconnectpool, bool fAddToMempool ) EXCLUSIVE_LOCKS_REQUIRED(cs_main, :: mempool.cs)
373+ static void UpdateMempoolForReorg (CTxMemPool& mempool, DisconnectedBlockTransactions& disconnectpool, bool fAddToMempool ) EXCLUSIVE_LOCKS_REQUIRED(cs_main, mempool.cs)
374374{
375375 AssertLockHeld (cs_main);
376+ AssertLockHeld (mempool.cs );
376377 std::vector<uint256> vHashUpdate;
377378 // disconnectpool's insertion_order index sorts the entries from
378379 // oldest to newest, but the oldest entry will be the last tx from the
@@ -1254,8 +1255,9 @@ void CoinsViews::InitCache()
12541255 m_cacheview = MakeUnique<CCoinsViewCache>(&m_catcherview);
12551256}
12561257
1257- CChainState::CChainState (BlockManager& blockman, uint256 from_snapshot_blockhash)
1258+ CChainState::CChainState (CTxMemPool& mempool, BlockManager& blockman, uint256 from_snapshot_blockhash)
12581259 : m_blockman(blockman),
1260+ m_mempool(mempool),
12591261 m_from_snapshot_blockhash(from_snapshot_blockhash) {}
12601262
12611263void CChainState::InitCoinsDB (
@@ -2280,7 +2282,7 @@ bool CChainState::FlushStateToDisk(
22802282 {
22812283 bool fFlushForPrune = false ;
22822284 bool fDoFullFlush = false ;
2283- CoinsCacheSizeState cache_state = GetCoinsCacheSizeState (&::mempool );
2285+ CoinsCacheSizeState cache_state = GetCoinsCacheSizeState (&m_mempool );
22842286 LOCK (cs_LastBlockFile);
22852287 if (fPruneMode && (fCheckForPruning || nManualPruneHeight > 0 ) && !fReindex ) {
22862288 if (nManualPruneHeight > 0 ) {
@@ -2426,7 +2428,7 @@ static void AppendWarning(bilingual_str& res, const bilingual_str& warn)
24262428}
24272429
24282430/* * Check warning conditions and do some notifications on new chain tip set. */
2429- void static UpdateTip (const CBlockIndex* pindexNew, const CChainParams& chainParams)
2431+ static void UpdateTip (CTxMemPool& mempool, const CBlockIndex* pindexNew, const CChainParams& chainParams)
24302432 EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
24312433{
24322434 // New best block
@@ -2472,7 +2474,6 @@ void static UpdateTip(const CBlockIndex* pindexNew, const CChainParams& chainPar
24722474 FormatISO8601DateTime (pindexNew->GetBlockTime ()),
24732475 GuessVerificationProgress (chainParams.TxData (), pindexNew), ::ChainstateActive ().CoinsTip ().DynamicMemoryUsage () * (1.0 / (1 <<20 )), ::ChainstateActive ().CoinsTip ().GetCacheSize (),
24742476 !warning_messages.empty () ? strprintf (" warning='%s'" , warning_messages.original ) : " " );
2475-
24762477}
24772478
24782479/* * Disconnect m_chain's tip.
@@ -2485,8 +2486,11 @@ void static UpdateTip(const CBlockIndex* pindexNew, const CChainParams& chainPar
24852486 * disconnectpool (note that the caller is responsible for mempool consistency
24862487 * in any case).
24872488 */
2488- bool CChainState::DisconnectTip (BlockValidationState& state, const CChainParams& chainparams, DisconnectedBlockTransactions * disconnectpool)
2489+ bool CChainState::DisconnectTip (BlockValidationState& state, const CChainParams& chainparams, DisconnectedBlockTransactions* disconnectpool)
24892490{
2491+ AssertLockHeld (cs_main);
2492+ AssertLockHeld (m_mempool.cs );
2493+
24902494 CBlockIndex *pindexDelete = m_chain.Tip ();
24912495 assert (pindexDelete);
24922496 // Read block from disk.
@@ -2517,14 +2521,14 @@ bool CChainState::DisconnectTip(BlockValidationState& state, const CChainParams&
25172521 while (disconnectpool->DynamicMemoryUsage () > MAX_DISCONNECTED_TX_POOL_SIZE * 1000 ) {
25182522 // Drop the earliest entry, and remove its children from the mempool.
25192523 auto it = disconnectpool->queuedTx .get <insertion_order>().begin ();
2520- mempool .removeRecursive (**it, MemPoolRemovalReason::REORG);
2524+ m_mempool .removeRecursive (**it, MemPoolRemovalReason::REORG);
25212525 disconnectpool->removeEntry (it);
25222526 }
25232527 }
25242528
25252529 m_chain.SetTip (pindexDelete->pprev );
25262530
2527- UpdateTip (pindexDelete->pprev , chainparams);
2531+ UpdateTip (m_mempool, pindexDelete->pprev , chainparams);
25282532 // Let wallets know transactions went from 1-confirmed to
25292533 // 0-confirmed or conflicted:
25302534 GetMainSignals ().BlockDisconnected (pblock, pindexDelete);
@@ -2585,6 +2589,9 @@ class ConnectTrace {
25852589 */
25862590bool CChainState::ConnectTip (BlockValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexNew, const std::shared_ptr<const CBlock>& pblock, ConnectTrace& connectTrace, DisconnectedBlockTransactions &disconnectpool)
25872591{
2592+ AssertLockHeld (cs_main);
2593+ AssertLockHeld (m_mempool.cs );
2594+
25882595 assert (pindexNew->pprev == m_chain.Tip ());
25892596 // Read block from disk.
25902597 int64_t nTime1 = GetTimeMicros ();
@@ -2625,11 +2632,11 @@ bool CChainState::ConnectTip(BlockValidationState& state, const CChainParams& ch
26252632 int64_t nTime5 = GetTimeMicros (); nTimeChainState += nTime5 - nTime4;
26262633 LogPrint (BCLog::BENCH, " - Writing chainstate: %.2fms [%.2fs (%.2fms/blk)]\n " , (nTime5 - nTime4) * MILLI, nTimeChainState * MICRO, nTimeChainState * MILLI / nBlocksTotal);
26272634 // Remove conflicting transactions from the mempool.;
2628- mempool .removeForBlock (blockConnecting.vtx , pindexNew->nHeight );
2635+ m_mempool .removeForBlock (blockConnecting.vtx , pindexNew->nHeight );
26292636 disconnectpool.removeForBlock (blockConnecting.vtx );
26302637 // Update m_chain & related variables.
26312638 m_chain.SetTip (pindexNew);
2632- UpdateTip (pindexNew, chainparams);
2639+ UpdateTip (m_mempool, pindexNew, chainparams);
26332640
26342641 int64_t nTime6 = GetTimeMicros (); nTimePostConnect += nTime6 - nTime5; nTimeTotal += nTime6 - nTime1;
26352642 LogPrint (BCLog::BENCH, " - Connect postprocess: %.2fms [%.2fs (%.2fms/blk)]\n " , (nTime6 - nTime5) * MILLI, nTimePostConnect * MICRO, nTimePostConnect * MILLI / nBlocksTotal);
@@ -2719,6 +2726,7 @@ void CChainState::PruneBlockIndexCandidates() {
27192726bool CChainState::ActivateBestChainStep (BlockValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexMostWork, const std::shared_ptr<const CBlock>& pblock, bool & fInvalidFound , ConnectTrace& connectTrace)
27202727{
27212728 AssertLockHeld (cs_main);
2729+ AssertLockHeld (m_mempool.cs );
27222730
27232731 const CBlockIndex *pindexOldTip = m_chain.Tip ();
27242732 const CBlockIndex *pindexFork = m_chain.FindFork (pindexMostWork);
@@ -2730,7 +2738,7 @@ bool CChainState::ActivateBestChainStep(BlockValidationState& state, const CChai
27302738 if (!DisconnectTip (state, chainparams, &disconnectpool)) {
27312739 // This is likely a fatal error, but keep the mempool consistent,
27322740 // just in case. Only remove from the mempool in this case.
2733- UpdateMempoolForReorg (disconnectpool, false );
2741+ UpdateMempoolForReorg (m_mempool, disconnectpool, false );
27342742
27352743 // If we're unable to disconnect a block during normal operation,
27362744 // then that is a failure of our local system -- we should abort
@@ -2774,7 +2782,7 @@ bool CChainState::ActivateBestChainStep(BlockValidationState& state, const CChai
27742782 // A system error occurred (disk space, database error, ...).
27752783 // Make the mempool consistent with the current tip, just in case
27762784 // any observers try to use it before shutdown.
2777- UpdateMempoolForReorg (disconnectpool, false );
2785+ UpdateMempoolForReorg (m_mempool, disconnectpool, false );
27782786 return false ;
27792787 }
27802788 } else {
@@ -2791,9 +2799,9 @@ bool CChainState::ActivateBestChainStep(BlockValidationState& state, const CChai
27912799 if (fBlocksDisconnected ) {
27922800 // If any blocks were disconnected, disconnectpool may be non empty. Add
27932801 // any disconnected transactions back to the mempool.
2794- UpdateMempoolForReorg (disconnectpool, true );
2802+ UpdateMempoolForReorg (m_mempool, disconnectpool, true );
27952803 }
2796- mempool .check (&CoinsTip ());
2804+ m_mempool .check (&CoinsTip ());
27972805
27982806 // Callbacks/notifications for a new best chain.
27992807 if (fInvalidFound )
@@ -2867,7 +2875,8 @@ bool CChainState::ActivateBestChain(BlockValidationState &state, const CChainPar
28672875 LimitValidationInterfaceQueue ();
28682876
28692877 {
2870- LOCK2 (cs_main, ::mempool.cs ); // Lock transaction pool for at least as long as it takes for connectTrace to be consumed
2878+ LOCK (cs_main);
2879+ LOCK (m_mempool.cs ); // Lock transaction pool for at least as long as it takes for connectTrace to be consumed
28712880 CBlockIndex* starting_tip = m_chain.Tip ();
28722881 bool blocks_connected = false ;
28732882 do {
@@ -3020,7 +3029,7 @@ bool CChainState::InvalidateBlock(BlockValidationState& state, const CChainParam
30203029 LimitValidationInterfaceQueue ();
30213030
30223031 LOCK (cs_main);
3023- LOCK (::mempool .cs ); // Lock for as long as disconnectpool is in scope to make sure UpdateMempoolForReorg is called after DisconnectTip without unlocking in between
3032+ LOCK (m_mempool .cs ); // Lock for as long as disconnectpool is in scope to make sure UpdateMempoolForReorg is called after DisconnectTip without unlocking in between
30243033 if (!m_chain.Contains (pindex)) break ;
30253034 pindex_was_in_chain = true ;
30263035 CBlockIndex *invalid_walk_tip = m_chain.Tip ();
@@ -3034,7 +3043,7 @@ bool CChainState::InvalidateBlock(BlockValidationState& state, const CChainParam
30343043 // transactions back to the mempool if disconnecting was successful,
30353044 // and we're not doing a very deep invalidation (in which case
30363045 // keeping the mempool up to date is probably futile anyway).
3037- UpdateMempoolForReorg (disconnectpool, /* fAddToMempool = */ (++disconnected <= 10 ) && ret);
3046+ UpdateMempoolForReorg (m_mempool, disconnectpool, /* fAddToMempool = */ (++disconnected <= 10 ) && ret);
30383047 if (!ret) return false ;
30393048 assert (invalid_walk_tip->pprev == m_chain.Tip ());
30403049
@@ -4517,7 +4526,8 @@ bool CChainState::RewindBlockIndex(const CChainParams& params)
45174526 // Loop until the tip is below nHeight, or we reach a pruned block.
45184527 while (!ShutdownRequested ()) {
45194528 {
4520- LOCK2 (cs_main, ::mempool.cs );
4529+ LOCK (cs_main);
4530+ LOCK (m_mempool.cs );
45214531 // Make sure nothing changed from under us (this won't happen because RewindBlockIndex runs before importing/network are active)
45224532 assert (tip == m_chain.Tip ());
45234533 if (tip == nullptr || tip->nHeight < nHeight) break ;
@@ -5246,7 +5256,7 @@ std::vector<CChainState*> ChainstateManager::GetAll()
52465256 return out;
52475257}
52485258
5249- CChainState& ChainstateManager::InitializeChainstate (const uint256& snapshot_blockhash)
5259+ CChainState& ChainstateManager::InitializeChainstate (CTxMemPool& mempool, const uint256& snapshot_blockhash)
52505260{
52515261 bool is_snapshot = !snapshot_blockhash.IsNull ();
52525262 std::unique_ptr<CChainState>& to_modify =
@@ -5255,8 +5265,7 @@ CChainState& ChainstateManager::InitializeChainstate(const uint256& snapshot_blo
52555265 if (to_modify) {
52565266 throw std::logic_error (" should not be overwriting a chainstate" );
52575267 }
5258-
5259- to_modify.reset (new CChainState (m_blockman, snapshot_blockhash));
5268+ to_modify.reset (new CChainState (mempool, m_blockman, snapshot_blockhash));
52605269
52615270 // Snapshot chainstates and initial IBD chaintates always become active.
52625271 if (is_snapshot || (!is_snapshot && !m_active_chainstate)) {
0 commit comments