|
|
|
@ -5,8 +5,8 @@ import ( |
|
|
|
|
"fmt" |
|
|
|
|
"testing" |
|
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/crypto" |
|
|
|
|
"github.com/ethereum/go-ethereum/common" |
|
|
|
|
"github.com/ethereum/go-ethereum/crypto" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
type Db map[string][]byte |
|
|
|
@ -16,18 +16,18 @@ func (self Db) Put(k, v []byte) { self[string(k)] = v } |
|
|
|
|
|
|
|
|
|
// Used for testing
|
|
|
|
|
func NewEmpty() *Trie { |
|
|
|
|
return New(nil, make(Db)) |
|
|
|
|
return New(common.Hash{}, make(Db)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func NewEmptySecure() *SecureTrie { |
|
|
|
|
return NewSecure(nil, make(Db)) |
|
|
|
|
return NewSecure(common.Hash{}, make(Db)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestEmptyTrie(t *testing.T) { |
|
|
|
|
trie := NewEmpty() |
|
|
|
|
res := trie.Hash() |
|
|
|
|
exp := crypto.Sha3(common.Encode("")) |
|
|
|
|
if !bytes.Equal(res, exp) { |
|
|
|
|
if !bytes.Equal(res[:], exp[:]) { |
|
|
|
|
t.Errorf("expected %x got %x", exp, res) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -41,7 +41,7 @@ func TestInsert(t *testing.T) { |
|
|
|
|
|
|
|
|
|
exp := common.Hex2Bytes("8aad789dff2f538bca5d8ea56e8abe10f4c7ba3a5dea95fea4cd6e7c3a1168d3") |
|
|
|
|
root := trie.Hash() |
|
|
|
|
if !bytes.Equal(root, exp) { |
|
|
|
|
if !bytes.Equal(root[:], exp[:]) { |
|
|
|
|
t.Errorf("exp %x got %x", exp, root) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -50,7 +50,7 @@ func TestInsert(t *testing.T) { |
|
|
|
|
|
|
|
|
|
exp = common.Hex2Bytes("d23786fb4a010da3ce639d66d5e904a11dbc02746d1ce25029e53290cabf28ab") |
|
|
|
|
root = trie.Hash() |
|
|
|
|
if !bytes.Equal(root, exp) { |
|
|
|
|
if !bytes.Equal(root[:], exp) { |
|
|
|
|
t.Errorf("exp %x got %x", exp, root) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -96,7 +96,7 @@ func TestDelete(t *testing.T) { |
|
|
|
|
|
|
|
|
|
hash := trie.Hash() |
|
|
|
|
exp := common.Hex2Bytes("5991bb8c6514148a29db676a14ac506cd2cd5775ace63c30a4fe457715e9ac84") |
|
|
|
|
if !bytes.Equal(hash, exp) { |
|
|
|
|
if !bytes.Equal(hash[:], exp) { |
|
|
|
|
t.Errorf("expected %x got %x", exp, hash) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -120,7 +120,7 @@ func TestEmptyValues(t *testing.T) { |
|
|
|
|
|
|
|
|
|
hash := trie.Hash() |
|
|
|
|
exp := common.Hex2Bytes("5991bb8c6514148a29db676a14ac506cd2cd5775ace63c30a4fe457715e9ac84") |
|
|
|
|
if !bytes.Equal(hash, exp) { |
|
|
|
|
if !bytes.Equal(hash[:], exp) { |
|
|
|
|
t.Errorf("expected %x got %x", exp, hash) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -150,7 +150,7 @@ func TestReplication(t *testing.T) { |
|
|
|
|
|
|
|
|
|
hash := trie2.Hash() |
|
|
|
|
exp := trie.Hash() |
|
|
|
|
if !bytes.Equal(hash, exp) { |
|
|
|
|
if !bytes.Equal(hash[:], exp[:]) { |
|
|
|
|
t.Errorf("root failure. expected %x got %x", exp, hash) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -168,7 +168,9 @@ func TestReset(t *testing.T) { |
|
|
|
|
} |
|
|
|
|
trie.Commit() |
|
|
|
|
|
|
|
|
|
before := common.CopyBytes(trie.roothash) |
|
|
|
|
var before common.Hash |
|
|
|
|
before.Set(trie.roothash) |
|
|
|
|
|
|
|
|
|
trie.UpdateString("should", "revert") |
|
|
|
|
trie.Hash() |
|
|
|
|
// Should have no effect
|
|
|
|
@ -177,9 +179,11 @@ func TestReset(t *testing.T) { |
|
|
|
|
// ###
|
|
|
|
|
|
|
|
|
|
trie.Reset() |
|
|
|
|
after := common.CopyBytes(trie.roothash) |
|
|
|
|
|
|
|
|
|
if !bytes.Equal(before, after) { |
|
|
|
|
var after common.Hash |
|
|
|
|
after.Set(trie.roothash) |
|
|
|
|
|
|
|
|
|
if before != after { |
|
|
|
|
t.Errorf("expected roots to be equal. %x - %x", before, after) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -248,7 +252,7 @@ func BenchmarkGets(b *testing.B) { |
|
|
|
|
|
|
|
|
|
b.ResetTimer() |
|
|
|
|
for i := 0; i < b.N; i++ { |
|
|
|
|
trie.Get([]byte("horse")) |
|
|
|
|
trie.GetString("horse") |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -263,8 +267,9 @@ func BenchmarkUpdate(b *testing.B) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
type kv struct { |
|
|
|
|
k, v []byte |
|
|
|
|
t bool |
|
|
|
|
k common.Hash |
|
|
|
|
v []byte |
|
|
|
|
t bool |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestLargeData(t *testing.T) { |
|
|
|
@ -272,17 +277,21 @@ func TestLargeData(t *testing.T) { |
|
|
|
|
vals := make(map[string]*kv) |
|
|
|
|
|
|
|
|
|
for i := byte(0); i < 255; i++ { |
|
|
|
|
value := &kv{common.LeftPadBytes([]byte{i}, 32), []byte{i}, false} |
|
|
|
|
value2 := &kv{common.LeftPadBytes([]byte{10, i}, 32), []byte{i}, false} |
|
|
|
|
var k1 common.Hash |
|
|
|
|
k1.SetBytes([]byte{i}) |
|
|
|
|
var k2 common.Hash |
|
|
|
|
k2.SetBytes([]byte{10, i}) |
|
|
|
|
value := &kv{k1, []byte{i}, false} |
|
|
|
|
value2 := &kv{k2, []byte{i}, false} |
|
|
|
|
trie.Update(value.k, value.v) |
|
|
|
|
trie.Update(value2.k, value2.v) |
|
|
|
|
vals[string(value.k)] = value |
|
|
|
|
vals[string(value2.k)] = value2 |
|
|
|
|
vals[value.k.Str()] = value |
|
|
|
|
vals[value2.k.Str()] = value2 |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
it := trie.Iterator() |
|
|
|
|
for it.Next() { |
|
|
|
|
vals[string(it.Key)].t = true |
|
|
|
|
vals[it.Key.Str()].t = true |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
var untouched []*kv |
|
|
|
@ -323,7 +332,7 @@ func TestSecureDelete(t *testing.T) { |
|
|
|
|
|
|
|
|
|
hash := trie.Hash() |
|
|
|
|
exp := common.Hex2Bytes("29b235a58c3c25ab83010c327d5932bcf05324b7d6b1185e650798034783ca9d") |
|
|
|
|
if !bytes.Equal(hash, exp) { |
|
|
|
|
if !bytes.Equal(hash[:], exp) { |
|
|
|
|
t.Errorf("expected %x got %x", exp, hash) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|