github.com/ethereum-optimism/optimism/l2geth@v0.0.0-20230612200230-50b04ade19e3/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 "encoding/binary" 22 23 "github.com/ethereum-optimism/optimism/l2geth/common" 24 "github.com/ethereum-optimism/optimism/l2geth/metrics" 25 ) 26 27 // The fields below define the low level database schema prefixing. 28 var ( 29 // databaseVerisionKey tracks the current database version. 30 databaseVerisionKey = []byte("DatabaseVersion") 31 32 // headHeaderKey tracks the latest known header's hash. 33 headHeaderKey = []byte("LastHeader") 34 35 // headBlockKey tracks the latest known full block's hash. 36 headBlockKey = []byte("LastBlock") 37 38 // headFastBlockKey tracks the latest known incomplete block's hash during fast sync. 39 headFastBlockKey = []byte("LastFast") 40 41 // fastTrieProgressKey tracks the number of trie entries imported during fast sync. 42 fastTrieProgressKey = []byte("TrieSync") 43 44 // Data item prefixes (use single byte to avoid mixing data types, avoid `i`, used for indexes). 45 headerPrefix = []byte("h") // headerPrefix + num (uint64 big endian) + hash -> header 46 headerTDSuffix = []byte("t") // headerPrefix + num (uint64 big endian) + hash + headerTDSuffix -> td 47 headerHashSuffix = []byte("n") // headerPrefix + num (uint64 big endian) + headerHashSuffix -> hash 48 headerNumberPrefix = []byte("H") // headerNumberPrefix + hash -> num (uint64 big endian) 49 50 blockBodyPrefix = []byte("b") // blockBodyPrefix + num (uint64 big endian) + hash -> block body 51 blockReceiptsPrefix = []byte("r") // blockReceiptsPrefix + num (uint64 big endian) + hash -> block receipts 52 53 txLookupPrefix = []byte("l") // txLookupPrefix + hash -> transaction/receipt lookup metadata 54 bloomBitsPrefix = []byte("B") // bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash -> bloom bits 55 56 // Optimism specific 57 txMetaPrefix = []byte("x") // txMetaPrefix + hash -> transaction metadata 58 59 // headIndexKey tracks the last processed ctc index 60 headIndexKey = []byte("LastIndex") 61 // headQueueIndexKey tracks th last processed queue index 62 headQueueIndexKey = []byte("LastQueueIndex") 63 // headVerifiedIndexKey tracks the latest verified index 64 headVerifiedIndexKey = []byte("LastVerifiedIndex") 65 // headBatchKey tracks the latest processed batch 66 headBatchKey = []byte("LastBatch") 67 68 preimagePrefix = []byte("secure-key-") // preimagePrefix + hash -> preimage 69 configPrefix = []byte("ethereum-config-") // config prefix for the db 70 71 // Chain index prefixes (use `i` + single byte to avoid mixing data types). 72 BloomBitsIndexPrefix = []byte("iB") // BloomBitsIndexPrefix is the data table of a chain indexer to track its progress 73 74 preimageCounter = metrics.NewRegisteredCounter("db/preimage/total", nil) 75 preimageHitCounter = metrics.NewRegisteredCounter("db/preimage/hits", nil) 76 ) 77 78 const ( 79 // freezerHeaderTable indicates the name of the freezer header table. 80 freezerHeaderTable = "headers" 81 82 // freezerHashTable indicates the name of the freezer canonical hash table. 83 freezerHashTable = "hashes" 84 85 // freezerBodiesTable indicates the name of the freezer block body table. 86 freezerBodiesTable = "bodies" 87 88 // freezerReceiptTable indicates the name of the freezer receipts table. 89 freezerReceiptTable = "receipts" 90 91 // freezerDifficultyTable indicates the name of the freezer total difficulty table. 92 freezerDifficultyTable = "diffs" 93 ) 94 95 // freezerNoSnappy configures whether compression is disabled for the ancient-tables. 96 // Hashes and difficulties don't compress well. 97 var freezerNoSnappy = map[string]bool{ 98 freezerHeaderTable: false, 99 freezerHashTable: true, 100 freezerBodiesTable: false, 101 freezerReceiptTable: false, 102 freezerDifficultyTable: true, 103 } 104 105 // LegacyTxLookupEntry is the legacy TxLookupEntry definition with some unnecessary 106 // fields. 107 type LegacyTxLookupEntry struct { 108 BlockHash common.Hash 109 BlockIndex uint64 110 Index uint64 111 } 112 113 // encodeBlockNumber encodes a block number as big endian uint64 114 func encodeBlockNumber(number uint64) []byte { 115 enc := make([]byte, 8) 116 binary.BigEndian.PutUint64(enc, number) 117 return enc 118 } 119 120 // headerKeyPrefix = headerPrefix + num (uint64 big endian) 121 func headerKeyPrefix(number uint64) []byte { 122 return append(headerPrefix, encodeBlockNumber(number)...) 123 } 124 125 // headerKey = headerPrefix + num (uint64 big endian) + hash 126 func headerKey(number uint64, hash common.Hash) []byte { 127 return append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...) 128 } 129 130 // headerTDKey = headerPrefix + num (uint64 big endian) + hash + headerTDSuffix 131 func headerTDKey(number uint64, hash common.Hash) []byte { 132 return append(headerKey(number, hash), headerTDSuffix...) 133 } 134 135 // headerHashKey = headerPrefix + num (uint64 big endian) + headerHashSuffix 136 func headerHashKey(number uint64) []byte { 137 return append(append(headerPrefix, encodeBlockNumber(number)...), headerHashSuffix...) 138 } 139 140 // headerNumberKey = headerNumberPrefix + hash 141 func headerNumberKey(hash common.Hash) []byte { 142 return append(headerNumberPrefix, hash.Bytes()...) 143 } 144 145 // blockBodyKey = blockBodyPrefix + num (uint64 big endian) + hash 146 func blockBodyKey(number uint64, hash common.Hash) []byte { 147 return append(append(blockBodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...) 148 } 149 150 // blockReceiptsKey = blockReceiptsPrefix + num (uint64 big endian) + hash 151 func blockReceiptsKey(number uint64, hash common.Hash) []byte { 152 return append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...) 153 } 154 155 // txLookupKey = txLookupPrefix + hash 156 func txLookupKey(hash common.Hash) []byte { 157 return append(txLookupPrefix, hash.Bytes()...) 158 } 159 160 // txMetaKey = txMetaPrefix + num (uint64 big endian) 161 func txMetaKey(number uint64) []byte { 162 return append(txMetaPrefix, encodeBlockNumber(number)...) 163 } 164 165 // bloomBitsKey = bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash 166 func bloomBitsKey(bit uint, section uint64, hash common.Hash) []byte { 167 key := append(append(bloomBitsPrefix, make([]byte, 10)...), hash.Bytes()...) 168 169 binary.BigEndian.PutUint16(key[1:], uint16(bit)) 170 binary.BigEndian.PutUint64(key[3:], section) 171 172 return key 173 } 174 175 // preimageKey = preimagePrefix + hash 176 func preimageKey(hash common.Hash) []byte { 177 return append(preimagePrefix, hash.Bytes()...) 178 } 179 180 // configKey = configPrefix + hash 181 func configKey(hash common.Hash) []byte { 182 return append(configPrefix, hash.Bytes()...) 183 }