aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorobscuren <geffobscura@gmail.com>2014-06-30 19:08:00 +0800
committerobscuren <geffobscura@gmail.com>2014-06-30 19:08:00 +0800
commit5a86892ecbd68c3d466cb1ef282c4cb81300abce (patch)
treede51bb2eeda7ae39de026491ff200173ebd454d0
parent8151858e70e19996d05928e4c63d36cd4847daa8 (diff)
downloadgo-tangerine-5a86892ecbd68c3d466cb1ef282c4cb81300abce.tar.gz
go-tangerine-5a86892ecbd68c3d466cb1ef282c4cb81300abce.tar.zst
go-tangerine-5a86892ecbd68c3d466cb1ef282c4cb81300abce.zip
Using remote for test cases
-rw-r--r--ethutil/trie.go17
-rw-r--r--ethutil/trie_test.go108
2 files changed, 108 insertions, 17 deletions
diff --git a/ethutil/trie.go b/ethutil/trie.go
index 18d0a5f0a..ce9c2da27 100644
--- a/ethutil/trie.go
+++ b/ethutil/trie.go
@@ -43,11 +43,11 @@ func NewCache(db 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 := NewValue(v)
enc := value.Encode()
- if len(enc) >= 32 {
+ if len(enc) >= 32 || force {
sha := Sha3Bin(enc)
cache.nodes[string(sha)] = NewNode(sha, value, true)
@@ -59,6 +59,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) *Value {
// First check if the key is the cache
if cache.nodes[string(key)] != nil {
@@ -168,7 +172,12 @@ 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)
+ if _, ok := root.([]byte); !ok {
+ t.Root = t.cache.PutValue(root, true)
+ } else {
+ t.Root = root
+ }
}
func (t *Trie) Get(key string) string {
@@ -527,6 +536,8 @@ func (it *TrieIterator) fetchNode(key []int, node []byte, cb EachCallback) {
}
func (it *TrieIterator) iterateNode(key []int, currentNode *Value, cb EachCallback) {
+ //fmt.Println("node", currentNode)
+
if currentNode.Len() == 2 {
k := CompactDecode(currentNode.Get(0).Str())
diff --git a/ethutil/trie_test.go b/ethutil/trie_test.go
index 2937b1525..3ee955b11 100644
--- a/ethutil/trie_test.go
+++ b/ethutil/trie_test.go
@@ -1,7 +1,12 @@
package ethutil
import (
+ "bytes"
+ "encoding/hex"
+ "encoding/json"
"fmt"
+ "io/ioutil"
+ "net/http"
"reflect"
"testing"
)
@@ -171,23 +176,98 @@ 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)
+}
- data := [][]string{
- {"do", "verb"},
- {"ether", "wookiedoo"},
- {"horse", "stallion"},
- {"shaman", "horse"},
- {"doge", "coin"},
- {"ether", ""},
- {"dog", "puppy"},
- {"shaman", ""},
+func get(in string) (out string) {
+ if len(in) > 2 && in[:2] == "0x" {
+ out = h(in[2:])
+ } else {
+ out = in
}
- for _, item := range data {
- trie.Update(item[0], item[1])
+ 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)
}
- fmt.Printf("root %x", trie.Root)
+ return t, nil
+}
+
+func CreateTests(uri string, cb func(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)
+ }
+
+ for name, testData := range objmap {
+ test, err := CreateTest(name, *testData)
+ if err != nil {
+ panic(err)
+ }
+
+ cb(test)
+ }
+}
+
+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))
+ }
+ fmt.Printf("%-15s: %x\n", test.Name, trie.Root)
+
+ 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, string(v.Str()))
+ })
+
+ a := NewValue(trie.Root).Bytes()
+ b := NewValue(trie2.Root).Bytes()
+ if bytes.Compare(a, b) != 0 {
+ t.Errorf("root %x %x\n", trie.Root, trie2.Root)
+ }
+ })
}