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