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 3
/
stored_merkle_tree_test.go
132 lines (109 loc) · 3.07 KB
/
stored_merkle_tree_test.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
package storage
import (
"testing"
"github.com/Worldcoin/hubble-commander/models"
"github.com/Worldcoin/hubble-commander/utils"
"github.com/Worldcoin/hubble-commander/utils/merkletree"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
type StoredMerkleTreeTestSuite struct {
*require.Assertions
suite.Suite
storage *TestStorage
treeDepth uint8
}
func (s *StoredMerkleTreeTestSuite) SetupSuite() {
s.Assertions = require.New(s.T())
s.treeDepth = 32
}
func (s *StoredMerkleTreeTestSuite) SetupTest() {
var err error
s.storage, err = NewTestStorage()
s.NoError(err)
}
func (s *StoredMerkleTreeTestSuite) TearDownTest() {
err := s.storage.Teardown()
s.NoError(err)
}
func (s *StoredMerkleTreeTestSuite) TestRoot_InitialRoot() {
tree := NewStoredMerkleTree("state", s.storage.database, s.treeDepth)
root, err := tree.Root()
s.NoError(err)
s.Equal(merkletree.GetZeroHash(s.treeDepth), *root)
}
func (s *StoredMerkleTreeTestSuite) TestRoot_ChangesAfterSet() {
tree := NewStoredMerkleTree("state", s.storage.database, s.treeDepth)
newRoot, _, err := tree.SetNode(&models.MerklePath{
Path: 0,
Depth: s.treeDepth,
}, utils.RandomHash())
s.NoError(err)
root, err := tree.Root()
s.NoError(err)
s.NotEqual(merkletree.GetZeroHash(s.treeDepth), *root)
s.Equal(newRoot, root)
}
func (s *StoredMerkleTreeTestSuite) TestSetSingleNode_VerifiesDepth() {
tree := NewStoredMerkleTree("state", s.storage.database, s.treeDepth)
err := tree.SetSingleNode(&models.MerkleTreeNode{
MerklePath: models.MerklePath{
Path: 0,
Depth: s.treeDepth,
},
DataHash: utils.RandomHash(),
})
s.NoError(err)
err = tree.SetSingleNode(&models.MerkleTreeNode{
MerklePath: models.MerklePath{
Path: 0,
Depth: 33,
},
DataHash: utils.RandomHash(),
})
s.ErrorIs(err, ErrExceededTreeDepth)
}
func (s *StoredMerkleTreeTestSuite) TestSetNode_VerifiesDepth() {
tree := NewStoredMerkleTree("state", s.storage.database, s.treeDepth)
_, _, err := tree.SetNode(&models.MerklePath{
Path: 0,
Depth: s.treeDepth,
}, utils.RandomHash())
s.NoError(err)
_, _, err = tree.SetNode(&models.MerklePath{
Path: 0,
Depth: 33,
}, utils.RandomHash())
s.ErrorIs(err, ErrExceededTreeDepth)
}
func (s *StoredMerkleTreeTestSuite) TestTwoTreesWithDifferentNamespaces() {
stateTree := NewStoredMerkleTree("state", s.storage.database, s.treeDepth)
accountTree := NewStoredMerkleTree("account", s.storage.database, s.treeDepth)
hash1 := utils.RandomHash()
_, _, err := stateTree.SetNode(&models.MerklePath{
Path: 0,
Depth: s.treeDepth,
}, hash1)
s.NoError(err)
hash2 := utils.RandomHash()
_, _, err = accountTree.SetNode(&models.MerklePath{
Path: 0,
Depth: s.treeDepth,
}, hash2)
s.NoError(err)
node1, err := stateTree.Get(models.MerklePath{
Path: 0,
Depth: s.treeDepth,
})
s.NoError(err)
s.Equal(hash1, node1.DataHash)
node2, err := accountTree.Get(models.MerklePath{
Path: 0,
Depth: s.treeDepth,
})
s.NoError(err)
s.Equal(hash2, node2.DataHash)
}
func TestStoredMerkleTreeTestSuite(t *testing.T) {
suite.Run(t, new(StoredMerkleTreeTestSuite))
}