Skip to content

Commit d1e3c5e

Browse files
committed
Merge #13012: [doc] Add comments for chainparams.h, validation.cpp
18326ae [doc] Add comments for chainparams.h, validation.cpp (James O'Beirne) Pull request description: Added a few comments during a leisurely read through some of the validation code. If this kind of thing seems useful, I can add similar documentation for most of the `CChainState` interface. Tree-SHA512: a4d9db60383a8ff02e74ac326ed88902eec1ee441e8cd4e1845bcf257072673c15974225288cebf0a633e76a3410f99e2206616b4694725a2a5b0d19c78327d6
2 parents 5713994 + 18326ae commit d1e3c5e

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/chainparams.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ struct CCheckpointData {
2525
MapCheckpoints mapCheckpoints;
2626
};
2727

28+
/**
29+
* Holds various statistics on transactions within a chain. Used to estimate
30+
* verification progress during chain sync.
31+
*
32+
* See also: CChainParams::TxData, GuessVerificationProgress.
33+
*/
2834
struct ChainTxData {
2935
int64_t nTime;
3036
int64_t nTxCount;

src/validation.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ class CChainState {
154154

155155
bool ActivateBestChain(CValidationState &state, const CChainParams& chainparams, std::shared_ptr<const CBlock> pblock);
156156

157+
/**
158+
* If a block header hasn't already been seen, call CheckBlockHeader on it, ensure
159+
* that it doesn't descend from an invalid block, and then add it to mapBlockIndex.
160+
*/
157161
bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex);
158162
bool AcceptBlock(const std::shared_ptr<const CBlock>& pblock, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex, bool fRequested, const CDiskBlockPos* dbp, bool* fNewBlock);
159163

@@ -185,6 +189,11 @@ class CChainState {
185189
CBlockIndex* AddToBlockIndex(const CBlockHeader& block);
186190
/** Create a new block index entry for a given block hash */
187191
CBlockIndex * InsertBlockIndex(const uint256& hash);
192+
/**
193+
* Make various assertions about the state of the block index.
194+
*
195+
* By default this only executes fully when using the Regtest chain; see: fCheckBlockIndex.
196+
*/
188197
void CheckBlockIndex(const Consensus::Params& consensusParams);
189198

190199
void InvalidBlockFound(CBlockIndex *pindex, const CValidationState &state);
@@ -2654,6 +2663,10 @@ static void NotifyHeaderTip() {
26542663
* Make the best chain active, in multiple steps. The result is either failure
26552664
* or an activated best chain. pblock is either nullptr or a pointer to a block
26562665
* that is already loaded (to avoid loading it again from disk).
2666+
*
2667+
* ActivateBestChain is split into steps (see ActivateBestChainStep) so that
2668+
* we avoid holding cs_main for an extended period of time; the length of this
2669+
* call may be quite long during reindexing or a substantial reorg.
26572670
*/
26582671
bool CChainState::ActivateBestChain(CValidationState &state, const CChainParams& chainparams, std::shared_ptr<const CBlock> pblock) {
26592672
// Note that while we're often called here from ProcessNewBlock, this is
@@ -3357,6 +3370,9 @@ bool CChainState::AcceptBlockHeader(const CBlockHeader& block, CValidationState&
33573370
if (!ContextualCheckBlockHeader(block, state, chainparams, pindexPrev, GetAdjustedTime()))
33583371
return error("%s: Consensus::ContextualCheckBlockHeader: %s, %s", __func__, hash.ToString(), FormatStateMessage(state));
33593372

3373+
// If the previous block index isn't valid, determine if it descends from any block which
3374+
// has been found invalid (g_failed_blocks), then mark pindexPrev and any blocks
3375+
// between them as failed.
33603376
if (!pindexPrev->IsValid(BLOCK_VALID_SCRIPTS)) {
33613377
for (const CBlockIndex* failedit : m_failed_blocks) {
33623378
if (pindexPrev->GetAncestor(failedit->nHeight) == failedit) {

0 commit comments

Comments
 (0)