|
|
@ -1,8 +1,8 @@ |
|
|
|
package trie |
|
|
|
package trie |
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"bytes" |
|
|
|
"bytes" |
|
|
|
|
|
|
|
"container/list" |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
"sync" |
|
|
|
"sync" |
|
|
|
|
|
|
|
|
|
|
@ -10,618 +10,325 @@ import ( |
|
|
|
"github.com/ethereum/go-ethereum/ethutil" |
|
|
|
"github.com/ethereum/go-ethereum/ethutil" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
func ParanoiaCheck(t1 *Trie) (bool, *Trie) { |
|
|
|
func ParanoiaCheck(t1 *Trie, backend Backend) (bool, *Trie) { |
|
|
|
t2 := New(ethutil.Config.Db, "") |
|
|
|
t2 := New(nil, backend) |
|
|
|
|
|
|
|
|
|
|
|
t1.NewIterator().Each(func(key string, v *ethutil.Value) { |
|
|
|
it := t1.Iterator() |
|
|
|
t2.Update(key, v.Str()) |
|
|
|
for it.Next() { |
|
|
|
}) |
|
|
|
t2.Update(it.Key, it.Value) |
|
|
|
|
|
|
|
} |
|
|
|
return bytes.Compare(t2.GetRoot(), t1.GetRoot()) == 0, t2 |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s *Cache) Len() int { |
|
|
|
|
|
|
|
return len(s.nodes) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO
|
|
|
|
|
|
|
|
// A StateObject is an object that has a state root
|
|
|
|
|
|
|
|
// This is goig to be the object for the second level caching (the caching of object which have a state such as contracts)
|
|
|
|
|
|
|
|
type StateObject interface { |
|
|
|
|
|
|
|
State() *Trie |
|
|
|
|
|
|
|
Sync() |
|
|
|
|
|
|
|
Undo() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type Node struct { |
|
|
|
|
|
|
|
Key []byte |
|
|
|
|
|
|
|
Value *ethutil.Value |
|
|
|
|
|
|
|
Dirty bool |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func NewNode(key []byte, val *ethutil.Value, dirty bool) *Node { |
|
|
|
|
|
|
|
return &Node{Key: key, Value: val, Dirty: dirty} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (n *Node) Copy() *Node { |
|
|
|
return bytes.Equal(t2.Hash(), t1.Hash()), t2 |
|
|
|
return NewNode(n.Key, n.Value, n.Dirty) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
type Cache struct { |
|
|
|
type Trie struct { |
|
|
|
nodes map[string]*Node |
|
|
|
mu sync.Mutex |
|
|
|
db ethutil.Database |
|
|
|
root Node |
|
|
|
IsDirty bool |
|
|
|
roothash []byte |
|
|
|
} |
|
|
|
cache *Cache |
|
|
|
|
|
|
|
|
|
|
|
func NewCache(db ethutil.Database) *Cache { |
|
|
|
revisions *list.List |
|
|
|
return &Cache{db: db, nodes: make(map[string]*Node)} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (cache *Cache) PutValue(v interface{}, force bool) interface{} { |
|
|
|
func New(root []byte, backend Backend) *Trie { |
|
|
|
value := ethutil.NewValue(v) |
|
|
|
trie := &Trie{} |
|
|
|
|
|
|
|
trie.revisions = list.New() |
|
|
|
enc := value.Encode() |
|
|
|
trie.roothash = root |
|
|
|
if len(enc) >= 32 || force { |
|
|
|
trie.cache = NewCache(backend) |
|
|
|
sha := crypto.Sha3(enc) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cache.nodes[string(sha)] = NewNode(sha, value, true) |
|
|
|
|
|
|
|
cache.IsDirty = true |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return sha |
|
|
|
if root != nil { |
|
|
|
|
|
|
|
value := ethutil.NewValueFromBytes(trie.cache.Get(root)) |
|
|
|
|
|
|
|
trie.root = trie.mknode(value) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return v |
|
|
|
return trie |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (cache *Cache) Put(v interface{}) interface{} { |
|
|
|
|
|
|
|
return cache.PutValue(v, false) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (cache *Cache) Get(key []byte) *ethutil.Value { |
|
|
|
func (self *Trie) Iterator() *Iterator { |
|
|
|
// First check if the key is the cache
|
|
|
|
return NewIterator(self) |
|
|
|
if cache.nodes[string(key)] != nil { |
|
|
|
|
|
|
|
return cache.nodes[string(key)].Value |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Get the key of the database instead and cache it
|
|
|
|
|
|
|
|
data, _ := cache.db.Get(key) |
|
|
|
|
|
|
|
// Create the cached value
|
|
|
|
|
|
|
|
value := ethutil.NewValueFromBytes(data) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
defer func() { |
|
|
|
|
|
|
|
if r := recover(); r != nil { |
|
|
|
|
|
|
|
fmt.Println("RECOVER GET", cache, cache.nodes) |
|
|
|
|
|
|
|
panic("bye") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}() |
|
|
|
|
|
|
|
// Create caching node
|
|
|
|
|
|
|
|
cache.nodes[string(key)] = NewNode(key, value, true) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return value |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (cache *Cache) Delete(key []byte) { |
|
|
|
func (self *Trie) Copy() *Trie { |
|
|
|
delete(cache.nodes, string(key)) |
|
|
|
return New(self.roothash, self.cache.backend) |
|
|
|
|
|
|
|
|
|
|
|
cache.db.Delete(key) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (cache *Cache) Commit() { |
|
|
|
// Legacy support
|
|
|
|
// Don't try to commit if it isn't dirty
|
|
|
|
func (self *Trie) Root() []byte { return self.Hash() } |
|
|
|
if !cache.IsDirty { |
|
|
|
func (self *Trie) Hash() []byte { |
|
|
|
return |
|
|
|
var hash []byte |
|
|
|
} |
|
|
|
if self.root != nil { |
|
|
|
|
|
|
|
t := self.root.Hash() |
|
|
|
for key, node := range cache.nodes { |
|
|
|
if byts, ok := t.([]byte); ok && len(byts) > 0 { |
|
|
|
if node.Dirty { |
|
|
|
hash = byts |
|
|
|
cache.db.Put([]byte(key), node.Value.Encode()) |
|
|
|
} else { |
|
|
|
node.Dirty = false |
|
|
|
hash = crypto.Sha3(ethutil.Encode(self.root.RlpData())) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
hash = crypto.Sha3(ethutil.Encode("")) |
|
|
|
} |
|
|
|
} |
|
|
|
cache.IsDirty = false |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// If the nodes grows beyond the 200 entries we simple empty it
|
|
|
|
|
|
|
|
// FIXME come up with something better
|
|
|
|
|
|
|
|
if len(cache.nodes) > 200 { |
|
|
|
|
|
|
|
cache.nodes = make(map[string]*Node) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (cache *Cache) Undo() { |
|
|
|
if !bytes.Equal(hash, self.roothash) { |
|
|
|
for key, node := range cache.nodes { |
|
|
|
self.revisions.PushBack(self.roothash) |
|
|
|
if node.Dirty { |
|
|
|
self.roothash = hash |
|
|
|
delete(cache.nodes, key) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
cache.IsDirty = false |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// A (modified) Radix Trie implementation. The Trie implements
|
|
|
|
return hash |
|
|
|
// a caching mechanism and will used cached values if they are
|
|
|
|
|
|
|
|
// present. If a node is not present in the cache it will try to
|
|
|
|
|
|
|
|
// fetch it from the database and store the cached value.
|
|
|
|
|
|
|
|
// Please note that the data isn't persisted unless `Sync` is
|
|
|
|
|
|
|
|
// explicitly called.
|
|
|
|
|
|
|
|
type Trie struct { |
|
|
|
|
|
|
|
mut sync.RWMutex |
|
|
|
|
|
|
|
prevRoot interface{} |
|
|
|
|
|
|
|
Root interface{} |
|
|
|
|
|
|
|
//db Database
|
|
|
|
|
|
|
|
cache *Cache |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
func (self *Trie) Commit() { |
|
|
|
|
|
|
|
self.mu.Lock() |
|
|
|
|
|
|
|
defer self.mu.Unlock() |
|
|
|
|
|
|
|
|
|
|
|
func copyRoot(root interface{}) interface{} { |
|
|
|
// Hash first
|
|
|
|
var prevRootCopy interface{} |
|
|
|
self.Hash() |
|
|
|
if b, ok := root.([]byte); ok { |
|
|
|
|
|
|
|
prevRootCopy = ethutil.CopyBytes(b) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
prevRootCopy = root |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return prevRootCopy |
|
|
|
self.cache.Flush() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func New(db ethutil.Database, Root interface{}) *Trie { |
|
|
|
// Reset should only be called if the trie has been hashed
|
|
|
|
// Make absolute sure the root is copied
|
|
|
|
func (self *Trie) Reset() { |
|
|
|
r := copyRoot(Root) |
|
|
|
self.mu.Lock() |
|
|
|
p := copyRoot(Root) |
|
|
|
defer self.mu.Unlock() |
|
|
|
|
|
|
|
|
|
|
|
trie := &Trie{cache: NewCache(db), Root: r, prevRoot: p} |
|
|
|
self.cache.Reset() |
|
|
|
trie.setRoot(Root) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return trie |
|
|
|
if self.revisions.Len() > 0 { |
|
|
|
} |
|
|
|
revision := self.revisions.Remove(self.revisions.Back()).([]byte) |
|
|
|
|
|
|
|
self.roothash = revision |
|
|
|
func (self *Trie) setRoot(root interface{}) { |
|
|
|
|
|
|
|
switch t := root.(type) { |
|
|
|
|
|
|
|
case string: |
|
|
|
|
|
|
|
//if t == "" {
|
|
|
|
|
|
|
|
// root = crypto.Sha3(ethutil.Encode(""))
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
self.Root = []byte(t) |
|
|
|
|
|
|
|
case []byte: |
|
|
|
|
|
|
|
self.Root = root |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
self.Root = self.cache.PutValue(root, true) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
value := ethutil.NewValueFromBytes(self.cache.Get(self.roothash)) |
|
|
|
|
|
|
|
self.root = self.mknode(value) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (t *Trie) Update(key, value string) { |
|
|
|
func (self *Trie) UpdateString(key, value string) Node { return self.Update([]byte(key), []byte(value)) } |
|
|
|
t.mut.Lock() |
|
|
|
func (self *Trie) Update(key, value []byte) Node { |
|
|
|
defer t.mut.Unlock() |
|
|
|
self.mu.Lock() |
|
|
|
|
|
|
|
defer self.mu.Unlock() |
|
|
|
|
|
|
|
|
|
|
|
k := CompactHexDecode(key) |
|
|
|
k := CompactHexDecode(string(key)) |
|
|
|
|
|
|
|
|
|
|
|
var root interface{} |
|
|
|
if len(value) != 0 { |
|
|
|
if value != "" { |
|
|
|
self.root = self.insert(self.root, k, &ValueNode{self, value}) |
|
|
|
root = t.UpdateState(t.Root, k, value) |
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
root = t.deleteState(t.Root, k) |
|
|
|
self.root = self.delete(self.root, k) |
|
|
|
} |
|
|
|
} |
|
|
|
t.setRoot(root) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (t *Trie) Get(key string) string { |
|
|
|
|
|
|
|
t.mut.Lock() |
|
|
|
|
|
|
|
defer t.mut.Unlock() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
k := CompactHexDecode(key) |
|
|
|
|
|
|
|
c := ethutil.NewValue(t.getState(t.Root, k)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return c.Str() |
|
|
|
return self.root |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (t *Trie) Delete(key string) { |
|
|
|
func (self *Trie) GetString(key string) []byte { return self.Get([]byte(key)) } |
|
|
|
t.mut.Lock() |
|
|
|
func (self *Trie) Get(key []byte) []byte { |
|
|
|
defer t.mut.Unlock() |
|
|
|
self.mu.Lock() |
|
|
|
|
|
|
|
defer self.mu.Unlock() |
|
|
|
k := CompactHexDecode(key) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
root := t.deleteState(t.Root, k) |
|
|
|
|
|
|
|
t.setRoot(root) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (self *Trie) GetRoot() []byte { |
|
|
|
|
|
|
|
switch t := self.Root.(type) { |
|
|
|
|
|
|
|
case string: |
|
|
|
|
|
|
|
if t == "" { |
|
|
|
|
|
|
|
return crypto.Sha3(ethutil.Encode("")) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return []byte(t) |
|
|
|
|
|
|
|
case []byte: |
|
|
|
|
|
|
|
if len(t) == 0 { |
|
|
|
|
|
|
|
return crypto.Sha3(ethutil.Encode("")) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return t |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
panic(fmt.Sprintf("invalid root type %T (%v)", self.Root, self.Root)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Simple compare function which creates a rlp value out of the evaluated objects
|
|
|
|
k := CompactHexDecode(string(key)) |
|
|
|
func (t *Trie) Cmp(trie *Trie) bool { |
|
|
|
|
|
|
|
return ethutil.NewValue(t.Root).Cmp(ethutil.NewValue(trie.Root)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Returns a copy of this trie
|
|
|
|
n := self.get(self.root, k) |
|
|
|
func (t *Trie) Copy() *Trie { |
|
|
|
if n != nil { |
|
|
|
trie := New(t.cache.db, t.Root) |
|
|
|
return n.(*ValueNode).Val() |
|
|
|
for key, node := range t.cache.nodes { |
|
|
|
|
|
|
|
trie.cache.nodes[key] = node.Copy() |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return trie |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Save the cached value to the database.
|
|
|
|
func (self *Trie) DeleteString(key string) Node { return self.Delete([]byte(key)) } |
|
|
|
func (t *Trie) Sync() { |
|
|
|
func (self *Trie) Delete(key []byte) Node { |
|
|
|
t.cache.Commit() |
|
|
|
self.mu.Lock() |
|
|
|
t.prevRoot = copyRoot(t.Root) |
|
|
|
defer self.mu.Unlock() |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (t *Trie) Undo() { |
|
|
|
k := CompactHexDecode(string(key)) |
|
|
|
t.cache.Undo() |
|
|
|
self.root = self.delete(self.root, k) |
|
|
|
t.Root = t.prevRoot |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (t *Trie) Cache() *Cache { |
|
|
|
return self.root |
|
|
|
return t.cache |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (t *Trie) getState(node interface{}, key []byte) interface{} { |
|
|
|
func (self *Trie) insert(node Node, key []byte, value Node) Node { |
|
|
|
n := ethutil.NewValue(node) |
|
|
|
if len(key) == 0 { |
|
|
|
// Return the node if key is empty (= found)
|
|
|
|
return value |
|
|
|
if len(key) == 0 || n.IsNil() || n.Len() == 0 { |
|
|
|
|
|
|
|
return node |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
currentNode := t.getNode(node) |
|
|
|
if node == nil { |
|
|
|
length := currentNode.Len() |
|
|
|
return NewShortNode(self, key, value) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if length == 0 { |
|
|
|
switch node := node.(type) { |
|
|
|
return "" |
|
|
|
case *ShortNode: |
|
|
|
} else if length == 2 { |
|
|
|
k := node.Key() |
|
|
|
// Decode the key
|
|
|
|
cnode := node.Value() |
|
|
|
k := CompactDecode(currentNode.Get(0).Str()) |
|
|
|
if bytes.Equal(k, key) { |
|
|
|
v := currentNode.Get(1).Raw() |
|
|
|
return NewShortNode(self, key, value) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if len(key) >= len(k) && bytes.Equal(k, key[:len(k)]) { //CompareIntSlice(k, key[:len(k)]) {
|
|
|
|
var n Node |
|
|
|
return t.getState(v, key[len(k):]) |
|
|
|
matchlength := MatchingNibbleLength(key, k) |
|
|
|
|
|
|
|
if matchlength == len(k) { |
|
|
|
|
|
|
|
n = self.insert(cnode, key[matchlength:], value) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
return "" |
|
|
|
pnode := self.insert(nil, k[matchlength+1:], cnode) |
|
|
|
|
|
|
|
nnode := self.insert(nil, key[matchlength+1:], value) |
|
|
|
|
|
|
|
fulln := NewFullNode(self) |
|
|
|
|
|
|
|
fulln.set(k[matchlength], pnode) |
|
|
|
|
|
|
|
fulln.set(key[matchlength], nnode) |
|
|
|
|
|
|
|
n = fulln |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if matchlength == 0 { |
|
|
|
|
|
|
|
return n |
|
|
|
} |
|
|
|
} |
|
|
|
} else if length == 17 { |
|
|
|
|
|
|
|
return t.getState(currentNode.Get(int(key[0])).Raw(), key[1:]) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// It shouldn't come this far
|
|
|
|
|
|
|
|
panic("unexpected return") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (t *Trie) getNode(node interface{}) *ethutil.Value { |
|
|
|
|
|
|
|
n := ethutil.NewValue(node) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if !n.Get(0).IsNil() { |
|
|
|
|
|
|
|
return n |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
str := n.Str() |
|
|
|
|
|
|
|
if len(str) == 0 { |
|
|
|
|
|
|
|
return n |
|
|
|
|
|
|
|
} else if len(str) < 32 { |
|
|
|
|
|
|
|
return ethutil.NewValueFromBytes([]byte(str)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
data := t.cache.Get(n.Bytes()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return data |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (t *Trie) UpdateState(node interface{}, key []byte, value string) interface{} { |
|
|
|
return NewShortNode(self, key[:matchlength], n) |
|
|
|
return t.InsertState(node, key, value) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (t *Trie) Put(node interface{}) interface{} { |
|
|
|
case *FullNode: |
|
|
|
return t.cache.Put(node) |
|
|
|
cpy := node.Copy().(*FullNode) |
|
|
|
|
|
|
|
cpy.set(key[0], self.insert(node.branch(key[0]), key[1:], value)) |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
return cpy |
|
|
|
|
|
|
|
|
|
|
|
func EmptyStringSlice(l int) []interface{} { |
|
|
|
default: |
|
|
|
slice := make([]interface{}, l) |
|
|
|
panic(fmt.Sprintf("%T: invalid node: %v", node, node)) |
|
|
|
for i := 0; i < l; i++ { |
|
|
|
|
|
|
|
slice[i] = "" |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return slice |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (t *Trie) InsertState(node interface{}, key []byte, value interface{}) interface{} { |
|
|
|
func (self *Trie) get(node Node, key []byte) Node { |
|
|
|
if len(key) == 0 { |
|
|
|
if len(key) == 0 { |
|
|
|
return value |
|
|
|
return node |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// New node
|
|
|
|
if node == nil { |
|
|
|
n := ethutil.NewValue(node) |
|
|
|
return nil |
|
|
|
if node == nil || n.Len() == 0 { |
|
|
|
|
|
|
|
newNode := []interface{}{CompactEncode(key), value} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return t.Put(newNode) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
currentNode := t.getNode(node) |
|
|
|
switch node := node.(type) { |
|
|
|
// Check for "special" 2 slice type node
|
|
|
|
case *ShortNode: |
|
|
|
if currentNode.Len() == 2 { |
|
|
|
k := node.Key() |
|
|
|
// Decode the key
|
|
|
|
cnode := node.Value() |
|
|
|
|
|
|
|
|
|
|
|
k := CompactDecode(currentNode.Get(0).Str()) |
|
|
|
|
|
|
|
v := currentNode.Get(1).Raw() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Matching key pair (ie. there's already an object with this key)
|
|
|
|
|
|
|
|
if bytes.Equal(k, key) { //CompareIntSlice(k, key) {
|
|
|
|
|
|
|
|
newNode := []interface{}{CompactEncode(key), value} |
|
|
|
|
|
|
|
return t.Put(newNode) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var newHash interface{} |
|
|
|
|
|
|
|
matchingLength := MatchingNibbleLength(key, k) |
|
|
|
|
|
|
|
if matchingLength == len(k) { |
|
|
|
|
|
|
|
// Insert the hash, creating a new node
|
|
|
|
|
|
|
|
newHash = t.InsertState(v, key[matchingLength:], value) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
// Expand the 2 length slice to a 17 length slice
|
|
|
|
|
|
|
|
oldNode := t.InsertState("", k[matchingLength+1:], v) |
|
|
|
|
|
|
|
newNode := t.InsertState("", key[matchingLength+1:], value) |
|
|
|
|
|
|
|
// Create an expanded slice
|
|
|
|
|
|
|
|
scaledSlice := EmptyStringSlice(17) |
|
|
|
|
|
|
|
// Set the copied and new node
|
|
|
|
|
|
|
|
scaledSlice[k[matchingLength]] = oldNode |
|
|
|
|
|
|
|
scaledSlice[key[matchingLength]] = newNode |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
newHash = t.Put(scaledSlice) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if matchingLength == 0 { |
|
|
|
|
|
|
|
// End of the chain, return
|
|
|
|
|
|
|
|
return newHash |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
newNode := []interface{}{CompactEncode(key[:matchingLength]), newHash} |
|
|
|
|
|
|
|
return t.Put(newNode) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Copy the current node over to the new node and replace the first nibble in the key
|
|
|
|
|
|
|
|
newNode := EmptyStringSlice(17) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for i := 0; i < 17; i++ { |
|
|
|
if len(key) >= len(k) && bytes.Equal(k, key[:len(k)]) { |
|
|
|
cpy := currentNode.Get(i).Raw() |
|
|
|
return self.get(cnode, key[len(k):]) |
|
|
|
if cpy != nil { |
|
|
|
|
|
|
|
newNode[i] = cpy |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
newNode[key[0]] = t.InsertState(currentNode.Get(int(key[0])).Raw(), key[1:], value) |
|
|
|
return nil |
|
|
|
|
|
|
|
case *FullNode: |
|
|
|
return t.Put(newNode) |
|
|
|
return self.get(node.branch(key[0]), key[1:]) |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
panic(fmt.Sprintf("%T: invalid node: %v", node, node)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
panic("unexpected end") |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (t *Trie) deleteState(node interface{}, key []byte) interface{} { |
|
|
|
func (self *Trie) delete(node Node, key []byte) Node { |
|
|
|
if len(key) == 0 { |
|
|
|
if len(key) == 0 && node == nil { |
|
|
|
return "" |
|
|
|
return nil |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// New node
|
|
|
|
|
|
|
|
n := ethutil.NewValue(node) |
|
|
|
|
|
|
|
//if node == nil || (n.Type() == reflect.String && (n.Str() == "" || n.Get(0).IsNil())) || n.Len() == 0 {
|
|
|
|
|
|
|
|
if node == nil || n.Len() == 0 { |
|
|
|
|
|
|
|
//return nil
|
|
|
|
|
|
|
|
//fmt.Printf("<empty ret> %x %d\n", n, len(n.Bytes()))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return "" |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
currentNode := t.getNode(node) |
|
|
|
switch node := node.(type) { |
|
|
|
// Check for "special" 2 slice type node
|
|
|
|
case *ShortNode: |
|
|
|
if currentNode.Len() == 2 { |
|
|
|
k := node.Key() |
|
|
|
// Decode the key
|
|
|
|
cnode := node.Value() |
|
|
|
k := CompactDecode(currentNode.Get(0).Str()) |
|
|
|
if bytes.Equal(key, k) { |
|
|
|
v := currentNode.Get(1).Raw() |
|
|
|
return nil |
|
|
|
|
|
|
|
} else if bytes.Equal(key[:len(k)], k) { |
|
|
|
// Matching key pair (ie. there's already an object with this key)
|
|
|
|
child := self.delete(cnode, key[len(k):]) |
|
|
|
if bytes.Equal(k, key) { //CompareIntSlice(k, key) {
|
|
|
|
|
|
|
|
//fmt.Printf("<delete ret> %x\n", v)
|
|
|
|
var n Node |
|
|
|
|
|
|
|
switch child := child.(type) { |
|
|
|
return "" |
|
|
|
case *ShortNode: |
|
|
|
} else if bytes.Equal(key[:len(k)], k) { //CompareIntSlice(key[:len(k)], k) {
|
|
|
|
nkey := append(k, child.Key()...) |
|
|
|
hash := t.deleteState(v, key[len(k):]) |
|
|
|
n = NewShortNode(self, nkey, child.Value()) |
|
|
|
child := t.getNode(hash) |
|
|
|
case *FullNode: |
|
|
|
|
|
|
|
sn := NewShortNode(self, node.Key(), child) |
|
|
|
var newNode []interface{} |
|
|
|
sn.key = node.key |
|
|
|
if child.Len() == 2 { |
|
|
|
n = sn |
|
|
|
newKey := append(k, CompactDecode(child.Get(0).Str())...) |
|
|
|
|
|
|
|
newNode = []interface{}{CompactEncode(newKey), child.Get(1).Raw()} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
newNode = []interface{}{currentNode.Get(0).Str(), hash} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//fmt.Printf("%x\n", newNode)
|
|
|
|
return n |
|
|
|
|
|
|
|
|
|
|
|
return t.Put(newNode) |
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
return node |
|
|
|
return node |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
|
|
|
|
// Copy the current node over to the new node and replace the first nibble in the key
|
|
|
|
|
|
|
|
n := EmptyStringSlice(17) |
|
|
|
|
|
|
|
var newNode []interface{} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for i := 0; i < 17; i++ { |
|
|
|
case *FullNode: |
|
|
|
cpy := currentNode.Get(i).Raw() |
|
|
|
n := node.Copy().(*FullNode) |
|
|
|
if cpy != nil { |
|
|
|
n.set(key[0], self.delete(n.branch(key[0]), key[1:])) |
|
|
|
n[i] = cpy |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
n[key[0]] = t.deleteState(n[key[0]], key[1:]) |
|
|
|
pos := -1 |
|
|
|
amount := -1 |
|
|
|
|
|
|
|
for i := 0; i < 17; i++ { |
|
|
|
for i := 0; i < 17; i++ { |
|
|
|
if n[i] != "" { |
|
|
|
if n.branch(byte(i)) != nil { |
|
|
|
if amount == -1 { |
|
|
|
if pos == -1 { |
|
|
|
amount = i |
|
|
|
pos = i |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
amount = -2 |
|
|
|
pos = -2 |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if amount == 16 { |
|
|
|
|
|
|
|
newNode = []interface{}{CompactEncode([]byte{16}), n[amount]} |
|
|
|
|
|
|
|
} else if amount >= 0 { |
|
|
|
|
|
|
|
child := t.getNode(n[amount]) |
|
|
|
|
|
|
|
if child.Len() == 17 { |
|
|
|
|
|
|
|
newNode = []interface{}{CompactEncode([]byte{byte(amount)}), n[amount]} |
|
|
|
|
|
|
|
} else if child.Len() == 2 { |
|
|
|
|
|
|
|
key := append([]byte{byte(amount)}, CompactDecode(child.Get(0).Str())...) |
|
|
|
|
|
|
|
newNode = []interface{}{CompactEncode(key), child.Get(1).Str()} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var nnode Node |
|
|
|
|
|
|
|
if pos == 16 { |
|
|
|
|
|
|
|
nnode = NewShortNode(self, []byte{16}, n.branch(byte(pos))) |
|
|
|
|
|
|
|
} else if pos >= 0 { |
|
|
|
|
|
|
|
cnode := n.branch(byte(pos)) |
|
|
|
|
|
|
|
switch cnode := cnode.(type) { |
|
|
|
|
|
|
|
case *ShortNode: |
|
|
|
|
|
|
|
// Stitch keys
|
|
|
|
|
|
|
|
k := append([]byte{byte(pos)}, cnode.Key()...) |
|
|
|
|
|
|
|
nnode = NewShortNode(self, k, cnode.Value()) |
|
|
|
|
|
|
|
case *FullNode: |
|
|
|
|
|
|
|
nnode = NewShortNode(self, []byte{byte(pos)}, n.branch(byte(pos))) |
|
|
|
|
|
|
|
} |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
newNode = n |
|
|
|
nnode = n |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//fmt.Printf("%x\n", newNode)
|
|
|
|
return nnode |
|
|
|
return t.Put(newNode) |
|
|
|
case nil: |
|
|
|
|
|
|
|
return nil |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
panic(fmt.Sprintf("%T: invalid node: %v (%v)", node, node, key)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
panic("unexpected return") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type TrieIterator struct { |
|
|
|
|
|
|
|
trie *Trie |
|
|
|
|
|
|
|
key string |
|
|
|
|
|
|
|
value string |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
shas [][]byte |
|
|
|
|
|
|
|
values []string |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
lastNode []byte |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (t *Trie) NewIterator() *TrieIterator { |
|
|
|
// casting functions and cache storing
|
|
|
|
return &TrieIterator{trie: t} |
|
|
|
func (self *Trie) mknode(value *ethutil.Value) Node { |
|
|
|
} |
|
|
|
l := value.Len() |
|
|
|
|
|
|
|
switch l { |
|
|
|
func (self *Trie) Iterator() *Iterator { |
|
|
|
case 0: |
|
|
|
return NewIterator(self) |
|
|
|
return nil |
|
|
|
} |
|
|
|
case 2: |
|
|
|
|
|
|
|
// A value node may consists of 2 bytes.
|
|
|
|
// Some time in the near future this will need refactoring :-)
|
|
|
|
if value.Get(0).Len() != 0 { |
|
|
|
// XXX Note to self, IsSlice == inline node. Str == sha3 to node
|
|
|
|
return NewShortNode(self, CompactDecode(string(value.Get(0).Bytes())), self.mknode(value.Get(1))) |
|
|
|
func (it *TrieIterator) workNode(currentNode *ethutil.Value) { |
|
|
|
|
|
|
|
if currentNode.Len() == 2 { |
|
|
|
|
|
|
|
k := CompactDecode(currentNode.Get(0).Str()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if currentNode.Get(1).Str() == "" { |
|
|
|
|
|
|
|
it.workNode(currentNode.Get(1)) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
if k[len(k)-1] == 16 { |
|
|
|
|
|
|
|
it.values = append(it.values, currentNode.Get(1).Str()) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
it.shas = append(it.shas, currentNode.Get(1).Bytes()) |
|
|
|
|
|
|
|
it.getNode(currentNode.Get(1).Bytes()) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
case 17: |
|
|
|
for i := 0; i < currentNode.Len(); i++ { |
|
|
|
fnode := NewFullNode(self) |
|
|
|
if i == 16 && currentNode.Get(i).Len() != 0 { |
|
|
|
for i := 0; i < l; i++ { |
|
|
|
it.values = append(it.values, currentNode.Get(i).Str()) |
|
|
|
fnode.set(byte(i), self.mknode(value.Get(i))) |
|
|
|
} else { |
|
|
|
|
|
|
|
if currentNode.Get(i).Str() == "" { |
|
|
|
|
|
|
|
it.workNode(currentNode.Get(i)) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
val := currentNode.Get(i).Str() |
|
|
|
|
|
|
|
if val != "" { |
|
|
|
|
|
|
|
it.shas = append(it.shas, currentNode.Get(1).Bytes()) |
|
|
|
|
|
|
|
it.getNode([]byte(val)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return fnode |
|
|
|
|
|
|
|
case 32: |
|
|
|
|
|
|
|
return &HashNode{value.Bytes()} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (it *TrieIterator) getNode(node []byte) { |
|
|
|
return &ValueNode{self, value.Bytes()} |
|
|
|
currentNode := it.trie.cache.Get(node) |
|
|
|
|
|
|
|
it.workNode(currentNode) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (it *TrieIterator) Collect() [][]byte { |
|
|
|
func (self *Trie) trans(node Node) Node { |
|
|
|
if it.trie.Root == "" { |
|
|
|
switch node := node.(type) { |
|
|
|
return nil |
|
|
|
case *HashNode: |
|
|
|
} |
|
|
|
value := ethutil.NewValueFromBytes(self.cache.Get(node.key)) |
|
|
|
|
|
|
|
return self.mknode(value) |
|
|
|
it.getNode(ethutil.NewValue(it.trie.Root).Bytes()) |
|
|
|
default: |
|
|
|
|
|
|
|
return node |
|
|
|
return it.shas |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (it *TrieIterator) Purge() int { |
|
|
|
|
|
|
|
shas := it.Collect() |
|
|
|
|
|
|
|
for _, sha := range shas { |
|
|
|
|
|
|
|
it.trie.cache.Delete(sha) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return len(it.values) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (it *TrieIterator) Key() string { |
|
|
|
|
|
|
|
return "" |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (it *TrieIterator) Value() string { |
|
|
|
func (self *Trie) store(node Node) interface{} { |
|
|
|
return "" |
|
|
|
data := ethutil.Encode(node) |
|
|
|
} |
|
|
|
if len(data) >= 32 { |
|
|
|
|
|
|
|
key := crypto.Sha3(data) |
|
|
|
type EachCallback func(key string, node *ethutil.Value) |
|
|
|
self.cache.Put(key, data) |
|
|
|
|
|
|
|
|
|
|
|
func (it *TrieIterator) Each(cb EachCallback) { |
|
|
|
return key |
|
|
|
it.fetchNode(nil, ethutil.NewValue(it.trie.Root).Bytes(), cb) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (it *TrieIterator) fetchNode(key []byte, node []byte, cb EachCallback) { |
|
|
|
return node.RlpData() |
|
|
|
it.iterateNode(key, it.trie.cache.Get(node), cb) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (it *TrieIterator) iterateNode(key []byte, currentNode *ethutil.Value, cb EachCallback) { |
|
|
|
func (self *Trie) PrintRoot() { |
|
|
|
if currentNode.Len() == 2 { |
|
|
|
fmt.Println(self.root) |
|
|
|
k := CompactDecode(currentNode.Get(0).Str()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pk := append(key, k...) |
|
|
|
|
|
|
|
if currentNode.Get(1).Len() != 0 && currentNode.Get(1).Str() == "" { |
|
|
|
|
|
|
|
it.iterateNode(pk, currentNode.Get(1), cb) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
if k[len(k)-1] == 16 { |
|
|
|
|
|
|
|
cb(DecodeCompact(pk), currentNode.Get(1)) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
it.fetchNode(pk, currentNode.Get(1).Bytes(), cb) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
for i := 0; i < currentNode.Len(); i++ { |
|
|
|
|
|
|
|
pk := append(key, byte(i)) |
|
|
|
|
|
|
|
if i == 16 && currentNode.Get(i).Len() != 0 { |
|
|
|
|
|
|
|
cb(DecodeCompact(pk), currentNode.Get(i)) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
if currentNode.Get(i).Len() != 0 && currentNode.Get(i).Str() == "" { |
|
|
|
|
|
|
|
it.iterateNode(pk, currentNode.Get(i), cb) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
val := currentNode.Get(i).Str() |
|
|
|
|
|
|
|
if val != "" { |
|
|
|
|
|
|
|
it.fetchNode(pk, []byte(val), cb) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
*/ |
|
|
|
|
|
|
|