默克尔树 Merkle树之Go语言实现
关于merkle树有很多实现。这里参考了一个纯内存实现,有助于理解。
项目地址 https://github.com/cbergoon/merkletree
核心分析
一、结构体
type Content interface {CalculateHash() ([]byte, error)Equals(other Content) (bool, error)
}//MerkleTree is the container for the tree. It holds a pointer to the root of the tree,
//a list of pointers to the leaf nodes, and the merkle root.
type MerkleTree struct {Root *NodemerkleRoot []byteLeafs []*NodehashStrategy func() hash.Hash
}//Node represents a node, root, or leaf in the tree. It stores pointers to its immediate
//relationships, a hash, the content stored if it is【 a leaf, and other metadata.
type Node struct {Tree *MerkleTreeParent *NodeLeft *NodeRight *Nodeleaf booldup boolHash []byteC Content
}
二、核心方法
//buildWithContent is a helper function that for a given set of Contents, generates a
//corresponding tree and returns the root node, a list of leaf nodes, and a possible error.
//Returns an error if cs contains no Contents.
// 这里先计算了所有的叶子节点的hash,然后在递归调用 buildIntermediate 来构建整个树
func buildWithContent(cs []Content, t *MerkleTree) (*Node, []*Node, error) {if len(cs) == 0 {return nil, nil, errors.New("error: cannot construct tree with no content")}var leafs []*Nodefor _, c := range cs {hash, err := c.CalculateHash()if err != nil {return nil, nil, err}leafs = append(leafs, &Node{Hash: hash,C: c,leaf: true,Tree: t,})}if len(leafs)%2 == 1 {duplicate := &Node{Hash: leafs[len(leafs)-1].Hash,C: leafs[len(leafs)-1].C,leaf: true,dup: true,Tree: t,}leafs = append(leafs, duplicate)}root, err := buildIntermediate(leafs, t)if err != nil {return nil, nil, err}return root, leafs, nil
}//buildIntermediate is a helper function that for a given list of leaf nodes, constructs
//the intermediate and root levels of the tree. Returns the resulting root node of the tree.
func buildIntermediate(nl []*Node, t *MerkleTree) (*Node, error) {var nodes []*Nodefor i := 0; i < len(nl); i += 2 {h := t.hashStrategy()var left, right int = i, i + 1if i+1 == len(nl) {right = i}chash := append(nl[left].Hash, nl[right].Hash...)if _, err := h.Write(chash); err != nil {return nil, err}n := &Node{Left: nl[left],Right: nl[right],Hash: h.Sum(nil),Tree: t,}nodes = append(nodes, n)nl[left].Parent = nnl[right].Parent = nif len(nl) == 2 {return n, nil}}return buildIntermediate(nodes, t)
}
其他方法请参考源码实现。整理来说比较容易理解。
默克尔树 Merkle树之Go语言实现相关推荐
- 简单了解默克尔(Merkle)树
Merkle树是Ralph Merkle在1988年发明的,旨在构建更好的数字签名.原文是A DIGITAL SIGNATURE BASED ON A CONVENTIONAL ENCRYPTION ...
- 常用的数据结构_三分钟了解区块链常用数据结构「默克尔树」
免责声明:本文旨在传递更多市场信息,不构成任何投资建议.文章仅代表作者观点,不代表火星财经官方立场. 小编:记得关注哦 来源:万向区块链 原文标题:三分钟了解区块链常用数据结构「默克尔树」 默克尔树是 ...
- 6.tendermint默克尔树
默克尔树概述 在ABCI应用响应Commit请求消息时,需要计算并返回当前状态的哈希,以便Tendermint 将其打包到下一个区块头里(app_hash字段). 但是,如果我们还按原来的方法计算一个 ...
- 默克尔树_默克尔树:正在使用中
默克尔树 Ralph C. Merkle (not pictured above), born 1952, is one of the founding fathers of Public Key C ...
- tendermint 六:默克尔树
默克尔树概述 在ABCI应用响应Commit请求消息时,需要计算并返回当前状态的哈希,以便Tendermint 将其打包到下一个区块头里(app_hash字段). 但是,如果我们还按原来的方法计算一个 ...
- Merkle Tree(默克尔树)原理解析
Merkle Tree(默克尔树)原理解析 一.Merkle Tree 1.1 Merkle Tree的特点 二.Hash list 三.Merkle tree VS Hash list 四.Merk ...
- 区块链学习笔记(2)难度整定,区块形成,区块体,Merkle树,Merkle Proof默克尔证明
难度的调整 是在每个完整节点中独立自动发生的.每2016个区块,所有节点都会按统的公式自动调整难度,这个公式是由最新2016个区块的花要时长与期望时长(期望时长为20160分钟,即两周,是按每10分钟 ...
- 【区块链 | 默克尔树】使用默克尔(Merkle)树实现NFT白名单
简介 在我们今天所知道和喜爱的区块链出现之前,默克尔树一直是密码学和计算机科学领域的一个方面.如今,我们开始慢慢看到它们在链上更频繁地被用于数据验证的目的.在这篇文章中,我将解释 Merkle Tre ...
- 默克尔树(Merkle Tree)总结
目录 为什么要有默克尔树 简介 Merkle Tree的特点 图解 创建树 检索-文件夹比较 检索-防伪 更新 插入删除 应用 数字签名 P2P网络 可信计算 区块链-简单验证支付 为什么要有默克尔树 ...
- 区块链--默克尔树(Merkle Tree)
Merkle Tree 默克尔树是一种二叉树,由一个根节点.一组中间节点和一些叶子节点组成.形状如下: D0.D1.D2和D3是叶子节点包含的数据,也就是叶子节点的value.继续往上看,N0.N1. ...
最新文章
- 矩阵乘法无需相乘,速度提升100倍,MIT开源最新近似算法 | ICML 2021
- Node.js流,这样的打开方式对不对!
- 手机自动化测试:appium源码分析之bootstrap八
- docker安装jenkins并用docker部署net
- 从零开始学习Hadoop--第1章 Hadoop的安装
- 前端学习(2713):重读vue电商网站33之实现首页路由重定向
- oracle rman和数据泵,使用RMAN或数据泵初始化OGG目标库
- JS 实现 jQuery的$(function(){});
- 张孝祥《Java就业培训教程》读书笔记
- 第四届2021美团网络安全 MT-CTF writeup
- 照片变老html源码,变老教程,利用ps把年轻人变成老年人效果
- mysql查询某学期开设的课程_求各学期开设的课程门数
- 变量、函数、流程控制与游标
- 怎么样成为java架构师_java架构师工资一般多少?怎样成为架构师?
- Java高级编程学习
- SUSE12 LVM- Logical Volume Manager(逻辑卷管理)实例
- 如果编程语言也来玩儿《权利的游戏》,怎么安排角色?
- 2012年3月19日
- Fuchsia 确认支持安卓 APP;悼念杰出的内核开发者李少华
- 嵌入式C语言自我修养:从芯片、编译器到操作系统(附送书籍)