diff --git a/src/validation.h b/src/validation.h --- a/src/validation.h +++ b/src/validation.h @@ -421,7 +421,8 @@ * Guess verification progress (as a fraction between 0.0=genesis and * 1.0=current tip). */ -double GuessVerificationProgress(const ChainTxData &data, CBlockIndex *pindex); +double GuessVerificationProgress(const ChainTxData &data, + const CBlockIndex *pindex); /** * Mark one block file as pruned. diff --git a/src/validation.cpp b/src/validation.cpp --- a/src/validation.cpp +++ b/src/validation.cpp @@ -83,6 +83,40 @@ */ std::set setBlockIndexCandidates; + /** + * Every received block is assigned a unique and increasing identifier, so + * we + * know which one to give priority in case of a fork. + */ + CCriticalSection cs_nBlockSequenceId; + /** Blocks loaded from disk are assigned id 0, so start the counter at 1. */ + int32_t nBlockSequenceId = 1; + /** Decreasing counter (used by subsequent preciousblock calls). */ + int32_t nBlockReverseSequenceId = -1; + /** chainwork for the last block that preciousblock has been applied to. */ + arith_uint256 nLastPreciousChainwork = 0; + + /** In order to efficiently track invalidity of headers, we keep the set of + * blocks which we tried to connect and found to be invalid here (ie which + * were set to BLOCK_FAILED_VALID since the last restart). We can then + * walk this set and check if a new header is a descendant of something in + * this set, preventing us from having to walk mapBlockIndex when we try + * to connect a bad block and fail. + * + * While this is more complicated than marking everything which descends + * from an invalid block as invalid at the time we discover it to be + * invalid, doing so would require walking all of mapBlockIndex to find all + * descendants. Since this case should be very rare, keeping track of all + * BLOCK_FAILED_VALID blocks in a set should be just fine and work just as + * well. + * + * Because we already walk mapBlockIndex in height-order at startup, we go + * ahead and mark descendants of invalid blocks as FAILED_CHILD at that + * time, + * instead of putting things in this set. + */ + std::set g_failed_blocks; + public: CChain chainActive; BlockMap mapBlockIndex; @@ -221,38 +255,6 @@ */ bool fCheckForPruning = false; -/** - * Every received block is assigned a unique and increasing identifier, so we - * know which one to give priority in case of a fork. - */ -CCriticalSection cs_nBlockSequenceId; -/** Blocks loaded from disk are assigned id 0, so start the counter at 1. */ -int32_t nBlockSequenceId = 1; -/** Decreasing counter (used by subsequent preciousblock calls). */ -int32_t nBlockReverseSequenceId = -1; -/** chainwork for the last block that preciousblock has been applied to. */ -arith_uint256 nLastPreciousChainwork = 0; - -/** In order to efficiently track invalidity of headers, we keep the set of - * blocks which we tried to connect and found to be invalid here (ie which - * were set to BLOCK_FAILED_VALID since the last restart). We can then - * walk this set and check if a new header is a descendant of something in - * this set, preventing us from having to walk mapBlockIndex when we try - * to connect a bad block and fail. - * - * While this is more complicated than marking everything which descends - * from an invalid block as invalid at the time we discover it to be - * invalid, doing so would require walking all of mapBlockIndex to find all - * descendants. Since this case should be very rare, keeping track of all - * BLOCK_FAILED_VALID blocks in a set should be just fine and work just as - * well. - * - * Because we already walk mapBlockIndex in height-order at startup, we go - * ahead and mark descendants of invalid blocks as FAILED_CHILD at that time, - * instead of putting things in this set. - */ -std::set g_failed_blocks; - /** Dirty block index entries. */ std::set setDirtyBlockIndex; @@ -4720,6 +4722,8 @@ // May NOT be used after any connections are up as much of the peer-processing // logic assumes a consistent block index state void CChainState::UnloadBlockIndex() { + nBlockSequenceId = 1; + g_failed_blocks.clear(); setBlockIndexCandidates.clear(); } @@ -4736,7 +4740,6 @@ mapBlocksUnlinked.clear(); vinfoBlockFile.clear(); nLastBlockFile = 0; - nBlockSequenceId = 1; setDirtyBlockIndex.clear(); setDirtyFileInfo.clear(); versionbitscache.Clear(); @@ -5436,7 +5439,8 @@ } //! Guess how far we are in the verification process at the given block index -double GuessVerificationProgress(const ChainTxData &data, CBlockIndex *pindex) { +double GuessVerificationProgress(const ChainTxData &data, + const CBlockIndex *pindex) { if (pindex == nullptr) { return 0.0; }