From a7f0c90c8a55839ece4a8c62c9d0414dcce14db1 Mon Sep 17 00:00:00 2001 From: JeremyRand <244188+JeremyRand@users.noreply.github.com> Date: Tue, 31 Oct 2023 14:02:20 +0000 Subject: [PATCH] Add pow_hash_header abstraction (#7592) There is no reason why the hash function for identifying a block and the hash function used to instantiate Hashcash must be the same; it's only a coincidence that Bitcoin happens to use the same hash for both use cases. Reflecting this fact by adding this abstraction makes the code more flexible. Co-authored-by: Jeremy Rand --- electrum/blockchain.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/electrum/blockchain.py b/electrum/blockchain.py index 2dd4982c1..f7af34da1 100644 --- a/electrum/blockchain.py +++ b/electrum/blockchain.py @@ -86,6 +86,9 @@ def hash_raw_header(header: str) -> str: return hash_encode(sha256d(bfh(header))) +pow_hash_header = hash_header + + # key: blockhash hex at forkpoint # the chain at some key is the best chain that includes the given hash blockchains = {} # type: Dict[str, Blockchain] @@ -305,9 +308,10 @@ class Blockchain(Logger): bits = cls.target_to_bits(target) if bits != header.get('bits'): raise InvalidHeader("bits mismatch: %s vs %s" % (bits, header.get('bits'))) - block_hash_as_num = int.from_bytes(bfh(_hash), byteorder='big') - if block_hash_as_num > target: - raise InvalidHeader(f"insufficient proof of work: {block_hash_as_num} vs target {target}") + _pow_hash = pow_hash_header(header) + pow_hash_as_num = int.from_bytes(bfh(_pow_hash), byteorder='big') + if pow_hash_as_num > target: + raise InvalidHeader(f"insufficient proof of work: {pow_hash_as_num} vs target {target}") def verify_chunk(self, index: int, data: bytes) -> None: num = len(data) // HEADER_SIZE