|
|
@ -19,372 +19,425 @@ package trie |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"bytes" |
|
|
|
"bytes" |
|
|
|
"container/list" |
|
|
|
"errors" |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
"sync" |
|
|
|
"hash" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common" |
|
|
|
"github.com/ethereum/go-ethereum/common" |
|
|
|
"github.com/ethereum/go-ethereum/crypto" |
|
|
|
"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" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
func ParanoiaCheck(t1 *Trie, backend Backend) (bool, *Trie) { |
|
|
|
const defaultCacheCapacity = 800 |
|
|
|
t2 := New(nil, backend) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
it := t1.Iterator() |
|
|
|
var ( |
|
|
|
for it.Next() { |
|
|
|
// The global cache stores decoded trie nodes by hash as they get loaded.
|
|
|
|
t2.Update(it.Key, it.Value) |
|
|
|
globalCache = newARC(defaultCacheCapacity) |
|
|
|
} |
|
|
|
// This is the known root hash of an empty trie.
|
|
|
|
|
|
|
|
emptyRoot = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421") |
|
|
|
return bytes.Equal(t2.Hash(), t1.Hash()), t2 |
|
|
|
) |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type Trie struct { |
|
|
|
|
|
|
|
mu sync.Mutex |
|
|
|
|
|
|
|
root Node |
|
|
|
|
|
|
|
roothash []byte |
|
|
|
|
|
|
|
cache *Cache |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
revisions *list.List |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func New(root []byte, backend Backend) *Trie { |
|
|
|
|
|
|
|
trie := &Trie{} |
|
|
|
|
|
|
|
trie.revisions = list.New() |
|
|
|
|
|
|
|
trie.roothash = root |
|
|
|
|
|
|
|
if backend != nil { |
|
|
|
|
|
|
|
trie.cache = NewCache(backend) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if root != nil { |
|
|
|
var ErrMissingRoot = errors.New("missing root node") |
|
|
|
value := common.NewValueFromBytes(trie.cache.Get(root)) |
|
|
|
|
|
|
|
trie.root = trie.mknode(value) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return trie |
|
|
|
// 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) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (self *Trie) Iterator() *Iterator { |
|
|
|
// DatabaseWriter wraps the Put method of a backing store for the trie.
|
|
|
|
return NewIterator(self) |
|
|
|
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 |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (self *Trie) Copy() *Trie { |
|
|
|
// Trie is a Merkle Patricia Trie.
|
|
|
|
cpy := make([]byte, 32) |
|
|
|
// The zero value is an empty trie with no database.
|
|
|
|
copy(cpy, self.roothash) // NOTE: cpy isn't being used anywhere?
|
|
|
|
// Use New to create a trie that sits on top of a database.
|
|
|
|
trie := New(nil, nil) |
|
|
|
//
|
|
|
|
trie.cache = self.cache.Copy() |
|
|
|
// Trie is not safe for concurrent use.
|
|
|
|
if self.root != nil { |
|
|
|
type Trie struct { |
|
|
|
trie.root = self.root.Copy(trie) |
|
|
|
root node |
|
|
|
} |
|
|
|
db Database |
|
|
|
|
|
|
|
*hasher |
|
|
|
return trie |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Legacy support
|
|
|
|
// New creates a trie with an existing root node from db.
|
|
|
|
func (self *Trie) Root() []byte { return self.Hash() } |
|
|
|
//
|
|
|
|
func (self *Trie) Hash() []byte { |
|
|
|
// If root is the zero hash or the sha3 hash of an empty string, the
|
|
|
|
var hash []byte |
|
|
|
// trie is initially empty and does not require a database. Otherwise,
|
|
|
|
if self.root != nil { |
|
|
|
// New will panics if db is nil or root does not exist in the
|
|
|
|
t := self.root.Hash() |
|
|
|
// database. Accessing the trie loads nodes from db on demand.
|
|
|
|
if byts, ok := t.([]byte); ok && len(byts) > 0 { |
|
|
|
func New(root common.Hash, db Database) (*Trie, error) { |
|
|
|
hash = byts |
|
|
|
trie := &Trie{db: db} |
|
|
|
} else { |
|
|
|
if (root != common.Hash{}) && root != emptyRoot { |
|
|
|
hash = crypto.Sha3(common.Encode(self.root.RlpData())) |
|
|
|
if db == nil { |
|
|
|
|
|
|
|
panic("trie.New: cannot use existing root without a database") |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
if v, _ := trie.db.Get(root[:]); len(v) == 0 { |
|
|
|
hash = crypto.Sha3(common.Encode("")) |
|
|
|
return nil, ErrMissingRoot |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
trie.root = hashNode(root.Bytes()) |
|
|
|
if !bytes.Equal(hash, self.roothash) { |
|
|
|
|
|
|
|
self.revisions.PushBack(self.roothash) |
|
|
|
|
|
|
|
self.roothash = hash |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return trie, nil |
|
|
|
return hash |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
func (self *Trie) Commit() { |
|
|
|
|
|
|
|
self.mu.Lock() |
|
|
|
|
|
|
|
defer self.mu.Unlock() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Hash first
|
|
|
|
// Iterator returns an iterator over all mappings in the trie.
|
|
|
|
self.Hash() |
|
|
|
func (t *Trie) Iterator() *Iterator { |
|
|
|
|
|
|
|
return NewIterator(t) |
|
|
|
self.cache.Flush() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Reset should only be called if the trie has been hashed
|
|
|
|
// Get returns the value for key stored in the trie.
|
|
|
|
func (self *Trie) Reset() { |
|
|
|
// The value bytes must not be modified by the caller.
|
|
|
|
self.mu.Lock() |
|
|
|
func (t *Trie) Get(key []byte) []byte { |
|
|
|
defer self.mu.Unlock() |
|
|
|
key = compactHexDecode(key) |
|
|
|
|
|
|
|
tn := t.root |
|
|
|
self.cache.Reset() |
|
|
|
for len(key) > 0 { |
|
|
|
|
|
|
|
switch n := tn.(type) { |
|
|
|
if self.revisions.Len() > 0 { |
|
|
|
case shortNode: |
|
|
|
revision := self.revisions.Remove(self.revisions.Back()).([]byte) |
|
|
|
if len(key) < len(n.Key) || !bytes.Equal(n.Key, key[:len(n.Key)]) { |
|
|
|
self.roothash = revision |
|
|
|
return nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
tn = n.Val |
|
|
|
|
|
|
|
key = key[len(n.Key):] |
|
|
|
|
|
|
|
case fullNode: |
|
|
|
|
|
|
|
tn = n[key[0]] |
|
|
|
|
|
|
|
key = key[1:] |
|
|
|
|
|
|
|
case nil: |
|
|
|
|
|
|
|
return nil |
|
|
|
|
|
|
|
case hashNode: |
|
|
|
|
|
|
|
tn = t.resolveHash(n) |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
panic(fmt.Sprintf("%T: invalid node: %v", tn, tn)) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
value := common.NewValueFromBytes(self.cache.Get(self.roothash)) |
|
|
|
return tn.(valueNode) |
|
|
|
self.root = self.mknode(value) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (self *Trie) UpdateString(key, value string) Node { return self.Update([]byte(key), []byte(value)) } |
|
|
|
// Update associates key with value in the trie. Subsequent calls to
|
|
|
|
func (self *Trie) Update(key, value []byte) Node { |
|
|
|
// Get will return value. If value has length zero, any existing value
|
|
|
|
self.mu.Lock() |
|
|
|
// is deleted from the trie and calls to Get will return nil.
|
|
|
|
defer self.mu.Unlock() |
|
|
|
//
|
|
|
|
|
|
|
|
// The value bytes must not be modified by the caller while they are
|
|
|
|
k := CompactHexDecode(key) |
|
|
|
// stored in the trie.
|
|
|
|
|
|
|
|
func (t *Trie) Update(key, value []byte) { |
|
|
|
|
|
|
|
k := compactHexDecode(key) |
|
|
|
if len(value) != 0 { |
|
|
|
if len(value) != 0 { |
|
|
|
node := NewValueNode(self, value) |
|
|
|
t.root = t.insert(t.root, k, valueNode(value)) |
|
|
|
node.dirty = true |
|
|
|
|
|
|
|
self.root = self.insert(self.root, k, node) |
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
self.root = self.delete(self.root, k) |
|
|
|
t.root = t.delete(t.root, k) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return self.root |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (self *Trie) GetString(key string) []byte { return self.Get([]byte(key)) } |
|
|
|
|
|
|
|
func (self *Trie) Get(key []byte) []byte { |
|
|
|
|
|
|
|
self.mu.Lock() |
|
|
|
|
|
|
|
defer self.mu.Unlock() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
k := CompactHexDecode(key) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
n := self.get(self.root, k) |
|
|
|
|
|
|
|
if n != nil { |
|
|
|
|
|
|
|
return n.(*ValueNode).Val() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (self *Trie) DeleteString(key string) Node { return self.Delete([]byte(key)) } |
|
|
|
func (t *Trie) insert(n node, key []byte, value node) node { |
|
|
|
func (self *Trie) Delete(key []byte) Node { |
|
|
|
|
|
|
|
self.mu.Lock() |
|
|
|
|
|
|
|
defer self.mu.Unlock() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
k := CompactHexDecode(key) |
|
|
|
|
|
|
|
self.root = self.delete(self.root, k) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return self.root |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (self *Trie) insert(node Node, key []byte, value Node) Node { |
|
|
|
|
|
|
|
if len(key) == 0 { |
|
|
|
if len(key) == 0 { |
|
|
|
return value |
|
|
|
return value |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
switch n := n.(type) { |
|
|
|
if node == nil { |
|
|
|
case shortNode: |
|
|
|
node := NewShortNode(self, key, value) |
|
|
|
matchlen := prefixLen(key, n.Key) |
|
|
|
node.dirty = true |
|
|
|
// If the whole key matches, keep this short node as is
|
|
|
|
return node |
|
|
|
// and only update the value.
|
|
|
|
} |
|
|
|
if matchlen == len(n.Key) { |
|
|
|
|
|
|
|
return shortNode{n.Key, t.insert(n.Val, key[matchlen:], value)} |
|
|
|
switch node := node.(type) { |
|
|
|
|
|
|
|
case *ShortNode: |
|
|
|
|
|
|
|
k := node.Key() |
|
|
|
|
|
|
|
cnode := node.Value() |
|
|
|
|
|
|
|
if bytes.Equal(k, key) { |
|
|
|
|
|
|
|
node := NewShortNode(self, key, value) |
|
|
|
|
|
|
|
node.dirty = true |
|
|
|
|
|
|
|
return node |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Otherwise branch out at the index where they differ.
|
|
|
|
var n Node |
|
|
|
var branch fullNode |
|
|
|
matchlength := MatchingNibbleLength(key, k) |
|
|
|
branch[n.Key[matchlen]] = t.insert(nil, n.Key[matchlen+1:], n.Val) |
|
|
|
if matchlength == len(k) { |
|
|
|
branch[key[matchlen]] = t.insert(nil, key[matchlen+1:], value) |
|
|
|
n = self.insert(cnode, key[matchlength:], value) |
|
|
|
// Replace this shortNode with the branch if it occurs at index 0.
|
|
|
|
} else { |
|
|
|
if matchlen == 0 { |
|
|
|
pnode := self.insert(nil, k[matchlength+1:], cnode) |
|
|
|
return branch |
|
|
|
nnode := self.insert(nil, key[matchlength+1:], value) |
|
|
|
|
|
|
|
fulln := NewFullNode(self) |
|
|
|
|
|
|
|
fulln.dirty = true |
|
|
|
|
|
|
|
fulln.set(k[matchlength], pnode) |
|
|
|
|
|
|
|
fulln.set(key[matchlength], nnode) |
|
|
|
|
|
|
|
n = fulln |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if matchlength == 0 { |
|
|
|
|
|
|
|
return n |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// Otherwise, replace it with a short node leading up to the branch.
|
|
|
|
|
|
|
|
return shortNode{key[:matchlen], branch} |
|
|
|
|
|
|
|
|
|
|
|
snode := NewShortNode(self, key[:matchlength], n) |
|
|
|
case fullNode: |
|
|
|
snode.dirty = true |
|
|
|
n[key[0]] = t.insert(n[key[0]], key[1:], value) |
|
|
|
return snode |
|
|
|
return n |
|
|
|
|
|
|
|
|
|
|
|
case *FullNode: |
|
|
|
case nil: |
|
|
|
cpy := node.Copy(self).(*FullNode) |
|
|
|
return shortNode{key, value} |
|
|
|
cpy.set(key[0], self.insert(node.branch(key[0]), key[1:], value)) |
|
|
|
|
|
|
|
cpy.dirty = true |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return cpy |
|
|
|
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.
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// TODO: track whether insertion changed the value and keep
|
|
|
|
|
|
|
|
// n as a hash node if it didn't.
|
|
|
|
|
|
|
|
return t.insert(t.resolveHash(n), key, value) |
|
|
|
|
|
|
|
|
|
|
|
default: |
|
|
|
default: |
|
|
|
panic(fmt.Sprintf("%T: invalid node: %v", node, node)) |
|
|
|
panic(fmt.Sprintf("%T: invalid node: %v", n, n)) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (self *Trie) get(node Node, key []byte) Node { |
|
|
|
// Delete removes any existing value for key from the trie.
|
|
|
|
if len(key) == 0 { |
|
|
|
func (t *Trie) Delete(key []byte) { |
|
|
|
return node |
|
|
|
k := compactHexDecode(key) |
|
|
|
} |
|
|
|
t.root = t.delete(t.root, k) |
|
|
|
|
|
|
|
|
|
|
|
if node == nil { |
|
|
|
|
|
|
|
return nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
switch node := node.(type) { |
|
|
|
|
|
|
|
case *ShortNode: |
|
|
|
|
|
|
|
k := node.Key() |
|
|
|
|
|
|
|
cnode := node.Value() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if len(key) >= len(k) && bytes.Equal(k, key[:len(k)]) { |
|
|
|
|
|
|
|
return self.get(cnode, key[len(k):]) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
|
|
|
|
case *FullNode: |
|
|
|
|
|
|
|
return self.get(node.branch(key[0]), key[1:]) |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
panic(fmt.Sprintf("%T: invalid node: %v", node, node)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (self *Trie) delete(node Node, key []byte) Node { |
|
|
|
// delete returns the new root of the trie with key deleted.
|
|
|
|
if len(key) == 0 && node == nil { |
|
|
|
// It reduces the trie to minimal form by simplifying
|
|
|
|
return nil |
|
|
|
// nodes on the way up after deleting recursively.
|
|
|
|
} |
|
|
|
func (t *Trie) delete(n node, key []byte) node { |
|
|
|
|
|
|
|
switch n := n.(type) { |
|
|
|
switch node := node.(type) { |
|
|
|
case shortNode: |
|
|
|
case *ShortNode: |
|
|
|
matchlen := prefixLen(key, n.Key) |
|
|
|
k := node.Key() |
|
|
|
if matchlen < len(n.Key) { |
|
|
|
cnode := node.Value() |
|
|
|
return n // don't replace n on mismatch
|
|
|
|
if bytes.Equal(key, k) { |
|
|
|
} |
|
|
|
return nil |
|
|
|
if matchlen == len(key) { |
|
|
|
} else if bytes.Equal(key[:len(k)], k) { |
|
|
|
return nil // remove n entirely for whole matches
|
|
|
|
child := self.delete(cnode, key[len(k):]) |
|
|
|
} |
|
|
|
|
|
|
|
// The key is longer than n.Key. Remove the remaining suffix
|
|
|
|
var n Node |
|
|
|
// from the subtrie. Child can never be nil here since the
|
|
|
|
switch child := child.(type) { |
|
|
|
// subtrie must contain at least two other values with keys
|
|
|
|
case *ShortNode: |
|
|
|
// longer than n.Key.
|
|
|
|
nkey := append(k, child.Key()...) |
|
|
|
child := t.delete(n.Val, key[len(n.Key):]) |
|
|
|
n = NewShortNode(self, nkey, child.Value()) |
|
|
|
switch child := child.(type) { |
|
|
|
n.(*ShortNode).dirty = true |
|
|
|
case shortNode: |
|
|
|
case *FullNode: |
|
|
|
// Deleting from the subtrie reduced it to another
|
|
|
|
sn := NewShortNode(self, node.Key(), child) |
|
|
|
// short node. Merge the nodes to avoid creating a
|
|
|
|
sn.dirty = true |
|
|
|
// shortNode{..., shortNode{...}}. Use concat (which
|
|
|
|
sn.key = node.key |
|
|
|
// always creates a new slice) instead of append to
|
|
|
|
n = sn |
|
|
|
// avoid modifying n.Key since it might be shared with
|
|
|
|
} |
|
|
|
// other nodes.
|
|
|
|
|
|
|
|
return shortNode{concat(n.Key, child.Key...), child.Val} |
|
|
|
return n |
|
|
|
default: |
|
|
|
} else { |
|
|
|
return shortNode{n.Key, child} |
|
|
|
return node |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
case *FullNode: |
|
|
|
case fullNode: |
|
|
|
n := node.Copy(self).(*FullNode) |
|
|
|
n[key[0]] = t.delete(n[key[0]], key[1:]) |
|
|
|
n.set(key[0], self.delete(n.branch(key[0]), key[1:])) |
|
|
|
// Check how many non-nil entries are left after deleting and
|
|
|
|
n.dirty = true |
|
|
|
// 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.
|
|
|
|
pos := -1 |
|
|
|
pos := -1 |
|
|
|
for i := 0; i < 17; i++ { |
|
|
|
for i, cld := range n { |
|
|
|
if n.branch(byte(i)) != nil { |
|
|
|
if cld != nil { |
|
|
|
if pos == -1 { |
|
|
|
if pos == -1 { |
|
|
|
pos = i |
|
|
|
pos = i |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
pos = -2 |
|
|
|
pos = -2 |
|
|
|
|
|
|
|
break |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if pos >= 0 { |
|
|
|
var nnode Node |
|
|
|
if pos != 16 { |
|
|
|
if pos == 16 { |
|
|
|
// If the remaining entry is a short node, it replaces
|
|
|
|
nnode = NewShortNode(self, []byte{16}, n.branch(byte(pos))) |
|
|
|
// n and its key gets the missing nibble tacked to the
|
|
|
|
nnode.(*ShortNode).dirty = true |
|
|
|
// front. This avoids creating an invalid
|
|
|
|
} else if pos >= 0 { |
|
|
|
// shortNode{..., shortNode{...}}. Since the entry
|
|
|
|
cnode := n.branch(byte(pos)) |
|
|
|
// might not be loaded yet, resolve it just for this
|
|
|
|
switch cnode := cnode.(type) { |
|
|
|
// check.
|
|
|
|
case *ShortNode: |
|
|
|
cnode := t.resolve(n[pos]) |
|
|
|
// Stitch keys
|
|
|
|
if cnode, ok := cnode.(shortNode); ok { |
|
|
|
k := append([]byte{byte(pos)}, cnode.Key()...) |
|
|
|
k := append([]byte{byte(pos)}, cnode.Key...) |
|
|
|
nnode = NewShortNode(self, k, cnode.Value()) |
|
|
|
return shortNode{k, cnode.Val} |
|
|
|
nnode.(*ShortNode).dirty = true |
|
|
|
} |
|
|
|
case *FullNode: |
|
|
|
|
|
|
|
nnode = NewShortNode(self, []byte{byte(pos)}, n.branch(byte(pos))) |
|
|
|
|
|
|
|
nnode.(*ShortNode).dirty = true |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
// Otherwise, n is replaced by a one-nibble short node
|
|
|
|
nnode = n |
|
|
|
// containing the child.
|
|
|
|
|
|
|
|
return shortNode{[]byte{byte(pos)}, n[pos]} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// n still contains at least two values and cannot be reduced.
|
|
|
|
|
|
|
|
return n |
|
|
|
|
|
|
|
|
|
|
|
return nnode |
|
|
|
|
|
|
|
case nil: |
|
|
|
case nil: |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// TODO: track whether deletion actually hit a key and keep
|
|
|
|
|
|
|
|
// n as a hash node if it didn't.
|
|
|
|
|
|
|
|
return t.delete(t.resolveHash(n), key) |
|
|
|
|
|
|
|
|
|
|
|
default: |
|
|
|
default: |
|
|
|
panic(fmt.Sprintf("%T: invalid node: %v (%v)", node, node, key)) |
|
|
|
panic(fmt.Sprintf("%T: invalid node: %v (%v)", n, n, key)) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// casting functions and cache storing
|
|
|
|
func concat(s1 []byte, s2 ...byte) []byte { |
|
|
|
func (self *Trie) mknode(value *common.Value) Node { |
|
|
|
r := make([]byte, len(s1)+len(s2)) |
|
|
|
l := value.Len() |
|
|
|
copy(r, s1) |
|
|
|
switch l { |
|
|
|
copy(r[len(s1):], s2) |
|
|
|
case 0: |
|
|
|
return r |
|
|
|
return nil |
|
|
|
} |
|
|
|
case 2: |
|
|
|
|
|
|
|
// A value node may consists of 2 bytes.
|
|
|
|
func (t *Trie) resolve(n node) node { |
|
|
|
if value.Get(0).Len() != 0 { |
|
|
|
if n, ok := n.(hashNode); ok { |
|
|
|
key := CompactDecode(value.Get(0).Bytes()) |
|
|
|
return t.resolveHash(n) |
|
|
|
if key[len(key)-1] == 16 { |
|
|
|
} |
|
|
|
return NewShortNode(self, key, NewValueNode(self, value.Get(1).Bytes())) |
|
|
|
return n |
|
|
|
} else { |
|
|
|
} |
|
|
|
return NewShortNode(self, key, self.mknode(value.Get(1))) |
|
|
|
|
|
|
|
} |
|
|
|
func (t *Trie) resolveHash(n hashNode) node { |
|
|
|
} |
|
|
|
if v, ok := globalCache.Get(n); ok { |
|
|
|
case 17: |
|
|
|
return v |
|
|
|
if len(value.Bytes()) != 17 { |
|
|
|
} |
|
|
|
fnode := NewFullNode(self) |
|
|
|
enc, err := t.db.Get(n) |
|
|
|
for i := 0; i < 16; i++ { |
|
|
|
if err != nil || enc == nil { |
|
|
|
fnode.set(byte(i), self.mknode(value.Get(i))) |
|
|
|
// TODO: This needs to be improved to properly distinguish errors.
|
|
|
|
} |
|
|
|
// Disk I/O errors shouldn't produce nil (and cause a
|
|
|
|
return fnode |
|
|
|
// consensus failure or weird crash), but it is unclear how
|
|
|
|
|
|
|
|
// they could be handled because the entire stack above the trie isn't
|
|
|
|
|
|
|
|
// prepared to cope with missing state nodes.
|
|
|
|
|
|
|
|
if glog.V(logger.Error) { |
|
|
|
|
|
|
|
glog.Errorf("Dangling hash node ref %x: %v", n, err) |
|
|
|
} |
|
|
|
} |
|
|
|
case 32: |
|
|
|
return nil |
|
|
|
return NewHash(value.Bytes(), self) |
|
|
|
} |
|
|
|
|
|
|
|
dec := mustDecodeNode(n, enc) |
|
|
|
|
|
|
|
if dec != nil { |
|
|
|
|
|
|
|
globalCache.Put(n, dec) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return dec |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Root returns the root hash of the trie.
|
|
|
|
|
|
|
|
// Deprecated: use Hash instead.
|
|
|
|
|
|
|
|
func (t *Trie) Root() []byte { return t.Hash().Bytes() } |
|
|
|
|
|
|
|
|
|
|
|
return NewValueNode(self, value.Bytes()) |
|
|
|
// 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 { |
|
|
|
|
|
|
|
root, _ := t.hashRoot(nil) |
|
|
|
|
|
|
|
return common.BytesToHash(root.(hashNode)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (self *Trie) trans(node Node) Node { |
|
|
|
// Commit writes all nodes to the trie's database.
|
|
|
|
switch node := node.(type) { |
|
|
|
// Nodes are stored with their sha3 hash as the key.
|
|
|
|
case *HashNode: |
|
|
|
//
|
|
|
|
value := common.NewValueFromBytes(self.cache.Get(node.key)) |
|
|
|
// Committing flushes nodes from memory.
|
|
|
|
return self.mknode(value) |
|
|
|
// Subsequent Get calls will load nodes from the database.
|
|
|
|
default: |
|
|
|
func (t *Trie) Commit() (root common.Hash, err error) { |
|
|
|
return node |
|
|
|
if t.db == nil { |
|
|
|
|
|
|
|
panic("Commit called on trie with nil database") |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return t.CommitTo(t.db) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (self *Trie) store(node Node) interface{} { |
|
|
|
// CommitTo writes all nodes to the given database.
|
|
|
|
data := common.Encode(node) |
|
|
|
// Nodes are stored with their sha3 hash as the key.
|
|
|
|
if len(data) >= 32 { |
|
|
|
//
|
|
|
|
key := crypto.Sha3(data) |
|
|
|
// Committing flushes nodes from memory. Subsequent Get calls will
|
|
|
|
if node.Dirty() { |
|
|
|
// load nodes from the trie's database. Calling code must ensure that
|
|
|
|
//fmt.Println("save", node)
|
|
|
|
// the changes made to db are written back to the trie's attached
|
|
|
|
//fmt.Println()
|
|
|
|
// database before using the trie.
|
|
|
|
self.cache.Put(key, data) |
|
|
|
func (t *Trie) CommitTo(db DatabaseWriter) (root common.Hash, err error) { |
|
|
|
} |
|
|
|
n, err := t.hashRoot(db) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return (common.Hash{}), err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
t.root = n |
|
|
|
|
|
|
|
return common.BytesToHash(n.(hashNode)), nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return key |
|
|
|
func (t *Trie) hashRoot(db DatabaseWriter) (node, error) { |
|
|
|
|
|
|
|
if t.root == nil { |
|
|
|
|
|
|
|
return hashNode(emptyRoot.Bytes()), nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if t.hasher == nil { |
|
|
|
|
|
|
|
t.hasher = newHasher() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return t.hasher.hash(t.root, db, true) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return node.RlpData() |
|
|
|
type hasher struct { |
|
|
|
|
|
|
|
tmp *bytes.Buffer |
|
|
|
|
|
|
|
sha hash.Hash |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (self *Trie) PrintRoot() { |
|
|
|
func newHasher() *hasher { |
|
|
|
fmt.Println(self.root) |
|
|
|
return &hasher{tmp: new(bytes.Buffer), sha: sha3.NewKeccak256()} |
|
|
|
fmt.Printf("root=%x\n", self.Root()) |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (h *hasher) hash(n node, db DatabaseWriter, force bool) (node, error) { |
|
|
|
|
|
|
|
hashed, err := h.replaceChildren(n, db) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return hashNode{}, err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if n, err = h.store(hashed, db, force); err != nil { |
|
|
|
|
|
|
|
return hashNode{}, err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return n, nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// hashChildren replaces child nodes of n with their hashes if the encoded
|
|
|
|
|
|
|
|
// size of the child is larger than a hash.
|
|
|
|
|
|
|
|
func (h *hasher) replaceChildren(n node, db DatabaseWriter) (node, error) { |
|
|
|
|
|
|
|
var err error |
|
|
|
|
|
|
|
switch n := n.(type) { |
|
|
|
|
|
|
|
case shortNode: |
|
|
|
|
|
|
|
n.Key = compactEncode(n.Key) |
|
|
|
|
|
|
|
if _, ok := n.Val.(valueNode); !ok { |
|
|
|
|
|
|
|
if n.Val, err = h.hash(n.Val, db, false); err != nil { |
|
|
|
|
|
|
|
return n, err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if n.Val == nil { |
|
|
|
|
|
|
|
// Ensure that nil children are encoded as empty strings.
|
|
|
|
|
|
|
|
n.Val = valueNode(nil) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return n, nil |
|
|
|
|
|
|
|
case fullNode: |
|
|
|
|
|
|
|
for i := 0; i < 16; i++ { |
|
|
|
|
|
|
|
if n[i] != nil { |
|
|
|
|
|
|
|
if n[i], err = h.hash(n[i], db, false); err != nil { |
|
|
|
|
|
|
|
return n, err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
// Ensure that nil children are encoded as empty strings.
|
|
|
|
|
|
|
|
n[i] = valueNode(nil) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if n[16] == nil { |
|
|
|
|
|
|
|
n[16] = valueNode(nil) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return n, nil |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
return n, nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
h.tmp.Reset() |
|
|
|
|
|
|
|
if err := rlp.Encode(h.tmp, n); err != nil { |
|
|
|
|
|
|
|
panic("encode error: " + err.Error()) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if h.tmp.Len() < 32 && !force { |
|
|
|
|
|
|
|
// Nodes smaller than 32 bytes are stored inside their parent.
|
|
|
|
|
|
|
|
return n, nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// Larger nodes are replaced by their hash and stored in the database.
|
|
|
|
|
|
|
|
h.sha.Reset() |
|
|
|
|
|
|
|
h.sha.Write(h.tmp.Bytes()) |
|
|
|
|
|
|
|
key := hashNode(h.sha.Sum(nil)) |
|
|
|
|
|
|
|
if db != nil { |
|
|
|
|
|
|
|
err := db.Put(key, h.tmp.Bytes()) |
|
|
|
|
|
|
|
return key, err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return key, nil |
|
|
|
} |
|
|
|
} |
|
|
|