9、区块链技术:原理、特性与共识机制解析

区块链技术:原理、特性与共识机制解析

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 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值