github.com/core-coin/go-core/v2@v2.1.9/core/rawdb/accessors_snapshot.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 rawdb
    18  
    19  import (
    20  	"encoding/binary"
    21  
    22  	"github.com/core-coin/go-core/v2/xcbdb"
    23  
    24  	"github.com/core-coin/go-core/v2/common"
    25  	"github.com/core-coin/go-core/v2/log"
    26  )
    27  
    28  // ReadSnapshotRoot retrieves the root of the block whose state is contained in
    29  // the persisted snapshot.
    30  func ReadSnapshotRoot(db xcbdb.KeyValueReader) common.Hash {
    31  	data, _ := db.Get(snapshotRootKey)
    32  	if len(data) != common.HashLength {
    33  		return common.Hash{}
    34  	}
    35  	return common.BytesToHash(data)
    36  }
    37  
    38  // WriteSnapshotRoot stores the root of the block whose state is contained in
    39  // the persisted snapshot.
    40  func WriteSnapshotRoot(db xcbdb.KeyValueWriter, root common.Hash) {
    41  	if err := db.Put(snapshotRootKey, root[:]); err != nil {
    42  		log.Crit("Failed to store snapshot root", "err", err)
    43  	}
    44  }
    45  
    46  // DeleteSnapshotRoot deletes the hash of the block whose state is contained in
    47  // the persisted snapshot. Since snapshots are not immutable, this  method can
    48  // be used during updates, so a crash or failure will mark the entire snapshot
    49  // invalid.
    50  func DeleteSnapshotRoot(db xcbdb.KeyValueWriter) {
    51  	if err := db.Delete(snapshotRootKey); err != nil {
    52  		log.Crit("Failed to remove snapshot root", "err", err)
    53  	}
    54  }
    55  
    56  // ReadAccountSnapshot retrieves the snapshot entry of an account trie leaf.
    57  func ReadAccountSnapshot(db xcbdb.KeyValueReader, hash common.Hash) []byte {
    58  	data, _ := db.Get(accountSnapshotKey(hash))
    59  	return data
    60  }
    61  
    62  // WriteAccountSnapshot stores the snapshot entry of an account trie leaf.
    63  func WriteAccountSnapshot(db xcbdb.KeyValueWriter, hash common.Hash, entry []byte) {
    64  	if err := db.Put(accountSnapshotKey(hash), entry); err != nil {
    65  		log.Crit("Failed to store account snapshot", "err", err)
    66  	}
    67  }
    68  
    69  // DeleteAccountSnapshot removes the snapshot entry of an account trie leaf.
    70  func DeleteAccountSnapshot(db xcbdb.KeyValueWriter, hash common.Hash) {
    71  	if err := db.Delete(accountSnapshotKey(hash)); err != nil {
    72  		log.Crit("Failed to delete account snapshot", "err", err)
    73  	}
    74  }
    75  
    76  // ReadStorageSnapshot retrieves the snapshot entry of an storage trie leaf.
    77  func ReadStorageSnapshot(db xcbdb.KeyValueReader, accountHash, storageHash common.Hash) []byte {
    78  	data, _ := db.Get(storageSnapshotKey(accountHash, storageHash))
    79  	return data
    80  }
    81  
    82  // WriteStorageSnapshot stores the snapshot entry of an storage trie leaf.
    83  func WriteStorageSnapshot(db xcbdb.KeyValueWriter, accountHash, storageHash common.Hash, entry []byte) {
    84  	if err := db.Put(storageSnapshotKey(accountHash, storageHash), entry); err != nil {
    85  		log.Crit("Failed to store storage snapshot", "err", err)
    86  	}
    87  }
    88  
    89  // DeleteStorageSnapshot removes the snapshot entry of an storage trie leaf.
    90  func DeleteStorageSnapshot(db xcbdb.KeyValueWriter, accountHash, storageHash common.Hash) {
    91  	if err := db.Delete(storageSnapshotKey(accountHash, storageHash)); err != nil {
    92  		log.Crit("Failed to delete storage snapshot", "err", err)
    93  	}
    94  }
    95  
    96  // IterateStorageSnapshots returns an iterator for walking the entire storage
    97  // space of a specific account.
    98  func IterateStorageSnapshots(db xcbdb.Iteratee, accountHash common.Hash) xcbdb.Iterator {
    99  	return db.NewIterator(storageSnapshotsKey(accountHash), nil)
   100  }
   101  
   102  // ReadSnapshotJournal retrieves the serialized in-memory diff layers saved at
   103  // the last shutdown. The blob is expected to be max a few 10s of megabytes.
   104  func ReadSnapshotJournal(db xcbdb.KeyValueReader) []byte {
   105  	data, _ := db.Get(snapshotJournalKey)
   106  	return data
   107  }
   108  
   109  // WriteSnapshotJournal stores the serialized in-memory diff layers to save at
   110  // shutdown. The blob is expected to be max a few 10s of megabytes.
   111  func WriteSnapshotJournal(db xcbdb.KeyValueWriter, journal []byte) {
   112  	if err := db.Put(snapshotJournalKey, journal); err != nil {
   113  		log.Crit("Failed to store snapshot journal", "err", err)
   114  	}
   115  }
   116  
   117  // DeleteSnapshotJournal deletes the serialized in-memory diff layers saved at
   118  // the last shutdown
   119  func DeleteSnapshotJournal(db xcbdb.KeyValueWriter) {
   120  	if err := db.Delete(snapshotJournalKey); err != nil {
   121  		log.Crit("Failed to remove snapshot journal", "err", err)
   122  	}
   123  }
   124  
   125  // ReadSnapshotGenerator retrieves the serialized snapshot generator saved at
   126  // the last shutdown.
   127  func ReadSnapshotGenerator(db xcbdb.KeyValueReader) []byte {
   128  	data, _ := db.Get(snapshotGeneratorKey)
   129  	return data
   130  }
   131  
   132  // WriteSnapshotGenerator stores the serialized snapshot generator to save at
   133  // shutdown.
   134  func WriteSnapshotGenerator(db xcbdb.KeyValueWriter, generator []byte) {
   135  	if err := db.Put(snapshotGeneratorKey, generator); err != nil {
   136  		log.Crit("Failed to store snapshot generator", "err", err)
   137  	}
   138  }
   139  
   140  // DeleteSnapshotGenerator deletes the serialized snapshot generator saved at
   141  // the last shutdown
   142  func DeleteSnapshotGenerator(db xcbdb.KeyValueWriter) {
   143  	if err := db.Delete(snapshotGeneratorKey); err != nil {
   144  		log.Crit("Failed to remove snapshot generator", "err", err)
   145  	}
   146  }
   147  
   148  // ReadSnapshotRecoveryNumber retrieves the block number of the last persisted
   149  // snapshot layer.
   150  func ReadSnapshotRecoveryNumber(db xcbdb.KeyValueReader) *uint64 {
   151  	data, _ := db.Get(snapshotRecoveryKey)
   152  	if len(data) == 0 {
   153  		return nil
   154  	}
   155  	if len(data) != 8 {
   156  		return nil
   157  	}
   158  	number := binary.BigEndian.Uint64(data)
   159  	return &number
   160  }
   161  
   162  // WriteSnapshotRecoveryNumber stores the block number of the last persisted
   163  // snapshot layer.
   164  func WriteSnapshotRecoveryNumber(db xcbdb.KeyValueWriter, number uint64) {
   165  	var buf [8]byte
   166  	binary.BigEndian.PutUint64(buf[:], number)
   167  	if err := db.Put(snapshotRecoveryKey, buf[:]); err != nil {
   168  		log.Crit("Failed to store snapshot recovery number", "err", err)
   169  	}
   170  }
   171  
   172  // DeleteSnapshotRecoveryNumber deletes the block number of the last persisted
   173  // snapshot layer.
   174  func DeleteSnapshotRecoveryNumber(db xcbdb.KeyValueWriter) {
   175  	if err := db.Delete(snapshotRecoveryKey); err != nil {
   176  		log.Crit("Failed to remove snapshot recovery number", "err", err)
   177  	}
   178  }