github.com/dim4egster/coreth@v0.10.2/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/dim4egster/coreth/metrics"
    35  	"github.com/ethereum/go-ethereum/common"
    36  )
    37  
    38  // The fields below define the low level database schema prefixing.
    39  var (
    40  	// databaseVersionKey tracks the current database version.
    41  	databaseVersionKey = []byte("DatabaseVersion")
    42  
    43  	// headHeaderKey tracks the latest known header's hash.
    44  	headHeaderKey = []byte("LastHeader")
    45  
    46  	// headBlockKey tracks the latest known full block's hash.
    47  	headBlockKey = []byte("LastBlock")
    48  
    49  	// snapshotRootKey tracks the hash of the last snapshot.
    50  	snapshotRootKey = []byte("SnapshotRoot")
    51  
    52  	// snapshotBlockHashKey tracks the block hash of the last snapshot.
    53  	snapshotBlockHashKey = []byte("SnapshotBlockHash")
    54  
    55  	// snapshotGeneratorKey tracks the snapshot generation marker across restarts.
    56  	snapshotGeneratorKey = []byte("SnapshotGenerator")
    57  
    58  	// uncleanShutdownKey tracks the list of local crashes
    59  	uncleanShutdownKey = []byte("unclean-shutdown") // config prefix for the db
    60  
    61  	// offlinePruningKey tracks runs of offline pruning
    62  	offlinePruningKey = []byte("OfflinePruning")
    63  
    64  	// populateMissingTriesKey tracks runs of trie backfills
    65  	populateMissingTriesKey = []byte("PopulateMissingTries")
    66  
    67  	// pruningDisabledKey tracks whether the node has ever run in archival mode
    68  	// to ensure that a user does not accidentally corrupt an archival node.
    69  	pruningDisabledKey = []byte("PruningDisabled")
    70  
    71  	// acceptorTipKey tracks the tip of the last accepted block that has been fully processed.
    72  	acceptorTipKey = []byte("AcceptorTipKey")
    73  
    74  	// Data item prefixes (use single byte to avoid mixing data types, avoid `i`, used for indexes).
    75  	headerPrefix       = []byte("h") // headerPrefix + num (uint64 big endian) + hash -> header
    76  	headerHashSuffix   = []byte("n") // headerPrefix + num (uint64 big endian) + headerHashSuffix -> hash
    77  	headerNumberPrefix = []byte("H") // headerNumberPrefix + hash -> num (uint64 big endian)
    78  
    79  	blockBodyPrefix     = []byte("b") // blockBodyPrefix + num (uint64 big endian) + hash -> block body
    80  	blockReceiptsPrefix = []byte("r") // blockReceiptsPrefix + num (uint64 big endian) + hash -> block receipts
    81  
    82  	txLookupPrefix        = []byte("l") // txLookupPrefix + hash -> transaction/receipt lookup metadata
    83  	bloomBitsPrefix       = []byte("B") // bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash -> bloom bits
    84  	SnapshotAccountPrefix = []byte("a") // SnapshotAccountPrefix + account hash -> account trie value
    85  	SnapshotStoragePrefix = []byte("o") // SnapshotStoragePrefix + account hash + storage hash -> storage trie value
    86  	CodePrefix            = []byte("c") // CodePrefix + code hash -> account code
    87  
    88  	// State sync progress keys and prefixes
    89  	syncRootKey            = []byte("sync_root")     // indicates the root of the main account trie currently being synced
    90  	syncStorageTriesPrefix = []byte("sync_storage")  // syncStorageTriesPrefix + trie root + account hash: indicates a storage trie must be fetched for the account
    91  	syncSegmentsPrefix     = []byte("sync_segments") // syncSegmentsPrefix + trie root + 32-byte start key: indicates the trie at root has a segment starting at the specified key
    92  	CodeToFetchPrefix      = []byte("CP")            // CodeToFetchPrefix + code hash -> empty value tracks the outstanding code hashes we need to fetch.
    93  
    94  	// State sync progress key lengths
    95  	syncStorageTriesKeyLength = len(syncStorageTriesPrefix) + 2*common.HashLength
    96  	syncSegmentsKeyLength     = len(syncSegmentsPrefix) + 2*common.HashLength
    97  	codeToFetchKeyLength      = len(CodeToFetchPrefix) + common.HashLength
    98  
    99  	preimagePrefix = []byte("secure-key-")      // preimagePrefix + hash -> preimage
   100  	configPrefix   = []byte("ethereum-config-") // config prefix for the db
   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  
   109  // LegacyTxLookupEntry is the legacy TxLookupEntry definition with some unnecessary
   110  // fields.
   111  type LegacyTxLookupEntry struct {
   112  	BlockHash  common.Hash
   113  	BlockIndex uint64
   114  	Index      uint64
   115  }
   116  
   117  // encodeBlockNumber encodes a block number as big endian uint64
   118  func encodeBlockNumber(number uint64) []byte {
   119  	enc := make([]byte, 8)
   120  	binary.BigEndian.PutUint64(enc, number)
   121  	return enc
   122  }
   123  
   124  // headerKeyPrefix = headerPrefix + num (uint64 big endian)
   125  func headerKeyPrefix(number uint64) []byte {
   126  	return append(headerPrefix, encodeBlockNumber(number)...)
   127  }
   128  
   129  // headerKey = headerPrefix + num (uint64 big endian) + hash
   130  func headerKey(number uint64, hash common.Hash) []byte {
   131  	return append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
   132  }
   133  
   134  // headerHashKey = headerPrefix + num (uint64 big endian) + headerHashSuffix
   135  func headerHashKey(number uint64) []byte {
   136  	return append(append(headerPrefix, encodeBlockNumber(number)...), headerHashSuffix...)
   137  }
   138  
   139  // headerNumberKey = headerNumberPrefix + hash
   140  func headerNumberKey(hash common.Hash) []byte {
   141  	return append(headerNumberPrefix, hash.Bytes()...)
   142  }
   143  
   144  // blockBodyKey = blockBodyPrefix + num (uint64 big endian) + hash
   145  func blockBodyKey(number uint64, hash common.Hash) []byte {
   146  	return append(append(blockBodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
   147  }
   148  
   149  // blockReceiptsKey = blockReceiptsPrefix + num (uint64 big endian) + hash
   150  func blockReceiptsKey(number uint64, hash common.Hash) []byte {
   151  	return append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
   152  }
   153  
   154  // txLookupKey = txLookupPrefix + hash
   155  func txLookupKey(hash common.Hash) []byte {
   156  	return append(txLookupPrefix, hash.Bytes()...)
   157  }
   158  
   159  // accountSnapshotKey = SnapshotAccountPrefix + hash
   160  func accountSnapshotKey(hash common.Hash) []byte {
   161  	return append(SnapshotAccountPrefix, hash.Bytes()...)
   162  }
   163  
   164  // storageSnapshotKey = SnapshotStoragePrefix + account hash + storage hash
   165  func storageSnapshotKey(accountHash, storageHash common.Hash) []byte {
   166  	return append(append(SnapshotStoragePrefix, accountHash.Bytes()...), storageHash.Bytes()...)
   167  }
   168  
   169  // storageSnapshotsKey = SnapshotStoragePrefix + account hash + storage hash
   170  func storageSnapshotsKey(accountHash common.Hash) []byte {
   171  	return append(SnapshotStoragePrefix, accountHash.Bytes()...)
   172  }
   173  
   174  // bloomBitsKey = bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash
   175  func bloomBitsKey(bit uint, section uint64, hash common.Hash) []byte {
   176  	key := append(append(bloomBitsPrefix, make([]byte, 10)...), hash.Bytes()...)
   177  
   178  	binary.BigEndian.PutUint16(key[1:], uint16(bit))
   179  	binary.BigEndian.PutUint64(key[3:], section)
   180  
   181  	return key
   182  }
   183  
   184  // preimageKey = preimagePrefix + hash
   185  func preimageKey(hash common.Hash) []byte {
   186  	return append(preimagePrefix, hash.Bytes()...)
   187  }
   188  
   189  // codeKey = CodePrefix + hash
   190  func codeKey(hash common.Hash) []byte {
   191  	return append(CodePrefix, hash.Bytes()...)
   192  }
   193  
   194  // IsCodeKey reports whether the given byte slice is the key of contract code,
   195  // if so return the raw code hash as well.
   196  func IsCodeKey(key []byte) (bool, []byte) {
   197  	if bytes.HasPrefix(key, CodePrefix) && len(key) == common.HashLength+len(CodePrefix) {
   198  		return true, key[len(CodePrefix):]
   199  	}
   200  	return false, nil
   201  }
   202  
   203  // configKey = configPrefix + hash
   204  func configKey(hash common.Hash) []byte {
   205  	return append(configPrefix, hash.Bytes()...)
   206  }