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