-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtypes.go
92 lines (83 loc) · 3.67 KB
/
types.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
89
90
91
92
package smt
// TODO_DISCUSS_CONSIDERIN_THE_FUTURE:
// 1. Should we rename all instances of digest to hash?
// > digest is the correct term for the output of a hashing function IIRC
// 2. Should we introduce a shared interface between SparseMerkleTrie and SparseMerkleSumTrie?
// > Sum() would have to be no-op but could be done
// 3. Should we rename Commit to FlushToDisk?
// > No because what if this is an in memory trie?
const (
// The bit value use to distinguish an inner nodes left child and right child
leftChildBit = 0
)
var (
// defaultEmptyValue is the default value for a leaf node
defaultEmptyValue []byte
// defaultEmptySum is the default sum value for a leaf node
defaultEmptySum [sumSizeBytes]byte
// defaultEmptyCount is the default count value for a leaf node
defaultEmptyCount [countSizeBytes]byte
)
// MerkleRoot is a type alias for a byte slice returned from SparseMerkleTrie#Root().
type MerkleRoot []byte
// MerkleSumRoot is a type alias for a byte slice returned from SparseMerkleSumTrie#Root().
type MerkleSumRoot []byte
// A high-level interface that captures the behaviour of all types of nodes
type trieNode interface {
// Persisted returns a boolean to determine whether or not the node
// has been persisted to disk or only held in memory.
// It can be used skip unnecessary iops if already persisted
Persisted() bool
// The digest of the node, returning a cached value if available.
CachedDigest() []byte
}
// SparseMerkleTrie represents a Sparse Merkle Trie.
type SparseMerkleTrie interface {
// Update inserts a value into the SMT.
Update(key, value []byte) error
// Delete deletes a value from the SMT. Raises an error if the key is not present.
Delete(key []byte) error
// Get descends the trie to access a value. Returns nil if key is not present.
Get(key []byte) ([]byte, error)
// Root computes the Merkle root digest.
Root() MerkleRoot
// Prove computes a Merkle proof of inclusion or exclusion of a key.
Prove(key []byte) (*SparseMerkleProof, error)
// ProveClosest computes a Merkle proof of inclusion for a key in the trie
// which is closest to the path provided. It will search for the key with
// the longest common prefix before finding the key with the most common
// bits as the path provided.
ProveClosest([]byte) (*SparseMerkleClosestProof, error)
// Commit saves the trie's state to its persistent storage.
Commit() error
// Spec returns the TrieSpec for the trie
Spec() *TrieSpec
}
// SparseMerkleSumTrie represents a Sparse Merkle Sum Trie.
type SparseMerkleSumTrie interface {
// Update inserts a value and its sum into the SMST.
Update(key, value []byte, sum uint64) error
// Delete deletes a value from the SMST. Raises an error if the key is not present.
Delete(key []byte) error
// Get descends the trie to access a value. Returns nil if key is not present.
Get(key []byte) (data []byte, sum uint64, err error)
// Root computes the Merkle root digest.
Root() MerkleSumRoot
// Sum computes the total sum of the Merkle trie
Sum() (uint64, error)
MustSum() uint64
// Count returns the total number of non-empty leaves in the trie
Count() (uint64, error)
MustCount() uint64
// Prove computes a Merkle proof of inclusion or exclusion of a key.
Prove(key []byte) (*SparseMerkleProof, error)
// ProveClosest computes a Merkle proof of inclusion for a key in the trie
// which is closest to the path provided. It will search for the key with
// the longest common prefix before finding the key with the most common
// bits as the path provided.
ProveClosest([]byte) (*SparseMerkleClosestProof, error)
// Commit saves the trie's state to its persistent storage.
Commit() error
// Spec returns the TrieSpec for the trie
Spec() *TrieSpec
}