refactor: Improve naming of CBlock::GetHash() -> GetHeaderHash() #29538

pull fjahr wants to merge 2 commits into bitcoin:master from fjahr:2024-03-headerhash changing 34 files +122 −114
  1. fjahr commented at 9:18 pm on March 2, 2024: contributor

    dergoegge noted here that CBlock::GetHash() is a footgun since it only calculates the hash of the header and not the full block. I agree with that sentiment and think it would be beneficial to make this explicit by renaming the inherited function.

    The same goes for ConstructBlockHash().

  2. DrahtBot commented at 9:18 pm on March 2, 2024: contributor

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

    Code Coverage

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

    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:

    • #29642 (kernel: Handle fatal errors through return values by TheCharlatan)
    • #29641 (scripted-diff: Use LogInfo/LogDebug over LogPrintf/LogPrint by maflcko)
    • #29575 (net_processing: make any misbehavior trigger immediate discouragement by sipa)
    • #28843 ([refactor] Remove BlockAssembler m_mempool member by TheCharlatan)
    • #27006 (reduce cs_main scope, guard block index ’nFile’ under a local mutex by furszy)

    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.

  3. DrahtBot added the label Refactoring on Mar 2, 2024
  4. mzumsande commented at 10:21 pm on March 2, 2024: contributor
    I don’t think that this is a good idea. The term “block hash” is used in dozens of RPCs, BIPs etc., basically everywhere all over the world. Renaming a single function within bitcoin core won’t change that and will only lead to confusion that the header hash might be different from what the rest of the world calls the “block hash”.
  5. fjahr commented at 6:13 pm on March 3, 2024: contributor

    I don’t think that this is a good idea. The term “block hash” is used in dozens of RPCs, BIPs etc., basically everywhere all over the world. Renaming a single function within bitcoin core won’t change that and will only lead to confusion that the header hash might be different from what the rest of the world calls the “block hash”.

    The rest of the world also doesn’t think about stuff like block maleation, verification of the merkle root etc., but we do here in the code. If this helps us with reasoning about these things easier and prevents footguns, we should do it. If it’s confusing someone who is contributing to this project that there is a distinction between hashing just header and hashing the whole block, I think that is a great learning opportunity for them and this might actually prevent them from shooting themselves in the foot in the future :)

  6. DrahtBot added the label Needs rebase on Mar 9, 2024
  7. fjahr force-pushed on Mar 10, 2024
  8. DrahtBot removed the label Needs rebase on Mar 10, 2024
  9. DrahtBot added the label Needs rebase on Mar 12, 2024
  10. fjahr force-pushed on Mar 12, 2024
  11. refactor: Rename CBlock::GetHash() to GetHeaderHash()
    The function only hashes the header of the block but not the full block as the name implies.
    07c8016eea
  12. refactor: Rename ConstructBlockHash() to ConstructHeaderHash() 7e2ac74cc3
  13. fjahr force-pushed on Mar 12, 2024
  14. DrahtBot added the label CI failed on Mar 12, 2024
  15. DrahtBot commented at 3:25 pm on March 12, 2024: contributor

    🚧 At least one of the CI tasks failed. Make sure to run all tests locally, according to the documentation.

    Possibly this is due to a silent merge conflict (the changes in this pull request being incompatible with the current code in the target branch). If so, make sure to rebase on the latest commit of the target branch.

    Leave a comment here, if you need help tracking down a confusing failure.

    Debug: https://github.com/bitcoin/bitcoin/runs/22563514445

  16. DrahtBot removed the label Needs rebase on Mar 12, 2024
  17. DrahtBot removed the label CI failed on Mar 13, 2024
  18. sipa commented at 7:27 pm on March 13, 2024: member

    I agree with @mzumsande. It’d have been nice perhaps is the adopted terminology was “block header hash” rather than “block hash”, but making that point now through the code I think will do more harm than good. People might equally be surprised by seeing something referred to as block header hash now, if they expect it to be called block hash.

    If it’s confusing someone who is contributing to this project that there is a distinction between hashing just header and hashing the whole block

    I don’t think I agree with that. The block header hash, as you call it, is the hash of the whole block (the merkle root commits to the transactions). In fact, the block header hash is in fact a hash of the entire chain up to that point too.

  19. fjahr commented at 2:04 pm on March 17, 2024: contributor

    I agree with @mzumsande. It’d have been nice perhaps is the adopted terminology was “block header hash” rather than “block hash”, but making that point now through the code I think will do more harm than good. People might equally be surprised by seeing something referred to as block header hash now, if they expect it to be called block hash.

    I don’t think anyone aside from regular contributors would even notice since this doesn’t change any exposed APIs.

    If it’s confusing someone who is contributing to this project that there is a distinction between hashing just header and hashing the whole block

    I don’t think I agree with that. The block header hash, as you call it, is the hash of the whole block (the merkle root commits to the transactions). In fact, the block header hash is in fact a hash of the entire chain up to that point too.

    But the whole issue is that the merke root is not validated here, so we don’t get the hash of the whole block, we only get the hash of the header. It is literally the GetHash() inherited from CBlockHeader that we are actually calling. It is a good point though that the hash also contains the prevblock header hash that is also not validated.

    Closing this due to lack of support but I still think this would be a good idea.

  20. fjahr closed this on Mar 17, 2024

  21. fjahr deleted the branch on Mar 17, 2024
  22. sipa commented at 3:57 pm on March 17, 2024: member

    But the whole issue is that the merke root is not validated here, so we don’t get the hash of the whole block, we only get the hash of the header. It is literally the GetHash() inherited from CBlockHeader that we are actually calling.

    Ah I see now where you’re coming from. Before a block header is validated, its hash is indeed just the hash of its header and it cannot be considered yet to be the hash of the full block. I still don’t think a change like this is desirable, though.


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: 2024-12-03 18:12 UTC

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