wipe.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. "bytes"
  19. "time"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/core/rawdb"
  22. "github.com/ethereum/go-ethereum/ethdb"
  23. "github.com/ethereum/go-ethereum/log"
  24. )
  25. // wipeSnapshot starts a goroutine to iterate over the entire key-value database
  26. // and delete all the data associated with the snapshot (accounts, storage,
  27. // metadata). After all is done, the snapshot range of the database is compacted
  28. // to free up unused data blocks.
  29. func wipeSnapshot(db ethdb.KeyValueStore, full bool) chan struct{} {
  30. // Wipe the snapshot root marker synchronously
  31. if full {
  32. rawdb.DeleteSnapshotRoot(db)
  33. }
  34. // Wipe everything else asynchronously
  35. wiper := make(chan struct{}, 1)
  36. go func() {
  37. if err := wipeContent(db); err != nil {
  38. log.Error("Failed to wipe state snapshot", "err", err) // Database close will trigger this
  39. return
  40. }
  41. close(wiper)
  42. }()
  43. return wiper
  44. }
  45. // wipeContent iterates over the entire key-value database and deletes all the
  46. // data associated with the snapshot (accounts, storage), but not the root hash
  47. // as the wiper is meant to run on a background thread but the root needs to be
  48. // removed in sync to avoid data races. After all is done, the snapshot range of
  49. // the database is compacted to free up unused data blocks.
  50. func wipeContent(db ethdb.KeyValueStore) error {
  51. if err := wipeKeyRange(db, "accounts", rawdb.SnapshotAccountPrefix, len(rawdb.SnapshotAccountPrefix)+common.HashLength); err != nil {
  52. return err
  53. }
  54. if err := wipeKeyRange(db, "storage", rawdb.SnapshotStoragePrefix, len(rawdb.SnapshotStoragePrefix)+2*common.HashLength); err != nil {
  55. return err
  56. }
  57. // Compact the snapshot section of the database to get rid of unused space
  58. start := time.Now()
  59. log.Info("Compacting snapshot account area ")
  60. end := common.CopyBytes(rawdb.SnapshotAccountPrefix)
  61. end[len(end)-1]++
  62. if err := db.Compact(rawdb.SnapshotAccountPrefix, end); err != nil {
  63. return err
  64. }
  65. log.Info("Compacting snapshot storage area ")
  66. end = common.CopyBytes(rawdb.SnapshotStoragePrefix)
  67. end[len(end)-1]++
  68. if err := db.Compact(rawdb.SnapshotStoragePrefix, end); err != nil {
  69. return err
  70. }
  71. log.Info("Compacted snapshot area in database", "elapsed", common.PrettyDuration(time.Since(start)))
  72. return nil
  73. }
  74. // wipeKeyRange deletes a range of keys from the database starting with prefix
  75. // and having a specific total key length.
  76. func wipeKeyRange(db ethdb.KeyValueStore, kind string, prefix []byte, keylen int) error {
  77. // Batch deletions together to avoid holding an iterator for too long
  78. var (
  79. batch = db.NewBatch()
  80. items int
  81. )
  82. // Iterate over the key-range and delete all of them
  83. start, logged := time.Now(), time.Now()
  84. it := db.NewIterator(prefix, nil)
  85. for it.Next() {
  86. // Skip any keys with the correct prefix but wrong lenth (trie nodes)
  87. key := it.Key()
  88. if !bytes.HasPrefix(key, prefix) {
  89. break
  90. }
  91. if len(key) != keylen {
  92. continue
  93. }
  94. // Delete the key and periodically recreate the batch and iterator
  95. batch.Delete(key)
  96. items++
  97. if items%10000 == 0 {
  98. // Batch too large (or iterator too long lived, flush and recreate)
  99. it.Release()
  100. if err := batch.Write(); err != nil {
  101. return err
  102. }
  103. batch.Reset()
  104. seekPos := key[len(prefix):]
  105. it = db.NewIterator(prefix, seekPos)
  106. if time.Since(logged) > 8*time.Second {
  107. log.Info("Deleting state snapshot leftovers", "kind", kind, "wiped", items, "elapsed", common.PrettyDuration(time.Since(start)))
  108. logged = time.Now()
  109. }
  110. }
  111. }
  112. it.Release()
  113. if err := batch.Write(); err != nil {
  114. return err
  115. }
  116. log.Info("Deleted state snapshot leftovers", "kind", kind, "wiped", items, "elapsed", common.PrettyDuration(time.Since(start)))
  117. return nil
  118. }