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
2025-12-30 04:27:53 -03:00
parent 2bcb3f6464
commit 3bd98b4508
2 changed files with 4 additions and 3 deletions

View File

@@ -136,6 +136,7 @@ using BlockMap = std::unordered_map<uint256, CBlockIndex, BlockHasher>;
struct CBlockIndexWorkComparator {
bool operator()(const CBlockIndex* pa, const CBlockIndex* pb) const;
using is_transparent = void;
};
struct CBlockIndexHeightOnlyComparator {

View File

@@ -5401,7 +5401,7 @@ void ChainstateManager::CheckBlockIndex() const
// needs to be added if it is an ancestor of the target
// block.
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
@@ -5409,7 +5409,7 @@ void ChainstateManager::CheckBlockIndex() const
// 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.
assert(!c->setBlockIndexCandidates.contains(const_cast<CBlockIndex*>(pindex)));
assert(!c->setBlockIndexCandidates.contains(pindex));
}
}
// 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
// setBlockIndexCandidates, then it must be in m_blocks_unlinked.
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 (!c->TargetBlock() || c->TargetBlock()->GetAncestor(pindex->nHeight) == pindex) {
assert(foundInUnlinked);