merge upstream
This commit is contained in:
@@ -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()
|
||||
|
@@ -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()
|
||||
|
||||
data := [][]string{
|
||||
{"do", "verb"},
|
||||
{"ether", "wookiedoo"},
|
||||
{"horse", "stallion"},
|
||||
{"shaman", "horse"},
|
||||
{"doge", "coin"},
|
||||
{"ether", ""},
|
||||
{"dog", "puppy"},
|
||||
{"shaman", ""},
|
||||
func h(str string) string {
|
||||
d, err := hex.DecodeString(str)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
for _, item := range data {
|
||||
trie.Update(item[0], item[1])
|
||||
}
|
||||
|
||||
fmt.Printf("root %x", trie.Root)
|
||||
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{
|
||||
{"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]
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
if len(roots) > 1 {
|
||||
for root, num := range roots {
|
||||
t.Errorf("%x => %d\n", root, num)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user