github.com/theQRL/go-zond@v0.2.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  	headerHashSuffix   = []byte("n") // headerPrefix + num (uint64 big endian) + headerHashSuffix -> hash
    97  	headerNumberPrefix = []byte("H") // headerNumberPrefix + hash -> num (uint64 big endian)
    98  
    99  	blockBodyPrefix     = []byte("b") // blockBodyPrefix + num (uint64 big endian) + hash -> block body
   100  	blockReceiptsPrefix = []byte("r") // blockReceiptsPrefix + num (uint64 big endian) + hash -> block receipts
   101  
   102  	txLookupPrefix        = []byte("l") // txLookupPrefix + hash -> transaction/receipt lookup metadata
   103  	bloomBitsPrefix       = []byte("B") // bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash -> bloom bits
   104  	SnapshotAccountPrefix = []byte("a") // SnapshotAccountPrefix + account hash -> account trie value
   105  	SnapshotStoragePrefix = []byte("o") // SnapshotStoragePrefix + account hash + storage hash -> storage trie value
   106  	CodePrefix            = []byte("c") // CodePrefix + code hash -> account code
   107  	skeletonHeaderPrefix  = []byte("S") // skeletonHeaderPrefix + num (uint64 big endian) -> header
   108  
   109  	// Path-based storage scheme of merkle patricia trie.
   110  	trieNodeAccountPrefix = []byte("A") // trieNodeAccountPrefix + hexPath -> trie node
   111  	trieNodeStoragePrefix = []byte("O") // trieNodeStoragePrefix + accountHash + hexPath -> trie node
   112  	stateIDPrefix         = []byte("L") // stateIDPrefix + state root -> state id
   113  
   114  	PreimagePrefix = []byte("secure-key-")       // PreimagePrefix + hash -> preimage
   115  	configPrefix   = []byte("ethereum-config-")  // config prefix for the db
   116  	genesisPrefix  = []byte("ethereum-genesis-") // genesis state prefix for the db
   117  
   118  	// BloomBitsIndexPrefix is the data table of a chain indexer to track its progress
   119  	BloomBitsIndexPrefix = []byte("iB")
   120  
   121  	ChtPrefix           = []byte("chtRootV2-") // ChtPrefix + chtNum (uint64 big endian) -> trie root hash
   122  	ChtTablePrefix      = []byte("cht-")
   123  	ChtIndexTablePrefix = []byte("chtIndexV2-")
   124  
   125  	BloomTriePrefix      = []byte("bltRoot-") // BloomTriePrefix + bloomTrieNum (uint64 big endian) -> trie root hash
   126  	BloomTrieTablePrefix = []byte("blt-")
   127  	BloomTrieIndexPrefix = []byte("bltIndex-")
   128  
   129  	preimageCounter    = metrics.NewRegisteredCounter("db/preimage/total", nil)
   130  	preimageHitCounter = metrics.NewRegisteredCounter("db/preimage/hits", nil)
   131  )
   132  
   133  // encodeBlockNumber encodes a block number as big endian uint64
   134  func encodeBlockNumber(number uint64) []byte {
   135  	enc := make([]byte, 8)
   136  	binary.BigEndian.PutUint64(enc, number)
   137  	return enc
   138  }
   139  
   140  // headerKeyPrefix = headerPrefix + num (uint64 big endian)
   141  func headerKeyPrefix(number uint64) []byte {
   142  	return append(headerPrefix, encodeBlockNumber(number)...)
   143  }
   144  
   145  // headerKey = headerPrefix + num (uint64 big endian) + hash
   146  func headerKey(number uint64, hash common.Hash) []byte {
   147  	return append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
   148  }
   149  
   150  // headerHashKey = headerPrefix + num (uint64 big endian) + headerHashSuffix
   151  func headerHashKey(number uint64) []byte {
   152  	return append(append(headerPrefix, encodeBlockNumber(number)...), headerHashSuffix...)
   153  }
   154  
   155  // headerNumberKey = headerNumberPrefix + hash
   156  func headerNumberKey(hash common.Hash) []byte {
   157  	return append(headerNumberPrefix, hash.Bytes()...)
   158  }
   159  
   160  // blockBodyKey = blockBodyPrefix + num (uint64 big endian) + hash
   161  func blockBodyKey(number uint64, hash common.Hash) []byte {
   162  	return append(append(blockBodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
   163  }
   164  
   165  // blockReceiptsKey = blockReceiptsPrefix + num (uint64 big endian) + hash
   166  func blockReceiptsKey(number uint64, hash common.Hash) []byte {
   167  	return append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
   168  }
   169  
   170  // txLookupKey = txLookupPrefix + hash
   171  func txLookupKey(hash common.Hash) []byte {
   172  	return append(txLookupPrefix, hash.Bytes()...)
   173  }
   174  
   175  // accountSnapshotKey = SnapshotAccountPrefix + hash
   176  func accountSnapshotKey(hash common.Hash) []byte {
   177  	return append(SnapshotAccountPrefix, hash.Bytes()...)
   178  }
   179  
   180  // storageSnapshotKey = SnapshotStoragePrefix + account hash + storage hash
   181  func storageSnapshotKey(accountHash, storageHash common.Hash) []byte {
   182  	buf := make([]byte, len(SnapshotStoragePrefix)+common.HashLength+common.HashLength)
   183  	n := copy(buf, SnapshotStoragePrefix)
   184  	n += copy(buf[n:], accountHash.Bytes())
   185  	copy(buf[n:], storageHash.Bytes())
   186  	return buf
   187  }
   188  
   189  // storageSnapshotsKey = SnapshotStoragePrefix + account hash + storage hash
   190  func storageSnapshotsKey(accountHash common.Hash) []byte {
   191  	return append(SnapshotStoragePrefix, accountHash.Bytes()...)
   192  }
   193  
   194  // bloomBitsKey = bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash
   195  func bloomBitsKey(bit uint, section uint64, hash common.Hash) []byte {
   196  	key := append(append(bloomBitsPrefix, make([]byte, 10)...), hash.Bytes()...)
   197  
   198  	binary.BigEndian.PutUint16(key[1:], uint16(bit))
   199  	binary.BigEndian.PutUint64(key[3:], section)
   200  
   201  	return key
   202  }
   203  
   204  // skeletonHeaderKey = skeletonHeaderPrefix + num (uint64 big endian)
   205  func skeletonHeaderKey(number uint64) []byte {
   206  	return append(skeletonHeaderPrefix, encodeBlockNumber(number)...)
   207  }
   208  
   209  // preimageKey = PreimagePrefix + hash
   210  func preimageKey(hash common.Hash) []byte {
   211  	return append(PreimagePrefix, hash.Bytes()...)
   212  }
   213  
   214  // codeKey = CodePrefix + hash
   215  func codeKey(hash common.Hash) []byte {
   216  	return append(CodePrefix, hash.Bytes()...)
   217  }
   218  
   219  // IsCodeKey reports whether the given byte slice is the key of contract code,
   220  // if so return the raw code hash as well.
   221  func IsCodeKey(key []byte) (bool, []byte) {
   222  	if bytes.HasPrefix(key, CodePrefix) && len(key) == common.HashLength+len(CodePrefix) {
   223  		return true, key[len(CodePrefix):]
   224  	}
   225  	return false, nil
   226  }
   227  
   228  // configKey = configPrefix + hash
   229  func configKey(hash common.Hash) []byte {
   230  	return append(configPrefix, hash.Bytes()...)
   231  }
   232  
   233  // genesisStateSpecKey = genesisPrefix + hash
   234  func genesisStateSpecKey(hash common.Hash) []byte {
   235  	return append(genesisPrefix, hash.Bytes()...)
   236  }
   237  
   238  // stateIDKey = stateIDPrefix + root (32 bytes)
   239  func stateIDKey(root common.Hash) []byte {
   240  	return append(stateIDPrefix, root.Bytes()...)
   241  }
   242  
   243  // accountTrieNodeKey = trieNodeAccountPrefix + nodePath.
   244  func accountTrieNodeKey(path []byte) []byte {
   245  	return append(trieNodeAccountPrefix, path...)
   246  }
   247  
   248  // storageTrieNodeKey = trieNodeStoragePrefix + accountHash + nodePath.
   249  func storageTrieNodeKey(accountHash common.Hash, path []byte) []byte {
   250  	buf := make([]byte, len(trieNodeStoragePrefix)+common.HashLength+len(path))
   251  	n := copy(buf, trieNodeStoragePrefix)
   252  	n += copy(buf[n:], accountHash.Bytes())
   253  	copy(buf[n:], path)
   254  	return buf
   255  }
   256  
   257  // IsLegacyTrieNode reports whether a provided database entry is a legacy trie
   258  // node. The characteristics of legacy trie node are:
   259  // - the key length is 32 bytes
   260  // - the key is the hash of val
   261  func IsLegacyTrieNode(key []byte, val []byte) bool {
   262  	if len(key) != common.HashLength {
   263  		return false
   264  	}
   265  	return bytes.Equal(key, crypto.Keccak256(val))
   266  }
   267  
   268  // ResolveAccountTrieNodeKey reports whether a provided database entry is an
   269  // account trie node in path-based state scheme, and returns the resolved
   270  // node path if so.
   271  func ResolveAccountTrieNodeKey(key []byte) (bool, []byte) {
   272  	if !bytes.HasPrefix(key, trieNodeAccountPrefix) {
   273  		return false, nil
   274  	}
   275  	// The remaining key should only consist a hex node path
   276  	// whose length is in the range 0 to 64 (64 is excluded
   277  	// since leaves are always wrapped with shortNode).
   278  	if len(key) >= len(trieNodeAccountPrefix)+common.HashLength*2 {
   279  		return false, nil
   280  	}
   281  	return true, key[len(trieNodeAccountPrefix):]
   282  }
   283  
   284  // IsAccountTrieNode reports whether a provided database entry is an account
   285  // trie node in path-based state scheme.
   286  func IsAccountTrieNode(key []byte) bool {
   287  	ok, _ := ResolveAccountTrieNodeKey(key)
   288  	return ok
   289  }
   290  
   291  // ResolveStorageTrieNode reports whether a provided database entry is a storage
   292  // trie node in path-based state scheme, and returns the resolved account hash
   293  // and node path if so.
   294  func ResolveStorageTrieNode(key []byte) (bool, common.Hash, []byte) {
   295  	if !bytes.HasPrefix(key, trieNodeStoragePrefix) {
   296  		return false, common.Hash{}, nil
   297  	}
   298  	// The remaining key consists of 2 parts:
   299  	// - 32 bytes account hash
   300  	// - hex node path whose length is in the range 0 to 64
   301  	if len(key) < len(trieNodeStoragePrefix)+common.HashLength {
   302  		return false, common.Hash{}, nil
   303  	}
   304  	if len(key) >= len(trieNodeStoragePrefix)+common.HashLength+common.HashLength*2 {
   305  		return false, common.Hash{}, nil
   306  	}
   307  	accountHash := common.BytesToHash(key[len(trieNodeStoragePrefix) : len(trieNodeStoragePrefix)+common.HashLength])
   308  	return true, accountHash, key[len(trieNodeStoragePrefix)+common.HashLength:]
   309  }
   310  
   311  // IsStorageTrieNode reports whether a provided database entry is a storage
   312  // trie node in path-based state scheme.
   313  func IsStorageTrieNode(key []byte) bool {
   314  	ok, _, _ := ResolveStorageTrieNode(key)
   315  	return ok
   316  }