refactor: Enable transparent lookup for setBlockIndexCandidates to remove const_cast #34179

pull joaonevess wants to merge 1 commits into bitcoin:master from joaonevess:refactor-is-transparent-comparator changing 2 files +5 −4
  1. joaonevess commented at 7:40 am on December 30, 2025: none

    Rationale

    This PR improves code safety by removing a const_cast in src/validation.cpp.

    Currently, setBlockIndexCandidates stores mutable CBlockIndex*. However, validation logic (like CVerifyDB) often holds const CBlockIndex*. Previously, checking for existence in the set required casting away constness. While currently benign, this bypasses compiler safety checks and could mask accidental modifications in future refactors.

    Description

    1. Enable Heterogeneous Lookup: Added using is_transparent = void; to CBlockIndexWorkComparator in src/node/blockstorage.h. This allows the std::set to natively accept const CBlockIndex* for lookup (utilizing C++14 heterogeneous lookup).
    2. Remove Cast: Removed the now unnecessary const_cast<CBlockIndex*> in src/validation.cpp, allowing the compiler to strictly enforce const-correctness.

    Notes

    • Refactoring only: No behavioral change.
    • Verification: validation_tests and blockmanager_tests pass.
  2. 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.
    1cda007cc3
  3. DrahtBot added the label Refactoring on Dec 30, 2025
  4. DrahtBot commented at 7:40 am on December 30, 2025: contributor

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    Code Coverage & Benchmarks

    For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/34179.

    Reviews

    See the guideline for information on the review process. A summary of reviews will appear here.

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #33637 (refactor: optimize block index comparisons (1.4-6.8x faster) by l0rinc)

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-01-02 00:13 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me