github.com/theQRL/go-zond@v0.1.1/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/theQRL/go-zond/common"
    25  	"github.com/theQRL/go-zond/crypto"
    26  	"github.com/theQRL/go-zond/metrics"
    27  )
    28  
    29  // The fields below define the low level database schema prefixing.
    30  var (
    31  	// databaseVersionKey tracks the current database version.
    32  	databaseVersionKey = []byte("DatabaseVersion")
    33  
    34  	// headHeaderKey tracks the latest known header's hash.
    35  	headHeaderKey = []byte("LastHeader")
    36  
    37  	// headBlockKey tracks the latest known full block's hash.
    38  	headBlockKey = []byte("LastBlock")
    39  
    40  	// headFastBlockKey tracks the latest known incomplete block's hash during fast sync.
    41  	headFastBlockKey = []byte("LastFast")
    42  
    43  	// headFinalizedBlockKey tracks the latest known finalized block hash.
    44  	headFinalizedBlockKey = []byte("LastFinalized")
    45  
    46  	// persistentStateIDKey tracks the id of latest stored state(for path-based only).
    47  	persistentStateIDKey = []byte("LastStateID")
    48  
    49  	// lastPivotKey tracks the last pivot block used by fast sync (to reenable on sethead).
    50  	lastPivotKey = []byte("LastPivot")
    51  
    52  	// fastTrieProgressKey tracks the number of trie entries imported during fast sync.
    53  	fastTrieProgressKey = []byte("TrieSync")
    54  
    55  	// snapshotDisabledKey flags that the snapshot should not be maintained due to initial sync.
    56  	snapshotDisabledKey = []byte("SnapshotDisabled")
    57  
    58  	// SnapshotRootKey tracks the hash of the last snapshot.
    59  	SnapshotRootKey = []byte("SnapshotRoot")
    60  
    61  	// snapshotJournalKey tracks the in-memory diff layers across restarts.
    62  	snapshotJournalKey = []byte("SnapshotJournal")
    63  
    64  	// snapshotGeneratorKey tracks the snapshot generation marker across restarts.
    65  	snapshotGeneratorKey = []byte("SnapshotGenerator")
    66  
    67  	// snapshotRecoveryKey tracks the snapshot recovery marker across restarts.
    68  	snapshotRecoveryKey = []byte("SnapshotRecovery")
    69  
    70  	// snapshotSyncStatusKey tracks the snapshot sync status across restarts.
    71  	snapshotSyncStatusKey = []byte("SnapshotSyncStatus")
    72  
    73  	// skeletonSyncStatusKey tracks the skeleton sync status across restarts.
    74  	skeletonSyncStatusKey = []byte("SkeletonSyncStatus")
    75  
    76  	// trieJournalKey tracks the in-memory trie node layers across restarts.
    77  	trieJournalKey = []byte("TrieJournal")
    78  
    79  	// txIndexTailKey tracks the oldest block whose transactions have been indexed.
    80  	txIndexTailKey = []byte("TransactionIndexTail")
    81  
    82  	// fastTxLookupLimitKey tracks the transaction lookup limit during fast sync.
    83  	fastTxLookupLimitKey = []byte("FastTransactionLookupLimit")
    84  
    85  	// badBlockKey tracks the list of bad blocks seen by local
    86  	badBlockKey = []byte("InvalidBlock")
    87  
    88  	// uncleanShutdownKey tracks the list of local crashes
    89  	uncleanShutdownKey = []byte("unclean-shutdown") // config prefix for the db
    90  
    91  	// transitionStatusKey tracks the eth2 transition status.
    92  	transitionStatusKey = []byte("eth2-transition")
    93  
    94  	// Data item prefixes (use single byte to avoid mixing data types, avoid `i`, used for indexes).
    95  	headerPrefix       = []byte("h") // headerPrefix + num (uint64 big endian) + hash -> header
    96  	headerTDSuffix     = []byte("t") // headerPrefix + num (uint64 big endian) + hash + headerTDSuffix -> td
    97  	headerHashSuffix   = []byte("n") // headerPrefix + num (uint64 big endian) + headerHashSuffix -> hash
    98  	headerNumberPrefix = []byte("H") // headerNumberPrefix + hash -> num (uint64 big endian)
    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  
   103  	txLookupPrefix        = []byte("l") // txLookupPrefix + hash -> transaction/receipt lookup metadata
   104  	bloomBitsPrefix       = []byte("B") // bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash -> bloom bits
   105  	SnapshotAccountPrefix = []byte("a") // SnapshotAccountPrefix + account hash -> account trie value
   106  	SnapshotStoragePrefix = []byte("o") // SnapshotStoragePrefix + account hash + storage hash -> storage trie value
   107  	CodePrefix            = []byte("c") // CodePrefix + code hash -> account code
   108  	skeletonHeaderPrefix  = []byte("S") // skeletonHeaderPrefix + num (uint64 big endian) -> header
   109  
   110  	// Path-based storage scheme of merkle patricia trie.
   111  	trieNodeAccountPrefix = []byte("A") // trieNodeAccountPrefix + hexPath -> trie node
   112  	trieNodeStoragePrefix = []byte("O") // trieNodeStoragePrefix + accountHash + hexPath -> trie node
   113  	stateIDPrefix         = []byte("L") // stateIDPrefix + state root -> state id
   114  
   115  	PreimagePrefix = []byte("secure-key-")       // PreimagePrefix + hash -> preimage
   116  	configPrefix   = []byte("ethereum-config-")  // config prefix for the db
   117  	genesisPrefix  = []byte("ethereum-genesis-") // genesis state prefix for the db
   118  
   119  	// BloomBitsIndexPrefix is the data table of a chain indexer to track its progress
   120  	BloomBitsIndexPrefix = []byte("iB")
   121  
   122  	ChtPrefix           = []byte("chtRootV2-") // ChtPrefix + chtNum (uint64 big endian) -> trie root hash
   123  	ChtTablePrefix      = []byte("cht-")
   124  	ChtIndexTablePrefix = []byte("chtIndexV2-")
   125  
   126  	BloomTriePrefix      = []byte("bltRoot-") // BloomTriePrefix + bloomTrieNum (uint64 big endian) -> trie root hash
   127  	BloomTrieTablePrefix = []byte("blt-")
   128  	BloomTrieIndexPrefix = []byte("bltIndex-")
   129  
   130  	CliqueSnapshotPrefix = []byte("clique-")
   131  
   132  	preimageCounter    = metrics.NewRegisteredCounter("db/preimage/total", nil)
   133  	preimageHitCounter = metrics.NewRegisteredCounter("db/preimage/hits", nil)
   134  )
   135  
   136  // LegacyTxLookupEntry is the legacy TxLookupEntry definition with some unnecessary
   137  // fields.
   138  type LegacyTxLookupEntry struct {
   139  	BlockHash  common.Hash
   140  	BlockIndex uint64
   141  	Index      uint64
   142  }
   143  
   144  // encodeBlockNumber encodes a block number as big endian uint64
   145  func encodeBlockNumber(number uint64) []byte {
   146  	enc := make([]byte, 8)
   147  	binary.BigEndian.PutUint64(enc, number)
   148  	return enc
   149  }
   150  
   151  // headerKeyPrefix = headerPrefix + num (uint64 big endian)
   152  func headerKeyPrefix(number uint64) []byte {
   153  	return append(headerPrefix, encodeBlockNumber(number)...)
   154  }
   155  
   156  // headerKey = headerPrefix + num (uint64 big endian) + hash
   157  func headerKey(number uint64, hash common.Hash) []byte {
   158  	return append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
   159  }
   160  
   161  // headerTDKey = headerPrefix + num (uint64 big endian) + hash + headerTDSuffix
   162  func headerTDKey(number uint64, hash common.Hash) []byte {
   163  	return append(headerKey(number, hash), headerTDSuffix...)
   164  }
   165  
   166  // headerHashKey = headerPrefix + num (uint64 big endian) + headerHashSuffix
   167  func headerHashKey(number uint64) []byte {
   168  	return append(append(headerPrefix, encodeBlockNumber(number)...), headerHashSuffix...)
   169  }
   170  
   171  // headerNumberKey = headerNumberPrefix + hash
   172  func headerNumberKey(hash common.Hash) []byte {
   173  	return append(headerNumberPrefix, hash.Bytes()...)
   174  }
   175  
   176  // blockBodyKey = blockBodyPrefix + num (uint64 big endian) + hash
   177  func blockBodyKey(number uint64, hash common.Hash) []byte {
   178  	return append(append(blockBodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
   179  }
   180  
   181  // blockReceiptsKey = blockReceiptsPrefix + num (uint64 big endian) + hash
   182  func blockReceiptsKey(number uint64, hash common.Hash) []byte {
   183  	return append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
   184  }
   185  
   186  // txLookupKey = txLookupPrefix + hash
   187  func txLookupKey(hash common.Hash) []byte {
   188  	return append(txLookupPrefix, hash.Bytes()...)
   189  }
   190  
   191  // accountSnapshotKey = SnapshotAccountPrefix + hash
   192  func accountSnapshotKey(hash common.Hash) []byte {
   193  	return append(SnapshotAccountPrefix, hash.Bytes()...)
   194  }
   195  
   196  // storageSnapshotKey = SnapshotStoragePrefix + account hash + storage hash
   197  func storageSnapshotKey(accountHash, storageHash common.Hash) []byte {
   198  	buf := make([]byte, len(SnapshotStoragePrefix)+common.HashLength+common.HashLength)
   199  	n := copy(buf, SnapshotStoragePrefix)
   200  	n += copy(buf[n:], accountHash.Bytes())
   201  	copy(buf[n:], storageHash.Bytes())
   202  	return buf
   203  }
   204  
   205  // storageSnapshotsKey = SnapshotStoragePrefix + account hash + storage hash
   206  func storageSnapshotsKey(accountHash common.Hash) []byte {
   207  	return append(SnapshotStoragePrefix, accountHash.Bytes()...)
   208  }
   209  
   210  // bloomBitsKey = bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash
   211  func bloomBitsKey(bit uint, section uint64, hash common.Hash) []byte {
   212  	key := append(append(bloomBitsPrefix, make([]byte, 10)...), hash.Bytes()...)
   213  
   214  	binary.BigEndian.PutUint16(key[1:], uint16(bit))
   215  	binary.BigEndian.PutUint64(key[3:], section)
   216  
   217  	return key
   218  }
   219  
   220  // skeletonHeaderKey = skeletonHeaderPrefix + num (uint64 big endian)
   221  func skeletonHeaderKey(number uint64) []byte {
   222  	return append(skeletonHeaderPrefix, encodeBlockNumber(number)...)
   223  }
   224  
   225  // preimageKey = PreimagePrefix + hash
   226  func preimageKey(hash common.Hash) []byte {
   227  	return append(PreimagePrefix, hash.Bytes()...)
   228  }
   229  
   230  // codeKey = CodePrefix + hash
   231  func codeKey(hash common.Hash) []byte {
   232  	return append(CodePrefix, hash.Bytes()...)
   233  }
   234  
   235  // IsCodeKey reports whether the given byte slice is the key of contract code,
   236  // if so return the raw code hash as well.
   237  func IsCodeKey(key []byte) (bool, []byte) {
   238  	if bytes.HasPrefix(key, CodePrefix) && len(key) == common.HashLength+len(CodePrefix) {
   239  		return true, key[len(CodePrefix):]
   240  	}
   241  	return false, nil
   242  }
   243  
   244  // configKey = configPrefix + hash
   245  func configKey(hash common.Hash) []byte {
   246  	return append(configPrefix, hash.Bytes()...)
   247  }
   248  
   249  // genesisStateSpecKey = genesisPrefix + hash
   250  func genesisStateSpecKey(hash common.Hash) []byte {
   251  	return append(genesisPrefix, hash.Bytes()...)
   252  }
   253  
   254  // stateIDKey = stateIDPrefix + root (32 bytes)
   255  func stateIDKey(root common.Hash) []byte {
   256  	return append(stateIDPrefix, root.Bytes()...)
   257  }
   258  
   259  // accountTrieNodeKey = trieNodeAccountPrefix + nodePath.
   260  func accountTrieNodeKey(path []byte) []byte {
   261  	return append(trieNodeAccountPrefix, path...)
   262  }
   263  
   264  // storageTrieNodeKey = trieNodeStoragePrefix + accountHash + nodePath.
   265  func storageTrieNodeKey(accountHash common.Hash, path []byte) []byte {
   266  	buf := make([]byte, len(trieNodeStoragePrefix)+common.HashLength+len(path))
   267  	n := copy(buf, trieNodeStoragePrefix)
   268  	n += copy(buf[n:], accountHash.Bytes())
   269  	copy(buf[n:], path)
   270  	return buf
   271  }
   272  
   273  // IsLegacyTrieNode reports whether a provided database entry is a legacy trie
   274  // node. The characteristics of legacy trie node are:
   275  // - the key length is 32 bytes
   276  // - the key is the hash of val
   277  func IsLegacyTrieNode(key []byte, val []byte) bool {
   278  	if len(key) != common.HashLength {
   279  		return false
   280  	}
   281  	return bytes.Equal(key, crypto.Keccak256(val))
   282  }
   283  
   284  // ResolveAccountTrieNodeKey reports whether a provided database entry is an
   285  // account trie node in path-based state scheme, and returns the resolved
   286  // node path if so.
   287  func ResolveAccountTrieNodeKey(key []byte) (bool, []byte) {
   288  	if !bytes.HasPrefix(key, trieNodeAccountPrefix) {
   289  		return false, nil
   290  	}
   291  	// The remaining key should only consist a hex node path
   292  	// whose length is in the range 0 to 64 (64 is excluded
   293  	// since leaves are always wrapped with shortNode).
   294  	if len(key) >= len(trieNodeAccountPrefix)+common.HashLength*2 {
   295  		return false, nil
   296  	}
   297  	return true, key[len(trieNodeAccountPrefix):]
   298  }
   299  
   300  // IsAccountTrieNode reports whether a provided database entry is an account
   301  // trie node in path-based state scheme.
   302  func IsAccountTrieNode(key []byte) bool {
   303  	ok, _ := ResolveAccountTrieNodeKey(key)
   304  	return ok
   305  }
   306  
   307  // ResolveStorageTrieNode reports whether a provided database entry is a storage
   308  // trie node in path-based state scheme, and returns the resolved account hash
   309  // and node path if so.
   310  func ResolveStorageTrieNode(key []byte) (bool, common.Hash, []byte) {
   311  	if !bytes.HasPrefix(key, trieNodeStoragePrefix) {
   312  		return false, common.Hash{}, nil
   313  	}
   314  	// The remaining key consists of 2 parts:
   315  	// - 32 bytes account hash
   316  	// - hex node path whose length is in the range 0 to 64
   317  	if len(key) < len(trieNodeStoragePrefix)+common.HashLength {
   318  		return false, common.Hash{}, nil
   319  	}
   320  	if len(key) >= len(trieNodeStoragePrefix)+common.HashLength+common.HashLength*2 {
   321  		return false, common.Hash{}, nil
   322  	}
   323  	accountHash := common.BytesToHash(key[len(trieNodeStoragePrefix) : len(trieNodeStoragePrefix)+common.HashLength])
   324  	return true, accountHash, key[len(trieNodeStoragePrefix)+common.HashLength:]
   325  }
   326  
   327  // IsStorageTrieNode reports whether a provided database entry is a storage
   328  // trie node in path-based state scheme.
   329  func IsStorageTrieNode(key []byte) bool {
   330  	ok, _, _ := ResolveStorageTrieNode(key)
   331  	return ok
   332  }