Skip to content

Commit

Permalink
removeForReorg calls once-per-disconnect-> once-per-reorg
Browse files Browse the repository at this point in the history
Coming from btc@bb8ea1f6304d7ed3f5fe0a01c060ac9f94629349
  • Loading branch information
furszy committed Jul 3, 2020
1 parent 7f5737f commit f35ebe3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
22 changes: 14 additions & 8 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2637,12 +2637,11 @@ void static UpdateTip(CBlockIndex* pindexNew)
}
}

/** Disconnect chainActive's tip. You want to manually re-limit mempool size after this */
/** Disconnect chainActive's tip. You probably want to call mempool.removeForReorg and manually re-limit mempool size after this, with cs_main held. */
bool static DisconnectTip(CValidationState& state)
{
CBlockIndex* pindexDelete = chainActive.Tip();
assert(pindexDelete);
mempool.check(pcoinsTip);
// Read block from disk.
CBlock block;
if (!ReadBlockFromDisk(block, pindexDelete))
Expand Down Expand Up @@ -2677,8 +2676,6 @@ bool static DisconnectTip(CValidationState& state)
// UpdateTransactionsFromBlock finds descendants of any transactions in this
// block that were added back and cleans up the mempool state.
mempool.UpdateTransactionsFromBlock(vHashUpdate);
mempool.removeForReorg(pcoinsTip, pindexDelete->nHeight);
mempool.check(pcoinsTip);
// Update chainActive and related variables.
UpdateTip(pindexDelete->pprev);
// Let wallets know transactions went from 1-confirmed to
Expand All @@ -2702,7 +2699,6 @@ static int64_t nTimePostConnect = 0;
bool static ConnectTip(CValidationState& state, CBlockIndex* pindexNew, const CBlock* pblock, bool fAlreadyChecked)
{
assert(pindexNew->pprev == chainActive.Tip());
mempool.check(pcoinsTip);

if (pblock == NULL)
fAlreadyChecked = false;
Expand Down Expand Up @@ -2752,7 +2748,6 @@ bool static ConnectTip(CValidationState& state, CBlockIndex* pindexNew, const CB
// Remove conflicting transactions from the mempool.
std::list<CTransaction> txConflicted;
mempool.removeForBlock(pblock->vtx, pindexNew->nHeight, txConflicted, !IsInitialBlockDownload());
mempool.check(pcoinsTip);
// Update chainActive & related variables.
UpdateTip(pindexNew);
// Tell wallet about transactions that went from mempool
Expand Down Expand Up @@ -2976,8 +2971,11 @@ static bool ActivateBestChainStep(CValidationState& state, CBlockIndex* pindexMo
// Disconnect active blocks which are no longer in the best chain.
bool fBlocksDisconnected = false;
while (chainActive.Tip() && chainActive.Tip() != pindexFork) {
if (!DisconnectTip(state))
if (!DisconnectTip(state)) {
// Probably an AbortNode() error, but try to keep mempool consistent anyway
mempool.removeForReorg(pcoinsTip, chainActive.Tip()->nHeight + 1);
return false;
}
fBlocksDisconnected = true;
}

Expand Down Expand Up @@ -3011,6 +3009,9 @@ static bool ActivateBestChainStep(CValidationState& state, CBlockIndex* pindexMo
break;
} else {
// A system error occurred (disk space, database error, ...).
// Probably gonna shut down ASAP, but try to keep mempool consistent anyway
if (fBlocksDisconnected)
mempool.removeForReorg(pcoinsTip, chainActive.Tip()->nHeight + 1);
return false;
}
} else {
Expand All @@ -3024,8 +3025,11 @@ static bool ActivateBestChainStep(CValidationState& state, CBlockIndex* pindexMo
}
}

if (fBlocksDisconnected)
if (fBlocksDisconnected) {
mempool.removeForReorg(pcoinsTip, chainActive.Tip()->nHeight + 1);
mempool.TrimToSize(GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000);
}
mempool.check(pcoinsTip);

// Callbacks/notifications for a new best chain.
if (fInvalidFound)
Expand Down Expand Up @@ -3140,6 +3144,7 @@ bool InvalidateBlock(CValidationState& state, CBlockIndex* pindex)
// ActivateBestChain considers blocks already in chainActive
// unconditionally valid already, so force disconnect away from it.
if (!DisconnectTip(state)) {
mempool.removeForReorg(pcoinsTip, chainActive.Tip()->nHeight + 1);
return false;
}
}
Expand All @@ -3157,6 +3162,7 @@ bool InvalidateBlock(CValidationState& state, CBlockIndex* pindex)
}

InvalidChainFound(pindex);
mempool.removeForReorg(pcoinsTip, chainActive.Tip()->nHeight + 1);
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ bool InvalidateBlock(CValidationState& state, CBlockIndex* pindex);
/** Remove invalidity status from a block and its descendants. */
bool ReconsiderBlock(CValidationState& state, CBlockIndex* pindex);

/** The currently-connected chain of blocks. */
/** The currently-connected chain of blocks (protected by cs_main). */
extern CChain chainActive;

/** Global variable that points to the active CCoinsView (protected by cs_main) */
Expand Down

0 comments on commit f35ebe3

Please sign in to comment.