test: Fix mining to an invalid target + ensure that a new block has the correct hash internally #18350

pull TheQuantumPhysicist wants to merge 1 commits into bitcoin:master from TheQuantumPhysicist:master changing 1 files +3 −1
  1. TheQuantumPhysicist commented at 1:55 PM on March 14, 2020: contributor

    Test with block 47 in the feature_block.py creates a block with a hash higher than the target, which is supposed to fail. Now two issues exist there, and both have low probability of showing up:

    1. The creation is done with while (hash < target), which is wrong, because hash = target is a valid mined value based on the code in the function CheckProofOfWork() that validates the mining target:
        if (UintToArith256(hash) > bnTarget)
            return false;
    
    1. As we know the hash stored in CBlock class in Python is stateful, unlike how it's in C++, where calling CBlock::GetHash() will actively calculate the hash and not cache it anywhere. With this, blocks that come out of the method next_block can have incorrect hash value when solve=False. This is because the next_block is mostly used with solve=True, and solving does call the function rehash() which calculates the hash of the block, but with solve=False, nothing calls that method. And since the work to be done in regtests is very low, the probably of this problem showing up is very low, but it practically happens (well, with much higher probability compared to issue No. 1 above).

    This PR fixes both these issues.

  2. Fix mining to an invalid target + ensure that a new block has the
    correct hash internally in Python tests
    7a6627ae87
  3. TheQuantumPhysicist renamed this:
    Fix mining to an invalid target + ensure that a new block has the correct hash internally
    Regtest: Fix mining to an invalid target + ensure that a new block has the correct hash internally
    on Mar 14, 2020
  4. DrahtBot added the label Tests on Mar 14, 2020
  5. MarcoFalke renamed this:
    Regtest: Fix mining to an invalid target + ensure that a new block has the correct hash internally
    test: Fix mining to an invalid target + ensure that a new block has the correct hash internally
    on Mar 14, 2020
  6. MarcoFalke commented at 4:40 PM on March 14, 2020: member

    If this was ever the cause of a test failure, it probably means our assumption about hash functions is broken.

    Still, ACK. I don't mind fixing it.

  7. jnewbery commented at 4:46 PM on March 14, 2020: member

    Concept ACK fixing issue 1. Like @MarcoFalke says, the probability of this ever causing a failure is negligible, but it's better that the test's assumptions are accurate.

    For issue 2 (next_block not recalculating the digest if solve is False), I think we can just remove the solve parameter from that function and always solve the block. The function is only called with solve=False in two places, and in both cases the nonce is recalculated (to be invalid for block 47, and resolved for block 48).

  8. TheQuantumPhysicist commented at 4:46 PM on March 14, 2020: contributor

    If this was ever the cause of a test failure, it probably means our assumption about hash functions is broken.

    Actually you're right about the first fix, but the second one has a much higher likelihood to happen. I was able to simulate it by putting it on the tip of the block 1 and it happened always within a loop of 1000 tests that repeat b47's test case.

  9. MarcoFalke commented at 1:22 PM on March 16, 2020: member

    @TheQuantumPhysicist let me know if you want to address @jnewbery 's feedback or not

  10. TheQuantumPhysicist commented at 8:11 PM on March 16, 2020: contributor

    @MarcoFalke Do you mean implement it? If so, I don't mind. I just need a confirmation that this is the solution we wanna go for.

    In my opinion, the solve option should remain, because solving and hashing are different requirements. But I don't mind either way.

  11. MarcoFalke commented at 8:18 PM on March 16, 2020: member

    I think in tests we aim for simplicity, even if that comes at a trivial cost like a few hashes. So I see @jnewbery 's point. I am happy to merge either version.

  12. TheQuantumPhysicist commented at 8:28 PM on March 16, 2020: contributor

    If you don't mind either way, please merge it as it's, and anyone is welcome to change it, since it's correct in its current form.

    If I see the majority over here insisting on changing it to remove solve, I'm all for it. I'm just too lazy to run more and test more spontaneously 😄

  13. MarcoFalke merged this on Mar 16, 2020
  14. MarcoFalke closed this on Mar 16, 2020

  15. jnewbery commented at 12:11 AM on March 17, 2020: member

    I really don't like the second commit. I think it adds complexity for no good reason, and without commenting it makes the code less clear instead of more.

  16. MarcoFalke commented at 2:18 AM on March 17, 2020: member

    This has been merged, but anyone who feels that the logic needs commenting or a refactor, may open a follow-up pull request.

  17. sidhujag referenced this in commit cc5eb80385 on Mar 17, 2020
  18. MarkLTZ referenced this in commit 8823f65197 on Apr 7, 2020
  19. MarkLTZ referenced this in commit 0a1571d657 on Apr 7, 2020
  20. sidhujag referenced this in commit 11c295855e on Nov 10, 2020
  21. Fabcien referenced this in commit ffcbbd7f48 on Jan 8, 2021
  22. PastaPastaPasta referenced this in commit cebc706ed9 on Jun 27, 2021
  23. PastaPastaPasta referenced this in commit 22db270c27 on Jun 28, 2021
  24. PastaPastaPasta referenced this in commit 2bf0881abd on Jun 29, 2021
  25. PastaPastaPasta referenced this in commit 8de74ed42d on Jul 1, 2021
  26. PastaPastaPasta referenced this in commit eed2761e29 on Jul 1, 2021
  27. PastaPastaPasta referenced this in commit 9156e07334 on Jul 14, 2021
  28. DrahtBot locked this on Feb 15, 2022

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-29 03:14 UTC

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