def get_merkle(self, tx_hash, height): block_hash = self.bitcoind('getblockhash', [height]) b = self.bitcoind('getblock', [block_hash]) tx_list = b.get('tx') tx_pos = tx_list.index(tx_hash) merkle = map(hash_decode, tx_list) target_hash = hash_decode(tx_hash) s = [] while len(merkle) != 1: if len(merkle)%2: merkle.append( merkle[-1] ) n = [] while merkle: new_hash = Hash( merkle[0] + merkle[1] ) if merkle[0] == target_hash: s.append( hash_encode( merkle[1])) target_hash = new_hash elif merkle[1] == target_hash: s.append( hash_encode( merkle[0])) target_hash = new_hash n.append( new_hash ) merkle = merkle[2:] merkle = n return {"block_height":height, "merkle":s, "pos":tx_pos}
def deserialize_block(self, block): txlist = block.get('tx') tx_hashes = [] # ordered txids txdict = {} # deserialized tx is_coinbase = True for raw_tx in txlist: tx_hash = hash_encode(Hash(raw_tx.decode('hex'))) tx_hashes.append(tx_hash) vds = deserialize.BCDataStream() vds.write(raw_tx.decode('hex')) tx = deserialize.parse_Transaction(vds, is_coinbase) txdict[tx_hash] = tx is_coinbase = False return tx_hashes, txdict