github.com/bcskill/bcschain/v3@v3.4.9-beta2/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/bcskill/bcschain/v3/common"
    24  )
    25  
    26  const (
    27  	// Data item prefixes (use single byte to avoid mixing data types, avoid `i`).
    28  	headerPrefix        byte = 'h' // headerPrefix + num (uint64 big endian) + hash -> header
    29  	tdSuffix            byte = 't' // headerPrefix + num (uint64 big endian) + hash + tdSuffix -> td
    30  	numSuffix           byte = 'n' // headerPrefix + num (uint64 big endian) + numSuffix -> hash
    31  	blockHashPrefix     byte = 'H' // blockHashPrefix + hash -> num (uint64 big endian)
    32  	bodyPrefix          byte = 'b' // bodyPrefix + num (uint64 big endian) + hash -> block body
    33  	blockReceiptsPrefix byte = 'r' // blockReceiptsPrefix + num (uint64 big endian) + hash -> block receipts
    34  	lookupPrefix        byte = 'l' // lookupPrefix + hash -> transaction/receipt lookup metadata
    35  	bloomBitsPrefix     byte = 'B' // bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash -> bloom bits
    36  )
    37  
    38  // The fields below define the low level database schema prefixing.
    39  var (
    40  	// databaseVersionKey tracks the current database version.
    41  	databaseVersionKey = []byte("BlockchainVersion")
    42  
    43  	// headHeaderKey tracks the latest know header's hash.
    44  	headHeaderKey = []byte("LastHeader")
    45  
    46  	// headBlockKey tracks the latest know full block's hash.
    47  	headBlockKey = []byte("LastBlock")
    48  
    49  	// headFastBlockKey tracks the latest known incomplete block's hash duirng fast sync.
    50  	headFastBlockKey = []byte("LastFast")
    51  
    52  	// fastTrieProgressKey tracks the number of trie entries imported during fast sync.
    53  	fastTrieProgressKey = []byte("TrieSync")
    54  
    55  	preimagePrefix = "secure-key-"              // preimagePrefix + hash -> preimage
    56  	configPrefix   = []byte("ethereum-config-") // config prefix for the db
    57  
    58  	// Chain index prefixes (use `i` + single byte to avoid mixing data types).
    59  	BloomBitsIndexPrefix = []byte("iB") // BloomBitsIndexPrefix is the data table of a chain indexer to track its progress
    60  )
    61  
    62  // LegacyTxLookupEntry is the legacy TxLookupEntry definition with some unnecessary
    63  // fields.
    64  type LegacyTxLookupEntry struct {
    65  	BlockHash  common.Hash
    66  	BlockIndex uint64
    67  	Index      uint64
    68  }
    69  
    70  // encodeBlockNumber encodes a block number as big endian uint64
    71  func encodeBlockNumber(number uint64) []byte {
    72  	enc := make([]byte, 8)
    73  	binary.BigEndian.PutUint64(enc, number)
    74  	return enc
    75  }
    76  
    77  func numHashKey(prefix byte, number uint64, hash common.Hash) []byte {
    78  	var k [41]byte
    79  	k[0] = prefix
    80  	binary.BigEndian.PutUint64(k[1:], number)
    81  	copy(k[9:], hash[:])
    82  	return k[:]
    83  }
    84  
    85  func numKey(number uint64) []byte {
    86  	var k [10]byte
    87  	k[0] = headerPrefix
    88  	binary.BigEndian.PutUint64(k[1:], number)
    89  	k[9] = numSuffix
    90  	return k[:]
    91  }
    92  
    93  func tdKey(number uint64, hash common.Hash) []byte {
    94  	var k [42]byte
    95  	k[0] = headerPrefix
    96  	binary.BigEndian.PutUint64(k[1:], number)
    97  	copy(k[9:], hash[:])
    98  	k[41] = tdSuffix
    99  	return k[:]
   100  }
   101  
   102  func hashKey(prefix byte, hash common.Hash) []byte {
   103  	var k [33]byte
   104  	k[0] = prefix
   105  	copy(k[1:], hash[:])
   106  	return k[:]
   107  }
   108  
   109  // preimageKey = preimagePrefix + hash
   110  func preimageKey(hash common.Hash) []byte {
   111  	return append([]byte(preimagePrefix), hash.Bytes()...)
   112  }
   113  
   114  // configKey = configPrefix + hash
   115  func configKey(hash common.Hash) []byte {
   116  	return append(configPrefix, hash.Bytes()...)
   117  }