From e6fd8cc0f20da457563892dd5f114f9836f4b209 Mon Sep 17 00:00:00 2001 From: Lex Berezhny Date: Wed, 15 Aug 2018 15:23:00 -0400 Subject: [PATCH] wip new header stuff --- lbrynet/wallet/header.py | 83 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 lbrynet/wallet/header.py diff --git a/lbrynet/wallet/header.py b/lbrynet/wallet/header.py new file mode 100644 index 000000000..d1e37c723 --- /dev/null +++ b/lbrynet/wallet/header.py @@ -0,0 +1,83 @@ +import struct +from typing import Optional +from binascii import hexlify, unhexlify + +from torba.baseheader import BaseHeaders, ArithUint256 +from torba.hash import sha512, double_sha256, ripemd160 + + +class Headers(BaseHeaders): + + header_size = 112 + chunk_size = 10**16 + + max_target = 0x0000ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + genesis_hash = b'9c89283ba0f3227f6c03b70216b9f665f0118d5e0fa729cedf4fb34d6a34f463' + target_timespan = 150 + + @property + def claim_trie_root(self): + return self[self.height]['claim_trie_root'] + + @staticmethod + def serialize(header): + return b''.join([ + struct.pack(' ArithUint256: + # https://github.com/lbryio/lbrycrd/blob/master/src/lbry.cpp + if previous is None and current is None: + return max_target + if previous is None: + previous = current + actual_timespan = current['timestamp'] - previous['timestamp'] + modulated_timespan = self.target_timespan + int((actual_timespan - self.target_timespan) / 8) + minimum_timespan = self.target_timespan - int(self.target_timespan / 8) # 150 - 18 = 132 + maximum_timespan = self.target_timespan + int(self.target_timespan / 2) # 150 + 75 = 225 + clamped_timespan = max(minimum_timespan, min(modulated_timespan, maximum_timespan)) + target = ArithUint256.from_compact(current['bits']) + new_target = min(max_target, (target * clamped_timespan) / self.target_timespan) + return new_target + + @classmethod + def get_proof_of_work(cls, header_hash: bytes): + return super().get_proof_of_work( + cls.header_hash_to_pow_hash(header_hash) + ) + + @staticmethod + def header_hash_to_pow_hash(header_hash: bytes): + header_hash_bytes = unhexlify(header_hash)[::-1] + h = sha512(header_hash_bytes) + pow_hash = double_sha256( + ripemd160(h[:len(h) // 2]) + + ripemd160(h[len(h) // 2:]) + ) + return hexlify(pow_hash[::-1]) + + +class UnvalidatedHeaders(Headers): + validate_difficulty = False + max_target = 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff + genesis_hash = b'6e3fcf1299d4ec5d79c3a4c91d624a4acf9e2e173d95a1a0504f677669687556'