gitlab.com/flarenetwork/coreth@v0.1.1/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 "encoding/binary" 31 32 "github.com/ethereum/go-ethereum/common" 33 "github.com/ethereum/go-ethereum/ethdb" 34 "github.com/ethereum/go-ethereum/log" 35 ) 36 37 // ReadSnapshotRoot retrieves the root of the block whose state is contained in 38 // the persisted snapshot. 39 func ReadSnapshotRoot(db ethdb.KeyValueReader) common.Hash { 40 data, _ := db.Get(snapshotRootKey) 41 if len(data) != common.HashLength { 42 return common.Hash{} 43 } 44 return common.BytesToHash(data) 45 } 46 47 // WriteSnapshotRoot stores the root of the block whose state is contained in 48 // the persisted snapshot. 49 func WriteSnapshotRoot(db ethdb.KeyValueWriter, root common.Hash) { 50 if err := db.Put(snapshotRootKey, root[:]); err != nil { 51 log.Crit("Failed to store snapshot root", "err", err) 52 } 53 } 54 55 // DeleteSnapshotRoot deletes the root of the block whose state is contained in 56 // the persisted snapshot. Since snapshots are not immutable, this method can 57 // be used during updates, so a crash or failure will mark the entire snapshot 58 // invalid. 59 func DeleteSnapshotRoot(db ethdb.KeyValueWriter) { 60 if err := db.Delete(snapshotRootKey); err != nil { 61 log.Crit("Failed to remove snapshot root", "err", err) 62 } 63 } 64 65 // ReadSnapshotBlockHash retrieves the hash of the block whose state is contained in 66 // the persisted snapshot. 67 func ReadSnapshotBlockHash(db ethdb.KeyValueReader) common.Hash { 68 data, _ := db.Get(snapshotBlockHashKey) 69 if len(data) != common.HashLength { 70 return common.Hash{} 71 } 72 return common.BytesToHash(data) 73 } 74 75 // WriteSnapshotRoot stores the root of the block whose state is contained in 76 // the persisted snapshot. 77 func WriteSnapshotBlockHash(db ethdb.KeyValueWriter, blockHash common.Hash) { 78 if err := db.Put(snapshotBlockHashKey, blockHash[:]); err != nil { 79 log.Crit("Failed to store snapshot block hash", "err", err) 80 } 81 } 82 83 // DeleteSnapshotBlockHash deletes the hash of the block whose state is contained in 84 // the persisted snapshot. Since snapshots are not immutable, this method can 85 // be used during updates, so a crash or failure will mark the entire snapshot 86 // invalid. 87 func DeleteSnapshotBlockHash(db ethdb.KeyValueWriter) { 88 if err := db.Delete(snapshotBlockHashKey); err != nil { 89 log.Crit("Failed to remove snapshot block hash", "err", err) 90 } 91 } 92 93 // ReadAccountSnapshot retrieves the snapshot entry of an account trie leaf. 94 func ReadAccountSnapshot(db ethdb.KeyValueReader, hash common.Hash) []byte { 95 data, _ := db.Get(accountSnapshotKey(hash)) 96 return data 97 } 98 99 // WriteAccountSnapshot stores the snapshot entry of an account trie leaf. 100 func WriteAccountSnapshot(db ethdb.KeyValueWriter, hash common.Hash, entry []byte) { 101 if err := db.Put(accountSnapshotKey(hash), entry); err != nil { 102 log.Crit("Failed to store account snapshot", "err", err) 103 } 104 } 105 106 // DeleteAccountSnapshot removes the snapshot entry of an account trie leaf. 107 func DeleteAccountSnapshot(db ethdb.KeyValueWriter, hash common.Hash) { 108 if err := db.Delete(accountSnapshotKey(hash)); err != nil { 109 log.Crit("Failed to delete account snapshot", "err", err) 110 } 111 } 112 113 // ReadStorageSnapshot retrieves the snapshot entry of an storage trie leaf. 114 func ReadStorageSnapshot(db ethdb.KeyValueReader, accountHash, storageHash common.Hash) []byte { 115 data, _ := db.Get(storageSnapshotKey(accountHash, storageHash)) 116 return data 117 } 118 119 // WriteStorageSnapshot stores the snapshot entry of an storage trie leaf. 120 func WriteStorageSnapshot(db ethdb.KeyValueWriter, accountHash, storageHash common.Hash, entry []byte) { 121 if err := db.Put(storageSnapshotKey(accountHash, storageHash), entry); err != nil { 122 log.Crit("Failed to store storage snapshot", "err", err) 123 } 124 } 125 126 // DeleteStorageSnapshot removes the snapshot entry of an storage trie leaf. 127 func DeleteStorageSnapshot(db ethdb.KeyValueWriter, accountHash, storageHash common.Hash) { 128 if err := db.Delete(storageSnapshotKey(accountHash, storageHash)); err != nil { 129 log.Crit("Failed to delete storage snapshot", "err", err) 130 } 131 } 132 133 // IterateStorageSnapshots returns an iterator for walking the entire storage 134 // space of a specific account. 135 func IterateStorageSnapshots(db ethdb.Iteratee, accountHash common.Hash) ethdb.Iterator { 136 return db.NewIterator(storageSnapshotsKey(accountHash), nil) 137 } 138 139 // ReadSnapshotGenerator retrieves the serialized snapshot generator saved at 140 // the last shutdown. 141 func ReadSnapshotGenerator(db ethdb.KeyValueReader) []byte { 142 data, _ := db.Get(snapshotGeneratorKey) 143 return data 144 } 145 146 // WriteSnapshotGenerator stores the serialized snapshot generator to save at 147 // shutdown. 148 func WriteSnapshotGenerator(db ethdb.KeyValueWriter, generator []byte) { 149 if err := db.Put(snapshotGeneratorKey, generator); err != nil { 150 log.Crit("Failed to store snapshot generator", "err", err) 151 } 152 } 153 154 // DeleteSnapshotGenerator deletes the serialized snapshot generator saved at 155 // the last shutdown 156 func DeleteSnapshotGenerator(db ethdb.KeyValueWriter) { 157 if err := db.Delete(snapshotGeneratorKey); err != nil { 158 log.Crit("Failed to remove snapshot generator", "err", err) 159 } 160 } 161 162 // ReadSnapshotRecoveryNumber retrieves the block number of the last persisted 163 // snapshot layer. 164 func ReadSnapshotRecoveryNumber(db ethdb.KeyValueReader) *uint64 { 165 data, _ := db.Get(snapshotRecoveryKey) 166 if len(data) == 0 { 167 return nil 168 } 169 if len(data) != 8 { 170 return nil 171 } 172 number := binary.BigEndian.Uint64(data) 173 return &number 174 } 175 176 // WriteSnapshotRecoveryNumber stores the block number of the last persisted 177 // snapshot layer. 178 func WriteSnapshotRecoveryNumber(db ethdb.KeyValueWriter, number uint64) { 179 var buf [8]byte 180 binary.BigEndian.PutUint64(buf[:], number) 181 if err := db.Put(snapshotRecoveryKey, buf[:]); err != nil { 182 log.Crit("Failed to store snapshot recovery number", "err", err) 183 } 184 } 185 186 // DeleteSnapshotRecoveryNumber deletes the block number of the last persisted 187 // snapshot layer. 188 func DeleteSnapshotRecoveryNumber(db ethdb.KeyValueWriter) { 189 if err := db.Delete(snapshotRecoveryKey); err != nil { 190 log.Crit("Failed to remove snapshot recovery number", "err", err) 191 } 192 } 193 194 // ReadSnapshotSyncStatus retrieves the serialized sync status saved at shutdown. 195 func ReadSnapshotSyncStatus(db ethdb.KeyValueReader) []byte { 196 data, _ := db.Get(snapshotSyncStatusKey) 197 return data 198 } 199 200 // WriteSnapshotSyncStatus stores the serialized sync status to save at shutdown. 201 func WriteSnapshotSyncStatus(db ethdb.KeyValueWriter, status []byte) { 202 if err := db.Put(snapshotSyncStatusKey, status); err != nil { 203 log.Crit("Failed to store snapshot sync status", "err", err) 204 } 205 } 206 207 // DeleteSnapshotSyncStatus deletes the serialized sync status saved at the last 208 // shutdown 209 func DeleteSnapshotSyncStatus(db ethdb.KeyValueWriter) { 210 if err := db.Delete(snapshotSyncStatusKey); err != nil { 211 log.Crit("Failed to remove snapshot sync status", "err", err) 212 } 213 }