Skip to content

Commit

Permalink
Dont create pcoinsTip until after ReplayBlocks.
Browse files Browse the repository at this point in the history
This requires that we not access pcoinsTip in InitBlockIndex's
FlushStateToDisk (so we just skip it until later in AppInitMain)
and the LoadChainTip in LoadBlockIndex (which there is already one
later in AppinitMain, after ReplayBlocks, so skipping it there is
fine).

Includes some simplifications by Suhas Daftuar and Pieter Wuille.
  • Loading branch information
TheBlueMatt authored and furszy committed Feb 16, 2021
1 parent 93f2b15 commit 8f04970
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 12 deletions.
3 changes: 1 addition & 2 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1567,7 +1567,6 @@ bool AppInitMain()
pblocktree = new CBlockTreeDB(nBlockTreeDBCache, false, fReindex);
pcoinsdbview = new CCoinsViewDB(nCoinDBCache, false, fReindex);
pcoinscatcher = new CCoinsViewErrorCatcher(pcoinsdbview);
pcoinsTip = new CCoinsViewCache(pcoinscatcher);

if (fReindex) {
pblocktree->WriteReindexing(true);
Expand Down Expand Up @@ -1620,7 +1619,7 @@ bool AppInitMain()
strLoadError = _("Unable to replay blocks. You will need to rebuild the database using -reindex-chainstate.");
break;
}
pcoinsTip->SetBestBlock(pcoinsdbview->GetBestBlock()); // TODO: only initialize pcoinsTip after ReplayBlocks
pcoinsTip = new CCoinsViewCache(pcoinscatcher);
LoadChainTip(chainparams);

// Populate list of invalid/fraudulent outpoints that are banned from the chain
Expand Down
8 changes: 1 addition & 7 deletions src/txdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ bool CCoinsViewDB::BatchWrite(CCoinsMap& mapCoins,
size_t changed = 0;
size_t batch_size = (size_t) gArgs.GetArg("-dbbatchsize", nDefaultDbBatchSize);
int crash_simulate = gArgs.GetArg("-dbcrashratio", 0);
assert(!hashBlock.IsNull());

uint256 old_tip = GetBestBlock();
if (old_tip.IsNull()) {
Expand All @@ -107,13 +108,6 @@ bool CCoinsViewDB::BatchWrite(CCoinsMap& mapCoins,
}
}

if (hashBlock.IsNull()) {
// Initial flush, nothing to write.
assert(mapCoins.empty());
assert(old_tip.IsNull());
return true;
}

// In the first batch, mark the database as being in the middle of a
// transition from old_tip to hashBlock.
// A vector is used for future extensibility, as we may want to support
Expand Down
3 changes: 0 additions & 3 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3554,7 +3554,6 @@ bool static LoadBlockIndexDB(std::string& strError)
// If this is written true before the next client init, then we know the shutdown process failed
pblocktree->WriteFlag("shutdown", false);

LoadChainTip(chainparams);
return true;
}

Expand Down Expand Up @@ -3817,8 +3816,6 @@ bool InitBlockIndex()
CBlockIndex* pindex = AddToBlockIndex(block);
if (!ReceivedBlockTransactions(block, state, pindex, blockPos))
return error("LoadBlockIndex() : genesis block not accepted");
// Force a chainstate write so that when we VerifyDB in a moment, it doesnt check stale data
return FlushStateToDisk(state, FLUSH_STATE_ALWAYS);
} catch (const std::runtime_error& e) {
return error("LoadBlockIndex() : failed to initialize block database: %s", e.what());
}
Expand Down

0 comments on commit 8f04970

Please sign in to comment.