@@ -329,7 +329,8 @@ static bool IsCurrentForFeeEstimation(CChainState& active_chainstate) EXCLUSIVE_
329
329
return true ;
330
330
}
331
331
332
- /* Make mempool consistent after a reorg, by re-adding or recursively erasing
332
+ /* *
333
+ * Make mempool consistent after a reorg, by re-adding or recursively erasing
333
334
* disconnected block transactions from the mempool, and also removing any
334
335
* other transactions from the mempool that are no longer valid given the new
335
336
* tip/height.
@@ -1208,7 +1209,7 @@ void CoinsViews::InitCache()
1208
1209
m_cacheview = std::make_unique<CCoinsViewCache>(&m_catcherview);
1209
1210
}
1210
1211
1211
- CChainState::CChainState (CTxMemPool& mempool, BlockManager& blockman, std::optional<uint256> from_snapshot_blockhash)
1212
+ CChainState::CChainState (CTxMemPool* mempool, BlockManager& blockman, std::optional<uint256> from_snapshot_blockhash)
1212
1213
: m_mempool(mempool),
1213
1214
m_params(::Params()),
1214
1215
m_blockman(blockman),
@@ -2053,7 +2054,7 @@ bool CChainState::FlushStateToDisk(
2053
2054
bool fFlushForPrune = false ;
2054
2055
bool fDoFullFlush = false ;
2055
2056
2056
- CoinsCacheSizeState cache_state = GetCoinsCacheSizeState (& m_mempool);
2057
+ CoinsCacheSizeState cache_state = GetCoinsCacheSizeState (m_mempool);
2057
2058
LOCK (cs_LastBlockFile);
2058
2059
if (fPruneMode && (fCheckForPruning || nManualPruneHeight > 0 ) && !fReindex ) {
2059
2060
// make sure we don't prune above the blockfilterindexes bestblocks
@@ -2205,11 +2206,13 @@ static void AppendWarning(bilingual_str& res, const bilingual_str& warn)
2205
2206
}
2206
2207
2207
2208
/* * Check warning conditions and do some notifications on new chain tip set. */
2208
- static void UpdateTip (CTxMemPool& mempool, const CBlockIndex* pindexNew, const CChainParams& chainParams, CChainState& active_chainstate)
2209
+ static void UpdateTip (CTxMemPool* mempool, const CBlockIndex* pindexNew, const CChainParams& chainParams, CChainState& active_chainstate)
2209
2210
EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
2210
2211
{
2211
2212
// New best block
2212
- mempool.AddTransactionsUpdated (1 );
2213
+ if (mempool) {
2214
+ mempool->AddTransactionsUpdated (1 );
2215
+ }
2213
2216
2214
2217
{
2215
2218
LOCK (g_best_block_mutex);
@@ -2254,7 +2257,7 @@ static void UpdateTip(CTxMemPool& mempool, const CBlockIndex* pindexNew, const C
2254
2257
bool CChainState::DisconnectTip (BlockValidationState& state, DisconnectedBlockTransactions* disconnectpool)
2255
2258
{
2256
2259
AssertLockHeld (cs_main);
2257
- AssertLockHeld (m_mempool. cs );
2260
+ if (m_mempool) AssertLockHeld (m_mempool-> cs );
2258
2261
2259
2262
CBlockIndex *pindexDelete = m_chain.Tip ();
2260
2263
assert (pindexDelete);
@@ -2280,15 +2283,15 @@ bool CChainState::DisconnectTip(BlockValidationState& state, DisconnectedBlockTr
2280
2283
return false ;
2281
2284
}
2282
2285
2283
- if (disconnectpool) {
2286
+ if (disconnectpool && m_mempool ) {
2284
2287
// Save transactions to re-add to mempool at end of reorg
2285
2288
for (auto it = block.vtx .rbegin (); it != block.vtx .rend (); ++it) {
2286
2289
disconnectpool->addTransaction (*it);
2287
2290
}
2288
2291
while (disconnectpool->DynamicMemoryUsage () > MAX_DISCONNECTED_TX_POOL_SIZE * 1000 ) {
2289
2292
// Drop the earliest entry, and remove its children from the mempool.
2290
2293
auto it = disconnectpool->queuedTx .get <insertion_order>().begin ();
2291
- m_mempool. removeRecursive (**it, MemPoolRemovalReason::REORG);
2294
+ m_mempool-> removeRecursive (**it, MemPoolRemovalReason::REORG);
2292
2295
disconnectpool->removeEntry (it);
2293
2296
}
2294
2297
}
@@ -2357,7 +2360,7 @@ class ConnectTrace {
2357
2360
bool CChainState::ConnectTip (BlockValidationState& state, CBlockIndex* pindexNew, const std::shared_ptr<const CBlock>& pblock, ConnectTrace& connectTrace, DisconnectedBlockTransactions& disconnectpool)
2358
2361
{
2359
2362
AssertLockHeld (cs_main);
2360
- AssertLockHeld (m_mempool. cs );
2363
+ if (m_mempool) AssertLockHeld (m_mempool-> cs );
2361
2364
2362
2365
assert (pindexNew->pprev == m_chain.Tip ());
2363
2366
// Read block from disk.
@@ -2401,8 +2404,10 @@ bool CChainState::ConnectTip(BlockValidationState& state, CBlockIndex* pindexNew
2401
2404
int64_t nTime5 = GetTimeMicros (); nTimeChainState += nTime5 - nTime4;
2402
2405
LogPrint (BCLog::BENCH, " - Writing chainstate: %.2fms [%.2fs (%.2fms/blk)]\n " , (nTime5 - nTime4) * MILLI, nTimeChainState * MICRO, nTimeChainState * MILLI / nBlocksTotal);
2403
2406
// Remove conflicting transactions from the mempool.;
2404
- m_mempool.removeForBlock (blockConnecting.vtx , pindexNew->nHeight );
2405
- disconnectpool.removeForBlock (blockConnecting.vtx );
2407
+ if (m_mempool) {
2408
+ m_mempool->removeForBlock (blockConnecting.vtx , pindexNew->nHeight );
2409
+ disconnectpool.removeForBlock (blockConnecting.vtx );
2410
+ }
2406
2411
// Update m_chain & related variables.
2407
2412
m_chain.SetTip (pindexNew);
2408
2413
UpdateTip (m_mempool, pindexNew, m_params, *this );
@@ -2495,7 +2500,7 @@ void CChainState::PruneBlockIndexCandidates() {
2495
2500
bool CChainState::ActivateBestChainStep (BlockValidationState& state, CBlockIndex* pindexMostWork, const std::shared_ptr<const CBlock>& pblock, bool & fInvalidFound , ConnectTrace& connectTrace)
2496
2501
{
2497
2502
AssertLockHeld (cs_main);
2498
- AssertLockHeld (m_mempool. cs );
2503
+ if (m_mempool) AssertLockHeld (m_mempool-> cs );
2499
2504
2500
2505
const CBlockIndex* pindexOldTip = m_chain.Tip ();
2501
2506
const CBlockIndex* pindexFork = m_chain.FindFork (pindexMostWork);
@@ -2507,7 +2512,7 @@ bool CChainState::ActivateBestChainStep(BlockValidationState& state, CBlockIndex
2507
2512
if (!DisconnectTip (state, &disconnectpool)) {
2508
2513
// This is likely a fatal error, but keep the mempool consistent,
2509
2514
// just in case. Only remove from the mempool in this case.
2510
- UpdateMempoolForReorg (*this , m_mempool, disconnectpool, false );
2515
+ if (m_mempool) UpdateMempoolForReorg (*this , * m_mempool, disconnectpool, false );
2511
2516
2512
2517
// If we're unable to disconnect a block during normal operation,
2513
2518
// then that is a failure of our local system -- we should abort
@@ -2551,7 +2556,7 @@ bool CChainState::ActivateBestChainStep(BlockValidationState& state, CBlockIndex
2551
2556
// A system error occurred (disk space, database error, ...).
2552
2557
// Make the mempool consistent with the current tip, just in case
2553
2558
// any observers try to use it before shutdown.
2554
- UpdateMempoolForReorg (*this , m_mempool, disconnectpool, false );
2559
+ if (m_mempool) UpdateMempoolForReorg (*this , * m_mempool, disconnectpool, false );
2555
2560
return false ;
2556
2561
}
2557
2562
} else {
@@ -2565,12 +2570,12 @@ bool CChainState::ActivateBestChainStep(BlockValidationState& state, CBlockIndex
2565
2570
}
2566
2571
}
2567
2572
2568
- if (fBlocksDisconnected ) {
2573
+ if (fBlocksDisconnected && m_mempool ) {
2569
2574
// If any blocks were disconnected, disconnectpool may be non empty. Add
2570
2575
// any disconnected transactions back to the mempool.
2571
- UpdateMempoolForReorg (*this , m_mempool, disconnectpool, true );
2576
+ UpdateMempoolForReorg (*this , * m_mempool, disconnectpool, true );
2572
2577
}
2573
- m_mempool. check (*this );
2578
+ if ( m_mempool) m_mempool-> check (*this );
2574
2579
2575
2580
CheckForkWarningConditions ();
2576
2581
@@ -2642,7 +2647,8 @@ bool CChainState::ActivateBestChain(BlockValidationState& state, std::shared_ptr
2642
2647
2643
2648
{
2644
2649
LOCK (cs_main);
2645
- LOCK (m_mempool.cs ); // Lock transaction pool for at least as long as it takes for connectTrace to be consumed
2650
+ // Lock transaction pool for at least as long as it takes for connectTrace to be consumed
2651
+ LOCK (MempoolMutex ());
2646
2652
CBlockIndex* starting_tip = m_chain.Tip ();
2647
2653
bool blocks_connected = false ;
2648
2654
do {
@@ -2792,7 +2798,9 @@ bool CChainState::InvalidateBlock(BlockValidationState& state, CBlockIndex* pind
2792
2798
LimitValidationInterfaceQueue ();
2793
2799
2794
2800
LOCK (cs_main);
2795
- 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
2801
+ // Lock for as long as disconnectpool is in scope to make sure UpdateMempoolForReorg is
2802
+ // called after DisconnectTip without unlocking in between
2803
+ LOCK (MempoolMutex ());
2796
2804
if (!m_chain.Contains (pindex)) break ;
2797
2805
pindex_was_in_chain = true ;
2798
2806
CBlockIndex *invalid_walk_tip = m_chain.Tip ();
@@ -2806,7 +2814,9 @@ bool CChainState::InvalidateBlock(BlockValidationState& state, CBlockIndex* pind
2806
2814
// transactions back to the mempool if disconnecting was successful,
2807
2815
// and we're not doing a very deep invalidation (in which case
2808
2816
// keeping the mempool up to date is probably futile anyway).
2809
- UpdateMempoolForReorg (*this , m_mempool, disconnectpool, /* fAddToMempool = */ (++disconnected <= 10 ) && ret);
2817
+ if (m_mempool) {
2818
+ UpdateMempoolForReorg (*this , *m_mempool, disconnectpool, /* fAddToMempool = */ (++disconnected <= 10 ) && ret);
2819
+ }
2810
2820
if (!ret) return false ;
2811
2821
assert (invalid_walk_tip->pprev == m_chain.Tip ());
2812
2822
@@ -3817,10 +3827,11 @@ bool CChainState::LoadBlockIndexDB()
3817
3827
3818
3828
void CChainState::LoadMempool (const ArgsManager& args)
3819
3829
{
3830
+ if (!m_mempool) return ;
3820
3831
if (args.GetArg (" -persistmempool" , DEFAULT_PERSIST_MEMPOOL)) {
3821
- ::LoadMempool (m_mempool, *this );
3832
+ ::LoadMempool (* m_mempool, *this );
3822
3833
}
3823
- m_mempool. SetIsLoaded (!ShutdownRequested ());
3834
+ m_mempool-> SetIsLoaded (!ShutdownRequested ());
3824
3835
}
3825
3836
3826
3837
bool CChainState::LoadChainTip ()
@@ -4684,7 +4695,8 @@ std::vector<CChainState*> ChainstateManager::GetAll()
4684
4695
return out;
4685
4696
}
4686
4697
4687
- CChainState& ChainstateManager::InitializeChainstate (CTxMemPool& mempool, const std::optional<uint256>& snapshot_blockhash)
4698
+ CChainState& ChainstateManager::InitializeChainstate (
4699
+ CTxMemPool* mempool, const std::optional<uint256>& snapshot_blockhash)
4688
4700
{
4689
4701
bool is_snapshot = snapshot_blockhash.has_value ();
4690
4702
std::unique_ptr<CChainState>& to_modify =
@@ -4763,7 +4775,7 @@ bool ChainstateManager::ActivateSnapshot(
4763
4775
}
4764
4776
4765
4777
auto snapshot_chainstate = WITH_LOCK (::cs_main, return std::make_unique<CChainState>(
4766
- this -> ActiveChainstate (). m_mempool , m_blockman, base_blockhash));
4778
+ /* mempool */ nullptr , m_blockman, base_blockhash));
4767
4779
4768
4780
{
4769
4781
LOCK (::cs_main);
@@ -4879,7 +4891,7 @@ bool ChainstateManager::PopulateAndValidateSnapshot(
4879
4891
}
4880
4892
4881
4893
const auto snapshot_cache_state = WITH_LOCK (::cs_main,
4882
- return snapshot_chainstate.GetCoinsCacheSizeState (& snapshot_chainstate.m_mempool ));
4894
+ return snapshot_chainstate.GetCoinsCacheSizeState (snapshot_chainstate.m_mempool ));
4883
4895
4884
4896
if (snapshot_cache_state >=
4885
4897
CoinsCacheSizeState::CRITICAL) {
0 commit comments