Browse Source

blockchain: fix difficulty retarget

"target" is a 256 bit int, but the "bits" field in the block headers
that is used to represent target is only 32 bits.
We were checking PoW against the untruncated target value, which is a
slightly larger value than the one that can actually be represented,
and hence we would have accepted a slightly lower difficulty chain
than what the consensus requires.
3.3.3.1
SomberNight 6 years ago
parent
commit
a8e6eaa247
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 2
      electrum/blockchain.py
  2. 502
      electrum/checkpoints.json

2
electrum/blockchain.py

@ -367,6 +367,8 @@ class Blockchain(util.PrintError):
nActualTimespan = max(nActualTimespan, nTargetTimespan // 4)
nActualTimespan = min(nActualTimespan, nTargetTimespan * 4)
new_target = min(MAX_TARGET, (target * nActualTimespan) // nTargetTimespan)
# not any target can be represented in 32 bits:
new_target = self.bits_to_target(self.target_to_bits(new_target))
return new_target
def bits_to_target(self, bits: int) -> int:

502
electrum/checkpoints.json

File diff suppressed because it is too large
Loading…
Cancel
Save