github.com/core-coin/go-core/v2@v2.1.9/core/state/snapshot/wipe.go (about)

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