Skip to content

Commit

Permalink
feat: add SMT specific errors
Browse files Browse the repository at this point in the history
  • Loading branch information
h5law committed Jan 3, 2024
1 parent 2d70eb0 commit 71ec219
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 14 deletions.
2 changes: 1 addition & 1 deletion bulk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func bulkOperations(t *testing.T, operations int, insert int, update int, delete
ki := r.Intn(len(kv))

err := smt.Delete(kv[ki].key)
if err != nil && err != simplemap.ErrKVStoreKeyNotFound {
if err != nil && err != ErrKeyNotFound {
t.Fatalf("error: %v", err)
}
kv[ki].val = defaultValue
Expand Down
8 changes: 6 additions & 2 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@ import (
"errors"
)

// ErrBadProof is returned when an invalid Merkle proof is supplied.
var ErrBadProof = errors.New("bad proof")
var (
// ErrBadProof is returned when an invalid Merkle proof is supplied.
ErrBadProof = errors.New("bad proof")
// ErrKeyNotFound is returned when a key is not found in the tree.
ErrKeyNotFound = errors.New("key not found")
)
6 changes: 3 additions & 3 deletions fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func FuzzSMT_DetectUnexpectedFailures(f *testing.F) {
_, err := trie.Get(key())
if err != nil {
require.ErrorIsf(
t, err, simplemap.ErrKVStoreKeyNotFound,
t, err, ErrKeyNotFound,
"unknown error occurred while getting",
)
}
Expand All @@ -85,7 +85,7 @@ func FuzzSMT_DetectUnexpectedFailures(f *testing.F) {
err := trie.Delete(key())
if err != nil {
require.ErrorIsf(
t, err, simplemap.ErrKVStoreKeyNotFound,
t, err, ErrKeyNotFound,
"unknown error occurred while deleting",
)
continue
Expand All @@ -97,7 +97,7 @@ func FuzzSMT_DetectUnexpectedFailures(f *testing.F) {
_, err := trie.Prove(key())
if err != nil {
require.ErrorIsf(
t, err, simplemap.ErrKVStoreKeyNotFound,
t, err, ErrKeyNotFound,
"unknown error occurred while proving",
)
}
Expand Down
3 changes: 1 addition & 2 deletions smst_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"

"github.com/pokt-network/smt/kvstore"
"github.com/pokt-network/smt/kvstore/simplemap"
)

// SMSTWithStorage wraps an SMST with a mapping of value hashes to values with
Expand Down Expand Up @@ -49,7 +48,7 @@ func (smst *SMSTWithStorage) GetValueSum(key []byte) ([]byte, uint64, error) {
}
value, err := smst.preimages.Get(valueHash)
if err != nil {
if errors.Is(err, simplemap.ErrKVStoreKeyNotFound) {
if errors.Is(err, ErrKeyNotFound) {
// If key isn't found, return default value and sum
return defaultValue, 0, nil
}
Expand Down
7 changes: 3 additions & 4 deletions smt.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"hash"

"github.com/pokt-network/smt/kvstore"
"github.com/pokt-network/smt/kvstore/simplemap"
)

var (
Expand Down Expand Up @@ -247,11 +246,11 @@ func (smt *SMT) delete(node trieNode, depth int, path []byte, orphans *orphanNod
}

if node == nil {
return node, simplemap.ErrKVStoreKeyNotFound
return node, ErrKeyNotFound
}
if leaf, ok := node.(*leafNode); ok {
if !bytes.Equal(path, leaf.path) {
return node, simplemap.ErrKVStoreKeyNotFound
return node, ErrKeyNotFound
}
smt.addOrphan(orphans, node)
return nil, nil
Expand All @@ -261,7 +260,7 @@ func (smt *SMT) delete(node trieNode, depth int, path []byte, orphans *orphanNod

if ext, ok := node.(*extensionNode); ok {
if _, match := ext.match(path, depth); !match {
return node, simplemap.ErrKVStoreKeyNotFound
return node, ErrKeyNotFound
}
ext.child, err = smt.delete(ext.child, depth+ext.length(), path, orphans)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions smt_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"

"github.com/pokt-network/smt/kvstore"
"github.com/pokt-network/smt/kvstore/simplemap"
)

// SMTWithStorage wraps an SMT with a mapping of value hashes to values
Expand Down Expand Up @@ -43,7 +42,7 @@ func (smt *SMTWithStorage) GetValue(key []byte) ([]byte, error) {
}
value, err := smt.preimages.Get(valueHash)
if err != nil {
if errors.Is(err, simplemap.ErrKVStoreKeyNotFound) {
if errors.Is(err, ErrKeyNotFound) {
// If key isn't found, return default value
value = defaultValue
} else {
Expand Down

0 comments on commit 71ec219

Please sign in to comment.