github.com/MetalBlockchain/subnet-evm@v0.4.9/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  )
    38  
    39  // The fields below define the low level database schema prefixing.
    40  var (
    41  	// databaseVersionKey tracks the current database version.
    42  	databaseVersionKey = []byte("DatabaseVersion")
    43  
    44  	// headHeaderKey tracks the latest known header's hash.
    45  	headHeaderKey = []byte("LastHeader")
    46  
    47  	// headBlockKey tracks the latest known full block's hash.
    48  	headBlockKey = []byte("LastBlock")
    49  
    50  	// snapshotRootKey tracks the hash of the last snapshot.
    51  	snapshotRootKey = []byte("SnapshotRoot")
    52  
    53  	// snapshotBlockHashKey tracks the block hash of the last snapshot.
    54  	snapshotBlockHashKey = []byte("SnapshotBlockHash")
    55  
    56  	// snapshotGeneratorKey tracks the snapshot generation marker across restarts.
    57  	snapshotGeneratorKey = []byte("SnapshotGenerator")
    58  
    59  	// txIndexTailKey tracks the oldest block whose transactions have been indexed.
    60  	txIndexTailKey = []byte("TransactionIndexTail")
    61  
    62  	// uncleanShutdownKey tracks the list of local crashes
    63  	uncleanShutdownKey = []byte("unclean-shutdown") // config prefix for the db
    64  
    65  	// offlinePruningKey tracks runs of offline pruning
    66  	offlinePruningKey = []byte("OfflinePruning")
    67  
    68  	// populateMissingTriesKey tracks runs of trie backfills
    69  	populateMissingTriesKey = []byte("PopulateMissingTries")
    70  
    71  	// pruningDisabledKey tracks whether the node has ever run in archival mode
    72  	// to ensure that a user does not accidentally corrupt an archival node.
    73  	pruningDisabledKey = []byte("PruningDisabled")
    74  
    75  	// acceptorTipKey tracks the tip of the last accepted block that has been fully processed.
    76  	acceptorTipKey = []byte("AcceptorTipKey")
    77  
    78  	// Data item prefixes (use single byte to avoid mixing data types, avoid `i`, used for indexes).
    79  	headerPrefix       = []byte("h") // headerPrefix + num (uint64 big endian) + hash -> header
    80  	headerHashSuffix   = []byte("n") // headerPrefix + num (uint64 big endian) + headerHashSuffix -> hash
    81  	headerNumberPrefix = []byte("H") // headerNumberPrefix + hash -> num (uint64 big endian)
    82  
    83  	blockBodyPrefix     = []byte("b") // blockBodyPrefix + num (uint64 big endian) + hash -> block body
    84  	blockReceiptsPrefix = []byte("r") // blockReceiptsPrefix + num (uint64 big endian) + hash -> block receipts
    85  
    86  	txLookupPrefix        = []byte("l") // txLookupPrefix + hash -> transaction/receipt lookup metadata
    87  	bloomBitsPrefix       = []byte("B") // bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash -> bloom bits
    88  	SnapshotAccountPrefix = []byte("a") // SnapshotAccountPrefix + account hash -> account trie value
    89  	SnapshotStoragePrefix = []byte("o") // SnapshotStoragePrefix + account hash + storage hash -> storage trie value
    90  	CodePrefix            = []byte("c") // CodePrefix + code hash -> account code
    91  
    92  	// State sync progress keys and prefixes
    93  	syncRootKey            = []byte("sync_root")     // indicates the root of the main account trie currently being synced
    94  	syncStorageTriesPrefix = []byte("sync_storage")  // syncStorageTriesPrefix + trie root + account hash: indicates a storage trie must be fetched for the account
    95  	syncSegmentsPrefix     = []byte("sync_segments") // syncSegmentsPrefix + trie root + 32-byte start key: indicates the trie at root has a segment starting at the specified key
    96  	CodeToFetchPrefix      = []byte("CP")            // CodeToFetchPrefix + code hash -> empty value tracks the outstanding code hashes we need to fetch.
    97  
    98  	// State sync progress key lengths
    99  	syncStorageTriesKeyLength = len(syncStorageTriesPrefix) + 2*common.HashLength
   100  	syncSegmentsKeyLength     = len(syncSegmentsPrefix) + 2*common.HashLength
   101  	codeToFetchKeyLength      = len(CodeToFetchPrefix) + common.HashLength
   102  
   103  	// State sync metadata
   104  	syncPerformedPrefix    = []byte("sync_performed")
   105  	syncPerformedKeyLength = len(syncPerformedPrefix) + wrappers.LongLen // prefix + block number as uint64
   106  
   107  	preimagePrefix      = []byte("secure-key-")      // preimagePrefix + hash -> preimage
   108  	configPrefix        = []byte("ethereum-config-") // config prefix for the db
   109  	upgradeConfigPrefix = []byte("upgrade-config-")  // upgrade bytes passed to the chain are stored with this prefix
   110  
   111  	// Chain index prefixes (use `i` + single byte to avoid mixing data types).
   112  	BloomBitsIndexPrefix = []byte("iB") // BloomBitsIndexPrefix is the data table of a chain indexer to track its progress
   113  
   114  	preimageCounter    = metrics.NewRegisteredCounter("db/preimage/total", nil)
   115  	preimageHitCounter = metrics.NewRegisteredCounter("db/preimage/hits", nil)
   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  // headerHashKey = headerPrefix + num (uint64 big endian) + headerHashSuffix
   144  func headerHashKey(number uint64) []byte {
   145  	return append(append(headerPrefix, encodeBlockNumber(number)...), headerHashSuffix...)
   146  }
   147  
   148  // headerNumberKey = headerNumberPrefix + hash
   149  func headerNumberKey(hash common.Hash) []byte {
   150  	return append(headerNumberPrefix, hash.Bytes()...)
   151  }
   152  
   153  // blockBodyKey = blockBodyPrefix + num (uint64 big endian) + hash
   154  func blockBodyKey(number uint64, hash common.Hash) []byte {
   155  	return append(append(blockBodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
   156  }
   157  
   158  // blockReceiptsKey = blockReceiptsPrefix + num (uint64 big endian) + hash
   159  func blockReceiptsKey(number uint64, hash common.Hash) []byte {
   160  	return append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
   161  }
   162  
   163  // txLookupKey = txLookupPrefix + hash
   164  func txLookupKey(hash common.Hash) []byte {
   165  	return append(txLookupPrefix, hash.Bytes()...)
   166  }
   167  
   168  // accountSnapshotKey = SnapshotAccountPrefix + hash
   169  func accountSnapshotKey(hash common.Hash) []byte {
   170  	return append(SnapshotAccountPrefix, hash.Bytes()...)
   171  }
   172  
   173  // storageSnapshotKey = SnapshotStoragePrefix + account hash + storage hash
   174  func storageSnapshotKey(accountHash, storageHash common.Hash) []byte {
   175  	return append(append(SnapshotStoragePrefix, accountHash.Bytes()...), storageHash.Bytes()...)
   176  }
   177  
   178  // storageSnapshotsKey = SnapshotStoragePrefix + account hash + storage hash
   179  func storageSnapshotsKey(accountHash common.Hash) []byte {
   180  	return append(SnapshotStoragePrefix, accountHash.Bytes()...)
   181  }
   182  
   183  // bloomBitsKey = bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash
   184  func bloomBitsKey(bit uint, section uint64, hash common.Hash) []byte {
   185  	key := append(append(bloomBitsPrefix, make([]byte, 10)...), hash.Bytes()...)
   186  
   187  	binary.BigEndian.PutUint16(key[1:], uint16(bit))
   188  	binary.BigEndian.PutUint64(key[3:], section)
   189  
   190  	return key
   191  }
   192  
   193  // preimageKey = preimagePrefix + hash
   194  func preimageKey(hash common.Hash) []byte {
   195  	return append(preimagePrefix, hash.Bytes()...)
   196  }
   197  
   198  // codeKey = CodePrefix + hash
   199  func codeKey(hash common.Hash) []byte {
   200  	return append(CodePrefix, hash.Bytes()...)
   201  }
   202  
   203  // IsCodeKey reports whether the given byte slice is the key of contract code,
   204  // if so return the raw code hash as well.
   205  func IsCodeKey(key []byte) (bool, []byte) {
   206  	if bytes.HasPrefix(key, CodePrefix) && len(key) == common.HashLength+len(CodePrefix) {
   207  		return true, key[len(CodePrefix):]
   208  	}
   209  	return false, nil
   210  }
   211  
   212  // configKey = configPrefix + hash
   213  func configKey(hash common.Hash) []byte {
   214  	return append(configPrefix, hash.Bytes()...)
   215  }
   216  
   217  // upgradeConfigKey = upgradeConfigPrefix + hash
   218  func upgradeConfigKey(hash common.Hash) []byte {
   219  	return append(upgradeConfigPrefix, hash.Bytes()...)
   220  }