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 <jeremyrand@danwin1210.de>
This commit is contained in:
@@ -86,6 +86,9 @@ def hash_raw_header(header: str) -> str:
|
|||||||
return hash_encode(sha256d(bfh(header)))
|
return hash_encode(sha256d(bfh(header)))
|
||||||
|
|
||||||
|
|
||||||
|
pow_hash_header = hash_header
|
||||||
|
|
||||||
|
|
||||||
# key: blockhash hex at forkpoint
|
# key: blockhash hex at forkpoint
|
||||||
# the chain at some key is the best chain that includes the given hash
|
# the chain at some key is the best chain that includes the given hash
|
||||||
blockchains = {} # type: Dict[str, Blockchain]
|
blockchains = {} # type: Dict[str, Blockchain]
|
||||||
@@ -305,9 +308,10 @@ class Blockchain(Logger):
|
|||||||
bits = cls.target_to_bits(target)
|
bits = cls.target_to_bits(target)
|
||||||
if bits != header.get('bits'):
|
if bits != header.get('bits'):
|
||||||
raise InvalidHeader("bits mismatch: %s vs %s" % (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')
|
_pow_hash = pow_hash_header(header)
|
||||||
if block_hash_as_num > target:
|
pow_hash_as_num = int.from_bytes(bfh(_pow_hash), byteorder='big')
|
||||||
raise InvalidHeader(f"insufficient proof of work: {block_hash_as_num} vs target {target}")
|
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:
|
def verify_chunk(self, index: int, data: bytes) -> None:
|
||||||
num = len(data) // HEADER_SIZE
|
num = len(data) // HEADER_SIZE
|
||||||
|
|||||||
Reference in New Issue
Block a user