区块链技术:原理、特性与共识机制解析
1. Merkle树代码示例
本部分给出了一个最基础的Merkle树的Python代码示例,不同语言的实现思路类似,只是需要找到合适的库函数。以下是具体代码:
# -*- coding: utf-8 -*-
from hashlib import sha256
class MerkelTree(object):
def __init__(self):
pass
def chunks(self, transaction, n):
# This function yeilds "n" number of transaction at time
for i in range(0, len(transaction), number):
yield transaction[i:i+2]
def merkel_tree(self, transactions):
# Here we will find the merkel tree hash of all
# transactions passed to this fuction
# Problem is solved using recursion techqiue
# Given a list of transactions, we concatinate the
# hashes in groups of two and compute
# the hash
超级会员免费看
订阅专栏 解锁全文
1192

被折叠的 条评论
为什么被折叠?



