Skip to content

Commit ff3a219

Browse files
committed
Call RewindBlockIndex even if we're about to run -reindex-chainstate
RewindBlockIndex works over both chainActive - disconnecting blocks from the tip that need witness verification - and mapBlockIndex - requiring redownload of blocks missing witness data. It should never have been the case that the second half is skipped if we're about to run -reindex-chainstate.
1 parent b0f3249 commit ff3a219

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/init.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1452,7 +1452,10 @@ bool AppInitMain(boost::thread_group& threadGroup, CScheduler& scheduler)
14521452
assert(chainActive.Tip() != NULL);
14531453
}
14541454

1455-
if (!fReindex && chainActive.Tip() != NULL) {
1455+
if (!fReindex) {
1456+
// Note that RewindBlockIndex MUST run even if we're about to -reindex-chainstate.
1457+
// It both disconnects blocks based on chainActive, and drops block data in
1458+
// mapBlockIndex based on lack of available witness data.
14561459
uiInterface.InitMessage(_("Rewinding blocks..."));
14571460
if (!RewindBlockIndex(chainparams)) {
14581461
strLoadError = _("Unable to rewind the database to a pre-fork state. You will need to redownload the blockchain");

src/validation.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3763,6 +3763,8 @@ bool RewindBlockIndex(const CChainParams& params)
37633763
{
37643764
LOCK(cs_main);
37653765

3766+
// Note that during -reindex-chainstate we are called with an empty chainActive!
3767+
37663768
int nHeight = 1;
37673769
while (nHeight <= chainActive.Height()) {
37683770
if (IsWitnessEnabled(chainActive[nHeight - 1], params.GetConsensus()) && !(chainActive[nHeight]->nStatus & BLOCK_OPT_WITNESS)) {
@@ -3832,12 +3834,19 @@ bool RewindBlockIndex(const CChainParams& params)
38323834
}
38333835
}
38343836

3835-
PruneBlockIndexCandidates();
3837+
if (chainActive.Tip() != NULL) {
3838+
// We can't prune block index candidates based on our tip if we have
3839+
// no tip due to chainActive being empty!
3840+
PruneBlockIndexCandidates();
38363841

3837-
CheckBlockIndex(params.GetConsensus());
3842+
CheckBlockIndex(params.GetConsensus());
38383843

3839-
if (!FlushStateToDisk(params, state, FLUSH_STATE_ALWAYS)) {
3840-
return false;
3844+
// FlushStateToDisk can possibly read chainActive. Be conservative
3845+
// and skip it here, we're about to -reindex-chainstate anyway, so
3846+
// it'll get called a bunch real soon.
3847+
if (!FlushStateToDisk(params, state, FLUSH_STATE_ALWAYS)) {
3848+
return false;
3849+
}
38413850
}
38423851

38433852
return true;

0 commit comments

Comments
 (0)