refactor: use transparent comparator for setBlockIndexCandidates lookups

This allows checking for existence in setBlockIndexCandidates using a const CBlockIndex* without casting away constness, replacing a legacy const_cast check in validation.cpp.
This commit is contained in:
joaonevess committed 2026-01-24 01:09:21 -03:00
1 parent 2bcb3f6464
commit 3bd98b4508
2 files changed
+4 -3

No files matched your search

+1
View File
@@ -136,6 +136,7 @@ using BlockMap = std::unordered_map<uint256, CBlockIndex, BlockHasher>;
struct CBlockIndexWorkComparator { struct CBlockIndexWorkComparator {
bool operator()(const CBlockIndex* pa, const CBlockIndex* pb) const; bool operator()(const CBlockIndex* pa, const CBlockIndex* pb) const;
using is_transparent = void;
}; };
struct CBlockIndexHeightOnlyComparator { struct CBlockIndexHeightOnlyComparator {
+3 -3
View File
@@ -5401,7 +5401,7 @@ void ChainstateManager::CheckBlockIndex() const
// needs to be added if it is an ancestor of the target // needs to be added if it is an ancestor of the target
// block. // block.
if (!c->TargetBlock() || c->TargetBlock()->GetAncestor(pindex->nHeight) == pindex) { if (!c->TargetBlock() || c->TargetBlock()->GetAncestor(pindex->nHeight) == pindex) {
assert(c->setBlockIndexCandidates.contains(const_cast<CBlockIndex*>(pindex))); assert(c->setBlockIndexCandidates.contains(pindex));
} }
} }
// If some parent is missing, then it could be that this block was in // If some parent is missing, then it could be that this block was in
@@ -5409,7 +5409,7 @@ void ChainstateManager::CheckBlockIndex() const
// In this case it must be in m_blocks_unlinked -- see test below. // In this case it must be in m_blocks_unlinked -- see test below.
} }
} else { // If this block sorts worse than the current tip or some ancestor's block has never been seen, it cannot be in setBlockIndexCandidates. } else { // If this block sorts worse than the current tip or some ancestor's block has never been seen, it cannot be in setBlockIndexCandidates.
assert(!c->setBlockIndexCandidates.contains(const_cast<CBlockIndex*>(pindex))); assert(!c->setBlockIndexCandidates.contains(pindex));
} }
} }
// Check whether this block is in m_blocks_unlinked. // Check whether this block is in m_blocks_unlinked.
@@ -5441,7 +5441,7 @@ void ChainstateManager::CheckBlockIndex() const
// So if this block is itself better than any m_chain.Tip() and it wasn't in // So if this block is itself better than any m_chain.Tip() and it wasn't in
// setBlockIndexCandidates, then it must be in m_blocks_unlinked. // setBlockIndexCandidates, then it must be in m_blocks_unlinked.
for (const auto& c : m_chainstates) { for (const auto& c : m_chainstates) {
if (!CBlockIndexWorkComparator()(pindex, c->m_chain.Tip()) && !c->setBlockIndexCandidates.contains(const_cast<CBlockIndex*>(pindex))) { if (!CBlockIndexWorkComparator()(pindex, c->m_chain.Tip()) && !c->setBlockIndexCandidates.contains(pindex)) {
if (pindexFirstInvalid == nullptr) { if (pindexFirstInvalid == nullptr) {
if (!c->TargetBlock() || c->TargetBlock()->GetAncestor(pindex->nHeight) == pindex) { if (!c->TargetBlock() || c->TargetBlock()->GetAncestor(pindex->nHeight) == pindex) {
assert(foundInUnlinked); assert(foundInUnlinked);