From a87a19a2309876e6210783957062d7aa7463f502 Mon Sep 17 00:00:00 2001 From: 4tochka Date: Sun, 1 Apr 2018 23:10:56 +0400 Subject: [PATCH] block template class --- pybtc/blockchain.py | 1 + pybtc/tools.py | 15 +++++++++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/pybtc/blockchain.py b/pybtc/blockchain.py index 48c49ad..1b65ef8 100644 --- a/pybtc/blockchain.py +++ b/pybtc/blockchain.py @@ -902,6 +902,7 @@ class BlockTemplate(): nonce = s2rh(nonce) cbh = double_sha256(unhexlify(cb)) merkle_root = merkleroot_from_branches(self.merkle_branches, cbh) + print("merkle_root ", hexlify(merkle_root)) header = version + prev_hash + merkle_root + time + bits + nonce block = hexlify(header).decode() block += hexlify(to_var_int(len (self.transactions)+1)).decode() diff --git a/pybtc/tools.py b/pybtc/tools.py index 77a4f55..83b7cde 100644 --- a/pybtc/tools.py +++ b/pybtc/tools.py @@ -450,14 +450,21 @@ def is_valid_signature_encoding(sig): def bits2target(bits): if type(bits) == str: bits = unhexlify(bits) - return int.from_bytes(bits[1:], 'big') * (2 ** (8 * (bits[0] - 3))) + if type(bits) == bytes: + return int.from_bytes(bits[1:], 'big') * (2 ** (8 * (bits[0] - 3))) + else: + shift = bits >> 24 + target = (bits & 0xffffff) * (1 << (8 * (shift - 3))) + return target -def target2diff(target): +def target2difficulty(target): return 0x00000000FFFF0000000000000000000000000000000000000000000000000000 / target -def bits2diff(bits): - return target2diff(bits2target(bits)) +def bits2difficulty(bits): + return target2difficulty(bits2target(bits)) +def difficulty2target(difficulty): + return int(0x00000000FFFF0000000000000000000000000000000000000000000000000000 / difficulty) def rh2s(tthash): return hexlify(tthash[::-1]).decode()