关于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 *Node
merkleRoot []byte
Leafs []*Node
hashStrategy 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 *MerkleTree
Parent *Node
Left *Node
Right *Node
leaf bool
dup bool
Hash []byte
C Content
}
二、核心方法
//

本文介绍了一种纯内存实现的Merkle树,包括其结构定义及核心构建方法。通过递归构建中间节点,最终形成完整的Merkle树结构。
最低0.47元/天 解锁文章
662

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



