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 +4 −3
  1. joaonevess commented at 7:40 am on December 30, 2025: contributor

    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. DrahtBot added the label Refactoring on Dec 30, 2025
  3. 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.

    Type Reviewers
    ACK maflcko, frankomosh, sedited
    Stale ACK bensig

    If your review is incorrectly listed, please copy-paste <!–meta-tag:bot-skip–> into the comment that the bot should ignore.

    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.

  4. bensig commented at 7:08 pm on January 2, 2026: contributor

    ACK 1cda007

    Built and ran validation_tests and blockmanager_tests on macOS - all pass. Clean removal of const_cast using transparent lookup. Good improvement to code safety.

  5. 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.
    3bd98b4508
  6. in src/validation.cpp:3245 in 1cda007cc3
    3241@@ -3242,7 +3242,7 @@ CBlockIndex* Chainstate::FindMostWorkChain()
    3242 void Chainstate::PruneBlockIndexCandidates() {
    3243     // Note that we can't delete the current block itself, as we may need to return to it later in case a
    3244     // reorganization to a better block fails.
    3245-    std::set<CBlockIndex*, CBlockIndexWorkComparator>::iterator it = setBlockIndexCandidates.begin();
    3246+    std::set<CBlockIndex*, node::CBlockIndexWorkComparator>::iterator it = setBlockIndexCandidates.begin();
    


    maflcko commented at 7:05 pm on January 19, 2026:
    unrelated change, which seems to go in the wrong direction?

    joaonevess commented at 4:12 am on January 24, 2026:
    Good catch. That was an accidental IDE auto-complete inclusion. Reverted.
  7. joaonevess force-pushed on Jan 24, 2026
  8. joaonevess requested review from maflcko on Jan 24, 2026
  9. DrahtBot added the label CI failed on Jan 26, 2026
  10. DrahtBot removed the label CI failed on Jan 27, 2026
  11. maflcko commented at 3:53 pm on January 27, 2026: member

    review ACK 3bd98b45084d3029465110a99e2486d48944ded8 🚪

    Signature:

    0untrusted comment: signature from minisign secret key on empty file; verify via: minisign -Vm "${path_to_any_empty_file}" -P RWTRmVTMeKV5noAMqVlsMugDDCyyTSbA3Re5AkUrhvLVln0tSaFWglOw -x "${path_to_this_whole_four_line_signature_blob}"
    1RUTRmVTMeKV5npGrKx1nqXCw5zeVHdtdYURB/KlyA/LMFgpNCs+SkW9a8N95d+U4AP1RJMi+krxU1A3Yux4bpwZNLvVBKy0wLgM=
    2trusted comment: review ACK 3bd98b45084d3029465110a99e2486d48944ded8 🚪
    345FOhKGjVyv3TrqHDxoEoZQlbANmAfbvFxa2bYgCkWkK+gF3QIDZFT46zWTU40TmGDwvN/Kh++rqPlYP1LAYDA==
    
  12. frankomosh commented at 10:44 am on January 28, 2026: contributor
    ACK 3bd98b45084d3029465110a99e2486d48944ded8. Good use of transparent comparator to eliminate const_cast in this specific code path.
  13. sedited approved
  14. sedited commented at 7:36 am on February 2, 2026: contributor
    ACK 3bd98b45084d3029465110a99e2486d48944ded8
  15. sedited merged this on Feb 2, 2026
  16. sedited closed this on Feb 2, 2026


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-02-11 21:13 UTC

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