@@ -433,9 +433,10 @@ static bool IsCurrentForFeeEstimation() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
433433 * and instead just erase from the mempool as needed.
434434 */
435435
436- static void UpdateMempoolForReorg (DisconnectedBlockTransactions& disconnectpool, bool fAddToMempool ) EXCLUSIVE_LOCKS_REQUIRED(cs_main, :: mempool.cs)
436+ static void UpdateMempoolForReorg (CTxMemPool& mempool, DisconnectedBlockTransactions& disconnectpool, bool fAddToMempool ) EXCLUSIVE_LOCKS_REQUIRED(cs_main, mempool.cs)
437437{
438438 AssertLockHeld (cs_main);
439+ AssertLockHeld (mempool.cs );
439440 std::vector<uint256> vHashUpdate;
440441 // disconnectpool's insertion_order index sorts the entries from
441442 // oldest to newest, but the oldest entry will be the last tx from the
@@ -1137,12 +1138,14 @@ CChainState::CChainState(BlockManager& blockman,
11371138 std::unique_ptr<llmq::CInstantSendManager>& isman,
11381139 std::unique_ptr<llmq::CQuorumBlockProcessor>& quorum_block_processor,
11391140 std::unique_ptr<CEvoDB>& evoDb,
1141+ CTxMemPool& mempool,
11401142 uint256 from_snapshot_blockhash)
11411143 : m_blockman(blockman),
11421144 m_clhandler(clhandler),
11431145 m_isman(isman),
11441146 m_quorum_block_processor(quorum_block_processor),
11451147 m_evoDb(evoDb),
1148+ m_mempool(mempool),
11461149 m_from_snapshot_blockhash(from_snapshot_blockhash) {}
11471150
11481151void CChainState::InitCoinsDB (
@@ -2486,7 +2489,7 @@ bool CChainState::FlushStateToDisk(
24862489 {
24872490 bool fFlushForPrune = false ;
24882491 bool fDoFullFlush = false ;
2489- CoinsCacheSizeState cache_state = GetCoinsCacheSizeState (&::mempool );
2492+ CoinsCacheSizeState cache_state = GetCoinsCacheSizeState (&m_mempool );
24902493 LOCK (cs_LastBlockFile);
24912494 if (fPruneMode && (fCheckForPruning || nManualPruneHeight > 0 ) && !fReindex ) {
24922495 if (nManualPruneHeight > 0 ) {
@@ -2638,7 +2641,7 @@ static void AppendWarning(std::string& res, const std::string& warn)
26382641}
26392642
26402643/* * Check warning conditions and do some notifications on new chain tip set. */
2641- void static UpdateTip (const CBlockIndex *pindexNew, const CChainParams& chainParams, const CEvoDB& evoDb)
2644+ void static UpdateTip (CTxMemPool& mempool, const CBlockIndex *pindexNew, const CChainParams& chainParams, const CEvoDB& evoDb)
26422645 EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
26432646{
26442647 // New best block
@@ -2685,7 +2688,6 @@ void static UpdateTip(const CBlockIndex *pindexNew, const CChainParams& chainPar
26852688 GuessVerificationProgress (chainParams.TxData (), pindexNew), ::ChainstateActive ().CoinsTip ().DynamicMemoryUsage () * (1.0 / (1 <<20 )), ::ChainstateActive ().CoinsTip ().GetCacheSize (),
26862689 evoDb.GetMemoryUsage () * (1.0 / (1 <<20 )),
26872690 !warningMessages.empty () ? strprintf (" warning='%s'" , warningMessages) : " " );
2688-
26892691}
26902692
26912693/* * Disconnect m_chain's tip.
@@ -2698,9 +2700,10 @@ void static UpdateTip(const CBlockIndex *pindexNew, const CChainParams& chainPar
26982700 * disconnectpool (note that the caller is responsible for mempool consistency
26992701 * in any case).
27002702 */
2701- bool CChainState::DisconnectTip (CValidationState& state, const CChainParams& chainparams, DisconnectedBlockTransactions * disconnectpool)
2703+ bool CChainState::DisconnectTip (CValidationState& state, const CChainParams& chainparams, DisconnectedBlockTransactions* disconnectpool)
27022704{
27032705 AssertLockHeld (cs_main);
2706+ AssertLockHeld (m_mempool.cs );
27042707
27052708 CBlockIndex *pindexDelete = m_chain.Tip ();
27062709 assert (pindexDelete);
@@ -2735,14 +2738,14 @@ bool CChainState::DisconnectTip(CValidationState& state, const CChainParams& cha
27352738 while (disconnectpool->DynamicMemoryUsage () > MAX_DISCONNECTED_TX_POOL_SIZE * 1000 ) {
27362739 // Drop the earliest entry, and remove its children from the mempool.
27372740 auto it = disconnectpool->queuedTx .get <insertion_order>().begin ();
2738- mempool .removeRecursive (**it, MemPoolRemovalReason::REORG);
2741+ m_mempool .removeRecursive (**it, MemPoolRemovalReason::REORG);
27392742 disconnectpool->removeEntry (it);
27402743 }
27412744 }
27422745
27432746 m_chain.SetTip (pindexDelete->pprev );
27442747
2745- UpdateTip (pindexDelete->pprev , chainparams, *m_evoDb);
2748+ UpdateTip (m_mempool, pindexDelete->pprev , chainparams, *m_evoDb);
27462749 // Let wallets know transactions went from 1-confirmed to
27472750 // 0-confirmed or conflicted:
27482751 GetMainSignals ().BlockDisconnected (pblock, pindexDelete);
@@ -2804,6 +2807,9 @@ class ConnectTrace {
28042807bool CChainState::ConnectTip (CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexNew, const std::shared_ptr<const CBlock>& pblock, ConnectTrace& connectTrace, DisconnectedBlockTransactions &disconnectpool)
28052808{
28062809 boost::posix_time::ptime start = boost::posix_time::microsec_clock::local_time ();
2810+ AssertLockHeld (cs_main);
2811+ AssertLockHeld (m_mempool.cs );
2812+
28072813 assert (pindexNew->pprev == m_chain.Tip ());
28082814 // Read block from disk.
28092815 int64_t nTime1 = GetTimeMicros ();
@@ -2847,11 +2853,11 @@ bool CChainState::ConnectTip(CValidationState& state, const CChainParams& chainp
28472853 int64_t nTime5 = GetTimeMicros (); nTimeChainState += nTime5 - nTime4;
28482854 LogPrint (BCLog::BENCHMARK, " - Writing chainstate: %.2fms [%.2fs (%.2fms/blk)]\n " , (nTime5 - nTime4) * MILLI, nTimeChainState * MICRO, nTimeChainState * MILLI / nBlocksTotal);
28492855 // Remove conflicting transactions from the mempool.;
2850- mempool .removeForBlock (blockConnecting.vtx , pindexNew->nHeight );
2856+ m_mempool .removeForBlock (blockConnecting.vtx , pindexNew->nHeight );
28512857 disconnectpool.removeForBlock (blockConnecting.vtx );
28522858 // Update m_chain & related variables.
28532859 m_chain.SetTip (pindexNew);
2854- UpdateTip (pindexNew, chainparams, *m_evoDb);
2860+ UpdateTip (m_mempool, pindexNew, chainparams, *m_evoDb);
28552861
28562862 int64_t nTime6 = GetTimeMicros (); nTimePostConnect += nTime6 - nTime5; nTimeTotal += nTime6 - nTime1;
28572863 LogPrint (BCLog::BENCHMARK, " - Connect postprocess: %.2fms [%.2fs (%.2fms/blk)]\n " , (nTime6 - nTime5) * MILLI, nTimePostConnect * MICRO, nTimePostConnect * MILLI / nBlocksTotal);
@@ -2949,6 +2955,7 @@ void CChainState::PruneBlockIndexCandidates() {
29492955bool CChainState::ActivateBestChainStep (CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexMostWork, const std::shared_ptr<const CBlock>& pblock, bool & fInvalidFound , ConnectTrace& connectTrace)
29502956{
29512957 AssertLockHeld (cs_main);
2958+ AssertLockHeld (m_mempool.cs );
29522959
29532960 const CBlockIndex *pindexOldTip = m_chain.Tip ();
29542961 const CBlockIndex *pindexFork = m_chain.FindFork (pindexMostWork);
@@ -2960,7 +2967,7 @@ bool CChainState::ActivateBestChainStep(CValidationState& state, const CChainPar
29602967 if (!DisconnectTip (state, chainparams, &disconnectpool)) {
29612968 // This is likely a fatal error, but keep the mempool consistent,
29622969 // just in case. Only remove from the mempool in this case.
2963- UpdateMempoolForReorg (disconnectpool, false );
2970+ UpdateMempoolForReorg (m_mempool, disconnectpool, false );
29642971
29652972 // If we're unable to disconnect a block during normal operation,
29662973 // then that is a failure of our local system -- we should abort
@@ -3004,7 +3011,7 @@ bool CChainState::ActivateBestChainStep(CValidationState& state, const CChainPar
30043011 // A system error occurred (disk space, database error, ...).
30053012 // Make the mempool consistent with the current tip, just in case
30063013 // any observers try to use it before shutdown.
3007- UpdateMempoolForReorg (disconnectpool, false );
3014+ UpdateMempoolForReorg (m_mempool, disconnectpool, false );
30083015 return false ;
30093016 }
30103017 } else {
@@ -3021,9 +3028,9 @@ bool CChainState::ActivateBestChainStep(CValidationState& state, const CChainPar
30213028 if (fBlocksDisconnected ) {
30223029 // If any blocks were disconnected, disconnectpool may be non empty. Add
30233030 // any disconnected transactions back to the mempool.
3024- UpdateMempoolForReorg (disconnectpool, true );
3031+ UpdateMempoolForReorg (m_mempool, disconnectpool, true );
30253032 }
3026- mempool .check (&CoinsTip ());
3033+ m_mempool .check (&CoinsTip ());
30273034
30283035 // Callbacks/notifications for a new best chain.
30293036 if (fInvalidFound )
@@ -3095,7 +3102,8 @@ bool CChainState::ActivateBestChain(CValidationState &state, const CChainParams&
30953102 LimitValidationInterfaceQueue ();
30963103
30973104 {
3098- LOCK2 (cs_main, ::mempool.cs ); // Lock transaction pool for at least as long as it takes for connectTrace to be consumed
3105+ LOCK (cs_main);
3106+ LOCK (m_mempool.cs ); // Lock transaction pool for at least as long as it takes for connectTrace to be consumed
30993107 CBlockIndex* starting_tip = m_chain.Tip ();
31003108 bool blocks_connected = false ;
31013109 do {
@@ -3258,7 +3266,7 @@ bool CChainState::InvalidateBlock(CValidationState& state, const CChainParams& c
32583266 LimitValidationInterfaceQueue ();
32593267
32603268 LOCK (cs_main);
3261- LOCK (::mempool .cs ); // Lock for as long as disconnectpool is in scope to make sure UpdateMempoolForReorg is called after DisconnectTip without unlocking in between
3269+ 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
32623270 if (!m_chain.Contains (pindex)) break ;
32633271 pindex_was_in_chain = true ;
32643272 CBlockIndex *invalid_walk_tip = m_chain.Tip ();
@@ -3278,7 +3286,7 @@ bool CChainState::InvalidateBlock(CValidationState& state, const CChainParams& c
32783286 // transactions back to the mempool if disconnecting was successful,
32793287 // and we're not doing a very deep invalidation (in which case
32803288 // keeping the mempool up to date is probably futile anyway).
3281- UpdateMempoolForReorg (disconnectpool, /* fAddToMempool = */ (++disconnected <= 10 ) && ret);
3289+ UpdateMempoolForReorg (m_mempool, disconnectpool, /* fAddToMempool = */ (++disconnected <= 10 ) && ret);
32823290 if (!ret) return false ;
32833291 assert (invalid_walk_tip->pprev == m_chain.Tip ());
32843292
@@ -3414,7 +3422,7 @@ bool CChainState::MarkConflictingBlock(CValidationState& state, const CChainPara
34143422 }
34153423
34163424 {
3417- LOCK (::mempool .cs ); // Lock for as long as disconnectpool is in scope to make sure UpdateMempoolForReorg is called after DisconnectTip without unlocking in between
3425+ 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
34183426 DisconnectedBlockTransactions disconnectpool;
34193427 while (m_chain.Contains (pindex)) {
34203428 const CBlockIndex* pindexOldTip = m_chain.Tip ();
@@ -3424,7 +3432,7 @@ bool CChainState::MarkConflictingBlock(CValidationState& state, const CChainPara
34243432 if (!DisconnectTip (state, chainparams, &disconnectpool)) {
34253433 // It's probably hopeless to try to make the mempool consistent
34263434 // here if DisconnectTip failed, but we can try.
3427- UpdateMempoolForReorg (disconnectpool, false );
3435+ UpdateMempoolForReorg (m_mempool, disconnectpool, false );
34283436 return false ;
34293437 }
34303438 if (pindexOldTip == pindexBestHeader) {
@@ -3446,8 +3454,8 @@ bool CChainState::MarkConflictingBlock(CValidationState& state, const CChainPara
34463454
34473455 // DisconnectTip will add transactions to disconnectpool; try to add these
34483456 // back to the mempool.
3449- UpdateMempoolForReorg (disconnectpool, true );
3450- } // ::mempool .cs
3457+ UpdateMempoolForReorg (m_mempool, disconnectpool, true );
3458+ } // m_mempool .cs
34513459
34523460 // The resulting new best tip may not be in setBlockIndexCandidates anymore, so
34533461 // add it again.
@@ -5535,6 +5543,7 @@ CChainState& ChainstateManager::InitializeChainstate(std::unique_ptr<llmq::CChai
55355543 std::unique_ptr<llmq::CInstantSendManager>& isman,
55365544 std::unique_ptr<llmq::CQuorumBlockProcessor>& quorum_block_processor,
55375545 std::unique_ptr<CEvoDB>& evoDb,
5546+ CTxMemPool& mempool,
55385547 const uint256& snapshot_blockhash)
55395548{
55405549 bool is_snapshot = !snapshot_blockhash.IsNull ();
@@ -5545,7 +5554,7 @@ CChainState& ChainstateManager::InitializeChainstate(std::unique_ptr<llmq::CChai
55455554 throw std::logic_error (" should not be overwriting a chainstate" );
55465555 }
55475556
5548- to_modify.reset (new CChainState (m_blockman, clhandler, isman, quorum_block_processor, evoDb, snapshot_blockhash));
5557+ to_modify.reset (new CChainState (m_blockman, clhandler, isman, quorum_block_processor, evoDb, mempool, snapshot_blockhash));
55495558
55505559 // Snapshot chainstates and initial IBD chaintates always become active.
55515560 if (is_snapshot || (!is_snapshot && !m_active_chainstate)) {
0 commit comments