@@ -2310,12 +2310,11 @@ void static UpdateTip(CBlockIndex *pindexNew) {
23102310 }
23112311}
23122312
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. */
23142314bool static DisconnectTip (CValidationState& state, const Consensus::Params& consensusParams)
23152315{
23162316 CBlockIndex *pindexDelete = chainActive.Tip ();
23172317 assert (pindexDelete);
2318- mempool.check (pcoinsTip);
23192318 // Read block from disk.
23202319 CBlock block;
23212320 if (!ReadBlockFromDisk (block, pindexDelete, consensusParams))
@@ -2350,8 +2349,6 @@ bool static DisconnectTip(CValidationState& state, const Consensus::Params& cons
23502349 // UpdateTransactionsFromBlock finds descendants of any transactions in this
23512350 // block that were added back and cleans up the mempool state.
23522351 mempool.UpdateTransactionsFromBlock (vHashUpdate);
2353- mempool.removeForReorg (pcoinsTip, pindexDelete->nHeight );
2354- mempool.check (pcoinsTip);
23552352 // Update chainActive and related variables.
23562353 UpdateTip (pindexDelete->pprev );
23572354 // Let wallets know transactions went from 1-confirmed to
@@ -2375,7 +2372,6 @@ static int64_t nTimePostConnect = 0;
23752372bool static ConnectTip (CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexNew, const CBlock* pblock)
23762373{
23772374 assert (pindexNew->pprev == chainActive.Tip ());
2378- mempool.check (pcoinsTip);
23792375 // Read block from disk.
23802376 int64_t nTime1 = GetTimeMicros ();
23812377 CBlock block;
@@ -2412,7 +2408,6 @@ bool static ConnectTip(CValidationState& state, const CChainParams& chainparams,
24122408 // Remove conflicting transactions from the mempool.
24132409 list<CTransaction> txConflicted;
24142410 mempool.removeForBlock (pblock->vtx , pindexNew->nHeight , txConflicted, !IsInitialBlockDownload ());
2415- mempool.check (pcoinsTip);
24162411 // Update chainActive & related variables.
24172412 UpdateTip (pindexNew);
24182413 // Tell wallet about transactions that went from mempool
@@ -2515,8 +2510,11 @@ static bool ActivateBestChainStep(CValidationState& state, const CChainParams& c
25152510 // Disconnect active blocks which are no longer in the best chain.
25162511 bool fBlocksDisconnected = false ;
25172512 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 );
25192516 return false ;
2517+ }
25202518 fBlocksDisconnected = true ;
25212519 }
25222520
@@ -2550,6 +2548,9 @@ static bool ActivateBestChainStep(CValidationState& state, const CChainParams& c
25502548 break ;
25512549 } else {
25522550 // 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 );
25532554 return false ;
25542555 }
25552556 } else {
@@ -2563,8 +2564,11 @@ static bool ActivateBestChainStep(CValidationState& state, const CChainParams& c
25632564 }
25642565 }
25652566
2566- if (fBlocksDisconnected )
2567+ if (fBlocksDisconnected ) {
2568+ mempool.removeForReorg (pcoinsTip, chainActive.Tip ()->nHeight + 1 );
25672569 mempool.TrimToSize (GetArg (" -maxmempool" , DEFAULT_MAX_MEMPOOL_SIZE) * 1000000 );
2570+ }
2571+ mempool.check (pcoinsTip);
25682572
25692573 // Callbacks/notifications for a new best chain.
25702574 if (fInvalidFound )
@@ -2672,6 +2676,7 @@ bool InvalidateBlock(CValidationState& state, const Consensus::Params& consensus
26722676 // ActivateBestChain considers blocks already in chainActive
26732677 // unconditionally valid already, so force disconnect away from it.
26742678 if (!DisconnectTip (state, consensusParams)) {
2679+ mempool.removeForReorg (pcoinsTip, chainActive.Tip ()->nHeight + 1 );
26752680 return false ;
26762681 }
26772682 }
@@ -2689,6 +2694,7 @@ bool InvalidateBlock(CValidationState& state, const Consensus::Params& consensus
26892694 }
26902695
26912696 InvalidChainFound (pindex);
2697+ mempool.removeForReorg (pcoinsTip, chainActive.Tip ()->nHeight + 1 );
26922698 return true ;
26932699}
26942700
0 commit comments