From 18524b072e6bdd590a9f6badd15d897b5ef5ce54 Mon Sep 17 00:00:00 2001 From: Sergi Delgado Segura Date: Tue, 28 Jan 2025 12:12:36 -0500 Subject: [PATCH] Make nSequenceId init value constants Make it easier to follow what the values come without having to go over the comments, plus easier to maintain --- src/chain.h | 9 ++++++--- src/node/blockstorage.cpp | 2 +- src/validation.cpp | 6 ++++-- src/validation.h | 9 +++++---- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/chain.h b/src/chain.h index d348fefe2a2..8aa1ebb546a 100644 --- a/src/chain.h +++ b/src/chain.h @@ -35,6 +35,9 @@ static constexpr int64_t MAX_FUTURE_BLOCK_TIME = 2 * 60 * 60; * MAX_FUTURE_BLOCK_TIME. */ static constexpr int64_t TIMESTAMP_WINDOW = MAX_FUTURE_BLOCK_TIME; +//! Init values for CBlockIndex nSequenceId when loaded from disk +static constexpr int32_t SEQ_ID_BEST_CHAIN_FROM_DISK = 0; +static constexpr int32_t SEQ_ID_INIT_FROM_DISK = 1; /** * Maximum gap between node time and block time used @@ -191,9 +194,9 @@ public: uint32_t nNonce{0}; //! (memory only) Sequential id assigned to distinguish order in which blocks are received. - //! Initialized to 1 when loading blocks from disk, except for blocks belonging to the best chain - //! which overwrite it to 0. - int32_t nSequenceId{1}; + //! Initialized to SEQ_ID_INIT_FROM_DISK{1} when loading blocks from disk, except for blocks + //! belonging to the best chain which overwrite it to SEQ_ID_BEST_CHAIN_FROM_DISK{0}. + int32_t nSequenceId{SEQ_ID_INIT_FROM_DISK}; //! (memory only) Maximum nTime in the chain up to and including this block. unsigned int nTimeMax{0}; diff --git a/src/node/blockstorage.cpp b/src/node/blockstorage.cpp index ac8839854b8..e1b54175dc2 100644 --- a/src/node/blockstorage.cpp +++ b/src/node/blockstorage.cpp @@ -218,7 +218,7 @@ CBlockIndex* BlockManager::AddToBlockIndex(const CBlockHeader& block, CBlockInde // We assign the sequence id to blocks only when the full data is available, // to avoid miners withholding blocks but broadcasting headers, to get a // competitive advantage. - pindexNew->nSequenceId = 1; + pindexNew->nSequenceId = SEQ_ID_INIT_FROM_DISK; pindexNew->phashBlock = &((*mi).first); BlockMap::iterator miPrev = m_block_index.find(block.hashPrevBlock); diff --git a/src/validation.cpp b/src/validation.cpp index f9a50d84e6b..c6137ddb137 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -4698,7 +4698,7 @@ bool Chainstate::LoadChainTip() // to maintain a consistent best tip over reboots in case of a tie. auto target = tip; while (target) { - target->nSequenceId = 0; + target->nSequenceId = SEQ_ID_BEST_CHAIN_FROM_DISK; target = target->pprev; } @@ -5357,7 +5357,9 @@ void ChainstateManager::CheckBlockIndex() const } } } - if (!pindex->HaveNumChainTxs()) assert(pindex->nSequenceId <= 1); // nSequenceId can't be set higher than 1 for blocks that aren't linked (negative is used for preciousblock, 0 for active chain) + // nSequenceId can't be set higher than SEQ_ID_INIT_FROM_DISK{1} for blocks that aren't linked + // (negative is used for preciousblock, SEQ_ID_BEST_CHAIN_FROM_DISK{0} for active chain when loaded from disk) + if (!pindex->HaveNumChainTxs()) assert(pindex->nSequenceId <= SEQ_ID_INIT_FROM_DISK); // VALID_TRANSACTIONS is equivalent to nTx > 0 for all nodes (whether or not pruning has occurred). // HAVE_DATA is only equivalent to nTx > 0 (or VALID_TRANSACTIONS) if no pruning has occurred. if (!m_blockman.m_have_pruned) { diff --git a/src/validation.h b/src/validation.h index de3711b5c8c..70c20ac135c 100644 --- a/src/validation.h +++ b/src/validation.h @@ -1034,9 +1034,10 @@ public: * Every received block is assigned a unique and increasing identifier, so we * know which one to give priority in case of a fork. */ - /** Blocks loaded from disk are assigned id 1 (0 if they belong to the best - * chain loaded from disk), so start the counter at 2. **/ - int32_t nBlockSequenceId GUARDED_BY(::cs_main) = 2; + /** Blocks loaded from disk are assigned id SEQ_ID_INIT_FROM_DISK{1} + * (SEQ_ID_BEST_CHAIN_FROM_DISK{0} if they belong to the best chain loaded from disk), + * so start the counter after that. **/ + int32_t nBlockSequenceId GUARDED_BY(::cs_main) = SEQ_ID_INIT_FROM_DISK + 1; /** Decreasing counter (used by subsequent preciousblock calls). */ int32_t nBlockReverseSequenceId = -1; /** chainwork for the last block that preciousblock has been applied to. */ @@ -1047,7 +1048,7 @@ public: void ResetBlockSequenceCounters() EXCLUSIVE_LOCKS_REQUIRED(::cs_main) { AssertLockHeld(::cs_main); - nBlockSequenceId = 2; + nBlockSequenceId = SEQ_ID_INIT_FROM_DISK + 1; nBlockReverseSequenceId = -1; }