validation: Don't use IsValid() to filter for invalid blocks

IsValid() also returns false for blocks that have not been
validated yet up to the default validity level of BLOCK_VALID_TRANSACTIONS but
are not marked as invalid - e.g. if we only know the header.
Here, we specifically want to filter for invalid blocks.

Also removes the default arg from IsValid() which is now unused outside
of tests, to prevent this kind of misuse for the future.

Co-authored-by: TheCharlatan <seb.kung@gmail.com>
This commit is contained in:
Martin Zumsande
2025-03-24 14:54:55 -04:00
parent 86d98b94e5
commit 8cc3ac6c23
3 changed files with 3 additions and 3 deletions

View File

@@ -292,7 +292,7 @@ public:
std::string ToString() const;
//! 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 BlockStatus nUpTo) const
EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
{
AssertLockHeld(::cs_main);

View File

@@ -30,7 +30,7 @@ FUZZ_TARGET(chain)
(void)disk_block_index->GetMedianTimePast();
(void)disk_block_index->GetUndoPos();
(void)disk_block_index->HaveNumChainTxs();
(void)disk_block_index->IsValid();
(void)disk_block_index->IsValid(BLOCK_VALID_TRANSACTIONS);
}
const CBlockHeader block_header = disk_block_index->GetBlockHeader();

View File

@@ -3851,7 +3851,7 @@ void Chainstate::ResetBlockFailureFlags(CBlockIndex *pindex) {
// Remove the invalidity flag from this block and all its descendants and ancestors.
for (auto& [_, block_index] : m_blockman.m_block_index) {
if (!block_index.IsValid() && (block_index.GetAncestor(nHeight) == pindex || pindex->GetAncestor(block_index.nHeight) == &block_index)) {
if ((block_index.nStatus & BLOCK_FAILED_MASK) && (block_index.GetAncestor(nHeight) == pindex || pindex->GetAncestor(block_index.nHeight) == &block_index)) {
block_index.nStatus &= ~BLOCK_FAILED_MASK;
m_blockman.m_dirty_blockindex.insert(&block_index);
if (block_index.IsValid(BLOCK_VALID_TRANSACTIONS) && block_index.HaveNumChainTxs() && setBlockIndexCandidates.value_comp()(m_chain.Tip(), &block_index)) {