From 31dbbe0ba5280090bf733921c210608de1bed701 Mon Sep 17 00:00:00 2001 From: "John L. Jegutanis" Date: Tue, 14 Aug 2018 00:26:13 +0200 Subject: [PATCH] Use precompiled structs for header parsing --- electrumx/lib/coins.py | 205 ++++++------------ tests/blocks/newyorkcoin_mainnet_3956926.json | 2 +- tests/test_blocks.py | 8 + 3 files changed, 79 insertions(+), 136 deletions(-) diff --git a/electrumx/lib/coins.py b/electrumx/lib/coins.py index 905b423..fcc3141 100644 --- a/electrumx/lib/coins.py +++ b/electrumx/lib/coins.py @@ -70,6 +70,9 @@ class Coin(object): DESERIALIZER = lib_tx.Deserializer DAEMON = daemon.Daemon BLOCK_PROCESSOR = block_proc.BlockProcessor + HEADER_VALUES = ('version', 'prev_block_hash', 'merkle_root', 'timestamp', + 'bits', 'nonce') + HEADER_UNPACK = struct.Struct('< I 32s 32s I I I').unpack_from MEMPOOL_HISTOGRAM_REFRESH_SECS = 500 XPUB_VERBYTES = bytes('????', 'utf-8') XPRV_VERBYTES = bytes('????', 'utf-8') @@ -296,18 +299,13 @@ class Coin(object): @classmethod def electrum_header(cls, header, height): - version, = struct.unpack(' 6: return super().header_hash(header) else: @@ -475,8 +473,7 @@ class BitcoinGold(EquihashMixin, BitcoinMixin, Coin): @classmethod def header_hash(cls, header): '''Given a header return hash''' - height, = struct.unpack('= cls.FORK_HEIGHT: return double_sha256(header) else: @@ -484,18 +481,9 @@ class BitcoinGold(EquihashMixin, BitcoinMixin, Coin): @classmethod def electrum_header(cls, header, height): - h = dict( - block_height=height, - version=struct.unpack(' 1: + h['nAccumulatorCheckpoint'] = hash_to_hex_str(header[80:]) + return h class Pac(Coin): @@ -1998,35 +1947,7 @@ class Monoeci(Coin): return x11_hash.getPoWHash(header) -class MinexcoinMixin(object): - STATIC_BLOCK_HEADERS = True - BASIC_HEADER_SIZE = 209 - DESERIALIZER = lib_tx.DeserializerEquihash - - @classmethod - def electrum_header(cls, header, height): - version, = struct.unpack('= 4: return super().header_hash(header) else: diff --git a/tests/blocks/newyorkcoin_mainnet_3956926.json b/tests/blocks/newyorkcoin_mainnet_3956926.json index 268f39d..ef29bad 100644 --- a/tests/blocks/newyorkcoin_mainnet_3956926.json +++ b/tests/blocks/newyorkcoin_mainnet_3956926.json @@ -7,7 +7,7 @@ "0300b2fe16c049641ec63f9eb435b9d773c847bc0185c927d5b8d87cd1ad3095" ], "time": 1515308763, - "nonce": "1570457681", + "nonce": 1570457681, "bits": "1b172a4e", "previousblockhash": "10c91aefd6698f03a2820b8aa462738a5c1c21f4f975786a8eb1ba25e4b33af3", "block": "01000000f33ab3e425bab18e6a7875f9f4211c5c8a7362a48a0b82a2038f69d6ef1ac9109530add17cd8b8d527c98501bc47c873d7b935b49e3fc61e6449c016feb20003dbc6515a4e2a171b51489b5d0101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff5203be603c062f503253482f04dfc6515afabe6d6dce67985c41a5f1ba5d526614e1744010b5e6a34d33c50ece5d420d75da15454e100000000000000008b8000274020000000c2f30324d515156434c59582f00000000010010a5d4e80000001976a914f0a150ec5709fae1d1814227b69cd1f0baf528c588ac00000000" diff --git a/tests/test_blocks.py b/tests/test_blocks.py index d82dc5b..c8803d6 100644 --- a/tests/test_blocks.py +++ b/tests/test_blocks.py @@ -32,6 +32,7 @@ import pytest from electrumx.lib.coins import Coin from electrumx.lib.hash import hex_str_to_hash +from electrumx.lib.util import pack_be_uint32 BLOCKS_DIR = os.path.join( os.path.dirname(os.path.realpath(__file__)), 'blocks') @@ -60,6 +61,13 @@ def test_block(block_details): raw_block = unhexlify(block_info['block']) block = coin.block(raw_block, block_info['height']) + h = coin.electrum_header(block.header, block_info['height']) + assert block_info['merkleroot'] == h['merkle_root'] + assert block_info['time'] == h['timestamp'] + assert block_info['previousblockhash'] == h['prev_block_hash'] + assert block_info['height'] == h['block_height'] + assert block_info['nonce'] == h['nonce'] + assert block_info['bits'] == pack_be_uint32(h['bits']).hex() assert coin.header_hash(block.header) == hex_str_to_hash(block_info['hash']) assert (coin.header_prevhash(block.header)