This repository has been archived by the owner on Oct 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
stored_merkle_tree.go
145 lines (123 loc) · 3.6 KB
/
stored_merkle_tree.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package storage
import (
"github.com/Worldcoin/hubble-commander/models"
"github.com/Worldcoin/hubble-commander/utils"
"github.com/Worldcoin/hubble-commander/utils/merkletree"
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
bh "github.com/timshannon/badgerhold/v4"
)
var ErrExceededTreeDepth = errors.New("node depth exceeds the tree depth")
type StoredMerkleTree struct {
database *Database
namespace string
depth uint8
}
func NewStoredMerkleTree(namespace string, database *Database, depth uint8) *StoredMerkleTree {
return &StoredMerkleTree{
database: database,
namespace: namespace,
depth: depth,
}
}
func (s *StoredMerkleTree) keyFor(path models.MerklePath) models.NamespacedMerklePath {
return models.NamespacedMerklePath{Namespace: s.namespace, Path: path}
}
func (s *StoredMerkleTree) Get(path models.MerklePath) (*models.MerkleTreeNode, error) {
node := models.MerkleTreeNode{MerklePath: path}
err := s.database.Badger.Get(s.keyFor(path), &node)
if errors.Is(err, bh.ErrNotFound) {
return s.newZeroNode(&path), nil
}
if err != nil {
return nil, err
}
return &node, nil
}
func (s *StoredMerkleTree) Root() (*common.Hash, error) {
node, err := s.Get(models.MerklePath{Path: 0, Depth: 0})
if err != nil {
return nil, err
}
return &node.DataHash, nil
}
func (s *StoredMerkleTree) SetSingleNode(node *models.MerkleTreeNode) error {
if node.MerklePath.Depth > s.depth {
return ErrExceededTreeDepth
}
return s.database.Badger.Upsert(s.keyFor(node.MerklePath), *node)
}
// SetNode sets node hash and update all nodes leading to root. Returns new root hash and the insertion witness.
func (s *StoredMerkleTree) SetNode(path *models.MerklePath, hash common.Hash) (*common.Hash, models.Witness, error) {
if path.Depth > s.depth {
return nil, nil, ErrExceededTreeDepth
}
currentPath := path
currentHash := hash
witness := make(models.Witness, 0, path.Depth)
for currentPath.Depth != 0 {
sibling, err := currentPath.Sibling()
if err != nil {
return nil, nil, err
}
siblingNode, err := s.Get(*sibling)
if err != nil {
return nil, nil, err
}
witness = append(witness, siblingNode.DataHash)
err = s.SetSingleNode(&models.MerkleTreeNode{
MerklePath: *currentPath,
DataHash: currentHash,
})
if err != nil {
return nil, nil, err
}
currentHash = calculateParentHash(¤tHash, currentPath, siblingNode.DataHash)
currentPath, err = currentPath.Parent()
if err != nil {
return nil, nil, err
}
}
rootPath := models.MerklePath{Depth: 0, Path: 0}
err := s.SetSingleNode(&models.MerkleTreeNode{
MerklePath: rootPath,
DataHash: currentHash,
})
if err != nil {
return nil, nil, err
}
return ¤tHash, witness, nil
}
func (s *StoredMerkleTree) GetWitness(path models.MerklePath) (models.Witness, error) {
witnessPaths, err := path.GetWitnessPaths()
if err != nil {
return nil, err
}
witness := make([]common.Hash, 0, len(witnessPaths))
for i := range witnessPaths {
var node *models.MerkleTreeNode
node, err = s.Get(witnessPaths[i])
if err != nil {
return nil, err
}
witness = append(witness, node.DataHash)
}
return witness, nil
}
func (s *StoredMerkleTree) newZeroNode(path *models.MerklePath) *models.MerkleTreeNode {
return &models.MerkleTreeNode{
MerklePath: *path,
DataHash: merkletree.GetZeroHash(s.depth - path.Depth),
}
}
func calculateParentHash(
currentHash *common.Hash,
currentPath *models.MerklePath,
witnessHash common.Hash,
) common.Hash {
if currentPath.IsLeftNode() {
return utils.HashTwo(*currentHash, witnessHash)
} else {
return utils.HashTwo(witnessHash, *currentHash)
}
}