wipe_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. // randomHash generates a random blob of data and returns it as a hash.
  25. func randomHash() common.Hash {
  26. var hash common.Hash
  27. if n, err := rand.Read(hash[:]); n != common.HashLength || err != nil {
  28. panic(err)
  29. }
  30. return hash
  31. }
  32. // Tests that given a database with random data content, all parts of a snapshot
  33. // can be crrectly wiped without touching anything else.
  34. func TestWipe(t *testing.T) {
  35. // Create a database with some random snapshot data
  36. db := memorydb.New()
  37. for i := 0; i < 128; i++ {
  38. account := randomHash()
  39. rawdb.WriteAccountSnapshot(db, account, randomHash().Bytes())
  40. for j := 0; j < 1024; j++ {
  41. rawdb.WriteStorageSnapshot(db, account, randomHash(), randomHash().Bytes())
  42. }
  43. }
  44. rawdb.WriteSnapshotRoot(db, randomHash())
  45. // Add some random non-snapshot data too to make wiping harder
  46. for i := 0; i < 65536; i++ {
  47. // Generate a key that's the wrong length for a state snapshot item
  48. var keysize int
  49. for keysize == 0 || keysize == 32 || keysize == 64 {
  50. keysize = 8 + rand.Intn(64) // +8 to ensure we will "never" randomize duplicates
  51. }
  52. // Randomize the suffix, dedup and inject it under the snapshot namespace
  53. keysuffix := make([]byte, keysize)
  54. rand.Read(keysuffix)
  55. if rand.Int31n(2) == 0 {
  56. db.Put(append(rawdb.SnapshotAccountPrefix, keysuffix...), randomHash().Bytes())
  57. } else {
  58. db.Put(append(rawdb.SnapshotStoragePrefix, keysuffix...), randomHash().Bytes())
  59. }
  60. }
  61. // Sanity check that all the keys are present
  62. var items int
  63. it := db.NewIteratorWithPrefix(rawdb.SnapshotAccountPrefix)
  64. defer it.Release()
  65. for it.Next() {
  66. key := it.Key()
  67. if len(key) == len(rawdb.SnapshotAccountPrefix)+common.HashLength {
  68. items++
  69. }
  70. }
  71. it = db.NewIteratorWithPrefix(rawdb.SnapshotStoragePrefix)
  72. defer it.Release()
  73. for it.Next() {
  74. key := it.Key()
  75. if len(key) == len(rawdb.SnapshotStoragePrefix)+2*common.HashLength {
  76. items++
  77. }
  78. }
  79. if items != 128+128*1024 {
  80. t.Fatalf("snapshot size mismatch: have %d, want %d", items, 128+128*1024)
  81. }
  82. if hash := rawdb.ReadSnapshotRoot(db); hash == (common.Hash{}) {
  83. t.Errorf("snapshot block marker mismatch: have %#x, want <not-nil>", hash)
  84. }
  85. // Wipe all snapshot entries from the database
  86. <-wipeSnapshot(db, true)
  87. // Iterate over the database end ensure no snapshot information remains
  88. it = db.NewIteratorWithPrefix(rawdb.SnapshotAccountPrefix)
  89. defer it.Release()
  90. for it.Next() {
  91. key := it.Key()
  92. if len(key) == len(rawdb.SnapshotAccountPrefix)+common.HashLength {
  93. t.Errorf("snapshot entry remained after wipe: %x", key)
  94. }
  95. }
  96. it = db.NewIteratorWithPrefix(rawdb.SnapshotStoragePrefix)
  97. defer it.Release()
  98. for it.Next() {
  99. key := it.Key()
  100. if len(key) == len(rawdb.SnapshotStoragePrefix)+2*common.HashLength {
  101. t.Errorf("snapshot entry remained after wipe: %x", key)
  102. }
  103. }
  104. if hash := rawdb.ReadSnapshotRoot(db); hash != (common.Hash{}) {
  105. t.Errorf("snapshot block marker remained after wipe: %#x", hash)
  106. }
  107. // Iterate over the database and ensure miscellaneous items are present
  108. items = 0
  109. it = db.NewIterator()
  110. defer it.Release()
  111. for it.Next() {
  112. items++
  113. }
  114. if items != 65536 {
  115. t.Fatalf("misc item count mismatch: have %d, want %d", items, 65536)
  116. }
  117. }