@@ -13,64 +13,82 @@ import (
1313 "github.com/pokt-network/smt/kvstore/simplemap"
1414)
1515
16- func TestMerkleRoot_TrieTypes (t * testing.T ) {
16+ func TestMerkleSumRoot_SumAndCountSuccess (t * testing.T ) {
1717 tests := []struct {
18- desc string
19- sumTree bool
20- hasher hash.Hash
21- expectedPanic string
18+ desc string
19+ hasher hash.Hash
2220 }{
2321 {
24- desc : "successfully: gets sum of sha256 hasher SMST" ,
25- sumTree : true ,
26- hasher : sha256 .New (),
27- expectedPanic : "" ,
22+ desc : "sha256 hasher" ,
23+ hasher : sha256 .New (),
2824 },
2925 {
30- desc : "successfully: gets sum of sha512 hasher SMST" ,
31- sumTree : true ,
32- hasher : sha512 .New (),
33- expectedPanic : "" ,
26+ desc : "sha512 hasher" ,
27+ hasher : sha512 .New (),
3428 },
29+ }
30+
31+ nodeStore := simplemap .NewSimpleMap ()
32+ for _ , test := range tests {
33+ t .Run (test .desc , func (t * testing.T ) {
34+ t .Cleanup (func () {
35+ require .NoError (t , nodeStore .ClearAll ())
36+ })
37+ trie := smt .NewSparseMerkleSumTrie (nodeStore , test .hasher )
38+ for i := uint64 (0 ); i < 10 ; i ++ {
39+ require .NoError (t , trie .Update ([]byte (fmt .Sprintf ("key%d" , i )), []byte (fmt .Sprintf ("value%d" , i )), i ))
40+ }
41+
42+ sum , sumErr := trie .Sum ()
43+ require .NoError (t , sumErr )
44+
45+ count , countErr := trie .Count ()
46+ require .NoError (t , countErr )
47+
48+ require .EqualValues (t , uint64 (45 ), sum )
49+ require .EqualValues (t , uint64 (10 ), count )
50+ })
51+ }
52+ }
53+
54+ func TestMekleRoot_SumAndCountError (t * testing.T ) {
55+ tests := []struct {
56+ desc string
57+ hasher hash.Hash
58+ }{
3559 {
36- desc : "failure: panics for sha256 hasher SMT" ,
37- sumTree : false ,
38- hasher : sha256 .New (),
39- expectedPanic : "roo#sum: not a merkle sum trie" ,
60+ desc : "sha256 hasher" ,
61+ hasher : sha256 .New (),
4062 },
4163 {
42- desc : "failure: panics for sha512 hasher SMT" ,
43- sumTree : false ,
44- hasher : sha512 .New (),
45- expectedPanic : "roo#sum: not a merkle sum trie" ,
64+ desc : "sha512 hasher" ,
65+ hasher : sha512 .New (),
4666 },
4767 }
4868
4969 nodeStore := simplemap .NewSimpleMap ()
50- for _ , tt := range tests {
51- tt := tt
52- t .Run (tt .desc , func (t * testing.T ) {
70+ for _ , test := range tests {
71+ t .Run (test .desc , func (t * testing.T ) {
5372 t .Cleanup (func () {
5473 require .NoError (t , nodeStore .ClearAll ())
5574 })
56- if tt .sumTree {
57- trie := smt .NewSparseMerkleSumTrie (nodeStore , tt .hasher )
58- for i := uint64 (0 ); i < 10 ; i ++ {
59- require .NoError (t , trie .Update ([]byte (fmt .Sprintf ("key%d" , i )), []byte (fmt .Sprintf ("value%d" , i )), i ))
60- }
61- require .NotNil (t , trie .Sum ())
62- require .EqualValues (t , 45 , trie .Sum ())
63- require .EqualValues (t , 10 , trie .Count ())
64-
65- return
66- }
67- trie := smt .NewSparseMerkleTrie (nodeStore , tt .hasher )
68- for i := 0 ; i < 10 ; i ++ {
69- require .NoError (t , trie .Update ([]byte (fmt .Sprintf ("key%d" , i )), []byte (fmt .Sprintf ("value%d" , i ))))
70- }
71- if panicStr := recover (); panicStr != nil {
72- require .Equal (t , tt .expectedPanic , panicStr )
75+ trie := smt .NewSparseMerkleSumTrie (nodeStore , test .hasher )
76+ for i := uint64 (0 ); i < 10 ; i ++ {
77+ require .NoError (t , trie .Update ([]byte (fmt .Sprintf ("key%d" , i )), []byte (fmt .Sprintf ("value%d" , i )), i ))
7378 }
79+
80+ root := trie .Root ()
81+
82+ // Mangle the root bytes.
83+ root = root [:len (root )- 1 ]
84+
85+ sum , sumErr := root .Sum ()
86+ require .Error (t , sumErr )
87+ require .Equal (t , uint64 (0 ), sum )
88+
89+ count , countErr := root .Count ()
90+ require .Error (t , countErr )
91+ require .Equal (t , uint64 (0 ), count )
7492 })
7593 }
7694}
0 commit comments