aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorzelig <viktor.tron@gmail.com>2014-06-29 23:26:58 +0800
committerzelig <viktor.tron@gmail.com>2014-06-29 23:26:58 +0800
commit707d413761927f5ad95298e666e297b820ad0901 (patch)
tree347ffb4f9e1c6694cf1d2b1d5892e297c98d2e94
parent4be3521727698141512eb6454121302bd3b9cc6d (diff)
downloaddexon-707d413761927f5ad95298e666e297b820ad0901.tar.gz
dexon-707d413761927f5ad95298e666e297b820ad0901.tar.zst
dexon-707d413761927f5ad95298e666e297b820ad0901.zip
refactor ethutil/trie to ethtrie
-rw-r--r--ethchain/state.go5
-rw-r--r--ethtrie/encoding.go (renamed from ethutil/encoding.go)2
-rw-r--r--ethtrie/encoding_test.go (renamed from ethutil/encoding_test.go)2
-rw-r--r--ethtrie/slice.go (renamed from ethutil/slice.go)2
-rw-r--r--ethtrie/trie.go (renamed from ethutil/trie.go)50
-rw-r--r--ethtrie/trie_test.go (renamed from ethutil/trie_test.go)2
6 files changed, 33 insertions, 30 deletions
diff --git a/ethchain/state.go b/ethchain/state.go
index e28b91909..4c66a973e 100644
--- a/ethchain/state.go
+++ b/ethchain/state.go
@@ -1,6 +1,7 @@
package ethchain
import (
+ "github.com/ethereum/eth-go/ethtrie"
"github.com/ethereum/eth-go/ethutil"
"math/big"
)
@@ -12,7 +13,7 @@ import (
// * Accounts
type State struct {
// The trie for this structure
- trie *ethutil.Trie
+ trie *ethtrie.Trie
stateObjects map[string]*StateObject
@@ -20,7 +21,7 @@ type State struct {
}
// Create a new state from a given trie
-func NewState(trie *ethutil.Trie) *State {
+func NewState(trie *ethtrie.Trie) *State {
return &State{trie: trie, stateObjects: make(map[string]*StateObject), manifest: NewManifest()}
}
diff --git a/ethutil/encoding.go b/ethtrie/encoding.go
index 9fcdf3edf..c9c391110 100644
--- a/ethutil/encoding.go
+++ b/ethtrie/encoding.go
@@ -1,4 +1,4 @@
-package ethutil
+package ethtrie
import (
"bytes"
diff --git a/ethutil/encoding_test.go b/ethtrie/encoding_test.go
index 10e1995c0..7a4849678 100644
--- a/ethutil/encoding_test.go
+++ b/ethtrie/encoding_test.go
@@ -1,4 +1,4 @@
-package ethutil
+package ethtrie
import (
"fmt"
diff --git a/ethutil/slice.go b/ethtrie/slice.go
index 3cedcb189..b9d5d1285 100644
--- a/ethutil/slice.go
+++ b/ethtrie/slice.go
@@ -1,4 +1,4 @@
-package ethutil
+package ethtrie
import ()
diff --git a/ethutil/trie.go b/ethtrie/trie.go
index 18d0a5f0a..c957e9b4c 100644
--- a/ethutil/trie.go
+++ b/ethtrie/trie.go
@@ -1,7 +1,9 @@
-package ethutil
+package ethtrie
import (
"fmt"
+ "github.com/ethereum/eth-go/ethcrypto"
+ "github.com/ethereum/eth-go/ethutil"
"reflect"
"sync"
)
@@ -21,11 +23,11 @@ type StateObject interface {
type Node struct {
Key []byte
- Value *Value
+ Value *ethutil.Value
Dirty bool
}
-func NewNode(key []byte, val *Value, dirty bool) *Node {
+func NewNode(key []byte, val *ethutil.Value, dirty bool) *Node {
return &Node{Key: key, Value: val, Dirty: dirty}
}
@@ -35,20 +37,20 @@ func (n *Node) Copy() *Node {
type Cache struct {
nodes map[string]*Node
- db Database
+ db ethutil.Database
IsDirty bool
}
-func NewCache(db Database) *Cache {
+func NewCache(db ethutil.Database) *Cache {
return &Cache{db: db, nodes: make(map[string]*Node)}
}
func (cache *Cache) Put(v interface{}) interface{} {
- value := NewValue(v)
+ value := ethutil.NewValue(v)
enc := value.Encode()
if len(enc) >= 32 {
- sha := Sha3Bin(enc)
+ sha := ethcrypto.Sha3Bin(enc)
cache.nodes[string(sha)] = NewNode(sha, value, true)
cache.IsDirty = true
@@ -59,7 +61,7 @@ func (cache *Cache) Put(v interface{}) interface{} {
return v
}
-func (cache *Cache) Get(key []byte) *Value {
+func (cache *Cache) Get(key []byte) *ethutil.Value {
// First check if the key is the cache
if cache.nodes[string(key)] != nil {
return cache.nodes[string(key)].Value
@@ -68,7 +70,7 @@ func (cache *Cache) Get(key []byte) *Value {
// Get the key of the database instead and cache it
data, _ := cache.db.Get(key)
// Create the cached value
- value := NewValueFromBytes(data)
+ value := ethutil.NewValueFromBytes(data)
// Create caching node
cache.nodes[string(key)] = NewNode(key, value, false)
@@ -128,7 +130,7 @@ type Trie struct {
func copyRoot(root interface{}) interface{} {
var prevRootCopy interface{}
if b, ok := root.([]byte); ok {
- prevRootCopy = CopyBytes(b)
+ prevRootCopy = ethutil.CopyBytes(b)
} else {
prevRootCopy = root
}
@@ -136,7 +138,7 @@ func copyRoot(root interface{}) interface{} {
return prevRootCopy
}
-func NewTrie(db Database, Root interface{}) *Trie {
+func NewTrie(db ethutil.Database, Root interface{}) *Trie {
// Make absolute sure the root is copied
r := copyRoot(Root)
p := copyRoot(Root)
@@ -176,7 +178,7 @@ func (t *Trie) Get(key string) string {
defer t.mut.RUnlock()
k := CompactHexDecode(key)
- c := NewValue(t.GetState(t.Root, k))
+ c := ethutil.NewValue(t.GetState(t.Root, k))
return c.Str()
}
@@ -186,7 +188,7 @@ func (t *Trie) Delete(key string) {
}
func (t *Trie) GetState(node interface{}, key []int) interface{} {
- n := NewValue(node)
+ n := ethutil.NewValue(node)
// Return the node if key is empty (= found)
if len(key) == 0 || n.IsNil() || n.Len() == 0 {
return node
@@ -216,8 +218,8 @@ func (t *Trie) GetState(node interface{}, key []int) interface{} {
return ""
}
-func (t *Trie) GetNode(node interface{}) *Value {
- n := NewValue(node)
+func (t *Trie) GetNode(node interface{}) *ethutil.Value {
+ n := ethutil.NewValue(node)
if !n.Get(0).IsNil() {
return n
@@ -227,7 +229,7 @@ func (t *Trie) GetNode(node interface{}) *Value {
if len(str) == 0 {
return n
} else if len(str) < 32 {
- return NewValueFromBytes([]byte(str))
+ return ethutil.NewValueFromBytes([]byte(str))
}
return t.cache.Get(n.Bytes())
@@ -273,7 +275,7 @@ func (t *Trie) InsertState(node interface{}, key []int, value interface{}) inter
}
// New node
- n := NewValue(node)
+ n := ethutil.NewValue(node)
if node == nil || (n.Type() == reflect.String && (n.Str() == "" || n.Get(0).IsNil())) || n.Len() == 0 {
newNode := []interface{}{CompactEncode(key), value}
@@ -345,7 +347,7 @@ func (t *Trie) DeleteState(node interface{}, key []int) interface{} {
}
// New node
- n := NewValue(node)
+ n := ethutil.NewValue(node)
if node == nil || (n.Type() == reflect.String && (n.Str() == "" || n.Get(0).IsNil())) || n.Len() == 0 {
return ""
}
@@ -422,7 +424,7 @@ func (t *Trie) DeleteState(node interface{}, key []int) interface{} {
// Simple compare function which creates a rlp value out of the evaluated objects
func (t *Trie) Cmp(trie *Trie) bool {
- return NewValue(t.Root).Cmp(NewValue(trie.Root))
+ return ethutil.NewValue(t.Root).Cmp(ethutil.NewValue(trie.Root))
}
// Returns a copy of this trie
@@ -452,7 +454,7 @@ func (t *Trie) NewIterator() *TrieIterator {
// Some time in the near future this will need refactoring :-)
// XXX Note to self, IsSlice == inline node. Str == sha3 to node
-func (it *TrieIterator) workNode(currentNode *Value) {
+func (it *TrieIterator) workNode(currentNode *ethutil.Value) {
if currentNode.Len() == 2 {
k := CompactDecode(currentNode.Get(0).Str())
@@ -495,7 +497,7 @@ func (it *TrieIterator) Collect() [][]byte {
return nil
}
- it.getNode(NewValue(it.trie.Root).Bytes())
+ it.getNode(ethutil.NewValue(it.trie.Root).Bytes())
return it.shas
}
@@ -516,17 +518,17 @@ func (it *TrieIterator) Value() string {
return ""
}
-type EachCallback func(key string, node *Value)
+type EachCallback func(key string, node *ethutil.Value)
func (it *TrieIterator) Each(cb EachCallback) {
- it.fetchNode(nil, NewValue(it.trie.Root).Bytes(), cb)
+ it.fetchNode(nil, ethutil.NewValue(it.trie.Root).Bytes(), cb)
}
func (it *TrieIterator) fetchNode(key []int, node []byte, cb EachCallback) {
it.iterateNode(key, it.trie.cache.Get(node), cb)
}
-func (it *TrieIterator) iterateNode(key []int, currentNode *Value, cb EachCallback) {
+func (it *TrieIterator) iterateNode(key []int, currentNode *ethutil.Value, cb EachCallback) {
if currentNode.Len() == 2 {
k := CompactDecode(currentNode.Get(0).Str())
diff --git a/ethutil/trie_test.go b/ethtrie/trie_test.go
index 2937b1525..284b189cb 100644
--- a/ethutil/trie_test.go
+++ b/ethtrie/trie_test.go
@@ -1,4 +1,4 @@
-package ethutil
+package ethtrie
import (
"fmt"