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