github.com/klaytn/klaytn@v1.12.1/storage/database/schema.go (about) 1 // Modifications Copyright 2018 The klaytn Authors 2 // Copyright 2018 The go-ethereum Authors 3 // This file is part of the go-ethereum library. 4 // 5 // The go-ethereum library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-ethereum library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 17 // 18 // This file is derived from core/rawdb/schema.go (2018/06/04). 19 // Modified and improved for the klaytn development. 20 21 package database 22 23 import ( 24 "bytes" 25 "encoding/binary" 26 27 "github.com/klaytn/klaytn/common" 28 "github.com/klaytn/klaytn/common/hexutil" 29 "github.com/rcrowley/go-metrics" 30 ) 31 32 // The fields below define the low level database schema prefixing. 33 var ( 34 // databaseVerisionKey tracks the current database version. 35 databaseVerisionKey = []byte("DatabaseVersion") 36 37 // headHeaderKey tracks the latest know header's hash. 38 headHeaderKey = []byte("LastHeader") 39 40 // headBlockKey tracks the latest know full block's hash. 41 headBlockKey = []byte("LastBlock") 42 headBlockBackupKey = []byte("LastBlockBackup") 43 44 // headFastBlockKey tracks the latest known incomplete block's hash duirng fast sync. 45 headFastBlockKey = []byte("LastFast") 46 headFastBlockBackupKey = []byte("LastFastBackup") 47 48 // fastTrieProgressKey tracks the number of trie entries imported during fast sync. 49 fastTrieProgressKey = []byte("TrieSync") 50 51 validSectionKey = []byte("count") 52 53 sectionHeadKeyPrefix = []byte("shead") 54 55 // snapshotKeyPrefix is a governance snapshot prefix 56 snapshotKeyPrefix = []byte("snapshot") 57 58 // snapshotJournalKey tracks the in-memory diff layers across restarts. 59 snapshotJournalKey = []byte("SnapshotJournal") 60 61 // SnapshotGeneratorKey tracks the snapshot generation marker across restarts. 62 SnapshotGeneratorKey = []byte("SnapshotGenerator") 63 64 // snapshotDisabledKey flags that the snapshot should not be maintained due to initial sync. 65 snapshotDisabledKey = []byte("SnapshotDisabled") 66 67 // snapshotRecoveryKey tracks the snapshot recovery marker across restarts. 68 snapshotRecoveryKey = []byte("SnapshotRecovery") 69 70 // snapshotSyncStatusKey tracks the snapshot sync status across restarts. 71 snapshotSyncStatusKey = []byte("SnapshotSyncStatus") 72 73 // snapshotRootKey tracks the hash of the last snapshot. 74 snapshotRootKey = []byte("SnapshotRoot") 75 76 // badBlockKey tracks the list of bad blocks seen by local 77 badBlockKey = []byte("InvalidBlock") 78 79 // Data item prefixes (use single byte to avoid mixing data types, avoid `i`, used for indexes). 80 headerPrefix = []byte("h") // headerPrefix + num (uint64 big endian) + hash -> header 81 headerTDSuffix = []byte("t") // headerPrefix + num (uint64 big endian) + hash + headerTDSuffix -> td 82 headerHashSuffix = []byte("n") // headerPrefix + num (uint64 big endian) + headerHashSuffix -> hash 83 headerNumberPrefix = []byte("H") // headerNumberPrefix + hash -> num (uint64 big endian) 84 85 blockBodyPrefix = []byte("b") // blockBodyPrefix + num (uint64 big endian) + hash -> block body 86 blockReceiptsPrefix = []byte("r") // blockReceiptsPrefix + num (uint64 big endian) + hash -> block receipts 87 88 txLookupPrefix = []byte("l") // txLookupPrefix + hash -> transaction/receipt lookup metadata 89 SnapshotAccountPrefix = []byte("a") // SnapshotAccountPrefix + account hash -> account trie value 90 SnapshotStoragePrefix = []byte("o") // SnapshotStoragePrefix + account hash + storage hash -> storage trie value 91 codePrefix = []byte("c") // codePrefix + code hash -> contract code 92 93 preimagePrefix = []byte("secure-key-") // preimagePrefix + hash -> preimage 94 configPrefix = []byte("klay-config-") // config prefix for the db 95 96 pruningEnabledKey = []byte("PruningEnabled") 97 pruningMarkPrefix = []byte("Pruning-") // KIP-111 pruning markings 98 pruningMarkValue = []byte{0x01} // A nonempty value to store a pruning mark 99 pruningMarkKeyLen = len(pruningMarkPrefix) + 8 + common.ExtHashLength // prefix + num (uint64) + node hash 100 lastPrunedBlockNumberKey = []byte("lastPrunedBlockNumber") 101 102 // Chain index prefixes (use `i` + single byte to avoid mixing data types). 103 BloomBitsIndexPrefix = []byte("iB") // BloomBitsIndexPrefix is the data table of a chain indexer to track its progress 104 105 preimageCounter = metrics.NewRegisteredCounter("db/preimage/total", nil) 106 preimageHitCounter = metrics.NewRegisteredCounter("db/preimage/hits", nil) 107 108 childChainTxHashPrefix = []byte("ccTxHash") 109 lastServiceChainTxReceiptKey = []byte("LastServiceChainTxReceipt") 110 lastIndexedBlockKey = []byte("LastIndexedBlockKey") 111 receiptFromParentChainKeyPrefix = []byte("receiptFromParentChain") 112 113 parentOperatorFeePayerPrefix = []byte("parentOperatorFeePayer") 114 childOperatorFeePayerPrefix = []byte("childOperatorFeePayer") 115 116 valueTransferTxHashPrefix = []byte("vt-tx-hash-key-") // Prefix + hash -> hash 117 118 // bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash -> bloom bits 119 bloomBitsPrefix = []byte("B") 120 121 senderTxHashToTxHashPrefix = []byte("SenderTxHash") 122 123 governancePrefix = []byte("governance") 124 governanceHistoryKey = []byte("governanceIdxHistory") 125 governanceStateKey = []byte("governanceState") 126 127 databaseDirPrefix = []byte("databaseDirectory") 128 migrationStatusKey = []byte("migrationStatus") 129 130 stakingInfoPrefix = []byte("stakingInfo") 131 132 chaindatafetcherCheckpointKey = []byte("chaindatafetcherCheckpoint") 133 ) 134 135 // TxLookupEntry is a positional metadata to help looking up the data content of 136 // a transaction or receipt given only its hash. 137 type TxLookupEntry struct { 138 BlockHash common.Hash 139 BlockIndex uint64 140 Index uint64 141 } 142 143 // headerKey = headerPrefix + num (uint64 big endian) + hash 144 func headerKey(number uint64, hash common.Hash) []byte { 145 return append(append(headerPrefix, common.Int64ToByteBigEndian(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, common.Int64ToByteBigEndian(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, common.Int64ToByteBigEndian(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, common.Int64ToByteBigEndian(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 func SenderTxHashToTxHashKey(senderTxHash common.Hash) []byte { 194 return append(senderTxHashToTxHashPrefix, senderTxHash.Bytes()...) 195 } 196 197 // preimageKey = preimagePrefix + hash 198 func preimageKey(hash common.Hash) []byte { 199 return append(preimagePrefix, hash.Bytes()...) 200 } 201 202 // CodeKey = codePrefix + hash 203 func CodeKey(hash common.Hash) []byte { 204 return append(codePrefix, hash.Bytes()...) 205 } 206 207 // IsCodeKey reports whether the given byte slice is the key of contract code, 208 // if so return the raw code hash as well. 209 func IsCodeKey(key []byte) (bool, []byte) { 210 if bytes.HasPrefix(key, codePrefix) && len(key) == common.HashLength+len(codePrefix) { 211 return true, key[len(codePrefix):] 212 } 213 return false, nil 214 } 215 216 // configKey = configPrefix + hash 217 func configKey(hash common.Hash) []byte { 218 return append(configPrefix, hash.Bytes()...) 219 } 220 221 func sectionHeadKey(encodedSection []byte) []byte { 222 return append(sectionHeadKeyPrefix, encodedSection...) 223 } 224 225 func snapshotKey(hash common.Hash) []byte { 226 return append(snapshotKeyPrefix, hash[:]...) 227 } 228 229 func childChainTxHashKey(ccBlockHash common.Hash) []byte { 230 return append(childChainTxHashPrefix, ccBlockHash.Bytes()...) 231 } 232 233 func receiptFromParentChainKey(blockHash common.Hash) []byte { 234 return append(receiptFromParentChainKeyPrefix, blockHash.Bytes()...) 235 } 236 237 func valueTransferTxHashKey(rTxHash common.Hash) []byte { 238 return append(valueTransferTxHashPrefix, rTxHash.Bytes()...) 239 } 240 241 // bloomBitsKey = bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash 242 func BloomBitsKey(bit uint, section uint64, hash common.Hash) []byte { 243 key := append(append(bloomBitsPrefix, make([]byte, 10)...), hash.Bytes()...) 244 245 binary.BigEndian.PutUint16(key[1:], uint16(bit)) 246 binary.BigEndian.PutUint64(key[3:], section) 247 248 return key 249 } 250 251 func makeKey(prefix []byte, num uint64) []byte { 252 byteKey := common.Int64ToByteLittleEndian(num) 253 return append(prefix, byteKey...) 254 } 255 256 func databaseDirKey(dbEntryType uint64) []byte { 257 return append(databaseDirPrefix, common.Int64ToByteBigEndian(dbEntryType)...) 258 } 259 260 // TrieNodeKey = if Legacy, hash32. Otherwise, exthash 261 func TrieNodeKey(hash common.ExtHash) []byte { 262 if hash.IsZeroExtended() { 263 return hash.Unextend().Bytes() 264 } else { 265 return hash.Bytes() 266 } 267 } 268 269 type PruningMark struct { 270 Number uint64 271 Hash common.ExtHash 272 } 273 274 // TriePruningMarkKey = prefix + number + hash 275 // Block number comes first to sort the entries by block numbers. 276 // Later we can iterate through the marks and extract any given block number range. 277 func pruningMarkKey(mark PruningMark) []byte { 278 bNumber := make([]byte, 8) 279 binary.BigEndian.PutUint64(bNumber, mark.Number) 280 bHash := mark.Hash.Bytes() 281 return append(append(pruningMarkPrefix, bNumber...), bHash...) 282 } 283 284 func parsePruningMarkKey(key []byte) PruningMark { 285 if len(key) != pruningMarkKeyLen { 286 logger.Crit("Invalid pruningMarkKey", "key", hexutil.Encode(key)) 287 } 288 prefixLen := len(pruningMarkPrefix) 289 bNumber := key[prefixLen : prefixLen+8] 290 bHash := key[prefixLen+8:] 291 return PruningMark{ 292 Number: binary.BigEndian.Uint64(bNumber), 293 Hash: common.BytesToExtHash(bHash), 294 } 295 }