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