def block_to_merkle(block_outkeys): '''Takes in the outkeys that all belong to the same block (by block hash, we can also do height) and then builds a Merkle Tree. It also updates the client side block_root_hash dictionary and the server side block_merkle dictionary ''' # change to # for block_hash, tx_hash, outkey, idx in block_outkeys block_merkle_leaves = [] block_hash = block_outkeys[0][0] assert all(bhash == block_hash for bhash, _, _, _ in block_outkeys) while block_outkeys: curr_tx_hash = block_outkeys[0][1] tx_outkeys = [] while block_outkeys[0][1] == curr_tx_hash: tx_outkeys.append(block_outkeys.pop(0)) if not block_outkeys: break block_merkle_leaves.append(tx_to_merkle(tx_outkeys)) block_merkle = MerkleTree(leaves=block_merkle_leaves) block_merkle.build() merkle_forest[codecs.encode(block_merkle.root.val, 'hex_codec')] = block_merkle return (codecs.encode(block_merkle.root.val, 'hex_codec'), block_merkle.root.idx)
def scan_over(): top_merkle_leaves = [] while testblocks: top_merkle_leaves.append(block_to_merkle(testblocks.pop(0))) top_merkle = MerkleTree(leaves=top_merkle_leaves) top_merkle.build() return top_merkle
def main(): for i in range(0, 1000, 5): # generate a block outputs = [] for j in range(0, 5): outkey = id_generator() output = (outkey, i + j) outputs.append(output) newtree = MerkleTree(leaves=outputs) newtree.build() k = newtree.root root = (codecs.encode(k.val, 'hex_codec'), k.idx) blocks[k.idx] = newtree block_headers[k.idx] = root block_headers_keys = block_headers.keys() blocks_keys = blocks.keys() #Run a test 100 times correct = incorrect = 0 for _ in range(0, 100): c = randint(0, 999) #The client will make a request using c, and collect the ground truth d = find_nearest_above(block_headers_keys, c) truth = block_headers[d] e = find_nearest_above(blocks_keys, c) tree = blocks[e] avail = [leaf.idx for leaf in tree.leaves] chosen = avail.index(e) proof = tree.get_proof(chosen) data = tree.leaves[chosen].data print "Chosen index: " + str(c) + ', Returned data: ' + str(data) #check if the data returned is hashed to the first element in the proof if hash_function(data).hexdigest() == proof[0][0][0]: #check the Merkle proof if check_proof(proof) == truth[0]: correct += 1 else: incorrect += 1 else: incorrect += 1 total = correct + incorrect print "Number Correct: " + str(correct) + '/' + str(total) print "Number Incorrect: " + str(incorrect) + '/' + str(total)
def tx_to_merkle(tx_outkeys): '''Takes in the outkeys that all belong to the same transaction (by transaction hash) and builds a Merkle Tree. It also updates the client side tx_root_hash dictionary and the server side tx_dict dictionary''' tx_hash = tx_outkeys[0][1] assert all(t_hash == tx_hash for _, t_hash, _, _ in tx_outkeys) tx_merkle_leaves = [(outkey, idx) for _, _, outkey, idx in tx_outkeys] tx_merkle = MerkleTree(leaves=tx_merkle_leaves) tx_merkle.build() merkle_forest[codecs.encode(tx_merkle.root.val, 'hex_codec')] = tx_merkle return (codecs.encode(tx_merkle.root.val, 'hex_codec'), tx_merkle.root.idx)
def merkle_tree(transactions): mt = MerkleTree() for t in transactions: mt.add(t.encode('utf-8')) return codecs.encode(mt.build(), 'hex-codec').decode('utf-8')
def block_to_merkle(blk): tx_leaves = [] if isinstance(blk, Block): for tx in blk.transactions: newtree = tx_to_merkle(tx) k = newtree.root tx_dict[tx.tx_hash] = newtree tx_root_hash[tx.tx_hash] = codecs.encode(k.val, 'hex_codec') tx_leaves.append((tx.tx_hash, k.idx)) blk_merkle_tree = MerkleTree(leaves=tx_leaves) blk_merkle_tree.build() blk_root = blk_merkle_tree.root blocks[blk.block_hash] = blk_merkle_tree block_root_hash[blk.block_hash] = codecs.encode( blk_root.val, 'hex_codec') return (blk.block_hash, blk_root.idx) else: raise TypeError("Input must be a block!")
def scan_over_new_blocks(new_blocks): '''Scan over the utxos, distinguishing new blocks We will use block hash to distinguish new blocks. The top Merkle Tree is created The client side top_root will be udpated, as well as the top_merkle ADS on the server''' top_merkle_leaves = [] while new_blocks: curr_block_hash = new_blocks[0][0] block_outkeys = [] while new_blocks[0][0] == curr_block_hash: block_outkeys.append(new_blocks.pop(0)) if not new_blocks: break top_merkle_leaves.append(block_to_merkle(block_outkeys)) global top_merkle top_merkle = MerkleTree(leaves=top_merkle_leaves) top_merkle.build() global top_root top_root = (codecs.encode(top_merkle.root.val, 'hex_codec'), top_merkle.root.idx) merkle_forest[codecs.encode(top_merkle.root.val, 'hex_codec')] = top_merkle
def calculate_merkleRoot(self): """ calcul markle tree root et le place dans le header """ mt = MerkleTree(str(self.transactionFiles)) mt.build() self.header['merkleRoot'] = mt.root.val.encode('hex')
def tx_to_merkle(tx): new_t = MerkleTree(leaves=tx.outputs) # build the tree new_t.build() return new_t