2020-05-22 07:37:07 +00:00
|
|
|
package mpt
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-10-21 13:58:41 +00:00
|
|
|
"encoding/binary"
|
2020-05-22 07:37:07 +00:00
|
|
|
"errors"
|
2020-10-21 13:58:41 +00:00
|
|
|
"fmt"
|
2020-05-22 07:37:07 +00:00
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/storage"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2021-07-18 13:32:10 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util/slice"
|
2020-05-22 07:37:07 +00:00
|
|
|
)
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// TrieMode is the storage mode of a trie, it affects the DB scheme.
|
2022-01-28 08:56:33 +00:00
|
|
|
type TrieMode byte
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// TrieMode is the storage mode of a trie.
|
2022-01-28 08:56:33 +00:00
|
|
|
const (
|
|
|
|
// ModeAll is used to store everything.
|
|
|
|
ModeAll TrieMode = 0
|
|
|
|
// ModeLatest is used to only store the latest root.
|
|
|
|
ModeLatest TrieMode = 0x01
|
|
|
|
// ModeGCFlag is a flag for GC.
|
|
|
|
ModeGCFlag TrieMode = 0x02
|
|
|
|
// ModeGC is used to store a set of roots with GC possible, it combines
|
|
|
|
// GCFlag and Latest (because it needs RC, but it has GC enabled).
|
|
|
|
ModeGC TrieMode = 0x03
|
|
|
|
)
|
|
|
|
|
2020-05-22 07:37:07 +00:00
|
|
|
// Trie is an MPT trie storing all key-value pairs.
|
|
|
|
type Trie struct {
|
|
|
|
Store *storage.MemCachedStore
|
|
|
|
|
2022-01-28 08:56:33 +00:00
|
|
|
root Node
|
|
|
|
mode TrieMode
|
|
|
|
refcount map[util.Uint256]*cachedNode
|
2020-10-21 13:58:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type cachedNode struct {
|
|
|
|
bytes []byte
|
|
|
|
initial int32
|
|
|
|
refcount int32
|
2020-05-22 07:37:07 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// ErrNotFound is returned when the requested trie item is missing.
|
2020-05-22 07:37:07 +00:00
|
|
|
var ErrNotFound = errors.New("item not found")
|
|
|
|
|
2022-01-28 08:56:33 +00:00
|
|
|
// RC returns true when reference counting is enabled.
|
|
|
|
func (m TrieMode) RC() bool {
|
|
|
|
return m&ModeLatest != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// GC returns true when garbage collection is enabled.
|
|
|
|
func (m TrieMode) GC() bool {
|
|
|
|
return m&ModeGCFlag != 0
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// NewTrie returns a new MPT trie. It accepts a MemCachedStore to decouple storage errors from logic errors,
|
2020-05-22 07:37:07 +00:00
|
|
|
// so that all storage errors are processed during `store.Persist()` at the caller.
|
2022-04-20 18:30:09 +00:00
|
|
|
// Another benefit is that every `Put` can be considered an atomic operation.
|
2022-01-28 08:56:33 +00:00
|
|
|
func NewTrie(root Node, mode TrieMode, store *storage.MemCachedStore) *Trie {
|
2020-05-22 07:37:07 +00:00
|
|
|
if root == nil {
|
2021-08-03 14:10:46 +00:00
|
|
|
root = EmptyNode{}
|
2020-05-22 07:37:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Trie{
|
|
|
|
Store: store,
|
|
|
|
root: root,
|
2020-10-21 13:58:41 +00:00
|
|
|
|
2022-01-28 08:56:33 +00:00
|
|
|
mode: mode,
|
|
|
|
refcount: make(map[util.Uint256]*cachedNode),
|
2020-05-22 07:37:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Get returns the value for the provided key in t.
|
2020-05-22 07:37:07 +00:00
|
|
|
func (t *Trie) Get(key []byte) ([]byte, error) {
|
2021-10-06 13:37:23 +00:00
|
|
|
if len(key) > MaxKeyLength {
|
|
|
|
return nil, errors.New("key is too big")
|
|
|
|
}
|
2020-05-22 07:37:07 +00:00
|
|
|
path := toNibbles(key)
|
2021-10-07 13:56:27 +00:00
|
|
|
r, leaf, _, err := t.getWithPath(t.root, path, true)
|
2020-05-22 07:37:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
t.root = r
|
2021-10-07 13:56:27 +00:00
|
|
|
return slice.Copy(leaf.(*LeafNode).value), nil
|
2020-05-22 07:37:07 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// getWithPath returns the current node with all hash nodes along the path replaced
|
|
|
|
// with their "unhashed" counterparts. It also returns node which the provided path in a
|
|
|
|
// subtrie rooting in curr points to. In case of `strict` set to `false`, the
|
|
|
|
// provided path can be incomplete, so it also returns the full path that points to
|
|
|
|
// the node found at the specified incomplete path. In case of `strict` set to `true`,
|
2021-10-07 13:56:27 +00:00
|
|
|
// the resulting path matches the provided one.
|
|
|
|
func (t *Trie) getWithPath(curr Node, path []byte, strict bool) (Node, Node, []byte, error) {
|
2020-05-22 07:37:07 +00:00
|
|
|
switch n := curr.(type) {
|
|
|
|
case *LeafNode:
|
|
|
|
if len(path) == 0 {
|
2021-10-07 13:56:27 +00:00
|
|
|
return curr, n, []byte{}, nil
|
2020-05-22 07:37:07 +00:00
|
|
|
}
|
|
|
|
case *BranchNode:
|
|
|
|
i, path := splitPath(path)
|
2021-10-07 13:56:27 +00:00
|
|
|
if i == lastChild && !strict {
|
|
|
|
return curr, n, []byte{}, nil
|
|
|
|
}
|
|
|
|
r, res, prefix, err := t.getWithPath(n.Children[i], path, strict)
|
2020-05-22 07:37:07 +00:00
|
|
|
if err != nil {
|
2021-10-07 13:56:27 +00:00
|
|
|
return nil, nil, nil, err
|
2020-05-22 07:37:07 +00:00
|
|
|
}
|
|
|
|
n.Children[i] = r
|
2021-10-07 13:56:27 +00:00
|
|
|
return n, res, append([]byte{i}, prefix...), nil
|
2021-08-03 14:10:46 +00:00
|
|
|
case EmptyNode:
|
2020-05-22 07:37:07 +00:00
|
|
|
case *HashNode:
|
2021-08-03 14:10:46 +00:00
|
|
|
if r, err := t.getFromStore(n.hash); err == nil {
|
2021-10-07 13:56:27 +00:00
|
|
|
return t.getWithPath(r, path, strict)
|
2020-05-22 07:37:07 +00:00
|
|
|
}
|
|
|
|
case *ExtensionNode:
|
2021-10-07 13:56:27 +00:00
|
|
|
if len(path) == 0 && !strict {
|
|
|
|
return curr, n.next, n.key, nil
|
|
|
|
}
|
2020-05-22 07:37:07 +00:00
|
|
|
if bytes.HasPrefix(path, n.key) {
|
2021-10-07 13:56:27 +00:00
|
|
|
r, res, prefix, err := t.getWithPath(n.next, path[len(n.key):], strict)
|
2020-05-22 07:37:07 +00:00
|
|
|
if err != nil {
|
2021-10-07 13:56:27 +00:00
|
|
|
return nil, nil, nil, err
|
2020-05-22 07:37:07 +00:00
|
|
|
}
|
|
|
|
n.next = r
|
2021-10-07 13:56:27 +00:00
|
|
|
return curr, res, append(n.key, prefix...), err
|
|
|
|
}
|
|
|
|
if !strict && bytes.HasPrefix(n.key, path) {
|
|
|
|
// path is shorter than prefix, stop seeking
|
|
|
|
return curr, n.next, n.key, nil
|
2020-05-22 07:37:07 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic("invalid MPT node type")
|
|
|
|
}
|
2021-10-07 13:56:27 +00:00
|
|
|
return curr, nil, nil, ErrNotFound
|
2020-05-22 07:37:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Put puts key-value pair in t.
|
|
|
|
func (t *Trie) Put(key, value []byte) error {
|
2021-06-03 12:46:35 +00:00
|
|
|
if len(key) == 0 {
|
|
|
|
return errors.New("key is empty")
|
|
|
|
} else if len(key) > MaxKeyLength {
|
2020-05-22 07:37:07 +00:00
|
|
|
return errors.New("key is too big")
|
|
|
|
} else if len(value) > MaxValueLength {
|
|
|
|
return errors.New("value is too big")
|
2021-08-23 15:32:10 +00:00
|
|
|
} else if value == nil {
|
|
|
|
// (t *Trie).Delete should be used to remove value
|
|
|
|
return errors.New("value is nil")
|
2020-05-22 07:37:07 +00:00
|
|
|
}
|
|
|
|
path := toNibbles(key)
|
|
|
|
n := NewLeafNode(value)
|
|
|
|
r, err := t.putIntoNode(t.root, path, n)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
t.root = r
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// putIntoLeaf puts the val to the trie if the current node is a Leaf.
|
|
|
|
// It returns a Node if curr needs to be replaced and an error has occurred, if any.
|
2020-05-22 07:37:07 +00:00
|
|
|
func (t *Trie) putIntoLeaf(curr *LeafNode, path []byte, val Node) (Node, error) {
|
|
|
|
v := val.(*LeafNode)
|
|
|
|
if len(path) == 0 {
|
2020-10-21 13:58:41 +00:00
|
|
|
t.removeRef(curr.Hash(), curr.bytes)
|
|
|
|
t.addRef(val.Hash(), val.Bytes())
|
2020-05-22 07:37:07 +00:00
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
b := NewBranchNode()
|
2020-10-21 13:58:41 +00:00
|
|
|
b.Children[path[0]] = t.newSubTrie(path[1:], v, true)
|
2020-05-22 07:37:07 +00:00
|
|
|
b.Children[lastChild] = curr
|
2020-10-21 13:58:41 +00:00
|
|
|
t.addRef(b.Hash(), b.bytes)
|
2020-05-22 07:37:07 +00:00
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// putIntoBranch puts the val to the trie if the current node is a Branch.
|
|
|
|
// It returns the Node if curr needs to be replaced and an error has occurred, if any.
|
2020-05-22 07:37:07 +00:00
|
|
|
func (t *Trie) putIntoBranch(curr *BranchNode, path []byte, val Node) (Node, error) {
|
|
|
|
i, path := splitPath(path)
|
2020-10-21 13:58:41 +00:00
|
|
|
t.removeRef(curr.Hash(), curr.bytes)
|
2020-05-22 07:37:07 +00:00
|
|
|
r, err := t.putIntoNode(curr.Children[i], path, val)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
curr.Children[i] = r
|
2020-06-02 20:49:46 +00:00
|
|
|
curr.invalidateCache()
|
2020-10-21 13:58:41 +00:00
|
|
|
t.addRef(curr.Hash(), curr.bytes)
|
2020-05-22 07:37:07 +00:00
|
|
|
return curr, nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// putIntoExtension puts the val to the trie if the current node is an Extension.
|
|
|
|
// It returns the Node if curr needs to be replaced and an error has occurred, if any.
|
2020-05-22 07:37:07 +00:00
|
|
|
func (t *Trie) putIntoExtension(curr *ExtensionNode, path []byte, val Node) (Node, error) {
|
2020-10-21 13:58:41 +00:00
|
|
|
t.removeRef(curr.Hash(), curr.bytes)
|
2020-05-22 07:37:07 +00:00
|
|
|
if bytes.HasPrefix(path, curr.key) {
|
|
|
|
r, err := t.putIntoNode(curr.next, path[len(curr.key):], val)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
curr.next = r
|
2020-06-02 20:49:46 +00:00
|
|
|
curr.invalidateCache()
|
2020-10-21 13:58:41 +00:00
|
|
|
t.addRef(curr.Hash(), curr.bytes)
|
2020-05-22 07:37:07 +00:00
|
|
|
return curr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pref := lcp(curr.key, path)
|
|
|
|
lp := len(pref)
|
|
|
|
keyTail := curr.key[lp:]
|
|
|
|
pathTail := path[lp:]
|
|
|
|
|
2020-10-21 13:58:41 +00:00
|
|
|
s1 := t.newSubTrie(keyTail[1:], curr.next, false)
|
2020-05-22 07:37:07 +00:00
|
|
|
b := NewBranchNode()
|
|
|
|
b.Children[keyTail[0]] = s1
|
|
|
|
|
|
|
|
i, pathTail := splitPath(pathTail)
|
2020-10-21 13:58:41 +00:00
|
|
|
s2 := t.newSubTrie(pathTail, val, true)
|
2020-05-22 07:37:07 +00:00
|
|
|
b.Children[i] = s2
|
|
|
|
|
2020-10-21 13:58:41 +00:00
|
|
|
t.addRef(b.Hash(), b.bytes)
|
2020-05-22 07:37:07 +00:00
|
|
|
if lp > 0 {
|
2021-07-18 13:32:10 +00:00
|
|
|
e := NewExtensionNode(slice.Copy(pref), b)
|
2020-10-21 13:58:41 +00:00
|
|
|
t.addRef(e.Hash(), e.bytes)
|
|
|
|
return e, nil
|
2020-05-22 07:37:07 +00:00
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2021-08-03 14:10:46 +00:00
|
|
|
func (t *Trie) putIntoEmpty(path []byte, val Node) (Node, error) {
|
|
|
|
return t.newSubTrie(path, val, true), nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// putIntoHash puts the val to the trie if the current node is a HashNode.
|
|
|
|
// It returns the Node if curr needs to be replaced and an error has occurred, if any.
|
2020-05-22 07:37:07 +00:00
|
|
|
func (t *Trie) putIntoHash(curr *HashNode, path []byte, val Node) (Node, error) {
|
|
|
|
result, err := t.getFromStore(curr.hash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return t.putIntoNode(result, path, val)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// newSubTrie creates a new trie containing the node at the provided path.
|
2020-10-21 13:58:41 +00:00
|
|
|
func (t *Trie) newSubTrie(path []byte, val Node, newVal bool) Node {
|
|
|
|
if newVal {
|
|
|
|
t.addRef(val.Hash(), val.Bytes())
|
|
|
|
}
|
2020-05-22 07:37:07 +00:00
|
|
|
if len(path) == 0 {
|
|
|
|
return val
|
|
|
|
}
|
2020-10-21 13:58:41 +00:00
|
|
|
e := NewExtensionNode(path, val)
|
|
|
|
t.addRef(e.Hash(), e.bytes)
|
|
|
|
return e
|
2020-05-22 07:37:07 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// putIntoNode puts the val with the provided path inside curr and returns an updated node.
|
2020-10-21 13:58:41 +00:00
|
|
|
// Reference counters are updated for both curr and returned value.
|
2020-05-22 07:37:07 +00:00
|
|
|
func (t *Trie) putIntoNode(curr Node, path []byte, val Node) (Node, error) {
|
|
|
|
switch n := curr.(type) {
|
|
|
|
case *LeafNode:
|
|
|
|
return t.putIntoLeaf(n, path, val)
|
|
|
|
case *BranchNode:
|
|
|
|
return t.putIntoBranch(n, path, val)
|
|
|
|
case *ExtensionNode:
|
|
|
|
return t.putIntoExtension(n, path, val)
|
|
|
|
case *HashNode:
|
|
|
|
return t.putIntoHash(n, path, val)
|
2021-08-03 14:10:46 +00:00
|
|
|
case EmptyNode:
|
|
|
|
return t.putIntoEmpty(path, val)
|
2020-05-22 07:37:07 +00:00
|
|
|
default:
|
|
|
|
panic("invalid MPT node type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Delete removes the key from the trie.
|
|
|
|
// It returns no error on a missing key.
|
2020-05-22 07:37:07 +00:00
|
|
|
func (t *Trie) Delete(key []byte) error {
|
2021-10-06 13:37:23 +00:00
|
|
|
if len(key) > MaxKeyLength {
|
|
|
|
return errors.New("key is too big")
|
|
|
|
}
|
2020-05-22 07:37:07 +00:00
|
|
|
path := toNibbles(key)
|
|
|
|
r, err := t.deleteFromNode(t.root, path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
t.root = r
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Trie) deleteFromBranch(b *BranchNode, path []byte) (Node, error) {
|
|
|
|
i, path := splitPath(path)
|
2020-10-21 13:58:41 +00:00
|
|
|
h := b.Hash()
|
|
|
|
bs := b.bytes
|
2020-05-22 07:37:07 +00:00
|
|
|
r, err := t.deleteFromNode(b.Children[i], path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-21 13:58:41 +00:00
|
|
|
t.removeRef(h, bs)
|
2020-05-22 07:37:07 +00:00
|
|
|
b.Children[i] = r
|
2020-06-02 20:49:46 +00:00
|
|
|
b.invalidateCache()
|
2020-05-22 07:37:07 +00:00
|
|
|
var count, index int
|
|
|
|
for i := range b.Children {
|
2021-08-03 14:10:46 +00:00
|
|
|
if !isEmpty(b.Children[i]) {
|
2020-05-22 07:37:07 +00:00
|
|
|
index = i
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// count is >= 1 because branch node had at least 2 children before deletion.
|
|
|
|
if count > 1 {
|
2020-10-21 13:58:41 +00:00
|
|
|
t.addRef(b.Hash(), b.bytes)
|
2020-05-22 07:37:07 +00:00
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
c := b.Children[index]
|
|
|
|
if index == lastChild {
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
if h, ok := c.(*HashNode); ok {
|
|
|
|
c, err = t.getFromStore(h.Hash())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if e, ok := c.(*ExtensionNode); ok {
|
2020-10-21 13:58:41 +00:00
|
|
|
t.removeRef(e.Hash(), e.bytes)
|
2020-05-22 07:37:07 +00:00
|
|
|
e.key = append([]byte{byte(index)}, e.key...)
|
2020-06-02 20:49:46 +00:00
|
|
|
e.invalidateCache()
|
2020-10-21 13:58:41 +00:00
|
|
|
t.addRef(e.Hash(), e.bytes)
|
2020-05-22 07:37:07 +00:00
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
2020-10-21 13:58:41 +00:00
|
|
|
e := NewExtensionNode([]byte{byte(index)}, c)
|
|
|
|
t.addRef(e.Hash(), e.bytes)
|
|
|
|
return e, nil
|
2020-05-22 07:37:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Trie) deleteFromExtension(n *ExtensionNode, path []byte) (Node, error) {
|
|
|
|
if !bytes.HasPrefix(path, n.key) {
|
2021-02-17 09:26:32 +00:00
|
|
|
return n, nil
|
2020-05-22 07:37:07 +00:00
|
|
|
}
|
2020-10-21 13:58:41 +00:00
|
|
|
h := n.Hash()
|
|
|
|
bs := n.bytes
|
2020-05-22 07:37:07 +00:00
|
|
|
r, err := t.deleteFromNode(n.next, path[len(n.key):])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-21 13:58:41 +00:00
|
|
|
t.removeRef(h, bs)
|
2020-05-22 07:37:07 +00:00
|
|
|
switch nxt := r.(type) {
|
|
|
|
case *ExtensionNode:
|
2020-10-21 13:58:41 +00:00
|
|
|
t.removeRef(nxt.Hash(), nxt.bytes)
|
2020-05-22 07:37:07 +00:00
|
|
|
n.key = append(n.key, nxt.key...)
|
|
|
|
n.next = nxt.next
|
2021-08-03 14:10:46 +00:00
|
|
|
case EmptyNode:
|
|
|
|
return nxt, nil
|
2020-05-22 07:37:07 +00:00
|
|
|
case *HashNode:
|
2021-07-07 12:46:51 +00:00
|
|
|
n.next = nxt
|
2020-05-22 07:37:07 +00:00
|
|
|
default:
|
|
|
|
n.next = r
|
|
|
|
}
|
2020-06-04 14:16:32 +00:00
|
|
|
n.invalidateCache()
|
2020-10-21 13:58:41 +00:00
|
|
|
t.addRef(n.Hash(), n.bytes)
|
2020-05-22 07:37:07 +00:00
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// deleteFromNode removes the value with the provided path from curr and returns an updated node.
|
2020-10-21 13:58:41 +00:00
|
|
|
// Reference counters are updated for both curr and returned value.
|
2020-05-22 07:37:07 +00:00
|
|
|
func (t *Trie) deleteFromNode(curr Node, path []byte) (Node, error) {
|
|
|
|
switch n := curr.(type) {
|
|
|
|
case *LeafNode:
|
|
|
|
if len(path) == 0 {
|
2020-10-21 13:58:41 +00:00
|
|
|
t.removeRef(curr.Hash(), curr.Bytes())
|
2021-08-03 14:10:46 +00:00
|
|
|
return EmptyNode{}, nil
|
2020-05-22 07:37:07 +00:00
|
|
|
}
|
2021-02-17 09:26:32 +00:00
|
|
|
return curr, nil
|
2020-05-22 07:37:07 +00:00
|
|
|
case *BranchNode:
|
|
|
|
return t.deleteFromBranch(n, path)
|
|
|
|
case *ExtensionNode:
|
|
|
|
return t.deleteFromExtension(n, path)
|
2021-08-03 14:10:46 +00:00
|
|
|
case EmptyNode:
|
|
|
|
return n, nil
|
2020-05-22 07:37:07 +00:00
|
|
|
case *HashNode:
|
|
|
|
newNode, err := t.getFromStore(n.Hash())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return t.deleteFromNode(newNode, path)
|
|
|
|
default:
|
|
|
|
panic("invalid MPT node type")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// StateRoot returns root hash of t.
|
|
|
|
func (t *Trie) StateRoot() util.Uint256 {
|
2021-08-03 14:10:46 +00:00
|
|
|
if isEmpty(t.root) {
|
2020-05-22 07:37:07 +00:00
|
|
|
return util.Uint256{}
|
|
|
|
}
|
|
|
|
return t.root.Hash()
|
|
|
|
}
|
|
|
|
|
2022-01-27 11:25:11 +00:00
|
|
|
func makeStorageKey(mptKey util.Uint256) []byte {
|
|
|
|
return append([]byte{byte(storage.DataMPT)}, mptKey[:]...)
|
2020-05-22 07:37:07 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Flush puts every node (except Hash ones) in the trie to the storage.
|
|
|
|
// Because we care about block-level changes only, there is no need to put every
|
|
|
|
// new node to the storage. Normally, flush should be called with every StateRoot persist, i.e.
|
2020-05-22 07:37:07 +00:00
|
|
|
// after every block.
|
2022-01-28 12:05:13 +00:00
|
|
|
func (t *Trie) Flush(index uint32) {
|
2022-02-08 15:15:05 +00:00
|
|
|
key := makeStorageKey(util.Uint256{})
|
2020-10-21 13:58:41 +00:00
|
|
|
for h, node := range t.refcount {
|
|
|
|
if node.refcount != 0 {
|
2022-02-08 15:15:05 +00:00
|
|
|
copy(key[1:], h[:])
|
2020-10-21 13:58:41 +00:00
|
|
|
if node.bytes == nil {
|
|
|
|
panic("item not in trie")
|
|
|
|
}
|
2022-01-28 08:56:33 +00:00
|
|
|
if t.mode.RC() {
|
2022-02-08 15:15:05 +00:00
|
|
|
node.initial = t.updateRefCount(h, key, index)
|
2020-10-21 13:58:41 +00:00
|
|
|
if node.initial == 0 {
|
|
|
|
delete(t.refcount, h)
|
|
|
|
}
|
|
|
|
} else if node.refcount > 0 {
|
2022-02-16 14:48:15 +00:00
|
|
|
t.Store.Put(key, node.bytes)
|
2020-10-21 13:58:41 +00:00
|
|
|
}
|
|
|
|
node.refcount = 0
|
|
|
|
} else {
|
|
|
|
delete(t.refcount, h)
|
|
|
|
}
|
|
|
|
}
|
2020-05-22 07:37:07 +00:00
|
|
|
}
|
|
|
|
|
2022-01-28 12:05:13 +00:00
|
|
|
func IsActiveValue(v []byte) bool {
|
|
|
|
return len(v) > 4 && v[len(v)-5] == 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func getFromStore(key []byte, mode TrieMode, store *storage.MemCachedStore) ([]byte, error) {
|
|
|
|
data, err := store.Get(key)
|
|
|
|
if err == nil && mode.GC() && !IsActiveValue(data) {
|
|
|
|
return nil, storage.ErrKeyNotFound
|
|
|
|
}
|
|
|
|
return data, err
|
|
|
|
}
|
|
|
|
|
2020-10-21 13:58:41 +00:00
|
|
|
// updateRefCount should be called only when refcounting is enabled.
|
2022-02-08 15:15:05 +00:00
|
|
|
func (t *Trie) updateRefCount(h util.Uint256, key []byte, index uint32) int32 {
|
2022-01-28 08:56:33 +00:00
|
|
|
if !t.mode.RC() {
|
2020-10-21 13:58:41 +00:00
|
|
|
panic("`updateRefCount` is called, but GC is disabled")
|
|
|
|
}
|
|
|
|
var data []byte
|
|
|
|
node := t.refcount[h]
|
|
|
|
cnt := node.initial
|
|
|
|
if cnt == 0 {
|
|
|
|
// A newly created item which may be in store.
|
|
|
|
var err error
|
2022-01-28 12:05:13 +00:00
|
|
|
data, err = getFromStore(key, t.mode, t.Store)
|
2020-10-21 13:58:41 +00:00
|
|
|
if err == nil {
|
|
|
|
cnt = int32(binary.LittleEndian.Uint32(data[len(data)-4:]))
|
|
|
|
}
|
2020-06-02 21:02:45 +00:00
|
|
|
}
|
2020-10-21 13:58:41 +00:00
|
|
|
if len(data) == 0 {
|
2022-01-28 12:05:13 +00:00
|
|
|
data = append(node.bytes, 1, 0, 0, 0, 0)
|
2020-10-21 13:58:41 +00:00
|
|
|
}
|
|
|
|
cnt += node.refcount
|
|
|
|
switch {
|
|
|
|
case cnt < 0:
|
|
|
|
// BUG: negative reference count
|
|
|
|
panic(fmt.Sprintf("negative reference count: %s new %d, upd %d", h.StringBE(), cnt, t.refcount[h]))
|
|
|
|
case cnt == 0:
|
2022-01-28 12:05:13 +00:00
|
|
|
if !t.mode.GC() {
|
2022-02-16 14:48:15 +00:00
|
|
|
t.Store.Delete(key)
|
2022-01-28 12:05:13 +00:00
|
|
|
} else {
|
|
|
|
data[len(data)-5] = 0
|
|
|
|
binary.LittleEndian.PutUint32(data[len(data)-4:], index)
|
2022-02-16 14:48:15 +00:00
|
|
|
t.Store.Put(key, data)
|
2022-01-28 12:05:13 +00:00
|
|
|
}
|
2020-10-21 13:58:41 +00:00
|
|
|
default:
|
|
|
|
binary.LittleEndian.PutUint32(data[len(data)-4:], uint32(cnt))
|
2022-02-16 14:48:15 +00:00
|
|
|
t.Store.Put(key, data)
|
2020-10-21 13:58:41 +00:00
|
|
|
}
|
|
|
|
return cnt
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Trie) addRef(h util.Uint256, bs []byte) {
|
|
|
|
node := t.refcount[h]
|
|
|
|
if node == nil {
|
|
|
|
t.refcount[h] = &cachedNode{
|
|
|
|
refcount: 1,
|
2020-12-25 15:13:41 +00:00
|
|
|
bytes: bs,
|
2020-05-22 07:37:07 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2020-10-21 13:58:41 +00:00
|
|
|
node.refcount++
|
2020-12-25 15:13:41 +00:00
|
|
|
if node.bytes == nil {
|
|
|
|
node.bytes = bs
|
|
|
|
}
|
2020-05-22 07:37:07 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 13:58:41 +00:00
|
|
|
func (t *Trie) removeRef(h util.Uint256, bs []byte) {
|
|
|
|
node := t.refcount[h]
|
|
|
|
if node == nil {
|
|
|
|
t.refcount[h] = &cachedNode{
|
|
|
|
refcount: -1,
|
2020-12-25 15:13:41 +00:00
|
|
|
bytes: bs,
|
2020-10-21 13:58:41 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
node.refcount--
|
2020-12-25 15:13:41 +00:00
|
|
|
if node.bytes == nil {
|
|
|
|
node.bytes = bs
|
|
|
|
}
|
2020-05-22 07:37:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Trie) getFromStore(h util.Uint256) (Node, error) {
|
2022-01-28 12:05:13 +00:00
|
|
|
data, err := getFromStore(makeStorageKey(h), t.mode, t.Store)
|
2020-05-22 07:37:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var n NodeObject
|
|
|
|
r := io.NewBinReaderFromBuf(data)
|
|
|
|
n.DecodeBinary(r)
|
|
|
|
if r.Err != nil {
|
|
|
|
return nil, r.Err
|
|
|
|
}
|
2020-10-21 13:58:41 +00:00
|
|
|
|
2022-01-28 08:56:33 +00:00
|
|
|
if t.mode.RC() {
|
2022-01-28 12:05:13 +00:00
|
|
|
data = data[:len(data)-5]
|
2020-10-21 13:58:41 +00:00
|
|
|
node := t.refcount[h]
|
|
|
|
if node != nil {
|
2020-12-25 15:13:41 +00:00
|
|
|
node.bytes = data
|
2022-01-28 12:05:13 +00:00
|
|
|
_ = r.ReadB()
|
2020-10-21 13:58:41 +00:00
|
|
|
node.initial = int32(r.ReadU32LE())
|
|
|
|
}
|
|
|
|
}
|
2020-10-23 07:58:51 +00:00
|
|
|
n.Node.(flushedNode).setCache(data, h)
|
2020-05-22 07:37:07 +00:00
|
|
|
return n.Node, nil
|
|
|
|
}
|
2020-05-28 08:53:19 +00:00
|
|
|
|
|
|
|
// Collapse compresses all nodes at depth n to the hash nodes.
|
|
|
|
// Note: this function does not perform any kind of storage flushing so
|
|
|
|
// `Flush()` should be called explicitly before invoking function.
|
|
|
|
func (t *Trie) Collapse(depth int) {
|
|
|
|
if depth < 0 {
|
|
|
|
panic("negative depth")
|
|
|
|
}
|
|
|
|
t.root = collapse(depth, t.root)
|
2020-10-21 13:58:41 +00:00
|
|
|
t.refcount = make(map[util.Uint256]*cachedNode)
|
2020-05-28 08:53:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func collapse(depth int, node Node) Node {
|
2021-08-03 14:10:46 +00:00
|
|
|
switch node.(type) {
|
|
|
|
case *HashNode, EmptyNode:
|
2020-05-28 08:53:19 +00:00
|
|
|
return node
|
2021-08-03 14:10:46 +00:00
|
|
|
}
|
|
|
|
if depth == 0 {
|
2020-05-28 08:53:19 +00:00
|
|
|
return NewHashNode(node.Hash())
|
|
|
|
}
|
|
|
|
|
|
|
|
switch n := node.(type) {
|
|
|
|
case *BranchNode:
|
|
|
|
for i := range n.Children {
|
|
|
|
n.Children[i] = collapse(depth-1, n.Children[i])
|
|
|
|
}
|
|
|
|
case *ExtensionNode:
|
|
|
|
n.next = collapse(depth-1, n.next)
|
|
|
|
case *LeafNode:
|
|
|
|
case *HashNode:
|
|
|
|
default:
|
|
|
|
panic("invalid MPT node type")
|
|
|
|
}
|
|
|
|
return node
|
|
|
|
}
|
2021-10-07 13:56:27 +00:00
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Find returns a list of storage key-value pairs whose key is prefixed by the specified
|
2021-10-13 08:38:53 +00:00
|
|
|
// prefix starting from the specified `prefix`+`from` path (not including the item at
|
|
|
|
// the specified `prefix`+`from` path if so). The `max` number of elements is returned at max.
|
2021-10-07 13:56:27 +00:00
|
|
|
func (t *Trie) Find(prefix, from []byte, max int) ([]storage.KeyValue, error) {
|
|
|
|
if len(prefix) > MaxKeyLength {
|
|
|
|
return nil, errors.New("invalid prefix length")
|
|
|
|
}
|
2021-10-13 08:38:53 +00:00
|
|
|
if len(from) > MaxKeyLength-len(prefix) {
|
2021-10-07 13:56:27 +00:00
|
|
|
return nil, errors.New("invalid from length")
|
|
|
|
}
|
|
|
|
prefixP := toNibbles(prefix)
|
|
|
|
fromP := []byte{}
|
|
|
|
if len(from) > 0 {
|
2021-10-13 08:38:53 +00:00
|
|
|
fromP = toNibbles(from)
|
2021-10-07 13:56:27 +00:00
|
|
|
}
|
|
|
|
_, start, path, err := t.getWithPath(t.root, prefixP, false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to determine the start node: %w", err)
|
|
|
|
}
|
2021-10-13 07:22:57 +00:00
|
|
|
path = path[len(prefixP):]
|
|
|
|
|
|
|
|
if len(fromP) > 0 {
|
|
|
|
if len(path) <= len(fromP) && bytes.HasPrefix(fromP, path) {
|
|
|
|
fromP = fromP[len(path):]
|
|
|
|
} else if len(path) > len(fromP) && bytes.HasPrefix(path, fromP) {
|
|
|
|
fromP = []byte{}
|
|
|
|
} else {
|
|
|
|
cmp := bytes.Compare(path, fromP)
|
|
|
|
switch {
|
|
|
|
case cmp < 0:
|
|
|
|
return []storage.KeyValue{}, nil
|
|
|
|
case cmp > 0:
|
|
|
|
fromP = []byte{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-07 13:56:27 +00:00
|
|
|
|
|
|
|
var (
|
|
|
|
res []storage.KeyValue
|
|
|
|
count int
|
|
|
|
)
|
2022-01-28 08:56:33 +00:00
|
|
|
b := NewBillet(t.root.Hash(), t.mode, 0, t.Store)
|
2021-10-07 13:56:27 +00:00
|
|
|
process := func(pathToNode []byte, node Node, _ []byte) bool {
|
|
|
|
if leaf, ok := node.(*LeafNode); ok {
|
2021-10-13 08:38:53 +00:00
|
|
|
if from == nil || !bytes.Equal(pathToNode, from) { // (*Billet).traverse includes `from` path into result if so. Need to filter out manually.
|
2021-10-13 07:22:57 +00:00
|
|
|
res = append(res, storage.KeyValue{
|
2021-10-13 08:38:53 +00:00
|
|
|
Key: append(slice.Copy(prefix), pathToNode...),
|
2021-10-13 07:22:57 +00:00
|
|
|
Value: slice.Copy(leaf.value),
|
|
|
|
})
|
|
|
|
count++
|
|
|
|
}
|
2021-10-07 13:56:27 +00:00
|
|
|
}
|
|
|
|
return count >= max
|
|
|
|
}
|
2022-04-07 15:11:05 +00:00
|
|
|
_, err = b.traverse(start, path, fromP, process, false, false)
|
2021-10-07 13:56:27 +00:00
|
|
|
if err != nil && !errors.Is(err, errStop) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|