|
|
@ -30,95 +30,50 @@ import ( |
|
|
|
func TestWipe(t *testing.T) { |
|
|
|
func TestWipe(t *testing.T) { |
|
|
|
// Create a database with some random snapshot data
|
|
|
|
// Create a database with some random snapshot data
|
|
|
|
db := memorydb.New() |
|
|
|
db := memorydb.New() |
|
|
|
|
|
|
|
|
|
|
|
for i := 0; i < 128; i++ { |
|
|
|
for i := 0; i < 128; i++ { |
|
|
|
account := randomHash() |
|
|
|
rawdb.WriteAccountSnapshot(db, randomHash(), randomHash().Bytes()) |
|
|
|
rawdb.WriteAccountSnapshot(db, account, randomHash().Bytes()) |
|
|
|
|
|
|
|
for j := 0; j < 1024; j++ { |
|
|
|
|
|
|
|
rawdb.WriteStorageSnapshot(db, account, randomHash(), randomHash().Bytes()) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
rawdb.WriteSnapshotRoot(db, randomHash()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Add some random non-snapshot data too to make wiping harder
|
|
|
|
// Add some random non-snapshot data too to make wiping harder
|
|
|
|
for i := 0; i < 65536; i++ { |
|
|
|
for i := 0; i < 500; i++ { |
|
|
|
// Generate a key that's the wrong length for a state snapshot item
|
|
|
|
// Generate keys with wrong length for a state snapshot item
|
|
|
|
var keysize int |
|
|
|
keysuffix := make([]byte, 31) |
|
|
|
for keysize == 0 || keysize == 32 || keysize == 64 { |
|
|
|
|
|
|
|
keysize = 8 + rand.Intn(64) // +8 to ensure we will "never" randomize duplicates
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// Randomize the suffix, dedup and inject it under the snapshot namespace
|
|
|
|
|
|
|
|
keysuffix := make([]byte, keysize) |
|
|
|
|
|
|
|
rand.Read(keysuffix) |
|
|
|
rand.Read(keysuffix) |
|
|
|
|
|
|
|
db.Put(append(rawdb.SnapshotAccountPrefix, keysuffix...), randomHash().Bytes()) |
|
|
|
if rand.Int31n(2) == 0 { |
|
|
|
keysuffix = make([]byte, 33) |
|
|
|
db.Put(append(rawdb.SnapshotAccountPrefix, keysuffix...), randomHash().Bytes()) |
|
|
|
rand.Read(keysuffix) |
|
|
|
} else { |
|
|
|
db.Put(append(rawdb.SnapshotAccountPrefix, keysuffix...), randomHash().Bytes()) |
|
|
|
db.Put(append(rawdb.SnapshotStoragePrefix, keysuffix...), randomHash().Bytes()) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
// Sanity check that all the keys are present
|
|
|
|
count := func() (items int) { |
|
|
|
var items int |
|
|
|
it := db.NewIterator(rawdb.SnapshotAccountPrefix, nil) |
|
|
|
|
|
|
|
defer it.Release() |
|
|
|
it := db.NewIterator(rawdb.SnapshotAccountPrefix, nil) |
|
|
|
for it.Next() { |
|
|
|
defer it.Release() |
|
|
|
if len(it.Key()) == len(rawdb.SnapshotAccountPrefix)+common.HashLength { |
|
|
|
|
|
|
|
items++ |
|
|
|
for it.Next() { |
|
|
|
} |
|
|
|
key := it.Key() |
|
|
|
|
|
|
|
if len(key) == len(rawdb.SnapshotAccountPrefix)+common.HashLength { |
|
|
|
|
|
|
|
items++ |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return items |
|
|
|
} |
|
|
|
} |
|
|
|
it = db.NewIterator(rawdb.SnapshotStoragePrefix, nil) |
|
|
|
// Sanity check that all the keys are present
|
|
|
|
defer it.Release() |
|
|
|
if items := count(); items != 128 { |
|
|
|
|
|
|
|
t.Fatalf("snapshot size mismatch: have %d, want %d", items, 128) |
|
|
|
for it.Next() { |
|
|
|
|
|
|
|
key := it.Key() |
|
|
|
|
|
|
|
if len(key) == len(rawdb.SnapshotStoragePrefix)+2*common.HashLength { |
|
|
|
|
|
|
|
items++ |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if items != 128+128*1024 { |
|
|
|
|
|
|
|
t.Fatalf("snapshot size mismatch: have %d, want %d", items, 128+128*1024) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
if hash := rawdb.ReadSnapshotRoot(db); hash == (common.Hash{}) { |
|
|
|
// Wipe the accounts
|
|
|
|
t.Errorf("snapshot block marker mismatch: have %#x, want <not-nil>", hash) |
|
|
|
if err := wipeKeyRange(db, "accounts", rawdb.SnapshotAccountPrefix, nil, nil, |
|
|
|
|
|
|
|
len(rawdb.SnapshotAccountPrefix)+common.HashLength, snapWipedAccountMeter, true); err != nil { |
|
|
|
|
|
|
|
t.Fatal(err) |
|
|
|
} |
|
|
|
} |
|
|
|
// Wipe all snapshot entries from the database
|
|
|
|
|
|
|
|
<-wipeSnapshot(db, true) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Iterate over the database end ensure no snapshot information remains
|
|
|
|
// Iterate over the database end ensure no snapshot information remains
|
|
|
|
it = db.NewIterator(rawdb.SnapshotAccountPrefix, nil) |
|
|
|
if items := count(); items != 0 { |
|
|
|
defer it.Release() |
|
|
|
t.Fatalf("snapshot size mismatch: have %d, want %d", items, 0) |
|
|
|
|
|
|
|
|
|
|
|
for it.Next() { |
|
|
|
|
|
|
|
key := it.Key() |
|
|
|
|
|
|
|
if len(key) == len(rawdb.SnapshotAccountPrefix)+common.HashLength { |
|
|
|
|
|
|
|
t.Errorf("snapshot entry remained after wipe: %x", key) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
it = db.NewIterator(rawdb.SnapshotStoragePrefix, nil) |
|
|
|
|
|
|
|
defer it.Release() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for it.Next() { |
|
|
|
|
|
|
|
key := it.Key() |
|
|
|
|
|
|
|
if len(key) == len(rawdb.SnapshotStoragePrefix)+2*common.HashLength { |
|
|
|
|
|
|
|
t.Errorf("snapshot entry remained after wipe: %x", key) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if hash := rawdb.ReadSnapshotRoot(db); hash != (common.Hash{}) { |
|
|
|
|
|
|
|
t.Errorf("snapshot block marker remained after wipe: %#x", hash) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
// Iterate over the database and ensure miscellaneous items are present
|
|
|
|
// Iterate over the database and ensure miscellaneous items are present
|
|
|
|
items = 0 |
|
|
|
items := 0 |
|
|
|
|
|
|
|
it := db.NewIterator(nil, nil) |
|
|
|
it = db.NewIterator(nil, nil) |
|
|
|
|
|
|
|
defer it.Release() |
|
|
|
defer it.Release() |
|
|
|
|
|
|
|
|
|
|
|
for it.Next() { |
|
|
|
for it.Next() { |
|
|
|
items++ |
|
|
|
items++ |
|
|
|
} |
|
|
|
} |
|
|
|
if items != 65536 { |
|
|
|
if items != 1000 { |
|
|
|
t.Fatalf("misc item count mismatch: have %d, want %d", items, 65536) |
|
|
|
t.Fatalf("misc item count mismatch: have %d, want %d", items, 1000) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|