|
|
|
@ -25,6 +25,7 @@ import ( |
|
|
|
|
"io" |
|
|
|
|
"math/rand" |
|
|
|
|
"reflect" |
|
|
|
|
"sort" |
|
|
|
|
"testing" |
|
|
|
|
"testing/quick" |
|
|
|
|
|
|
|
|
@ -46,7 +47,7 @@ func init() { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestEmptyTrie(t *testing.T) { |
|
|
|
|
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase(), nil)) |
|
|
|
|
trie := NewEmpty(newTestDatabase(rawdb.NewMemoryDatabase(), rawdb.HashScheme)) |
|
|
|
|
res := trie.Hash() |
|
|
|
|
exp := types.EmptyRootHash |
|
|
|
|
if res != exp { |
|
|
|
@ -55,7 +56,7 @@ func TestEmptyTrie(t *testing.T) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestNull(t *testing.T) { |
|
|
|
|
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase(), nil)) |
|
|
|
|
trie := NewEmpty(newTestDatabase(rawdb.NewMemoryDatabase(), rawdb.HashScheme)) |
|
|
|
|
key := make([]byte, 32) |
|
|
|
|
value := []byte("test") |
|
|
|
|
trie.MustUpdate(key, value) |
|
|
|
@ -95,10 +96,10 @@ func testMissingNode(t *testing.T, memonly bool, scheme string) { |
|
|
|
|
updateString(trie, "120000", "qwerqwerqwerqwerqwerqwerqwerqwer") |
|
|
|
|
updateString(trie, "123456", "asdfasdfasdfasdfasdfasdfasdfasdf") |
|
|
|
|
root, nodes, _ := trie.Commit(false) |
|
|
|
|
triedb.Update(root, types.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil) |
|
|
|
|
triedb.Update(root, types.EmptyRootHash, trienode.NewWithNodeSet(nodes)) |
|
|
|
|
|
|
|
|
|
if !memonly { |
|
|
|
|
triedb.Commit(root, false) |
|
|
|
|
triedb.Commit(root) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
trie, _ = New(TrieID(root), triedb) |
|
|
|
@ -167,7 +168,7 @@ func testMissingNode(t *testing.T, memonly bool, scheme string) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestInsert(t *testing.T) { |
|
|
|
|
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase(), nil)) |
|
|
|
|
trie := NewEmpty(newTestDatabase(rawdb.NewMemoryDatabase(), rawdb.HashScheme)) |
|
|
|
|
|
|
|
|
|
updateString(trie, "doe", "reindeer") |
|
|
|
|
updateString(trie, "dog", "puppy") |
|
|
|
@ -179,7 +180,7 @@ func TestInsert(t *testing.T) { |
|
|
|
|
t.Errorf("case 1: exp %x got %x", exp, root) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
trie = NewEmpty(NewDatabase(rawdb.NewMemoryDatabase(), nil)) |
|
|
|
|
trie = NewEmpty(newTestDatabase(rawdb.NewMemoryDatabase(), rawdb.HashScheme)) |
|
|
|
|
updateString(trie, "A", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa") |
|
|
|
|
|
|
|
|
|
exp = common.HexToHash("d23786fb4a010da3ce639d66d5e904a11dbc02746d1ce25029e53290cabf28ab") |
|
|
|
@ -190,7 +191,7 @@ func TestInsert(t *testing.T) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestGet(t *testing.T) { |
|
|
|
|
db := NewDatabase(rawdb.NewMemoryDatabase(), nil) |
|
|
|
|
db := newTestDatabase(rawdb.NewMemoryDatabase(), rawdb.HashScheme) |
|
|
|
|
trie := NewEmpty(db) |
|
|
|
|
updateString(trie, "doe", "reindeer") |
|
|
|
|
updateString(trie, "dog", "puppy") |
|
|
|
@ -209,13 +210,14 @@ func TestGet(t *testing.T) { |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
root, nodes, _ := trie.Commit(false) |
|
|
|
|
db.Update(root, types.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil) |
|
|
|
|
db.Update(root, types.EmptyRootHash, trienode.NewWithNodeSet(nodes)) |
|
|
|
|
trie, _ = New(TrieID(root), db) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestDelete(t *testing.T) { |
|
|
|
|
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase(), nil)) |
|
|
|
|
db := newTestDatabase(rawdb.NewMemoryDatabase(), rawdb.HashScheme) |
|
|
|
|
trie := NewEmpty(db) |
|
|
|
|
vals := []struct{ k, v string }{ |
|
|
|
|
{"do", "verb"}, |
|
|
|
|
{"ether", "wookiedoo"}, |
|
|
|
@ -242,7 +244,7 @@ func TestDelete(t *testing.T) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestEmptyValues(t *testing.T) { |
|
|
|
|
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase(), nil)) |
|
|
|
|
trie := NewEmpty(newTestDatabase(rawdb.NewMemoryDatabase(), rawdb.HashScheme)) |
|
|
|
|
|
|
|
|
|
vals := []struct{ k, v string }{ |
|
|
|
|
{"do", "verb"}, |
|
|
|
@ -266,7 +268,7 @@ func TestEmptyValues(t *testing.T) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestReplication(t *testing.T) { |
|
|
|
|
db := NewDatabase(rawdb.NewMemoryDatabase(), nil) |
|
|
|
|
db := newTestDatabase(rawdb.NewMemoryDatabase(), rawdb.HashScheme) |
|
|
|
|
trie := NewEmpty(db) |
|
|
|
|
vals := []struct{ k, v string }{ |
|
|
|
|
{"do", "verb"}, |
|
|
|
@ -281,7 +283,7 @@ func TestReplication(t *testing.T) { |
|
|
|
|
updateString(trie, val.k, val.v) |
|
|
|
|
} |
|
|
|
|
root, nodes, _ := trie.Commit(false) |
|
|
|
|
db.Update(root, types.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil) |
|
|
|
|
db.Update(root, types.EmptyRootHash, trienode.NewWithNodeSet(nodes)) |
|
|
|
|
|
|
|
|
|
// create a new trie on top of the database and check that lookups work.
|
|
|
|
|
trie2, err := New(TrieID(root), db) |
|
|
|
@ -300,7 +302,7 @@ func TestReplication(t *testing.T) { |
|
|
|
|
|
|
|
|
|
// recreate the trie after commit
|
|
|
|
|
if nodes != nil { |
|
|
|
|
db.Update(hash, types.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil) |
|
|
|
|
db.Update(hash, types.EmptyRootHash, trienode.NewWithNodeSet(nodes)) |
|
|
|
|
} |
|
|
|
|
trie2, err = New(TrieID(hash), db) |
|
|
|
|
if err != nil { |
|
|
|
@ -327,7 +329,7 @@ func TestReplication(t *testing.T) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func TestLargeValue(t *testing.T) { |
|
|
|
|
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase(), nil)) |
|
|
|
|
trie := NewEmpty(newTestDatabase(rawdb.NewMemoryDatabase(), rawdb.HashScheme)) |
|
|
|
|
trie.MustUpdate([]byte("key1"), []byte{99, 99, 99, 99}) |
|
|
|
|
trie.MustUpdate([]byte("key2"), bytes.Repeat([]byte{1}, 32)) |
|
|
|
|
trie.Hash() |
|
|
|
@ -531,7 +533,7 @@ func runRandTest(rt randTest) error { |
|
|
|
|
case opCommit: |
|
|
|
|
root, nodes, _ := tr.Commit(true) |
|
|
|
|
if nodes != nil { |
|
|
|
|
triedb.Update(root, origin, 0, trienode.NewWithNodeSet(nodes), nil) |
|
|
|
|
triedb.Update(root, origin, trienode.NewWithNodeSet(nodes)) |
|
|
|
|
} |
|
|
|
|
newtr, err := New(TrieID(root), triedb) |
|
|
|
|
if err != nil { |
|
|
|
@ -632,7 +634,7 @@ func BenchmarkUpdateLE(b *testing.B) { benchUpdate(b, binary.LittleEndian) } |
|
|
|
|
const benchElemCount = 20000 |
|
|
|
|
|
|
|
|
|
func benchGet(b *testing.B) { |
|
|
|
|
triedb := NewDatabase(rawdb.NewMemoryDatabase(), nil) |
|
|
|
|
triedb := newTestDatabase(rawdb.NewMemoryDatabase(), rawdb.HashScheme) |
|
|
|
|
trie := NewEmpty(triedb) |
|
|
|
|
k := make([]byte, 32) |
|
|
|
|
for i := 0; i < benchElemCount; i++ { |
|
|
|
@ -651,7 +653,7 @@ func benchGet(b *testing.B) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func benchUpdate(b *testing.B, e binary.ByteOrder) *Trie { |
|
|
|
|
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase(), nil)) |
|
|
|
|
trie := NewEmpty(newTestDatabase(rawdb.NewMemoryDatabase(), rawdb.HashScheme)) |
|
|
|
|
k := make([]byte, 32) |
|
|
|
|
b.ReportAllocs() |
|
|
|
|
for i := 0; i < b.N; i++ { |
|
|
|
@ -683,7 +685,7 @@ func BenchmarkHash(b *testing.B) { |
|
|
|
|
// entries, then adding N more.
|
|
|
|
|
addresses, accounts := makeAccounts(2 * b.N) |
|
|
|
|
// Insert the accounts into the trie and hash it
|
|
|
|
|
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase(), nil)) |
|
|
|
|
trie := NewEmpty(newTestDatabase(rawdb.NewMemoryDatabase(), rawdb.HashScheme)) |
|
|
|
|
i := 0 |
|
|
|
|
for ; i < len(addresses)/2; i++ { |
|
|
|
|
trie.MustUpdate(crypto.Keccak256(addresses[i][:]), accounts[i]) |
|
|
|
@ -714,7 +716,7 @@ func BenchmarkCommitAfterHash(b *testing.B) { |
|
|
|
|
func benchmarkCommitAfterHash(b *testing.B, collectLeaf bool) { |
|
|
|
|
// Make the random benchmark deterministic
|
|
|
|
|
addresses, accounts := makeAccounts(b.N) |
|
|
|
|
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase(), nil)) |
|
|
|
|
trie := NewEmpty(newTestDatabase(rawdb.NewMemoryDatabase(), rawdb.HashScheme)) |
|
|
|
|
for i := 0; i < len(addresses); i++ { |
|
|
|
|
trie.MustUpdate(crypto.Keccak256(addresses[i][:]), accounts[i]) |
|
|
|
|
} |
|
|
|
@ -728,7 +730,7 @@ func benchmarkCommitAfterHash(b *testing.B, collectLeaf bool) { |
|
|
|
|
func TestTinyTrie(t *testing.T) { |
|
|
|
|
// Create a realistic account trie to hash
|
|
|
|
|
_, accounts := makeAccounts(5) |
|
|
|
|
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase(), nil)) |
|
|
|
|
trie := NewEmpty(newTestDatabase(rawdb.NewMemoryDatabase(), rawdb.HashScheme)) |
|
|
|
|
trie.MustUpdate(common.Hex2Bytes("0000000000000000000000000000000000000000000000000000000000001337"), accounts[3]) |
|
|
|
|
if exp, root := common.HexToHash("8c6a85a4d9fda98feff88450299e574e5378e32391f75a055d470ac0653f1005"), trie.Hash(); exp != root { |
|
|
|
|
t.Errorf("1: got %x, exp %x", root, exp) |
|
|
|
@ -741,7 +743,7 @@ func TestTinyTrie(t *testing.T) { |
|
|
|
|
if exp, root := common.HexToHash("0608c1d1dc3905fa22204c7a0e43644831c3b6d3def0f274be623a948197e64a"), trie.Hash(); exp != root { |
|
|
|
|
t.Errorf("3: got %x, exp %x", root, exp) |
|
|
|
|
} |
|
|
|
|
checktr := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase(), nil)) |
|
|
|
|
checktr := NewEmpty(newTestDatabase(rawdb.NewMemoryDatabase(), rawdb.HashScheme)) |
|
|
|
|
it := NewIterator(trie.MustNodeIterator(nil)) |
|
|
|
|
for it.Next() { |
|
|
|
|
checktr.MustUpdate(it.Key, it.Value) |
|
|
|
@ -754,7 +756,7 @@ func TestTinyTrie(t *testing.T) { |
|
|
|
|
func TestCommitAfterHash(t *testing.T) { |
|
|
|
|
// Create a realistic account trie to hash
|
|
|
|
|
addresses, accounts := makeAccounts(1000) |
|
|
|
|
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase(), nil)) |
|
|
|
|
trie := NewEmpty(newTestDatabase(rawdb.NewMemoryDatabase(), rawdb.HashScheme)) |
|
|
|
|
for i := 0; i < len(addresses); i++ { |
|
|
|
|
trie.MustUpdate(crypto.Keccak256(addresses[i][:]), accounts[i]) |
|
|
|
|
} |
|
|
|
@ -808,6 +810,8 @@ type spongeDb struct { |
|
|
|
|
sponge hash.Hash |
|
|
|
|
id string |
|
|
|
|
journal []string |
|
|
|
|
keys []string |
|
|
|
|
values map[string]string |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (s *spongeDb) Has(key []byte) (bool, error) { panic("implement me") } |
|
|
|
@ -831,12 +835,27 @@ func (s *spongeDb) Put(key []byte, value []byte) error { |
|
|
|
|
valbrief = valbrief[:8] |
|
|
|
|
} |
|
|
|
|
s.journal = append(s.journal, fmt.Sprintf("%v: PUT([%x...], [%d bytes] %x...)\n", s.id, keybrief, len(value), valbrief)) |
|
|
|
|
|
|
|
|
|
if s.values == nil { |
|
|
|
|
s.sponge.Write(key) |
|
|
|
|
s.sponge.Write(value) |
|
|
|
|
} else { |
|
|
|
|
s.keys = append(s.keys, string(key)) |
|
|
|
|
s.values[string(key)] = string(value) |
|
|
|
|
} |
|
|
|
|
return nil |
|
|
|
|
} |
|
|
|
|
func (s *spongeDb) NewIterator(prefix []byte, start []byte) ethdb.Iterator { panic("implement me") } |
|
|
|
|
|
|
|
|
|
func (s *spongeDb) Flush() { |
|
|
|
|
// Bottom-up, the longest path first
|
|
|
|
|
sort.Sort(sort.Reverse(sort.StringSlice(s.keys))) |
|
|
|
|
for _, key := range s.keys { |
|
|
|
|
s.sponge.Write([]byte(key)) |
|
|
|
|
s.sponge.Write([]byte(s.values[key])) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// spongeBatch is a dummy batch which immediately writes to the underlying spongedb
|
|
|
|
|
type spongeBatch struct { |
|
|
|
|
db *spongeDb |
|
|
|
@ -861,14 +880,14 @@ func TestCommitSequence(t *testing.T) { |
|
|
|
|
count int |
|
|
|
|
expWriteSeqHash []byte |
|
|
|
|
}{ |
|
|
|
|
{20, common.FromHex("873c78df73d60e59d4a2bcf3716e8bfe14554549fea2fc147cb54129382a8066")}, |
|
|
|
|
{200, common.FromHex("ba03d891bb15408c940eea5ee3d54d419595102648d02774a0268d892add9c8e")}, |
|
|
|
|
{2000, common.FromHex("f7a184f20df01c94f09537401d11e68d97ad0c00115233107f51b9c287ce60c7")}, |
|
|
|
|
{20, common.FromHex("330b0afae2853d96b9f015791fbe0fb7f239bf65f335f16dfc04b76c7536276d")}, |
|
|
|
|
{200, common.FromHex("5162b3735c06b5d606b043a3ee8adbdbbb408543f4966bca9dcc63da82684eeb")}, |
|
|
|
|
{2000, common.FromHex("4574cd8e6b17f3fe8ad89140d1d0bf4f1bd7a87a8ac3fb623b33550544c77635")}, |
|
|
|
|
} { |
|
|
|
|
addresses, accounts := makeAccounts(tc.count) |
|
|
|
|
// This spongeDb is used to check the sequence of disk-db-writes
|
|
|
|
|
s := &spongeDb{sponge: sha3.NewLegacyKeccak256()} |
|
|
|
|
db := NewDatabase(rawdb.NewDatabase(s), nil) |
|
|
|
|
db := newTestDatabase(rawdb.NewDatabase(s), rawdb.HashScheme) |
|
|
|
|
trie := NewEmpty(db) |
|
|
|
|
// Fill the trie with elements
|
|
|
|
|
for i := 0; i < tc.count; i++ { |
|
|
|
@ -876,9 +895,9 @@ func TestCommitSequence(t *testing.T) { |
|
|
|
|
} |
|
|
|
|
// Flush trie -> database
|
|
|
|
|
root, nodes, _ := trie.Commit(false) |
|
|
|
|
db.Update(root, types.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil) |
|
|
|
|
db.Update(root, types.EmptyRootHash, trienode.NewWithNodeSet(nodes)) |
|
|
|
|
// Flush memdb -> disk (sponge)
|
|
|
|
|
db.Commit(root, false) |
|
|
|
|
db.Commit(root) |
|
|
|
|
if got, exp := s.sponge.Sum(nil), tc.expWriteSeqHash; !bytes.Equal(got, exp) { |
|
|
|
|
t.Errorf("test %d, disk write sequence wrong:\ngot %x exp %x\n", i, got, exp) |
|
|
|
|
} |
|
|
|
@ -892,14 +911,14 @@ func TestCommitSequenceRandomBlobs(t *testing.T) { |
|
|
|
|
count int |
|
|
|
|
expWriteSeqHash []byte |
|
|
|
|
}{ |
|
|
|
|
{20, common.FromHex("8e4a01548551d139fa9e833ebc4e66fc1ba40a4b9b7259d80db32cff7b64ebbc")}, |
|
|
|
|
{200, common.FromHex("6869b4e7b95f3097a19ddb30ff735f922b915314047e041614df06958fc50554")}, |
|
|
|
|
{2000, common.FromHex("444200e6f4e2df49f77752f629a96ccf7445d4698c164f962bbd85a0526ef424")}, |
|
|
|
|
{20, common.FromHex("8016650c7a50cf88485fd06cde52d634a89711051107f00d21fae98234f2f13d")}, |
|
|
|
|
{200, common.FromHex("dde92ca9812e068e6982d04b40846dc65a61a9fd4996fc0f55f2fde172a8e13c")}, |
|
|
|
|
{2000, common.FromHex("ab553a7f9aff82e3929c382908e30ef7dd17a332933e92ba3fe873fc661ef382")}, |
|
|
|
|
} { |
|
|
|
|
prng := rand.New(rand.NewSource(int64(i))) |
|
|
|
|
// This spongeDb is used to check the sequence of disk-db-writes
|
|
|
|
|
s := &spongeDb{sponge: sha3.NewLegacyKeccak256()} |
|
|
|
|
db := NewDatabase(rawdb.NewDatabase(s), nil) |
|
|
|
|
db := newTestDatabase(rawdb.NewDatabase(s), rawdb.HashScheme) |
|
|
|
|
trie := NewEmpty(db) |
|
|
|
|
// Fill the trie with elements
|
|
|
|
|
for i := 0; i < tc.count; i++ { |
|
|
|
@ -917,9 +936,9 @@ func TestCommitSequenceRandomBlobs(t *testing.T) { |
|
|
|
|
} |
|
|
|
|
// Flush trie -> database
|
|
|
|
|
root, nodes, _ := trie.Commit(false) |
|
|
|
|
db.Update(root, types.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil) |
|
|
|
|
db.Update(root, types.EmptyRootHash, trienode.NewWithNodeSet(nodes)) |
|
|
|
|
// Flush memdb -> disk (sponge)
|
|
|
|
|
db.Commit(root, false) |
|
|
|
|
db.Commit(root) |
|
|
|
|
if got, exp := s.sponge.Sum(nil), tc.expWriteSeqHash; !bytes.Equal(got, exp) { |
|
|
|
|
t.Fatalf("test %d, disk write sequence wrong:\ngot %x exp %x\n", i, got, exp) |
|
|
|
|
} |
|
|
|
@ -930,17 +949,26 @@ func TestCommitSequenceStackTrie(t *testing.T) { |
|
|
|
|
for count := 1; count < 200; count++ { |
|
|
|
|
prng := rand.New(rand.NewSource(int64(count))) |
|
|
|
|
// This spongeDb is used to check the sequence of disk-db-writes
|
|
|
|
|
s := &spongeDb{sponge: sha3.NewLegacyKeccak256(), id: "a"} |
|
|
|
|
db := NewDatabase(rawdb.NewDatabase(s), nil) |
|
|
|
|
s := &spongeDb{ |
|
|
|
|
sponge: sha3.NewLegacyKeccak256(), |
|
|
|
|
id: "a", |
|
|
|
|
values: make(map[string]string), |
|
|
|
|
} |
|
|
|
|
db := newTestDatabase(rawdb.NewDatabase(s), rawdb.HashScheme) |
|
|
|
|
trie := NewEmpty(db) |
|
|
|
|
// Another sponge is used for the stacktrie commits
|
|
|
|
|
stackTrieSponge := &spongeDb{sponge: sha3.NewLegacyKeccak256(), id: "b"} |
|
|
|
|
|
|
|
|
|
// Another sponge is used for the stacktrie commits
|
|
|
|
|
stackTrieSponge := &spongeDb{ |
|
|
|
|
sponge: sha3.NewLegacyKeccak256(), |
|
|
|
|
id: "b", |
|
|
|
|
values: make(map[string]string), |
|
|
|
|
} |
|
|
|
|
options := NewStackTrieOptions() |
|
|
|
|
options = options.WithWriter(func(path []byte, hash common.Hash, blob []byte) { |
|
|
|
|
rawdb.WriteTrieNode(stackTrieSponge, common.Hash{}, path, hash, blob, db.Scheme()) |
|
|
|
|
}) |
|
|
|
|
stTrie := NewStackTrie(options) |
|
|
|
|
|
|
|
|
|
// Fill the trie with elements
|
|
|
|
|
for i := 0; i < count; i++ { |
|
|
|
|
// For the stack trie, we need to do inserts in proper order
|
|
|
|
@ -960,13 +988,16 @@ func TestCommitSequenceStackTrie(t *testing.T) { |
|
|
|
|
// Flush trie -> database
|
|
|
|
|
root, nodes, _ := trie.Commit(false) |
|
|
|
|
// Flush memdb -> disk (sponge)
|
|
|
|
|
db.Update(root, types.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil) |
|
|
|
|
db.Commit(root, false) |
|
|
|
|
db.Update(root, types.EmptyRootHash, trienode.NewWithNodeSet(nodes)) |
|
|
|
|
db.Commit(root) |
|
|
|
|
s.Flush() |
|
|
|
|
|
|
|
|
|
// And flush stacktrie -> disk
|
|
|
|
|
stRoot := stTrie.Commit() |
|
|
|
|
if stRoot != root { |
|
|
|
|
t.Fatalf("root wrong, got %x exp %x", stRoot, root) |
|
|
|
|
} |
|
|
|
|
stackTrieSponge.Flush() |
|
|
|
|
if got, exp := stackTrieSponge.sponge.Sum(nil), s.sponge.Sum(nil); !bytes.Equal(got, exp) { |
|
|
|
|
// Show the journal
|
|
|
|
|
t.Logf("Expected:") |
|
|
|
@ -989,34 +1020,47 @@ func TestCommitSequenceStackTrie(t *testing.T) { |
|
|
|
|
// that even a small trie which contains a leaf will have an extension making it
|
|
|
|
|
// not fit into 32 bytes, rlp-encoded. However, it's still the correct thing to do.
|
|
|
|
|
func TestCommitSequenceSmallRoot(t *testing.T) { |
|
|
|
|
s := &spongeDb{sponge: sha3.NewLegacyKeccak256(), id: "a"} |
|
|
|
|
db := NewDatabase(rawdb.NewDatabase(s), nil) |
|
|
|
|
s := &spongeDb{ |
|
|
|
|
sponge: sha3.NewLegacyKeccak256(), |
|
|
|
|
id: "a", |
|
|
|
|
values: make(map[string]string), |
|
|
|
|
} |
|
|
|
|
db := newTestDatabase(rawdb.NewDatabase(s), rawdb.HashScheme) |
|
|
|
|
trie := NewEmpty(db) |
|
|
|
|
// Another sponge is used for the stacktrie commits
|
|
|
|
|
stackTrieSponge := &spongeDb{sponge: sha3.NewLegacyKeccak256(), id: "b"} |
|
|
|
|
|
|
|
|
|
// Another sponge is used for the stacktrie commits
|
|
|
|
|
stackTrieSponge := &spongeDb{ |
|
|
|
|
sponge: sha3.NewLegacyKeccak256(), |
|
|
|
|
id: "b", |
|
|
|
|
values: make(map[string]string), |
|
|
|
|
} |
|
|
|
|
options := NewStackTrieOptions() |
|
|
|
|
options = options.WithWriter(func(path []byte, hash common.Hash, blob []byte) { |
|
|
|
|
rawdb.WriteTrieNode(stackTrieSponge, common.Hash{}, path, hash, blob, db.Scheme()) |
|
|
|
|
}) |
|
|
|
|
stTrie := NewStackTrie(options) |
|
|
|
|
|
|
|
|
|
// Add a single small-element to the trie(s)
|
|
|
|
|
key := make([]byte, 5) |
|
|
|
|
key[0] = 1 |
|
|
|
|
trie.Update(key, []byte{0x1}) |
|
|
|
|
stTrie.Update(key, []byte{0x1}) |
|
|
|
|
|
|
|
|
|
// Flush trie -> database
|
|
|
|
|
root, nodes, _ := trie.Commit(false) |
|
|
|
|
// Flush memdb -> disk (sponge)
|
|
|
|
|
db.Update(root, types.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil) |
|
|
|
|
db.Commit(root, false) |
|
|
|
|
db.Update(root, types.EmptyRootHash, trienode.NewWithNodeSet(nodes)) |
|
|
|
|
db.Commit(root) |
|
|
|
|
|
|
|
|
|
// And flush stacktrie -> disk
|
|
|
|
|
stRoot := stTrie.Commit() |
|
|
|
|
if stRoot != root { |
|
|
|
|
t.Fatalf("root wrong, got %x exp %x", stRoot, root) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
t.Logf("root: %x\n", stRoot) |
|
|
|
|
|
|
|
|
|
s.Flush() |
|
|
|
|
stackTrieSponge.Flush() |
|
|
|
|
if got, exp := stackTrieSponge.sponge.Sum(nil), s.sponge.Sum(nil); !bytes.Equal(got, exp) { |
|
|
|
|
t.Fatalf("test, disk write sequence wrong:\ngot %x exp %x\n", got, exp) |
|
|
|
|
} |
|
|
|
@ -1067,7 +1111,7 @@ func BenchmarkHashFixedSize(b *testing.B) { |
|
|
|
|
|
|
|
|
|
func benchmarkHashFixedSize(b *testing.B, addresses [][20]byte, accounts [][]byte) { |
|
|
|
|
b.ReportAllocs() |
|
|
|
|
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase(), nil)) |
|
|
|
|
trie := NewEmpty(newTestDatabase(rawdb.NewMemoryDatabase(), rawdb.HashScheme)) |
|
|
|
|
for i := 0; i < len(addresses); i++ { |
|
|
|
|
trie.MustUpdate(crypto.Keccak256(addresses[i][:]), accounts[i]) |
|
|
|
|
} |
|
|
|
@ -1118,7 +1162,7 @@ func BenchmarkCommitAfterHashFixedSize(b *testing.B) { |
|
|
|
|
|
|
|
|
|
func benchmarkCommitAfterHashFixedSize(b *testing.B, addresses [][20]byte, accounts [][]byte) { |
|
|
|
|
b.ReportAllocs() |
|
|
|
|
trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase(), nil)) |
|
|
|
|
trie := NewEmpty(newTestDatabase(rawdb.NewMemoryDatabase(), rawdb.HashScheme)) |
|
|
|
|
for i := 0; i < len(addresses); i++ { |
|
|
|
|
trie.MustUpdate(crypto.Keccak256(addresses[i][:]), accounts[i]) |
|
|
|
|
} |
|
|
|
@ -1129,60 +1173,6 @@ func benchmarkCommitAfterHashFixedSize(b *testing.B, addresses [][20]byte, accou |
|
|
|
|
b.StopTimer() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func BenchmarkDerefRootFixedSize(b *testing.B) { |
|
|
|
|
b.Run("10", func(b *testing.B) { |
|
|
|
|
b.StopTimer() |
|
|
|
|
acc, add := makeAccounts(20) |
|
|
|
|
for i := 0; i < b.N; i++ { |
|
|
|
|
benchmarkDerefRootFixedSize(b, acc, add) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
b.Run("100", func(b *testing.B) { |
|
|
|
|
b.StopTimer() |
|
|
|
|
acc, add := makeAccounts(100) |
|
|
|
|
for i := 0; i < b.N; i++ { |
|
|
|
|
benchmarkDerefRootFixedSize(b, acc, add) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
b.Run("1K", func(b *testing.B) { |
|
|
|
|
b.StopTimer() |
|
|
|
|
acc, add := makeAccounts(1000) |
|
|
|
|
for i := 0; i < b.N; i++ { |
|
|
|
|
benchmarkDerefRootFixedSize(b, acc, add) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
b.Run("10K", func(b *testing.B) { |
|
|
|
|
b.StopTimer() |
|
|
|
|
acc, add := makeAccounts(10000) |
|
|
|
|
for i := 0; i < b.N; i++ { |
|
|
|
|
benchmarkDerefRootFixedSize(b, acc, add) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
b.Run("100K", func(b *testing.B) { |
|
|
|
|
b.StopTimer() |
|
|
|
|
acc, add := makeAccounts(100000) |
|
|
|
|
for i := 0; i < b.N; i++ { |
|
|
|
|
benchmarkDerefRootFixedSize(b, acc, add) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func benchmarkDerefRootFixedSize(b *testing.B, addresses [][20]byte, accounts [][]byte) { |
|
|
|
|
b.ReportAllocs() |
|
|
|
|
triedb := NewDatabase(rawdb.NewMemoryDatabase(), nil) |
|
|
|
|
trie := NewEmpty(triedb) |
|
|
|
|
for i := 0; i < len(addresses); i++ { |
|
|
|
|
trie.MustUpdate(crypto.Keccak256(addresses[i][:]), accounts[i]) |
|
|
|
|
} |
|
|
|
|
h := trie.Hash() |
|
|
|
|
root, nodes, _ := trie.Commit(false) |
|
|
|
|
triedb.Update(root, types.EmptyRootHash, 0, trienode.NewWithNodeSet(nodes), nil) |
|
|
|
|
b.StartTimer() |
|
|
|
|
triedb.Dereference(h) |
|
|
|
|
b.StopTimer() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func getString(trie *Trie, k string) []byte { |
|
|
|
|
return trie.MustGet([]byte(k)) |
|
|
|
|
} |
|
|
|
|