github.com/dominant-strategies/go-quai@v0.28.2/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  	"bytes"
    22  	"encoding/binary"
    23  
    24  	"github.com/dominant-strategies/go-quai/common"
    25  	"github.com/dominant-strategies/go-quai/metrics"
    26  )
    27  
    28  // The fields below define the low level database schema prefixing.
    29  var (
    30  	// databaseVersionKey tracks the current database version.
    31  	databaseVersionKey = []byte("DatabaseVersion")
    32  
    33  	// headHeaderKey tracks the latest known header's hash.
    34  	headHeaderKey = []byte("LastHeader")
    35  
    36  	// headBlockKey tracks the latest known full block's hash.
    37  	headBlockKey = []byte("LastBlock")
    38  
    39  	// headersHashKey tracks the latest known headers hash in Blockchain.
    40  	headsHashesKey = []byte("HeadersHash")
    41  
    42  	// phCacheKey tracks the latest known pending headers in Blockchain.
    43  	phCacheKey = []byte("PhCache")
    44  
    45  	// phHead tracks the latest known pending headers hash in Blockchain.
    46  	phHeadKey = []byte("PhHead")
    47  
    48  	// lastPivotKey tracks the last pivot block used by fast sync (to reenable on sethead).
    49  	lastPivotKey = []byte("LastPivot")
    50  
    51  	// fastTrieProgressKey tracks the number of trie entries imported during fast sync.
    52  	fastTrieProgressKey = []byte("TrieSync")
    53  
    54  	// snapshotDisabledKey flags that the snapshot should not be maintained due to initial sync.
    55  	snapshotDisabledKey = []byte("SnapshotDisabled")
    56  
    57  	// snapshotRootKey tracks the hash of the last snapshot.
    58  	snapshotRootKey = []byte("SnapshotRoot")
    59  
    60  	// snapshotJournalKey tracks the in-memory diff layers across restarts.
    61  	snapshotJournalKey = []byte("SnapshotJournal")
    62  
    63  	// snapshotGeneratorKey tracks the snapshot generation marker across restarts.
    64  	snapshotGeneratorKey = []byte("SnapshotGenerator")
    65  
    66  	// snapshotRecoveryKey tracks the snapshot recovery marker across restarts.
    67  	snapshotRecoveryKey = []byte("SnapshotRecovery")
    68  
    69  	// snapshotSyncStatusKey tracks the snapshot sync status across restarts.
    70  	snapshotSyncStatusKey = []byte("SnapshotSyncStatus")
    71  
    72  	// txIndexTailKey tracks the oldest block whose transactions have been indexed.
    73  	txIndexTailKey = []byte("TransactionIndexTail")
    74  
    75  	// fastTxLookupLimitKey tracks the transaction lookup limit during fast sync.
    76  	fastTxLookupLimitKey = []byte("FastTransactionLookupLimit")
    77  
    78  	// badBlockKey tracks the list of bad blocks seen by local
    79  	badBlockKey = []byte("InvalidBlock")
    80  
    81  	// uncleanShutdownKey tracks the list of local crashes
    82  	uncleanShutdownKey = []byte("unclean-shutdown") // config prefix for the db
    83  
    84  	// Data item prefixes (use single byte to avoid mixing data types, avoid `i`, used for indexes).
    85  	headerPrefix       = []byte("h") // headerPrefix + num (uint64 big endian) + hash -> header
    86  	headerTDSuffix     = []byte("t") // headerPrefix + num (uint64 big endian) + hash + headerTDSuffix -> td
    87  	headerHashSuffix   = []byte("n") // headerPrefix + num (uint64 big endian) + headerHashSuffix -> hash
    88  	headerNumberPrefix = []byte("H") // headerNumberPrefix + hash -> num (uint64 big endian)
    89  
    90  	pendingHeaderPrefix = []byte("ph")    // pendingHeaderPrefix + hash -> header
    91  	candidateBodyPrefix = []byte("cb")    // candidateBodyPrefix + hash -> Body
    92  	pbBodyPrefix        = []byte("pb")    // pbBodyPrefix + hash -> *types.Body
    93  	pbBodyHashPrefix    = []byte("pbKey") // pbBodyPrefix -> []common.Hash
    94  	phTerminiPrefix     = []byte("pht")   // phTerminiPrefix + hash -> []common.Hash
    95  	phBodyPrefix        = []byte("pc")    // phBodyPrefix + hash -> []common.Hash + Td
    96  	terminiPrefix       = []byte("tk")    //terminiPrefix + hash -> []common.Hash
    97  	badHashesListPrefix = []byte("bh")
    98  	inboundEtxsPrefix   = []byte("ie") // inboundEtxsPrefix + hash -> types.Transactions
    99  
   100  	blockBodyPrefix         = []byte("b")  // blockBodyPrefix + num (uint64 big endian) + hash -> block body
   101  	blockReceiptsPrefix     = []byte("r")  // blockReceiptsPrefix + num (uint64 big endian) + hash -> block receipts
   102  	etxSetPrefix            = []byte("e")  // etxSetPrefix + num (uint64 big endian) + hash -> EtxSet at block
   103  	pendingEtxsPrefix       = []byte("pe") // pendingEtxsPrefix + hash -> PendingEtxs at block
   104  	pendingEtxsRollupPrefix = []byte("pr") // pendingEtxsRollupPrefix + hash -> PendingEtxsRollup at block
   105  	manifestPrefix          = []byte("ma") // manifestPrefix + hash -> Manifest at block
   106  	bloomPrefix             = []byte("bl") // bloomPrefix + hash -> bloom at block
   107  
   108  	txLookupPrefix        = []byte("l") // txLookupPrefix + hash -> transaction/receipt lookup metadata
   109  	bloomBitsPrefix       = []byte("B") // bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash -> bloom bits
   110  	SnapshotAccountPrefix = []byte("a") // SnapshotAccountPrefix + account hash -> account trie value
   111  	SnapshotStoragePrefix = []byte("o") // SnapshotStoragePrefix + account hash + storage hash -> storage trie value
   112  	CodePrefix            = []byte("c") // CodePrefix + code hash -> account code
   113  
   114  	preimagePrefix = []byte("secure-key-")  // preimagePrefix + hash -> preimage
   115  	configPrefix   = []byte("quai-config-") // config prefix for the db
   116  
   117  	// Chain index prefixes (use `i` + single byte to avoid mixing data types).
   118  	BloomBitsIndexPrefix = []byte("iB") // BloomBitsIndexPrefix is the data table of a chain indexer to track its progress
   119  
   120  	preimageCounter    = metrics.NewRegisteredCounter("db/preimage/total", nil)
   121  	preimageHitCounter = metrics.NewRegisteredCounter("db/preimage/hits", nil)
   122  )
   123  
   124  const (
   125  	// freezerHeaderTable indicates the name of the freezer header table.
   126  	freezerHeaderTable = "headers"
   127  
   128  	// freezerHashTable indicates the name of the freezer canonical hash table.
   129  	freezerHashTable = "hashes"
   130  
   131  	// freezerBodiesTable indicates the name of the freezer block body table.
   132  	freezerBodiesTable = "bodies"
   133  
   134  	// freezerReceiptTable indicates the name of the freezer receipts table.
   135  	freezerReceiptTable = "receipts"
   136  
   137  	// freezerDifficultyTable indicates the name of the freezer total difficulty table.
   138  	freezerDifficultyTable = "diffs"
   139  
   140  	// freezerEtxSetsTable indicates the name of the etx set table.
   141  	freezerEtxSetsTable = "etxSets"
   142  )
   143  
   144  // FreezerNoSnappy configures whether compression is disabled for the ancient-tables.
   145  // Hashes and difficulties don't compress well.
   146  var FreezerNoSnappy = map[string]bool{
   147  	freezerHeaderTable:     false,
   148  	freezerHashTable:       true,
   149  	freezerBodiesTable:     false,
   150  	freezerReceiptTable:    false,
   151  	freezerDifficultyTable: true,
   152  	freezerEtxSetsTable:    false,
   153  }
   154  
   155  // LegacyTxLookupEntry is the legacy TxLookupEntry definition with some unnecessary
   156  // fields.
   157  type LegacyTxLookupEntry struct {
   158  	BlockHash  common.Hash
   159  	BlockIndex uint64
   160  	Index      uint64
   161  }
   162  
   163  // encodeBlockNumber encodes a block number as big endian uint64
   164  func encodeBlockNumber(number uint64) []byte {
   165  	enc := make([]byte, 8)
   166  	binary.BigEndian.PutUint64(enc, number)
   167  	return enc
   168  }
   169  
   170  // headerKeyPrefix = headerPrefix + num (uint64 big endian)
   171  func headerKeyPrefix(number uint64) []byte {
   172  	return append(headerPrefix, encodeBlockNumber(number)...)
   173  }
   174  
   175  // headerKey = headerPrefix + num (uint64 big endian) + hash
   176  func headerKey(number uint64, hash common.Hash) []byte {
   177  	return append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
   178  }
   179  
   180  // terminiKey = domPendingHeaderPrefix + hash
   181  func terminiKey(hash common.Hash) []byte {
   182  	return append(terminiPrefix, hash.Bytes()...)
   183  }
   184  
   185  // pendingHeaderKey = pendingHeaderPrefix + hash
   186  func pendingHeaderKey(hash common.Hash) []byte {
   187  	return append(pendingHeaderPrefix, hash.Bytes()...)
   188  }
   189  
   190  // pbBodyKey = pbBodyPrefix + hash
   191  func pbBodyKey(hash common.Hash) []byte {
   192  	return append(pbBodyPrefix, hash.Bytes()...)
   193  }
   194  
   195  // pbBodyHashKey = pbBodyPrefix
   196  func pbBodyHashKey() []byte {
   197  	return pbBodyHashPrefix
   198  }
   199  
   200  // phBodyTerminiKey = phTerminiPrefix + hash
   201  func phBodyTerminiKey(hash common.Hash) []byte {
   202  	return append(phTerminiPrefix, hash.Bytes()...)
   203  }
   204  
   205  // headerTDKey = headerPrefix + num (uint64 big endian) + hash + headerTDSuffix
   206  func headerTDKey(number uint64, hash common.Hash) []byte {
   207  	return append(headerKey(number, hash), headerTDSuffix...)
   208  }
   209  
   210  // headerHashKey = headerPrefix + num (uint64 big endian) + headerHashSuffix
   211  func headerHashKey(number uint64) []byte {
   212  	return append(append(headerPrefix, encodeBlockNumber(number)...), headerHashSuffix...)
   213  }
   214  
   215  // headerNumberKey = headerNumberPrefix + hash
   216  func headerNumberKey(hash common.Hash) []byte {
   217  	return append(headerNumberPrefix, hash.Bytes()...)
   218  }
   219  
   220  // blockBodyKey = blockBodyPrefix + num (uint64 big endian) + hash
   221  func blockBodyKey(number uint64, hash common.Hash) []byte {
   222  	return append(append(blockBodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
   223  }
   224  
   225  // blockReceiptsKey = blockReceiptsPrefix + num (uint64 big endian) + hash
   226  func blockReceiptsKey(number uint64, hash common.Hash) []byte {
   227  	return append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
   228  }
   229  
   230  // txLookupKey = txLookupPrefix + hash
   231  func txLookupKey(hash common.Hash) []byte {
   232  	return append(txLookupPrefix, hash.Bytes()...)
   233  }
   234  
   235  // accountSnapshotKey = SnapshotAccountPrefix + hash
   236  func accountSnapshotKey(hash common.Hash) []byte {
   237  	return append(SnapshotAccountPrefix, hash.Bytes()...)
   238  }
   239  
   240  // storageSnapshotKey = SnapshotStoragePrefix + account hash + storage hash
   241  func storageSnapshotKey(accountHash, storageHash common.Hash) []byte {
   242  	return append(append(SnapshotStoragePrefix, accountHash.Bytes()...), storageHash.Bytes()...)
   243  }
   244  
   245  // storageSnapshotsKey = SnapshotStoragePrefix + account hash + storage hash
   246  func storageSnapshotsKey(accountHash common.Hash) []byte {
   247  	return append(SnapshotStoragePrefix, accountHash.Bytes()...)
   248  }
   249  
   250  // bloomBitsKey = bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash
   251  func bloomBitsKey(bit uint, section uint64, hash common.Hash) []byte {
   252  	key := append(append(bloomBitsPrefix, make([]byte, 10)...), hash.Bytes()...)
   253  
   254  	binary.BigEndian.PutUint16(key[1:], uint16(bit))
   255  	binary.BigEndian.PutUint64(key[3:], section)
   256  
   257  	return key
   258  }
   259  
   260  // preimageKey = preimagePrefix + hash
   261  func preimageKey(hash common.Hash) []byte {
   262  	return append(preimagePrefix, hash.Bytes()...)
   263  }
   264  
   265  // codeKey = CodePrefix + hash
   266  func codeKey(hash common.Hash) []byte {
   267  	return append(CodePrefix, hash.Bytes()...)
   268  }
   269  
   270  // IsCodeKey reports whether the given byte slice is the key of contract code,
   271  // if so return the raw code hash as well.
   272  func IsCodeKey(key []byte) (bool, []byte) {
   273  	if bytes.HasPrefix(key, CodePrefix) && len(key) == common.HashLength+len(CodePrefix) {
   274  		return true, key[len(CodePrefix):]
   275  	}
   276  	return false, nil
   277  }
   278  
   279  // configKey = configPrefix + hash
   280  func configKey(hash common.Hash) []byte {
   281  	return append(configPrefix, hash.Bytes()...)
   282  }
   283  
   284  // etxSetKey = etxSetPrefix + num (uint64 big endian) + hash
   285  func etxSetKey(number uint64, hash common.Hash) []byte {
   286  	return append(append(etxSetPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
   287  }
   288  
   289  // pendingEtxsKey = pendingEtxsPrefix + hash
   290  func pendingEtxsKey(hash common.Hash) []byte {
   291  	return append(pendingEtxsPrefix, hash.Bytes()...)
   292  }
   293  
   294  // pendingEtxsRollupKey = pendingEtxsRollupPrefix + hash
   295  func pendingEtxsRollupKey(hash common.Hash) []byte {
   296  	return append(pendingEtxsRollupPrefix, hash.Bytes()...)
   297  }
   298  
   299  // manifestKey = manifestPrefix + hash
   300  func manifestKey(hash common.Hash) []byte {
   301  	return append(manifestPrefix, hash.Bytes()...)
   302  }
   303  
   304  func bloomKey(hash common.Hash) []byte {
   305  	return append(bloomPrefix, hash.Bytes()...)
   306  }
   307  
   308  func inboundEtxsKey(hash common.Hash) []byte {
   309  	return append(inboundEtxsPrefix, hash.Bytes()...)
   310  }