|
|
@ -24,7 +24,6 @@ import ( |
|
|
|
"hash" |
|
|
|
"hash" |
|
|
|
"math/big" |
|
|
|
"math/big" |
|
|
|
"math/rand" |
|
|
|
"math/rand" |
|
|
|
"os" |
|
|
|
|
|
|
|
"reflect" |
|
|
|
"reflect" |
|
|
|
"testing" |
|
|
|
"testing" |
|
|
|
"testing/quick" |
|
|
|
"testing/quick" |
|
|
@ -35,7 +34,6 @@ import ( |
|
|
|
"github.com/ethereum/go-ethereum/core/types" |
|
|
|
"github.com/ethereum/go-ethereum/core/types" |
|
|
|
"github.com/ethereum/go-ethereum/crypto" |
|
|
|
"github.com/ethereum/go-ethereum/crypto" |
|
|
|
"github.com/ethereum/go-ethereum/ethdb" |
|
|
|
"github.com/ethereum/go-ethereum/ethdb" |
|
|
|
"github.com/ethereum/go-ethereum/ethdb/leveldb" |
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/ethdb/memorydb" |
|
|
|
"github.com/ethereum/go-ethereum/ethdb/memorydb" |
|
|
|
"github.com/ethereum/go-ethereum/rlp" |
|
|
|
"github.com/ethereum/go-ethereum/rlp" |
|
|
|
"golang.org/x/crypto/sha3" |
|
|
|
"golang.org/x/crypto/sha3" |
|
|
@ -46,12 +44,6 @@ func init() { |
|
|
|
spew.Config.DisableMethods = false |
|
|
|
spew.Config.DisableMethods = false |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Used for testing
|
|
|
|
|
|
|
|
func newEmpty() *Trie { |
|
|
|
|
|
|
|
trie := NewEmpty(NewDatabase(memorydb.New())) |
|
|
|
|
|
|
|
return trie |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func TestEmptyTrie(t *testing.T) { |
|
|
|
func TestEmptyTrie(t *testing.T) { |
|
|
|
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase())) |
|
|
|
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase())) |
|
|
|
res := trie.Hash() |
|
|
|
res := trie.Hash() |
|
|
@ -91,7 +83,8 @@ func testMissingNode(t *testing.T, memonly bool) { |
|
|
|
trie := NewEmpty(triedb) |
|
|
|
trie := NewEmpty(triedb) |
|
|
|
updateString(trie, "120000", "qwerqwerqwerqwerqwerqwerqwerqwer") |
|
|
|
updateString(trie, "120000", "qwerqwerqwerqwerqwerqwerqwerqwer") |
|
|
|
updateString(trie, "123456", "asdfasdfasdfasdfasdfasdfasdfasdf") |
|
|
|
updateString(trie, "123456", "asdfasdfasdfasdfasdfasdfasdfasdf") |
|
|
|
root, _, _ := trie.Commit(nil) |
|
|
|
root, nodes, _ := trie.Commit(false) |
|
|
|
|
|
|
|
triedb.Update(NewWithNodeSet(nodes)) |
|
|
|
if !memonly { |
|
|
|
if !memonly { |
|
|
|
triedb.Commit(root, true, nil) |
|
|
|
triedb.Commit(root, true, nil) |
|
|
|
} |
|
|
|
} |
|
|
@ -157,7 +150,7 @@ func testMissingNode(t *testing.T, memonly bool) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func TestInsert(t *testing.T) { |
|
|
|
func TestInsert(t *testing.T) { |
|
|
|
trie := newEmpty() |
|
|
|
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase())) |
|
|
|
|
|
|
|
|
|
|
|
updateString(trie, "doe", "reindeer") |
|
|
|
updateString(trie, "doe", "reindeer") |
|
|
|
updateString(trie, "dog", "puppy") |
|
|
|
updateString(trie, "dog", "puppy") |
|
|
@ -169,11 +162,11 @@ func TestInsert(t *testing.T) { |
|
|
|
t.Errorf("case 1: exp %x got %x", exp, root) |
|
|
|
t.Errorf("case 1: exp %x got %x", exp, root) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
trie = newEmpty() |
|
|
|
trie = NewEmpty(NewDatabase(rawdb.NewMemoryDatabase())) |
|
|
|
updateString(trie, "A", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") |
|
|
|
updateString(trie, "A", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") |
|
|
|
|
|
|
|
|
|
|
|
exp = common.HexToHash("d23786fb4a010da3ce639d66d5e904a11dbc02746d1ce25029e53290cabf28ab") |
|
|
|
exp = common.HexToHash("d23786fb4a010da3ce639d66d5e904a11dbc02746d1ce25029e53290cabf28ab") |
|
|
|
root, _, err := trie.Commit(nil) |
|
|
|
root, _, err := trie.Commit(false) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
t.Fatalf("commit error: %v", err) |
|
|
|
t.Fatalf("commit error: %v", err) |
|
|
|
} |
|
|
|
} |
|
|
@ -183,7 +176,8 @@ func TestInsert(t *testing.T) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func TestGet(t *testing.T) { |
|
|
|
func TestGet(t *testing.T) { |
|
|
|
trie := newEmpty() |
|
|
|
db := NewDatabase(rawdb.NewMemoryDatabase()) |
|
|
|
|
|
|
|
trie := NewEmpty(db) |
|
|
|
updateString(trie, "doe", "reindeer") |
|
|
|
updateString(trie, "doe", "reindeer") |
|
|
|
updateString(trie, "dog", "puppy") |
|
|
|
updateString(trie, "dog", "puppy") |
|
|
|
updateString(trie, "dogglesworth", "cat") |
|
|
|
updateString(trie, "dogglesworth", "cat") |
|
|
@ -193,21 +187,21 @@ func TestGet(t *testing.T) { |
|
|
|
if !bytes.Equal(res, []byte("puppy")) { |
|
|
|
if !bytes.Equal(res, []byte("puppy")) { |
|
|
|
t.Errorf("expected puppy got %x", res) |
|
|
|
t.Errorf("expected puppy got %x", res) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
unknown := getString(trie, "unknown") |
|
|
|
unknown := getString(trie, "unknown") |
|
|
|
if unknown != nil { |
|
|
|
if unknown != nil { |
|
|
|
t.Errorf("expected nil got %x", unknown) |
|
|
|
t.Errorf("expected nil got %x", unknown) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if i == 1 { |
|
|
|
if i == 1 { |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
trie.Commit(nil) |
|
|
|
root, nodes, _ := trie.Commit(false) |
|
|
|
|
|
|
|
db.Update(NewWithNodeSet(nodes)) |
|
|
|
|
|
|
|
trie, _ = New(common.Hash{}, root, db) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func TestDelete(t *testing.T) { |
|
|
|
func TestDelete(t *testing.T) { |
|
|
|
trie := newEmpty() |
|
|
|
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase())) |
|
|
|
vals := []struct{ k, v string }{ |
|
|
|
vals := []struct{ k, v string }{ |
|
|
|
{"do", "verb"}, |
|
|
|
{"do", "verb"}, |
|
|
|
{"ether", "wookiedoo"}, |
|
|
|
{"ether", "wookiedoo"}, |
|
|
@ -234,7 +228,7 @@ func TestDelete(t *testing.T) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func TestEmptyValues(t *testing.T) { |
|
|
|
func TestEmptyValues(t *testing.T) { |
|
|
|
trie := newEmpty() |
|
|
|
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase())) |
|
|
|
|
|
|
|
|
|
|
|
vals := []struct{ k, v string }{ |
|
|
|
vals := []struct{ k, v string }{ |
|
|
|
{"do", "verb"}, |
|
|
|
{"do", "verb"}, |
|
|
@ -258,7 +252,8 @@ func TestEmptyValues(t *testing.T) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func TestReplication(t *testing.T) { |
|
|
|
func TestReplication(t *testing.T) { |
|
|
|
trie := newEmpty() |
|
|
|
triedb := NewDatabase(rawdb.NewMemoryDatabase()) |
|
|
|
|
|
|
|
trie := NewEmpty(triedb) |
|
|
|
vals := []struct{ k, v string }{ |
|
|
|
vals := []struct{ k, v string }{ |
|
|
|
{"do", "verb"}, |
|
|
|
{"do", "verb"}, |
|
|
|
{"ether", "wookiedoo"}, |
|
|
|
{"ether", "wookiedoo"}, |
|
|
@ -271,13 +266,14 @@ func TestReplication(t *testing.T) { |
|
|
|
for _, val := range vals { |
|
|
|
for _, val := range vals { |
|
|
|
updateString(trie, val.k, val.v) |
|
|
|
updateString(trie, val.k, val.v) |
|
|
|
} |
|
|
|
} |
|
|
|
exp, _, err := trie.Commit(nil) |
|
|
|
exp, nodes, err := trie.Commit(false) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
t.Fatalf("commit error: %v", err) |
|
|
|
t.Fatalf("commit error: %v", err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
triedb.Update(NewWithNodeSet(nodes)) |
|
|
|
|
|
|
|
|
|
|
|
// create a new trie on top of the database and check that lookups work.
|
|
|
|
// create a new trie on top of the database and check that lookups work.
|
|
|
|
trie2, err := New(common.Hash{}, exp, trie.db) |
|
|
|
trie2, err := New(common.Hash{}, exp, triedb) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
t.Fatalf("can't recreate trie at %x: %v", exp, err) |
|
|
|
t.Fatalf("can't recreate trie at %x: %v", exp, err) |
|
|
|
} |
|
|
|
} |
|
|
@ -286,7 +282,7 @@ func TestReplication(t *testing.T) { |
|
|
|
t.Errorf("trie2 doesn't have %q => %q", kv.k, kv.v) |
|
|
|
t.Errorf("trie2 doesn't have %q => %q", kv.k, kv.v) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
hash, _, err := trie2.Commit(nil) |
|
|
|
hash, nodes, err := trie2.Commit(false) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
t.Fatalf("commit error: %v", err) |
|
|
|
t.Fatalf("commit error: %v", err) |
|
|
|
} |
|
|
|
} |
|
|
@ -294,6 +290,14 @@ func TestReplication(t *testing.T) { |
|
|
|
t.Errorf("root failure. expected %x got %x", exp, hash) |
|
|
|
t.Errorf("root failure. expected %x got %x", exp, hash) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// recreate the trie after commit
|
|
|
|
|
|
|
|
if nodes != nil { |
|
|
|
|
|
|
|
triedb.Update(NewWithNodeSet(nodes)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
trie2, err = New(common.Hash{}, hash, triedb) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
t.Fatalf("can't recreate trie at %x: %v", exp, err) |
|
|
|
|
|
|
|
} |
|
|
|
// perform some insertions on the new trie.
|
|
|
|
// perform some insertions on the new trie.
|
|
|
|
vals2 := []struct{ k, v string }{ |
|
|
|
vals2 := []struct{ k, v string }{ |
|
|
|
{"do", "verb"}, |
|
|
|
{"do", "verb"}, |
|
|
@ -315,7 +319,7 @@ func TestReplication(t *testing.T) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func TestLargeValue(t *testing.T) { |
|
|
|
func TestLargeValue(t *testing.T) { |
|
|
|
trie := newEmpty() |
|
|
|
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase())) |
|
|
|
trie.Update([]byte("key1"), []byte{99, 99, 99, 99}) |
|
|
|
trie.Update([]byte("key1"), []byte{99, 99, 99, 99}) |
|
|
|
trie.Update([]byte("key2"), bytes.Repeat([]byte{1}, 32)) |
|
|
|
trie.Update([]byte("key2"), bytes.Repeat([]byte{1}, 32)) |
|
|
|
trie.Hash() |
|
|
|
trie.Hash() |
|
|
@ -369,9 +373,8 @@ const ( |
|
|
|
opUpdate = iota |
|
|
|
opUpdate = iota |
|
|
|
opDelete |
|
|
|
opDelete |
|
|
|
opGet |
|
|
|
opGet |
|
|
|
opCommit |
|
|
|
|
|
|
|
opHash |
|
|
|
opHash |
|
|
|
opReset |
|
|
|
opCommit |
|
|
|
opItercheckhash |
|
|
|
opItercheckhash |
|
|
|
opNodeDiff |
|
|
|
opNodeDiff |
|
|
|
opMax // boundary value, not an actual op
|
|
|
|
opMax // boundary value, not an actual op
|
|
|
@ -433,17 +436,17 @@ func runRandTest(rt randTest) bool { |
|
|
|
if string(v) != want { |
|
|
|
if string(v) != want { |
|
|
|
rt[i].err = fmt.Errorf("mismatch for key %#x, got %#x want %#x", step.key, v, want) |
|
|
|
rt[i].err = fmt.Errorf("mismatch for key %#x, got %#x want %#x", step.key, v, want) |
|
|
|
} |
|
|
|
} |
|
|
|
case opCommit: |
|
|
|
|
|
|
|
_, _, rt[i].err = tr.Commit(nil) |
|
|
|
|
|
|
|
origTrie = tr.Copy() |
|
|
|
|
|
|
|
case opHash: |
|
|
|
case opHash: |
|
|
|
tr.Hash() |
|
|
|
tr.Hash() |
|
|
|
case opReset: |
|
|
|
case opCommit: |
|
|
|
hash, _, err := tr.Commit(nil) |
|
|
|
hash, nodes, err := tr.Commit(false) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
rt[i].err = err |
|
|
|
rt[i].err = err |
|
|
|
return false |
|
|
|
return false |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if nodes != nil { |
|
|
|
|
|
|
|
triedb.Update(NewWithNodeSet(nodes)) |
|
|
|
|
|
|
|
} |
|
|
|
newtr, err := New(common.Hash{}, hash, triedb) |
|
|
|
newtr, err := New(common.Hash{}, hash, triedb) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
rt[i].err = err |
|
|
|
rt[i].err = err |
|
|
@ -533,44 +536,31 @@ func TestRandom(t *testing.T) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func BenchmarkGet(b *testing.B) { benchGet(b, false) } |
|
|
|
func BenchmarkGet(b *testing.B) { benchGet(b) } |
|
|
|
func BenchmarkGetDB(b *testing.B) { benchGet(b, true) } |
|
|
|
|
|
|
|
func BenchmarkUpdateBE(b *testing.B) { benchUpdate(b, binary.BigEndian) } |
|
|
|
func BenchmarkUpdateBE(b *testing.B) { benchUpdate(b, binary.BigEndian) } |
|
|
|
func BenchmarkUpdateLE(b *testing.B) { benchUpdate(b, binary.LittleEndian) } |
|
|
|
func BenchmarkUpdateLE(b *testing.B) { benchUpdate(b, binary.LittleEndian) } |
|
|
|
|
|
|
|
|
|
|
|
const benchElemCount = 20000 |
|
|
|
const benchElemCount = 20000 |
|
|
|
|
|
|
|
|
|
|
|
func benchGet(b *testing.B, commit bool) { |
|
|
|
func benchGet(b *testing.B) { |
|
|
|
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase())) |
|
|
|
triedb := NewDatabase(rawdb.NewMemoryDatabase()) |
|
|
|
if commit { |
|
|
|
trie := NewEmpty(triedb) |
|
|
|
tmpdb := tempDB(b) |
|
|
|
|
|
|
|
trie = NewEmpty(tmpdb) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
k := make([]byte, 32) |
|
|
|
k := make([]byte, 32) |
|
|
|
for i := 0; i < benchElemCount; i++ { |
|
|
|
for i := 0; i < benchElemCount; i++ { |
|
|
|
binary.LittleEndian.PutUint64(k, uint64(i)) |
|
|
|
binary.LittleEndian.PutUint64(k, uint64(i)) |
|
|
|
trie.Update(k, k) |
|
|
|
trie.Update(k, k) |
|
|
|
} |
|
|
|
} |
|
|
|
binary.LittleEndian.PutUint64(k, benchElemCount/2) |
|
|
|
binary.LittleEndian.PutUint64(k, benchElemCount/2) |
|
|
|
if commit { |
|
|
|
|
|
|
|
trie.Commit(nil) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
b.ResetTimer() |
|
|
|
b.ResetTimer() |
|
|
|
for i := 0; i < b.N; i++ { |
|
|
|
for i := 0; i < b.N; i++ { |
|
|
|
trie.Get(k) |
|
|
|
trie.Get(k) |
|
|
|
} |
|
|
|
} |
|
|
|
b.StopTimer() |
|
|
|
b.StopTimer() |
|
|
|
|
|
|
|
|
|
|
|
if commit { |
|
|
|
|
|
|
|
ldb := trie.db.diskdb.(*leveldb.Database) |
|
|
|
|
|
|
|
ldb.Close() |
|
|
|
|
|
|
|
os.RemoveAll(ldb.Path()) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func benchUpdate(b *testing.B, e binary.ByteOrder) *Trie { |
|
|
|
func benchUpdate(b *testing.B, e binary.ByteOrder) *Trie { |
|
|
|
trie := newEmpty() |
|
|
|
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase())) |
|
|
|
k := make([]byte, 32) |
|
|
|
k := make([]byte, 32) |
|
|
|
b.ReportAllocs() |
|
|
|
b.ReportAllocs() |
|
|
|
for i := 0; i < b.N; i++ { |
|
|
|
for i := 0; i < b.N; i++ { |
|
|
@ -600,7 +590,7 @@ func BenchmarkHash(b *testing.B) { |
|
|
|
// entries, then adding N more.
|
|
|
|
// entries, then adding N more.
|
|
|
|
addresses, accounts := makeAccounts(2 * b.N) |
|
|
|
addresses, accounts := makeAccounts(2 * b.N) |
|
|
|
// Insert the accounts into the trie and hash it
|
|
|
|
// Insert the accounts into the trie and hash it
|
|
|
|
trie := newEmpty() |
|
|
|
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase())) |
|
|
|
i := 0 |
|
|
|
i := 0 |
|
|
|
for ; i < len(addresses)/2; i++ { |
|
|
|
for ; i < len(addresses)/2; i++ { |
|
|
|
trie.Update(crypto.Keccak256(addresses[i][:]), accounts[i]) |
|
|
|
trie.Update(crypto.Keccak256(addresses[i][:]), accounts[i]) |
|
|
@ -621,22 +611,17 @@ func BenchmarkHash(b *testing.B) { |
|
|
|
// insert into the trie before measuring the hashing.
|
|
|
|
// insert into the trie before measuring the hashing.
|
|
|
|
func BenchmarkCommitAfterHash(b *testing.B) { |
|
|
|
func BenchmarkCommitAfterHash(b *testing.B) { |
|
|
|
b.Run("no-onleaf", func(b *testing.B) { |
|
|
|
b.Run("no-onleaf", func(b *testing.B) { |
|
|
|
benchmarkCommitAfterHash(b, nil) |
|
|
|
benchmarkCommitAfterHash(b, false) |
|
|
|
}) |
|
|
|
}) |
|
|
|
var a types.StateAccount |
|
|
|
|
|
|
|
onleaf := func(paths [][]byte, hexpath []byte, leaf []byte, parent common.Hash, parentPath []byte) error { |
|
|
|
|
|
|
|
rlp.DecodeBytes(leaf, &a) |
|
|
|
|
|
|
|
return nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
b.Run("with-onleaf", func(b *testing.B) { |
|
|
|
b.Run("with-onleaf", func(b *testing.B) { |
|
|
|
benchmarkCommitAfterHash(b, onleaf) |
|
|
|
benchmarkCommitAfterHash(b, true) |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func benchmarkCommitAfterHash(b *testing.B, onleaf LeafCallback) { |
|
|
|
func benchmarkCommitAfterHash(b *testing.B, collectLeaf bool) { |
|
|
|
// Make the random benchmark deterministic
|
|
|
|
// Make the random benchmark deterministic
|
|
|
|
addresses, accounts := makeAccounts(b.N) |
|
|
|
addresses, accounts := makeAccounts(b.N) |
|
|
|
trie := newEmpty() |
|
|
|
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase())) |
|
|
|
for i := 0; i < len(addresses); i++ { |
|
|
|
for i := 0; i < len(addresses); i++ { |
|
|
|
trie.Update(crypto.Keccak256(addresses[i][:]), accounts[i]) |
|
|
|
trie.Update(crypto.Keccak256(addresses[i][:]), accounts[i]) |
|
|
|
} |
|
|
|
} |
|
|
@ -644,13 +629,13 @@ func benchmarkCommitAfterHash(b *testing.B, onleaf LeafCallback) { |
|
|
|
trie.Hash() |
|
|
|
trie.Hash() |
|
|
|
b.ResetTimer() |
|
|
|
b.ResetTimer() |
|
|
|
b.ReportAllocs() |
|
|
|
b.ReportAllocs() |
|
|
|
trie.Commit(onleaf) |
|
|
|
trie.Commit(collectLeaf) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func TestTinyTrie(t *testing.T) { |
|
|
|
func TestTinyTrie(t *testing.T) { |
|
|
|
// Create a realistic account trie to hash
|
|
|
|
// Create a realistic account trie to hash
|
|
|
|
_, accounts := makeAccounts(5) |
|
|
|
_, accounts := makeAccounts(5) |
|
|
|
trie := newEmpty() |
|
|
|
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase())) |
|
|
|
trie.Update(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000001337"), accounts[3]) |
|
|
|
trie.Update(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000001337"), accounts[3]) |
|
|
|
if exp, root := common.HexToHash("8c6a85a4d9fda98feff88450299e574e5378e32391f75a055d470ac0653f1005"), trie.Hash(); exp != root { |
|
|
|
if exp, root := common.HexToHash("8c6a85a4d9fda98feff88450299e574e5378e32391f75a055d470ac0653f1005"), trie.Hash(); exp != root { |
|
|
|
t.Errorf("1: got %x, exp %x", root, exp) |
|
|
|
t.Errorf("1: got %x, exp %x", root, exp) |
|
|
@ -663,7 +648,7 @@ func TestTinyTrie(t *testing.T) { |
|
|
|
if exp, root := common.HexToHash("0608c1d1dc3905fa22204c7a0e43644831c3b6d3def0f274be623a948197e64a"), trie.Hash(); exp != root { |
|
|
|
if exp, root := common.HexToHash("0608c1d1dc3905fa22204c7a0e43644831c3b6d3def0f274be623a948197e64a"), trie.Hash(); exp != root { |
|
|
|
t.Errorf("3: got %x, exp %x", root, exp) |
|
|
|
t.Errorf("3: got %x, exp %x", root, exp) |
|
|
|
} |
|
|
|
} |
|
|
|
checktr := NewEmpty(trie.db) |
|
|
|
checktr := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase())) |
|
|
|
it := NewIterator(trie.NodeIterator(nil)) |
|
|
|
it := NewIterator(trie.NodeIterator(nil)) |
|
|
|
for it.Next() { |
|
|
|
for it.Next() { |
|
|
|
checktr.Update(it.Key, it.Value) |
|
|
|
checktr.Update(it.Key, it.Value) |
|
|
@ -676,19 +661,19 @@ func TestTinyTrie(t *testing.T) { |
|
|
|
func TestCommitAfterHash(t *testing.T) { |
|
|
|
func TestCommitAfterHash(t *testing.T) { |
|
|
|
// Create a realistic account trie to hash
|
|
|
|
// Create a realistic account trie to hash
|
|
|
|
addresses, accounts := makeAccounts(1000) |
|
|
|
addresses, accounts := makeAccounts(1000) |
|
|
|
trie := newEmpty() |
|
|
|
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase())) |
|
|
|
for i := 0; i < len(addresses); i++ { |
|
|
|
for i := 0; i < len(addresses); i++ { |
|
|
|
trie.Update(crypto.Keccak256(addresses[i][:]), accounts[i]) |
|
|
|
trie.Update(crypto.Keccak256(addresses[i][:]), accounts[i]) |
|
|
|
} |
|
|
|
} |
|
|
|
// Insert the accounts into the trie and hash it
|
|
|
|
// Insert the accounts into the trie and hash it
|
|
|
|
trie.Hash() |
|
|
|
trie.Hash() |
|
|
|
trie.Commit(nil) |
|
|
|
trie.Commit(false) |
|
|
|
root := trie.Hash() |
|
|
|
root := trie.Hash() |
|
|
|
exp := common.HexToHash("72f9d3f3fe1e1dd7b8936442e7642aef76371472d94319900790053c493f3fe6") |
|
|
|
exp := common.HexToHash("72f9d3f3fe1e1dd7b8936442e7642aef76371472d94319900790053c493f3fe6") |
|
|
|
if exp != root { |
|
|
|
if exp != root { |
|
|
|
t.Errorf("got %x, exp %x", root, exp) |
|
|
|
t.Errorf("got %x, exp %x", root, exp) |
|
|
|
} |
|
|
|
} |
|
|
|
root, _, _ = trie.Commit(nil) |
|
|
|
root, _, _ = trie.Commit(false) |
|
|
|
if exp != root { |
|
|
|
if exp != root { |
|
|
|
t.Errorf("got %x, exp %x", root, exp) |
|
|
|
t.Errorf("got %x, exp %x", root, exp) |
|
|
|
} |
|
|
|
} |
|
|
@ -797,7 +782,8 @@ func TestCommitSequence(t *testing.T) { |
|
|
|
trie.Update(crypto.Keccak256(addresses[i][:]), accounts[i]) |
|
|
|
trie.Update(crypto.Keccak256(addresses[i][:]), accounts[i]) |
|
|
|
} |
|
|
|
} |
|
|
|
// Flush trie -> database
|
|
|
|
// Flush trie -> database
|
|
|
|
root, _, _ := trie.Commit(nil) |
|
|
|
root, nodes, _ := trie.Commit(false) |
|
|
|
|
|
|
|
db.Update(NewWithNodeSet(nodes)) |
|
|
|
// Flush memdb -> disk (sponge)
|
|
|
|
// Flush memdb -> disk (sponge)
|
|
|
|
db.Commit(root, false, func(c common.Hash) { |
|
|
|
db.Commit(root, false, func(c common.Hash) { |
|
|
|
// And spongify the callback-order
|
|
|
|
// And spongify the callback-order
|
|
|
@ -849,7 +835,8 @@ func TestCommitSequenceRandomBlobs(t *testing.T) { |
|
|
|
trie.Update(key, val) |
|
|
|
trie.Update(key, val) |
|
|
|
} |
|
|
|
} |
|
|
|
// Flush trie -> database
|
|
|
|
// Flush trie -> database
|
|
|
|
root, _, _ := trie.Commit(nil) |
|
|
|
root, nodes, _ := trie.Commit(false) |
|
|
|
|
|
|
|
db.Update(NewWithNodeSet(nodes)) |
|
|
|
// Flush memdb -> disk (sponge)
|
|
|
|
// Flush memdb -> disk (sponge)
|
|
|
|
db.Commit(root, false, func(c common.Hash) { |
|
|
|
db.Commit(root, false, func(c common.Hash) { |
|
|
|
// And spongify the callback-order
|
|
|
|
// And spongify the callback-order
|
|
|
@ -875,7 +862,7 @@ func TestCommitSequenceStackTrie(t *testing.T) { |
|
|
|
stackTrieSponge := &spongeDb{sponge: sha3.NewLegacyKeccak256(), id: "b"} |
|
|
|
stackTrieSponge := &spongeDb{sponge: sha3.NewLegacyKeccak256(), id: "b"} |
|
|
|
stTrie := NewStackTrie(stackTrieSponge) |
|
|
|
stTrie := NewStackTrie(stackTrieSponge) |
|
|
|
// Fill the trie with elements
|
|
|
|
// Fill the trie with elements
|
|
|
|
for i := 1; i < count; i++ { |
|
|
|
for i := 0; i < count; i++ { |
|
|
|
// For the stack trie, we need to do inserts in proper order
|
|
|
|
// For the stack trie, we need to do inserts in proper order
|
|
|
|
key := make([]byte, 32) |
|
|
|
key := make([]byte, 32) |
|
|
|
binary.BigEndian.PutUint64(key, uint64(i)) |
|
|
|
binary.BigEndian.PutUint64(key, uint64(i)) |
|
|
@ -891,8 +878,9 @@ func TestCommitSequenceStackTrie(t *testing.T) { |
|
|
|
stTrie.TryUpdate(key, val) |
|
|
|
stTrie.TryUpdate(key, val) |
|
|
|
} |
|
|
|
} |
|
|
|
// Flush trie -> database
|
|
|
|
// Flush trie -> database
|
|
|
|
root, _, _ := trie.Commit(nil) |
|
|
|
root, nodes, _ := trie.Commit(false) |
|
|
|
// Flush memdb -> disk (sponge)
|
|
|
|
// Flush memdb -> disk (sponge)
|
|
|
|
|
|
|
|
db.Update(NewWithNodeSet(nodes)) |
|
|
|
db.Commit(root, false, nil) |
|
|
|
db.Commit(root, false, nil) |
|
|
|
// And flush stacktrie -> disk
|
|
|
|
// And flush stacktrie -> disk
|
|
|
|
stRoot, err := stTrie.Commit() |
|
|
|
stRoot, err := stTrie.Commit() |
|
|
@ -936,8 +924,9 @@ func TestCommitSequenceSmallRoot(t *testing.T) { |
|
|
|
trie.TryUpdate(key, []byte{0x1}) |
|
|
|
trie.TryUpdate(key, []byte{0x1}) |
|
|
|
stTrie.TryUpdate(key, []byte{0x1}) |
|
|
|
stTrie.TryUpdate(key, []byte{0x1}) |
|
|
|
// Flush trie -> database
|
|
|
|
// Flush trie -> database
|
|
|
|
root, _, _ := trie.Commit(nil) |
|
|
|
root, nodes, _ := trie.Commit(false) |
|
|
|
// Flush memdb -> disk (sponge)
|
|
|
|
// Flush memdb -> disk (sponge)
|
|
|
|
|
|
|
|
db.Update(NewWithNodeSet(nodes)) |
|
|
|
db.Commit(root, false, nil) |
|
|
|
db.Commit(root, false, nil) |
|
|
|
// And flush stacktrie -> disk
|
|
|
|
// And flush stacktrie -> disk
|
|
|
|
stRoot, err := stTrie.Commit() |
|
|
|
stRoot, err := stTrie.Commit() |
|
|
@ -999,7 +988,7 @@ func BenchmarkHashFixedSize(b *testing.B) { |
|
|
|
|
|
|
|
|
|
|
|
func benchmarkHashFixedSize(b *testing.B, addresses [][20]byte, accounts [][]byte) { |
|
|
|
func benchmarkHashFixedSize(b *testing.B, addresses [][20]byte, accounts [][]byte) { |
|
|
|
b.ReportAllocs() |
|
|
|
b.ReportAllocs() |
|
|
|
trie := newEmpty() |
|
|
|
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase())) |
|
|
|
for i := 0; i < len(addresses); i++ { |
|
|
|
for i := 0; i < len(addresses); i++ { |
|
|
|
trie.Update(crypto.Keccak256(addresses[i][:]), accounts[i]) |
|
|
|
trie.Update(crypto.Keccak256(addresses[i][:]), accounts[i]) |
|
|
|
} |
|
|
|
} |
|
|
@ -1050,14 +1039,14 @@ func BenchmarkCommitAfterHashFixedSize(b *testing.B) { |
|
|
|
|
|
|
|
|
|
|
|
func benchmarkCommitAfterHashFixedSize(b *testing.B, addresses [][20]byte, accounts [][]byte) { |
|
|
|
func benchmarkCommitAfterHashFixedSize(b *testing.B, addresses [][20]byte, accounts [][]byte) { |
|
|
|
b.ReportAllocs() |
|
|
|
b.ReportAllocs() |
|
|
|
trie := newEmpty() |
|
|
|
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase())) |
|
|
|
for i := 0; i < len(addresses); i++ { |
|
|
|
for i := 0; i < len(addresses); i++ { |
|
|
|
trie.Update(crypto.Keccak256(addresses[i][:]), accounts[i]) |
|
|
|
trie.Update(crypto.Keccak256(addresses[i][:]), accounts[i]) |
|
|
|
} |
|
|
|
} |
|
|
|
// Insert the accounts into the trie and hash it
|
|
|
|
// Insert the accounts into the trie and hash it
|
|
|
|
trie.Hash() |
|
|
|
trie.Hash() |
|
|
|
b.StartTimer() |
|
|
|
b.StartTimer() |
|
|
|
trie.Commit(nil) |
|
|
|
trie.Commit(false) |
|
|
|
b.StopTimer() |
|
|
|
b.StopTimer() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -1102,26 +1091,19 @@ func BenchmarkDerefRootFixedSize(b *testing.B) { |
|
|
|
|
|
|
|
|
|
|
|
func benchmarkDerefRootFixedSize(b *testing.B, addresses [][20]byte, accounts [][]byte) { |
|
|
|
func benchmarkDerefRootFixedSize(b *testing.B, addresses [][20]byte, accounts [][]byte) { |
|
|
|
b.ReportAllocs() |
|
|
|
b.ReportAllocs() |
|
|
|
trie := newEmpty() |
|
|
|
triedb := NewDatabase(rawdb.NewMemoryDatabase()) |
|
|
|
|
|
|
|
trie := NewEmpty(triedb) |
|
|
|
for i := 0; i < len(addresses); i++ { |
|
|
|
for i := 0; i < len(addresses); i++ { |
|
|
|
trie.Update(crypto.Keccak256(addresses[i][:]), accounts[i]) |
|
|
|
trie.Update(crypto.Keccak256(addresses[i][:]), accounts[i]) |
|
|
|
} |
|
|
|
} |
|
|
|
h := trie.Hash() |
|
|
|
h := trie.Hash() |
|
|
|
trie.Commit(nil) |
|
|
|
_, nodes, _ := trie.Commit(false) |
|
|
|
|
|
|
|
triedb.Update(NewWithNodeSet(nodes)) |
|
|
|
b.StartTimer() |
|
|
|
b.StartTimer() |
|
|
|
trie.db.Dereference(h) |
|
|
|
triedb.Dereference(h) |
|
|
|
b.StopTimer() |
|
|
|
b.StopTimer() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func tempDB(tb testing.TB) *Database { |
|
|
|
|
|
|
|
dir := tb.TempDir() |
|
|
|
|
|
|
|
diskdb, err := leveldb.New(dir, 256, 0, "", false) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
panic(fmt.Sprintf("can't create temporary database: %v", err)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return NewDatabase(diskdb) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func getString(trie *Trie, k string) []byte { |
|
|
|
func getString(trie *Trie, k string) []byte { |
|
|
|
return trie.Get([]byte(k)) |
|
|
|
return trie.Get([]byte(k)) |
|
|
|
} |
|
|
|
} |
|
|
|