gitlab.com/flarenetwork/coreth@v0.1.1/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/ethereum/go-ethereum/common"
    34  	"github.com/ethereum/go-ethereum/ethdb"
    35  	"github.com/ethereum/go-ethereum/log"
    36  	"gitlab.com/flarenetwork/coreth/core/rawdb"
    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).
    42  func wipeSnapshot(db ethdb.KeyValueStore, full bool) chan struct{} {
    43  	// Wipe the snapshot root marker synchronously
    44  	if full {
    45  		rawdb.DeleteSnapshotBlockHash(db)
    46  		rawdb.DeleteSnapshotRoot(db)
    47  	}
    48  	// Wipe everything else asynchronously
    49  	wiper := make(chan struct{}, 1)
    50  	go func() {
    51  		if err := wipeContent(db); err != nil {
    52  			log.Error("Failed to wipe state snapshot", "err", err) // Database close will trigger this
    53  			return
    54  		}
    55  		close(wiper)
    56  	}()
    57  	return wiper
    58  }
    59  
    60  // wipeContent iterates over the entire key-value database and deletes all the
    61  // data associated with the snapshot (accounts, storage), but not the root hash
    62  // as the wiper is meant to run on a background thread but the root needs to be
    63  // removed in sync to avoid data races.
    64  func wipeContent(db ethdb.KeyValueStore) error {
    65  	if err := wipeKeyRange(db, "accounts", rawdb.SnapshotAccountPrefix, nil, nil, len(rawdb.SnapshotAccountPrefix)+common.HashLength, true); err != nil {
    66  		return err
    67  	}
    68  	if err := wipeKeyRange(db, "storage", rawdb.SnapshotStoragePrefix, nil, nil, len(rawdb.SnapshotStoragePrefix)+2*common.HashLength, true); err != nil {
    69  		return err
    70  	}
    71  
    72  	return nil
    73  }
    74  
    75  // wipeKeyRange deletes a range of keys from the database starting with prefix
    76  // and having a specific total key length. The start and limit is optional for
    77  // specifying a particular key range for deletion.
    78  //
    79  // Origin is included for wiping and limit is excluded if they are specified.
    80  func wipeKeyRange(db ethdb.KeyValueStore, kind string, prefix []byte, origin []byte, limit []byte, keylen int, report bool) error {
    81  	// Batch deletions together to avoid holding an iterator for too long
    82  	var (
    83  		batch = db.NewBatch()
    84  		items int
    85  	)
    86  	// Iterate over the key-range and delete all of them
    87  	start, logged := time.Now(), time.Now()
    88  
    89  	it := db.NewIterator(prefix, origin)
    90  	var stop []byte
    91  	if limit != nil {
    92  		stop = append(prefix, limit...)
    93  	}
    94  	for it.Next() {
    95  		// Skip any keys with the correct prefix but wrong length (trie nodes)
    96  		key := it.Key()
    97  		if !bytes.HasPrefix(key, prefix) {
    98  			break
    99  		}
   100  		if len(key) != keylen {
   101  			continue
   102  		}
   103  		if stop != nil && bytes.Compare(key, stop) >= 0 {
   104  			break
   105  		}
   106  		// Delete the key and periodically recreate the batch and iterator
   107  		batch.Delete(key)
   108  		items++
   109  
   110  		if items%10000 == 0 {
   111  			// Batch too large (or iterator too long lived, flush and recreate)
   112  			it.Release()
   113  			if err := batch.Write(); err != nil {
   114  				return err
   115  			}
   116  			batch.Reset()
   117  			seekPos := key[len(prefix):]
   118  			it = db.NewIterator(prefix, seekPos)
   119  
   120  			if time.Since(logged) > 8*time.Second && report {
   121  				log.Info("Deleting state snapshot leftovers", "kind", kind, "wiped", items, "elapsed", common.PrettyDuration(time.Since(start)))
   122  				logged = time.Now()
   123  			}
   124  		}
   125  	}
   126  	it.Release()
   127  	if err := batch.Write(); err != nil {
   128  		return err
   129  	}
   130  	if report {
   131  		log.Info("Deleted state snapshot leftovers", "kind", kind, "wiped", items, "elapsed", common.PrettyDuration(time.Since(start)))
   132  	}
   133  	return nil
   134  }