Skip to content

Commit

Permalink
[fixup] prevent import cycle error errors in treestore
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanlott committed Jul 27, 2023
1 parent 7a7d95c commit 0f87f0b
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 85 deletions.
12 changes: 12 additions & 0 deletions persistence/trees/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//go:build test

package trees

import (
"crypto/sha256"
"hash"
)

type TreeStore = treeStore

var SMTTreeHasher hash.Hash = sha256.New()
90 changes: 90 additions & 0 deletions persistence/trees/prove_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package trees

import (
"fmt"
"testing"

"github.com/pokt-network/pocket/persistence/kvstore"
"github.com/pokt-network/smt"
"github.com/stretchr/testify/require"
)

func TestTreeStore_Prove(t *testing.T) {
nodeStore := kvstore.NewMemKVStore()
tree := smt.NewSparseMerkleTree(nodeStore, smtTreeHasher)
testTree := &stateTree{
name: "test",
tree: tree,
nodeStore: nodeStore,
}

require.NoError(t, testTree.tree.Update([]byte("key"), []byte("value")))
require.NoError(t, testTree.tree.Commit())

treeStore := &treeStore{
merkleTrees: make(map[string]*stateTree, 1),
}
treeStore.merkleTrees["test"] = testTree

testCases := []struct {
name string
treeName string
key []byte
value []byte
valid bool
expectedErr error
}{
{
name: "valid inclusion proof: key and value in tree",
treeName: "test",
key: []byte("key"),
value: []byte("value"),
valid: true,
expectedErr: nil,
},
{
name: "valid exclusion proof: key not in tree",
treeName: "test",
key: []byte("key2"),
value: nil,
valid: true,
expectedErr: nil,
},
{
name: "invalid proof: tree not in store",
treeName: "unstored tree",
key: []byte("key"),
value: []byte("value"),
valid: false,
expectedErr: fmt.Errorf("tree not found: %s", "unstored tree"),
},
{
name: "invalid inclusion proof: key in tree, wrong value",
treeName: "test",
key: []byte("key"),
value: []byte("wrong value"),
valid: false,
expectedErr: nil,
},
{
name: "invalid exclusion proof: key in tree",
treeName: "test",
key: []byte("key"),
value: nil,
valid: false,
expectedErr: nil,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
valid, err := treeStore.Prove(tc.treeName, tc.key, tc.value)
require.Equal(t, valid, tc.valid)
if tc.expectedErr == nil {
require.NoError(t, err)
return
}
require.ErrorAs(t, err, &tc.expectedErr)
})
}
}
88 changes: 3 additions & 85 deletions persistence/trees/trees_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package trees_test

import (
"encoding/hex"
"fmt"
"log"
"math/big"
"testing"

"github.com/pokt-network/pocket/persistence"
"github.com/pokt-network/pocket/persistence/kvstore"
"github.com/pokt-network/pocket/persistence/trees"
"github.com/pokt-network/pocket/runtime"
"github.com/pokt-network/pocket/runtime/configs"
Expand All @@ -19,7 +17,7 @@ import (
"github.com/pokt-network/pocket/shared/messaging"
"github.com/pokt-network/pocket/shared/modules"
"github.com/pokt-network/pocket/shared/utils"
"github.com/pokt-network/smt"

"github.com/stretchr/testify/require"
)

Expand All @@ -39,7 +37,7 @@ var (
)

const (
h1 = "5282ee91a3ec0a6f2b30e4780b369bae78c80ef3ea40587fef6ae263bf41f244"
trees_hash1 = "5282ee91a3ec0a6f2b30e4780b369bae78c80ef3ea40587fef6ae263bf41f244"
)

func TestTreeStore_Update(t *testing.T) {
Expand All @@ -57,7 +55,7 @@ func TestTreeStore_Update(t *testing.T) {
hash1, err := context.ComputeStateHash()
require.NoError(t, err)
require.NotEmpty(t, hash1)
require.Equal(t, hash1, h1)
require.Equal(t, hash1, trees_hash1)

_, err = createAndInsertDefaultTestApp(t, context)
require.NoError(t, err)
Expand Down Expand Up @@ -205,83 +203,3 @@ func resetStateToGenesis(m modules.PersistenceModule) {
func TestTreeStore_GetTreeHashes(t *testing.T) {
t.Skip("TODO: Write test case for GetTreeHashes method") // context: https://github.com/pokt-network/pocket/pull/915#discussion_r1267313664
}

func TestTreeStore_Prove(t *testing.T) {
nodeStore := kvstore.NewMemKVStore()
tree := smt.NewSparseMerkleTree(nodeStore, smtTreeHasher)
testTree := &stateTree{
name: "test",
tree: tree,
nodeStore: nodeStore,
}

require.NoError(t, testTree.tree.Update([]byte("key"), []byte("value")))
require.NoError(t, testTree.tree.Commit())

treeStore := &treeStore{
merkleTrees: make(map[string]*stateTree, 1),
}
treeStore.merkleTrees["test"] = testTree

testCases := []struct {
name string
treeName string
key []byte
value []byte
valid bool
expectedErr error
}{
{
name: "valid inclusion proof: key and value in tree",
treeName: "test",
key: []byte("key"),
value: []byte("value"),
valid: true,
expectedErr: nil,
},
{
name: "valid exclusion proof: key not in tree",
treeName: "test",
key: []byte("key2"),
value: nil,
valid: true,
expectedErr: nil,
},
{
name: "invalid proof: tree not in store",
treeName: "unstored tree",
key: []byte("key"),
value: []byte("value"),
valid: false,
expectedErr: fmt.Errorf("tree not found: %s", "unstored tree"),
},
{
name: "invalid inclusion proof: key in tree, wrong value",
treeName: "test",
key: []byte("key"),
value: []byte("wrong value"),
valid: false,
expectedErr: nil,
},
{
name: "invalid exclusion proof: key in tree",
treeName: "test",
key: []byte("key"),
value: nil,
valid: false,
expectedErr: nil,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
valid, err := treeStore.Prove(tc.treeName, tc.key, tc.value)
require.Equal(t, valid, tc.valid)
if tc.expectedErr == nil {
require.NoError(t, err)
return
}
require.ErrorAs(t, err, &tc.expectedErr)
})
}
}

0 comments on commit 0f87f0b

Please sign in to comment.