github.com/zhiqiangxu/go-ethereum@v1.9.16-0.20210824055606-be91cfdebc48/core/rawdb/accessors_indexes.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
    18  
    19  import (
    20  	"math/big"
    21  
    22  	"github.com/zhiqiangxu/go-ethereum/common"
    23  	"github.com/zhiqiangxu/go-ethereum/core/types"
    24  	"github.com/zhiqiangxu/go-ethereum/ethdb"
    25  	"github.com/zhiqiangxu/go-ethereum/log"
    26  	"github.com/zhiqiangxu/go-ethereum/params"
    27  	"github.com/zhiqiangxu/go-ethereum/rlp"
    28  )
    29  
    30  // ReadTxLookupEntry retrieves the positional metadata associated with a transaction
    31  // hash to allow retrieving the transaction or receipt by hash.
    32  func ReadTxLookupEntry(db ethdb.Reader, hash common.Hash) *uint64 {
    33  	data, _ := db.Get(txLookupKey(hash))
    34  	if len(data) == 0 {
    35  		return nil
    36  	}
    37  	// Database v6 tx lookup just stores the block number
    38  	if len(data) < common.HashLength {
    39  		number := new(big.Int).SetBytes(data).Uint64()
    40  		return &number
    41  	}
    42  	// Database v4-v5 tx lookup format just stores the hash
    43  	if len(data) == common.HashLength {
    44  		return ReadHeaderNumber(db, common.BytesToHash(data))
    45  	}
    46  	// Finally try database v3 tx lookup format
    47  	var entry LegacyTxLookupEntry
    48  	if err := rlp.DecodeBytes(data, &entry); err != nil {
    49  		log.Error("Invalid transaction lookup entry RLP", "hash", hash, "blob", data, "err", err)
    50  		return nil
    51  	}
    52  	return &entry.BlockIndex
    53  }
    54  
    55  // WriteTxLookupEntries stores a positional metadata for every transaction from
    56  // a block, enabling hash based transaction and receipt lookups.
    57  func WriteTxLookupEntries(db ethdb.KeyValueWriter, block *types.Block) {
    58  	number := block.Number().Bytes()
    59  	for _, tx := range block.Transactions() {
    60  		if err := db.Put(txLookupKey(tx.Hash()), number); err != nil {
    61  			log.Crit("Failed to store transaction lookup entry", "err", err)
    62  		}
    63  	}
    64  }
    65  
    66  // WriteTxLookupEntriesByHash is identical to WriteTxLookupEntries, but does not
    67  // require a full types.Block as input.
    68  func WriteTxLookupEntriesByHash(db ethdb.KeyValueWriter, number uint64, hashes []common.Hash) {
    69  	numberBytes := new(big.Int).SetUint64(number).Bytes()
    70  	for _, hash := range hashes {
    71  		if err := db.Put(txLookupKey(hash), numberBytes); err != nil {
    72  			log.Crit("Failed to store transaction lookup entry", "err", err)
    73  		}
    74  	}
    75  }
    76  
    77  // DeleteTxLookupEntry removes all transaction data associated with a hash.
    78  func DeleteTxLookupEntry(db ethdb.KeyValueWriter, hash common.Hash) {
    79  	if err := db.Delete(txLookupKey(hash)); err != nil {
    80  		log.Crit("Failed to delete transaction lookup entry", "err", err)
    81  	}
    82  }
    83  
    84  // DeleteTxLookupEntries removes all transaction lookups for a given block.
    85  func DeleteTxLookupEntriesByHash(db ethdb.KeyValueWriter, hashes []common.Hash) {
    86  	for _, hash := range hashes {
    87  		if err := db.Delete(txLookupKey(hash)); err != nil {
    88  			log.Crit("Failed to delete transaction lookup entry", "err", err)
    89  		}
    90  	}
    91  }
    92  
    93  // ReadTransaction retrieves a specific transaction from the database, along with
    94  // its added positional metadata.
    95  func ReadTransaction(db ethdb.Reader, hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64) {
    96  	blockNumber := ReadTxLookupEntry(db, hash)
    97  	if blockNumber == nil {
    98  		return nil, common.Hash{}, 0, 0
    99  	}
   100  	blockHash := ReadCanonicalHash(db, *blockNumber)
   101  	if blockHash == (common.Hash{}) {
   102  		return nil, common.Hash{}, 0, 0
   103  	}
   104  	body := ReadBody(db, blockHash, *blockNumber)
   105  	if body == nil {
   106  		log.Error("Transaction referenced missing", "number", blockNumber, "hash", blockHash)
   107  		return nil, common.Hash{}, 0, 0
   108  	}
   109  	for txIndex, tx := range body.Transactions {
   110  		if tx.Hash() == hash {
   111  			return tx, blockHash, *blockNumber, uint64(txIndex)
   112  		}
   113  	}
   114  	log.Error("Transaction not found", "number", blockNumber, "hash", blockHash, "txhash", hash)
   115  	return nil, common.Hash{}, 0, 0
   116  }
   117  
   118  // ReadReceipt retrieves a specific transaction receipt from the database, along with
   119  // its added positional metadata.
   120  func ReadReceipt(db ethdb.Reader, hash common.Hash, config *params.ChainConfig) (*types.Receipt, common.Hash, uint64, uint64) {
   121  	// Retrieve the context of the receipt based on the transaction hash
   122  	blockNumber := ReadTxLookupEntry(db, hash)
   123  	if blockNumber == nil {
   124  		return nil, common.Hash{}, 0, 0
   125  	}
   126  	blockHash := ReadCanonicalHash(db, *blockNumber)
   127  	if blockHash == (common.Hash{}) {
   128  		return nil, common.Hash{}, 0, 0
   129  	}
   130  	// Read all the receipts from the block and return the one with the matching hash
   131  	receipts := ReadReceipts(db, blockHash, *blockNumber, config)
   132  	for receiptIndex, receipt := range receipts {
   133  		if receipt.TxHash == hash {
   134  			return receipt, blockHash, *blockNumber, uint64(receiptIndex)
   135  		}
   136  	}
   137  	log.Error("Receipt not found", "number", blockNumber, "hash", blockHash, "txhash", hash)
   138  	return nil, common.Hash{}, 0, 0
   139  }
   140  
   141  // ReadBloomBits retrieves the compressed bloom bit vector belonging to the given
   142  // section and bit index from the.
   143  func ReadBloomBits(db ethdb.KeyValueReader, bit uint, section uint64, head common.Hash) ([]byte, error) {
   144  	return db.Get(bloomBitsKey(bit, section, head))
   145  }
   146  
   147  // WriteBloomBits stores the compressed bloom bits vector belonging to the given
   148  // section and bit index.
   149  func WriteBloomBits(db ethdb.KeyValueWriter, bit uint, section uint64, head common.Hash, bits []byte) {
   150  	if err := db.Put(bloomBitsKey(bit, section, head), bits); err != nil {
   151  		log.Crit("Failed to store bloom bits", "err", err)
   152  	}
   153  }