github.com/MetalBlockchain/subnet-evm@v0.4.9/core/state/snapshot/wipe.go (about) 1 // (c) 2019-2020, Ava Labs, Inc. 2 // 3 // This file is a derived work, based on the go-ethereum library whose original 4 // notices appear below. 5 // 6 // It is distributed under a license compatible with the licensing terms of the 7 // original code from which it is derived. 8 // 9 // Much love to the original authors for their work. 10 // ********** 11 // Copyright 2019 The go-ethereum Authors 12 // This file is part of the go-ethereum library. 13 // 14 // The go-ethereum library is free software: you can redistribute it and/or modify 15 // it under the terms of the GNU Lesser General Public License as published by 16 // the Free Software Foundation, either version 3 of the License, or 17 // (at your option) any later version. 18 // 19 // The go-ethereum library is distributed in the hope that it will be useful, 20 // but WITHOUT ANY WARRANTY; without even the implied warranty of 21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 // GNU Lesser General Public License for more details. 23 // 24 // You should have received a copy of the GNU Lesser General Public License 25 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 26 27 package snapshot 28 29 import ( 30 "bytes" 31 "time" 32 33 "github.com/MetalBlockchain/subnet-evm/core/rawdb" 34 "github.com/MetalBlockchain/subnet-evm/ethdb" 35 "github.com/ethereum/go-ethereum/common" 36 "github.com/ethereum/go-ethereum/log" 37 ) 38 39 // WipeSnapshot starts a goroutine to iterate over the entire key-value database 40 // and delete all the data associated with the snapshot (accounts, storage, 41 // metadata). After all is done, the snapshot range of the database is compacted 42 // to free up unused data blocks. 43 func WipeSnapshot(db ethdb.KeyValueStore, full bool) chan struct{} { 44 // Wipe the snapshot root marker synchronously 45 if full { 46 rawdb.DeleteSnapshotBlockHash(db) 47 rawdb.DeleteSnapshotRoot(db) 48 } 49 // Wipe everything else asynchronously 50 wiper := make(chan struct{}, 1) 51 go func() { 52 if err := wipeContent(db); err != nil { 53 log.Error("Failed to wipe state snapshot", "err", err) // Database close will trigger this 54 return 55 } 56 close(wiper) 57 }() 58 return wiper 59 } 60 61 // wipeContent iterates over the entire key-value database and deletes all the 62 // data associated with the snapshot (accounts, storage), but not the root hash 63 // as the wiper is meant to run on a background thread but the root needs to be 64 // removed in sync to avoid data races. After all is done, the snapshot range of 65 // the database is compacted to free up unused data blocks. 66 func wipeContent(db ethdb.KeyValueStore) error { 67 if err := wipeKeyRange(db, "accounts", rawdb.SnapshotAccountPrefix, nil, nil, len(rawdb.SnapshotAccountPrefix)+common.HashLength, true); err != nil { 68 return err 69 } 70 if err := wipeKeyRange(db, "storage", rawdb.SnapshotStoragePrefix, nil, nil, len(rawdb.SnapshotStoragePrefix)+2*common.HashLength, true); err != nil { 71 return err 72 } 73 74 return nil 75 } 76 77 // wipeKeyRange deletes a range of keys from the database starting with prefix 78 // and having a specific total key length. The start and limit is optional for 79 // specifying a particular key range for deletion. 80 // 81 // Origin is included for wiping and limit is excluded if they are specified. 82 func wipeKeyRange(db ethdb.KeyValueStore, kind string, prefix []byte, origin []byte, limit []byte, keylen int, report bool) error { 83 // Batch deletions together to avoid holding an iterator for too long 84 var ( 85 batch = db.NewBatch() 86 items int 87 ) 88 // Iterate over the key-range and delete all of them 89 start, logged := time.Now(), time.Now() 90 91 it := db.NewIterator(prefix, origin) 92 var stop []byte 93 if limit != nil { 94 stop = append(prefix, limit...) 95 } 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 if stop != nil && bytes.Compare(key, stop) >= 0 { 106 break 107 } 108 // Delete the key and periodically recreate the batch and iterator 109 batch.Delete(key) 110 items++ 111 112 if items%10000 == 0 { 113 // Batch too large (or iterator too long lived, flush and recreate) 114 it.Release() 115 if err := batch.Write(); err != nil { 116 return err 117 } 118 batch.Reset() 119 seekPos := key[len(prefix):] 120 it = db.NewIterator(prefix, seekPos) 121 122 if time.Since(logged) > 8*time.Second && report { 123 log.Info("Deleting state snapshot leftovers", "kind", kind, "wiped", items, "elapsed", common.PrettyDuration(time.Since(start))) 124 logged = time.Now() 125 } 126 } 127 } 128 it.Release() 129 if err := batch.Write(); err != nil { 130 return err 131 } 132 if report { 133 log.Info("Deleted state snapshot leftovers", "kind", kind, "wiped", items, "elapsed", common.PrettyDuration(time.Since(start))) 134 } 135 return nil 136 }