github.com/gilgames000/kcc-geth@v1.0.6/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/ethereum/go-ethereum/common"
    25  	"github.com/ethereum/go-ethereum/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  	// headFastBlockKey tracks the latest known incomplete block's hash during fast sync.
    40  	headFastBlockKey = []byte("LastFast")
    41  
    42  	// lastPivotKey tracks the last pivot block used by fast sync (to reenable on sethead).
    43  	lastPivotKey = []byte("LastPivot")
    44  
    45  	// fastTrieProgressKey tracks the number of trie entries imported during fast sync.
    46  	fastTrieProgressKey = []byte("TrieSync")
    47  
    48  	// snapshotRootKey tracks the hash of the last snapshot.
    49  	snapshotRootKey = []byte("SnapshotRoot")
    50  
    51  	// snapshotJournalKey tracks the in-memory diff layers across restarts.
    52  	snapshotJournalKey = []byte("SnapshotJournal")
    53  
    54  	// snapshotGeneratorKey tracks the snapshot generation marker across restarts.
    55  	snapshotGeneratorKey = []byte("SnapshotGenerator")
    56  
    57  	// snapshotRecoveryKey tracks the snapshot recovery marker across restarts.
    58  	snapshotRecoveryKey = []byte("SnapshotRecovery")
    59  
    60  	// snapshotSyncStatusKey tracks the snapshot sync status across restarts.
    61  	snapshotSyncStatusKey = []byte("SnapshotSyncStatus")
    62  
    63  	// txIndexTailKey tracks the oldest block whose transactions have been indexed.
    64  	txIndexTailKey = []byte("TransactionIndexTail")
    65  
    66  	// fastTxLookupLimitKey tracks the transaction lookup limit during fast sync.
    67  	fastTxLookupLimitKey = []byte("FastTransactionLookupLimit")
    68  
    69  	// badBlockKey tracks the list of bad blocks seen by local
    70  	badBlockKey = []byte("InvalidBlock")
    71  
    72  	// uncleanShutdownKey tracks the list of local crashes
    73  	uncleanShutdownKey = []byte("unclean-shutdown") // config prefix for the db
    74  
    75  	// Data item prefixes (use single byte to avoid mixing data types, avoid `i`, used for indexes).
    76  	headerPrefix       = []byte("h") // headerPrefix + num (uint64 big endian) + hash -> header
    77  	headerTDSuffix     = []byte("t") // headerPrefix + num (uint64 big endian) + hash + headerTDSuffix -> td
    78  	headerHashSuffix   = []byte("n") // headerPrefix + num (uint64 big endian) + headerHashSuffix -> hash
    79  	headerNumberPrefix = []byte("H") // headerNumberPrefix + hash -> num (uint64 big endian)
    80  
    81  	blockBodyPrefix     = []byte("b") // blockBodyPrefix + num (uint64 big endian) + hash -> block body
    82  	blockReceiptsPrefix = []byte("r") // blockReceiptsPrefix + num (uint64 big endian) + hash -> block receipts
    83  
    84  	txLookupPrefix        = []byte("l") // txLookupPrefix + hash -> transaction/receipt lookup metadata
    85  	bloomBitsPrefix       = []byte("B") // bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash -> bloom bits
    86  	SnapshotAccountPrefix = []byte("a") // SnapshotAccountPrefix + account hash -> account trie value
    87  	SnapshotStoragePrefix = []byte("o") // SnapshotStoragePrefix + account hash + storage hash -> storage trie value
    88  	CodePrefix            = []byte("c") // CodePrefix + code hash -> account code
    89  
    90  	preimagePrefix = []byte("secure-key-")      // preimagePrefix + hash -> preimage
    91  	configPrefix   = []byte("ethereum-config-") // config prefix for the db
    92  
    93  	// Chain index prefixes (use `i` + single byte to avoid mixing data types).
    94  	BloomBitsIndexPrefix = []byte("iB") // BloomBitsIndexPrefix is the data table of a chain indexer to track its progress
    95  
    96  	preimageCounter    = metrics.NewRegisteredCounter("db/preimage/total", nil)
    97  	preimageHitCounter = metrics.NewRegisteredCounter("db/preimage/hits", nil)
    98  )
    99  
   100  const (
   101  	// freezerHeaderTable indicates the name of the freezer header table.
   102  	freezerHeaderTable = "headers"
   103  
   104  	// freezerHashTable indicates the name of the freezer canonical hash table.
   105  	freezerHashTable = "hashes"
   106  
   107  	// freezerBodiesTable indicates the name of the freezer block body table.
   108  	freezerBodiesTable = "bodies"
   109  
   110  	// freezerReceiptTable indicates the name of the freezer receipts table.
   111  	freezerReceiptTable = "receipts"
   112  
   113  	// freezerDifficultyTable indicates the name of the freezer total difficulty table.
   114  	freezerDifficultyTable = "diffs"
   115  )
   116  
   117  // freezerNoSnappy configures whether compression is disabled for the ancient-tables.
   118  // Hashes and difficulties don't compress well.
   119  var freezerNoSnappy = map[string]bool{
   120  	freezerHeaderTable:     false,
   121  	freezerHashTable:       true,
   122  	freezerBodiesTable:     false,
   123  	freezerReceiptTable:    false,
   124  	freezerDifficultyTable: true,
   125  }
   126  
   127  // LegacyTxLookupEntry is the legacy TxLookupEntry definition with some unnecessary
   128  // fields.
   129  type LegacyTxLookupEntry struct {
   130  	BlockHash  common.Hash
   131  	BlockIndex uint64
   132  	Index      uint64
   133  }
   134  
   135  // encodeBlockNumber encodes a block number as big endian uint64
   136  func encodeBlockNumber(number uint64) []byte {
   137  	enc := make([]byte, 8)
   138  	binary.BigEndian.PutUint64(enc, number)
   139  	return enc
   140  }
   141  
   142  // headerKeyPrefix = headerPrefix + num (uint64 big endian)
   143  func headerKeyPrefix(number uint64) []byte {
   144  	return append(headerPrefix, encodeBlockNumber(number)...)
   145  }
   146  
   147  // headerKey = headerPrefix + num (uint64 big endian) + hash
   148  func headerKey(number uint64, hash common.Hash) []byte {
   149  	return append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
   150  }
   151  
   152  // headerTDKey = headerPrefix + num (uint64 big endian) + hash + headerTDSuffix
   153  func headerTDKey(number uint64, hash common.Hash) []byte {
   154  	return append(headerKey(number, hash), headerTDSuffix...)
   155  }
   156  
   157  // headerHashKey = headerPrefix + num (uint64 big endian) + headerHashSuffix
   158  func headerHashKey(number uint64) []byte {
   159  	return append(append(headerPrefix, encodeBlockNumber(number)...), headerHashSuffix...)
   160  }
   161  
   162  // headerNumberKey = headerNumberPrefix + hash
   163  func headerNumberKey(hash common.Hash) []byte {
   164  	return append(headerNumberPrefix, hash.Bytes()...)
   165  }
   166  
   167  // blockBodyKey = blockBodyPrefix + num (uint64 big endian) + hash
   168  func blockBodyKey(number uint64, hash common.Hash) []byte {
   169  	return append(append(blockBodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
   170  }
   171  
   172  // blockReceiptsKey = blockReceiptsPrefix + num (uint64 big endian) + hash
   173  func blockReceiptsKey(number uint64, hash common.Hash) []byte {
   174  	return append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
   175  }
   176  
   177  // txLookupKey = txLookupPrefix + hash
   178  func txLookupKey(hash common.Hash) []byte {
   179  	return append(txLookupPrefix, hash.Bytes()...)
   180  }
   181  
   182  // accountSnapshotKey = SnapshotAccountPrefix + hash
   183  func accountSnapshotKey(hash common.Hash) []byte {
   184  	return append(SnapshotAccountPrefix, hash.Bytes()...)
   185  }
   186  
   187  // storageSnapshotKey = SnapshotStoragePrefix + account hash + storage hash
   188  func storageSnapshotKey(accountHash, storageHash common.Hash) []byte {
   189  	return append(append(SnapshotStoragePrefix, accountHash.Bytes()...), storageHash.Bytes()...)
   190  }
   191  
   192  // storageSnapshotsKey = SnapshotStoragePrefix + account hash + storage hash
   193  func storageSnapshotsKey(accountHash common.Hash) []byte {
   194  	return append(SnapshotStoragePrefix, accountHash.Bytes()...)
   195  }
   196  
   197  // bloomBitsKey = bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash
   198  func bloomBitsKey(bit uint, section uint64, hash common.Hash) []byte {
   199  	key := append(append(bloomBitsPrefix, make([]byte, 10)...), hash.Bytes()...)
   200  
   201  	binary.BigEndian.PutUint16(key[1:], uint16(bit))
   202  	binary.BigEndian.PutUint64(key[3:], section)
   203  
   204  	return key
   205  }
   206  
   207  // preimageKey = preimagePrefix + hash
   208  func preimageKey(hash common.Hash) []byte {
   209  	return append(preimagePrefix, hash.Bytes()...)
   210  }
   211  
   212  // codeKey = CodePrefix + hash
   213  func codeKey(hash common.Hash) []byte {
   214  	return append(CodePrefix, hash.Bytes()...)
   215  }
   216  
   217  // IsCodeKey reports whether the given byte slice is the key of contract code,
   218  // if so return the raw code hash as well.
   219  func IsCodeKey(key []byte) (bool, []byte) {
   220  	if bytes.HasPrefix(key, CodePrefix) && len(key) == common.HashLength+len(CodePrefix) {
   221  		return true, key[len(CodePrefix):]
   222  	}
   223  	return false, nil
   224  }
   225  
   226  // configKey = configPrefix + hash
   227  func configKey(hash common.Hash) []byte {
   228  	return append(configPrefix, hash.Bytes()...)
   229  }