diff options
author | Jeff Garzik <jgarzik@bitpay.com> | 2014-08-23 23:32:56 -0400 |
---|---|---|
committer | Jeff Garzik <jgarzik@bitpay.com> | 2014-08-23 23:32:56 -0400 |
commit | 087eb43cff13e3ba3cc34f047acaab861f723204 (patch) | |
tree | a49b8badd7e9ec348ab73753f8b09238cee38028 /contrib/linearize/linearize-hashes.py | |
parent | 5cd00bc8cb6820d3b41bea329fcf0c26c03db64a (diff) | |
parent | b4a72a75b4795dffbe973b0ae287343df2dac13c (diff) | |
download | bitcoin-087eb43cff13e3ba3cc34f047acaab861f723204.tar.xz |
Merge pull request #4757
Diffstat (limited to 'contrib/linearize/linearize-hashes.py')
-rwxr-xr-x | contrib/linearize/linearize-hashes.py | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/contrib/linearize/linearize-hashes.py b/contrib/linearize/linearize-hashes.py new file mode 100755 index 0000000000..791b71bc33 --- /dev/null +++ b/contrib/linearize/linearize-hashes.py @@ -0,0 +1,106 @@ +#!/usr/bin/python +# +# linearize-hashes.py: List blocks in a linear, no-fork version of the chain. +# +# Copyright (c) 2013 The Bitcoin developers +# Distributed under the MIT/X11 software license, see the accompanying +# file COPYING or http://www.opensource.org/licenses/mit-license.php. +# + +import json +import struct +import re +import base64 +import httplib +import sys + +settings = {} + +class BitcoinRPC: + OBJID = 1 + + 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) + def rpc(self, method, params=None): + self.OBJID += 1 + obj = { 'version' : '1.1', + 'method' : method, + 'id' : self.OBJID } + if params is None: + obj['params'] = [] + else: + obj['params'] = params + self.conn.request('POST', '/', json.dumps(obj), + { 'Authorization' : self.authhdr, + 'Content-type' : 'application/json' }) + + resp = self.conn.getresponse() + if resp is None: + print "JSON-RPC: no response" + return None + + body = resp.read() + resp_obj = json.loads(body) + if resp_obj is None: + print "JSON-RPC: cannot JSON-decode body" + return None + if 'error' in resp_obj and resp_obj['error'] != None: + return resp_obj['error'] + if 'result' not in resp_obj: + print "JSON-RPC: no result in object" + return None + + return resp_obj['result'] + def getblock(self, hash, verbose=True): + return self.rpc('getblock', [hash, verbose]) + def getblockhash(self, index): + return self.rpc('getblockhash', [index]) + +def get_block_hashes(settings): + rpc = BitcoinRPC(settings['host'], settings['port'], + settings['rpcuser'], settings['rpcpassword']) + + for height in xrange(settings['min_height'], settings['max_height']+1): + hash = rpc.getblockhash(height) + + print(hash) + +if __name__ == '__main__': + if len(sys.argv) != 2: + print "Usage: linearize-hashes.py CONFIG-FILE" + 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: + settings['port'] = 8332 + if 'min_height' not in settings: + settings['min_height'] = 0 + if 'max_height' not in settings: + settings['max_height'] = 313000 + if 'rpcuser' not in settings or 'rpcpassword' not in settings: + print "Missing username and/or password in cfg file" + sys.exit(1) + + settings['port'] = int(settings['port']) + settings['min_height'] = int(settings['min_height']) + settings['max_height'] = int(settings['max_height']) + + get_block_hashes(settings) + |