@@ -2310,12 +2310,11 @@ void static UpdateTip(CBlockIndex *pindexNew) {
2310
2310
}
2311
2311
}
2312
2312
2313
- /* * Disconnect chainActive's tip. You want to manually re-limit mempool size after this */
2313
+ /* * Disconnect chainActive's tip. You probably want to call mempool.removeForReorg and manually re-limit mempool size after this, with cs_main held. */
2314
2314
bool static DisconnectTip (CValidationState& state, const Consensus::Params& consensusParams)
2315
2315
{
2316
2316
CBlockIndex *pindexDelete = chainActive.Tip ();
2317
2317
assert (pindexDelete);
2318
- mempool.check (pcoinsTip);
2319
2318
// Read block from disk.
2320
2319
CBlock block;
2321
2320
if (!ReadBlockFromDisk (block, pindexDelete, consensusParams))
@@ -2350,8 +2349,6 @@ bool static DisconnectTip(CValidationState& state, const Consensus::Params& cons
2350
2349
// UpdateTransactionsFromBlock finds descendants of any transactions in this
2351
2350
// block that were added back and cleans up the mempool state.
2352
2351
mempool.UpdateTransactionsFromBlock (vHashUpdate);
2353
- mempool.removeForReorg (pcoinsTip, pindexDelete->nHeight );
2354
- mempool.check (pcoinsTip);
2355
2352
// Update chainActive and related variables.
2356
2353
UpdateTip (pindexDelete->pprev );
2357
2354
// Let wallets know transactions went from 1-confirmed to
@@ -2375,7 +2372,6 @@ static int64_t nTimePostConnect = 0;
2375
2372
bool static ConnectTip (CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexNew, const CBlock* pblock)
2376
2373
{
2377
2374
assert (pindexNew->pprev == chainActive.Tip ());
2378
- mempool.check (pcoinsTip);
2379
2375
// Read block from disk.
2380
2376
int64_t nTime1 = GetTimeMicros ();
2381
2377
CBlock block;
@@ -2412,7 +2408,6 @@ bool static ConnectTip(CValidationState& state, const CChainParams& chainparams,
2412
2408
// Remove conflicting transactions from the mempool.
2413
2409
list<CTransaction> txConflicted;
2414
2410
mempool.removeForBlock (pblock->vtx , pindexNew->nHeight , txConflicted, !IsInitialBlockDownload ());
2415
- mempool.check (pcoinsTip);
2416
2411
// Update chainActive & related variables.
2417
2412
UpdateTip (pindexNew);
2418
2413
// Tell wallet about transactions that went from mempool
@@ -2515,8 +2510,11 @@ static bool ActivateBestChainStep(CValidationState& state, const CChainParams& c
2515
2510
// Disconnect active blocks which are no longer in the best chain.
2516
2511
bool fBlocksDisconnected = false ;
2517
2512
while (chainActive.Tip () && chainActive.Tip () != pindexFork) {
2518
- if (!DisconnectTip (state, chainparams.GetConsensus ()))
2513
+ if (!DisconnectTip (state, chainparams.GetConsensus ())) {
2514
+ // Probably an AbortNode() error, but try to keep mempool consistent anyway
2515
+ mempool.removeForReorg (pcoinsTip, chainActive.Tip ()->nHeight + 1 );
2519
2516
return false ;
2517
+ }
2520
2518
fBlocksDisconnected = true ;
2521
2519
}
2522
2520
@@ -2550,6 +2548,9 @@ static bool ActivateBestChainStep(CValidationState& state, const CChainParams& c
2550
2548
break ;
2551
2549
} else {
2552
2550
// A system error occurred (disk space, database error, ...).
2551
+ // Probably gonna shut down ASAP, but try to keep mempool consistent anyway
2552
+ if (fBlocksDisconnected )
2553
+ mempool.removeForReorg (pcoinsTip, chainActive.Tip ()->nHeight + 1 );
2553
2554
return false ;
2554
2555
}
2555
2556
} else {
@@ -2563,8 +2564,11 @@ static bool ActivateBestChainStep(CValidationState& state, const CChainParams& c
2563
2564
}
2564
2565
}
2565
2566
2566
- if (fBlocksDisconnected )
2567
+ if (fBlocksDisconnected ) {
2568
+ mempool.removeForReorg (pcoinsTip, chainActive.Tip ()->nHeight + 1 );
2567
2569
mempool.TrimToSize (GetArg (" -maxmempool" , DEFAULT_MAX_MEMPOOL_SIZE) * 1000000 );
2570
+ }
2571
+ mempool.check (pcoinsTip);
2568
2572
2569
2573
// Callbacks/notifications for a new best chain.
2570
2574
if (fInvalidFound )
@@ -2672,6 +2676,7 @@ bool InvalidateBlock(CValidationState& state, const Consensus::Params& consensus
2672
2676
// ActivateBestChain considers blocks already in chainActive
2673
2677
// unconditionally valid already, so force disconnect away from it.
2674
2678
if (!DisconnectTip (state, consensusParams)) {
2679
+ mempool.removeForReorg (pcoinsTip, chainActive.Tip ()->nHeight + 1 );
2675
2680
return false ;
2676
2681
}
2677
2682
}
@@ -2689,6 +2694,7 @@ bool InvalidateBlock(CValidationState& state, const Consensus::Params& consensus
2689
2694
}
2690
2695
2691
2696
InvalidChainFound (pindex);
2697
+ mempool.removeForReorg (pcoinsTip, chainActive.Tip ()->nHeight + 1 );
2692
2698
return true ;
2693
2699
}
2694
2700
0 commit comments