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"
|
2015-07-05 18:19:48 -05:00
|
|
|
"hash"
|
2014-08-04 03:38:18 -05:00
|
|
|
|
2015-03-16 05:27:38 -05:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2015-10-07 04:14:30 -05:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2015-07-05 18:19:48 -05:00
|
|
|
"github.com/ethereum/go-ethereum/crypto/sha3"
|
|
|
|
"github.com/ethereum/go-ethereum/logger"
|
|
|
|
"github.com/ethereum/go-ethereum/logger/glog"
|
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
2014-02-14 16:56:09 -06:00
|
|
|
)
|
|
|
|
|
2015-07-05 18:19:48 -05:00
|
|
|
const defaultCacheCapacity = 800
|
2014-07-02 10:47:18 -05:00
|
|
|
|
2015-07-05 18:19:48 -05:00
|
|
|
var (
|
|
|
|
// The global cache stores decoded trie nodes by hash as they get loaded.
|
|
|
|
globalCache = newARC(defaultCacheCapacity)
|
2015-10-07 04:14:30 -05:00
|
|
|
|
2015-07-05 18:19:48 -05:00
|
|
|
// This is the known root hash of an empty trie.
|
|
|
|
emptyRoot = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421")
|
2015-10-07 04:14:30 -05:00
|
|
|
|
|
|
|
// This is the known hash of an empty state trie entry.
|
2016-02-21 12:40:27 -06:00
|
|
|
emptyState = crypto.Keccak256Hash(nil)
|
2015-07-05 18:19:48 -05:00
|
|
|
)
|
2014-02-14 16:56:09 -06:00
|
|
|
|
2015-11-25 11:28:21 -06:00
|
|
|
// ClearGlobalCache clears the global trie cache
|
|
|
|
func ClearGlobalCache() {
|
|
|
|
globalCache.Clear()
|
|
|
|
}
|
2014-02-14 16:56:09 -06:00
|
|
|
|
2015-07-05 18:19:48 -05:00
|
|
|
// Database must be implemented by backing stores for the trie.
|
|
|
|
type Database interface {
|
|
|
|
DatabaseWriter
|
|
|
|
// Get returns the value for key from the database.
|
|
|
|
Get(key []byte) (value []byte, err error)
|
2014-06-30 06:08:00 -05:00
|
|
|
}
|
|
|
|
|
2015-07-05 18:19:48 -05:00
|
|
|
// DatabaseWriter wraps the Put method of a backing store for the trie.
|
|
|
|
type DatabaseWriter interface {
|
|
|
|
// Put stores the mapping key->value in the database.
|
|
|
|
// Implementations must not hold onto the value bytes, the trie
|
|
|
|
// will reuse the slice across calls to Put.
|
|
|
|
Put(key, value []byte) 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 {
|
2015-11-25 11:28:21 -06:00
|
|
|
root node
|
|
|
|
db Database
|
|
|
|
originalRoot common.Hash
|
2015-07-05 18:19:48 -05:00
|
|
|
*hasher
|
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.
|
2015-07-05 18:19:48 -05:00
|
|
|
func New(root common.Hash, db Database) (*Trie, error) {
|
2015-11-25 11:28:21 -06:00
|
|
|
trie := &Trie{db: db, originalRoot: root}
|
2015-07-05 18:19:48 -05:00
|
|
|
if (root != common.Hash{}) && root != emptyRoot {
|
|
|
|
if db == nil {
|
|
|
|
panic("trie.New: cannot use existing root without a database")
|
2014-02-14 16:56:09 -06:00
|
|
|
}
|
2015-07-05 18:19:48 -05:00
|
|
|
if v, _ := trie.db.Get(root[:]); len(v) == 0 {
|
2015-11-25 11:28:21 -06:00
|
|
|
return nil, &MissingNodeError{
|
|
|
|
RootHash: root,
|
|
|
|
NodeHash: root,
|
|
|
|
}
|
2015-07-05 18:19:48 -05:00
|
|
|
}
|
|
|
|
trie.root = hashNode(root.Bytes())
|
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
|
|
|
}
|
|
|
|
|
2015-07-05 18:19:48 -05:00
|
|
|
// Iterator returns an iterator over all mappings in the trie.
|
|
|
|
func (t *Trie) Iterator() *Iterator {
|
|
|
|
return NewIterator(t)
|
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)
|
|
|
|
if err != nil && glog.V(logger.Error) {
|
|
|
|
glog.Errorf("Unhandled trie error: %v", err)
|
|
|
|
}
|
|
|
|
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) {
|
2015-07-05 18:19:48 -05:00
|
|
|
key = compactHexDecode(key)
|
2015-11-25 11:28:21 -06:00
|
|
|
pos := 0
|
2015-07-05 18:19:48 -05:00
|
|
|
tn := t.root
|
2015-11-25 11:28:21 -06:00
|
|
|
for pos < len(key) {
|
2015-07-05 18:19:48 -05:00
|
|
|
switch n := tn.(type) {
|
|
|
|
case shortNode:
|
2015-11-25 11:28:21 -06:00
|
|
|
if len(key)-pos < len(n.Key) || !bytes.Equal(n.Key, key[pos:pos+len(n.Key)]) {
|
|
|
|
return nil, nil
|
2015-07-05 18:19:48 -05:00
|
|
|
}
|
|
|
|
tn = n.Val
|
2015-11-25 11:28:21 -06:00
|
|
|
pos += len(n.Key)
|
2015-07-05 18:19:48 -05:00
|
|
|
case fullNode:
|
2016-05-19 05:24:14 -05:00
|
|
|
tn = n.Children[key[pos]]
|
2015-11-25 11:28:21 -06:00
|
|
|
pos++
|
2015-07-05 18:19:48 -05:00
|
|
|
case nil:
|
2015-11-25 11:28:21 -06:00
|
|
|
return nil, nil
|
2015-07-05 18:19:48 -05:00
|
|
|
case hashNode:
|
2015-11-25 11:28:21 -06:00
|
|
|
var err error
|
|
|
|
tn, err = t.resolveHash(n, key[:pos], key[pos:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-07-05 18:19:48 -05:00
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("%T: invalid node: %v", tn, tn))
|
|
|
|
}
|
2014-10-29 08:20:42 -05:00
|
|
|
}
|
2015-11-25 11:28:21 -06:00
|
|
|
return tn.(valueNode), nil
|
2014-02-14 16:56:09 -06:00
|
|
|
}
|
|
|
|
|
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) {
|
2015-11-25 11:28:21 -06:00
|
|
|
if err := t.TryUpdate(key, value); err != nil && glog.V(logger.Error) {
|
|
|
|
glog.Errorf("Unhandled trie error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2015-07-05 18:19:48 -05:00
|
|
|
k := compactHexDecode(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) {
|
|
|
|
case shortNode:
|
|
|
|
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)
|
2015-11-25 11:28:21 -06:00
|
|
|
if err != nil {
|
2016-05-19 05:24:14 -05:00
|
|
|
return false, nil, err
|
|
|
|
}
|
|
|
|
if !dirty {
|
|
|
|
return false, n, nil
|
2015-11-25 11:28:21 -06:00
|
|
|
}
|
2016-05-19 05:24:14 -05:00
|
|
|
return true, shortNode{n.Key, nn, nil, true}, 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-05-19 05:24:14 -05:00
|
|
|
branch := fullNode{dirty: true}
|
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-05-19 05:24:14 -05:00
|
|
|
return true, shortNode{key[:matchlen], branch, nil, true}, nil
|
2014-02-14 16:56:09 -06:00
|
|
|
|
2015-07-05 18:19:48 -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)
|
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
|
|
|
if !dirty {
|
|
|
|
return false, n, nil
|
|
|
|
}
|
|
|
|
n.Children[key[0]], n.hash, n.dirty = nn, nil, true
|
|
|
|
return true, n, nil
|
2014-02-14 16:56:09 -06:00
|
|
|
|
2015-07-05 18:19:48 -05:00
|
|
|
case nil:
|
2016-05-19 05:24:14 -05:00
|
|
|
return true, shortNode{key, value, nil, true}, 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.
|
2015-11-25 11:28:21 -06:00
|
|
|
rn, err := t.resolveHash(n, prefix, key)
|
|
|
|
if err != nil {
|
2016-05-19 05:24:14 -05:00
|
|
|
return false, nil, err
|
|
|
|
}
|
|
|
|
dirty, nn, err := t.insert(rn, prefix, key, value)
|
|
|
|
if err != nil {
|
|
|
|
return false, nil, err
|
2015-11-25 11:28:21 -06:00
|
|
|
}
|
2016-05-19 05:24:14 -05:00
|
|
|
if !dirty {
|
|
|
|
return false, rn, nil
|
|
|
|
}
|
|
|
|
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) {
|
2015-11-25 11:28:21 -06:00
|
|
|
if err := t.TryDelete(key); err != nil && glog.V(logger.Error) {
|
|
|
|
glog.Errorf("Unhandled trie error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2015-07-05 18:19:48 -05:00
|
|
|
k := compactHexDecode(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) {
|
|
|
|
case shortNode:
|
|
|
|
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):])
|
2015-11-25 11:28:21 -06:00
|
|
|
if err != nil {
|
2016-05-19 05:24:14 -05:00
|
|
|
return false, nil, err
|
|
|
|
}
|
|
|
|
if !dirty {
|
|
|
|
return false, n, nil
|
2015-11-25 11:28:21 -06:00
|
|
|
}
|
2015-07-05 18:19:48 -05:00
|
|
|
switch child := child.(type) {
|
|
|
|
case shortNode:
|
|
|
|
// 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-05-19 05:24:14 -05:00
|
|
|
return true, shortNode{concat(n.Key, child.Key...), child.Val, nil, true}, nil
|
2015-07-05 18:19:48 -05:00
|
|
|
default:
|
2016-05-19 05:24:14 -05:00
|
|
|
return true, shortNode{n.Key, child, nil, true}, nil
|
2014-02-20 07:40:00 -06:00
|
|
|
}
|
|
|
|
|
2015-07-05 18:19:48 -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:])
|
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
|
|
|
if !dirty {
|
|
|
|
return false, n, nil
|
|
|
|
}
|
|
|
|
n.Children[key[0]], n.hash, n.dirty = nn, nil, true
|
|
|
|
|
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
|
2016-05-19 05:24:14 -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.
|
2016-05-19 05:24:14 -05:00
|
|
|
cnode, err := t.resolve(n.Children[pos], prefix, []byte{byte(pos)})
|
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
|
|
|
if cnode, ok := cnode.(shortNode); ok {
|
|
|
|
k := append([]byte{byte(pos)}, cnode.Key...)
|
2016-05-19 05:24:14 -05:00
|
|
|
return true, shortNode{k, cnode.Val, nil, true}, 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-05-19 05:24:14 -05:00
|
|
|
return true, shortNode{[]byte{byte(pos)}, n.Children[pos], nil, true}, 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
|
|
|
|
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.
|
2015-11-25 11:28:21 -06:00
|
|
|
rn, err := t.resolveHash(n, prefix, key)
|
|
|
|
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)
|
|
|
|
if err != nil {
|
|
|
|
return false, nil, err
|
|
|
|
}
|
|
|
|
if !dirty {
|
|
|
|
return false, rn, nil
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-11-25 11:28:21 -06:00
|
|
|
func (t *Trie) resolve(n node, prefix, suffix []byte) (node, error) {
|
2015-07-05 18:19:48 -05:00
|
|
|
if n, ok := n.(hashNode); ok {
|
2015-11-25 11:28:21 -06:00
|
|
|
return t.resolveHash(n, prefix, suffix)
|
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
|
|
|
}
|
|
|
|
|
2015-11-25 11:28:21 -06:00
|
|
|
func (t *Trie) resolveHash(n hashNode, prefix, suffix []byte) (node, error) {
|
2015-07-05 18:19:48 -05:00
|
|
|
if v, ok := globalCache.Get(n); ok {
|
2015-11-25 11:28:21 -06:00
|
|
|
return v, nil
|
2015-07-05 18:19:48 -05:00
|
|
|
}
|
|
|
|
enc, err := t.db.Get(n)
|
|
|
|
if err != nil || enc == nil {
|
2015-11-25 11:28:21 -06:00
|
|
|
return nil, &MissingNodeError{
|
|
|
|
RootHash: t.originalRoot,
|
|
|
|
NodeHash: common.BytesToHash(n),
|
2015-11-30 06:34:19 -06:00
|
|
|
Key: compactHexEncode(append(prefix, suffix...)),
|
|
|
|
PrefixLen: len(prefix),
|
|
|
|
SuffixLen: len(suffix),
|
2014-02-24 05:11:00 -06:00
|
|
|
}
|
2015-07-05 18:19:48 -05:00
|
|
|
}
|
|
|
|
dec := mustDecodeNode(n, enc)
|
|
|
|
if dec != nil {
|
|
|
|
globalCache.Put(n, dec)
|
2014-02-24 05:11:00 -06:00
|
|
|
}
|
2015-11-25 11:28:21 -06:00
|
|
|
return dec, nil
|
2015-07-05 18:19:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Root returns the root hash of the trie.
|
|
|
|
// Deprecated: use Hash instead.
|
|
|
|
func (t *Trie) Root() []byte { return t.Hash().Bytes() }
|
2014-02-24 05:11:00 -06:00
|
|
|
|
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 {
|
2016-05-19 05:24:14 -05:00
|
|
|
hash, cached, _ := t.hashRoot(nil)
|
|
|
|
t.root = cached
|
|
|
|
return common.BytesToHash(hash.(hashNode))
|
2014-02-24 05:11:00 -06:00
|
|
|
}
|
|
|
|
|
2015-07-05 18:19:48 -05:00
|
|
|
// Commit writes all nodes to the trie's database.
|
|
|
|
// Nodes are stored with their sha3 hash as the key.
|
|
|
|
//
|
|
|
|
// Committing flushes nodes from memory.
|
|
|
|
// Subsequent Get calls will load nodes from the database.
|
|
|
|
func (t *Trie) Commit() (root common.Hash, err error) {
|
|
|
|
if t.db == nil {
|
|
|
|
panic("Commit called on trie with nil database")
|
2014-02-24 05:11:00 -06:00
|
|
|
}
|
2015-07-05 18:19:48 -05:00
|
|
|
return t.CommitTo(t.db)
|
2014-02-24 05:11:00 -06:00
|
|
|
}
|
|
|
|
|
2015-07-05 18:19:48 -05:00
|
|
|
// CommitTo writes all nodes to the given database.
|
|
|
|
// Nodes are stored with their sha3 hash as the key.
|
|
|
|
//
|
|
|
|
// Committing flushes nodes from memory. Subsequent Get calls will
|
|
|
|
// load nodes from the trie's database. Calling code must ensure that
|
|
|
|
// the changes made to db are written back to the trie's attached
|
|
|
|
// database before using the trie.
|
|
|
|
func (t *Trie) CommitTo(db DatabaseWriter) (root common.Hash, err error) {
|
2016-05-19 05:24:14 -05:00
|
|
|
hash, cached, err := t.hashRoot(db)
|
2015-07-05 18:19:48 -05:00
|
|
|
if err != nil {
|
|
|
|
return (common.Hash{}), err
|
|
|
|
}
|
2016-05-19 05:24:14 -05:00
|
|
|
t.root = cached
|
|
|
|
return common.BytesToHash(hash.(hashNode)), nil
|
2015-07-05 18:19:48 -05:00
|
|
|
}
|
2014-05-26 18:08:51 -05:00
|
|
|
|
2016-05-19 05:24:14 -05:00
|
|
|
func (t *Trie) hashRoot(db DatabaseWriter) (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
|
|
|
}
|
|
|
|
if t.hasher == nil {
|
|
|
|
t.hasher = newHasher()
|
2015-01-08 04:47:04 -06:00
|
|
|
}
|
2015-07-05 18:19:48 -05:00
|
|
|
return t.hasher.hash(t.root, db, true)
|
|
|
|
}
|
2014-05-26 18:08:51 -05:00
|
|
|
|
2015-07-05 18:19:48 -05:00
|
|
|
type hasher struct {
|
|
|
|
tmp *bytes.Buffer
|
|
|
|
sha hash.Hash
|
2014-05-26 18:08:51 -05:00
|
|
|
}
|
|
|
|
|
2015-07-05 18:19:48 -05:00
|
|
|
func newHasher() *hasher {
|
|
|
|
return &hasher{tmp: new(bytes.Buffer), sha: sha3.NewKeccak256()}
|
|
|
|
}
|
|
|
|
|
2016-05-19 05:24:14 -05:00
|
|
|
// hash collapses a node down into a hash node, also returning a copy of the
|
|
|
|
// original node initialzied with the computed hash to replace the original one.
|
|
|
|
func (h *hasher) hash(n node, db DatabaseWriter, force bool) (node, node, error) {
|
|
|
|
// If we're not storing the node, just hashing, use avaialble cached data
|
|
|
|
if hash, dirty := n.cache(); hash != nil && (db == nil || !dirty) {
|
|
|
|
return hash, n, nil
|
|
|
|
}
|
|
|
|
// Trie not processed yet or needs storage, walk the children
|
|
|
|
collapsed, cached, err := h.hashChildren(n, db)
|
2015-07-05 18:19:48 -05:00
|
|
|
if err != nil {
|
2016-05-19 05:24:14 -05:00
|
|
|
return hashNode{}, n, err
|
2015-07-05 18:19:48 -05:00
|
|
|
}
|
2016-05-19 05:24:14 -05:00
|
|
|
hashed, err := h.store(collapsed, db, force)
|
|
|
|
if err != nil {
|
|
|
|
return hashNode{}, n, err
|
2015-07-05 18:19:48 -05:00
|
|
|
}
|
2016-05-19 05:24:14 -05:00
|
|
|
// Cache the hash and RLP blob of the ndoe for later reuse
|
|
|
|
if hash, ok := hashed.(hashNode); ok && !force {
|
|
|
|
switch cached := cached.(type) {
|
|
|
|
case shortNode:
|
|
|
|
cached.hash = hash
|
|
|
|
if db != nil {
|
|
|
|
cached.dirty = false
|
|
|
|
}
|
|
|
|
return hashed, cached, nil
|
|
|
|
case fullNode:
|
|
|
|
cached.hash = hash
|
|
|
|
if db != nil {
|
|
|
|
cached.dirty = false
|
|
|
|
}
|
|
|
|
return hashed, cached, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hashed, cached, nil
|
2015-07-05 18:19:48 -05:00
|
|
|
}
|
|
|
|
|
2016-05-19 05:24:14 -05:00
|
|
|
// hashChildren replaces the children of a node with their hashes if the encoded
|
|
|
|
// size of the child is larger than a hash, returning the collapsed node as well
|
|
|
|
// as a replacement for the original node with the child hashes cached in.
|
|
|
|
func (h *hasher) hashChildren(original node, db DatabaseWriter) (node, node, error) {
|
2015-07-05 18:19:48 -05:00
|
|
|
var err error
|
2016-05-19 05:24:14 -05:00
|
|
|
|
|
|
|
switch n := original.(type) {
|
2015-07-05 18:19:48 -05:00
|
|
|
case shortNode:
|
2016-05-19 05:24:14 -05:00
|
|
|
// Hash the short node's child, caching the newly hashed subtree
|
|
|
|
cached := n
|
|
|
|
cached.Key = common.CopyBytes(cached.Key)
|
|
|
|
|
2015-07-05 18:19:48 -05:00
|
|
|
n.Key = compactEncode(n.Key)
|
|
|
|
if _, ok := n.Val.(valueNode); !ok {
|
2016-05-19 05:24:14 -05:00
|
|
|
if n.Val, cached.Val, err = h.hash(n.Val, db, false); err != nil {
|
|
|
|
return n, original, err
|
2015-07-05 18:19:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if n.Val == nil {
|
2016-05-19 05:24:14 -05:00
|
|
|
n.Val = valueNode(nil) // Ensure that nil children are encoded as empty strings.
|
2015-07-05 18:19:48 -05:00
|
|
|
}
|
2016-05-19 05:24:14 -05:00
|
|
|
return n, cached, nil
|
|
|
|
|
2015-07-05 18:19:48 -05:00
|
|
|
case fullNode:
|
2016-05-19 05:24:14 -05:00
|
|
|
// Hash the full node's children, caching the newly hashed subtrees
|
|
|
|
cached := fullNode{dirty: n.dirty}
|
|
|
|
|
2015-07-05 18:19:48 -05:00
|
|
|
for i := 0; i < 16; i++ {
|
2016-05-19 05:24:14 -05:00
|
|
|
if n.Children[i] != nil {
|
|
|
|
if n.Children[i], cached.Children[i], err = h.hash(n.Children[i], db, false); err != nil {
|
|
|
|
return n, original, err
|
2015-07-05 18:19:48 -05:00
|
|
|
}
|
|
|
|
} else {
|
2016-05-19 05:24:14 -05:00
|
|
|
n.Children[i] = valueNode(nil) // Ensure that nil children are encoded as empty strings.
|
2015-07-05 18:19:48 -05:00
|
|
|
}
|
|
|
|
}
|
2016-05-19 05:24:14 -05:00
|
|
|
cached.Children[16] = n.Children[16]
|
|
|
|
if n.Children[16] == nil {
|
|
|
|
n.Children[16] = valueNode(nil)
|
2015-07-05 18:19:48 -05:00
|
|
|
}
|
2016-05-19 05:24:14 -05:00
|
|
|
return n, cached, nil
|
|
|
|
|
2015-07-05 18:19:48 -05:00
|
|
|
default:
|
2016-05-19 05:24:14 -05:00
|
|
|
// Value and hash nodes don't have children so they're left as were
|
|
|
|
return n, original, nil
|
2015-07-05 18:19:48 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *hasher) store(n node, db DatabaseWriter, force bool) (node, error) {
|
|
|
|
// Don't store hashes or empty nodes.
|
|
|
|
if _, isHash := n.(hashNode); n == nil || isHash {
|
|
|
|
return n, nil
|
|
|
|
}
|
2016-05-19 05:24:14 -05:00
|
|
|
// Generate the RLP encoding of the node
|
2015-07-05 18:19:48 -05:00
|
|
|
h.tmp.Reset()
|
|
|
|
if err := rlp.Encode(h.tmp, n); err != nil {
|
|
|
|
panic("encode error: " + err.Error())
|
|
|
|
}
|
|
|
|
if h.tmp.Len() < 32 && !force {
|
2016-05-19 05:24:14 -05:00
|
|
|
return n, nil // Nodes smaller than 32 bytes are stored inside their parent
|
2015-07-05 18:19:48 -05:00
|
|
|
}
|
|
|
|
// Larger nodes are replaced by their hash and stored in the database.
|
2016-05-19 05:24:14 -05:00
|
|
|
hash, _ := n.cache()
|
|
|
|
if hash == nil {
|
|
|
|
h.sha.Reset()
|
|
|
|
h.sha.Write(h.tmp.Bytes())
|
|
|
|
hash = hashNode(h.sha.Sum(nil))
|
|
|
|
}
|
2015-07-05 18:19:48 -05:00
|
|
|
if db != nil {
|
2016-05-19 05:24:14 -05:00
|
|
|
return hash, db.Put(hash, h.tmp.Bytes())
|
2015-07-05 18:19:48 -05:00
|
|
|
}
|
2016-05-19 05:24:14 -05:00
|
|
|
return hash, nil
|
2014-05-26 18:08:51 -05:00
|
|
|
}
|