generate_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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.WriteSnapshotBlock(db, 123, 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. db.Put(append(rawdb.StateSnapshotPrefix, keysuffix...), randomHash().Bytes())
  56. }
  57. // Sanity check that all the keys are present
  58. var items int
  59. it := db.NewIteratorWithPrefix(rawdb.StateSnapshotPrefix)
  60. defer it.Release()
  61. for it.Next() {
  62. key := it.Key()
  63. if len(key) == len(rawdb.StateSnapshotPrefix)+32 || len(key) == len(rawdb.StateSnapshotPrefix)+64 {
  64. items++
  65. }
  66. }
  67. if items != 128+128*1024 {
  68. t.Fatalf("snapshot size mismatch: have %d, want %d", items, 128+128*1024)
  69. }
  70. if number, hash := rawdb.ReadSnapshotBlock(db); number != 123 || hash == (common.Hash{}) {
  71. t.Errorf("snapshot block marker mismatch: have #%d [%#x], want #%d [<not-nil>]", number, hash, 123)
  72. }
  73. // Wipe all snapshot entries from the database
  74. if err := wipeSnapshot(db); err != nil {
  75. t.Fatalf("failed to wipe snapshot: %v", err)
  76. }
  77. // Iterate over the database end ensure no snapshot information remains
  78. it = db.NewIteratorWithPrefix(rawdb.StateSnapshotPrefix)
  79. defer it.Release()
  80. for it.Next() {
  81. key := it.Key()
  82. if len(key) == len(rawdb.StateSnapshotPrefix)+32 || len(key) == len(rawdb.StateSnapshotPrefix)+64 {
  83. t.Errorf("snapshot entry remained after wipe: %x", key)
  84. }
  85. }
  86. if number, hash := rawdb.ReadSnapshotBlock(db); number != 0 || hash != (common.Hash{}) {
  87. t.Errorf("snapshot block marker remained after wipe: #%d [%#x]", number, hash)
  88. }
  89. // Iterate over the database and ensure miscellaneous items are present
  90. items = 0
  91. it = db.NewIterator()
  92. defer it.Release()
  93. for it.Next() {
  94. items++
  95. }
  96. if items != 65536 {
  97. t.Fatalf("misc item count mismatch: have %d, want %d", items, 65536)
  98. }
  99. }