From ee10e8e4d305a369ff8e3b87507dd6d6f344cb9b Mon Sep 17 00:00:00 2001 From: SomberNight Date: Sat, 13 Nov 2021 03:07:36 +0100 Subject: [PATCH] blockchain: bits_to_target: small clean-up note: why is the first byte cut unconditionally? what if it's non-zero? Maybe the intention of cutting the first two chars in the hex case intended to cut a "0x" prefix?? But there was no such prefix with the given format string... --- electrum/blockchain.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/electrum/blockchain.py b/electrum/blockchain.py index 9e9944902..b850b04b3 100644 --- a/electrum/blockchain.py +++ b/electrum/blockchain.py @@ -552,10 +552,11 @@ class Blockchain(Logger): @classmethod def target_to_bits(cls, target: int) -> int: - c = ("%064x" % target)[2:] - while c[:2] == '00' and len(c) > 6: - c = c[2:] - bitsN, bitsBase = len(c) // 2, int.from_bytes(bfh(c[:6]), byteorder='big') + c = target.to_bytes(length=32, byteorder='big') + c = c[1:] # FIXME why is this done unconditionally? + while c[0] == 0 and len(c) > 3: + c = c[1:] + bitsN, bitsBase = len(c), int.from_bytes(c[:3], byteorder='big') if bitsBase >= 0x800000: bitsN += 1 bitsBase >>= 8