2013-08-30 13:37:26 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
#
|
2014-08-24 03:46:12 +02:00
|
|
|
# linearize-hashes.py: List blocks in a linear, no-fork version of the chain.
|
2013-08-30 13:41:29 +02:00
|
|
|
#
|
2014-12-17 02:47:57 +01:00
|
|
|
# Copyright (c) 2013-2014 The Bitcoin Core developers
|
2014-12-13 05:09:33 +01:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
2013-08-30 13:37:26 +02:00
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
#
|
|
|
|
|
2014-10-06 13:23:27 +02:00
|
|
|
from __future__ import print_function
|
2013-08-30 13:37:26 +02:00
|
|
|
import json
|
|
|
|
import struct
|
|
|
|
import re
|
|
|
|
import base64
|
|
|
|
import httplib
|
|
|
|
import sys
|
|
|
|
|
|
|
|
settings = {}
|
|
|
|
|
|
|
|
class BitcoinRPC:
|
|
|
|
def __init__(self, host, port, username, password):
|
|
|
|
authpair = "%s:%s" % (username, password)
|
|
|
|
self.authhdr = "Basic %s" % (base64.b64encode(authpair))
|
|
|
|
self.conn = httplib.HTTPConnection(host, port, False, 30)
|
2014-10-06 13:23:27 +02:00
|
|
|
|
|
|
|
def execute(self, obj):
|
2013-08-30 13:37:26 +02:00
|
|
|
self.conn.request('POST', '/', json.dumps(obj),
|
|
|
|
{ 'Authorization' : self.authhdr,
|
|
|
|
'Content-type' : 'application/json' })
|
|
|
|
|
|
|
|
resp = self.conn.getresponse()
|
|
|
|
if resp is None:
|
2014-10-06 13:23:27 +02:00
|
|
|
print("JSON-RPC: no response", file=sys.stderr)
|
2013-08-30 13:37:26 +02:00
|
|
|
return None
|
|
|
|
|
|
|
|
body = resp.read()
|
|
|
|
resp_obj = json.loads(body)
|
2014-10-06 13:23:27 +02:00
|
|
|
return resp_obj
|
2013-08-30 13:37:26 +02:00
|
|
|
|
2014-10-06 13:23:27 +02:00
|
|
|
@staticmethod
|
|
|
|
def build_request(idx, method, params):
|
|
|
|
obj = { 'version' : '1.1',
|
|
|
|
'method' : method,
|
|
|
|
'id' : idx }
|
|
|
|
if params is None:
|
|
|
|
obj['params'] = []
|
|
|
|
else:
|
|
|
|
obj['params'] = params
|
|
|
|
return obj
|
2013-08-30 13:37:26 +02:00
|
|
|
|
2014-10-06 13:23:27 +02:00
|
|
|
@staticmethod
|
|
|
|
def response_is_error(resp_obj):
|
|
|
|
return 'error' in resp_obj and resp_obj['error'] is not None
|
2013-08-30 13:37:26 +02:00
|
|
|
|
2014-10-06 13:23:27 +02:00
|
|
|
def get_block_hashes(settings, max_blocks_per_call=10000):
|
2013-08-30 13:37:26 +02:00
|
|
|
rpc = BitcoinRPC(settings['host'], settings['port'],
|
2014-02-10 15:52:31 +01:00
|
|
|
settings['rpcuser'], settings['rpcpassword'])
|
2013-08-30 13:37:26 +02:00
|
|
|
|
2014-10-06 13:23:27 +02:00
|
|
|
height = settings['min_height']
|
|
|
|
while height < settings['max_height']+1:
|
|
|
|
num_blocks = min(settings['max_height']+1-height, max_blocks_per_call)
|
|
|
|
batch = []
|
|
|
|
for x in range(num_blocks):
|
|
|
|
batch.append(rpc.build_request(x, 'getblockhash', [height + x]))
|
2013-08-30 13:37:26 +02:00
|
|
|
|
2014-10-06 13:23:27 +02:00
|
|
|
reply = rpc.execute(batch)
|
2013-08-30 13:37:26 +02:00
|
|
|
|
2014-10-06 13:23:27 +02:00
|
|
|
for x,resp_obj in enumerate(reply):
|
|
|
|
if rpc.response_is_error(resp_obj):
|
|
|
|
print('JSON-RPC: error at height', height+x, ': ', resp_obj['error'], file=sys.stderr)
|
|
|
|
exit(1)
|
|
|
|
assert(resp_obj['id'] == x) # assume replies are in-sequence
|
|
|
|
print(resp_obj['result'])
|
2013-08-30 13:37:26 +02:00
|
|
|
|
2014-10-06 13:23:27 +02:00
|
|
|
height += num_blocks
|
2013-08-30 13:37:26 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
if len(sys.argv) != 2:
|
2014-10-06 13:23:27 +02:00
|
|
|
print("Usage: linearize-hashes.py CONFIG-FILE")
|
2013-08-30 13:37:26 +02:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
f = open(sys.argv[1])
|
|
|
|
for line in f:
|
|
|
|
# skip comment lines
|
|
|
|
m = re.search('^\s*#', line)
|
|
|
|
if m:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# parse key=value lines
|
|
|
|
m = re.search('^(\w+)\s*=\s*(\S.*)$', line)
|
|
|
|
if m is None:
|
|
|
|
continue
|
|
|
|
settings[m.group(1)] = m.group(2)
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
if 'host' not in settings:
|
|
|
|
settings['host'] = '127.0.0.1'
|
|
|
|
if 'port' not in settings:
|
2024-02-05 23:00:22 +01:00
|
|
|
settings['port'] = 11426
|
2014-01-12 12:48:39 +01:00
|
|
|
if 'min_height' not in settings:
|
|
|
|
settings['min_height'] = 0
|
2013-08-30 13:37:26 +02:00
|
|
|
if 'max_height' not in settings:
|
2014-08-24 03:46:12 +02:00
|
|
|
settings['max_height'] = 313000
|
2014-02-10 15:52:31 +01:00
|
|
|
if 'rpcuser' not in settings or 'rpcpassword' not in settings:
|
2014-10-06 13:23:27 +02:00
|
|
|
print("Missing username and/or password in cfg file", file=stderr)
|
2013-08-30 13:37:26 +02:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
settings['port'] = int(settings['port'])
|
2014-01-12 12:48:39 +01:00
|
|
|
settings['min_height'] = int(settings['min_height'])
|
2013-08-30 13:37:26 +02:00
|
|
|
settings['max_height'] = int(settings['max_height'])
|
|
|
|
|
2014-08-24 03:46:12 +02:00
|
|
|
get_block_hashes(settings)
|
2013-08-30 13:37:26 +02:00
|
|
|
|