github.com/MetalBlockchain/subnet-evm@v0.6.3/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/MetalBlockchain/metalgo/utils/wrappers" 35 "github.com/MetalBlockchain/subnet-evm/metrics" 36 "github.com/ethereum/go-ethereum/common" 37 "github.com/ethereum/go-ethereum/crypto" 38 ) 39 40 // The fields below define the low level database schema prefixing. 41 var ( 42 // databaseVersionKey tracks the current database version. 43 databaseVersionKey = []byte("DatabaseVersion") 44 45 // headHeaderKey tracks the latest known header's hash. 46 headHeaderKey = []byte("LastHeader") 47 48 // headBlockKey tracks the latest known full block's hash. 49 headBlockKey = []byte("LastBlock") 50 51 // persistentStateIDKey tracks the id of latest stored state(for path-based only). 52 persistentStateIDKey = []byte("LastStateID") 53 54 // snapshotRootKey tracks the hash of the last snapshot. 55 snapshotRootKey = []byte("SnapshotRoot") 56 57 // snapshotBlockHashKey tracks the block hash of the last snapshot. 58 snapshotBlockHashKey = []byte("SnapshotBlockHash") 59 60 // snapshotGeneratorKey tracks the snapshot generation marker across restarts. 61 snapshotGeneratorKey = []byte("SnapshotGenerator") 62 63 // trieJournalKey tracks the in-memory trie node layers across restarts. 64 trieJournalKey = []byte("TrieJournal") 65 66 // txIndexTailKey tracks the oldest block whose transactions have been indexed. 67 txIndexTailKey = []byte("TransactionIndexTail") 68 69 // uncleanShutdownKey tracks the list of local crashes 70 uncleanShutdownKey = []byte("unclean-shutdown") // config prefix for the db 71 72 // offlinePruningKey tracks runs of offline pruning 73 offlinePruningKey = []byte("OfflinePruning") 74 75 // populateMissingTriesKey tracks runs of trie backfills 76 populateMissingTriesKey = []byte("PopulateMissingTries") 77 78 // pruningDisabledKey tracks whether the node has ever run in archival mode 79 // to ensure that a user does not accidentally corrupt an archival node. 80 pruningDisabledKey = []byte("PruningDisabled") 81 82 // acceptorTipKey tracks the tip of the last accepted block that has been fully processed. 83 acceptorTipKey = []byte("AcceptorTipKey") 84 85 // Data item prefixes (use single byte to avoid mixing data types, avoid `i`, used for indexes). 86 headerPrefix = []byte("h") // headerPrefix + num (uint64 big endian) + hash -> header 87 headerHashSuffix = []byte("n") // headerPrefix + num (uint64 big endian) + headerHashSuffix -> hash 88 headerNumberPrefix = []byte("H") // headerNumberPrefix + hash -> num (uint64 big endian) 89 90 blockBodyPrefix = []byte("b") // blockBodyPrefix + num (uint64 big endian) + hash -> block body 91 blockReceiptsPrefix = []byte("r") // blockReceiptsPrefix + num (uint64 big endian) + hash -> block receipts 92 93 txLookupPrefix = []byte("l") // txLookupPrefix + hash -> transaction/receipt lookup metadata 94 bloomBitsPrefix = []byte("B") // bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash -> bloom bits 95 SnapshotAccountPrefix = []byte("a") // SnapshotAccountPrefix + account hash -> account trie value 96 SnapshotStoragePrefix = []byte("o") // SnapshotStoragePrefix + account hash + storage hash -> storage trie value 97 CodePrefix = []byte("c") // CodePrefix + code hash -> account code 98 99 // Path-based storage scheme of merkle patricia trie. 100 trieNodeAccountPrefix = []byte("A") // trieNodeAccountPrefix + hexPath -> trie node 101 trieNodeStoragePrefix = []byte("O") // trieNodeStoragePrefix + accountHash + hexPath -> trie node 102 stateIDPrefix = []byte("L") // stateIDPrefix + state root -> state id 103 104 PreimagePrefix = []byte("secure-key-") // PreimagePrefix + hash -> preimage 105 configPrefix = []byte("ethereum-config-") // config prefix for the db 106 upgradeConfigPrefix = []byte("upgrade-config-") // upgrade bytes passed to the chain are stored with this prefix 107 108 // BloomBitsIndexPrefix is the data table of a chain indexer to track its progress 109 BloomBitsIndexPrefix = []byte("iB") 110 111 preimageCounter = metrics.NewRegisteredCounter("db/preimage/total", nil) 112 preimageHitCounter = metrics.NewRegisteredCounter("db/preimage/hits", nil) 113 114 // State sync progress keys and prefixes 115 syncRootKey = []byte("sync_root") // indicates the root of the main account trie currently being synced 116 syncStorageTriesPrefix = []byte("sync_storage") // syncStorageTriesPrefix + trie root + account hash: indicates a storage trie must be fetched for the account 117 syncSegmentsPrefix = []byte("sync_segments") // syncSegmentsPrefix + trie root + 32-byte start key: indicates the trie at root has a segment starting at the specified key 118 CodeToFetchPrefix = []byte("CP") // CodeToFetchPrefix + code hash -> empty value tracks the outstanding code hashes we need to fetch. 119 120 // State sync progress key lengths 121 syncStorageTriesKeyLength = len(syncStorageTriesPrefix) + 2*common.HashLength 122 syncSegmentsKeyLength = len(syncSegmentsPrefix) + 2*common.HashLength 123 codeToFetchKeyLength = len(CodeToFetchPrefix) + common.HashLength 124 125 // State sync metadata 126 syncPerformedPrefix = []byte("sync_performed") 127 syncPerformedKeyLength = len(syncPerformedPrefix) + wrappers.LongLen // prefix + block number as uint64 128 ) 129 130 // LegacyTxLookupEntry is the legacy TxLookupEntry definition with some unnecessary 131 // fields. 132 type LegacyTxLookupEntry struct { 133 BlockHash common.Hash 134 BlockIndex uint64 135 Index uint64 136 } 137 138 // encodeBlockNumber encodes a block number as big endian uint64 139 func encodeBlockNumber(number uint64) []byte { 140 enc := make([]byte, 8) 141 binary.BigEndian.PutUint64(enc, number) 142 return enc 143 } 144 145 // headerKeyPrefix = headerPrefix + num (uint64 big endian) 146 func headerKeyPrefix(number uint64) []byte { 147 return append(headerPrefix, encodeBlockNumber(number)...) 148 } 149 150 // headerKey = headerPrefix + num (uint64 big endian) + hash 151 func headerKey(number uint64, hash common.Hash) []byte { 152 return append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...) 153 } 154 155 // headerHashKey = headerPrefix + num (uint64 big endian) + headerHashSuffix 156 func headerHashKey(number uint64) []byte { 157 return append(append(headerPrefix, encodeBlockNumber(number)...), headerHashSuffix...) 158 } 159 160 // headerNumberKey = headerNumberPrefix + hash 161 func headerNumberKey(hash common.Hash) []byte { 162 return append(headerNumberPrefix, hash.Bytes()...) 163 } 164 165 // blockBodyKey = blockBodyPrefix + num (uint64 big endian) + hash 166 func blockBodyKey(number uint64, hash common.Hash) []byte { 167 return append(append(blockBodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...) 168 } 169 170 // blockReceiptsKey = blockReceiptsPrefix + num (uint64 big endian) + hash 171 func blockReceiptsKey(number uint64, hash common.Hash) []byte { 172 return append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...) 173 } 174 175 // txLookupKey = txLookupPrefix + hash 176 func txLookupKey(hash common.Hash) []byte { 177 return append(txLookupPrefix, hash.Bytes()...) 178 } 179 180 // accountSnapshotKey = SnapshotAccountPrefix + hash 181 func accountSnapshotKey(hash common.Hash) []byte { 182 return append(SnapshotAccountPrefix, hash.Bytes()...) 183 } 184 185 // storageSnapshotKey = SnapshotStoragePrefix + account hash + storage hash 186 func storageSnapshotKey(accountHash, storageHash common.Hash) []byte { 187 return append(append(SnapshotStoragePrefix, accountHash.Bytes()...), storageHash.Bytes()...) 188 } 189 190 // storageSnapshotsKey = SnapshotStoragePrefix + account hash + storage hash 191 func storageSnapshotsKey(accountHash common.Hash) []byte { 192 return append(SnapshotStoragePrefix, accountHash.Bytes()...) 193 } 194 195 // bloomBitsKey = bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash 196 func bloomBitsKey(bit uint, section uint64, hash common.Hash) []byte { 197 key := append(append(bloomBitsPrefix, make([]byte, 10)...), hash.Bytes()...) 198 199 binary.BigEndian.PutUint16(key[1:], uint16(bit)) 200 binary.BigEndian.PutUint64(key[3:], section) 201 202 return key 203 } 204 205 // preimageKey = preimagePrefix + hash 206 func preimageKey(hash common.Hash) []byte { 207 return append(PreimagePrefix, hash.Bytes()...) 208 } 209 210 // codeKey = CodePrefix + hash 211 func codeKey(hash common.Hash) []byte { 212 return append(CodePrefix, hash.Bytes()...) 213 } 214 215 // IsCodeKey reports whether the given byte slice is the key of contract code, 216 // if so return the raw code hash as well. 217 func IsCodeKey(key []byte) (bool, []byte) { 218 if bytes.HasPrefix(key, CodePrefix) && len(key) == common.HashLength+len(CodePrefix) { 219 return true, key[len(CodePrefix):] 220 } 221 return false, nil 222 } 223 224 // configKey = configPrefix + hash 225 func configKey(hash common.Hash) []byte { 226 return append(configPrefix, hash.Bytes()...) 227 } 228 229 // upgradeConfigKey = upgradeConfigPrefix + hash 230 func upgradeConfigKey(hash common.Hash) []byte { 231 return append(upgradeConfigPrefix, hash.Bytes()...) 232 } 233 234 // stateIDKey = stateIDPrefix + root (32 bytes) 235 func stateIDKey(root common.Hash) []byte { 236 return append(stateIDPrefix, root.Bytes()...) 237 } 238 239 // accountTrieNodeKey = trieNodeAccountPrefix + nodePath. 240 func accountTrieNodeKey(path []byte) []byte { 241 return append(trieNodeAccountPrefix, path...) 242 } 243 244 // storageTrieNodeKey = trieNodeStoragePrefix + accountHash + nodePath. 245 func storageTrieNodeKey(accountHash common.Hash, path []byte) []byte { 246 return append(append(trieNodeStoragePrefix, accountHash.Bytes()...), path...) 247 } 248 249 // IsLegacyTrieNode reports whether a provided database entry is a legacy trie 250 // node. The characteristics of legacy trie node are: 251 // - the key length is 32 bytes 252 // - the key is the hash of val 253 func IsLegacyTrieNode(key []byte, val []byte) bool { 254 if len(key) != common.HashLength { 255 return false 256 } 257 return bytes.Equal(key, crypto.Keccak256(val)) 258 } 259 260 // IsAccountTrieNode reports whether a provided database entry is an account 261 // trie node in path-based state scheme. 262 func IsAccountTrieNode(key []byte) (bool, []byte) { 263 if !bytes.HasPrefix(key, trieNodeAccountPrefix) { 264 return false, nil 265 } 266 // The remaining key should only consist a hex node path 267 // whose length is in the range 0 to 64 (64 is excluded 268 // since leaves are always wrapped with shortNode). 269 if len(key) >= len(trieNodeAccountPrefix)+common.HashLength*2 { 270 return false, nil 271 } 272 return true, key[len(trieNodeAccountPrefix):] 273 } 274 275 // IsStorageTrieNode reports whether a provided database entry is a storage 276 // trie node in path-based state scheme. 277 func IsStorageTrieNode(key []byte) (bool, common.Hash, []byte) { 278 if !bytes.HasPrefix(key, trieNodeStoragePrefix) { 279 return false, common.Hash{}, nil 280 } 281 // The remaining key consists of 2 parts: 282 // - 32 bytes account hash 283 // - hex node path whose length is in the range 0 to 64 284 if len(key) < len(trieNodeStoragePrefix)+common.HashLength { 285 return false, common.Hash{}, nil 286 } 287 if len(key) >= len(trieNodeStoragePrefix)+common.HashLength+common.HashLength*2 { 288 return false, common.Hash{}, nil 289 } 290 accountHash := common.BytesToHash(key[len(trieNodeStoragePrefix) : len(trieNodeStoragePrefix)+common.HashLength]) 291 return true, accountHash, key[len(trieNodeStoragePrefix)+common.HashLength:] 292 }