github.com/cheng762/platon-go@v1.8.17-0.20190529111256-7deff2d7be26/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/PlatONnetwork/PlatON-Go/common"
    24  	"github.com/PlatONnetwork/PlatON-Go/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 know header's hash.
    33  	headHeaderKey = []byte("LastHeader")
    34  
    35  	// headBlockKey tracks the latest know full block's hash.
    36  	headBlockKey = []byte("LastBlock")
    37  
    38  	// headFastBlockKey tracks the latest known incomplete block's hash duirng 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  	// Data item prefixes (use single byte to avoid mixing data types, avoid `i`, used for indexes).
    45  	headerPrefix       = []byte("h") // headerPrefix + num (uint64 big endian) + hash -> header
    46  	headerHashSuffix   = []byte("n") // headerPrefix + num (uint64 big endian) + headerHashSuffix -> hash
    47  	headerNumberPrefix = []byte("H") // headerNumberPrefix + hash -> num (uint64 big endian)
    48  
    49  	blockBodyPrefix     = []byte("b") // blockBodyPrefix + num (uint64 big endian) + hash -> block body
    50  	blockReceiptsPrefix = []byte("r") // blockReceiptsPrefix + num (uint64 big endian) + hash -> block receipts
    51  	blockConfirmSignsPrefix = []byte("cs") // blockConfirmSignsPrefix + num (uint64 big endian) + hash
    52  
    53  	txLookupPrefix  = []byte("l") // txLookupPrefix + hash -> transaction/receipt lookup metadata
    54  	bloomBitsPrefix = []byte("B") // bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash -> bloom bits
    55  
    56  	preimagePrefix = []byte("secure-key-")      // preimagePrefix + hash -> preimage
    57  	configPrefix   = []byte("ethereum-config-") // config prefix for the db
    58  
    59  	// Chain index prefixes (use `i` + single byte to avoid mixing data types).
    60  	BloomBitsIndexPrefix = []byte("iB") // BloomBitsIndexPrefix is the data table of a chain indexer to track its progress
    61  
    62  	preimageCounter    = metrics.NewRegisteredCounter("db/preimage/total", nil)
    63  	preimageHitCounter = metrics.NewRegisteredCounter("db/preimage/hits", nil)
    64  )
    65  
    66  // TxLookupEntry is a positional metadata to help looking up the data content of
    67  // a transaction or receipt given only its hash.
    68  type TxLookupEntry struct {
    69  	BlockHash  common.Hash
    70  	BlockIndex uint64
    71  	Index      uint64
    72  }
    73  
    74  // encodeBlockNumber encodes a block number as big endian uint64
    75  func encodeBlockNumber(number uint64) []byte {
    76  	enc := make([]byte, 8)
    77  	binary.BigEndian.PutUint64(enc, number)
    78  	return enc
    79  }
    80  
    81  // headerKey = headerPrefix + num (uint64 big endian) + hash
    82  func headerKey(number uint64, hash common.Hash) []byte {
    83  	return append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
    84  }
    85  
    86  
    87  // headerHashKey = headerPrefix + num (uint64 big endian) + headerHashSuffix
    88  func headerHashKey(number uint64) []byte {
    89  	return append(append(headerPrefix, encodeBlockNumber(number)...), headerHashSuffix...)
    90  }
    91  
    92  // headerNumberKey = headerNumberPrefix + hash
    93  func headerNumberKey(hash common.Hash) []byte {
    94  	return append(headerNumberPrefix, hash.Bytes()...)
    95  }
    96  
    97  // blockBodyKey = blockBodyPrefix + num (uint64 big endian) + hash
    98  func blockBodyKey(number uint64, hash common.Hash) []byte {
    99  	return append(append(blockBodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
   100  }
   101  
   102  // blockReceiptsKey = blockReceiptsPrefix + num (uint64 big endian) + hash
   103  func blockReceiptsKey(number uint64, hash common.Hash) []byte {
   104  	return append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
   105  }
   106  
   107  // blockConfirmSignsKey = blockConfirmSignsPrefix + num (uint64 big endian) + hash
   108  func blockConfirmSignsKey(number uint64, hash common.Hash) []byte {
   109  	return append(append(blockConfirmSignsPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
   110  }
   111  
   112  // txLookupKey = txLookupPrefix + hash
   113  func txLookupKey(hash common.Hash) []byte {
   114  	return append(txLookupPrefix, hash.Bytes()...)
   115  }
   116  
   117  // bloomBitsKey = bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash
   118  func bloomBitsKey(bit uint, section uint64, hash common.Hash) []byte {
   119  	key := append(append(bloomBitsPrefix, make([]byte, 10)...), hash.Bytes()...)
   120  
   121  	binary.BigEndian.PutUint16(key[1:], uint16(bit))
   122  	binary.BigEndian.PutUint64(key[3:], section)
   123  
   124  	return key
   125  }
   126  
   127  // preimageKey = preimagePrefix + hash
   128  func preimageKey(hash common.Hash) []byte {
   129  	return append(preimagePrefix, hash.Bytes()...)
   130  }
   131  
   132  // configKey = configPrefix + hash
   133  func configKey(hash common.Hash) []byte {
   134  	return append(configPrefix, hash.Bytes()...)
   135  }