github.com/ccm-chain/ccmchain@v1.0.0/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/ccm-chain/ccmchain/common"
    25  	"github.com/ccm-chain/ccmchain/metrics"
    26  )
    27  
    28  // The fields below define the low level database schema prefixing.
    29  var (
    30  	// databaseVerisionKey tracks the current database version.
    31  	databaseVerisionKey = []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  	// txIndexTailKey tracks the oldest block whose transactions have been indexed.
    55  	txIndexTailKey = []byte("TransactionIndexTail")
    56  
    57  	// fastTxLookupLimitKey tracks the transaction lookup limit during fast sync.
    58  	fastTxLookupLimitKey = []byte("FastTransactionLookupLimit")
    59  
    60  	// Data item prefixes (use single byte to avoid mixing data types, avoid `i`, used for indexes).
    61  	headerPrefix       = []byte("h") // headerPrefix + num (uint64 big endian) + hash -> header
    62  	headerTDSuffix     = []byte("t") // headerPrefix + num (uint64 big endian) + hash + headerTDSuffix -> td
    63  	headerHashSuffix   = []byte("n") // headerPrefix + num (uint64 big endian) + headerHashSuffix -> hash
    64  	headerNumberPrefix = []byte("H") // headerNumberPrefix + hash -> num (uint64 big endian)
    65  
    66  	blockBodyPrefix     = []byte("b") // blockBodyPrefix + num (uint64 big endian) + hash -> block body
    67  	blockReceiptsPrefix = []byte("r") // blockReceiptsPrefix + num (uint64 big endian) + hash -> block receipts
    68  
    69  	txLookupPrefix        = []byte("l") // txLookupPrefix + hash -> transaction/receipt lookup metadata
    70  	bloomBitsPrefix       = []byte("B") // bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash -> bloom bits
    71  	SnapshotAccountPrefix = []byte("a") // SnapshotAccountPrefix + account hash -> account trie value
    72  	SnapshotStoragePrefix = []byte("o") // SnapshotStoragePrefix + account hash + storage hash -> storage trie value
    73  	codePrefix            = []byte("c") // codePrefix + code hash -> account code
    74  
    75  	preimagePrefix = []byte("secure-key-")      // preimagePrefix + hash -> preimage
    76  	configPrefix   = []byte("ccmchain-config-") // config prefix for the db
    77  
    78  	// Chain index prefixes (use `i` + single byte to avoid mixing data types).
    79  	BloomBitsIndexPrefix = []byte("iB") // BloomBitsIndexPrefix is the data table of a chain indexer to track its progress
    80  
    81  	preimageCounter    = metrics.NewRegisteredCounter("db/preimage/total", nil)
    82  	preimageHitCounter = metrics.NewRegisteredCounter("db/preimage/hits", nil)
    83  )
    84  
    85  const (
    86  	// freezerHeaderTable indicates the name of the freezer header table.
    87  	freezerHeaderTable = "headers"
    88  
    89  	// freezerHashTable indicates the name of the freezer canonical hash table.
    90  	freezerHashTable = "hashes"
    91  
    92  	// freezerBodiesTable indicates the name of the freezer block body table.
    93  	freezerBodiesTable = "bodies"
    94  
    95  	// freezerReceiptTable indicates the name of the freezer receipts table.
    96  	freezerReceiptTable = "receipts"
    97  
    98  	// freezerDifficultyTable indicates the name of the freezer total difficulty table.
    99  	freezerDifficultyTable = "diffs"
   100  )
   101  
   102  // freezerNoSnappy configures whether compression is disabled for the ancient-tables.
   103  // Hashes and difficulties don't compress well.
   104  var freezerNoSnappy = map[string]bool{
   105  	freezerHeaderTable:     false,
   106  	freezerHashTable:       true,
   107  	freezerBodiesTable:     false,
   108  	freezerReceiptTable:    false,
   109  	freezerDifficultyTable: true,
   110  }
   111  
   112  // LegacyTxLookupEntry is the legacy TxLookupEntry definition with some unnecessary
   113  // fields.
   114  type LegacyTxLookupEntry struct {
   115  	BlockHash  common.Hash
   116  	BlockIndex uint64
   117  	Index      uint64
   118  }
   119  
   120  // encodeBlockNumber encodes a block number as big endian uint64
   121  func encodeBlockNumber(number uint64) []byte {
   122  	enc := make([]byte, 8)
   123  	binary.BigEndian.PutUint64(enc, number)
   124  	return enc
   125  }
   126  
   127  // headerKeyPrefix = headerPrefix + num (uint64 big endian)
   128  func headerKeyPrefix(number uint64) []byte {
   129  	return append(headerPrefix, encodeBlockNumber(number)...)
   130  }
   131  
   132  // headerKey = headerPrefix + num (uint64 big endian) + hash
   133  func headerKey(number uint64, hash common.Hash) []byte {
   134  	return append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
   135  }
   136  
   137  // headerTDKey = headerPrefix + num (uint64 big endian) + hash + headerTDSuffix
   138  func headerTDKey(number uint64, hash common.Hash) []byte {
   139  	return append(headerKey(number, hash), headerTDSuffix...)
   140  }
   141  
   142  // headerHashKey = headerPrefix + num (uint64 big endian) + headerHashSuffix
   143  func headerHashKey(number uint64) []byte {
   144  	return append(append(headerPrefix, encodeBlockNumber(number)...), headerHashSuffix...)
   145  }
   146  
   147  // headerNumberKey = headerNumberPrefix + hash
   148  func headerNumberKey(hash common.Hash) []byte {
   149  	return append(headerNumberPrefix, hash.Bytes()...)
   150  }
   151  
   152  // blockBodyKey = blockBodyPrefix + num (uint64 big endian) + hash
   153  func blockBodyKey(number uint64, hash common.Hash) []byte {
   154  	return append(append(blockBodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
   155  }
   156  
   157  // blockReceiptsKey = blockReceiptsPrefix + num (uint64 big endian) + hash
   158  func blockReceiptsKey(number uint64, hash common.Hash) []byte {
   159  	return append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
   160  }
   161  
   162  // txLookupKey = txLookupPrefix + hash
   163  func txLookupKey(hash common.Hash) []byte {
   164  	return append(txLookupPrefix, hash.Bytes()...)
   165  }
   166  
   167  // accountSnapshotKey = SnapshotAccountPrefix + hash
   168  func accountSnapshotKey(hash common.Hash) []byte {
   169  	return append(SnapshotAccountPrefix, hash.Bytes()...)
   170  }
   171  
   172  // storageSnapshotKey = SnapshotStoragePrefix + account hash + storage hash
   173  func storageSnapshotKey(accountHash, storageHash common.Hash) []byte {
   174  	return append(append(SnapshotStoragePrefix, accountHash.Bytes()...), storageHash.Bytes()...)
   175  }
   176  
   177  // storageSnapshotsKey = SnapshotStoragePrefix + account hash + storage hash
   178  func storageSnapshotsKey(accountHash common.Hash) []byte {
   179  	return append(SnapshotStoragePrefix, accountHash.Bytes()...)
   180  }
   181  
   182  // bloomBitsKey = bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash
   183  func bloomBitsKey(bit uint, section uint64, hash common.Hash) []byte {
   184  	key := append(append(bloomBitsPrefix, make([]byte, 10)...), hash.Bytes()...)
   185  
   186  	binary.BigEndian.PutUint16(key[1:], uint16(bit))
   187  	binary.BigEndian.PutUint64(key[3:], section)
   188  
   189  	return key
   190  }
   191  
   192  // preimageKey = preimagePrefix + hash
   193  func preimageKey(hash common.Hash) []byte {
   194  	return append(preimagePrefix, hash.Bytes()...)
   195  }
   196  
   197  // codeKey = codePrefix + hash
   198  func codeKey(hash common.Hash) []byte {
   199  	return append(codePrefix, hash.Bytes()...)
   200  }
   201  
   202  // IsCodeKey reports whether the given byte slice is the key of contract code,
   203  // if so return the raw code hash as well.
   204  func IsCodeKey(key []byte) (bool, []byte) {
   205  	if bytes.HasPrefix(key, codePrefix) && len(key) == common.HashLength+len(codePrefix) {
   206  		return true, key[len(codePrefix):]
   207  	}
   208  	return false, nil
   209  }
   210  
   211  // configKey = configPrefix + hash
   212  func configKey(hash common.Hash) []byte {
   213  	return append(configPrefix, hash.Bytes()...)
   214  }