aboutsummaryrefslogtreecommitdiffstats
path: root/ethtrie/trie_test.go
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/trie_test.go
parent12972b4b65a303dc3f9e135b0e2d97f8b7a661e2 (diff)
parent550407b0ec78b7026737d1abe28127da8c0c9063 (diff)
downloaddexon-89630d2826300d119f2cdc9f8af6c94926f478a2.tar.gz
dexon-89630d2826300d119f2cdc9f8af6c94926f478a2.tar.zst
dexon-89630d2826300d119f2cdc9f8af6c94926f478a2.zip
merge upstream
Diffstat (limited to 'ethtrie/trie_test.go')
-rw-r--r--ethtrie/trie_test.go164
1 files changed, 151 insertions, 13 deletions
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)
+ }
+ }
}