github.com/Debrief-BC/go-debrief@v0.0.0-20200420203408-0c26ca968123/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  	"encoding/binary"
    22  
    23  	"github.com/Debrief-BC/go-debrief/common"
    24  	"github.com/Debrief-BC/go-debrief/metrics"
    25  )
    26  
    27  // The fields below define the low level database schema prefixing.
    28  var (
    29  	// databaseVerisionKey tracks the current database version.
    30  	databaseVerisionKey = []byte("DatabaseVersion")
    31  
    32  	// headHeaderKey tracks the latest known header's hash.
    33  	headHeaderKey = []byte("LastHeader")
    34  
    35  	// headBlockKey tracks the latest known full block's hash.
    36  	headBlockKey = []byte("LastBlock")
    37  
    38  	// headFastBlockKey tracks the latest known incomplete block's hash during fast sync.
    39  	headFastBlockKey = []byte("LastFast")
    40  
    41  	// fastTrieProgressKey tracks the number of trie entries imported during fast sync.
    42  	fastTrieProgressKey = []byte("TrieSync")
    43  
    44  	// snapshotRootKey tracks the hash of the last snapshot.
    45  	snapshotRootKey = []byte("SnapshotRoot")
    46  
    47  	// snapshotJournalKey tracks the in-memory diff layers across restarts.
    48  	snapshotJournalKey = []byte("SnapshotJournal")
    49  
    50  	// Data item prefixes (use single byte to avoid mixing data types, avoid `i`, used for indexes).
    51  	headerPrefix       = []byte("h") // headerPrefix + num (uint64 big endian) + hash -> header
    52  	headerTDSuffix     = []byte("t") // headerPrefix + num (uint64 big endian) + hash + headerTDSuffix -> td
    53  	headerHashSuffix   = []byte("n") // headerPrefix + num (uint64 big endian) + headerHashSuffix -> hash
    54  	headerNumberPrefix = []byte("H") // headerNumberPrefix + hash -> num (uint64 big endian)
    55  
    56  	blockBodyPrefix     = []byte("b") // blockBodyPrefix + num (uint64 big endian) + hash -> block body
    57  	blockReceiptsPrefix = []byte("r") // blockReceiptsPrefix + num (uint64 big endian) + hash -> block receipts
    58  
    59  	txLookupPrefix        = []byte("l") // txLookupPrefix + hash -> transaction/receipt lookup metadata
    60  	bloomBitsPrefix       = []byte("B") // bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash -> bloom bits
    61  	SnapshotAccountPrefix = []byte("a") // SnapshotAccountPrefix + account hash -> account trie value
    62  	SnapshotStoragePrefix = []byte("o") // SnapshotStoragePrefix + account hash + storage hash -> storage trie value
    63  
    64  	preimagePrefix = []byte("secure-key-")      // preimagePrefix + hash -> preimage
    65  	configPrefix   = []byte("ethereum-config-") // config prefix for the db
    66  
    67  	// Chain index prefixes (use `i` + single byte to avoid mixing data types).
    68  	BloomBitsIndexPrefix = []byte("iB") // BloomBitsIndexPrefix is the data table of a chain indexer to track its progress
    69  
    70  	preimageCounter    = metrics.NewRegisteredCounter("db/preimage/total", nil)
    71  	preimageHitCounter = metrics.NewRegisteredCounter("db/preimage/hits", nil)
    72  )
    73  
    74  const (
    75  	// freezerHeaderTable indicates the name of the freezer header table.
    76  	freezerHeaderTable = "headers"
    77  
    78  	// freezerHashTable indicates the name of the freezer canonical hash table.
    79  	freezerHashTable = "hashes"
    80  
    81  	// freezerBodiesTable indicates the name of the freezer block body table.
    82  	freezerBodiesTable = "bodies"
    83  
    84  	// freezerReceiptTable indicates the name of the freezer receipts table.
    85  	freezerReceiptTable = "receipts"
    86  
    87  	// freezerDifficultyTable indicates the name of the freezer total difficulty table.
    88  	freezerDifficultyTable = "diffs"
    89  )
    90  
    91  // freezerNoSnappy configures whether compression is disabled for the ancient-tables.
    92  // Hashes and difficulties don't compress well.
    93  var freezerNoSnappy = map[string]bool{
    94  	freezerHeaderTable:     false,
    95  	freezerHashTable:       true,
    96  	freezerBodiesTable:     false,
    97  	freezerReceiptTable:    false,
    98  	freezerDifficultyTable: true,
    99  }
   100  
   101  // LegacyTxLookupEntry is the legacy TxLookupEntry definition with some unnecessary
   102  // fields.
   103  type LegacyTxLookupEntry struct {
   104  	BlockHash  common.Hash
   105  	BlockIndex uint64
   106  	Index      uint64
   107  }
   108  
   109  // encodeBlockNumber encodes a block number as big endian uint64
   110  func encodeBlockNumber(number uint64) []byte {
   111  	enc := make([]byte, 8)
   112  	binary.BigEndian.PutUint64(enc, number)
   113  	return enc
   114  }
   115  
   116  // headerKeyPrefix = headerPrefix + num (uint64 big endian)
   117  func headerKeyPrefix(number uint64) []byte {
   118  	return append(headerPrefix, encodeBlockNumber(number)...)
   119  }
   120  
   121  // headerKey = headerPrefix + num (uint64 big endian) + hash
   122  func headerKey(number uint64, hash common.Hash) []byte {
   123  	return append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
   124  }
   125  
   126  // headerTDKey = headerPrefix + num (uint64 big endian) + hash + headerTDSuffix
   127  func headerTDKey(number uint64, hash common.Hash) []byte {
   128  	return append(headerKey(number, hash), headerTDSuffix...)
   129  }
   130  
   131  // headerHashKey = headerPrefix + num (uint64 big endian) + headerHashSuffix
   132  func headerHashKey(number uint64) []byte {
   133  	return append(append(headerPrefix, encodeBlockNumber(number)...), headerHashSuffix...)
   134  }
   135  
   136  // headerNumberKey = headerNumberPrefix + hash
   137  func headerNumberKey(hash common.Hash) []byte {
   138  	return append(headerNumberPrefix, hash.Bytes()...)
   139  }
   140  
   141  // blockBodyKey = blockBodyPrefix + num (uint64 big endian) + hash
   142  func blockBodyKey(number uint64, hash common.Hash) []byte {
   143  	return append(append(blockBodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
   144  }
   145  
   146  // blockReceiptsKey = blockReceiptsPrefix + num (uint64 big endian) + hash
   147  func blockReceiptsKey(number uint64, hash common.Hash) []byte {
   148  	return append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
   149  }
   150  
   151  // txLookupKey = txLookupPrefix + hash
   152  func txLookupKey(hash common.Hash) []byte {
   153  	return append(txLookupPrefix, hash.Bytes()...)
   154  }
   155  
   156  // accountSnapshotKey = SnapshotAccountPrefix + hash
   157  func accountSnapshotKey(hash common.Hash) []byte {
   158  	return append(SnapshotAccountPrefix, hash.Bytes()...)
   159  }
   160  
   161  // storageSnapshotKey = SnapshotStoragePrefix + account hash + storage hash
   162  func storageSnapshotKey(accountHash, storageHash common.Hash) []byte {
   163  	return append(append(SnapshotStoragePrefix, accountHash.Bytes()...), storageHash.Bytes()...)
   164  }
   165  
   166  // storageSnapshotsKey = SnapshotStoragePrefix + account hash + storage hash
   167  func storageSnapshotsKey(accountHash common.Hash) []byte {
   168  	return append(SnapshotStoragePrefix, accountHash.Bytes()...)
   169  }
   170  
   171  // bloomBitsKey = bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash
   172  func bloomBitsKey(bit uint, section uint64, hash common.Hash) []byte {
   173  	key := append(append(bloomBitsPrefix, make([]byte, 10)...), hash.Bytes()...)
   174  
   175  	binary.BigEndian.PutUint16(key[1:], uint16(bit))
   176  	binary.BigEndian.PutUint64(key[3:], section)
   177  
   178  	return key
   179  }
   180  
   181  // preimageKey = preimagePrefix + hash
   182  func preimageKey(hash common.Hash) []byte {
   183  	return append(preimagePrefix, hash.Bytes()...)
   184  }
   185  
   186  // configKey = configPrefix + hash
   187  func configKey(hash common.Hash) []byte {
   188  	return append(configPrefix, hash.Bytes()...)
   189  }