Skip to content

Commit 7060d2d

Browse files
author
MarcoFalke
committed
Merge #18350: test: Fix mining to an invalid target + ensure that a new block has the correct hash internally
7a6627a Fix mining to an invalid target + ensure that a new block has the correct hash internally in Python tests (Samer Afach) Pull request description: 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; ``` 2. 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. Top commit has no ACKs. Tree-SHA512: f3b54d18f5073d6f1c26eab89bfec78620dda4ac1e4dde4f1d69543f1b85a7989d64c907e091db63f3f062408f5ed1e111018b842819ba1a5f8348c7b01ade96
2 parents 25424cf + 7a6627a commit 7060d2d

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

test/functional/feature_block.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ def run_test(self):
632632
self.move_tip(44)
633633
b47 = self.next_block(47, solve=False)
634634
target = uint256_from_compact(b47.nBits)
635-
while b47.sha256 < target:
635+
while b47.sha256 <= target:
636636
b47.nNonce += 1
637637
b47.rehash()
638638
self.send_blocks([b47], False, force_send=True, reject_reason='high-hash', reconnect=True)
@@ -1345,6 +1345,8 @@ def next_block(self, number, spend=None, additional_coinbase_value=0, script=CSc
13451345
block.hashMerkleRoot = block.calc_merkle_root()
13461346
if solve:
13471347
block.solve()
1348+
else:
1349+
block.rehash()
13481350
self.tip = block
13491351
self.block_heights[block.sha256] = height
13501352
assert number not in self.blocks

0 commit comments

Comments
 (0)