-
Notifications
You must be signed in to change notification settings - Fork 3
/
merkleTree.go
58 lines (52 loc) · 1.29 KB
/
merkleTree.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package simpleBlockchain
import (
"crypto/sha256"
)
type merkleTree struct {
root *merkleNode
}
type merkleNode struct {
left *merkleNode
right *merkleNode
hash []byte
}
func CalculateMerkleRoot(transactions []*Transaction) []byte {
var dataset [][]byte
for _, tx := range transactions {
dataset = append(dataset, tx.newHash())
}
mt := NewMerkleTree(dataset)
return mt.root.hash
}
func NewMerkleTree(dataset [][]byte) *merkleTree{
var merkleNodes []*merkleNode
if len(dataset) == 1 {
return &merkleTree{root:NewMerkleNode(nil, nil, dataset[0])}
}
if len(dataset) % 2 != 0 {
dataset = append(dataset, dataset[len(dataset)-1])
}
for i:=0 ; i<len(dataset); i=i+1 {
merkleNodes = append(merkleNodes, NewMerkleNode(nil, nil, dataset[i]))
}
for i:=0; i<len(merkleNodes); i=i+2 {
if i == len(merkleNodes) -1 {
break
}
merkleNodes = append(merkleNodes,NewMerkleNode(merkleNodes[i], merkleNodes[i+1], nil))
}
return &merkleTree{root:merkleNodes[len(merkleNodes)-1]}
}
func NewMerkleNode(left, right *merkleNode, data []byte) *merkleNode{
var node merkleNode
if left == nil && right == nil {
hash := sha256.Sum256(data)
node.hash = hash[:]
}else{
hash := sha256.Sum256(append(left.hash, right.hash...))
node.hash = hash[:]
}
node.left = left
node.right = right
return &node
}