0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-09 10:43:19 -05:00

Merge the disconnection and erasing loops in RewindBlockIndex

This commit is contained in:
Pieter Wuille 2019-02-24 12:54:53 -08:00
parent 32b2696ab4
commit 1d342875c2

View file

@ -4234,10 +4234,13 @@ bool CChainState::RewindBlockIndex(const CChainParams& params)
// nHeight is now the height of the first insufficiently-validated block, or tipheight + 1 // nHeight is now the height of the first insufficiently-validated block, or tipheight + 1
CValidationState state; CValidationState state;
CBlockIndex* pindex = chainActive.Tip(); CBlockIndex* tip = chainActive.Tip();
CBlockIndex* tip = pindex; // Loop until the tip is below nHeight, or we reach a pruned block.
while (chainActive.Height() >= nHeight) { while (true) {
if (fPruneMode && !(chainActive.Tip()->nStatus & BLOCK_HAVE_DATA)) { // Make sure nothing changed from under us (this won't happen because RewindBlockIndex runs before importing/network are active)
assert(tip == chainActive.Tip());
if (tip == nullptr || tip->nHeight < nHeight) break;
if (fPruneMode && !(tip->nStatus & BLOCK_HAVE_DATA)) {
// If pruning, don't try rewinding past the HAVE_DATA point; // If pruning, don't try rewinding past the HAVE_DATA point;
// since older blocks can't be served anyway, there's // since older blocks can't be served anyway, there's
// no need to walk further, and trying to DisconnectTip() // no need to walk further, and trying to DisconnectTip()
@ -4245,29 +4248,29 @@ bool CChainState::RewindBlockIndex(const CChainParams& params)
// of the blockchain). // of the blockchain).
break; break;
} }
// Disconnect block
if (!DisconnectTip(state, params, nullptr)) { if (!DisconnectTip(state, params, nullptr)) {
return error("RewindBlockIndex: unable to disconnect block at height %i (%s)", pindex->nHeight, FormatStateMessage(state)); return error("RewindBlockIndex: unable to disconnect block at height %i (%s)", tip->nHeight, FormatStateMessage(state));
}
// Occasionally flush state to disk.
if (!FlushStateToDisk(params, state, FlushStateMode::PERIODIC)) {
LogPrintf("RewindBlockIndex: unable to flush state to disk (%s)\n", FormatStateMessage(state));
return false;
}
} }
// Reduce validity flag and have-data flags. // Reduce validity flag and have-data flags.
// We do this after actual disconnecting, otherwise we'll end up writing the lack of data // We do this after actual disconnecting, otherwise we'll end up writing the lack of data
// to disk before writing the chainstate, resulting in a failure to continue if interrupted. // to disk before writing the chainstate, resulting in a failure to continue if interrupted.
while (tip->nHeight > chainActive.Height()) {
// Note: If we encounter an insufficiently validated block that // Note: If we encounter an insufficiently validated block that
// is on chainActive, it must be because we are a pruning node, and // is on chainActive, it must be because we are a pruning node, and
// this block or some successor doesn't HAVE_DATA, so we were unable to // this block or some successor doesn't HAVE_DATA, so we were unable to
// rewind all the way. Blocks remaining on chainActive at this point // rewind all the way. Blocks remaining on chainActive at this point
// must not have their validity reduced. // must not have their validity reduced.
if (IsWitnessEnabled(tip->pprev, params.GetConsensus()) && !(tip->nStatus & BLOCK_OPT_WITNESS)) {
EraseBlockData(tip); EraseBlockData(tip);
}
tip = tip->pprev; tip = tip->pprev;
// Occasionally flush state to disk.
if (!FlushStateToDisk(params, state, FlushStateMode::PERIODIC)) {
LogPrintf("RewindBlockIndex: unable to flush state to disk (%s)\n", FormatStateMessage(state));
return false;
}
} }
if (chainActive.Tip() != nullptr) { if (chainActive.Tip() != nullptr) {