-
Notifications
You must be signed in to change notification settings - Fork 4
/
root_test.go
94 lines (80 loc) · 1.96 KB
/
root_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
package smt_test
import (
"crypto/sha256"
"crypto/sha512"
"fmt"
"hash"
"testing"
"github.com/stretchr/testify/require"
"github.com/pokt-network/smt"
"github.com/pokt-network/smt/kvstore/simplemap"
)
func TestMerkleSumRoot_SumAndCountSuccess(t *testing.T) {
tests := []struct {
desc string
hasher hash.Hash
}{
{
desc: "sha256 hasher",
hasher: sha256.New(),
},
{
desc: "sha512 hasher",
hasher: sha512.New(),
},
}
nodeStore := simplemap.NewSimpleMap()
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
t.Cleanup(func() {
require.NoError(t, nodeStore.ClearAll())
})
trie := smt.NewSparseMerkleSumTrie(nodeStore, test.hasher)
for i := uint64(0); i < 10; i++ {
require.NoError(t, trie.Update([]byte(fmt.Sprintf("key%d", i)), []byte(fmt.Sprintf("value%d", i)), i))
}
sum, sumErr := trie.Sum()
require.NoError(t, sumErr)
count, countErr := trie.Count()
require.NoError(t, countErr)
require.EqualValues(t, uint64(45), sum)
require.EqualValues(t, uint64(10), count)
})
}
}
func TestMekleRoot_SumAndCountError(t *testing.T) {
tests := []struct {
desc string
hasher hash.Hash
}{
{
desc: "sha256 hasher",
hasher: sha256.New(),
},
{
desc: "sha512 hasher",
hasher: sha512.New(),
},
}
nodeStore := simplemap.NewSimpleMap()
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
t.Cleanup(func() {
require.NoError(t, nodeStore.ClearAll())
})
trie := smt.NewSparseMerkleSumTrie(nodeStore, test.hasher)
for i := uint64(0); i < 10; i++ {
require.NoError(t, trie.Update([]byte(fmt.Sprintf("key%d", i)), []byte(fmt.Sprintf("value%d", i)), i))
}
root := trie.Root()
// Mangle the root bytes.
root = root[:len(root)-1]
sum, sumErr := root.Sum()
require.Error(t, sumErr)
require.Equal(t, uint64(0), sum)
count, countErr := root.Count()
require.Error(t, countErr)
require.Equal(t, uint64(0), count)
})
}
}