2019-12-25 08:28:59 +00:00
|
|
|
package hash
|
2018-03-25 10:45:54 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2018-03-25 10:45:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// MerkleTree implementation.
|
|
|
|
type MerkleTree struct {
|
|
|
|
root *MerkleTreeNode
|
|
|
|
depth int
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMerkleTree returns new MerkleTree object.
|
|
|
|
func NewMerkleTree(hashes []util.Uint256) (*MerkleTree, error) {
|
|
|
|
if len(hashes) == 0 {
|
|
|
|
return nil, errors.New("length of the hashes cannot be zero")
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes := make([]*MerkleTreeNode, len(hashes))
|
|
|
|
for i := 0; i < len(hashes); i++ {
|
|
|
|
nodes[i] = &MerkleTreeNode{
|
|
|
|
hash: hashes[i],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &MerkleTree{
|
2019-12-25 11:38:48 +00:00
|
|
|
root: buildMerkleTree(nodes),
|
2018-03-25 10:45:54 +00:00
|
|
|
depth: 1,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// Root returns the computed root hash of the MerkleTree.
|
2018-03-25 10:45:54 +00:00
|
|
|
func (t *MerkleTree) Root() util.Uint256 {
|
|
|
|
return t.root.hash
|
|
|
|
}
|
|
|
|
|
2019-12-25 11:38:48 +00:00
|
|
|
func buildMerkleTree(leaves []*MerkleTreeNode) *MerkleTreeNode {
|
2018-03-25 10:45:54 +00:00
|
|
|
if len(leaves) == 0 {
|
2019-12-25 11:38:48 +00:00
|
|
|
panic("length of leaves cannot be zero")
|
2018-03-25 10:45:54 +00:00
|
|
|
}
|
|
|
|
if len(leaves) == 1 {
|
2019-12-25 11:38:48 +00:00
|
|
|
return leaves[0]
|
2018-03-25 10:45:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
parents := make([]*MerkleTreeNode, (len(leaves)+1)/2)
|
|
|
|
for i := 0; i < len(parents); i++ {
|
|
|
|
parents[i] = &MerkleTreeNode{}
|
|
|
|
parents[i].leftChild = leaves[i*2]
|
|
|
|
leaves[i*2].parent = parents[i]
|
|
|
|
|
|
|
|
if i*2+1 == len(leaves) {
|
|
|
|
parents[i].rightChild = parents[i].leftChild
|
|
|
|
} else {
|
|
|
|
parents[i].rightChild = leaves[i*2+1]
|
|
|
|
leaves[i*2+1].parent = parents[i]
|
|
|
|
}
|
|
|
|
|
2019-11-27 09:23:18 +00:00
|
|
|
b1 := parents[i].leftChild.hash.BytesBE()
|
|
|
|
b2 := parents[i].rightChild.hash.BytesBE()
|
2018-03-25 10:45:54 +00:00
|
|
|
b1 = append(b1, b2...)
|
2019-12-25 08:28:59 +00:00
|
|
|
parents[i].hash = DoubleSha256(b1)
|
2018-03-25 10:45:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return buildMerkleTree(parents)
|
|
|
|
}
|
|
|
|
|
2020-09-15 15:38:15 +00:00
|
|
|
// CalcMerkleRoot calculcates Merkle root hash value for a given slice of hashes.
|
|
|
|
// It doesn't create a full MerkleTree structure and it uses given slice as a
|
|
|
|
// scratchpad, so it will destroy its contents in the process. But it's much more
|
|
|
|
// memory efficient if you only need root hash value, while NewMerkleTree would
|
|
|
|
// make 3*N allocations for N hashes, this function will only make 4. It also is
|
|
|
|
// an error to call this function for zero-length hashes slice, the function will
|
|
|
|
// panic.
|
|
|
|
func CalcMerkleRoot(hashes []util.Uint256) util.Uint256 {
|
|
|
|
if len(hashes) == 0 {
|
2021-03-01 12:20:27 +00:00
|
|
|
return util.Uint256{}
|
2020-09-15 15:38:15 +00:00
|
|
|
}
|
|
|
|
if len(hashes) == 1 {
|
|
|
|
return hashes[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
scratch := make([]byte, 64)
|
|
|
|
parents := hashes[:(len(hashes)+1)/2]
|
|
|
|
for i := 0; i < len(parents); i++ {
|
|
|
|
copy(scratch, hashes[i*2].BytesBE())
|
|
|
|
|
|
|
|
if i*2+1 == len(hashes) {
|
|
|
|
copy(scratch[32:], hashes[i*2].BytesBE())
|
|
|
|
} else {
|
|
|
|
copy(scratch[32:], hashes[i*2+1].BytesBE())
|
|
|
|
}
|
|
|
|
|
|
|
|
parents[i] = DoubleSha256(scratch)
|
|
|
|
}
|
|
|
|
|
|
|
|
return CalcMerkleRoot(parents)
|
|
|
|
}
|
|
|
|
|
2018-03-25 10:45:54 +00:00
|
|
|
// MerkleTreeNode represents a node in the MerkleTree.
|
|
|
|
type MerkleTreeNode struct {
|
|
|
|
hash util.Uint256
|
|
|
|
parent *MerkleTreeNode
|
|
|
|
leftChild *MerkleTreeNode
|
|
|
|
rightChild *MerkleTreeNode
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsLeaf returns whether this node is a leaf node or not.
|
|
|
|
func (n *MerkleTreeNode) IsLeaf() bool {
|
|
|
|
return n.leftChild == nil && n.rightChild == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsRoot returns whether this node is a root node or not.
|
|
|
|
func (n *MerkleTreeNode) IsRoot() bool {
|
|
|
|
return n.parent == nil
|
|
|
|
}
|