2014-06-29 10:26:58 -05:00
|
|
|
package ethtrie
|
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"
|
2014-07-17 04:21:18 -05:00
|
|
|
_ "reflect"
|
2014-05-20 18:12:28 -05:00
|
|
|
"sync"
|
2014-08-04 03:38:18 -05:00
|
|
|
|
|
|
|
"github.com/ethereum/eth-go/ethcrypto"
|
|
|
|
"github.com/ethereum/eth-go/ethutil"
|
2014-02-14 16:56:09 -06:00
|
|
|
)
|
|
|
|
|
2014-07-15 08:29:54 -05:00
|
|
|
func __ignore() { fmt.Println("") }
|
|
|
|
|
2014-07-02 10:47:18 -05:00
|
|
|
func ParanoiaCheck(t1 *Trie) (bool, *Trie) {
|
2014-08-04 03:38:18 -05:00
|
|
|
t2 := New(ethutil.Config.Db, "")
|
2014-07-02 10:47:18 -05:00
|
|
|
|
|
|
|
t1.NewIterator().Each(func(key string, v *ethutil.Value) {
|
|
|
|
t2.Update(key, v.Str())
|
|
|
|
})
|
|
|
|
|
|
|
|
a := ethutil.NewValue(t2.Root).Bytes()
|
|
|
|
b := ethutil.NewValue(t1.Root).Bytes()
|
|
|
|
|
|
|
|
return bytes.Compare(a, b) == 0, t2
|
|
|
|
}
|
|
|
|
|
2014-05-20 18:12:28 -05:00
|
|
|
func (s *Cache) Len() int {
|
|
|
|
return len(s.nodes)
|
|
|
|
}
|
|
|
|
|
2014-02-19 09:28:08 -06:00
|
|
|
// 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()
|
|
|
|
}
|
|
|
|
|
2014-02-14 16:56:09 -06:00
|
|
|
type Node struct {
|
|
|
|
Key []byte
|
2014-06-29 10:26:58 -05:00
|
|
|
Value *ethutil.Value
|
2014-02-14 16:56:09 -06:00
|
|
|
Dirty bool
|
|
|
|
}
|
|
|
|
|
2014-06-29 10:26:58 -05:00
|
|
|
func NewNode(key []byte, val *ethutil.Value, dirty bool) *Node {
|
2014-02-14 16:56:09 -06:00
|
|
|
return &Node{Key: key, Value: val, Dirty: dirty}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *Node) Copy() *Node {
|
|
|
|
return NewNode(n.Key, n.Value, n.Dirty)
|
|
|
|
}
|
|
|
|
|
|
|
|
type Cache struct {
|
2014-02-16 13:32:56 -06:00
|
|
|
nodes map[string]*Node
|
2014-06-29 10:26:58 -05:00
|
|
|
db ethutil.Database
|
2014-02-16 13:32:56 -06:00
|
|
|
IsDirty bool
|
2014-02-14 16:56:09 -06:00
|
|
|
}
|
|
|
|
|
2014-06-29 10:26:58 -05:00
|
|
|
func NewCache(db ethutil.Database) *Cache {
|
2014-02-14 16:56:09 -06:00
|
|
|
return &Cache{db: db, nodes: make(map[string]*Node)}
|
|
|
|
}
|
|
|
|
|
2014-06-30 06:08:00 -05:00
|
|
|
func (cache *Cache) PutValue(v interface{}, force bool) interface{} {
|
2014-06-29 10:26:58 -05:00
|
|
|
value := ethutil.NewValue(v)
|
2014-02-14 16:56:09 -06:00
|
|
|
|
|
|
|
enc := value.Encode()
|
2014-06-30 06:08:00 -05:00
|
|
|
if len(enc) >= 32 || force {
|
2014-10-08 05:06:39 -05:00
|
|
|
sha := ethcrypto.Sha3(enc)
|
2014-02-14 16:56:09 -06:00
|
|
|
|
|
|
|
cache.nodes[string(sha)] = NewNode(sha, value, true)
|
2014-02-16 13:32:56 -06:00
|
|
|
cache.IsDirty = true
|
2014-02-14 16:56:09 -06:00
|
|
|
|
|
|
|
return sha
|
|
|
|
}
|
|
|
|
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2014-06-30 06:08:00 -05:00
|
|
|
func (cache *Cache) Put(v interface{}) interface{} {
|
|
|
|
return cache.PutValue(v, false)
|
|
|
|
}
|
|
|
|
|
2014-06-29 10:26:58 -05:00
|
|
|
func (cache *Cache) Get(key []byte) *ethutil.Value {
|
2014-02-14 16:56:09 -06:00
|
|
|
// First check if the key is the cache
|
|
|
|
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
|
2014-06-29 10:26:58 -05:00
|
|
|
value := ethutil.NewValueFromBytes(data)
|
2014-09-17 18:02:15 -05:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
fmt.Println("RECOVER GET", cache, cache.nodes)
|
|
|
|
panic("bye")
|
|
|
|
}
|
|
|
|
}()
|
2014-02-14 16:56:09 -06:00
|
|
|
// Create caching node
|
|
|
|
cache.nodes[string(key)] = NewNode(key, value, false)
|
|
|
|
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2014-02-24 05:11:00 -06:00
|
|
|
func (cache *Cache) Delete(key []byte) {
|
|
|
|
delete(cache.nodes, string(key))
|
|
|
|
|
|
|
|
cache.db.Delete(key)
|
|
|
|
}
|
|
|
|
|
2014-02-14 16:56:09 -06:00
|
|
|
func (cache *Cache) Commit() {
|
2014-02-16 13:32:56 -06:00
|
|
|
// Don't try to commit if it isn't dirty
|
|
|
|
if !cache.IsDirty {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-02-14 16:56:09 -06:00
|
|
|
for key, node := range cache.nodes {
|
|
|
|
if node.Dirty {
|
|
|
|
cache.db.Put([]byte(key), node.Value.Encode())
|
|
|
|
node.Dirty = false
|
|
|
|
}
|
|
|
|
}
|
2014-02-16 13:32:56 -06:00
|
|
|
cache.IsDirty = false
|
2014-02-14 16:56:09 -06:00
|
|
|
|
|
|
|
// 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() {
|
|
|
|
for key, node := range cache.nodes {
|
|
|
|
if node.Dirty {
|
|
|
|
delete(cache.nodes, key)
|
|
|
|
}
|
|
|
|
}
|
2014-02-16 13:32:56 -06:00
|
|
|
cache.IsDirty = false
|
2014-02-14 16:56:09 -06:00
|
|
|
}
|
|
|
|
|
2014-02-14 18:34:18 -06:00
|
|
|
// A (modified) Radix Trie implementation. The Trie implements
|
|
|
|
// 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.
|
2014-02-14 16:56:09 -06:00
|
|
|
type Trie struct {
|
2014-05-20 18:12:28 -05:00
|
|
|
mut sync.RWMutex
|
2014-02-17 13:40:33 -06:00
|
|
|
prevRoot interface{}
|
|
|
|
Root interface{}
|
2014-02-14 16:56:09 -06:00
|
|
|
//db Database
|
|
|
|
cache *Cache
|
|
|
|
}
|
|
|
|
|
2014-04-29 05:36:27 -05:00
|
|
|
func copyRoot(root interface{}) interface{} {
|
|
|
|
var prevRootCopy interface{}
|
|
|
|
if b, ok := root.([]byte); ok {
|
2014-06-29 10:26:58 -05:00
|
|
|
prevRootCopy = ethutil.CopyBytes(b)
|
2014-04-29 05:36:27 -05:00
|
|
|
} else {
|
|
|
|
prevRootCopy = root
|
|
|
|
}
|
|
|
|
|
|
|
|
return prevRootCopy
|
|
|
|
}
|
|
|
|
|
2014-08-04 03:38:18 -05:00
|
|
|
func New(db ethutil.Database, Root interface{}) *Trie {
|
2014-04-29 05:36:27 -05:00
|
|
|
// Make absolute sure the root is copied
|
|
|
|
r := copyRoot(Root)
|
|
|
|
p := copyRoot(Root)
|
|
|
|
|
|
|
|
return &Trie{cache: NewCache(db), Root: r, prevRoot: p}
|
2014-02-14 16:56:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Public (query) interface functions
|
|
|
|
*/
|
2014-07-02 06:40:02 -05:00
|
|
|
|
|
|
|
func (t *Trie) Update(key, value string) {
|
2014-05-20 18:12:28 -05:00
|
|
|
t.mut.Lock()
|
|
|
|
defer t.mut.Unlock()
|
|
|
|
|
2014-02-14 16:56:09 -06:00
|
|
|
k := CompactHexDecode(key)
|
|
|
|
|
2014-06-30 06:08:00 -05:00
|
|
|
root := t.UpdateState(t.Root, k, value)
|
2014-06-30 13:03:31 -05:00
|
|
|
switch root.(type) {
|
|
|
|
case string:
|
2014-06-30 06:08:00 -05:00
|
|
|
t.Root = root
|
2014-06-30 13:03:31 -05:00
|
|
|
case []byte:
|
|
|
|
t.Root = root
|
|
|
|
default:
|
|
|
|
t.Root = t.cache.PutValue(root, true)
|
2014-06-30 06:08:00 -05:00
|
|
|
}
|
2014-02-14 16:56:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Trie) Get(key string) string {
|
2014-09-24 10:47:01 -05:00
|
|
|
t.mut.Lock()
|
|
|
|
defer t.mut.Unlock()
|
2014-05-20 18:12:28 -05:00
|
|
|
|
2014-02-14 16:56:09 -06:00
|
|
|
k := CompactHexDecode(key)
|
2014-07-02 06:40:02 -05:00
|
|
|
c := ethutil.NewValue(t.getState(t.Root, k))
|
2014-02-14 16:56:09 -06:00
|
|
|
|
|
|
|
return c.Str()
|
|
|
|
}
|
|
|
|
|
2014-02-20 07:40:00 -06:00
|
|
|
func (t *Trie) Delete(key string) {
|
2014-06-30 06:34:47 -05:00
|
|
|
t.mut.Lock()
|
|
|
|
defer t.mut.Unlock()
|
|
|
|
|
|
|
|
k := CompactHexDecode(key)
|
|
|
|
|
2014-07-02 06:40:02 -05:00
|
|
|
root := t.deleteState(t.Root, k)
|
2014-06-30 13:03:31 -05:00
|
|
|
switch root.(type) {
|
|
|
|
case string:
|
2014-06-30 06:34:47 -05:00
|
|
|
t.Root = root
|
2014-06-30 13:03:31 -05:00
|
|
|
case []byte:
|
|
|
|
t.Root = root
|
|
|
|
default:
|
|
|
|
t.Root = t.cache.PutValue(root, true)
|
2014-06-30 06:34:47 -05:00
|
|
|
}
|
2014-02-20 07:40:00 -06:00
|
|
|
}
|
|
|
|
|
2014-07-02 06:40:02 -05:00
|
|
|
// Simple compare function which creates a rlp value out of the evaluated objects
|
|
|
|
func (t *Trie) Cmp(trie *Trie) bool {
|
|
|
|
return ethutil.NewValue(t.Root).Cmp(ethutil.NewValue(trie.Root))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a copy of this trie
|
|
|
|
func (t *Trie) Copy() *Trie {
|
2014-08-04 03:38:18 -05:00
|
|
|
trie := New(t.cache.db, t.Root)
|
2014-07-02 06:40:02 -05:00
|
|
|
for key, node := range t.cache.nodes {
|
|
|
|
trie.cache.nodes[key] = node.Copy()
|
|
|
|
}
|
|
|
|
|
|
|
|
return trie
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the cached value to the database.
|
|
|
|
func (t *Trie) Sync() {
|
|
|
|
t.cache.Commit()
|
|
|
|
t.prevRoot = copyRoot(t.Root)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Trie) Undo() {
|
|
|
|
t.cache.Undo()
|
|
|
|
t.Root = t.prevRoot
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Trie) Cache() *Cache {
|
|
|
|
return t.cache
|
|
|
|
}
|
|
|
|
|
2014-10-10 10:00:06 -05:00
|
|
|
func (t *Trie) getState(node interface{}, key []byte) interface{} {
|
2014-06-29 10:26:58 -05:00
|
|
|
n := ethutil.NewValue(node)
|
2014-02-14 16:56:09 -06:00
|
|
|
// Return the node if key is empty (= found)
|
|
|
|
if len(key) == 0 || n.IsNil() || n.Len() == 0 {
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
2014-07-02 06:40:02 -05:00
|
|
|
currentNode := t.getNode(node)
|
2014-02-14 16:56:09 -06:00
|
|
|
length := currentNode.Len()
|
|
|
|
|
|
|
|
if length == 0 {
|
|
|
|
return ""
|
|
|
|
} else if length == 2 {
|
|
|
|
// Decode the key
|
|
|
|
k := CompactDecode(currentNode.Get(0).Str())
|
|
|
|
v := currentNode.Get(1).Raw()
|
|
|
|
|
2014-10-10 10:00:06 -05:00
|
|
|
if len(key) >= len(k) && bytes.Equal(k, key[:len(k)]) { //CompareIntSlice(k, key[:len(k)]) {
|
2014-07-02 06:40:02 -05:00
|
|
|
return t.getState(v, key[len(k):])
|
2014-02-14 16:56:09 -06:00
|
|
|
} else {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
} else if length == 17 {
|
2014-10-10 10:00:06 -05:00
|
|
|
return t.getState(currentNode.Get(int(key[0])).Raw(), key[1:])
|
2014-02-14 16:56:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// It shouldn't come this far
|
2014-07-15 08:29:54 -05:00
|
|
|
panic("unexpected return")
|
2014-02-14 16:56:09 -06:00
|
|
|
}
|
|
|
|
|
2014-07-02 06:40:02 -05:00
|
|
|
func (t *Trie) getNode(node interface{}) *ethutil.Value {
|
2014-06-29 10:26:58 -05:00
|
|
|
n := ethutil.NewValue(node)
|
2014-02-14 16:56:09 -06:00
|
|
|
|
|
|
|
if !n.Get(0).IsNil() {
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
str := n.Str()
|
|
|
|
if len(str) == 0 {
|
|
|
|
return n
|
|
|
|
} else if len(str) < 32 {
|
2014-06-29 10:26:58 -05:00
|
|
|
return ethutil.NewValueFromBytes([]byte(str))
|
2014-02-14 16:56:09 -06:00
|
|
|
}
|
|
|
|
|
2014-07-15 08:29:54 -05:00
|
|
|
data := t.cache.Get(n.Bytes())
|
|
|
|
|
|
|
|
return data
|
2014-02-14 16:56:09 -06:00
|
|
|
}
|
|
|
|
|
2014-10-10 10:00:06 -05:00
|
|
|
func (t *Trie) UpdateState(node interface{}, key []byte, value string) interface{} {
|
2014-06-30 06:34:47 -05:00
|
|
|
return t.InsertState(node, key, value)
|
2014-02-14 16:56:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Trie) Put(node interface{}) interface{} {
|
|
|
|
return t.cache.Put(node)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func EmptyStringSlice(l int) []interface{} {
|
|
|
|
slice := make([]interface{}, l)
|
|
|
|
for i := 0; i < l; i++ {
|
|
|
|
slice[i] = ""
|
|
|
|
}
|
|
|
|
return slice
|
|
|
|
}
|
|
|
|
|
2014-10-10 10:00:06 -05:00
|
|
|
func (t *Trie) InsertState(node interface{}, key []byte, value interface{}) interface{} {
|
2014-02-14 16:56:09 -06:00
|
|
|
if len(key) == 0 {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
// New node
|
2014-06-29 10:26:58 -05:00
|
|
|
n := ethutil.NewValue(node)
|
2014-07-17 04:21:18 -05:00
|
|
|
if node == nil || n.Len() == 0 {
|
2014-02-14 16:56:09 -06:00
|
|
|
newNode := []interface{}{CompactEncode(key), value}
|
|
|
|
|
|
|
|
return t.Put(newNode)
|
|
|
|
}
|
|
|
|
|
2014-07-02 06:40:02 -05:00
|
|
|
currentNode := t.getNode(node)
|
2014-02-14 16:56:09 -06:00
|
|
|
// Check for "special" 2 slice type node
|
|
|
|
if currentNode.Len() == 2 {
|
|
|
|
// Decode the key
|
2014-02-17 13:40:33 -06:00
|
|
|
|
2014-02-14 16:56:09 -06:00
|
|
|
k := CompactDecode(currentNode.Get(0).Str())
|
|
|
|
v := currentNode.Get(1).Raw()
|
|
|
|
|
|
|
|
// Matching key pair (ie. there's already an object with this key)
|
2014-10-10 10:00:06 -05:00
|
|
|
if bytes.Equal(k, key) { //CompareIntSlice(k, key) {
|
2014-02-14 16:56:09 -06:00
|
|
|
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++ {
|
|
|
|
cpy := currentNode.Get(i).Raw()
|
|
|
|
if cpy != nil {
|
|
|
|
newNode[i] = cpy
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-10 10:00:06 -05:00
|
|
|
newNode[key[0]] = t.InsertState(currentNode.Get(int(key[0])).Raw(), key[1:], value)
|
2014-02-14 16:56:09 -06:00
|
|
|
|
|
|
|
return t.Put(newNode)
|
|
|
|
}
|
|
|
|
|
2014-07-15 08:29:54 -05:00
|
|
|
panic("unexpected end")
|
2014-02-14 16:56:09 -06:00
|
|
|
}
|
|
|
|
|
2014-10-10 10:00:06 -05:00
|
|
|
func (t *Trie) deleteState(node interface{}, key []byte) interface{} {
|
2014-02-20 07:40:00 -06:00
|
|
|
if len(key) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// New node
|
2014-06-29 10:26:58 -05:00
|
|
|
n := ethutil.NewValue(node)
|
2014-07-17 04:21:18 -05:00
|
|
|
//if node == nil || (n.Type() == reflect.String && (n.Str() == "" || n.Get(0).IsNil())) || n.Len() == 0 {
|
|
|
|
if node == nil || n.Len() == 0 {
|
2014-07-15 08:29:54 -05:00
|
|
|
//return nil
|
2014-07-17 04:21:18 -05:00
|
|
|
//fmt.Printf("<empty ret> %x %d\n", n, len(n.Bytes()))
|
|
|
|
|
2014-02-20 07:40:00 -06:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2014-07-02 06:40:02 -05:00
|
|
|
currentNode := t.getNode(node)
|
2014-02-20 07:40:00 -06:00
|
|
|
// Check for "special" 2 slice type node
|
|
|
|
if currentNode.Len() == 2 {
|
|
|
|
// Decode the key
|
|
|
|
k := CompactDecode(currentNode.Get(0).Str())
|
|
|
|
v := currentNode.Get(1).Raw()
|
|
|
|
|
|
|
|
// Matching key pair (ie. there's already an object with this key)
|
2014-10-10 10:00:06 -05:00
|
|
|
if bytes.Equal(k, key) { //CompareIntSlice(k, key) {
|
2014-07-17 04:21:18 -05:00
|
|
|
//fmt.Printf("<delete ret> %x\n", v)
|
|
|
|
|
2014-02-20 07:40:00 -06:00
|
|
|
return ""
|
2014-10-10 10:00:06 -05:00
|
|
|
} else if bytes.Equal(key[:len(k)], k) { //CompareIntSlice(key[:len(k)], k) {
|
2014-07-02 06:40:02 -05:00
|
|
|
hash := t.deleteState(v, key[len(k):])
|
|
|
|
child := t.getNode(hash)
|
2014-02-20 07:40:00 -06:00
|
|
|
|
|
|
|
var newNode []interface{}
|
|
|
|
if child.Len() == 2 {
|
|
|
|
newKey := append(k, CompactDecode(child.Get(0).Str())...)
|
|
|
|
newNode = []interface{}{CompactEncode(newKey), child.Get(1).Raw()}
|
|
|
|
} else {
|
|
|
|
newNode = []interface{}{currentNode.Get(0).Str(), hash}
|
|
|
|
}
|
|
|
|
|
2014-07-17 04:21:18 -05:00
|
|
|
//fmt.Printf("%x\n", newNode)
|
|
|
|
|
2014-02-20 07:40:00 -06:00
|
|
|
return t.Put(newNode)
|
|
|
|
} else {
|
|
|
|
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++ {
|
|
|
|
cpy := currentNode.Get(i).Raw()
|
|
|
|
if cpy != nil {
|
|
|
|
n[i] = cpy
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-02 06:40:02 -05:00
|
|
|
n[key[0]] = t.deleteState(n[key[0]], key[1:])
|
2014-02-20 07:40:00 -06:00
|
|
|
amount := -1
|
|
|
|
for i := 0; i < 17; i++ {
|
|
|
|
if n[i] != "" {
|
|
|
|
if amount == -1 {
|
|
|
|
amount = i
|
|
|
|
} else {
|
|
|
|
amount = -2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if amount == 16 {
|
2014-10-10 10:00:06 -05:00
|
|
|
newNode = []interface{}{CompactEncode([]byte{16}), n[amount]}
|
2014-02-20 07:40:00 -06:00
|
|
|
} else if amount >= 0 {
|
2014-07-02 06:40:02 -05:00
|
|
|
child := t.getNode(n[amount])
|
2014-02-20 07:40:00 -06:00
|
|
|
if child.Len() == 17 {
|
2014-10-10 10:00:06 -05:00
|
|
|
newNode = []interface{}{CompactEncode([]byte{byte(amount)}), n[amount]}
|
2014-02-20 07:40:00 -06:00
|
|
|
} else if child.Len() == 2 {
|
2014-10-10 10:00:06 -05:00
|
|
|
key := append([]byte{byte(amount)}, CompactDecode(child.Get(0).Str())...)
|
2014-02-20 07:40:00 -06:00
|
|
|
newNode = []interface{}{CompactEncode(key), child.Get(1).Str()}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
newNode = n
|
|
|
|
}
|
|
|
|
|
2014-07-17 04:21:18 -05:00
|
|
|
//fmt.Printf("%x\n", newNode)
|
2014-02-20 07:40:00 -06:00
|
|
|
return t.Put(newNode)
|
|
|
|
}
|
|
|
|
|
2014-07-17 04:21:18 -05:00
|
|
|
panic("unexpected return")
|
2014-02-20 07:40:00 -06:00
|
|
|
}
|
|
|
|
|
2014-02-24 05:11:00 -06:00
|
|
|
type TrieIterator struct {
|
|
|
|
trie *Trie
|
|
|
|
key string
|
|
|
|
value string
|
|
|
|
|
|
|
|
shas [][]byte
|
|
|
|
values []string
|
2014-05-26 18:08:51 -05:00
|
|
|
|
|
|
|
lastNode []byte
|
2014-02-24 05:11:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *Trie) NewIterator() *TrieIterator {
|
|
|
|
return &TrieIterator{trie: t}
|
|
|
|
}
|
|
|
|
|
2014-10-10 10:00:06 -05:00
|
|
|
func (self *Trie) Iterator() *Iterator {
|
|
|
|
return NewIterator(self)
|
|
|
|
}
|
|
|
|
|
2014-02-24 05:11:00 -06:00
|
|
|
// Some time in the near future this will need refactoring :-)
|
|
|
|
// XXX Note to self, IsSlice == inline node. Str == sha3 to node
|
2014-06-29 10:26:58 -05:00
|
|
|
func (it *TrieIterator) workNode(currentNode *ethutil.Value) {
|
2014-02-24 05:11:00 -06:00
|
|
|
if currentNode.Len() == 2 {
|
|
|
|
k := CompactDecode(currentNode.Get(0).Str())
|
|
|
|
|
2014-02-25 03:50:53 -06:00
|
|
|
if currentNode.Get(1).Str() == "" {
|
2014-02-24 05:11:00 -06:00
|
|
|
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 {
|
|
|
|
for i := 0; i < currentNode.Len(); i++ {
|
|
|
|
if i == 16 && currentNode.Get(i).Len() != 0 {
|
|
|
|
it.values = append(it.values, currentNode.Get(i).Str())
|
|
|
|
} else {
|
2014-02-25 03:50:53 -06:00
|
|
|
if currentNode.Get(i).Str() == "" {
|
2014-02-24 05:11:00 -06:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (it *TrieIterator) getNode(node []byte) {
|
|
|
|
currentNode := it.trie.cache.Get(node)
|
|
|
|
it.workNode(currentNode)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (it *TrieIterator) Collect() [][]byte {
|
|
|
|
if it.trie.Root == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-06-29 10:26:58 -05:00
|
|
|
it.getNode(ethutil.NewValue(it.trie.Root).Bytes())
|
2014-02-24 05:11:00 -06:00
|
|
|
|
|
|
|
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 {
|
|
|
|
return ""
|
|
|
|
}
|
2014-05-26 18:08:51 -05:00
|
|
|
|
2014-06-29 10:26:58 -05:00
|
|
|
type EachCallback func(key string, node *ethutil.Value)
|
2014-05-26 18:08:51 -05:00
|
|
|
|
|
|
|
func (it *TrieIterator) Each(cb EachCallback) {
|
2014-06-29 10:26:58 -05:00
|
|
|
it.fetchNode(nil, ethutil.NewValue(it.trie.Root).Bytes(), cb)
|
2014-05-26 18:08:51 -05:00
|
|
|
}
|
|
|
|
|
2014-10-10 10:00:06 -05:00
|
|
|
func (it *TrieIterator) fetchNode(key []byte, node []byte, cb EachCallback) {
|
2014-05-26 18:08:51 -05:00
|
|
|
it.iterateNode(key, it.trie.cache.Get(node), cb)
|
|
|
|
}
|
|
|
|
|
2014-10-10 10:00:06 -05:00
|
|
|
func (it *TrieIterator) iterateNode(key []byte, currentNode *ethutil.Value, cb EachCallback) {
|
2014-05-26 18:08:51 -05:00
|
|
|
if currentNode.Len() == 2 {
|
|
|
|
k := CompactDecode(currentNode.Get(0).Str())
|
|
|
|
|
2014-06-30 07:28:54 -05:00
|
|
|
pk := append(key, k...)
|
|
|
|
if currentNode.Get(1).Len() != 0 && currentNode.Get(1).Str() == "" {
|
|
|
|
it.iterateNode(pk, currentNode.Get(1), cb)
|
2014-05-26 18:08:51 -05:00
|
|
|
} 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++ {
|
2014-10-10 10:00:06 -05:00
|
|
|
pk := append(key, byte(i))
|
2014-05-26 18:08:51 -05:00
|
|
|
if i == 16 && currentNode.Get(i).Len() != 0 {
|
|
|
|
cb(DecodeCompact(pk), currentNode.Get(i))
|
|
|
|
} else {
|
2014-06-30 07:28:54 -05:00
|
|
|
if currentNode.Get(i).Len() != 0 && currentNode.Get(i).Str() == "" {
|
2014-05-26 18:08:51 -05:00
|
|
|
it.iterateNode(pk, currentNode.Get(i), cb)
|
|
|
|
} else {
|
|
|
|
val := currentNode.Get(i).Str()
|
|
|
|
if val != "" {
|
|
|
|
it.fetchNode(pk, []byte(val), cb)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|