2015-07-06 19:54:22 -05:00
|
|
|
// Copyright 2014 The go-ethereum Authors
|
2015-07-22 11:48:40 -05:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-06 19:54:22 -05:00
|
|
|
//
|
2015-07-23 11:35:11 -05:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-06 19:54:22 -05:00
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
2015-07-22 11:48:40 -05:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-06 19:54:22 -05:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 11:48:40 -05:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-06 19:54:22 -05:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 11:48:40 -05:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-06 19:54:22 -05:00
|
|
|
|
2015-07-06 22:08:16 -05:00
|
|
|
// Package trie implements Merkle Patricia Tries.
|
2014-10-31 08:45:03 -05:00
|
|
|
package trie
|
2014-02-14 16:56:09 -06:00
|
|
|
|
|
|
|
import (
|
2014-07-02 10:47:18 -05:00
|
|
|
"bytes"
|
2014-02-14 16:56:09 -06:00
|
|
|
"fmt"
|
2020-02-03 09:28:30 -06:00
|
|
|
"sync"
|
2014-08-04 03:38:18 -05:00
|
|
|
|
2015-03-16 05:27:38 -05:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2018-02-05 10:40:32 -06:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2017-02-22 06:10:07 -06:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2020-08-28 02:50:37 -05:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2014-02-14 16:56:09 -06:00
|
|
|
)
|
|
|
|
|
2015-07-05 18:19:48 -05:00
|
|
|
var (
|
2018-02-05 10:40:32 -06:00
|
|
|
// emptyRoot is the known root hash of an empty trie.
|
2015-07-05 18:19:48 -05:00
|
|
|
emptyRoot = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")
|
2018-02-05 10:40:32 -06:00
|
|
|
|
|
|
|
// emptyState is the known hash of an empty state trie entry.
|
|
|
|
emptyState = crypto.Keccak256Hash(nil)
|
2015-07-05 18:19:48 -05:00
|
|
|
)
|
2014-02-14 16:56:09 -06:00
|
|
|
|
2018-02-05 10:40:32 -06:00
|
|
|
// LeafCallback is a callback type invoked when a trie operation reaches a leaf
|
|
|
|
// node. It's used by state sync and commit to allow handling external references
|
|
|
|
// between account and storage tries.
|
2020-08-26 05:05:06 -05:00
|
|
|
type LeafCallback func(path []byte, leaf []byte, parent common.Hash) error
|
2014-02-14 16:56:09 -06:00
|
|
|
|
2015-07-05 18:19:48 -05:00
|
|
|
// Trie is a Merkle Patricia Trie.
|
|
|
|
// The zero value is an empty trie with no database.
|
|
|
|
// Use New to create a trie that sits on top of a database.
|
|
|
|
//
|
|
|
|
// Trie is not safe for concurrent use.
|
|
|
|
type Trie struct {
|
2018-10-08 06:16:16 -05:00
|
|
|
db *Database
|
|
|
|
root node
|
2020-02-04 06:02:38 -06:00
|
|
|
// Keep track of the number leafs which have been inserted since the last
|
|
|
|
// hashing operation. This number will not directly map to the number of
|
|
|
|
// actually unhashed nodes
|
|
|
|
unhashed int
|
2016-10-14 11:04:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// newFlag returns the cache flag value for a newly created node.
|
|
|
|
func (t *Trie) newFlag() nodeFlag {
|
2019-03-14 08:25:12 -05:00
|
|
|
return nodeFlag{dirty: true}
|
2014-02-24 05:11:00 -06:00
|
|
|
}
|
|
|
|
|
2015-07-05 18:19:48 -05:00
|
|
|
// New creates a trie with an existing root node from db.
|
|
|
|
//
|
|
|
|
// If root is the zero hash or the sha3 hash of an empty string, the
|
|
|
|
// trie is initially empty and does not require a database. Otherwise,
|
2015-11-25 11:28:21 -06:00
|
|
|
// New will panic if db is nil and returns a MissingNodeError if root does
|
|
|
|
// not exist in the database. Accessing the trie loads nodes from db on demand.
|
2018-02-05 10:40:32 -06:00
|
|
|
func New(root common.Hash, db *Database) (*Trie, error) {
|
|
|
|
if db == nil {
|
|
|
|
panic("trie.New called without a database")
|
|
|
|
}
|
|
|
|
trie := &Trie{
|
2018-10-08 06:16:16 -05:00
|
|
|
db: db,
|
2018-02-05 10:40:32 -06:00
|
|
|
}
|
2018-05-24 07:55:20 -05:00
|
|
|
if root != (common.Hash{}) && root != emptyRoot {
|
2017-06-20 11:26:09 -05:00
|
|
|
rootnode, err := trie.resolveHash(root[:], nil)
|
2016-10-17 14:31:27 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-07-05 18:19:48 -05:00
|
|
|
}
|
2016-10-17 14:31:27 -05:00
|
|
|
trie.root = rootnode
|
2014-02-14 16:56:09 -06:00
|
|
|
}
|
2015-07-05 18:19:48 -05:00
|
|
|
return trie, nil
|
2014-02-14 16:56:09 -06:00
|
|
|
}
|
|
|
|
|
2017-04-13 07:41:24 -05:00
|
|
|
// NodeIterator returns an iterator that returns nodes of the trie. Iteration starts at
|
|
|
|
// the key after the given start key.
|
|
|
|
func (t *Trie) NodeIterator(start []byte) NodeIterator {
|
|
|
|
return newNodeIterator(t, start)
|
2014-04-29 05:36:27 -05:00
|
|
|
}
|
|
|
|
|
2015-07-05 18:19:48 -05:00
|
|
|
// Get returns the value for key stored in the trie.
|
|
|
|
// The value bytes must not be modified by the caller.
|
|
|
|
func (t *Trie) Get(key []byte) []byte {
|
2015-11-25 11:28:21 -06:00
|
|
|
res, err := t.TryGet(key)
|
2017-02-22 06:10:07 -06:00
|
|
|
if err != nil {
|
|
|
|
log.Error(fmt.Sprintf("Unhandled trie error: %v", err))
|
2015-11-25 11:28:21 -06:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// TryGet returns the value for key stored in the trie.
|
|
|
|
// The value bytes must not be modified by the caller.
|
|
|
|
// If a node was not found in the database, a MissingNodeError is returned.
|
|
|
|
func (t *Trie) TryGet(key []byte) ([]byte, error) {
|
2020-08-28 02:50:37 -05:00
|
|
|
value, newroot, didResolve, err := t.tryGet(t.root, keybytesToHex(key), 0)
|
2016-09-25 13:49:02 -05:00
|
|
|
if err == nil && didResolve {
|
|
|
|
t.root = newroot
|
|
|
|
}
|
|
|
|
return value, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Trie) tryGet(origNode node, key []byte, pos int) (value []byte, newnode node, didResolve bool, err error) {
|
|
|
|
switch n := (origNode).(type) {
|
|
|
|
case nil:
|
|
|
|
return nil, nil, false, nil
|
|
|
|
case valueNode:
|
|
|
|
return n, n, false, nil
|
2016-10-14 11:04:33 -05:00
|
|
|
case *shortNode:
|
2016-09-25 13:49:02 -05:00
|
|
|
if len(key)-pos < len(n.Key) || !bytes.Equal(n.Key, key[pos:pos+len(n.Key)]) {
|
|
|
|
// key not found in trie
|
|
|
|
return nil, n, false, nil
|
|
|
|
}
|
|
|
|
value, newnode, didResolve, err = t.tryGet(n.Val, key, pos+len(n.Key))
|
|
|
|
if err == nil && didResolve {
|
2016-10-14 11:04:33 -05:00
|
|
|
n = n.copy()
|
2016-09-25 13:49:02 -05:00
|
|
|
n.Val = newnode
|
|
|
|
}
|
2016-10-14 11:04:33 -05:00
|
|
|
return value, n, didResolve, err
|
|
|
|
case *fullNode:
|
|
|
|
value, newnode, didResolve, err = t.tryGet(n.Children[key[pos]], key, pos+1)
|
2016-09-25 13:49:02 -05:00
|
|
|
if err == nil && didResolve {
|
2016-10-14 11:04:33 -05:00
|
|
|
n = n.copy()
|
2016-09-25 13:49:02 -05:00
|
|
|
n.Children[key[pos]] = newnode
|
|
|
|
}
|
2016-10-14 11:04:33 -05:00
|
|
|
return value, n, didResolve, err
|
2016-09-25 13:49:02 -05:00
|
|
|
case hashNode:
|
2017-06-20 11:26:09 -05:00
|
|
|
child, err := t.resolveHash(n, key[:pos])
|
2016-09-25 13:49:02 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, n, true, err
|
2015-07-05 18:19:48 -05:00
|
|
|
}
|
2016-09-25 13:49:02 -05:00
|
|
|
value, newnode, _, err := t.tryGet(child, key, pos)
|
|
|
|
return value, newnode, true, err
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("%T: invalid node: %v", origNode, origNode))
|
2014-10-29 08:20:42 -05:00
|
|
|
}
|
2014-02-14 16:56:09 -06:00
|
|
|
}
|
|
|
|
|
2020-08-28 02:50:37 -05:00
|
|
|
// TryGetNode attempts to retrieve a trie node by compact-encoded path. It is not
|
|
|
|
// possible to use keybyte-encoding as the path might contain odd nibbles.
|
|
|
|
func (t *Trie) TryGetNode(path []byte) ([]byte, int, error) {
|
|
|
|
item, newroot, resolved, err := t.tryGetNode(t.root, compactToHex(path), 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resolved, err
|
|
|
|
}
|
|
|
|
if resolved > 0 {
|
|
|
|
t.root = newroot
|
|
|
|
}
|
|
|
|
if item == nil {
|
|
|
|
return nil, resolved, nil
|
|
|
|
}
|
|
|
|
enc, err := rlp.EncodeToBytes(item)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Encoding existing trie node failed", "err", err)
|
|
|
|
return nil, resolved, err
|
|
|
|
}
|
|
|
|
return enc, resolved, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Trie) tryGetNode(origNode node, path []byte, pos int) (item node, newnode node, resolved int, err error) {
|
|
|
|
// If we reached the requested path, return the current node
|
|
|
|
if pos >= len(path) {
|
|
|
|
// Don't return collapsed hash nodes though
|
|
|
|
if _, ok := origNode.(hashNode); !ok {
|
|
|
|
// Short nodes have expanded keys, compact them before returning
|
|
|
|
item := origNode
|
|
|
|
if sn, ok := item.(*shortNode); ok {
|
|
|
|
item = &shortNode{
|
|
|
|
Key: hexToCompact(sn.Key),
|
|
|
|
Val: sn.Val,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return item, origNode, 0, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Path still needs to be traversed, descend into children
|
|
|
|
switch n := (origNode).(type) {
|
|
|
|
case nil:
|
|
|
|
// Non-existent path requested, abort
|
|
|
|
return nil, nil, 0, nil
|
|
|
|
|
|
|
|
case valueNode:
|
|
|
|
// Path prematurely ended, abort
|
|
|
|
return nil, nil, 0, nil
|
|
|
|
|
|
|
|
case *shortNode:
|
|
|
|
if len(path)-pos < len(n.Key) || !bytes.Equal(n.Key, path[pos:pos+len(n.Key)]) {
|
|
|
|
// Path branches off from short node
|
|
|
|
return nil, n, 0, nil
|
|
|
|
}
|
|
|
|
item, newnode, resolved, err = t.tryGetNode(n.Val, path, pos+len(n.Key))
|
|
|
|
if err == nil && resolved > 0 {
|
|
|
|
n = n.copy()
|
|
|
|
n.Val = newnode
|
|
|
|
}
|
|
|
|
return item, n, resolved, err
|
|
|
|
|
|
|
|
case *fullNode:
|
|
|
|
item, newnode, resolved, err = t.tryGetNode(n.Children[path[pos]], path, pos+1)
|
|
|
|
if err == nil && resolved > 0 {
|
|
|
|
n = n.copy()
|
|
|
|
n.Children[path[pos]] = newnode
|
|
|
|
}
|
|
|
|
return item, n, resolved, err
|
|
|
|
|
|
|
|
case hashNode:
|
|
|
|
child, err := t.resolveHash(n, path[:pos])
|
|
|
|
if err != nil {
|
|
|
|
return nil, n, 1, err
|
|
|
|
}
|
|
|
|
item, newnode, resolved, err := t.tryGetNode(child, path, pos)
|
|
|
|
return item, newnode, resolved + 1, err
|
|
|
|
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("%T: invalid node: %v", origNode, origNode))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-05 18:19:48 -05:00
|
|
|
// Update associates key with value in the trie. Subsequent calls to
|
|
|
|
// Get will return value. If value has length zero, any existing value
|
|
|
|
// is deleted from the trie and calls to Get will return nil.
|
|
|
|
//
|
|
|
|
// The value bytes must not be modified by the caller while they are
|
|
|
|
// stored in the trie.
|
|
|
|
func (t *Trie) Update(key, value []byte) {
|
2017-02-22 06:10:07 -06:00
|
|
|
if err := t.TryUpdate(key, value); err != nil {
|
|
|
|
log.Error(fmt.Sprintf("Unhandled trie error: %v", err))
|
2015-11-25 11:28:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TryUpdate associates key with value in the trie. Subsequent calls to
|
|
|
|
// Get will return value. If value has length zero, any existing value
|
|
|
|
// is deleted from the trie and calls to Get will return nil.
|
|
|
|
//
|
|
|
|
// The value bytes must not be modified by the caller while they are
|
|
|
|
// stored in the trie.
|
|
|
|
//
|
|
|
|
// If a node was not found in the database, a MissingNodeError is returned.
|
|
|
|
func (t *Trie) TryUpdate(key, value []byte) error {
|
2020-02-04 06:02:38 -06:00
|
|
|
t.unhashed++
|
2017-04-18 06:25:07 -05:00
|
|
|
k := keybytesToHex(key)
|
2015-01-08 04:47:04 -06:00
|
|
|
if len(value) != 0 {
|
2016-05-19 05:24:14 -05:00
|
|
|
_, n, err := t.insert(t.root, nil, k, valueNode(value))
|
2015-11-25 11:28:21 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
t.root = n
|
2014-11-18 05:03:09 -06:00
|
|
|
} else {
|
2016-05-19 05:24:14 -05:00
|
|
|
_, n, err := t.delete(t.root, nil, k)
|
2015-11-25 11:28:21 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
t.root = n
|
2014-11-18 05:03:09 -06:00
|
|
|
}
|
2015-11-25 11:28:21 -06:00
|
|
|
return nil
|
2014-07-02 06:40:02 -05:00
|
|
|
}
|
|
|
|
|
2016-05-19 05:24:14 -05:00
|
|
|
func (t *Trie) insert(n node, prefix, key []byte, value node) (bool, node, error) {
|
2015-01-08 04:47:04 -06:00
|
|
|
if len(key) == 0 {
|
2016-05-19 05:24:14 -05:00
|
|
|
if v, ok := n.(valueNode); ok {
|
|
|
|
return !bytes.Equal(v, value.(valueNode)), value, nil
|
|
|
|
}
|
|
|
|
return true, value, nil
|
2014-02-14 16:56:09 -06:00
|
|
|
}
|
2015-07-05 18:19:48 -05:00
|
|
|
switch n := n.(type) {
|
2016-10-14 11:04:33 -05:00
|
|
|
case *shortNode:
|
2015-07-05 18:19:48 -05:00
|
|
|
matchlen := prefixLen(key, n.Key)
|
|
|
|
// If the whole key matches, keep this short node as is
|
|
|
|
// and only update the value.
|
|
|
|
if matchlen == len(n.Key) {
|
2016-05-19 05:24:14 -05:00
|
|
|
dirty, nn, err := t.insert(n.Val, append(prefix, key[:matchlen]...), key[matchlen:], value)
|
2016-10-14 11:04:33 -05:00
|
|
|
if !dirty || err != nil {
|
|
|
|
return false, n, err
|
2016-05-19 05:24:14 -05:00
|
|
|
}
|
2016-10-14 11:04:33 -05:00
|
|
|
return true, &shortNode{n.Key, nn, t.newFlag()}, nil
|
2015-01-08 04:47:04 -06:00
|
|
|
}
|
2015-07-05 18:19:48 -05:00
|
|
|
// Otherwise branch out at the index where they differ.
|
2016-10-14 11:04:33 -05:00
|
|
|
branch := &fullNode{flags: t.newFlag()}
|
2015-11-25 11:28:21 -06:00
|
|
|
var err error
|
2016-05-19 05:24:14 -05:00
|
|
|
_, branch.Children[n.Key[matchlen]], err = t.insert(nil, append(prefix, n.Key[:matchlen+1]...), n.Key[matchlen+1:], n.Val)
|
2015-11-25 11:28:21 -06:00
|
|
|
if err != nil {
|
2016-05-19 05:24:14 -05:00
|
|
|
return false, nil, err
|
2015-11-25 11:28:21 -06:00
|
|
|
}
|
2016-05-19 05:24:14 -05:00
|
|
|
_, branch.Children[key[matchlen]], err = t.insert(nil, append(prefix, key[:matchlen+1]...), key[matchlen+1:], value)
|
2015-11-25 11:28:21 -06:00
|
|
|
if err != nil {
|
2016-05-19 05:24:14 -05:00
|
|
|
return false, nil, err
|
2015-11-25 11:28:21 -06:00
|
|
|
}
|
2015-07-05 18:19:48 -05:00
|
|
|
// Replace this shortNode with the branch if it occurs at index 0.
|
|
|
|
if matchlen == 0 {
|
2016-05-19 05:24:14 -05:00
|
|
|
return true, branch, nil
|
2014-02-14 16:56:09 -06:00
|
|
|
}
|
2015-07-05 18:19:48 -05:00
|
|
|
// Otherwise, replace it with a short node leading up to the branch.
|
2016-10-14 11:04:33 -05:00
|
|
|
return true, &shortNode{key[:matchlen], branch, t.newFlag()}, nil
|
2014-02-14 16:56:09 -06:00
|
|
|
|
2016-10-14 11:04:33 -05:00
|
|
|
case *fullNode:
|
2016-05-19 05:24:14 -05:00
|
|
|
dirty, nn, err := t.insert(n.Children[key[0]], append(prefix, key[0]), key[1:], value)
|
2016-10-14 11:04:33 -05:00
|
|
|
if !dirty || err != nil {
|
|
|
|
return false, n, err
|
2015-11-25 11:28:21 -06:00
|
|
|
}
|
2016-10-14 11:04:33 -05:00
|
|
|
n = n.copy()
|
2016-10-17 09:13:50 -05:00
|
|
|
n.flags = t.newFlag()
|
|
|
|
n.Children[key[0]] = nn
|
2016-05-19 05:24:14 -05:00
|
|
|
return true, n, nil
|
2014-02-14 16:56:09 -06:00
|
|
|
|
2015-07-05 18:19:48 -05:00
|
|
|
case nil:
|
2016-10-14 11:04:33 -05:00
|
|
|
return true, &shortNode{key, value, t.newFlag()}, nil
|
2014-02-14 16:56:09 -06:00
|
|
|
|
2015-07-05 18:19:48 -05:00
|
|
|
case hashNode:
|
|
|
|
// We've hit a part of the trie that isn't loaded yet. Load
|
|
|
|
// the node and insert into it. This leaves all child nodes on
|
|
|
|
// the path to the value in the trie.
|
2017-06-20 11:26:09 -05:00
|
|
|
rn, err := t.resolveHash(n, prefix)
|
2015-11-25 11:28:21 -06:00
|
|
|
if err != nil {
|
2016-05-19 05:24:14 -05:00
|
|
|
return false, nil, err
|
|
|
|
}
|
|
|
|
dirty, nn, err := t.insert(rn, prefix, key, value)
|
2016-10-14 11:04:33 -05:00
|
|
|
if !dirty || err != nil {
|
|
|
|
return false, rn, err
|
2016-05-19 05:24:14 -05:00
|
|
|
}
|
|
|
|
return true, nn, nil
|
2014-02-14 16:56:09 -06:00
|
|
|
|
2015-01-08 04:47:04 -06:00
|
|
|
default:
|
2015-07-05 18:19:48 -05:00
|
|
|
panic(fmt.Sprintf("%T: invalid node: %v", n, n))
|
2014-02-14 16:56:09 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-05 18:19:48 -05:00
|
|
|
// Delete removes any existing value for key from the trie.
|
|
|
|
func (t *Trie) Delete(key []byte) {
|
2017-02-22 06:10:07 -06:00
|
|
|
if err := t.TryDelete(key); err != nil {
|
|
|
|
log.Error(fmt.Sprintf("Unhandled trie error: %v", err))
|
2015-11-25 11:28:21 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TryDelete removes any existing value for key from the trie.
|
|
|
|
// If a node was not found in the database, a MissingNodeError is returned.
|
|
|
|
func (t *Trie) TryDelete(key []byte) error {
|
2020-02-04 06:02:38 -06:00
|
|
|
t.unhashed++
|
2017-04-18 06:25:07 -05:00
|
|
|
k := keybytesToHex(key)
|
2016-05-19 05:24:14 -05:00
|
|
|
_, n, err := t.delete(t.root, nil, k)
|
2015-11-25 11:28:21 -06:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
t.root = n
|
|
|
|
return nil
|
2014-02-14 16:56:09 -06:00
|
|
|
}
|
|
|
|
|
2015-07-05 18:19:48 -05:00
|
|
|
// delete returns the new root of the trie with key deleted.
|
|
|
|
// It reduces the trie to minimal form by simplifying
|
|
|
|
// nodes on the way up after deleting recursively.
|
2016-05-19 05:24:14 -05:00
|
|
|
func (t *Trie) delete(n node, prefix, key []byte) (bool, node, error) {
|
2015-07-05 18:19:48 -05:00
|
|
|
switch n := n.(type) {
|
2016-10-14 11:04:33 -05:00
|
|
|
case *shortNode:
|
2015-07-05 18:19:48 -05:00
|
|
|
matchlen := prefixLen(key, n.Key)
|
|
|
|
if matchlen < len(n.Key) {
|
2016-05-19 05:24:14 -05:00
|
|
|
return false, n, nil // don't replace n on mismatch
|
2015-07-05 18:19:48 -05:00
|
|
|
}
|
|
|
|
if matchlen == len(key) {
|
2016-05-19 05:24:14 -05:00
|
|
|
return true, nil, nil // remove n entirely for whole matches
|
2015-07-05 18:19:48 -05:00
|
|
|
}
|
|
|
|
// The key is longer than n.Key. Remove the remaining suffix
|
|
|
|
// from the subtrie. Child can never be nil here since the
|
|
|
|
// subtrie must contain at least two other values with keys
|
|
|
|
// longer than n.Key.
|
2016-05-19 05:24:14 -05:00
|
|
|
dirty, child, err := t.delete(n.Val, append(prefix, key[:len(n.Key)]...), key[len(n.Key):])
|
2016-10-14 11:04:33 -05:00
|
|
|
if !dirty || err != nil {
|
|
|
|
return false, n, err
|
2015-11-25 11:28:21 -06:00
|
|
|
}
|
2015-07-05 18:19:48 -05:00
|
|
|
switch child := child.(type) {
|
2016-10-14 11:04:33 -05:00
|
|
|
case *shortNode:
|
2015-07-05 18:19:48 -05:00
|
|
|
// Deleting from the subtrie reduced it to another
|
|
|
|
// short node. Merge the nodes to avoid creating a
|
|
|
|
// shortNode{..., shortNode{...}}. Use concat (which
|
|
|
|
// always creates a new slice) instead of append to
|
|
|
|
// avoid modifying n.Key since it might be shared with
|
|
|
|
// other nodes.
|
2016-10-14 11:04:33 -05:00
|
|
|
return true, &shortNode{concat(n.Key, child.Key...), child.Val, t.newFlag()}, nil
|
2015-07-05 18:19:48 -05:00
|
|
|
default:
|
2016-10-14 11:04:33 -05:00
|
|
|
return true, &shortNode{n.Key, child, t.newFlag()}, nil
|
2014-02-20 07:40:00 -06:00
|
|
|
}
|
|
|
|
|
2016-10-14 11:04:33 -05:00
|
|
|
case *fullNode:
|
2016-05-19 05:24:14 -05:00
|
|
|
dirty, nn, err := t.delete(n.Children[key[0]], append(prefix, key[0]), key[1:])
|
2016-10-14 11:04:33 -05:00
|
|
|
if !dirty || err != nil {
|
|
|
|
return false, n, err
|
2016-05-19 05:24:14 -05:00
|
|
|
}
|
2016-10-14 11:04:33 -05:00
|
|
|
n = n.copy()
|
2016-10-17 09:13:50 -05:00
|
|
|
n.flags = t.newFlag()
|
|
|
|
n.Children[key[0]] = nn
|
2016-05-19 05:24:14 -05:00
|
|
|
|
2015-07-05 18:19:48 -05:00
|
|
|
// Check how many non-nil entries are left after deleting and
|
|
|
|
// reduce the full node to a short node if only one entry is
|
|
|
|
// left. Since n must've contained at least two children
|
|
|
|
// before deletion (otherwise it would not be a full node) n
|
|
|
|
// can never be reduced to nil.
|
|
|
|
//
|
|
|
|
// When the loop is done, pos contains the index of the single
|
|
|
|
// value that is left in n or -2 if n contains at least two
|
|
|
|
// values.
|
2015-01-08 04:47:04 -06:00
|
|
|
pos := -1
|
2018-08-07 05:56:40 -05:00
|
|
|
for i, cld := range &n.Children {
|
2015-07-05 18:19:48 -05:00
|
|
|
if cld != nil {
|
2015-01-08 04:47:04 -06:00
|
|
|
if pos == -1 {
|
|
|
|
pos = i
|
2014-02-20 07:40:00 -06:00
|
|
|
} else {
|
2015-01-08 04:47:04 -06:00
|
|
|
pos = -2
|
2015-07-05 18:19:48 -05:00
|
|
|
break
|
2014-02-20 07:40:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-05 18:19:48 -05:00
|
|
|
if pos >= 0 {
|
|
|
|
if pos != 16 {
|
|
|
|
// If the remaining entry is a short node, it replaces
|
|
|
|
// n and its key gets the missing nibble tacked to the
|
|
|
|
// front. This avoids creating an invalid
|
|
|
|
// shortNode{..., shortNode{...}}. Since the entry
|
|
|
|
// might not be loaded yet, resolve it just for this
|
|
|
|
// check.
|
2017-06-20 11:26:09 -05:00
|
|
|
cnode, err := t.resolve(n.Children[pos], prefix)
|
2015-11-25 11:28:21 -06:00
|
|
|
if err != nil {
|
2016-05-19 05:24:14 -05:00
|
|
|
return false, nil, err
|
2015-11-25 11:28:21 -06:00
|
|
|
}
|
2016-10-14 11:04:33 -05:00
|
|
|
if cnode, ok := cnode.(*shortNode); ok {
|
2015-07-05 18:19:48 -05:00
|
|
|
k := append([]byte{byte(pos)}, cnode.Key...)
|
2016-10-14 11:04:33 -05:00
|
|
|
return true, &shortNode{k, cnode.Val, t.newFlag()}, nil
|
2015-07-05 18:19:48 -05:00
|
|
|
}
|
2015-01-08 04:47:04 -06:00
|
|
|
}
|
2015-07-05 18:19:48 -05:00
|
|
|
// Otherwise, n is replaced by a one-nibble short node
|
|
|
|
// containing the child.
|
2016-10-14 11:04:33 -05:00
|
|
|
return true, &shortNode{[]byte{byte(pos)}, n.Children[pos], t.newFlag()}, nil
|
2014-02-20 07:40:00 -06:00
|
|
|
}
|
2015-07-05 18:19:48 -05:00
|
|
|
// n still contains at least two values and cannot be reduced.
|
2016-05-19 05:24:14 -05:00
|
|
|
return true, n, nil
|
2014-02-20 07:40:00 -06:00
|
|
|
|
2016-09-29 09:51:32 -05:00
|
|
|
case valueNode:
|
|
|
|
return true, nil, nil
|
|
|
|
|
2015-01-08 04:47:04 -06:00
|
|
|
case nil:
|
2016-05-19 05:24:14 -05:00
|
|
|
return false, nil, nil
|
2015-07-05 18:19:48 -05:00
|
|
|
|
|
|
|
case hashNode:
|
|
|
|
// We've hit a part of the trie that isn't loaded yet. Load
|
|
|
|
// the node and delete from it. This leaves all child nodes on
|
|
|
|
// the path to the value in the trie.
|
2017-06-20 11:26:09 -05:00
|
|
|
rn, err := t.resolveHash(n, prefix)
|
2015-11-25 11:28:21 -06:00
|
|
|
if err != nil {
|
2016-05-19 05:24:14 -05:00
|
|
|
return false, nil, err
|
2015-11-25 11:28:21 -06:00
|
|
|
}
|
2016-05-19 05:24:14 -05:00
|
|
|
dirty, nn, err := t.delete(rn, prefix, key)
|
2016-10-14 11:04:33 -05:00
|
|
|
if !dirty || err != nil {
|
|
|
|
return false, rn, err
|
2016-05-19 05:24:14 -05:00
|
|
|
}
|
|
|
|
return true, nn, nil
|
2015-07-05 18:19:48 -05:00
|
|
|
|
2015-01-08 04:47:04 -06:00
|
|
|
default:
|
2015-07-05 18:19:48 -05:00
|
|
|
panic(fmt.Sprintf("%T: invalid node: %v (%v)", n, n, key))
|
2014-02-20 07:40:00 -06:00
|
|
|
}
|
2014-02-24 05:11:00 -06:00
|
|
|
}
|
|
|
|
|
2015-07-05 18:19:48 -05:00
|
|
|
func concat(s1 []byte, s2 ...byte) []byte {
|
|
|
|
r := make([]byte, len(s1)+len(s2))
|
|
|
|
copy(r, s1)
|
|
|
|
copy(r[len(s1):], s2)
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2017-06-20 11:26:09 -05:00
|
|
|
func (t *Trie) resolve(n node, prefix []byte) (node, error) {
|
2015-07-05 18:19:48 -05:00
|
|
|
if n, ok := n.(hashNode); ok {
|
2017-06-20 11:26:09 -05:00
|
|
|
return t.resolveHash(n, prefix)
|
2015-07-05 18:19:48 -05:00
|
|
|
}
|
2015-11-25 11:28:21 -06:00
|
|
|
return n, nil
|
2015-07-05 18:19:48 -05:00
|
|
|
}
|
|
|
|
|
2017-06-20 11:26:09 -05:00
|
|
|
func (t *Trie) resolveHash(n hashNode, prefix []byte) (node, error) {
|
2018-02-05 10:40:32 -06:00
|
|
|
hash := common.BytesToHash(n)
|
2019-03-14 08:25:12 -05:00
|
|
|
if node := t.db.node(hash); node != nil {
|
2018-06-21 04:28:05 -05:00
|
|
|
return node, nil
|
2015-07-05 18:19:48 -05:00
|
|
|
}
|
2018-06-21 04:28:05 -05:00
|
|
|
return nil, &MissingNodeError{NodeHash: hash, Path: prefix}
|
2015-07-05 18:19:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Hash returns the root hash of the trie. It does not write to the
|
|
|
|
// database and can be used even if the trie doesn't have one.
|
|
|
|
func (t *Trie) Hash() common.Hash {
|
2020-02-04 06:02:38 -06:00
|
|
|
hash, cached, _ := t.hashRoot(nil)
|
2016-05-19 05:24:14 -05:00
|
|
|
t.root = cached
|
|
|
|
return common.BytesToHash(hash.(hashNode))
|
2014-02-24 05:11:00 -06:00
|
|
|
}
|
|
|
|
|
2018-02-05 10:40:32 -06:00
|
|
|
// Commit writes all nodes to the trie's memory database, tracking the internal
|
|
|
|
// and external (for account tries) references.
|
|
|
|
func (t *Trie) Commit(onleaf LeafCallback) (root common.Hash, err error) {
|
2015-07-05 18:19:48 -05:00
|
|
|
if t.db == nil {
|
2018-02-05 10:40:32 -06:00
|
|
|
panic("commit called on trie with nil database")
|
2014-02-24 05:11:00 -06:00
|
|
|
}
|
2020-02-03 09:28:30 -06:00
|
|
|
if t.root == nil {
|
|
|
|
return emptyRoot, nil
|
|
|
|
}
|
|
|
|
rootHash := t.Hash()
|
|
|
|
h := newCommitter()
|
|
|
|
defer returnCommitterToPool(h)
|
|
|
|
// Do a quick check if we really need to commit, before we spin
|
|
|
|
// up goroutines. This can happen e.g. if we load a trie for reading storage
|
|
|
|
// values, but don't write to it.
|
|
|
|
if !h.commitNeeded(t.root) {
|
|
|
|
return rootHash, nil
|
|
|
|
}
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
if onleaf != nil {
|
|
|
|
h.onleaf = onleaf
|
|
|
|
h.leafCh = make(chan *leaf, leafChanSize)
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
h.commitLoop(t.db)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
var newRoot hashNode
|
|
|
|
newRoot, err = h.Commit(t.root, t.db)
|
|
|
|
if onleaf != nil {
|
|
|
|
// The leafch is created in newCommitter if there was an onleaf callback
|
|
|
|
// provided. The commitLoop only _reads_ from it, and the commit
|
|
|
|
// operation was the sole writer. Therefore, it's safe to close this
|
|
|
|
// channel here.
|
|
|
|
close(h.leafCh)
|
|
|
|
wg.Wait()
|
|
|
|
}
|
2015-07-05 18:19:48 -05:00
|
|
|
if err != nil {
|
2018-01-03 06:14:47 -06:00
|
|
|
return common.Hash{}, err
|
2015-07-05 18:19:48 -05:00
|
|
|
}
|
2020-02-03 09:28:30 -06:00
|
|
|
t.root = newRoot
|
|
|
|
return rootHash, nil
|
2015-07-05 18:19:48 -05:00
|
|
|
}
|
2014-05-26 18:08:51 -05:00
|
|
|
|
2020-02-03 09:28:30 -06:00
|
|
|
// hashRoot calculates the root hash of the given trie
|
2020-02-04 06:02:38 -06:00
|
|
|
func (t *Trie) hashRoot(db *Database) (node, node, error) {
|
2015-07-05 18:19:48 -05:00
|
|
|
if t.root == nil {
|
2016-05-19 05:24:14 -05:00
|
|
|
return hashNode(emptyRoot.Bytes()), nil, nil
|
2015-07-05 18:19:48 -05:00
|
|
|
}
|
2020-02-04 06:02:38 -06:00
|
|
|
// If the number of changes is below 100, we let one thread handle it
|
|
|
|
h := newHasher(t.unhashed >= 100)
|
2018-01-15 07:32:14 -06:00
|
|
|
defer returnHasherToPool(h)
|
2020-02-03 09:28:30 -06:00
|
|
|
hashed, cached := h.hash(t.root, true)
|
2020-02-04 06:02:38 -06:00
|
|
|
t.unhashed = 0
|
2020-02-03 09:28:30 -06:00
|
|
|
return hashed, cached, nil
|
2014-05-26 18:08:51 -05:00
|
|
|
}
|
2020-08-21 07:10:40 -05:00
|
|
|
|
|
|
|
// Reset drops the referenced root node and cleans all internal state.
|
|
|
|
func (t *Trie) Reset() {
|
|
|
|
t.root = nil
|
|
|
|
t.unhashed = 0
|
|
|
|
}
|