refactor: use transparent comparator for setBlockIndexCandidates lookups
What changed, and why it matters
This is a small code cleanup (refactor) in Bitcoin Core. It removes three uses of const_cast in a consistency-checking function by teaching the block index comparator to accept lookups with const pointers. There is no change to program logic, no bug fix, and no security-relevant behavior change.
No security action needed. Treat as normal code-quality refactor.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit adds is_transparent to CBlockIndexWorkComparator so std::set::contains can be called with a const CBlockIndex* key. This eliminates three const_cast
Changed components
src/node/blockstorage.hsrc/validation.cppInspect captured patch +4 / −3
diff --git a/src/node/blockstorage.h b/src/node/blockstorage.h
index 3fb6cc3e..0a2f99bb 100644
--- a/src/node/blockstorage.h
+++ b/src/node/blockstorage.h
@@ -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 {
diff --git a/src/validation.cpp b/src/validation.cpp
index fe4237db..6cf3a31c 100644
--- a/src/validation.cpp
+++ b/src/validation.cpp
@@ -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);
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.