#!/usr/bin/python # # linearize.py: Construct a linear, no-fork, best version of the blockchain. # # # 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 ERR_SLEEP = 15 MAX_NONCE = 1000000L 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 getblock(rpc, settings, n): hash = rpc.getblockhash(n) hexdata = rpc.getblock(hash, False) data = hexdata.decode('hex') return data def get_blocks(settings): rpc = BitcoinRPC(settings['host'], settings['port'], settings['rpcuser'], settings['rpcpassword']) outf = open(settings['output'], 'ab') for height in xrange(settings['min_height'], settings['max_height']+1): data = getblock(rpc, settings, height) outhdr = settings['netmagic'] outhdr += struct.pack("