From 8cc3ac6c2328873e57c31f237d194c5f22b6748a Mon Sep 17 00:00:00 2001 From: Martin Zumsande Date: Mon, 24 Mar 2025 14:54:55 -0400 Subject: [PATCH] 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 --- src/chain.h | 2 +- src/test/fuzz/chain.cpp | 2 +- src/validation.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/chain.h b/src/chain.h index c6d1640768e..f5bfdb2fb4b 100644 --- a/src/chain.h +++ b/src/chain.h @@ -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); diff --git a/src/test/fuzz/chain.cpp b/src/test/fuzz/chain.cpp index 0363f317b63..09053e4815c 100644 --- a/src/test/fuzz/chain.cpp +++ b/src/test/fuzz/chain.cpp @@ -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(); diff --git a/src/validation.cpp b/src/validation.cpp index 495154ad616..72ed6e8fe90 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -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)) {