diff options
author | zelig <viktor.tron@gmail.com> | 2014-07-01 22:03:02 +0800 |
---|---|---|
committer | zelig <viktor.tron@gmail.com> | 2014-07-01 22:03:02 +0800 |
commit | 89630d2826300d119f2cdc9f8af6c94926f478a2 (patch) | |
tree | aa2addd061e2d2a8f3cdc0821260409f31ac3b09 /ethtrie/trie.go | |
parent | 12972b4b65a303dc3f9e135b0e2d97f8b7a661e2 (diff) | |
parent | 550407b0ec78b7026737d1abe28127da8c0c9063 (diff) | |
download | go-tangerine-89630d2826300d119f2cdc9f8af6c94926f478a2.tar go-tangerine-89630d2826300d119f2cdc9f8af6c94926f478a2.tar.gz go-tangerine-89630d2826300d119f2cdc9f8af6c94926f478a2.tar.bz2 go-tangerine-89630d2826300d119f2cdc9f8af6c94926f478a2.tar.lz go-tangerine-89630d2826300d119f2cdc9f8af6c94926f478a2.tar.xz go-tangerine-89630d2826300d119f2cdc9f8af6c94926f478a2.tar.zst go-tangerine-89630d2826300d119f2cdc9f8af6c94926f478a2.zip |
merge upstream
Diffstat (limited to 'ethtrie/trie.go')
-rw-r--r-- | ethtrie/trie.go | 51 |
1 files changed, 34 insertions, 17 deletions
diff --git a/ethtrie/trie.go b/ethtrie/trie.go index c957e9b4c..6a5d3807c 100644 --- a/ethtrie/trie.go +++ b/ethtrie/trie.go @@ -45,11 +45,11 @@ func NewCache(db ethutil.Database) *Cache { return &Cache{db: db, nodes: make(map[string]*Node)} } -func (cache *Cache) Put(v interface{}) interface{} { +func (cache *Cache) PutValue(v interface{}, force bool) interface{} { value := ethutil.NewValue(v) enc := value.Encode() - if len(enc) >= 32 { + if len(enc) >= 32 || force { sha := ethcrypto.Sha3Bin(enc) cache.nodes[string(sha)] = NewNode(sha, value, true) @@ -61,6 +61,10 @@ func (cache *Cache) Put(v interface{}) interface{} { return v } +func (cache *Cache) Put(v interface{}) interface{} { + return cache.PutValue(v, false) +} + func (cache *Cache) Get(key []byte) *ethutil.Value { // First check if the key is the cache if cache.nodes[string(key)] != nil { @@ -170,7 +174,15 @@ func (t *Trie) Update(key string, value string) { k := CompactHexDecode(key) - t.Root = t.UpdateState(t.Root, k, value) + root := t.UpdateState(t.Root, k, value) + switch root.(type) { + case string: + t.Root = root + case []byte: + t.Root = root + default: + t.Root = t.cache.PutValue(root, true) + } } func (t *Trie) Get(key string) string { @@ -184,7 +196,20 @@ func (t *Trie) Get(key string) string { } func (t *Trie) Delete(key string) { - t.Update(key, "") + t.mut.Lock() + defer t.mut.Unlock() + + k := CompactHexDecode(key) + + root := t.DeleteState(t.Root, k) + switch root.(type) { + case string: + t.Root = root + case []byte: + t.Root = root + default: + t.Root = t.cache.PutValue(root, true) + } } func (t *Trie) GetState(node interface{}, key []int) interface{} { @@ -236,15 +261,7 @@ func (t *Trie) GetNode(node interface{}) *ethutil.Value { } func (t *Trie) UpdateState(node interface{}, key []int, value string) interface{} { - - if value != "" { - return t.InsertState(node, key, value) - } else { - // delete it - return t.DeleteState(node, key) - } - - return t.Root + return t.InsertState(node, key, value) } func (t *Trie) Put(node interface{}) interface{} { @@ -532,10 +549,10 @@ func (it *TrieIterator) iterateNode(key []int, currentNode *ethutil.Value, cb Ea if currentNode.Len() == 2 { k := CompactDecode(currentNode.Get(0).Str()) - if currentNode.Get(1).Str() == "" { - it.iterateNode(key, currentNode.Get(1), cb) + pk := append(key, k...) + if currentNode.Get(1).Len() != 0 && currentNode.Get(1).Str() == "" { + it.iterateNode(pk, currentNode.Get(1), cb) } else { - pk := append(key, k...) if k[len(k)-1] == 16 { cb(DecodeCompact(pk), currentNode.Get(1)) @@ -549,7 +566,7 @@ func (it *TrieIterator) iterateNode(key []int, currentNode *ethutil.Value, cb Ea if i == 16 && currentNode.Get(i).Len() != 0 { cb(DecodeCompact(pk), currentNode.Get(i)) } else { - if currentNode.Get(i).Str() == "" { + if currentNode.Get(i).Len() != 0 && currentNode.Get(i).Str() == "" { it.iterateNode(pk, currentNode.Get(i), cb) } else { val := currentNode.Get(i).Str() |