chainparams: Handle Testnet4 in GetNetworkForMagic #30625

pull fjahr wants to merge 1 commits into bitcoin:master from fjahr:2024-08-testnet4-missing-check changing 1 files +3 −0
  1. fjahr commented at 8:23 PM on August 10, 2024: contributor

    Found during testing: The recently introduced GetNetworkForMagic() doesn't handle Testnet4 yet.

  2. chainparams: Handle Testnet4 in GetNetworkForMagic b0ec8716bf
  3. DrahtBot commented at 8:23 PM on August 10, 2024: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--006a51241073e994b41acfe9ec718e94-->

    Code Coverage

    For detailed information about the code coverage, see the test coverage report.

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline for information on the review process.

    Type Reviewers
    ACK tdb3, maflcko, theStack, willcl-ark

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

  4. tdb3 approved
  5. tdb3 commented at 9:39 PM on August 10, 2024: contributor

    cr ACK b0ec8716bf27335686471e0ae4c6a34f9a08f33c

  6. in src/kernel/chainparams.cpp:696 in b0ec8716bf
     691 |  
     692 |      if (std::equal(message.begin(), message.end(), mainnet_msg.data())) {
     693 |          return ChainType::MAIN;
     694 |      } else if (std::equal(message.begin(), message.end(), testnet_msg.data())) {
     695 |          return ChainType::TESTNET;
     696 | +    } else if (std::equal(message.begin(), message.end(), testnet4_msg.data())) {
    


    maflcko commented at 10:09 AM on August 11, 2024:

    style-nit: I know you copied this from the surrounding code, but creating a copy is not needed to compare two const references, and if the size of the second range in std::equal is different, the result is either wrong or undefined behavior (UB/uninitialized read). See also the notes section (https://en.cppreference.com/w/cpp/algorithm/equal#Notes), which says "... When comparing entire containers for equality, operator== for the corresponding container are usually preferred".

    So that'd be:

    diff --git a/src/kernel/chainparams.cpp b/src/kernel/chainparams.cpp
    index d0de3a4eab..c8522aa314 100644
    --- a/src/kernel/chainparams.cpp
    +++ b/src/kernel/chainparams.cpp
    @@ -688,7 +688,7 @@ std::optional<ChainType> GetNetworkForMagic(const MessageStartChars& message)
         const auto regtest_msg = CChainParams::RegTest({})->MessageStart();
         const auto signet_msg = CChainParams::SigNet({})->MessageStart();
     
    -    if (std::equal(message.begin(), message.end(), mainnet_msg.data())) {
    +    if (message == CChainParams::Main()->MessageStart()) {
             return ChainType::MAIN;
         } else if (std::equal(message.begin(), message.end(), testnet_msg.data())) {
             return ChainType::TESTNET;
    

    An alternative would be to use std::ranges::equal https://en.cppreference.com/w/cpp/algorithm/ranges/equal or the std::equal overload that takes 4 iterators.

  7. maflcko approved
  8. maflcko commented at 10:10 AM on August 11, 2024: member

    review ACK b0ec8716bf27335686471e0ae4c6a34f9a08f33c

    Left a style-nit to avoid a copy and to make the code shorter, but only if you re-touch.

  9. theStack approved
  10. theStack commented at 10:29 PM on August 11, 2024: contributor

    ACK b0ec8716bf27335686471e0ae4c6a34f9a08f33c

  11. willcl-ark approved
  12. willcl-ark commented at 8:48 AM on August 12, 2024: member

    crACK b0ec8716bf27335686471e0ae4c6a34f9a08f33c

  13. Theschorpioen approved
  14. fanquake merged this on Aug 12, 2024
  15. fanquake closed this on Aug 12, 2024

  16. bitcoin locked this on Aug 12, 2025

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-04-22 03:13 UTC

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