@@ -432,9 +432,10 @@ static bool IsCurrentForFeeEstimation() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
432432 * and instead just erase from the mempool as needed.
433433 */
434434
435- static void UpdateMempoolForReorg (DisconnectedBlockTransactions& disconnectpool, bool fAddToMempool ) EXCLUSIVE_LOCKS_REQUIRED(cs_main, :: mempool.cs)
435+ static void UpdateMempoolForReorg (CTxMemPool& mempool, DisconnectedBlockTransactions& disconnectpool, bool fAddToMempool ) EXCLUSIVE_LOCKS_REQUIRED(cs_main, mempool.cs)
436436{
437437 AssertLockHeld (cs_main);
438+ AssertLockHeld (mempool.cs );
438439 std::vector<uint256> vHashUpdate;
439440 // disconnectpool's insertion_order index sorts the entries from
440441 // oldest to newest, but the oldest entry will be the last tx from the
@@ -1136,12 +1137,14 @@ CChainState::CChainState(BlockManager& blockman,
11361137 std::unique_ptr<llmq::CInstantSendManager>& isman,
11371138 std::unique_ptr<llmq::CQuorumBlockProcessor>& quorum_block_processor,
11381139 std::unique_ptr<CEvoDB>& evoDb,
1140+ CTxMemPool& mempool,
11391141 uint256 from_snapshot_blockhash)
11401142 : m_blockman(blockman),
11411143 m_clhandler(clhandler),
11421144 m_isman(isman),
11431145 m_quorum_block_processor(quorum_block_processor),
11441146 m_evoDb(evoDb),
1147+ m_mempool(mempool),
11451148 m_from_snapshot_blockhash(from_snapshot_blockhash) {}
11461149
11471150void CChainState::InitCoinsDB (
@@ -2485,7 +2488,7 @@ bool CChainState::FlushStateToDisk(
24852488 {
24862489 bool fFlushForPrune = false ;
24872490 bool fDoFullFlush = false ;
2488- CoinsCacheSizeState cache_state = GetCoinsCacheSizeState (&::mempool );
2491+ CoinsCacheSizeState cache_state = GetCoinsCacheSizeState (&m_mempool );
24892492 LOCK (cs_LastBlockFile);
24902493 if (fPruneMode && (fCheckForPruning || nManualPruneHeight > 0 ) && !fReindex ) {
24912494 if (nManualPruneHeight > 0 ) {
@@ -2637,7 +2640,7 @@ static void AppendWarning(std::string& res, const std::string& warn)
26372640}
26382641
26392642/* * Check warning conditions and do some notifications on new chain tip set. */
2640- void static UpdateTip (const CBlockIndex *pindexNew, const CChainParams& chainParams, const CEvoDB& evoDb)
2643+ void static UpdateTip (CTxMemPool& mempool, const CBlockIndex *pindexNew, const CChainParams& chainParams, const CEvoDB& evoDb)
26412644 EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
26422645{
26432646 // New best block
@@ -2684,7 +2687,6 @@ void static UpdateTip(const CBlockIndex *pindexNew, const CChainParams& chainPar
26842687 GuessVerificationProgress (chainParams.TxData (), pindexNew), ::ChainstateActive ().CoinsTip ().DynamicMemoryUsage () * (1.0 / (1 <<20 )), ::ChainstateActive ().CoinsTip ().GetCacheSize (),
26852688 evoDb.GetMemoryUsage () * (1.0 / (1 <<20 )),
26862689 !warningMessages.empty () ? strprintf (" warning='%s'" , warningMessages) : " " );
2687-
26882690}
26892691
26902692/* * Disconnect m_chain's tip.
@@ -2697,9 +2699,10 @@ void static UpdateTip(const CBlockIndex *pindexNew, const CChainParams& chainPar
26972699 * disconnectpool (note that the caller is responsible for mempool consistency
26982700 * in any case).
26992701 */
2700- bool CChainState::DisconnectTip (CValidationState& state, const CChainParams& chainparams, DisconnectedBlockTransactions * disconnectpool)
2702+ bool CChainState::DisconnectTip (CValidationState& state, const CChainParams& chainparams, DisconnectedBlockTransactions* disconnectpool)
27012703{
27022704 AssertLockHeld (cs_main);
2705+ AssertLockHeld (m_mempool.cs );
27032706
27042707 CBlockIndex *pindexDelete = m_chain.Tip ();
27052708 assert (pindexDelete);
@@ -2734,14 +2737,14 @@ bool CChainState::DisconnectTip(CValidationState& state, const CChainParams& cha
27342737 while (disconnectpool->DynamicMemoryUsage () > MAX_DISCONNECTED_TX_POOL_SIZE * 1000 ) {
27352738 // Drop the earliest entry, and remove its children from the mempool.
27362739 auto it = disconnectpool->queuedTx .get <insertion_order>().begin ();
2737- mempool .removeRecursive (**it, MemPoolRemovalReason::REORG);
2740+ m_mempool .removeRecursive (**it, MemPoolRemovalReason::REORG);
27382741 disconnectpool->removeEntry (it);
27392742 }
27402743 }
27412744
27422745 m_chain.SetTip (pindexDelete->pprev );
27432746
2744- UpdateTip (pindexDelete->pprev , chainparams, *m_evoDb);
2747+ UpdateTip (m_mempool, pindexDelete->pprev , chainparams, *m_evoDb);
27452748 // Let wallets know transactions went from 1-confirmed to
27462749 // 0-confirmed or conflicted:
27472750 GetMainSignals ().BlockDisconnected (pblock, pindexDelete);
@@ -2803,6 +2806,9 @@ class ConnectTrace {
28032806bool CChainState::ConnectTip (CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexNew, const std::shared_ptr<const CBlock>& pblock, ConnectTrace& connectTrace, DisconnectedBlockTransactions &disconnectpool)
28042807{
28052808 boost::posix_time::ptime start = boost::posix_time::microsec_clock::local_time ();
2809+ AssertLockHeld (cs_main);
2810+ AssertLockHeld (m_mempool.cs );
2811+
28062812 assert (pindexNew->pprev == m_chain.Tip ());
28072813 // Read block from disk.
28082814 int64_t nTime1 = GetTimeMicros ();
@@ -2846,11 +2852,11 @@ bool CChainState::ConnectTip(CValidationState& state, const CChainParams& chainp
28462852 int64_t nTime5 = GetTimeMicros (); nTimeChainState += nTime5 - nTime4;
28472853 LogPrint (BCLog::BENCHMARK, " - Writing chainstate: %.2fms [%.2fs (%.2fms/blk)]\n " , (nTime5 - nTime4) * MILLI, nTimeChainState * MICRO, nTimeChainState * MILLI / nBlocksTotal);
28482854 // Remove conflicting transactions from the mempool.;
2849- mempool .removeForBlock (blockConnecting.vtx , pindexNew->nHeight );
2855+ m_mempool .removeForBlock (blockConnecting.vtx , pindexNew->nHeight );
28502856 disconnectpool.removeForBlock (blockConnecting.vtx );
28512857 // Update m_chain & related variables.
28522858 m_chain.SetTip (pindexNew);
2853- UpdateTip (pindexNew, chainparams, *m_evoDb);
2859+ UpdateTip (m_mempool, pindexNew, chainparams, *m_evoDb);
28542860
28552861 int64_t nTime6 = GetTimeMicros (); nTimePostConnect += nTime6 - nTime5; nTimeTotal += nTime6 - nTime1;
28562862 LogPrint (BCLog::BENCHMARK, " - Connect postprocess: %.2fms [%.2fs (%.2fms/blk)]\n " , (nTime6 - nTime5) * MILLI, nTimePostConnect * MICRO, nTimePostConnect * MILLI / nBlocksTotal);
@@ -2948,6 +2954,7 @@ void CChainState::PruneBlockIndexCandidates() {
29482954bool CChainState::ActivateBestChainStep (CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexMostWork, const std::shared_ptr<const CBlock>& pblock, bool & fInvalidFound , ConnectTrace& connectTrace)
29492955{
29502956 AssertLockHeld (cs_main);
2957+ AssertLockHeld (m_mempool.cs );
29512958
29522959 const CBlockIndex *pindexOldTip = m_chain.Tip ();
29532960 const CBlockIndex *pindexFork = m_chain.FindFork (pindexMostWork);
@@ -2959,7 +2966,7 @@ bool CChainState::ActivateBestChainStep(CValidationState& state, const CChainPar
29592966 if (!DisconnectTip (state, chainparams, &disconnectpool)) {
29602967 // This is likely a fatal error, but keep the mempool consistent,
29612968 // just in case. Only remove from the mempool in this case.
2962- UpdateMempoolForReorg (disconnectpool, false );
2969+ UpdateMempoolForReorg (m_mempool, disconnectpool, false );
29632970
29642971 // If we're unable to disconnect a block during normal operation,
29652972 // then that is a failure of our local system -- we should abort
@@ -3003,7 +3010,7 @@ bool CChainState::ActivateBestChainStep(CValidationState& state, const CChainPar
30033010 // A system error occurred (disk space, database error, ...).
30043011 // Make the mempool consistent with the current tip, just in case
30053012 // any observers try to use it before shutdown.
3006- UpdateMempoolForReorg (disconnectpool, false );
3013+ UpdateMempoolForReorg (m_mempool, disconnectpool, false );
30073014 return false ;
30083015 }
30093016 } else {
@@ -3020,9 +3027,9 @@ bool CChainState::ActivateBestChainStep(CValidationState& state, const CChainPar
30203027 if (fBlocksDisconnected ) {
30213028 // If any blocks were disconnected, disconnectpool may be non empty. Add
30223029 // any disconnected transactions back to the mempool.
3023- UpdateMempoolForReorg (disconnectpool, true );
3030+ UpdateMempoolForReorg (m_mempool, disconnectpool, true );
30243031 }
3025- mempool .check (&CoinsTip ());
3032+ m_mempool .check (&CoinsTip ());
30263033
30273034 // Callbacks/notifications for a new best chain.
30283035 if (fInvalidFound )
@@ -3092,7 +3099,8 @@ bool CChainState::ActivateBestChain(CValidationState &state, const CChainParams&
30923099 LimitValidationInterfaceQueue ();
30933100
30943101 {
3095- LOCK2 (cs_main, ::mempool.cs ); // Lock transaction pool for at least as long as it takes for connectTrace to be consumed
3102+ LOCK (cs_main);
3103+ LOCK (m_mempool.cs ); // Lock transaction pool for at least as long as it takes for connectTrace to be consumed
30963104 CBlockIndex* starting_tip = m_chain.Tip ();
30973105 bool blocks_connected = false ;
30983106 do {
@@ -3254,7 +3262,7 @@ bool CChainState::InvalidateBlock(CValidationState& state, const CChainParams& c
32543262 LimitValidationInterfaceQueue ();
32553263
32563264 LOCK (cs_main);
3257- LOCK (::mempool .cs ); // Lock for as long as disconnectpool is in scope to make sure UpdateMempoolForReorg is called after DisconnectTip without unlocking in between
3265+ 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
32583266 if (!m_chain.Contains (pindex)) break ;
32593267 pindex_was_in_chain = true ;
32603268 CBlockIndex *invalid_walk_tip = m_chain.Tip ();
@@ -3274,7 +3282,7 @@ bool CChainState::InvalidateBlock(CValidationState& state, const CChainParams& c
32743282 // transactions back to the mempool if disconnecting was successful,
32753283 // and we're not doing a very deep invalidation (in which case
32763284 // keeping the mempool up to date is probably futile anyway).
3277- UpdateMempoolForReorg (disconnectpool, /* fAddToMempool = */ (++disconnected <= 10 ) && ret);
3285+ UpdateMempoolForReorg (m_mempool, disconnectpool, /* fAddToMempool = */ (++disconnected <= 10 ) && ret);
32783286 if (!ret) return false ;
32793287 assert (invalid_walk_tip->pprev == m_chain.Tip ());
32803288
@@ -3410,7 +3418,7 @@ bool CChainState::MarkConflictingBlock(CValidationState& state, const CChainPara
34103418 }
34113419
34123420 {
3413- LOCK (::mempool .cs ); // Lock for as long as disconnectpool is in scope to make sure UpdateMempoolForReorg is called after DisconnectTip without unlocking in between
3421+ 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
34143422 DisconnectedBlockTransactions disconnectpool;
34153423 while (m_chain.Contains (pindex)) {
34163424 const CBlockIndex* pindexOldTip = m_chain.Tip ();
@@ -3420,7 +3428,7 @@ bool CChainState::MarkConflictingBlock(CValidationState& state, const CChainPara
34203428 if (!DisconnectTip (state, chainparams, &disconnectpool)) {
34213429 // It's probably hopeless to try to make the mempool consistent
34223430 // here if DisconnectTip failed, but we can try.
3423- UpdateMempoolForReorg (disconnectpool, false );
3431+ UpdateMempoolForReorg (m_mempool, disconnectpool, false );
34243432 return false ;
34253433 }
34263434 if (pindexOldTip == pindexBestHeader) {
@@ -3442,8 +3450,8 @@ bool CChainState::MarkConflictingBlock(CValidationState& state, const CChainPara
34423450
34433451 // DisconnectTip will add transactions to disconnectpool; try to add these
34443452 // back to the mempool.
3445- UpdateMempoolForReorg (disconnectpool, true );
3446- } // ::mempool .cs
3453+ UpdateMempoolForReorg (m_mempool, disconnectpool, true );
3454+ } // m_mempool .cs
34473455
34483456 // The resulting new best tip may not be in setBlockIndexCandidates anymore, so
34493457 // add it again.
@@ -5529,6 +5537,7 @@ CChainState& ChainstateManager::InitializeChainstate(std::unique_ptr<llmq::CChai
55295537 std::unique_ptr<llmq::CInstantSendManager>& isman,
55305538 std::unique_ptr<llmq::CQuorumBlockProcessor>& quorum_block_processor,
55315539 std::unique_ptr<CEvoDB>& evoDb,
5540+ CTxMemPool& mempool,
55325541 const uint256& snapshot_blockhash)
55335542{
55345543 bool is_snapshot = !snapshot_blockhash.IsNull ();
@@ -5539,7 +5548,7 @@ CChainState& ChainstateManager::InitializeChainstate(std::unique_ptr<llmq::CChai
55395548 throw std::logic_error (" should not be overwriting a chainstate" );
55405549 }
55415550
5542- to_modify.reset (new CChainState (m_blockman, clhandler, isman, quorum_block_processor, evoDb, snapshot_blockhash));
5551+ to_modify.reset (new CChainState (m_blockman, clhandler, isman, quorum_block_processor, evoDb, mempool, snapshot_blockhash));
55435552
55445553 // Snapshot chainstates and initial IBD chaintates always become active.
55455554 if (is_snapshot || (!is_snapshot && !m_active_chainstate)) {
0 commit comments