10 from __future__
import print_function
21 def __init__(self, host, port, username, password):
22 authpair =
"%s:%s" % (username, password)
23 self.
authhdr =
"Basic %s" % (base64.b64encode(authpair))
24 self.
conn = httplib.HTTPConnection(host, port,
False, 30)
27 self.
conn.request(
'POST',
'/', json.dumps(obj),
28 {
'Authorization' : self.
authhdr,
29 'Content-type' :
'application/json' })
31 resp = self.
conn.getresponse()
33 print(
"JSON-RPC: no response", file=sys.stderr)
37 resp_obj = json.loads(body)
42 obj = {
'version' :
'1.1',
48 obj[
'params'] = params
53 return 'error' in resp_obj
and resp_obj[
'error']
is not None 56 rpc =
BitcoinRPC(settings[
'host'], settings[
'port'],
57 settings[
'rpcuser'], settings[
'rpcpassword'])
59 height = settings[
'min_height']
60 while height < settings[
'max_height']+1:
61 num_blocks = min(settings[
'max_height']+1-height, max_blocks_per_call)
63 for x
in range(num_blocks):
64 batch.append(rpc.build_request(x,
'getblockhash', [height + x]))
66 reply = rpc.execute(batch)
68 for x,resp_obj
in enumerate(reply):
69 if rpc.response_is_error(resp_obj):
70 print(
'JSON-RPC: error at height', height+x,
': ', resp_obj[
'error'], file=sys.stderr)
72 assert(resp_obj[
'id'] == x)
73 print(resp_obj[
'result'])
77 if __name__ ==
'__main__':
78 if len(sys.argv) != 2:
79 print(
"Usage: linearize-hashes.py CONFIG-FILE")
85 m = re.search(
'^\s*#', line)
90 m = re.search(
'^(\w+)\s*=\s*(\S.*)$', line)
93 settings[m.group(1)] = m.group(2)
96 if 'host' not in settings:
97 settings[
'host'] =
'127.0.0.1' 98 if 'port' not in settings:
99 settings[
'port'] = 9998
100 if 'min_height' not in settings:
101 settings[
'min_height'] = 0
102 if 'max_height' not in settings:
103 settings[
'max_height'] = 313000
104 if 'rpcuser' not in settings
or 'rpcpassword' not in settings:
105 print(
"Missing username and/or password in cfg file", file=stderr)
108 settings[
'port'] = int(settings[
'port'])
109 settings[
'min_height'] = int(settings[
'min_height'])
110 settings[
'max_height'] = int(settings[
'max_height'])
def get_block_hashes(settings, max_blocks_per_call=10000)
def response_is_error(resp_obj)
def build_request(idx, method, params)
def __init__(self, host, port, username, password)