github.com/MetalBlockchain/subnet-evm@v0.6.3/core/rawdb/accessors_snapshot.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 rawdb 28 29 import ( 30 "github.com/ethereum/go-ethereum/common" 31 "github.com/ethereum/go-ethereum/ethdb" 32 "github.com/ethereum/go-ethereum/log" 33 ) 34 35 // ReadSnapshotRoot retrieves the root of the block whose state is contained in 36 // the persisted snapshot. 37 func ReadSnapshotRoot(db ethdb.KeyValueReader) common.Hash { 38 data, _ := db.Get(snapshotRootKey) 39 if len(data) != common.HashLength { 40 return common.Hash{} 41 } 42 return common.BytesToHash(data) 43 } 44 45 // WriteSnapshotRoot stores the root of the block whose state is contained in 46 // the persisted snapshot. 47 func WriteSnapshotRoot(db ethdb.KeyValueWriter, root common.Hash) { 48 if err := db.Put(snapshotRootKey, root[:]); err != nil { 49 log.Crit("Failed to store snapshot root", "err", err) 50 } 51 } 52 53 // DeleteSnapshotRoot deletes the root of the block whose state is contained in 54 // the persisted snapshot. Since snapshots are not immutable, this method can 55 // be used during updates, so a crash or failure will mark the entire snapshot 56 // invalid. 57 func DeleteSnapshotRoot(db ethdb.KeyValueWriter) { 58 if err := db.Delete(snapshotRootKey); err != nil { 59 log.Crit("Failed to remove snapshot root", "err", err) 60 } 61 } 62 63 // ReadSnapshotBlockHash retrieves the hash of the block whose state is contained in 64 // the persisted snapshot. 65 func ReadSnapshotBlockHash(db ethdb.KeyValueReader) common.Hash { 66 data, _ := db.Get(snapshotBlockHashKey) 67 if len(data) != common.HashLength { 68 return common.Hash{} 69 } 70 return common.BytesToHash(data) 71 } 72 73 // WriteSnapshotRoot stores the root of the block whose state is contained in 74 // the persisted snapshot. 75 func WriteSnapshotBlockHash(db ethdb.KeyValueWriter, blockHash common.Hash) { 76 if err := db.Put(snapshotBlockHashKey, blockHash[:]); err != nil { 77 log.Crit("Failed to store snapshot block hash", "err", err) 78 } 79 } 80 81 // DeleteSnapshotBlockHash deletes the hash of the block whose state is contained in 82 // the persisted snapshot. Since snapshots are not immutable, this method can 83 // be used during updates, so a crash or failure will mark the entire snapshot 84 // invalid. 85 func DeleteSnapshotBlockHash(db ethdb.KeyValueWriter) { 86 if err := db.Delete(snapshotBlockHashKey); err != nil { 87 log.Crit("Failed to remove snapshot block hash", "err", err) 88 } 89 } 90 91 // ReadAccountSnapshot retrieves the snapshot entry of an account trie leaf. 92 func ReadAccountSnapshot(db ethdb.KeyValueReader, hash common.Hash) []byte { 93 data, _ := db.Get(accountSnapshotKey(hash)) 94 return data 95 } 96 97 // WriteAccountSnapshot stores the snapshot entry of an account trie leaf. 98 func WriteAccountSnapshot(db ethdb.KeyValueWriter, hash common.Hash, entry []byte) { 99 if err := db.Put(accountSnapshotKey(hash), entry); err != nil { 100 log.Crit("Failed to store account snapshot", "err", err) 101 } 102 } 103 104 // DeleteAccountSnapshot removes the snapshot entry of an account trie leaf. 105 func DeleteAccountSnapshot(db ethdb.KeyValueWriter, hash common.Hash) { 106 if err := db.Delete(accountSnapshotKey(hash)); err != nil { 107 log.Crit("Failed to delete account snapshot", "err", err) 108 } 109 } 110 111 // ReadStorageSnapshot retrieves the snapshot entry of an storage trie leaf. 112 func ReadStorageSnapshot(db ethdb.KeyValueReader, accountHash, storageHash common.Hash) []byte { 113 data, _ := db.Get(storageSnapshotKey(accountHash, storageHash)) 114 return data 115 } 116 117 // WriteStorageSnapshot stores the snapshot entry of an storage trie leaf. 118 func WriteStorageSnapshot(db ethdb.KeyValueWriter, accountHash, storageHash common.Hash, entry []byte) { 119 if err := db.Put(storageSnapshotKey(accountHash, storageHash), entry); err != nil { 120 log.Crit("Failed to store storage snapshot", "err", err) 121 } 122 } 123 124 // DeleteStorageSnapshot removes the snapshot entry of an storage trie leaf. 125 func DeleteStorageSnapshot(db ethdb.KeyValueWriter, accountHash, storageHash common.Hash) { 126 if err := db.Delete(storageSnapshotKey(accountHash, storageHash)); err != nil { 127 log.Crit("Failed to delete storage snapshot", "err", err) 128 } 129 } 130 131 // IterateStorageSnapshots returns an iterator for walking the entire storage 132 // space of a specific account. 133 func IterateStorageSnapshots(db ethdb.Iteratee, accountHash common.Hash) ethdb.Iterator { 134 return NewKeyLengthIterator(db.NewIterator(storageSnapshotsKey(accountHash), nil), len(SnapshotStoragePrefix)+2*common.HashLength) 135 } 136 137 // IterateAccountSnapshots returns an iterator for walking all of the accounts in the snapshot 138 func IterateAccountSnapshots(db ethdb.Iteratee) ethdb.Iterator { 139 return NewKeyLengthIterator(db.NewIterator(SnapshotAccountPrefix, nil), len(SnapshotAccountPrefix)+common.HashLength) 140 } 141 142 // ReadSnapshotGenerator retrieves the serialized snapshot generator saved at 143 // the last shutdown. 144 func ReadSnapshotGenerator(db ethdb.KeyValueReader) []byte { 145 data, _ := db.Get(snapshotGeneratorKey) 146 return data 147 } 148 149 // WriteSnapshotGenerator stores the serialized snapshot generator to save at 150 // shutdown. 151 func WriteSnapshotGenerator(db ethdb.KeyValueWriter, generator []byte) { 152 if err := db.Put(snapshotGeneratorKey, generator); err != nil { 153 log.Crit("Failed to store snapshot generator", "err", err) 154 } 155 }