def fetch_block(rpc, h, check): reply = rpc.execute([rpc.build_request(0, 'getblock', [h, False])]); for resp_obj in reply: if rpc.response_is_error(resp_obj): print('JSON-RPC: error at getblock', file=sys.stderr) exit(1) hexblock = resp_obj['result'] block = check(hexblock) return hashStr(block.blockHeader.previousHash)
def sample_block_parser(): blkfile = '../Library/Application Support/Bitcoin/blocks/blk00000.dat' with open(blkfile, 'rb') as blockfile: print "Magic Number:\t %8x" % blocktools.uint4(blockfile) print "Blocksize:\t %u" % blocktools.uint4(blockfile) """Block Header""" print "Version:\t %d" % blocktools.uint4(blockfile) print "Previous Hash\t %s" % blocktools.hashStr(blocktools.hash32(blockfile)) print "Merkle Root\t %s" % blocktools.hashStr(blocktools.hash32(blockfile)) print "Time\t\t %s" % str(blocktools.time(blockfile)) print "Difficulty\t %8x" % blocktools.uint4(blockfile) print "Nonce\t\t %s" % blocktools.uint4(blockfile) print "Tx Count\t %d" % blocktools.varint(blockfile) print "Version Number\t %s" % blocktools.uint4(blockfile) print "Inputs\t\t %s" % blocktools.varint(blockfile) print "Previous Tx\t %s" % blocktools.hashStr(blocktools.hash32(blockfile)) print "Prev Index \t %d" % blocktools.uint4(blockfile) script_len = blocktools.varint(blockfile) print "Script Length\t %d" % script_len script_sig = blockfile.read(script_len) print "ScriptSig\t %s" % blocktools.hashStr(script_sig) print "ScriptSig\t %s" % blocktools.hashStr(script_sig).decode('hex') print "Seq Num\t\t %8x" % blocktools.uint4(blockfile) print "Outputs\t\t %s" % blocktools.varint(blockfile) print "Value\t\t %s" % str((blocktools.uint8(blockfile)*1.0)/100000000.00) script_len = blocktools.varint(blockfile) print "Script Length\t %d" % script_len script_pubkey = blockfile.read(script_len) print "Script Pub Key\t %s" % blocktools.hashStr(script_pubkey) print "Lock Time %8x" % blocktools.uint4(blockfile) print