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