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