wipe_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2019 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package snapshot
  17. import (
  18. "math/rand"
  19. "testing"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/core/rawdb"
  22. "github.com/ethereum/go-ethereum/ethdb/memorydb"
  23. )
  24. // Tests that given a database with random data content, all parts of a snapshot
  25. // can be crrectly wiped without touching anything else.
  26. func TestWipe(t *testing.T) {
  27. // Create a database with some random snapshot data
  28. db := memorydb.New()
  29. for i := 0; i < 128; i++ {
  30. account := randomHash()
  31. rawdb.WriteAccountSnapshot(db, account, randomHash().Bytes())
  32. for j := 0; j < 1024; j++ {
  33. rawdb.WriteStorageSnapshot(db, account, randomHash(), randomHash().Bytes())
  34. }
  35. }
  36. rawdb.WriteSnapshotRoot(db, randomHash())
  37. // Add some random non-snapshot data too to make wiping harder
  38. for i := 0; i < 65536; i++ {
  39. // Generate a key that's the wrong length for a state snapshot item
  40. var keysize int
  41. for keysize == 0 || keysize == 32 || keysize == 64 {
  42. keysize = 8 + rand.Intn(64) // +8 to ensure we will "never" randomize duplicates
  43. }
  44. // Randomize the suffix, dedup and inject it under the snapshot namespace
  45. keysuffix := make([]byte, keysize)
  46. rand.Read(keysuffix)
  47. if rand.Int31n(2) == 0 {
  48. db.Put(append(rawdb.SnapshotAccountPrefix, keysuffix...), randomHash().Bytes())
  49. } else {
  50. db.Put(append(rawdb.SnapshotStoragePrefix, keysuffix...), randomHash().Bytes())
  51. }
  52. }
  53. // Sanity check that all the keys are present
  54. var items int
  55. it := db.NewIterator(rawdb.SnapshotAccountPrefix, nil)
  56. defer it.Release()
  57. for it.Next() {
  58. key := it.Key()
  59. if len(key) == len(rawdb.SnapshotAccountPrefix)+common.HashLength {
  60. items++
  61. }
  62. }
  63. it = db.NewIterator(rawdb.SnapshotStoragePrefix, nil)
  64. defer it.Release()
  65. for it.Next() {
  66. key := it.Key()
  67. if len(key) == len(rawdb.SnapshotStoragePrefix)+2*common.HashLength {
  68. items++
  69. }
  70. }
  71. if items != 128+128*1024 {
  72. t.Fatalf("snapshot size mismatch: have %d, want %d", items, 128+128*1024)
  73. }
  74. if hash := rawdb.ReadSnapshotRoot(db); hash == (common.Hash{}) {
  75. t.Errorf("snapshot block marker mismatch: have %#x, want <not-nil>", hash)
  76. }
  77. // Wipe all snapshot entries from the database
  78. <-wipeSnapshot(db, true)
  79. // Iterate over the database end ensure no snapshot information remains
  80. it = db.NewIterator(rawdb.SnapshotAccountPrefix, nil)
  81. defer it.Release()
  82. for it.Next() {
  83. key := it.Key()
  84. if len(key) == len(rawdb.SnapshotAccountPrefix)+common.HashLength {
  85. t.Errorf("snapshot entry remained after wipe: %x", key)
  86. }
  87. }
  88. it = db.NewIterator(rawdb.SnapshotStoragePrefix, nil)
  89. defer it.Release()
  90. for it.Next() {
  91. key := it.Key()
  92. if len(key) == len(rawdb.SnapshotStoragePrefix)+2*common.HashLength {
  93. t.Errorf("snapshot entry remained after wipe: %x", key)
  94. }
  95. }
  96. if hash := rawdb.ReadSnapshotRoot(db); hash != (common.Hash{}) {
  97. t.Errorf("snapshot block marker remained after wipe: %#x", hash)
  98. }
  99. // Iterate over the database and ensure miscellaneous items are present
  100. items = 0
  101. it = db.NewIterator(nil, nil)
  102. defer it.Release()
  103. for it.Next() {
  104. items++
  105. }
  106. if items != 65536 {
  107. t.Fatalf("misc item count mismatch: have %d, want %d", items, 65536)
  108. }
  109. }