From 882e2cdb9fc5081e95c5061654c8e34b6da129ad Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Thu, 5 Jan 2017 11:19:11 +0100 Subject: [PATCH] Merge #9373: Linearize script update (hash byte reversal and Python 3 support) 3c8f63b Make linearize scripts Python 3-compatible. (Doug) d5aa198 Allow linearization scripts to support hash byte reversal (Doug) --- contrib/linearize/README.md | 46 +++++++++++++++++-------- contrib/linearize/example-linearize.cfg | 4 ++- contrib/linearize/linearize-data.py | 44 ++++++++++++++++------- contrib/linearize/linearize-hashes.py | 42 +++++++++++++++++----- 4 files changed, 99 insertions(+), 37 deletions(-) diff --git a/contrib/linearize/README.md b/contrib/linearize/README.md index 3570f65e62..10cb19c8dd 100644 --- a/contrib/linearize/README.md +++ b/contrib/linearize/README.md @@ -1,5 +1,6 @@ # Linearize -Construct a linear, no-fork, best version of the blockchain. +Construct a linear, no-fork, best version of the Dash blockchain. The scripts +run using Python 3 but are compatible with Python 2. ## Step 0: Install dash_hash @@ -10,27 +11,42 @@ https://github.com/dashpay/dash_hash $ ./linearize-hashes.py linearize.cfg > hashlist.txt Required configuration file settings for linearize-hashes: -* RPC: rpcuser, rpcpassword +* RPC: `rpcuser`, `rpcpassword` Optional config file setting for linearize-hashes: -* RPC: host, port -* Block chain: min_height, max_height +* RPC: `host` (Default: `127.0.0.1`) +* RPC: `port` (Default: `9998`) +* Blockchain: `min_height`, `max_height` +* `rev_hash_bytes`: If true, the written block hash list will be +byte-reversed. (In other words, the hash returned by getblockhash will have its +bytes reversed.) False by default. Intended for generation of +standalone hash lists but safe to use with linearize-data.py, which will output +the same data no matter which byte format is chosen. + +The `linearize-hashes` script requires a connection, local or remote, to a +JSON-RPC server. Running `bitcoind` or `bitcoin-qt -server` will be sufficient. ## Step 2: Copy local block data $ ./linearize-data.py linearize.cfg Required configuration file settings: -* "input": bitcoind blocks/ directory containing blkNNNNN.dat -* "hashlist": text file containing list of block hashes, linearized-hashes.py -output. -* "output_file" for bootstrap.dat or "output" for output directory for linearized blocks/blkNNNNN.dat output +* `output_file`: The file that will contain the final blockchain. + or +* `output`: Output directory for linearized `blocks/blkNNNNN.dat` output. Optional config file setting for linearize-data: -* "netmagic": network magic number (default is 'cee2caff', testnet) -* "genesis": genesis block hash (default is '00000bafbc94add76cb75e2ec92894837288a481e5c005f6563d91623bf8bc2c', testnet) -* "max_out_sz": maximum output file size (default 100 \* 1000 \* 1000) -* "split_timestamp": Split files when a new month is first seen, in addition to -reaching a maximum file size. -* "file_timestamp": Set each file's last-modified time to that of the -most recent block in that file. +* `file_timestamp`: Set each file's last-modified time to that of the most +recent block in that file. +* `genesis`: The hash of the genesis block in the blockchain. (default is '00000bafbc94add76cb75e2ec92894837288a481e5c005f6563d91623bf8bc2c', testnet) +* `input`: bitcoind blocks/ directory containing blkNNNNN.dat +* `hashlist`: text file containing list of block hashes created by +linearize-hashes.py. +* `max_out_sz`: Maximum size for files created by the `output_file` option. +(Default: `1000*1000*1000 bytes`) +* `netmagic`: Network magic number. (default is 'cee2caff', testnet) +* `rev_hash_bytes`: If true, the block hash list written by linearize-hashes.py +will be byte-reversed when read by linearize-data.py. See the linearize-hashes +entry for more information. +* `split_timestamp`: Split blockchain files when a new month is first seen, in +addition to reaching a maximum file size (`max_out_sz`). diff --git a/contrib/linearize/example-linearize.cfg b/contrib/linearize/example-linearize.cfg index 09a66dc74e..d8fe8a55f2 100644 --- a/contrib/linearize/example-linearize.cfg +++ b/contrib/linearize/example-linearize.cfg @@ -13,8 +13,10 @@ netmagic=bf0c6bbd input=/home/example/.dashcore/blocks output_file=/home/example/Downloads/bootstrap.dat hashlist=hashlist.txt -split_year=1 genesis=00000ffd590b1485b3caadc19b22e6379c733355108f107a430458cdf3407ab6 # Maxmimum size in bytes of out-of-order blocks cache in memory out_of_order_cache_sz = 10000000 + +# Do we want the reverse the hash bytes coming from getblockhash? +rev_hash_bytes = False diff --git a/contrib/linearize/linearize-data.py b/contrib/linearize/linearize-data.py index d57c59904d..c0fca0426b 100755 --- a/contrib/linearize/linearize-data.py +++ b/contrib/linearize/linearize-data.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # linearize-data.py: Construct a linear, no-fork version of the chain. # @@ -8,24 +8,34 @@ # from __future__ import print_function, division +try: # Python 3 + import http.client as httplib +except ImportError: # Python 2 + import httplib import json import struct import re import os import os.path import base64 -import httplib import sys import hashlib import dash_hash import datetime import time from collections import namedtuple +from binascii import hexlify, unhexlify settings = {} +##### Switch endian-ness ##### +def hex_switchEndian(s): + """ Switches the endianness of a hex string (in pairs of hex chars) """ + pairList = [s[i:i+2].encode() for i in range(0, len(s), 2)] + return b''.join(pairList[::-1]).decode() + def uint32(x): - return x & 0xffffffffL + return x & 0xffffffff def bytereverse(x): return uint32(( ((x) << 24) | (((x) << 8) & 0x00ff0000) | @@ -36,14 +46,14 @@ def bufreverse(in_buf): for i in range(0, len(in_buf), 4): word = struct.unpack('@I', in_buf[i:i+4])[0] out_words.append(struct.pack('@I', bytereverse(word))) - return ''.join(out_words) + return b''.join(out_words) def wordreverse(in_buf): out_words = [] for i in range(0, len(in_buf), 4): out_words.append(in_buf[i:i+4]) out_words.reverse() - return ''.join(out_words) + return b''.join(out_words) def calc_hdr_hash(blk_hdr): #hash1 = hashlib.sha256() @@ -62,7 +72,7 @@ def calc_hash_str(blk_hdr): hash = calc_hdr_hash(blk_hdr) hash = bufreverse(hash) hash = wordreverse(hash) - hash_str = hash.encode('hex') + hash_str = hexlify(hash).decode('utf-8') return hash_str def get_blk_dt(blk_hdr): @@ -72,17 +82,21 @@ def get_blk_dt(blk_hdr): dt_ym = datetime.datetime(dt.year, dt.month, 1) return (dt_ym, nTime) +# When getting the list of block hashes, undo any byte reversals. def get_block_hashes(settings): blkindex = [] f = open(settings['hashlist'], "r") for line in f: line = line.rstrip() + if settings['rev_hash_bytes'] == 'true': + line = hex_switchEndian(line) blkindex.append(line) print("Read " + str(len(blkindex)) + " hashes") return blkindex +# The block map shouldn't give or receive byte-reversed hashes. def mkblockmap(blkindex): blkmap = {} for height,hash in enumerate(blkindex): @@ -210,7 +224,7 @@ class BlockDataCopier: inMagic = inhdr[:4] if (inMagic != self.settings['netmagic']): - print("Invalid magic: " + inMagic.encode('hex')) + print("Invalid magic: " + hexlify(inMagic).decode('utf-8')) return inLenLE = inhdr[4:] su = struct.unpack("