mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-26 06:36:43 +00:00
triedb/pathdb: add tests
This commit is contained in:
parent
fd40142558
commit
2ac16fcb9e
1 changed files with 76 additions and 17 deletions
|
|
@ -816,7 +816,8 @@ func testAccountIteratorDeletions(t *testing.T, newIterator func(db *Database, r
|
||||||
config := &Config{
|
config := &Config{
|
||||||
NoAsyncGeneration: true,
|
NoAsyncGeneration: true,
|
||||||
}
|
}
|
||||||
db := New(rawdb.NewMemoryDatabase(), config, false)
|
memoryDB := rawdb.NewMemoryDatabase()
|
||||||
|
db := New(memoryDB, config, false)
|
||||||
|
|
||||||
// Stack three diff layers on top with various overlaps
|
// Stack three diff layers on top with various overlaps
|
||||||
db.Update(common.HexToHash("0x02"), types.EmptyRootHash, 1, trienode.NewMergedNodeSet(),
|
db.Update(common.HexToHash("0x02"), types.EmptyRootHash, 1, trienode.NewMergedNodeSet(),
|
||||||
|
|
@ -831,31 +832,55 @@ func testAccountIteratorDeletions(t *testing.T, newIterator func(db *Database, r
|
||||||
db.Update(common.HexToHash("0x04"), common.HexToHash("0x03"), 3, trienode.NewMergedNodeSet(),
|
db.Update(common.HexToHash("0x04"), common.HexToHash("0x03"), 3, trienode.NewMergedNodeSet(),
|
||||||
NewStateSetWithOrigin(randomAccountSet("0x33", "0x44", "0x55"), nil, nil, nil, false))
|
NewStateSetWithOrigin(randomAccountSet("0x33", "0x44", "0x55"), nil, nil, nil, false))
|
||||||
|
|
||||||
// The output should be 11,33,44,55
|
verify := func() {
|
||||||
it := newIterator(db, common.HexToHash("0x04"), common.Hash{})
|
// The output should be 11,33,44,55
|
||||||
// Do a quick check
|
it := newIterator(db, common.HexToHash("0x04"), common.Hash{})
|
||||||
verifyIterator(t, 4, it, verifyAccount)
|
// Do a quick check
|
||||||
it.Release()
|
verifyIterator(t, 4, it, verifyAccount)
|
||||||
|
it.Release()
|
||||||
|
|
||||||
// And a more detailed verification that we indeed do not see '0x22'
|
// And a more detailed verification that we indeed do not see '0x22'
|
||||||
it = newIterator(db, common.HexToHash("0x04"), common.Hash{})
|
it = newIterator(db, common.HexToHash("0x04"), common.Hash{})
|
||||||
defer it.Release()
|
defer it.Release()
|
||||||
for it.Next() {
|
for it.Next() {
|
||||||
hash := it.Hash()
|
hash := it.Hash()
|
||||||
if it.Account() == nil {
|
if it.Account() == nil {
|
||||||
t.Errorf("iterator returned nil-value for hash %x", hash)
|
t.Errorf("iterator returned nil-value for hash %x", hash)
|
||||||
}
|
}
|
||||||
if hash == deleted {
|
if hash == deleted {
|
||||||
t.Errorf("expected deleted elem %x to not be returned by iterator", deleted)
|
t.Errorf("expected deleted elem %x to not be returned by iterator", deleted)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
verify()
|
||||||
|
|
||||||
|
if err := db.Journal(common.HexToHash("0x04")); err != nil {
|
||||||
|
t.Fatalf("Failed to journal the database, %v", err)
|
||||||
|
}
|
||||||
|
if err := db.Close(); err != nil {
|
||||||
|
t.Fatalf("Failed to close the database, %v", err)
|
||||||
|
}
|
||||||
|
db = New(memoryDB, config, false)
|
||||||
|
|
||||||
|
verify()
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestStorageIteratorDeletions(t *testing.T) {
|
func TestStorageIteratorDeletions(t *testing.T) {
|
||||||
config := &Config{
|
config := &Config{
|
||||||
NoAsyncGeneration: true,
|
NoAsyncGeneration: true,
|
||||||
}
|
}
|
||||||
db := New(rawdb.NewMemoryDatabase(), config, false)
|
memoryDB := rawdb.NewMemoryDatabase()
|
||||||
|
db := New(memoryDB, config, false)
|
||||||
|
|
||||||
|
restart := func(head common.Hash) {
|
||||||
|
if err := db.Journal(head); err != nil {
|
||||||
|
t.Fatalf("Failed to journal the database, %v", err)
|
||||||
|
}
|
||||||
|
if err := db.Close(); err != nil {
|
||||||
|
t.Fatalf("Failed to close the database, %v", err)
|
||||||
|
}
|
||||||
|
db = New(memoryDB, config, false)
|
||||||
|
}
|
||||||
|
|
||||||
// Stack three diff layers on top with various overlaps
|
// Stack three diff layers on top with various overlaps
|
||||||
db.Update(common.HexToHash("0x02"), types.EmptyRootHash, 1, trienode.NewMergedNodeSet(),
|
db.Update(common.HexToHash("0x02"), types.EmptyRootHash, 1, trienode.NewMergedNodeSet(),
|
||||||
|
|
@ -874,6 +899,19 @@ func TestStorageIteratorDeletions(t *testing.T) {
|
||||||
verifyIterator(t, 3, it, verifyStorage)
|
verifyIterator(t, 3, it, verifyStorage)
|
||||||
it.Release()
|
it.Release()
|
||||||
|
|
||||||
|
// Ensure the iteration result aligns after the database restart
|
||||||
|
restart(common.HexToHash("0x03"))
|
||||||
|
|
||||||
|
// The output should be 02,04,05,06
|
||||||
|
it, _ = db.StorageIterator(common.HexToHash("0x03"), common.HexToHash("0xaa"), common.Hash{})
|
||||||
|
verifyIterator(t, 4, it, verifyStorage)
|
||||||
|
it.Release()
|
||||||
|
|
||||||
|
// The output should be 04,05,06
|
||||||
|
it, _ = db.StorageIterator(common.HexToHash("0x03"), common.HexToHash("0xaa"), common.HexToHash("0x03"))
|
||||||
|
verifyIterator(t, 3, it, verifyStorage)
|
||||||
|
it.Release()
|
||||||
|
|
||||||
// Destruct the whole storage
|
// Destruct the whole storage
|
||||||
accounts := map[common.Hash][]byte{
|
accounts := map[common.Hash][]byte{
|
||||||
common.HexToHash("0xaa"): nil,
|
common.HexToHash("0xaa"): nil,
|
||||||
|
|
@ -885,6 +923,12 @@ func TestStorageIteratorDeletions(t *testing.T) {
|
||||||
verifyIterator(t, 0, it, verifyStorage)
|
verifyIterator(t, 0, it, verifyStorage)
|
||||||
it.Release()
|
it.Release()
|
||||||
|
|
||||||
|
// Ensure the iteration result aligns after the database restart
|
||||||
|
restart(common.HexToHash("0x04"))
|
||||||
|
it, _ = db.StorageIterator(common.HexToHash("0x04"), common.HexToHash("0xaa"), common.Hash{})
|
||||||
|
verifyIterator(t, 0, it, verifyStorage)
|
||||||
|
it.Release()
|
||||||
|
|
||||||
// Re-insert the slots of the same account
|
// Re-insert the slots of the same account
|
||||||
db.Update(common.HexToHash("0x05"), common.HexToHash("0x04"), 4, trienode.NewMergedNodeSet(),
|
db.Update(common.HexToHash("0x05"), common.HexToHash("0x04"), 4, trienode.NewMergedNodeSet(),
|
||||||
NewStateSetWithOrigin(randomAccountSet("0xaa"), randomStorageSet([]string{"0xaa"}, [][]string{{"0x07", "0x08", "0x09"}}, nil), nil, nil, false))
|
NewStateSetWithOrigin(randomAccountSet("0xaa"), randomStorageSet([]string{"0xaa"}, [][]string{{"0x07", "0x08", "0x09"}}, nil), nil, nil, false))
|
||||||
|
|
@ -894,6 +938,14 @@ func TestStorageIteratorDeletions(t *testing.T) {
|
||||||
verifyIterator(t, 3, it, verifyStorage)
|
verifyIterator(t, 3, it, verifyStorage)
|
||||||
it.Release()
|
it.Release()
|
||||||
|
|
||||||
|
// Ensure the iteration result aligns after the database restart
|
||||||
|
restart(common.HexToHash("0x05"))
|
||||||
|
|
||||||
|
// The output should be 07,08,09
|
||||||
|
it, _ = db.StorageIterator(common.HexToHash("0x05"), common.HexToHash("0xaa"), common.Hash{})
|
||||||
|
verifyIterator(t, 3, it, verifyStorage)
|
||||||
|
it.Release()
|
||||||
|
|
||||||
// Destruct the whole storage but re-create the account in the same layer
|
// Destruct the whole storage but re-create the account in the same layer
|
||||||
db.Update(common.HexToHash("0x06"), common.HexToHash("0x05"), 5, trienode.NewMergedNodeSet(),
|
db.Update(common.HexToHash("0x06"), common.HexToHash("0x05"), 5, trienode.NewMergedNodeSet(),
|
||||||
NewStateSetWithOrigin(randomAccountSet("0xaa"), randomStorageSet([]string{"0xaa"}, [][]string{{"0x11", "0x12"}}, [][]string{{"0x07", "0x08", "0x09"}}), nil, nil, false))
|
NewStateSetWithOrigin(randomAccountSet("0xaa"), randomStorageSet([]string{"0xaa"}, [][]string{{"0x11", "0x12"}}, [][]string{{"0x07", "0x08", "0x09"}}), nil, nil, false))
|
||||||
|
|
@ -903,6 +955,13 @@ func TestStorageIteratorDeletions(t *testing.T) {
|
||||||
it.Release()
|
it.Release()
|
||||||
|
|
||||||
verifyIterator(t, 2, db.tree.get(common.HexToHash("0x06")).(*diffLayer).newBinaryStorageIterator(common.HexToHash("0xaa"), common.Hash{}), verifyStorage)
|
verifyIterator(t, 2, db.tree.get(common.HexToHash("0x06")).(*diffLayer).newBinaryStorageIterator(common.HexToHash("0xaa"), common.Hash{}), verifyStorage)
|
||||||
|
|
||||||
|
// Ensure the iteration result aligns after the database restart
|
||||||
|
restart(common.HexToHash("0x06"))
|
||||||
|
it, _ = db.StorageIterator(common.HexToHash("0x06"), common.HexToHash("0xaa"), common.Hash{})
|
||||||
|
verifyIterator(t, 2, it, verifyStorage) // The output should be 11,12
|
||||||
|
it.Release()
|
||||||
|
verifyIterator(t, 2, db.tree.get(common.HexToHash("0x06")).(*diffLayer).newBinaryStorageIterator(common.HexToHash("0xaa"), common.Hash{}), verifyStorage)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestStaleIterator tests if the iterator could correctly terminate the iteration
|
// TestStaleIterator tests if the iterator could correctly terminate the iteration
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue