github.com/ccm-chain/ccmchain@v1.0.0/core/rawdb/accessors_snapshot.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 rawdb
    18  
    19  import (
    20  	"github.com/ccm-chain/ccmchain/common"
    21  	"github.com/ccm-chain/ccmchain/database"
    22  	"github.com/ccm-chain/ccmchain/log"
    23  )
    24  
    25  // ReadSnapshotRoot retrieves the root of the block whose state is contained in
    26  // the persisted snapshot.
    27  func ReadSnapshotRoot(db database.KeyValueReader) common.Hash {
    28  	data, _ := db.Get(snapshotRootKey)
    29  	if len(data) != common.HashLength {
    30  		return common.Hash{}
    31  	}
    32  	return common.BytesToHash(data)
    33  }
    34  
    35  // WriteSnapshotRoot stores the root of the block whose state is contained in
    36  // the persisted snapshot.
    37  func WriteSnapshotRoot(db database.KeyValueWriter, root common.Hash) {
    38  	if err := db.Put(snapshotRootKey, root[:]); err != nil {
    39  		log.Crit("Failed to store snapshot root", "err", err)
    40  	}
    41  }
    42  
    43  // DeleteSnapshotRoot deletes the hash of the block whose state is contained in
    44  // the persisted snapshot. Since snapshots are not immutable, this  method can
    45  // be used during updates, so a crash or failure will mark the entire snapshot
    46  // invalid.
    47  func DeleteSnapshotRoot(db database.KeyValueWriter) {
    48  	if err := db.Delete(snapshotRootKey); err != nil {
    49  		log.Crit("Failed to remove snapshot root", "err", err)
    50  	}
    51  }
    52  
    53  // ReadAccountSnapshot retrieves the snapshot entry of an account trie leaf.
    54  func ReadAccountSnapshot(db database.KeyValueReader, hash common.Hash) []byte {
    55  	data, _ := db.Get(accountSnapshotKey(hash))
    56  	return data
    57  }
    58  
    59  // WriteAccountSnapshot stores the snapshot entry of an account trie leaf.
    60  func WriteAccountSnapshot(db database.KeyValueWriter, hash common.Hash, entry []byte) {
    61  	if err := db.Put(accountSnapshotKey(hash), entry); err != nil {
    62  		log.Crit("Failed to store account snapshot", "err", err)
    63  	}
    64  }
    65  
    66  // DeleteAccountSnapshot removes the snapshot entry of an account trie leaf.
    67  func DeleteAccountSnapshot(db database.KeyValueWriter, hash common.Hash) {
    68  	if err := db.Delete(accountSnapshotKey(hash)); err != nil {
    69  		log.Crit("Failed to delete account snapshot", "err", err)
    70  	}
    71  }
    72  
    73  // ReadStorageSnapshot retrieves the snapshot entry of an storage trie leaf.
    74  func ReadStorageSnapshot(db database.KeyValueReader, accountHash, storageHash common.Hash) []byte {
    75  	data, _ := db.Get(storageSnapshotKey(accountHash, storageHash))
    76  	return data
    77  }
    78  
    79  // WriteStorageSnapshot stores the snapshot entry of an storage trie leaf.
    80  func WriteStorageSnapshot(db database.KeyValueWriter, accountHash, storageHash common.Hash, entry []byte) {
    81  	if err := db.Put(storageSnapshotKey(accountHash, storageHash), entry); err != nil {
    82  		log.Crit("Failed to store storage snapshot", "err", err)
    83  	}
    84  }
    85  
    86  // DeleteStorageSnapshot removes the snapshot entry of an storage trie leaf.
    87  func DeleteStorageSnapshot(db database.KeyValueWriter, accountHash, storageHash common.Hash) {
    88  	if err := db.Delete(storageSnapshotKey(accountHash, storageHash)); err != nil {
    89  		log.Crit("Failed to delete storage snapshot", "err", err)
    90  	}
    91  }
    92  
    93  // IterateStorageSnapshots returns an iterator for walking the entire storage
    94  // space of a specific account.
    95  func IterateStorageSnapshots(db database.Iteratee, accountHash common.Hash) database.Iterator {
    96  	return db.NewIterator(storageSnapshotsKey(accountHash), nil)
    97  }
    98  
    99  // ReadSnapshotJournal retrieves the serialized in-memory diff layers saved at
   100  // the last shutdown. The blob is expected to be max a few 10s of megabytes.
   101  func ReadSnapshotJournal(db database.KeyValueReader) []byte {
   102  	data, _ := db.Get(snapshotJournalKey)
   103  	return data
   104  }
   105  
   106  // WriteSnapshotJournal stores the serialized in-memory diff layers to save at
   107  // shutdown. The blob is expected to be max a few 10s of megabytes.
   108  func WriteSnapshotJournal(db database.KeyValueWriter, journal []byte) {
   109  	if err := db.Put(snapshotJournalKey, journal); err != nil {
   110  		log.Crit("Failed to store snapshot journal", "err", err)
   111  	}
   112  }
   113  
   114  // DeleteSnapshotJournal deletes the serialized in-memory diff layers saved at
   115  // the last shutdown
   116  func DeleteSnapshotJournal(db database.KeyValueWriter) {
   117  	if err := db.Delete(snapshotJournalKey); err != nil {
   118  		log.Crit("Failed to remove snapshot journal", "err", err)
   119  	}
   120  }