github.com/fff-chain/go-fff@v0.0.0-20220726032732-1c84420b8a99/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/fff-chain/go-fff/common"
    25  	"github.com/fff-chain/go-fff/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  	// snapshotDisabledKey flags that the snapshot should not be maintained due to initial sync.
    49  	snapshotDisabledKey = []byte("SnapshotDisabled")
    50  
    51  	// snapshotRootKey tracks the hash of the last snapshot.
    52  	snapshotRootKey = []byte("SnapshotRoot")
    53  
    54  	// snapshotJournalKey tracks the in-memory diff layers across restarts.
    55  	snapshotJournalKey = []byte("SnapshotJournal")
    56  
    57  	// snapshotGeneratorKey tracks the snapshot generation marker across restarts.
    58  	snapshotGeneratorKey = []byte("SnapshotGenerator")
    59  
    60  	// snapshotRecoveryKey tracks the snapshot recovery marker across restarts.
    61  	snapshotRecoveryKey = []byte("SnapshotRecovery")
    62  
    63  	// snapshotSyncStatusKey tracks the snapshot sync status across restarts.
    64  	snapshotSyncStatusKey = []byte("SnapshotSyncStatus")
    65  
    66  	// txIndexTailKey tracks the oldest block whose transactions have been indexed.
    67  	txIndexTailKey = []byte("TransactionIndexTail")
    68  
    69  	// fastTxLookupLimitKey tracks the transaction lookup limit during fast sync.
    70  	fastTxLookupLimitKey = []byte("FastTransactionLookupLimit")
    71  
    72  	//offSet of new updated ancientDB.
    73  	offSetOfCurrentAncientFreezer = []byte("offSetOfCurrentAncientFreezer")
    74  
    75  	//offSet of the ancientDB before updated version.
    76  	offSetOfLastAncientFreezer = []byte("offSetOfLastAncientFreezer")
    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  	blockBodyPrefix     = []byte("b") // blockBodyPrefix + num (uint64 big endian) + hash -> block body
    91  	blockReceiptsPrefix = []byte("r") // blockReceiptsPrefix + num (uint64 big endian) + hash -> block receipts
    92  
    93  	txLookupPrefix        = []byte("l") // txLookupPrefix + hash -> transaction/receipt lookup metadata
    94  	bloomBitsPrefix       = []byte("B") // bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash -> bloom bits
    95  	SnapshotAccountPrefix = []byte("a") // SnapshotAccountPrefix + account hash -> account trie value
    96  	SnapshotStoragePrefix = []byte("o") // SnapshotStoragePrefix + account hash + storage hash -> storage trie value
    97  	CodePrefix            = []byte("c") // CodePrefix + code hash -> account code
    98  
    99  	// difflayer database
   100  	diffLayerPrefix = []byte("d") // diffLayerPrefix + hash  -> diffLayer
   101  
   102  	preimagePrefix = []byte("secure-key-")      // preimagePrefix + hash -> preimage
   103  	configPrefix   = []byte("ethereum-config-") // config prefix for the db
   104  
   105  	// Chain index prefixes (use `i` + single byte to avoid mixing data types).
   106  	BloomBitsIndexPrefix = []byte("iB") // BloomBitsIndexPrefix is the data table of a chain indexer to track its progress
   107  
   108  	preimageCounter    = metrics.NewRegisteredCounter("db/preimage/total", nil)
   109  	preimageHitCounter = metrics.NewRegisteredCounter("db/preimage/hits", nil)
   110  )
   111  
   112  const (
   113  	// freezerHeaderTable indicates the name of the freezer header table.
   114  	freezerHeaderTable = "headers"
   115  
   116  	// freezerHashTable indicates the name of the freezer canonical hash table.
   117  	freezerHashTable = "hashes"
   118  
   119  	// freezerBodiesTable indicates the name of the freezer block body table.
   120  	freezerBodiesTable = "bodies"
   121  
   122  	// freezerReceiptTable indicates the name of the freezer receipts table.
   123  	freezerReceiptTable = "receipts"
   124  
   125  	// freezerDifficultyTable indicates the name of the freezer total difficulty table.
   126  	freezerDifficultyTable = "diffs"
   127  )
   128  
   129  // FreezerNoSnappy configures whether compression is disabled for the ancient-tables.
   130  // Hashes and difficulties don't compress well.
   131  var FreezerNoSnappy = map[string]bool{
   132  	freezerHeaderTable:     false,
   133  	freezerHashTable:       true,
   134  	freezerBodiesTable:     false,
   135  	freezerReceiptTable:    false,
   136  	freezerDifficultyTable: true,
   137  }
   138  
   139  // LegacyTxLookupEntry is the legacy TxLookupEntry definition with some unnecessary
   140  // fields.
   141  type LegacyTxLookupEntry struct {
   142  	BlockHash  common.Hash
   143  	BlockIndex uint64
   144  	Index      uint64
   145  }
   146  
   147  // encodeBlockNumber encodes a block number as big endian uint64
   148  func encodeBlockNumber(number uint64) []byte {
   149  	enc := make([]byte, 8)
   150  	binary.BigEndian.PutUint64(enc, number)
   151  	return enc
   152  }
   153  
   154  // headerKeyPrefix = headerPrefix + num (uint64 big endian)
   155  func headerKeyPrefix(number uint64) []byte {
   156  	return append(headerPrefix, encodeBlockNumber(number)...)
   157  }
   158  
   159  // headerKey = headerPrefix + num (uint64 big endian) + hash
   160  func headerKey(number uint64, hash common.Hash) []byte {
   161  	return append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
   162  }
   163  
   164  // headerTDKey = headerPrefix + num (uint64 big endian) + hash + headerTDSuffix
   165  func headerTDKey(number uint64, hash common.Hash) []byte {
   166  	return append(headerKey(number, hash), headerTDSuffix...)
   167  }
   168  
   169  // headerHashKey = headerPrefix + num (uint64 big endian) + headerHashSuffix
   170  func headerHashKey(number uint64) []byte {
   171  	return append(append(headerPrefix, encodeBlockNumber(number)...), headerHashSuffix...)
   172  }
   173  
   174  // headerNumberKey = headerNumberPrefix + hash
   175  func headerNumberKey(hash common.Hash) []byte {
   176  	return append(headerNumberPrefix, hash.Bytes()...)
   177  }
   178  
   179  // blockBodyKey = blockBodyPrefix + num (uint64 big endian) + hash
   180  func blockBodyKey(number uint64, hash common.Hash) []byte {
   181  	return append(append(blockBodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
   182  }
   183  
   184  // blockReceiptsKey = blockReceiptsPrefix + num (uint64 big endian) + hash
   185  func blockReceiptsKey(number uint64, hash common.Hash) []byte {
   186  	return append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
   187  }
   188  
   189  // diffLayerKey = diffLayerKeyPrefix + hash
   190  func diffLayerKey(hash common.Hash) []byte {
   191  	return append(append(diffLayerPrefix, hash.Bytes()...))
   192  }
   193  
   194  // txLookupKey = txLookupPrefix + hash
   195  func txLookupKey(hash common.Hash) []byte {
   196  	return append(txLookupPrefix, hash.Bytes()...)
   197  }
   198  
   199  // accountSnapshotKey = SnapshotAccountPrefix + hash
   200  func accountSnapshotKey(hash common.Hash) []byte {
   201  	return append(SnapshotAccountPrefix, hash.Bytes()...)
   202  }
   203  
   204  // storageSnapshotKey = SnapshotStoragePrefix + account hash + storage hash
   205  func storageSnapshotKey(accountHash, storageHash common.Hash) []byte {
   206  	return append(append(SnapshotStoragePrefix, accountHash.Bytes()...), storageHash.Bytes()...)
   207  }
   208  
   209  // storageSnapshotsKey = SnapshotStoragePrefix + account hash + storage hash
   210  func storageSnapshotsKey(accountHash common.Hash) []byte {
   211  	return append(SnapshotStoragePrefix, accountHash.Bytes()...)
   212  }
   213  
   214  // bloomBitsKey = bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash
   215  func bloomBitsKey(bit uint, section uint64, hash common.Hash) []byte {
   216  	key := append(append(bloomBitsPrefix, make([]byte, 10)...), hash.Bytes()...)
   217  
   218  	binary.BigEndian.PutUint16(key[1:], uint16(bit))
   219  	binary.BigEndian.PutUint64(key[3:], section)
   220  
   221  	return key
   222  }
   223  
   224  // preimageKey = preimagePrefix + hash
   225  func preimageKey(hash common.Hash) []byte {
   226  	return append(preimagePrefix, hash.Bytes()...)
   227  }
   228  
   229  // codeKey = CodePrefix + hash
   230  func codeKey(hash common.Hash) []byte {
   231  	return append(CodePrefix, hash.Bytes()...)
   232  }
   233  
   234  // IsCodeKey reports whether the given byte slice is the key of contract code,
   235  // if so return the raw code hash as well.
   236  func IsCodeKey(key []byte) (bool, []byte) {
   237  	if bytes.HasPrefix(key, CodePrefix) && len(key) == common.HashLength+len(CodePrefix) {
   238  		return true, key[len(CodePrefix):]
   239  	}
   240  	return false, nil
   241  }
   242  
   243  // configKey = configPrefix + hash
   244  func configKey(hash common.Hash) []byte {
   245  	return append(configPrefix, hash.Bytes()...)
   246  }