diff --git a/src/chain.h b/src/chain.h --- a/src/chain.h +++ b/src/chain.h @@ -130,17 +130,23 @@ } }; -enum BlockStatus : uint32_t { - //! Unused. - BLOCK_VALID_UNKNOWN = 0, +enum class BlockValidity : uint32_t { + /** + * Unused. + */ + UNKNOWN = 0, - //! Parsed, version ok, hash satisfies claimed PoW, 1 <= vtx count <= max, - //! timestamp not in future - BLOCK_VALID_HEADER = 1, + /** + * Parsed, version ok, hash satisfies claimed PoW, 1 <= vtx count <= max, + * timestamp not in future. + */ + HEADER = 1, - //! All parent headers found, difficulty matches, timestamp >= median - //! previous, checkpoint. Implies all parents are also at least TREE. - BLOCK_VALID_TREE = 2, + /** + * All parent headers found, difficulty matches, timestamp >= median + * previous, checkpoint. Implies all parents are also at least TREE. + */ + TREE = 2, /** * Only first tx is coinbase, 2 <= coinbase input script length <= 100, @@ -149,30 +155,48 @@ * When all parent blocks also have TRANSACTIONS, CBlockIndex::nChainTx will * be set. */ - BLOCK_VALID_TRANSACTIONS = 3, + TRANSACTIONS = 3, - //! Outputs do not overspend inputs, no double spends, coinbase output ok, - //! no immature coinbase spends, BIP30. - //! Implies all parents are also at least CHAIN. - BLOCK_VALID_CHAIN = 4, + /** + * Outputs do not overspend inputs, no double spends, coinbase output ok, no + * immature coinbase spends, BIP30. + * Implies all parents are also at least CHAIN. + */ + CHAIN = 4, + + /** + * Scripts & signatures ok. Implies all parents are also at least SCRIPTS. + */ + SCRIPTS = 5, +}; - //! Scripts & signatures ok. Implies all parents are also at least SCRIPTS. - BLOCK_VALID_SCRIPTS = 5, +enum BlockStatusEnum : uint32_t { + /** + * Validity levels. + */ + BLOCK_VALID_UNKNOWN = uint32_t(BlockValidity::UNKNOWN), + BLOCK_VALID_HEADER = uint32_t(BlockValidity::HEADER), + BLOCK_VALID_TREE = uint32_t(BlockValidity::TREE), + BLOCK_VALID_TRANSACTIONS = uint32_t(BlockValidity::TRANSACTIONS), + BLOCK_VALID_CHAIN = uint32_t(BlockValidity::CHAIN), + BLOCK_VALID_SCRIPTS = uint32_t(BlockValidity::SCRIPTS), - //! All validity bits. + /** + * All validity bits. + */ BLOCK_VALID_MASK = BLOCK_VALID_HEADER | BLOCK_VALID_TREE | BLOCK_VALID_TRANSACTIONS | BLOCK_VALID_CHAIN | BLOCK_VALID_SCRIPTS, - //!< full block available in blk*.dat + // Full block available in blk*.dat BLOCK_HAVE_DATA = 8, - //!< undo data available in rev*.dat + // Undo data available in rev*.dat BLOCK_HAVE_UNDO = 16, BLOCK_HAVE_MASK = BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO, - //!< stage after last reached validness failed + // The block is invalid. BLOCK_FAILED_VALID = 32, - //!< descends from failed block + // The block has an invalid parent. BLOCK_FAILED_CHILD = 64, BLOCK_FAILED_MASK = BLOCK_FAILED_VALID | BLOCK_FAILED_CHILD, }; @@ -347,28 +371,29 @@ //! Check whether this block index entry is valid up to the passed validity //! level. - bool IsValid(enum BlockStatus nUpTo = BLOCK_VALID_TRANSACTIONS) const { + bool IsValid(enum BlockValidity nUpTo = BlockValidity::TRANSACTIONS) const { // Only validity flags allowed. - assert(!(nUpTo & ~BLOCK_VALID_MASK)); if (nStatus & BLOCK_FAILED_MASK) { return false; } - return ((nStatus & BLOCK_VALID_MASK) >= nUpTo); + + return BlockValidity(nStatus & BLOCK_VALID_MASK) >= nUpTo; } //! Raise the validity level of this block index entry. //! Returns true if the validity was changed. - bool RaiseValidity(enum BlockStatus nUpTo) { + bool RaiseValidity(enum BlockValidity nUpTo) { // Only validity flags allowed. - assert(!(nUpTo & ~BLOCK_VALID_MASK)); if (nStatus & BLOCK_FAILED_MASK) { return false; } - if ((nStatus & BLOCK_VALID_MASK) < nUpTo) { - nStatus = (nStatus & ~BLOCK_VALID_MASK) | nUpTo; - return true; + + if (BlockValidity(nStatus & BLOCK_VALID_MASK) >= nUpTo) { + return false; } - return false; + + nStatus = (nStatus & ~BLOCK_VALID_MASK) | uint32_t(nUpTo); + return true; } //! Build the skiplist pointer for this entry. diff --git a/src/net_processing.cpp b/src/net_processing.cpp --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -100,8 +100,10 @@ std::unique_ptr recentRejects; uint256 hashRecentRejectsChainTip; -/** Blocks that are in flight, and that are in the queue to be downloaded. - * Protected by cs_main. */ +/** + * Blocks that are in flight, and that are in the queue to be downloaded. + * Protected by cs_main. + */ struct QueuedBlock { uint256 hash; //!< Optional. @@ -617,7 +619,7 @@ // are already downloaded, or if it's already part of our chain (and // therefore don't need it even if pruned). for (const CBlockIndex *pindex : vToFetch) { - if (!pindex->IsValid(BLOCK_VALID_TREE)) { + if (!pindex->IsValid(BlockValidity::TREE)) { // We consider the chain that this peer is on invalid. return; } @@ -1146,8 +1148,8 @@ BlockMap::iterator mi = mapBlockIndex.find(inv.hash); if (mi != mapBlockIndex.end()) { if (mi->second->nChainTx && - !mi->second->IsValid(BLOCK_VALID_SCRIPTS) && - mi->second->IsValid(BLOCK_VALID_TREE)) { + !mi->second->IsValid(BlockValidity::SCRIPTS) && + mi->second->IsValid(BlockValidity::TREE)) { // If we have the block and all of its parents, but have // not yet validated it, we might be in the middle of // connecting it (ie in the unlock of cs_main before @@ -1171,7 +1173,7 @@ // more than a month older (both in time, and in best // equivalent proof of work) than the best header chain // we know about. - send = mi->second->IsValid(BLOCK_VALID_SCRIPTS) && + send = mi->second->IsValid(BlockValidity::SCRIPTS) && (pindexBestHeader != nullptr) && (pindexBestHeader->GetBlockTime() - mi->second->GetBlockTime() < @@ -2470,7 +2472,7 @@ // hold cs_main for CBlockIndex::IsValid() LOCK(cs_main); - if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS)) { + if (pindex->IsValid(BlockValidity::TRANSACTIONS)) { // Clear download state for this block, which is in process from // some other peer. We do this after calling. ProcessNewBlock so // that a malleated cmpctblock announcement can't be used to @@ -2692,7 +2694,7 @@ bool fCanDirectFetch = CanDirectFetch(chainparams.GetConsensus()); // If this set of headers is valid and ends in a block with at least // as much work as our tip, download as much as possible. - if (fCanDirectFetch && pindexLast->IsValid(BLOCK_VALID_TREE) && + if (fCanDirectFetch && pindexLast->IsValid(BlockValidity::TREE) && chainActive.Tip()->nChainWork <= pindexLast->nChainWork) { std::vector vToFetch; const CBlockIndex *pindexWalk = pindexLast; @@ -2746,7 +2748,7 @@ if (nodestate->fSupportsDesiredCmpctVersion && vGetData.size() == 1 && mapBlocksInFlight.size() == 1 && - pindexLast->pprev->IsValid(BLOCK_VALID_CHAIN)) { + pindexLast->pprev->IsValid(BlockValidity::CHAIN)) { // In any case, we want to download using a compact // block, not a regular one. vGetData[0] = diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1446,12 +1446,12 @@ // This block cannot be connected because full block data for it or // one of its parents is missing. status = "headers-only"; - } else if (block->IsValid(BLOCK_VALID_SCRIPTS)) { + } else if (block->IsValid(BlockValidity::SCRIPTS)) { // This block is fully validated, but no longer part of the active // chain. It was probably the active block once, but was // reorganized. status = "valid-fork"; - } else if (block->IsValid(BLOCK_VALID_TREE)) { + } else if (block->IsValid(BlockValidity::TREE)) { // The headers for this block are valid, but it has not been // validated. It was probably never part of the most-work chain. status = "valid-headers"; diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -501,7 +501,7 @@ BlockMap::iterator mi = mapBlockIndex.find(hash); if (mi != mapBlockIndex.end()) { CBlockIndex *pindex = mi->second; - if (pindex->IsValid(BLOCK_VALID_SCRIPTS)) { + if (pindex->IsValid(BlockValidity::SCRIPTS)) { return "duplicate"; } if (pindex->nStatus & BLOCK_FAILED_MASK) { @@ -856,7 +856,7 @@ BlockMap::iterator mi = mapBlockIndex.find(hash); if (mi != mapBlockIndex.end()) { CBlockIndex *pindex = mi->second; - if (pindex->IsValid(BLOCK_VALID_SCRIPTS)) { + if (pindex->IsValid(BlockValidity::SCRIPTS)) { return "duplicate"; } if (pindex->nStatus & BLOCK_FAILED_MASK) { diff --git a/src/validation.cpp b/src/validation.cpp --- a/src/validation.cpp +++ b/src/validation.cpp @@ -2244,7 +2244,7 @@ // Write undo information to disk if (pindex->GetUndoPos().IsNull() || - !pindex->IsValid(BLOCK_VALID_SCRIPTS)) { + !pindex->IsValid(BlockValidity::SCRIPTS)) { if (pindex->GetUndoPos().IsNull()) { CDiskBlockPos _pos; if (!FindUndoPos( @@ -2263,7 +2263,7 @@ pindex->nStatus |= BLOCK_HAVE_UNDO; } - pindex->RaiseValidity(BLOCK_VALID_SCRIPTS); + pindex->RaiseValidity(BlockValidity::SCRIPTS); setDirtyBlockIndex.insert(pindex); } @@ -3106,7 +3106,7 @@ // call preciousblock 2**31-1 times on the same set of tips... nBlockReverseSequenceId--; } - if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS) && pindex->nChainTx) { + if (pindex->IsValid(BlockValidity::TRANSACTIONS) && pindex->nChainTx) { setBlockIndexCandidates.insert(pindex); PruneBlockIndexCandidates(); } @@ -3148,7 +3148,7 @@ // so add it again. BlockMap::iterator it = mapBlockIndex.begin(); while (it != mapBlockIndex.end()) { - if (it->second->IsValid(BLOCK_VALID_TRANSACTIONS) && + if (it->second->IsValid(BlockValidity::TRANSACTIONS) && it->second->nChainTx && !setBlockIndexCandidates.value_comp()(it->second, chainActive.Tip())) { @@ -3174,7 +3174,7 @@ it->second->GetAncestor(nHeight) == pindex) { it->second->nStatus &= ~BLOCK_FAILED_MASK; setDirtyBlockIndex.insert(it->second); - if (it->second->IsValid(BLOCK_VALID_TRANSACTIONS) && + if (it->second->IsValid(BlockValidity::TRANSACTIONS) && it->second->nChainTx && setBlockIndexCandidates.value_comp()(chainActive.Tip(), it->second)) { @@ -3229,7 +3229,7 @@ pindexNew->nChainWork = (pindexNew->pprev ? pindexNew->pprev->nChainWork : 0) + GetBlockProof(*pindexNew); - pindexNew->RaiseValidity(BLOCK_VALID_TREE); + pindexNew->RaiseValidity(BlockValidity::TREE); if (pindexBestHeader == nullptr || pindexBestHeader->nChainWork < pindexNew->nChainWork) { pindexBestHeader = pindexNew; @@ -3253,7 +3253,7 @@ pindexNew->nDataPos = pos.nPos; pindexNew->nUndoPos = 0; pindexNew->nStatus |= BLOCK_HAVE_DATA; - pindexNew->RaiseValidity(BLOCK_VALID_TRANSACTIONS); + pindexNew->RaiseValidity(BlockValidity::TRANSACTIONS); setDirtyBlockIndex.insert(pindexNew); if (pindexNew->pprev == nullptr || pindexNew->pprev->nChainTx) { @@ -3290,7 +3290,8 @@ } } } else { - if (pindexNew->pprev && pindexNew->pprev->IsValid(BLOCK_VALID_TREE)) { + if (pindexNew->pprev && + pindexNew->pprev->IsValid(BlockValidity::TREE)) { mapBlocksUnlinked.insert( std::make_pair(pindexNew->pprev, pindexNew)); } @@ -4301,7 +4302,7 @@ pindex->nChainTx = pindex->nTx; } } - if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS) && + if (pindex->IsValid(BlockValidity::TRANSACTIONS) && (pindex->nChainTx || pindex->pprev == nullptr)) { setBlockIndexCandidates.insert(pindex); } @@ -4313,7 +4314,7 @@ if (pindex->pprev) { pindex->BuildSkip(); } - if (pindex->IsValid(BLOCK_VALID_TREE) && + if (pindex->IsValid(BlockValidity::TREE) && (pindexBestHeader == nullptr || CBlockIndexWorkComparator()(pindexBestHeader, pindex))) { pindexBestHeader = pindex; @@ -4717,7 +4718,7 @@ it != mapBlockIndex.end(); it++) { CBlockIndex *pindexIter = it->second; - if (pindexIter->IsValid(BLOCK_VALID_TRANSACTIONS) && + if (pindexIter->IsValid(BlockValidity::TRANSACTIONS) && pindexIter->nChainTx) { setBlockIndexCandidates.insert(pindexIter); }