mirror of https://github.com/ethereum/go-ethereum
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
564 lines
12 KiB
564 lines
12 KiB
11 years ago
|
package ethtrie
|
||
11 years ago
|
|
||
|
import (
|
||
|
"fmt"
|
||
11 years ago
|
"github.com/ethereum/eth-go/ethcrypto"
|
||
|
"github.com/ethereum/eth-go/ethutil"
|
||
11 years ago
|
"reflect"
|
||
11 years ago
|
"sync"
|
||
11 years ago
|
)
|
||
|
|
||
11 years ago
|
func (s *Cache) Len() int {
|
||
|
return len(s.nodes)
|
||
|
}
|
||
|
|
||
11 years ago
|
// 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()
|
||
|
}
|
||
|
|
||
11 years ago
|
type Node struct {
|
||
|
Key []byte
|
||
11 years ago
|
Value *ethutil.Value
|
||
11 years ago
|
Dirty bool
|
||
|
}
|
||
|
|
||
11 years ago
|
func NewNode(key []byte, val *ethutil.Value, dirty bool) *Node {
|
||
11 years ago
|
return &Node{Key: key, Value: val, Dirty: dirty}
|
||
|
}
|
||
|
|
||
|
func (n *Node) Copy() *Node {
|
||
|
return NewNode(n.Key, n.Value, n.Dirty)
|
||
|
}
|
||
|
|
||
|
type Cache struct {
|
||
11 years ago
|
nodes map[string]*Node
|
||
11 years ago
|
db ethutil.Database
|
||
11 years ago
|
IsDirty bool
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
func NewCache(db ethutil.Database) *Cache {
|
||
11 years ago
|
return &Cache{db: db, nodes: make(map[string]*Node)}
|
||
|
}
|
||
|
|
||
|
func (cache *Cache) Put(v interface{}) interface{} {
|
||
11 years ago
|
value := ethutil.NewValue(v)
|
||
11 years ago
|
|
||
|
enc := value.Encode()
|
||
11 years ago
|
if len(enc) >= 32 {
|
||
11 years ago
|
sha := ethcrypto.Sha3Bin(enc)
|
||
11 years ago
|
|
||
|
cache.nodes[string(sha)] = NewNode(sha, value, true)
|
||
11 years ago
|
cache.IsDirty = true
|
||
11 years ago
|
|
||
|
return sha
|
||
|
}
|
||
|
|
||
|
return v
|
||
|
}
|
||
|
|
||
11 years ago
|
func (cache *Cache) Get(key []byte) *ethutil.Value {
|
||
11 years ago
|
// 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
|
||
11 years ago
|
value := ethutil.NewValueFromBytes(data)
|
||
11 years ago
|
// Create caching node
|
||
|
cache.nodes[string(key)] = NewNode(key, value, false)
|
||
|
|
||
|
return value
|
||
|
}
|
||
|
|
||
11 years ago
|
func (cache *Cache) Delete(key []byte) {
|
||
|
delete(cache.nodes, string(key))
|
||
|
|
||
|
cache.db.Delete(key)
|
||
|
}
|
||
|
|
||
11 years ago
|
func (cache *Cache) Commit() {
|
||
11 years ago
|
// Don't try to commit if it isn't dirty
|
||
|
if !cache.IsDirty {
|
||
|
return
|
||
|
}
|
||
|
|
||
11 years ago
|
for key, node := range cache.nodes {
|
||
|
if node.Dirty {
|
||
|
cache.db.Put([]byte(key), node.Value.Encode())
|
||
|
node.Dirty = false
|
||
|
}
|
||
|
}
|
||
11 years ago
|
cache.IsDirty = false
|
||
11 years ago
|
|
||
|
// 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)
|
||
|
}
|
||
|
}
|
||
11 years ago
|
cache.IsDirty = false
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
// 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.
|
||
11 years ago
|
type Trie struct {
|
||
11 years ago
|
mut sync.RWMutex
|
||
11 years ago
|
prevRoot interface{}
|
||
|
Root interface{}
|
||
11 years ago
|
//db Database
|
||
|
cache *Cache
|
||
|
}
|
||
|
|
||
11 years ago
|
func copyRoot(root interface{}) interface{} {
|
||
|
var prevRootCopy interface{}
|
||
|
if b, ok := root.([]byte); ok {
|
||
11 years ago
|
prevRootCopy = ethutil.CopyBytes(b)
|
||
11 years ago
|
} else {
|
||
|
prevRootCopy = root
|
||
|
}
|
||
|
|
||
|
return prevRootCopy
|
||
|
}
|
||
|
|
||
11 years ago
|
func NewTrie(db ethutil.Database, Root interface{}) *Trie {
|
||
11 years ago
|
// Make absolute sure the root is copied
|
||
|
r := copyRoot(Root)
|
||
|
p := copyRoot(Root)
|
||
|
|
||
|
return &Trie{cache: NewCache(db), Root: r, prevRoot: p}
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
// Save the cached value to the database.
|
||
11 years ago
|
func (t *Trie) Sync() {
|
||
|
t.cache.Commit()
|
||
11 years ago
|
t.prevRoot = copyRoot(t.Root)
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
func (t *Trie) Undo() {
|
||
|
t.cache.Undo()
|
||
11 years ago
|
t.Root = t.prevRoot
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
func (t *Trie) Cache() *Cache {
|
||
|
return t.cache
|
||
|
}
|
||
|
|
||
11 years ago
|
/*
|
||
|
* Public (query) interface functions
|
||
|
*/
|
||
|
func (t *Trie) Update(key string, value string) {
|
||
11 years ago
|
t.mut.Lock()
|
||
|
defer t.mut.Unlock()
|
||
|
|
||
11 years ago
|
k := CompactHexDecode(key)
|
||
|
|
||
|
t.Root = t.UpdateState(t.Root, k, value)
|
||
|
}
|
||
|
|
||
|
func (t *Trie) Get(key string) string {
|
||
11 years ago
|
t.mut.RLock()
|
||
|
defer t.mut.RUnlock()
|
||
|
|
||
11 years ago
|
k := CompactHexDecode(key)
|
||
11 years ago
|
c := ethutil.NewValue(t.GetState(t.Root, k))
|
||
11 years ago
|
|
||
|
return c.Str()
|
||
|
}
|
||
|
|
||
11 years ago
|
func (t *Trie) Delete(key string) {
|
||
|
t.Update(key, "")
|
||
|
}
|
||
|
|
||
11 years ago
|
func (t *Trie) GetState(node interface{}, key []int) interface{} {
|
||
11 years ago
|
n := ethutil.NewValue(node)
|
||
11 years ago
|
// Return the node if key is empty (= found)
|
||
|
if len(key) == 0 || n.IsNil() || n.Len() == 0 {
|
||
|
return node
|
||
|
}
|
||
|
|
||
|
currentNode := t.GetNode(node)
|
||
|
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()
|
||
|
|
||
|
if len(key) >= len(k) && CompareIntSlice(k, key[:len(k)]) {
|
||
|
return t.GetState(v, key[len(k):])
|
||
|
} else {
|
||
|
return ""
|
||
|
}
|
||
|
} else if length == 17 {
|
||
|
return t.GetState(currentNode.Get(key[0]).Raw(), key[1:])
|
||
|
}
|
||
|
|
||
|
// It shouldn't come this far
|
||
|
fmt.Println("GetState unexpected return")
|
||
|
return ""
|
||
|
}
|
||
|
|
||
11 years ago
|
func (t *Trie) GetNode(node interface{}) *ethutil.Value {
|
||
|
n := ethutil.NewValue(node)
|
||
11 years ago
|
|
||
|
if !n.Get(0).IsNil() {
|
||
|
return n
|
||
|
}
|
||
|
|
||
|
str := n.Str()
|
||
|
if len(str) == 0 {
|
||
|
return n
|
||
|
} else if len(str) < 32 {
|
||
11 years ago
|
return ethutil.NewValueFromBytes([]byte(str))
|
||
11 years ago
|
}
|
||
|
|
||
|
return t.cache.Get(n.Bytes())
|
||
|
}
|
||
|
|
||
|
func (t *Trie) UpdateState(node interface{}, key []int, value string) interface{} {
|
||
11 years ago
|
|
||
11 years ago
|
if value != "" {
|
||
|
return t.InsertState(node, key, value)
|
||
|
} else {
|
||
|
// delete it
|
||
11 years ago
|
return t.DeleteState(node, key)
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
return t.Root
|
||
11 years ago
|
}
|
||
|
|
||
|
func (t *Trie) Put(node interface{}) interface{} {
|
||
|
/*
|
||
|
TODO?
|
||
|
c := Conv(t.Root)
|
||
|
fmt.Println(c.Type(), c.Length())
|
||
|
if c.Type() == reflect.String && c.AsString() == "" {
|
||
|
return enc
|
||
|
}
|
||
|
*/
|
||
|
|
||
|
return t.cache.Put(node)
|
||
|
|
||
|
}
|
||
|
|
||
|
func EmptyStringSlice(l int) []interface{} {
|
||
|
slice := make([]interface{}, l)
|
||
|
for i := 0; i < l; i++ {
|
||
|
slice[i] = ""
|
||
|
}
|
||
|
return slice
|
||
|
}
|
||
|
|
||
|
func (t *Trie) InsertState(node interface{}, key []int, value interface{}) interface{} {
|
||
|
if len(key) == 0 {
|
||
|
return value
|
||
|
}
|
||
|
|
||
|
// New node
|
||
11 years ago
|
n := ethutil.NewValue(node)
|
||
11 years ago
|
if node == nil || (n.Type() == reflect.String && (n.Str() == "" || n.Get(0).IsNil())) || n.Len() == 0 {
|
||
|
newNode := []interface{}{CompactEncode(key), value}
|
||
|
|
||
|
return t.Put(newNode)
|
||
|
}
|
||
|
|
||
|
currentNode := t.GetNode(node)
|
||
|
// Check for "special" 2 slice type node
|
||
|
if currentNode.Len() == 2 {
|
||
|
// Decode the key
|
||
11 years ago
|
|
||
11 years ago
|
k := CompactDecode(currentNode.Get(0).Str())
|
||
|
v := currentNode.Get(1).Raw()
|
||
|
|
||
|
// Matching key pair (ie. there's already an object with this key)
|
||
|
if 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++ {
|
||
|
cpy := currentNode.Get(i).Raw()
|
||
|
if cpy != nil {
|
||
|
newNode[i] = cpy
|
||
|
}
|
||
|
}
|
||
|
|
||
|
newNode[key[0]] = t.InsertState(currentNode.Get(key[0]).Raw(), key[1:], value)
|
||
|
|
||
|
return t.Put(newNode)
|
||
|
}
|
||
|
|
||
|
return ""
|
||
|
}
|
||
|
|
||
11 years ago
|
func (t *Trie) DeleteState(node interface{}, key []int) interface{} {
|
||
|
if len(key) == 0 {
|
||
|
return ""
|
||
|
}
|
||
|
|
||
|
// New node
|
||
11 years ago
|
n := ethutil.NewValue(node)
|
||
11 years ago
|
if node == nil || (n.Type() == reflect.String && (n.Str() == "" || n.Get(0).IsNil())) || n.Len() == 0 {
|
||
|
return ""
|
||
|
}
|
||
|
|
||
|
currentNode := t.GetNode(node)
|
||
|
// 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)
|
||
|
if CompareIntSlice(k, key) {
|
||
|
return ""
|
||
|
} else if CompareIntSlice(key[:len(k)], k) {
|
||
|
hash := t.DeleteState(v, key[len(k):])
|
||
|
child := t.GetNode(hash)
|
||
|
|
||
|
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}
|
||
|
}
|
||
|
|
||
|
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
|
||
|
}
|
||
|
}
|
||
|
|
||
|
n[key[0]] = t.DeleteState(n[key[0]], key[1:])
|
||
|
amount := -1
|
||
|
for i := 0; i < 17; i++ {
|
||
|
if n[i] != "" {
|
||
|
if amount == -1 {
|
||
|
amount = i
|
||
|
} else {
|
||
|
amount = -2
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
if amount == 16 {
|
||
|
newNode = []interface{}{CompactEncode([]int{16}), n[amount]}
|
||
|
} else if amount >= 0 {
|
||
|
child := t.GetNode(n[amount])
|
||
|
if child.Len() == 17 {
|
||
|
newNode = []interface{}{CompactEncode([]int{amount}), n[amount]}
|
||
|
} else if child.Len() == 2 {
|
||
|
key := append([]int{amount}, CompactDecode(child.Get(0).Str())...)
|
||
|
newNode = []interface{}{CompactEncode(key), child.Get(1).Str()}
|
||
|
}
|
||
|
|
||
|
} else {
|
||
|
newNode = n
|
||
|
}
|
||
|
|
||
|
return t.Put(newNode)
|
||
|
}
|
||
|
|
||
|
return ""
|
||
|
}
|
||
|
|
||
11 years ago
|
// Simple compare function which creates a rlp value out of the evaluated objects
|
||
|
func (t *Trie) Cmp(trie *Trie) bool {
|
||
11 years ago
|
return ethutil.NewValue(t.Root).Cmp(ethutil.NewValue(trie.Root))
|
||
11 years ago
|
}
|
||
|
|
||
|
// Returns a copy of this trie
|
||
|
func (t *Trie) Copy() *Trie {
|
||
|
trie := NewTrie(t.cache.db, t.Root)
|
||
|
for key, node := range t.cache.nodes {
|
||
|
trie.cache.nodes[key] = node.Copy()
|
||
|
}
|
||
|
|
||
|
return trie
|
||
|
}
|
||
11 years ago
|
|
||
|
type TrieIterator struct {
|
||
|
trie *Trie
|
||
|
key string
|
||
|
value string
|
||
|
|
||
|
shas [][]byte
|
||
|
values []string
|
||
11 years ago
|
|
||
|
lastNode []byte
|
||
11 years ago
|
}
|
||
|
|
||
|
func (t *Trie) NewIterator() *TrieIterator {
|
||
|
return &TrieIterator{trie: t}
|
||
|
}
|
||
|
|
||
|
// Some time in the near future this will need refactoring :-)
|
||
|
// XXX Note to self, IsSlice == inline node. Str == sha3 to node
|
||
11 years ago
|
func (it *TrieIterator) workNode(currentNode *ethutil.Value) {
|
||
11 years ago
|
if currentNode.Len() == 2 {
|
||
|
k := CompactDecode(currentNode.Get(0).Str())
|
||
|
|
||
11 years ago
|
if currentNode.Get(1).Str() == "" {
|
||
11 years ago
|
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 {
|
||
11 years ago
|
if currentNode.Get(i).Str() == "" {
|
||
11 years ago
|
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
|
||
|
}
|
||
|
|
||
11 years ago
|
it.getNode(ethutil.NewValue(it.trie.Root).Bytes())
|
||
11 years ago
|
|
||
|
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 ""
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
type EachCallback func(key string, node *ethutil.Value)
|
||
11 years ago
|
|
||
|
func (it *TrieIterator) Each(cb EachCallback) {
|
||
11 years ago
|
it.fetchNode(nil, ethutil.NewValue(it.trie.Root).Bytes(), cb)
|
||
11 years ago
|
}
|
||
|
|
||
|
func (it *TrieIterator) fetchNode(key []int, node []byte, cb EachCallback) {
|
||
|
it.iterateNode(key, it.trie.cache.Get(node), cb)
|
||
|
}
|
||
|
|
||
11 years ago
|
func (it *TrieIterator) iterateNode(key []int, currentNode *ethutil.Value, cb EachCallback) {
|
||
11 years ago
|
if currentNode.Len() == 2 {
|
||
|
k := CompactDecode(currentNode.Get(0).Str())
|
||
|
|
||
|
if currentNode.Get(1).Str() == "" {
|
||
|
it.iterateNode(key, currentNode.Get(1), cb)
|
||
|
} else {
|
||
|
pk := append(key, k...)
|
||
|
|
||
|
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, i)
|
||
|
if i == 16 && currentNode.Get(i).Len() != 0 {
|
||
|
cb(DecodeCompact(pk), currentNode.Get(i))
|
||
|
} else {
|
||
|
if 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)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|