-
Notifications
You must be signed in to change notification settings - Fork 4
/
smt_utils_test.go
88 lines (77 loc) · 2.24 KB
/
smt_utils_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
package smt
import (
"bytes"
"errors"
"github.com/pokt-network/smt/kvstore"
)
// SMTWithStorage wraps an SMT with a mapping of value hashes to values
// (preimages), for use in tests.
// Note: this doesn't delete from preimages (inputs to hashing functions),
// since there could be duplicate stored values.
type SMTWithStorage struct {
*SMT
preimages kvstore.MapStore
}
// Update updates a key with a new value in the trie and adds the value to
// the preimages KVStore
// Preimages are the values prior to them being hashed - they are used to
// confirm the values are in the trie
func (smt *SMTWithStorage) Update(key, value []byte) error {
if err := smt.SMT.Update(key, value); err != nil {
return err
}
valueHash := smt.valueHash(value)
return smt.preimages.Set(valueHash, value)
}
// Delete deletes a key from the trie.
func (smt *SMTWithStorage) Delete(key []byte) error {
return smt.SMT.Delete(key)
}
// Get gets the value of a key from the trie.
func (smt *SMTWithStorage) GetValue(key []byte) ([]byte, error) {
valueHash, err := smt.Get(key)
if err != nil {
return nil, err
}
if valueHash == nil {
return nil, nil
}
value, err := smt.preimages.Get(valueHash)
if err != nil {
if errors.Is(err, ErrKeyNotFound) {
// If key isn't found, return default value
value = defaultEmptyValue
} else {
// Otherwise percolate up any other error
return nil, err
}
}
return value, nil
}
// Has returns true if the value at the given key is non-default, false
// otherwise.
func (smt *SMTWithStorage) Has(key []byte) (bool, error) {
val, err := smt.GetValue(key)
return !bytes.Equal(defaultEmptyValue, val), err
}
// ProveCompact generates a compacted Merkle proof for a key against the
// current root.
func ProveCompact(key []byte, smt SparseMerkleTrie) (*SparseCompactMerkleProof, error) {
proof, err := smt.Prove(key)
if err != nil {
return nil, err
}
return CompactProof(proof, smt.Spec())
}
// dummyHasher is a dummy hasher for tests, where the digest of keys is
// equivalent to the preimage.
type dummyPathHasher struct {
size int
}
func (h dummyPathHasher) Path(key []byte) []byte {
if len(key) != h.size {
panic("len(key) must equal path size")
}
return key
}
func (h dummyPathHasher) PathSize() int { return h.size }