github.com/authcall/reference-optimistic-geth@v0.0.0-20220816224302-06313bfeb8d2/core/rawdb/schema.go (about) 1 // Copyright 2018 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 contains a collection of low level database accessors. 18 package rawdb 19 20 import ( 21 "bytes" 22 "encoding/binary" 23 24 "github.com/ethereum/go-ethereum/common" 25 "github.com/ethereum/go-ethereum/metrics" 26 ) 27 28 // The fields below define the low level database schema prefixing. 29 var ( 30 // databaseVersionKey tracks the current database version. 31 databaseVersionKey = []byte("DatabaseVersion") 32 33 // headHeaderKey tracks the latest known header's hash. 34 headHeaderKey = []byte("LastHeader") 35 36 // headBlockKey tracks the latest known full block's hash. 37 headBlockKey = []byte("LastBlock") 38 39 // headFastBlockKey tracks the latest known incomplete block's hash during fast sync. 40 headFastBlockKey = []byte("LastFast") 41 42 // headFinalizedBlockKey tracks the latest known finalized block hash. 43 headFinalizedBlockKey = []byte("LastFinalized") 44 45 // lastPivotKey tracks the last pivot block used by fast sync (to reenable on sethead). 46 lastPivotKey = []byte("LastPivot") 47 48 // fastTrieProgressKey tracks the number of trie entries imported during fast sync. 49 fastTrieProgressKey = []byte("TrieSync") 50 51 // snapshotDisabledKey flags that the snapshot should not be maintained due to initial sync. 52 snapshotDisabledKey = []byte("SnapshotDisabled") 53 54 // SnapshotRootKey tracks the hash of the last snapshot. 55 SnapshotRootKey = []byte("SnapshotRoot") 56 57 // snapshotJournalKey tracks the in-memory diff layers across restarts. 58 snapshotJournalKey = []byte("SnapshotJournal") 59 60 // snapshotGeneratorKey tracks the snapshot generation marker across restarts. 61 snapshotGeneratorKey = []byte("SnapshotGenerator") 62 63 // snapshotRecoveryKey tracks the snapshot recovery marker across restarts. 64 snapshotRecoveryKey = []byte("SnapshotRecovery") 65 66 // snapshotSyncStatusKey tracks the snapshot sync status across restarts. 67 snapshotSyncStatusKey = []byte("SnapshotSyncStatus") 68 69 // skeletonSyncStatusKey tracks the skeleton sync status across restarts. 70 skeletonSyncStatusKey = []byte("SkeletonSyncStatus") 71 72 // txIndexTailKey tracks the oldest block whose transactions have been indexed. 73 txIndexTailKey = []byte("TransactionIndexTail") 74 75 // fastTxLookupLimitKey tracks the transaction lookup limit during fast sync. 76 fastTxLookupLimitKey = []byte("FastTransactionLookupLimit") 77 78 // badBlockKey tracks the list of bad blocks seen by local 79 badBlockKey = []byte("InvalidBlock") 80 81 // uncleanShutdownKey tracks the list of local crashes 82 uncleanShutdownKey = []byte("unclean-shutdown") // config prefix for the db 83 84 // transitionStatusKey tracks the eth2 transition status. 85 transitionStatusKey = []byte("eth2-transition") 86 87 // Data item prefixes (use single byte to avoid mixing data types, avoid `i`, used for indexes). 88 headerPrefix = []byte("h") // headerPrefix + num (uint64 big endian) + hash -> header 89 headerTDSuffix = []byte("t") // headerPrefix + num (uint64 big endian) + hash + headerTDSuffix -> td 90 headerHashSuffix = []byte("n") // headerPrefix + num (uint64 big endian) + headerHashSuffix -> hash 91 headerNumberPrefix = []byte("H") // headerNumberPrefix + hash -> num (uint64 big endian) 92 93 blockBodyPrefix = []byte("b") // blockBodyPrefix + num (uint64 big endian) + hash -> block body 94 blockReceiptsPrefix = []byte("r") // blockReceiptsPrefix + num (uint64 big endian) + hash -> block receipts 95 96 txLookupPrefix = []byte("l") // txLookupPrefix + hash -> transaction/receipt lookup metadata 97 bloomBitsPrefix = []byte("B") // bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash -> bloom bits 98 SnapshotAccountPrefix = []byte("a") // SnapshotAccountPrefix + account hash -> account trie value 99 SnapshotStoragePrefix = []byte("o") // SnapshotStoragePrefix + account hash + storage hash -> storage trie value 100 CodePrefix = []byte("c") // CodePrefix + code hash -> account code 101 skeletonHeaderPrefix = []byte("S") // skeletonHeaderPrefix + num (uint64 big endian) -> header 102 103 PreimagePrefix = []byte("secure-key-") // PreimagePrefix + hash -> preimage 104 configPrefix = []byte("ethereum-config-") // config prefix for the db 105 genesisPrefix = []byte("ethereum-genesis-") // genesis state prefix for the db 106 107 // Chain index prefixes (use `i` + single byte to avoid mixing data types). 108 BloomBitsIndexPrefix = []byte("iB") // BloomBitsIndexPrefix is the data table of a chain indexer to track its progress 109 110 preimageCounter = metrics.NewRegisteredCounter("db/preimage/total", nil) 111 preimageHitCounter = metrics.NewRegisteredCounter("db/preimage/hits", nil) 112 ) 113 114 const ( 115 // freezerHeaderTable indicates the name of the freezer header table. 116 freezerHeaderTable = "headers" 117 118 // freezerHashTable indicates the name of the freezer canonical hash table. 119 freezerHashTable = "hashes" 120 121 // freezerBodiesTable indicates the name of the freezer block body table. 122 freezerBodiesTable = "bodies" 123 124 // freezerReceiptTable indicates the name of the freezer receipts table. 125 freezerReceiptTable = "receipts" 126 127 // freezerDifficultyTable indicates the name of the freezer total difficulty table. 128 freezerDifficultyTable = "diffs" 129 ) 130 131 // FreezerNoSnappy configures whether compression is disabled for the ancient-tables. 132 // Hashes and difficulties don't compress well. 133 var FreezerNoSnappy = map[string]bool{ 134 freezerHeaderTable: false, 135 freezerHashTable: true, 136 freezerBodiesTable: false, 137 freezerReceiptTable: false, 138 freezerDifficultyTable: true, 139 } 140 141 // LegacyTxLookupEntry is the legacy TxLookupEntry definition with some unnecessary 142 // fields. 143 type LegacyTxLookupEntry struct { 144 BlockHash common.Hash 145 BlockIndex uint64 146 Index uint64 147 } 148 149 // encodeBlockNumber encodes a block number as big endian uint64 150 func encodeBlockNumber(number uint64) []byte { 151 enc := make([]byte, 8) 152 binary.BigEndian.PutUint64(enc, number) 153 return enc 154 } 155 156 // headerKeyPrefix = headerPrefix + num (uint64 big endian) 157 func headerKeyPrefix(number uint64) []byte { 158 return append(headerPrefix, encodeBlockNumber(number)...) 159 } 160 161 // headerKey = headerPrefix + num (uint64 big endian) + hash 162 func headerKey(number uint64, hash common.Hash) []byte { 163 return append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...) 164 } 165 166 // headerTDKey = headerPrefix + num (uint64 big endian) + hash + headerTDSuffix 167 func headerTDKey(number uint64, hash common.Hash) []byte { 168 return append(headerKey(number, hash), headerTDSuffix...) 169 } 170 171 // headerHashKey = headerPrefix + num (uint64 big endian) + headerHashSuffix 172 func headerHashKey(number uint64) []byte { 173 return append(append(headerPrefix, encodeBlockNumber(number)...), headerHashSuffix...) 174 } 175 176 // headerNumberKey = headerNumberPrefix + hash 177 func headerNumberKey(hash common.Hash) []byte { 178 return append(headerNumberPrefix, hash.Bytes()...) 179 } 180 181 // blockBodyKey = blockBodyPrefix + num (uint64 big endian) + hash 182 func blockBodyKey(number uint64, hash common.Hash) []byte { 183 return append(append(blockBodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...) 184 } 185 186 // blockReceiptsKey = blockReceiptsPrefix + num (uint64 big endian) + hash 187 func blockReceiptsKey(number uint64, hash common.Hash) []byte { 188 return append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...) 189 } 190 191 // txLookupKey = txLookupPrefix + hash 192 func txLookupKey(hash common.Hash) []byte { 193 return append(txLookupPrefix, hash.Bytes()...) 194 } 195 196 // accountSnapshotKey = SnapshotAccountPrefix + hash 197 func accountSnapshotKey(hash common.Hash) []byte { 198 return append(SnapshotAccountPrefix, hash.Bytes()...) 199 } 200 201 // storageSnapshotKey = SnapshotStoragePrefix + account hash + storage hash 202 func storageSnapshotKey(accountHash, storageHash common.Hash) []byte { 203 return append(append(SnapshotStoragePrefix, accountHash.Bytes()...), storageHash.Bytes()...) 204 } 205 206 // storageSnapshotsKey = SnapshotStoragePrefix + account hash + storage hash 207 func storageSnapshotsKey(accountHash common.Hash) []byte { 208 return append(SnapshotStoragePrefix, accountHash.Bytes()...) 209 } 210 211 // bloomBitsKey = bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash 212 func bloomBitsKey(bit uint, section uint64, hash common.Hash) []byte { 213 key := append(append(bloomBitsPrefix, make([]byte, 10)...), hash.Bytes()...) 214 215 binary.BigEndian.PutUint16(key[1:], uint16(bit)) 216 binary.BigEndian.PutUint64(key[3:], section) 217 218 return key 219 } 220 221 // skeletonHeaderKey = skeletonHeaderPrefix + num (uint64 big endian) 222 func skeletonHeaderKey(number uint64) []byte { 223 return append(skeletonHeaderPrefix, encodeBlockNumber(number)...) 224 } 225 226 // preimageKey = PreimagePrefix + hash 227 func preimageKey(hash common.Hash) []byte { 228 return append(PreimagePrefix, hash.Bytes()...) 229 } 230 231 // codeKey = CodePrefix + hash 232 func codeKey(hash common.Hash) []byte { 233 return append(CodePrefix, hash.Bytes()...) 234 } 235 236 // IsCodeKey reports whether the given byte slice is the key of contract code, 237 // if so return the raw code hash as well. 238 func IsCodeKey(key []byte) (bool, []byte) { 239 if bytes.HasPrefix(key, CodePrefix) && len(key) == common.HashLength+len(CodePrefix) { 240 return true, key[len(CodePrefix):] 241 } 242 return false, nil 243 } 244 245 // configKey = configPrefix + hash 246 func configKey(hash common.Hash) []byte { 247 return append(configPrefix, hash.Bytes()...) 248 } 249 250 // genesisKey = genesisPrefix + hash 251 func genesisKey(hash common.Hash) []byte { 252 return append(genesisPrefix, hash.Bytes()...) 253 }