@@ -370,9 +370,10 @@ static bool IsCurrentForFeeEstimation() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
370
370
* and instead just erase from the mempool as needed.
371
371
*/
372
372
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)
374
374
{
375
375
AssertLockHeld (cs_main);
376
+ AssertLockHeld (mempool.cs );
376
377
std::vector<uint256> vHashUpdate;
377
378
// disconnectpool's insertion_order index sorts the entries from
378
379
// oldest to newest, but the oldest entry will be the last tx from the
@@ -1254,8 +1255,9 @@ void CoinsViews::InitCache()
1254
1255
m_cacheview = MakeUnique<CCoinsViewCache>(&m_catcherview);
1255
1256
}
1256
1257
1257
- CChainState::CChainState (BlockManager& blockman, uint256 from_snapshot_blockhash)
1258
+ CChainState::CChainState (CTxMemPool& mempool, BlockManager& blockman, uint256 from_snapshot_blockhash)
1258
1259
: m_blockman(blockman),
1260
+ m_mempool(mempool),
1259
1261
m_from_snapshot_blockhash(from_snapshot_blockhash) {}
1260
1262
1261
1263
void CChainState::InitCoinsDB (
@@ -2280,7 +2282,7 @@ bool CChainState::FlushStateToDisk(
2280
2282
{
2281
2283
bool fFlushForPrune = false ;
2282
2284
bool fDoFullFlush = false ;
2283
- CoinsCacheSizeState cache_state = GetCoinsCacheSizeState (&::mempool );
2285
+ CoinsCacheSizeState cache_state = GetCoinsCacheSizeState (&m_mempool );
2284
2286
LOCK (cs_LastBlockFile);
2285
2287
if (fPruneMode && (fCheckForPruning || nManualPruneHeight > 0 ) && !fReindex ) {
2286
2288
if (nManualPruneHeight > 0 ) {
@@ -2426,7 +2428,7 @@ static void AppendWarning(bilingual_str& res, const bilingual_str& warn)
2426
2428
}
2427
2429
2428
2430
/* * 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)
2430
2432
EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
2431
2433
{
2432
2434
// New best block
@@ -2472,7 +2474,6 @@ void static UpdateTip(const CBlockIndex* pindexNew, const CChainParams& chainPar
2472
2474
FormatISO8601DateTime (pindexNew->GetBlockTime ()),
2473
2475
GuessVerificationProgress (chainParams.TxData (), pindexNew), ::ChainstateActive ().CoinsTip ().DynamicMemoryUsage () * (1.0 / (1 <<20 )), ::ChainstateActive ().CoinsTip ().GetCacheSize (),
2474
2476
!warning_messages.empty () ? strprintf (" warning='%s'" , warning_messages.original ) : " " );
2475
-
2476
2477
}
2477
2478
2478
2479
/* * Disconnect m_chain's tip.
@@ -2485,8 +2486,11 @@ void static UpdateTip(const CBlockIndex* pindexNew, const CChainParams& chainPar
2485
2486
* disconnectpool (note that the caller is responsible for mempool consistency
2486
2487
* in any case).
2487
2488
*/
2488
- bool CChainState::DisconnectTip (BlockValidationState& state, const CChainParams& chainparams, DisconnectedBlockTransactions * disconnectpool)
2489
+ bool CChainState::DisconnectTip (BlockValidationState& state, const CChainParams& chainparams, DisconnectedBlockTransactions* disconnectpool)
2489
2490
{
2491
+ AssertLockHeld (cs_main);
2492
+ AssertLockHeld (m_mempool.cs );
2493
+
2490
2494
CBlockIndex *pindexDelete = m_chain.Tip ();
2491
2495
assert (pindexDelete);
2492
2496
// Read block from disk.
@@ -2517,14 +2521,14 @@ bool CChainState::DisconnectTip(BlockValidationState& state, const CChainParams&
2517
2521
while (disconnectpool->DynamicMemoryUsage () > MAX_DISCONNECTED_TX_POOL_SIZE * 1000 ) {
2518
2522
// Drop the earliest entry, and remove its children from the mempool.
2519
2523
auto it = disconnectpool->queuedTx .get <insertion_order>().begin ();
2520
- mempool .removeRecursive (**it, MemPoolRemovalReason::REORG);
2524
+ m_mempool .removeRecursive (**it, MemPoolRemovalReason::REORG);
2521
2525
disconnectpool->removeEntry (it);
2522
2526
}
2523
2527
}
2524
2528
2525
2529
m_chain.SetTip (pindexDelete->pprev );
2526
2530
2527
- UpdateTip (pindexDelete->pprev , chainparams);
2531
+ UpdateTip (m_mempool, pindexDelete->pprev , chainparams);
2528
2532
// Let wallets know transactions went from 1-confirmed to
2529
2533
// 0-confirmed or conflicted:
2530
2534
GetMainSignals ().BlockDisconnected (pblock, pindexDelete);
@@ -2585,6 +2589,9 @@ class ConnectTrace {
2585
2589
*/
2586
2590
bool CChainState::ConnectTip (BlockValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexNew, const std::shared_ptr<const CBlock>& pblock, ConnectTrace& connectTrace, DisconnectedBlockTransactions &disconnectpool)
2587
2591
{
2592
+ AssertLockHeld (cs_main);
2593
+ AssertLockHeld (m_mempool.cs );
2594
+
2588
2595
assert (pindexNew->pprev == m_chain.Tip ());
2589
2596
// Read block from disk.
2590
2597
int64_t nTime1 = GetTimeMicros ();
@@ -2625,11 +2632,11 @@ bool CChainState::ConnectTip(BlockValidationState& state, const CChainParams& ch
2625
2632
int64_t nTime5 = GetTimeMicros (); nTimeChainState += nTime5 - nTime4;
2626
2633
LogPrint (BCLog::BENCH, " - Writing chainstate: %.2fms [%.2fs (%.2fms/blk)]\n " , (nTime5 - nTime4) * MILLI, nTimeChainState * MICRO, nTimeChainState * MILLI / nBlocksTotal);
2627
2634
// Remove conflicting transactions from the mempool.;
2628
- mempool .removeForBlock (blockConnecting.vtx , pindexNew->nHeight );
2635
+ m_mempool .removeForBlock (blockConnecting.vtx , pindexNew->nHeight );
2629
2636
disconnectpool.removeForBlock (blockConnecting.vtx );
2630
2637
// Update m_chain & related variables.
2631
2638
m_chain.SetTip (pindexNew);
2632
- UpdateTip (pindexNew, chainparams);
2639
+ UpdateTip (m_mempool, pindexNew, chainparams);
2633
2640
2634
2641
int64_t nTime6 = GetTimeMicros (); nTimePostConnect += nTime6 - nTime5; nTimeTotal += nTime6 - nTime1;
2635
2642
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() {
2719
2726
bool CChainState::ActivateBestChainStep (BlockValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexMostWork, const std::shared_ptr<const CBlock>& pblock, bool & fInvalidFound , ConnectTrace& connectTrace)
2720
2727
{
2721
2728
AssertLockHeld (cs_main);
2729
+ AssertLockHeld (m_mempool.cs );
2722
2730
2723
2731
const CBlockIndex *pindexOldTip = m_chain.Tip ();
2724
2732
const CBlockIndex *pindexFork = m_chain.FindFork (pindexMostWork);
@@ -2730,7 +2738,7 @@ bool CChainState::ActivateBestChainStep(BlockValidationState& state, const CChai
2730
2738
if (!DisconnectTip (state, chainparams, &disconnectpool)) {
2731
2739
// This is likely a fatal error, but keep the mempool consistent,
2732
2740
// just in case. Only remove from the mempool in this case.
2733
- UpdateMempoolForReorg (disconnectpool, false );
2741
+ UpdateMempoolForReorg (m_mempool, disconnectpool, false );
2734
2742
2735
2743
// If we're unable to disconnect a block during normal operation,
2736
2744
// then that is a failure of our local system -- we should abort
@@ -2774,7 +2782,7 @@ bool CChainState::ActivateBestChainStep(BlockValidationState& state, const CChai
2774
2782
// A system error occurred (disk space, database error, ...).
2775
2783
// Make the mempool consistent with the current tip, just in case
2776
2784
// any observers try to use it before shutdown.
2777
- UpdateMempoolForReorg (disconnectpool, false );
2785
+ UpdateMempoolForReorg (m_mempool, disconnectpool, false );
2778
2786
return false ;
2779
2787
}
2780
2788
} else {
@@ -2791,9 +2799,9 @@ bool CChainState::ActivateBestChainStep(BlockValidationState& state, const CChai
2791
2799
if (fBlocksDisconnected ) {
2792
2800
// If any blocks were disconnected, disconnectpool may be non empty. Add
2793
2801
// any disconnected transactions back to the mempool.
2794
- UpdateMempoolForReorg (disconnectpool, true );
2802
+ UpdateMempoolForReorg (m_mempool, disconnectpool, true );
2795
2803
}
2796
- mempool .check (&CoinsTip ());
2804
+ m_mempool .check (&CoinsTip ());
2797
2805
2798
2806
// Callbacks/notifications for a new best chain.
2799
2807
if (fInvalidFound )
@@ -2867,7 +2875,8 @@ bool CChainState::ActivateBestChain(BlockValidationState &state, const CChainPar
2867
2875
LimitValidationInterfaceQueue ();
2868
2876
2869
2877
{
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
2871
2880
CBlockIndex* starting_tip = m_chain.Tip ();
2872
2881
bool blocks_connected = false ;
2873
2882
do {
@@ -3020,7 +3029,7 @@ bool CChainState::InvalidateBlock(BlockValidationState& state, const CChainParam
3020
3029
LimitValidationInterfaceQueue ();
3021
3030
3022
3031
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
3024
3033
if (!m_chain.Contains (pindex)) break ;
3025
3034
pindex_was_in_chain = true ;
3026
3035
CBlockIndex *invalid_walk_tip = m_chain.Tip ();
@@ -3034,7 +3043,7 @@ bool CChainState::InvalidateBlock(BlockValidationState& state, const CChainParam
3034
3043
// transactions back to the mempool if disconnecting was successful,
3035
3044
// and we're not doing a very deep invalidation (in which case
3036
3045
// 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);
3038
3047
if (!ret) return false ;
3039
3048
assert (invalid_walk_tip->pprev == m_chain.Tip ());
3040
3049
@@ -4517,7 +4526,8 @@ bool CChainState::RewindBlockIndex(const CChainParams& params)
4517
4526
// Loop until the tip is below nHeight, or we reach a pruned block.
4518
4527
while (!ShutdownRequested ()) {
4519
4528
{
4520
- LOCK2 (cs_main, ::mempool.cs );
4529
+ LOCK (cs_main);
4530
+ LOCK (m_mempool.cs );
4521
4531
// Make sure nothing changed from under us (this won't happen because RewindBlockIndex runs before importing/network are active)
4522
4532
assert (tip == m_chain.Tip ());
4523
4533
if (tip == nullptr || tip->nHeight < nHeight) break ;
@@ -5246,7 +5256,7 @@ std::vector<CChainState*> ChainstateManager::GetAll()
5246
5256
return out;
5247
5257
}
5248
5258
5249
- CChainState& ChainstateManager::InitializeChainstate (const uint256& snapshot_blockhash)
5259
+ CChainState& ChainstateManager::InitializeChainstate (CTxMemPool& mempool, const uint256& snapshot_blockhash)
5250
5260
{
5251
5261
bool is_snapshot = !snapshot_blockhash.IsNull ();
5252
5262
std::unique_ptr<CChainState>& to_modify =
@@ -5255,8 +5265,7 @@ CChainState& ChainstateManager::InitializeChainstate(const uint256& snapshot_blo
5255
5265
if (to_modify) {
5256
5266
throw std::logic_error (" should not be overwriting a chainstate" );
5257
5267
}
5258
-
5259
- to_modify.reset (new CChainState (m_blockman, snapshot_blockhash));
5268
+ to_modify.reset (new CChainState (mempool, m_blockman, snapshot_blockhash));
5260
5269
5261
5270
// Snapshot chainstates and initial IBD chaintates always become active.
5262
5271
if (is_snapshot || (!is_snapshot && !m_active_chainstate)) {
0 commit comments