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
|
|
|
)
|
|
|
|
|
|
|
|
// Trie is an MPT trie storing all key-value pairs.
|
|
|
|
type Trie struct {
|
|
|
|
Store *storage.MemCachedStore
|
|
|
|
|
2020-10-21 13:58:41 +00:00
|
|
|
root Node
|
|
|
|
refcountEnabled bool
|
|
|
|
refcount map[util.Uint256]*cachedNode
|
|
|
|
}
|
|
|
|
|
|
|
|
type cachedNode struct {
|
|
|
|
bytes []byte
|
|
|
|
initial int32
|
|
|
|
refcount int32
|
2020-05-22 07:37:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ErrNotFound is returned when requested trie item is missing.
|
|
|
|
var ErrNotFound = errors.New("item not found")
|
|
|
|
|
|
|
|
// NewTrie returns new MPT trie. It accepts a MemCachedStore to decouple storage errors from logic errors
|
|
|
|
// so that all storage errors are processed during `store.Persist()` at the caller.
|
|
|
|
// This also has the benefit, that every `Put` can be considered an atomic operation.
|
2020-10-21 13:58:41 +00:00
|
|
|
func NewTrie(root Node, enableRefCount bool, 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
|
|
|
|
|
|
|
refcountEnabled: enableRefCount,
|
|
|
|
refcount: make(map[util.Uint256]*cachedNode),
|
2020-05-22 07:37:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns value for the provided key in t.
|
|
|
|
func (t *Trie) Get(key []byte) ([]byte, error) {
|
|
|
|
path := toNibbles(key)
|
|
|
|
r, bs, err := t.getWithPath(t.root, path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
t.root = r
|
|
|
|
return bs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// getWithPath returns value the provided path in a subtrie rooting in curr.
|
|
|
|
// It also returns a current node with all hash nodes along the path
|
|
|
|
// replaced to their "unhashed" counterparts.
|
|
|
|
func (t *Trie) getWithPath(curr Node, path []byte) (Node, []byte, error) {
|
|
|
|
switch n := curr.(type) {
|
|
|
|
case *LeafNode:
|
|
|
|
if len(path) == 0 {
|
2021-07-18 13:32:10 +00:00
|
|
|
return curr, slice.Copy(n.value), nil
|
2020-05-22 07:37:07 +00:00
|
|
|
}
|
|
|
|
case *BranchNode:
|
|
|
|
i, path := splitPath(path)
|
|
|
|
r, bs, err := t.getWithPath(n.Children[i], path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
n.Children[i] = r
|
|
|
|
return n, bs, 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 {
|
|
|
|
return t.getWithPath(r, path)
|
2020-05-22 07:37:07 +00:00
|
|
|
}
|
|
|
|
case *ExtensionNode:
|
|
|
|
if bytes.HasPrefix(path, n.key) {
|
|
|
|
r, bs, err := t.getWithPath(n.next, path[len(n.key):])
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
n.next = r
|
|
|
|
return curr, bs, err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic("invalid MPT node type")
|
|
|
|
}
|
|
|
|
return curr, nil, ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// putIntoLeaf puts val to trie if current node is a Leaf.
|
|
|
|
// It returns Node if curr needs to be replaced and error if any.
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// putIntoBranch puts val to trie if current node is a Branch.
|
|
|
|
// It returns Node if curr needs to be replaced and error if any.
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// putIntoExtension puts val to trie if current node is an Extension.
|
|
|
|
// It returns Node if curr needs to be replaced and error if any.
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-22 07:37:07 +00:00
|
|
|
// putIntoHash puts val to trie if current node is a HashNode.
|
|
|
|
// It returns Node if curr needs to be replaced and error if any.
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// newSubTrie create new trie containing node at 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
|
|
|
}
|
|
|
|
|
2020-10-21 13:58:41 +00:00
|
|
|
// putIntoNode puts val with provided path inside curr and returns updated node.
|
|
|
|
// 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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete removes key from trie.
|
|
|
|
// It returns no error on missing key.
|
|
|
|
func (t *Trie) Delete(key []byte) error {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-10-21 13:58:41 +00:00
|
|
|
// deleteFromNode removes value with provided path from curr and returns an updated node.
|
|
|
|
// 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()
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeStorageKey(mptKey []byte) []byte {
|
|
|
|
return append([]byte{byte(storage.DataMPT)}, mptKey...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush puts every node in the trie except Hash ones to the storage.
|
|
|
|
// Because we care only about block-level changes, there is no need to put every
|
|
|
|
// new node to storage. Normally, flush should be called with every StateRoot persist, i.e.
|
|
|
|
// after every block.
|
|
|
|
func (t *Trie) Flush() {
|
2020-10-21 13:58:41 +00:00
|
|
|
for h, node := range t.refcount {
|
|
|
|
if node.refcount != 0 {
|
|
|
|
if node.bytes == nil {
|
|
|
|
panic("item not in trie")
|
|
|
|
}
|
|
|
|
if t.refcountEnabled {
|
|
|
|
node.initial = t.updateRefCount(h)
|
|
|
|
if node.initial == 0 {
|
|
|
|
delete(t.refcount, h)
|
|
|
|
}
|
|
|
|
} else if node.refcount > 0 {
|
|
|
|
_ = t.Store.Put(makeStorageKey(h.BytesBE()), node.bytes)
|
|
|
|
}
|
|
|
|
node.refcount = 0
|
|
|
|
} else {
|
|
|
|
delete(t.refcount, h)
|
|
|
|
}
|
|
|
|
}
|
2020-05-22 07:37:07 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 13:58:41 +00:00
|
|
|
// updateRefCount should be called only when refcounting is enabled.
|
|
|
|
func (t *Trie) updateRefCount(h util.Uint256) int32 {
|
|
|
|
if !t.refcountEnabled {
|
|
|
|
panic("`updateRefCount` is called, but GC is disabled")
|
|
|
|
}
|
|
|
|
var data []byte
|
|
|
|
key := makeStorageKey(h.BytesBE())
|
|
|
|
node := t.refcount[h]
|
|
|
|
cnt := node.initial
|
|
|
|
if cnt == 0 {
|
|
|
|
// A newly created item which may be in store.
|
|
|
|
var err error
|
|
|
|
data, err = t.Store.Get(key)
|
|
|
|
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 {
|
|
|
|
data = append(node.bytes, 0, 0, 0, 0)
|
|
|
|
}
|
|
|
|
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:
|
|
|
|
_ = t.Store.Delete(key)
|
|
|
|
default:
|
|
|
|
binary.LittleEndian.PutUint32(data[len(data)-4:], uint32(cnt))
|
|
|
|
_ = t.Store.Put(key, data)
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
data, err := t.Store.Get(makeStorageKey(h.BytesBE()))
|
|
|
|
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
|
|
|
|
|
|
|
if t.refcountEnabled {
|
|
|
|
data = data[:len(data)-4]
|
|
|
|
node := t.refcount[h]
|
|
|
|
if node != nil {
|
2020-12-25 15:13:41 +00:00
|
|
|
node.bytes = data
|
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
|
|
|
|
}
|