kernel: trim Chain interface #33820

pull stickies-v wants to merge 2 commits into bitcoin:master from stickies-v:2025-11/kernel-trim-chain-interface changing 4 files +4 −45
  1. stickies-v commented at 4:50 PM on November 7, 2025: contributor

    Removes btck_chain_get_genesis and btck_chain_get_tip.

    They are trivially replaced with btck_chain_get_by_height (as indicated in the updated bitcoinkernel_wrapper.h), so I think it makes sense to trim the interface.

    For btck_chain_get_tip: on master we don't provide any guarantees that the returned block index still corresponds to the actual tip, so the extra call doesn't seem like a regression to me.

  2. DrahtBot added the label Validation on Nov 7, 2025
  3. DrahtBot commented at 4:51 PM on November 7, 2025: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--006a51241073e994b41acfe9ec718e94-->

    Code Coverage & Benchmarks

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

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline for information on the review process.

    Type Reviewers
    Concept ACK TheCharlatan
    Stale ACK yuvicc

    If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  4. yuvicc commented at 2:32 PM on November 8, 2025: contributor

    ACK f4b3ba18d9bf3e7c2c55a98f3c3d98129a1fa6e2

    This makes sense to me.

  5. in src/kernel/bitcoinkernel_wrapper.h:970 in f4b3ba18d9
     968 | @@ -969,7 +969,7 @@ class ChainView : public View<btck_Chain>
     969 |  
     970 |      BlockTreeEntry Tip() const
    


    sedited commented at 10:23 PM on November 8, 2025:

    I wonder if we should even keep these methods now. The previous calls to tip and genesis were at least thread safe (which these are no longer). I agree that it is not worth keeping them just for the sake of it, but also not sure if they still provide utility here.


    stickies-v commented at 9:38 AM on November 10, 2025:

    The previous calls to tip and genesis were at least thread safe

    With previous, do you mean master? I'm not sure how the current calls are not thread safe? These are read-only calls without side effects? And both in master and this PR, the caller has no guarantees that the returned tip is indeed the tip because cs_main isn't exposed (regardless of threads)?

    I wonder if we should even keep these methods now.

    I agree, but I want to generalize that even more. I think Range objects should be the only public interface for their contents. Duplicated getter and count methods on the parent seem confusing and unnecessary, and are best made private imo. I'm taking that approach with py-bitcoinkernel and I think it works well. But I think given the added scope that's better suited for a separate PR, which I'll push out soon.


    maflcko commented at 9:45 AM on November 10, 2025:

    With previous, do you mean master? I'm not sure how the current calls are not thread safe? These are read-only calls without side effects? And both in master and this PR, the caller has no guarantees that the returned tip is indeed the tip because cs_main isn't exposed (regardless of threads)?

    I think there could be a rare and theoretical race where the function does not return any tip, but rather throws std::runtime_error("No entry in the chain at the provided height"); when the height is re-orged out to a lower more-work height. But this is mostly an edge case.


    stickies-v commented at 10:29 AM on November 10, 2025:

    I hadn't thought of the reduced-height reorg scenario, thanks for that. Reorg-safety in general is still an unsolved problem for the public interface, and this is another case of that.

    Note: the problem you report already exists for objects based on the Range class whenever size() depends on chain length, such as for Chain::Entries with Chain::Entries::back() being able to throw a std::runtime_error.


    sedited commented at 10:35 AM on November 10, 2025:

    Yes, I was merely mentioning it, because the Tip and Genesis methods in current master at least provided a bit better integrity. I don't think this is important at all though. External callers should provide synchronization for this. So the two calls should just be removed imo.


    stickies-v commented at 12:50 PM on November 10, 2025:

    So the two calls should just be removed imo.

    Done.

  6. sedited commented at 10:24 PM on November 8, 2025: contributor

    Concept ACK

    These were introduced before we had a dedicated iterator for traversing the chain. At this point, their benefit (thread safety) is kind of marginal.

  7. sedited added this to a project on Nov 10, 2025
  8. sedited changed the project status on Nov 10, 2025
  9. kernel: remove btck_chain_get_genesis
    It is equivalent to calling btck_chain_get_by_height(0).
    4dd7e6dc48
  10. kernel: remove btck_chain_get_tip
    It is equivalent to calling btck_chain_get_by_height with the
    height obtained from btck_chain_get_height. In neither case do we
    provide guarantees that the returned block index still corresponds
    to the actual tip.
    66978a1a95
  11. stickies-v force-pushed on Nov 10, 2025
  12. stickies-v commented at 12:51 PM on November 10, 2025: contributor

    Force-pushed to remove the bitcoinkernel_wrapper.h Tip() and Genesis() methods too, as suggested here.

  13. sedited approved
  14. sedited commented at 2:13 PM on November 10, 2025: contributor

    ACK 66978a1a95379a2fe5d41032682dedfaddc99db9

  15. in src/test/kernel/test_kernel.cpp:669 in 66978a1a95
     663 | @@ -664,7 +664,7 @@ void chainman_reindex_test(TestDirectory& test_directory)
     664 |      // Sanity check some block retrievals
     665 |      auto chain{chainman->GetChain()};
     666 |      BOOST_CHECK_THROW(chain.GetByHeight(1000), std::runtime_error);
     667 | -    auto genesis_index{chain.Genesis()};
     668 | +    auto genesis_index{chain.Entries().front()};
     669 |      BOOST_CHECK(!genesis_index.GetPrevious());
     670 |      auto genesis_block_raw{chainman->ReadBlock(genesis_index).value().ToBytes()};
    


    janb84 commented at 3:47 PM on November 10, 2025:
        auto genesis_block_raw{chainman->ReadBlock(genesis_index).value().ToBytes()};
        auto first_index{chain.GetByHeight(0)};
        auto first_block_raw{chainman->ReadBlock(genesis_index).value().ToBytes()};
    

    Not sure if this part of the test still makes sense, the double retrieval of the data of block 0 auto first_block_raw{chainman->ReadBlock(genesis_index).value().ToBytes()}; we are now testing if chainman->ReadBlock will retrieve the same block twice not if it will get the expected block.

    We test on line 761 we test if readblock retrieves the correct block.

  16. janb84 commented at 4:09 PM on November 10, 2025: contributor

    ACK 66978a1a95379a2fe5d41032682dedfaddc99db9

    Was already wondering why these 2 functions where present in the code. With the iteration ability I agree that there is no need for these "speciality" functions. The functionality can be implemented downstream if needed.

    steps taken: code review, build, ran all tests,

    (small contemplation; chain.tip is 'often' used in the rest of the bitcoin code, is removing the tip function from the header not a performance burden downstream to call 2 functions instead of 1 => no because downstream you could even cache that. )

  17. fanquake merged this on Nov 11, 2025
  18. fanquake closed this on Nov 11, 2025

  19. github-project-automation[bot] changed the project status on Nov 11, 2025
  20. stickies-v deleted the branch on Nov 11, 2025
  21. sedited referenced this in commit 94f024ad07 on Nov 11, 2025
  22. yuvicc referenced this in commit f620a2206b on Nov 16, 2025
  23. stringintech referenced this in commit 52fffa1e5d on Nov 24, 2025
  24. stickies-v referenced this in commit 9fa736f8e4 on Jan 22, 2026
  25. Kino1994 referenced this in commit c3320b109f on Jun 28, 2026
  26. BigcoinBGC referenced this in commit 154c79f2e7 on Jun 30, 2026
  27. Kino1994 referenced this in commit 1561f059c4 on Aug 19, 2026
  28. bitcoin locked this on Nov 11, 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-11-27 03:51 UTC

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