Skip to content

Commit

Permalink
refactor: move RewindBlockIndex to CChainState
Browse files Browse the repository at this point in the history
This is in preparation for multiple chainstate initialization in init.
  • Loading branch information
jamesob committed Mar 17, 2020
1 parent 89cdf4d commit 5b690f0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/init.cpp
Expand Up @@ -1614,7 +1614,7 @@ bool AppInitMain(NodeContext& node)
// It both disconnects blocks based on ::ChainActive(), and drops block data in
// BlockIndex() based on lack of available witness data.
uiInterface.InitMessage(_("Rewinding blocks...").translated);
if (!RewindBlockIndex(chainparams)) {
if (!::ChainstateActive().RewindBlockIndex(chainparams)) {
strLoadError = _("Unable to rewind the database to a pre-fork state. You will need to redownload the blockchain").translated;
break;
}
Expand Down
27 changes: 8 additions & 19 deletions src/validation.cpp
Expand Up @@ -4538,26 +4538,15 @@ bool CChainState::RewindBlockIndex(const CChainParams& params)
PruneBlockIndexCandidates();

CheckBlockIndex(params.GetConsensus());
}
}

return true;
}

bool RewindBlockIndex(const CChainParams& params) {
if (!::ChainstateActive().RewindBlockIndex(params)) {
return false;
}

LOCK(cs_main);
if (::ChainActive().Tip() != nullptr) {
// FlushStateToDisk can possibly read ::ChainActive(). Be conservative
// and skip it here, we're about to -reindex-chainstate anyway, so
// it'll get called a bunch real soon.
BlockValidationState state;
if (!::ChainstateActive().FlushStateToDisk(params, state, FlushStateMode::ALWAYS)) {
LogPrintf("RewindBlockIndex: unable to flush state to disk (%s)\n", state.ToString());
return false;
// FlushStateToDisk can possibly read ::ChainActive(). Be conservative
// and skip it here, we're about to -reindex-chainstate anyway, so
// it'll get called a bunch real soon.
BlockValidationState state;
if (!FlushStateToDisk(params, state, FlushStateMode::ALWAYS)) {
LogPrintf("RewindBlockIndex: unable to flush state to disk (%s)\n", state.ToString());
return false;
}
}
}

Expand Down
3 changes: 0 additions & 3 deletions src/validation.h
Expand Up @@ -380,9 +380,6 @@ bool TestBlockValidity(BlockValidationState& state, const CChainParams& chainpar
* Note that transaction witness validation rules are always enforced when P2SH is enforced. */
bool IsWitnessEnabled(const CBlockIndex* pindexPrev, const Consensus::Params& params);

/** When there are blocks in the active chain with missing data, rewind the chainstate and remove them from the block index */
bool RewindBlockIndex(const CChainParams& params) LOCKS_EXCLUDED(cs_main);

/** Compute at which vout of the block's coinbase transaction the witness commitment occurs, or -1 if not found */
int GetWitnessCommitmentIndex(const CBlock& block);

Expand Down

0 comments on commit 5b690f0

Please sign in to comment.