github.com/edxfund/validator@v1.8.16-0.20181020093046-c1def72855da/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  	"strconv"
    23  
    24  	"github.com/EDXFund/Validator/common"
    25  	"github.com/EDXFund/Validator/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 know header's hash.
    34  	headHeaderKey = []byte("LastHeader")
    35  
    36  	// headBlockKey tracks the latest know full block's hash.
    37  	headBlockKey = []byte("LastBlock")
    38  
    39  	// headFastBlockKey tracks the latest known incomplete block's hash duirng 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 + shardId (uint16 big endian) + num (uint64 big endian) + hash -> header
    47  	headerTDSuffix     = []byte("t") // headerPrefix + shardId (uint16 big endian) + num (uint64 big endian) + hash + headerTDSuffix -> td
    48  	headerHashSuffix   = []byte("n") // headerPrefix + shardId (uint16 big endian) + num (uint64 big endian) + headerHashSuffix -> hash
    49  	headerNumberPrefix = []byte("H") // headerNumberPrefix + hash ->  shardId (uint16 big endian) + num (uint64 big endian)
    50  
    51  	blockBodyPrefix     = []byte("b") // blockBodyPrefix + shardId (uint16 big endian)+  num (uint64 big endian) + hash -> block body
    52  	blockReceiptsPrefix = []byte("r") // blockReceiptsPrefix + shardId (uint16 big endian)+ 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  // TxLookupEntry is a positional metadata to help looking up the data content of
    68  // a transaction or receipt given only its hash.
    69  type TxLookupEntry struct {
    70  	ShardId    uint16
    71  	BlockHash  common.Hash
    72  	BlockIndex uint64
    73  	Index      uint64
    74  }
    75  
    76  // encodeBlockNumber encodes a block number as big endian uint64
    77  func encodeBlockNumber(number uint64) []byte {
    78  	enc := make([]byte, 8)
    79  	binary.BigEndian.PutUint64(enc, number)
    80  	return enc
    81  }
    82  
    83  // encodeBlockNumber encodes a block number as big endian uint64
    84  func encodeBlockNumberWithShardId(shardId uint16, number uint64) []byte {
    85  	enc := make([]byte, 10)
    86  	binary.BigEndian.PutUint16(enc, shardId)
    87  	binary.BigEndian.PutUint64(enc[2:], number)
    88  	return enc
    89  }
    90  
    91  // headerKey = headerPrefix + shardId (uint16 big endian) + num (uint64 big endian) + hash
    92  func headerKey(shardId uint16, number uint64, hash common.Hash) []byte {
    93  	return append(append(append(headerPrefix, strconv.FormatUint(uint64(shardId), 16)...), encodeBlockNumber(number)...), hash.Bytes()...)
    94  
    95  }
    96  
    97  // headerTDKey = headerPrefix + shardId (uint16 big endian) + num (uint64 big endian) + hash + headerTDSuffix
    98  func headerTDKey(shardId uint16, number uint64, hash common.Hash) []byte {
    99  
   100  	return append(headerKey(shardId, number, hash), headerTDSuffix...)
   101  }
   102  
   103  // headerHashKey = headerPrefix + num (uint64 big endian) + headerHashSuffix
   104  func headerHashKey(shardId uint16, number uint64) []byte {
   105  
   106  	return append(append(append(headerPrefix, strconv.FormatUint(uint64(shardId), 16)...), encodeBlockNumber(number)...), headerHashSuffix...)
   107  }
   108  
   109  // headerNumberKey = headerNumberPrefix + hash
   110  func headerNumberKey(hash common.Hash) []byte {
   111  	return append(headerNumberPrefix, hash.Bytes()...)
   112  }
   113  
   114  // blockBodyKey = blockBodyPrefix + num (uint64 big endian) + hash
   115  func blockBodyKey(shardId uint16, number uint64, hash common.Hash) []byte {
   116  	enc := make([]byte, 2)
   117  	binary.BigEndian.PutUint16(enc, shardId)
   118  
   119  	return append(append(append(blockBodyPrefix, enc...), encodeBlockNumber(number)...), hash.Bytes()...)
   120  }
   121  
   122  // blockReceiptsKey = blockReceiptsPrefix + num (uint64 big endian) + hash
   123  func blockReceiptsKey(shardId uint16, number uint64, hash common.Hash) []byte {
   124  
   125  	return append(append(append(blockReceiptsPrefix, strconv.FormatUint(uint64(shardId), 16)...), encodeBlockNumber(number)...), hash.Bytes()...)
   126  }
   127  
   128  // txLookupKey = txLookupPrefix + hash
   129  func txLookupKey(hash common.Hash) []byte {
   130  	return append(txLookupPrefix, hash.Bytes()...)
   131  }
   132  
   133  // bloomBitsKey = bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash
   134  func bloomBitsKey(bit uint, section uint64, hash common.Hash) []byte {
   135  	key := append(append(bloomBitsPrefix, make([]byte, 10)...), hash.Bytes()...)
   136  
   137  	binary.BigEndian.PutUint16(key[1:], uint16(bit))
   138  	binary.BigEndian.PutUint64(key[3:], section)
   139  
   140  	return key
   141  }
   142  
   143  // preimageKey = preimagePrefix + hash
   144  func preimageKey(hash common.Hash) []byte {
   145  	return append(preimagePrefix, hash.Bytes()...)
   146  }
   147  
   148  // configKey = configPrefix + hash
   149  func configKey(hash common.Hash) []byte {
   150  	return append(configPrefix, hash.Bytes()...)
   151  }