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