github.com/klaytn/klaytn@v1.10.2/storage/database/schema.go (about)

     1  // Modifications Copyright 2018 The klaytn Authors
     2  // Copyright 2018 The go-ethereum Authors
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  // This file is derived from core/rawdb/schema.go (2018/06/04).
    19  // Modified and improved for the klaytn development.
    20  
    21  package database
    22  
    23  import (
    24  	"bytes"
    25  	"encoding/binary"
    26  
    27  	"github.com/klaytn/klaytn/common"
    28  	"github.com/rcrowley/go-metrics"
    29  )
    30  
    31  // The fields below define the low level database schema prefixing.
    32  var (
    33  	// databaseVerisionKey tracks the current database version.
    34  	databaseVerisionKey = []byte("DatabaseVersion")
    35  
    36  	// headHeaderKey tracks the latest know header's hash.
    37  	headHeaderKey = []byte("LastHeader")
    38  
    39  	// headBlockKey tracks the latest know full block's hash.
    40  	headBlockKey       = []byte("LastBlock")
    41  	headBlockBackupKey = []byte("LastBlockBackup")
    42  
    43  	// headFastBlockKey tracks the latest known incomplete block's hash duirng fast sync.
    44  	headFastBlockKey       = []byte("LastFast")
    45  	headFastBlockBackupKey = []byte("LastFastBackup")
    46  
    47  	// fastTrieProgressKey tracks the number of trie entries imported during fast sync.
    48  	fastTrieProgressKey = []byte("TrieSync")
    49  
    50  	validSectionKey = []byte("count")
    51  
    52  	sectionHeadKeyPrefix = []byte("shead")
    53  
    54  	// snapshotKeyPrefix is a governance snapshot prefix
    55  	snapshotKeyPrefix = []byte("snapshot")
    56  
    57  	// snapshotJournalKey tracks the in-memory diff layers across restarts.
    58  	snapshotJournalKey = []byte("SnapshotJournal")
    59  
    60  	// SnapshotGeneratorKey tracks the snapshot generation marker across restarts.
    61  	SnapshotGeneratorKey = []byte("SnapshotGenerator")
    62  
    63  	// snapshotDisabledKey flags that the snapshot should not be maintained due to initial sync.
    64  	snapshotDisabledKey = []byte("SnapshotDisabled")
    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  	// snapshotRootKey tracks the hash of the last snapshot.
    73  	snapshotRootKey = []byte("SnapshotRoot")
    74  
    75  	// badBlockKey tracks the list of bad blocks seen by local
    76  	badBlockKey = []byte("InvalidBlock")
    77  
    78  	// Data item prefixes (use single byte to avoid mixing data types, avoid `i`, used for indexes).
    79  	headerPrefix       = []byte("h") // headerPrefix + num (uint64 big endian) + hash -> header
    80  	headerTDSuffix     = []byte("t") // headerPrefix + num (uint64 big endian) + hash + headerTDSuffix -> td
    81  	headerHashSuffix   = []byte("n") // headerPrefix + num (uint64 big endian) + headerHashSuffix -> hash
    82  	headerNumberPrefix = []byte("H") // headerNumberPrefix + hash -> num (uint64 big endian)
    83  
    84  	blockBodyPrefix     = []byte("b") // blockBodyPrefix + num (uint64 big endian) + hash -> block body
    85  	blockReceiptsPrefix = []byte("r") // blockReceiptsPrefix + num (uint64 big endian) + hash -> block receipts
    86  
    87  	txLookupPrefix        = []byte("l") // txLookupPrefix + hash -> transaction/receipt lookup metadata
    88  	SnapshotAccountPrefix = []byte("a") // SnapshotAccountPrefix + account hash -> account trie value
    89  	SnapshotStoragePrefix = []byte("o") // SnapshotStoragePrefix + account hash + storage hash -> storage trie value
    90  	codePrefix            = []byte("c") // codePrefix + code hash -> contract code
    91  
    92  	preimagePrefix = []byte("secure-key-")  // preimagePrefix + hash -> preimage
    93  	configPrefix   = []byte("klay-config-") // config prefix for the db
    94  
    95  	// Chain index prefixes (use `i` + single byte to avoid mixing data types).
    96  	BloomBitsIndexPrefix = []byte("iB") // BloomBitsIndexPrefix is the data table of a chain indexer to track its progress
    97  
    98  	preimageCounter    = metrics.NewRegisteredCounter("db/preimage/total", nil)
    99  	preimageHitCounter = metrics.NewRegisteredCounter("db/preimage/hits", nil)
   100  
   101  	childChainTxHashPrefix          = []byte("ccTxHash")
   102  	lastServiceChainTxReceiptKey    = []byte("LastServiceChainTxReceipt")
   103  	lastIndexedBlockKey             = []byte("LastIndexedBlockKey")
   104  	receiptFromParentChainKeyPrefix = []byte("receiptFromParentChain")
   105  
   106  	parentOperatorFeePayerPrefix = []byte("parentOperatorFeePayer")
   107  	childOperatorFeePayerPrefix  = []byte("childOperatorFeePayer")
   108  
   109  	valueTransferTxHashPrefix = []byte("vt-tx-hash-key-") // Prefix + hash -> hash
   110  
   111  	// bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash -> bloom bits
   112  	bloomBitsPrefix = []byte("B")
   113  
   114  	senderTxHashToTxHashPrefix = []byte("SenderTxHash")
   115  
   116  	governancePrefix     = []byte("governance")
   117  	governanceHistoryKey = []byte("governanceIdxHistory")
   118  	governanceStateKey   = []byte("governanceState")
   119  
   120  	databaseDirPrefix  = []byte("databaseDirectory")
   121  	migrationStatusKey = []byte("migrationStatus")
   122  
   123  	stakingInfoPrefix = []byte("stakingInfo")
   124  
   125  	chaindatafetcherCheckpointKey = []byte("chaindatafetcherCheckpoint")
   126  )
   127  
   128  // TxLookupEntry is a positional metadata to help looking up the data content of
   129  // a transaction or receipt given only its hash.
   130  type TxLookupEntry struct {
   131  	BlockHash  common.Hash
   132  	BlockIndex uint64
   133  	Index      uint64
   134  }
   135  
   136  // encodeBlockNumber encodes a block number as big endian uint64
   137  func encodeBlockNumber(number uint64) []byte {
   138  	enc := make([]byte, 8)
   139  	binary.BigEndian.PutUint64(enc, number)
   140  	return enc
   141  }
   142  
   143  // headerKeyPrefix = headerPrefix + num (uint64 big endian)
   144  func headerKeyPrefix(number uint64) []byte {
   145  	return append(headerPrefix, common.Int64ToByteBigEndian(number)...)
   146  }
   147  
   148  // headerKey = headerPrefix + num (uint64 big endian) + hash
   149  func headerKey(number uint64, hash common.Hash) []byte {
   150  	return append(append(headerPrefix, common.Int64ToByteBigEndian(number)...), hash.Bytes()...)
   151  }
   152  
   153  // headerTDKey = headerPrefix + num (uint64 big endian) + hash + headerTDSuffix
   154  func headerTDKey(number uint64, hash common.Hash) []byte {
   155  	return append(headerKey(number, hash), headerTDSuffix...)
   156  }
   157  
   158  // headerHashKey = headerPrefix + num (uint64 big endian) + headerHashSuffix
   159  func headerHashKey(number uint64) []byte {
   160  	return append(append(headerPrefix, common.Int64ToByteBigEndian(number)...), headerHashSuffix...)
   161  }
   162  
   163  // headerNumberKey = headerNumberPrefix + hash
   164  func headerNumberKey(hash common.Hash) []byte {
   165  	return append(headerNumberPrefix, hash.Bytes()...)
   166  }
   167  
   168  // blockBodyKey = blockBodyPrefix + num (uint64 big endian) + hash
   169  func blockBodyKey(number uint64, hash common.Hash) []byte {
   170  	return append(append(blockBodyPrefix, common.Int64ToByteBigEndian(number)...), hash.Bytes()...)
   171  }
   172  
   173  // blockReceiptsKey = blockReceiptsPrefix + num (uint64 big endian) + hash
   174  func blockReceiptsKey(number uint64, hash common.Hash) []byte {
   175  	return append(append(blockReceiptsPrefix, common.Int64ToByteBigEndian(number)...), hash.Bytes()...)
   176  }
   177  
   178  // TxLookupKey = txLookupPrefix + hash
   179  func TxLookupKey(hash common.Hash) []byte {
   180  	return append(txLookupPrefix, hash.Bytes()...)
   181  }
   182  
   183  // AccountSnapshotKey = SnapshotAccountPrefix + hash
   184  func AccountSnapshotKey(hash common.Hash) []byte {
   185  	return append(SnapshotAccountPrefix, hash.Bytes()...)
   186  }
   187  
   188  // StorageSnapshotKey = SnapshotStoragePrefix + account hash + storage hash
   189  func StorageSnapshotKey(accountHash, storageHash common.Hash) []byte {
   190  	return append(append(SnapshotStoragePrefix, accountHash.Bytes()...), storageHash.Bytes()...)
   191  }
   192  
   193  // StorageSnapshotsKey = SnapshotStoragePrefix + account hash + storage hash
   194  func StorageSnapshotsKey(accountHash common.Hash) []byte {
   195  	return append(SnapshotStoragePrefix, accountHash.Bytes()...)
   196  }
   197  
   198  func SenderTxHashToTxHashKey(senderTxHash common.Hash) []byte {
   199  	return append(senderTxHashToTxHashPrefix, senderTxHash.Bytes()...)
   200  }
   201  
   202  // preimageKey = preimagePrefix + hash
   203  func preimageKey(hash common.Hash) []byte {
   204  	return append(preimagePrefix, hash.Bytes()...)
   205  }
   206  
   207  // CodeKey = codePrefix + hash
   208  func CodeKey(hash common.Hash) []byte {
   209  	return append(codePrefix, hash.Bytes()...)
   210  }
   211  
   212  // IsCodeKey reports whether the given byte slice is the key of contract code,
   213  // if so return the raw code hash as well.
   214  func IsCodeKey(key []byte) (bool, []byte) {
   215  	if bytes.HasPrefix(key, codePrefix) && len(key) == common.HashLength+len(codePrefix) {
   216  		return true, key[len(codePrefix):]
   217  	}
   218  	return false, nil
   219  }
   220  
   221  // configKey = configPrefix + hash
   222  func configKey(hash common.Hash) []byte {
   223  	return append(configPrefix, hash.Bytes()...)
   224  }
   225  
   226  func sectionHeadKey(encodedSection []byte) []byte {
   227  	return append(sectionHeadKeyPrefix, encodedSection...)
   228  }
   229  
   230  func snapshotKey(hash common.Hash) []byte {
   231  	return append(snapshotKeyPrefix, hash[:]...)
   232  }
   233  
   234  func childChainTxHashKey(ccBlockHash common.Hash) []byte {
   235  	return append(append(childChainTxHashPrefix, ccBlockHash.Bytes()...))
   236  }
   237  
   238  func receiptFromParentChainKey(blockHash common.Hash) []byte {
   239  	return append(receiptFromParentChainKeyPrefix, blockHash.Bytes()...)
   240  }
   241  
   242  func valueTransferTxHashKey(rTxHash common.Hash) []byte {
   243  	return append(valueTransferTxHashPrefix, rTxHash.Bytes()...)
   244  }
   245  
   246  // bloomBitsKey = bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash
   247  func BloomBitsKey(bit uint, section uint64, hash common.Hash) []byte {
   248  	key := append(append(bloomBitsPrefix, make([]byte, 10)...), hash.Bytes()...)
   249  
   250  	binary.BigEndian.PutUint16(key[1:], uint16(bit))
   251  	binary.BigEndian.PutUint64(key[3:], section)
   252  
   253  	return key
   254  }
   255  
   256  func makeKey(prefix []byte, num uint64) []byte {
   257  	byteKey := common.Int64ToByteLittleEndian(num)
   258  	return append(prefix, byteKey...)
   259  }
   260  
   261  func databaseDirKey(dbEntryType uint64) []byte {
   262  	return append(databaseDirPrefix, common.Int64ToByteBigEndian(dbEntryType)...)
   263  }