github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/core/rawdb/schema.go (about)

     1  //  Copyright 2018 The go-ethereum Authors
     2  //  Copyright 2019 The go-aigar Authors
     3  //  This file is part of the go-aigar library.
     4  //
     5  //  The go-aigar 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-aigar 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-aigar library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  // Package rawdb contains a collection of low level database accessors.
    19  package rawdb
    20  
    21  import (
    22  	"encoding/binary"
    23  
    24  	"github.com/AigarNetwork/aigar/common"
    25  	"github.com/AigarNetwork/aigar/metrics"
    26  )
    27  
    28  // The fields below define the low level database schema prefixing.
    29  var (
    30  	// databaseVerisionKey tracks the current database version.
    31  	databaseVerisionKey = []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  	// fastTrieProgressKey tracks the number of trie entries imported during fast sync.
    43  	fastTrieProgressKey = []byte("TrieSync")
    44  
    45  	// Data item prefixes (use single byte to avoid mixing data types, avoid `i`, used for indexes).
    46  	headerPrefix       = []byte("h") // headerPrefix + num (uint64 big endian) + hash -> header
    47  	headerTDSuffix     = []byte("t") // headerPrefix + num (uint64 big endian) + hash + headerTDSuffix -> td
    48  	headerHashSuffix   = []byte("n") // headerPrefix + num (uint64 big endian) + headerHashSuffix -> hash
    49  	headerNumberPrefix = []byte("H") // headerNumberPrefix + hash -> num (uint64 big endian)
    50  
    51  	blockBodyPrefix     = []byte("b") // blockBodyPrefix + num (uint64 big endian) + hash -> block body
    52  	blockReceiptsPrefix = []byte("r") // blockReceiptsPrefix + num (uint64 big endian) + hash -> block receipts
    53  
    54  	txLookupPrefix  = []byte("l") // txLookupPrefix + hash -> transaction/receipt lookup metadata
    55  	bloomBitsPrefix = []byte("B") // bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash -> bloom bits
    56  
    57  	preimagePrefix = []byte("secure-key-")      // preimagePrefix + hash -> preimage
    58  	configPrefix   = []byte("ethereum-config-") // config prefix for the db
    59  
    60  	// Chain index prefixes (use `i` + single byte to avoid mixing data types).
    61  	BloomBitsIndexPrefix = []byte("iB") // BloomBitsIndexPrefix is the data table of a chain indexer to track its progress
    62  
    63  	preimageCounter    = metrics.NewRegisteredCounter("db/preimage/total", nil)
    64  	preimageHitCounter = metrics.NewRegisteredCounter("db/preimage/hits", nil)
    65  )
    66  
    67  const (
    68  	// freezerHeaderTable indicates the name of the freezer header table.
    69  	freezerHeaderTable = "headers"
    70  
    71  	// freezerHashTable indicates the name of the freezer canonical hash table.
    72  	freezerHashTable = "hashes"
    73  
    74  	// freezerBodiesTable indicates the name of the freezer block body table.
    75  	freezerBodiesTable = "bodies"
    76  
    77  	// freezerReceiptTable indicates the name of the freezer receipts table.
    78  	freezerReceiptTable = "receipts"
    79  
    80  	// freezerDifficultyTable indicates the name of the freezer total difficulty table.
    81  	freezerDifficultyTable = "diffs"
    82  )
    83  
    84  // freezerNoSnappy configures whether compression is disabled for the ancient-tables.
    85  // Hashes and difficulties don't compress well.
    86  var freezerNoSnappy = map[string]bool{
    87  	freezerHeaderTable:     false,
    88  	freezerHashTable:       true,
    89  	freezerBodiesTable:     false,
    90  	freezerReceiptTable:    false,
    91  	freezerDifficultyTable: true,
    92  }
    93  
    94  // LegacyTxLookupEntry is the legacy TxLookupEntry definition with some unnecessary
    95  // fields.
    96  type LegacyTxLookupEntry struct {
    97  	BlockHash  common.Hash
    98  	BlockIndex uint64
    99  	Index      uint64
   100  }
   101  
   102  // encodeBlockNumber encodes a block number as big endian uint64
   103  func encodeBlockNumber(number uint64) []byte {
   104  	enc := make([]byte, 8)
   105  	binary.BigEndian.PutUint64(enc, number)
   106  	return enc
   107  }
   108  
   109  // headerKeyPrefix = headerPrefix + num (uint64 big endian)
   110  func headerKeyPrefix(number uint64) []byte {
   111  	return append(headerPrefix, encodeBlockNumber(number)...)
   112  }
   113  
   114  // headerKey = headerPrefix + num (uint64 big endian) + hash
   115  func headerKey(number uint64, hash common.Hash) []byte {
   116  	return append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
   117  }
   118  
   119  // headerTDKey = headerPrefix + num (uint64 big endian) + hash + headerTDSuffix
   120  func headerTDKey(number uint64, hash common.Hash) []byte {
   121  	return append(headerKey(number, hash), headerTDSuffix...)
   122  }
   123  
   124  // headerHashKey = headerPrefix + num (uint64 big endian) + headerHashSuffix
   125  func headerHashKey(number uint64) []byte {
   126  	return append(append(headerPrefix, encodeBlockNumber(number)...), headerHashSuffix...)
   127  }
   128  
   129  // headerNumberKey = headerNumberPrefix + hash
   130  func headerNumberKey(hash common.Hash) []byte {
   131  	return append(headerNumberPrefix, hash.Bytes()...)
   132  }
   133  
   134  // blockBodyKey = blockBodyPrefix + num (uint64 big endian) + hash
   135  func blockBodyKey(number uint64, hash common.Hash) []byte {
   136  	return append(append(blockBodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
   137  }
   138  
   139  // blockReceiptsKey = blockReceiptsPrefix + num (uint64 big endian) + hash
   140  func blockReceiptsKey(number uint64, hash common.Hash) []byte {
   141  	return append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
   142  }
   143  
   144  // txLookupKey = txLookupPrefix + hash
   145  func txLookupKey(hash common.Hash) []byte {
   146  	return append(txLookupPrefix, hash.Bytes()...)
   147  }
   148  
   149  // bloomBitsKey = bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash
   150  func bloomBitsKey(bit uint, section uint64, hash common.Hash) []byte {
   151  	key := append(append(bloomBitsPrefix, make([]byte, 10)...), hash.Bytes()...)
   152  
   153  	binary.BigEndian.PutUint16(key[1:], uint16(bit))
   154  	binary.BigEndian.PutUint64(key[3:], section)
   155  
   156  	return key
   157  }
   158  
   159  // preimageKey = preimagePrefix + hash
   160  func preimageKey(hash common.Hash) []byte {
   161  	return append(preimagePrefix, hash.Bytes()...)
   162  }
   163  
   164  // configKey = configPrefix + hash
   165  func configKey(hash common.Hash) []byte {
   166  	return append(configPrefix, hash.Bytes()...)
   167  }