-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Enhancement] Add MerkleRoot type alias for []byte with Sum method (#37)
- Loading branch information
h5law
authored
Jan 9, 2024
1 parent
54a4fe4
commit d16ac8d
Showing
6 changed files
with
125 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package smt_test | ||
|
||
import ( | ||
"crypto/sha256" | ||
"crypto/sha512" | ||
"encoding/binary" | ||
"fmt" | ||
"hash" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/pokt-network/smt" | ||
"github.com/pokt-network/smt/kvstore/simplemap" | ||
) | ||
|
||
func TestMerkleRoot_TrieTypes(t *testing.T) { | ||
tests := []struct { | ||
desc string | ||
sumTree bool | ||
hasher hash.Hash | ||
expectedPanic string | ||
}{ | ||
{ | ||
desc: "successfully: gets sum of sha256 hasher SMST", | ||
sumTree: true, | ||
hasher: sha256.New(), | ||
expectedPanic: "", | ||
}, | ||
{ | ||
desc: "successfully: gets sum of sha512 hasher SMST", | ||
sumTree: true, | ||
hasher: sha512.New(), | ||
expectedPanic: "", | ||
}, | ||
{ | ||
desc: "failure: panics for sha256 hasher SMT", | ||
sumTree: false, | ||
hasher: sha256.New(), | ||
expectedPanic: "roo#sum: not a merkle sum trie", | ||
}, | ||
{ | ||
desc: "failure: panics for sha512 hasher SMT", | ||
sumTree: false, | ||
hasher: sha512.New(), | ||
expectedPanic: "roo#sum: not a merkle sum trie", | ||
}, | ||
} | ||
|
||
nodeStore := simplemap.NewSimpleMap() | ||
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.desc, func(t *testing.T) { | ||
t.Cleanup(func() { | ||
require.NoError(t, nodeStore.ClearAll()) | ||
}) | ||
if tt.sumTree { | ||
trie := smt.NewSparseMerkleSumTrie(nodeStore, tt.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() | ||
require.Equal(t, root.Sum(), getSumBzHelper(t, root)) | ||
return | ||
} | ||
trie := smt.NewSparseMerkleTrie(nodeStore, tt.hasher) | ||
for i := 0; i < 10; i++ { | ||
require.NoError(t, trie.Update([]byte(fmt.Sprintf("key%d", i)), []byte(fmt.Sprintf("value%d", i)))) | ||
} | ||
if panicStr := recover(); panicStr != nil { | ||
require.Equal(t, tt.expectedPanic, panicStr) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func getSumBzHelper(t *testing.T, r []byte) uint64 { | ||
sumSize := len(r) % 32 | ||
sumBz := make([]byte, sumSize) | ||
copy(sumBz[:], []byte(r)[len([]byte(r))-sumSize:]) | ||
return binary.BigEndian.Uint64(sumBz[:]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters