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
This commit is contained in:
Sergi Delgado Segura 2025-01-28 12:12:36 -05:00
parent c8f5e6234f
commit c7f9061d55
4 changed files with 16 additions and 10 deletions

View file

@ -35,6 +35,9 @@ static constexpr int64_t MAX_FUTURE_BLOCK_TIME = 2 * 60 * 60;
* MAX_FUTURE_BLOCK_TIME. * MAX_FUTURE_BLOCK_TIME.
*/ */
static constexpr int64_t TIMESTAMP_WINDOW = 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 * Maximum gap between node time and block time used
@ -191,9 +194,9 @@ public:
uint32_t nNonce{0}; uint32_t nNonce{0};
//! (memory only) Sequential id assigned to distinguish order in which blocks are received. //! (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 //! Initialized to SEQ_ID_INIT_FROM_DISK{1} when loading blocks from disk, except for blocks
//! which overwrite it to 0. //! belonging to the best chain which overwrite it to SEQ_ID_BEST_CHAIN_FROM_DISK{0}.
int32_t nSequenceId{1}; int32_t nSequenceId{SEQ_ID_INIT_FROM_DISK};
//! (memory only) Maximum nTime in the chain up to and including this block. //! (memory only) Maximum nTime in the chain up to and including this block.
unsigned int nTimeMax{0}; unsigned int nTimeMax{0};

View file

@ -215,7 +215,7 @@ CBlockIndex* BlockManager::AddToBlockIndex(const CBlockHeader& block, CBlockInde
// We assign the sequence id to blocks only when the full data is available, // 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 // to avoid miners withholding blocks but broadcasting headers, to get a
// competitive advantage. // competitive advantage.
pindexNew->nSequenceId = 1; pindexNew->nSequenceId = SEQ_ID_INIT_FROM_DISK;
pindexNew->phashBlock = &((*mi).first); pindexNew->phashBlock = &((*mi).first);
BlockMap::iterator miPrev = m_block_index.find(block.hashPrevBlock); BlockMap::iterator miPrev = m_block_index.find(block.hashPrevBlock);

View file

@ -4724,7 +4724,7 @@ bool Chainstate::LoadChainTip()
// to maintain a consistent best tip over reboots // to maintain a consistent best tip over reboots
auto target = tip; auto target = tip;
while (target) { while (target) {
target->nSequenceId = 0; target->nSequenceId = SEQ_ID_BEST_CHAIN_FROM_DISK;
target = target->pprev; target = target->pprev;
} }
@ -5379,7 +5379,9 @@ void ChainstateManager::CheckBlockIndex()
} }
} }
} }
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). // 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. // HAVE_DATA is only equivalent to nTx > 0 (or VALID_TRANSACTIONS) if no pruning has occurred.
if (!m_blockman.m_have_pruned) { if (!m_blockman.m_have_pruned) {

View file

@ -1021,9 +1021,10 @@ public:
* Every received block is assigned a unique and increasing identifier, so we * Every received block is assigned a unique and increasing identifier, so we
* know which one to give priority in case of a fork. * 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 /** Blocks loaded from disk are assigned id SEQ_ID_INIT_FROM_DISK{1}
* chain loaded from disk), so start the counter at 2. **/ * (SEQ_ID_BEST_CHAIN_FROM_DISK{0} if they belong to the best chain loaded from disk),
int32_t nBlockSequenceId GUARDED_BY(::cs_main) = 2; * 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). */ /** Decreasing counter (used by subsequent preciousblock calls). */
int32_t nBlockReverseSequenceId = -1; int32_t nBlockReverseSequenceId = -1;
/** chainwork for the last block that preciousblock has been applied to. */ /** chainwork for the last block that preciousblock has been applied to. */
@ -1034,7 +1035,7 @@ public:
void ResetBlockSequenceCounters() EXCLUSIVE_LOCKS_REQUIRED(::cs_main) void ResetBlockSequenceCounters() EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
{ {
AssertLockHeld(::cs_main); AssertLockHeld(::cs_main);
nBlockSequenceId = 2; nBlockSequenceId = SEQ_ID_INIT_FROM_DISK + 1;
nBlockReverseSequenceId = -1; nBlockReverseSequenceId = -1;
} }