aboutsummaryrefslogtreecommitdiffstats
path: root/ethtrie
diff options
context:
space:
mode:
authorzelig <viktor.tron@gmail.com>2014-07-01 22:03:02 +0800
committerzelig <viktor.tron@gmail.com>2014-07-01 22:03:02 +0800
commit89630d2826300d119f2cdc9f8af6c94926f478a2 (patch)
treeaa2addd061e2d2a8f3cdc0821260409f31ac3b09 /ethtrie
parent12972b4b65a303dc3f9e135b0e2d97f8b7a661e2 (diff)
parent550407b0ec78b7026737d1abe28127da8c0c9063 (diff)
downloaddexon-89630d2826300d119f2cdc9f8af6c94926f478a2.tar
dexon-89630d2826300d119f2cdc9f8af6c94926f478a2.tar.gz
dexon-89630d2826300d119f2cdc9f8af6c94926f478a2.tar.bz2
dexon-89630d2826300d119f2cdc9f8af6c94926f478a2.tar.lz
dexon-89630d2826300d119f2cdc9f8af6c94926f478a2.tar.xz
dexon-89630d2826300d119f2cdc9f8af6c94926f478a2.tar.zst
dexon-89630d2826300d119f2cdc9f8af6c94926f478a2.zip
merge upstream
Diffstat (limited to 'ethtrie')
-rw-r--r--ethtrie/trie.go51
-rw-r--r--ethtrie/trie_test.go164
2 files changed, 185 insertions, 30 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()
diff --git a/ethtrie/trie_test.go b/ethtrie/trie_test.go
index 284b189cb..a3d4547d7 100644
--- a/ethtrie/trie_test.go
+++ b/ethtrie/trie_test.go
@@ -1,9 +1,16 @@
package ethtrie
import (
+ "bytes"
+ "encoding/hex"
+ "encoding/json"
"fmt"
+ "io/ioutil"
+ "math/rand"
+ "net/http"
"reflect"
"testing"
+ "time"
)
const LONG_WORD = "1234567890abcdefghijklmnopqrstuvwxxzABCEFGHIJKLMNOPQRSTUVWXYZ"
@@ -171,23 +178,154 @@ func TestTriePurge(t *testing.T) {
}
}
-func TestTrieIt(t *testing.T) {
- _, trie := New()
+func h(str string) string {
+ d, err := hex.DecodeString(str)
+ if err != nil {
+ panic(err)
+ }
+
+ return string(d)
+}
+
+func get(in string) (out string) {
+ if len(in) > 2 && in[:2] == "0x" {
+ out = h(in[2:])
+ } else {
+ out = in
+ }
+
+ return
+}
+
+type Test struct {
+ Name string
+ In map[string]string
+ Root string
+}
+
+func CreateTest(name string, data []byte) (Test, error) {
+ t := Test{Name: name}
+ err := json.Unmarshal(data, &t)
+ if err != nil {
+ return Test{}, fmt.Errorf("%v", err)
+ }
+
+ return t, nil
+}
+
+func CreateTests(uri string, cb func(Test)) map[string]Test {
+ resp, err := http.Get(uri)
+ if err != nil {
+ panic(err)
+ }
+ defer resp.Body.Close()
+
+ data, err := ioutil.ReadAll(resp.Body)
+
+ var objmap map[string]*json.RawMessage
+ err = json.Unmarshal(data, &objmap)
+ if err != nil {
+ panic(err)
+ }
+ tests := make(map[string]Test)
+ for name, testData := range objmap {
+ test, err := CreateTest(name, *testData)
+ if err != nil {
+ panic(err)
+ }
+
+ if cb != nil {
+ cb(test)
+ }
+ tests[name] = test
+ }
+
+ return tests
+}
+
+func TestRemote(t *testing.T) {
+ CreateTests("https://raw.githubusercontent.com/ethereum/tests/develop/trietest.json", func(test Test) {
+ _, trie := New()
+ for key, value := range test.In {
+ trie.Update(get(key), get(value))
+ }
+
+ a := NewValue(h(test.Root)).Bytes()
+ b := NewValue(trie.Root).Bytes()
+ if bytes.Compare(a, b) != 0 {
+ t.Errorf("%-10s: %x %x", test.Name, a, b)
+ }
+ })
+}
+
+func TestTrieReplay(t *testing.T) {
+ CreateTests("https://raw.githubusercontent.com/ethereum/tests/develop/trietest.json", func(test Test) {
+ _, trie := New()
+ for key, value := range test.In {
+ trie.Update(get(key), get(value))
+ }
+
+ _, trie2 := New()
+ trie.NewIterator().Each(func(key string, v *Value) {
+ trie2.Update(key, v.Str())
+ })
+
+ a := NewValue(trie.Root).Bytes()
+ b := NewValue(trie2.Root).Bytes()
+ if bytes.Compare(a, b) != 0 {
+ t.Errorf("%s %x %x\n", test.Name, trie.Root, trie2.Root)
+ }
+ })
+}
+
+func RandomData() [][]string {
data := [][]string{
- {"do", "verb"},
- {"ether", "wookiedoo"},
- {"horse", "stallion"},
- {"shaman", "horse"},
- {"doge", "coin"},
- {"ether", ""},
- {"dog", "puppy"},
- {"shaman", ""},
+ {"0x000000000000000000000000ec4f34c97e43fbb2816cfd95e388353c7181dab1", "0x4e616d6552656700000000000000000000000000000000000000000000000000"},
+ {"0x0000000000000000000000000000000000000000000000000000000000000045", "0x22b224a1420a802ab51d326e29fa98e34c4f24ea"},
+ {"0x0000000000000000000000000000000000000000000000000000000000000046", "0x67706c2076330000000000000000000000000000000000000000000000000000"},
+ {"0x000000000000000000000000697c7b8c961b56f675d570498424ac8de1a918f6", "0x6f6f6f6820736f2067726561742c207265616c6c6c793f000000000000000000"},
+ {"0x0000000000000000000000007ef9e639e2733cb34e4dfc576d4b23f72db776b2", "0x4655474156000000000000000000000000000000000000000000000000000000"},
+ {"0x6f6f6f6820736f2067726561742c207265616c6c6c793f000000000000000000", "0x697c7b8c961b56f675d570498424ac8de1a918f6"},
+ {"0x4655474156000000000000000000000000000000000000000000000000000000", "0x7ef9e639e2733cb34e4dfc576d4b23f72db776b2"},
+ {"0x4e616d6552656700000000000000000000000000000000000000000000000000", "0xec4f34c97e43fbb2816cfd95e388353c7181dab1"},
+ }
+
+ var c [][]string
+ for len(data) != 0 {
+ e := rand.Intn(len(data))
+ c = append(c, data[e])
+
+ copy(data[e:], data[e+1:])
+ data[len(data)-1] = nil
+ data = data[:len(data)-1]
}
- for _, item := range data {
- trie.Update(item[0], item[1])
+ return c
+}
+
+const MaxTest = 1000
+
+// This test insert data in random order and seeks to find indifferences between the different tries
+func TestRegression(t *testing.T) {
+ rand.Seed(time.Now().Unix())
+
+ roots := make(map[string]int)
+ for i := 0; i < MaxTest; i++ {
+ _, trie := New()
+ data := RandomData()
+
+ for _, test := range data {
+ trie.Update(test[0], test[1])
+ }
+ trie.Delete("0x4e616d6552656700000000000000000000000000000000000000000000000000")
+
+ roots[string(trie.Root.([]byte))] += 1
}
- fmt.Printf("root %x", trie.Root)
+ if len(roots) > 1 {
+ for root, num := range roots {
+ t.Errorf("%x => %d\n", root, num)
+ }
+ }
}