mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-02-11 09:42:17 +01:00
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:
@@ -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 {
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user