From 707d413761927f5ad95298e666e297b820ad0901 Mon Sep 17 00:00:00 2001 From: zelig Date: Sun, 29 Jun 2014 16:26:58 +0100 Subject: [PATCH] refactor ethutil/trie to ethtrie --- ethchain/state.go | 5 +-- {ethutil => ethtrie}/encoding.go | 2 +- {ethutil => ethtrie}/encoding_test.go | 2 +- {ethutil => ethtrie}/slice.go | 2 +- {ethutil => ethtrie}/trie.go | 50 ++++++++++++++------------- {ethutil => ethtrie}/trie_test.go | 2 +- 6 files changed, 33 insertions(+), 30 deletions(-) rename {ethutil => ethtrie}/encoding.go (98%) rename {ethutil => ethtrie}/encoding_test.go (99%) rename {ethutil => ethtrie}/slice.go (96%) rename {ethutil => ethtrie}/trie.go (90%) rename {ethutil => ethtrie}/trie_test.go (99%) diff --git a/ethchain/state.go b/ethchain/state.go index e28b919099..4c66a973e5 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 similarity index 98% rename from ethutil/encoding.go rename to ethtrie/encoding.go index 9fcdf3edfc..c9c3911104 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 similarity index 99% rename from ethutil/encoding_test.go rename to ethtrie/encoding_test.go index 10e1995c02..7a48496788 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 similarity index 96% rename from ethutil/slice.go rename to ethtrie/slice.go index 3cedcb1894..b9d5d12853 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 similarity index 90% rename from ethutil/trie.go rename to ethtrie/trie.go index 18d0a5f0ae..c957e9b4cb 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 similarity index 99% rename from ethutil/trie_test.go rename to ethtrie/trie_test.go index 2937b15250..284b189cba 100644 --- a/ethutil/trie_test.go +++ b/ethtrie/trie_test.go @@ -1,4 +1,4 @@ -package ethutil +package ethtrie import ( "fmt"