github.com/jincm/wesharechain@v0.0.0-20210122032815-1537409ce26a/chain/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  	"github.com/ethereum/go-ethereum/common"
    21  	"github.com/ethereum/go-ethereum/core/types"
    22  	"github.com/ethereum/go-ethereum/log"
    23  	"github.com/ethereum/go-ethereum/rlp"
    24  )
    25  
    26  // ReadTxLookupEntry retrieves the positional metadata associated with a transaction
    27  // hash to allow retrieving the transaction or receipt by hash.
    28  func ReadTxLookupEntry(db DatabaseReader, hash common.Hash) common.Hash {
    29  	data, _ := db.Get(txLookupKey(hash))
    30  	if len(data) == 0 {
    31  		return common.Hash{}
    32  	}
    33  	if len(data) == common.HashLength {
    34  		return common.BytesToHash(data)
    35  	}
    36  	// Probably it's legacy txlookup entry data, try to decode it.
    37  	var entry LegacyTxLookupEntry
    38  	if err := rlp.DecodeBytes(data, &entry); err != nil {
    39  		log.Error("Invalid transaction lookup entry RLP", "hash", hash, "blob", data, "err", err)
    40  		return common.Hash{}
    41  	}
    42  	return entry.BlockHash
    43  }
    44  
    45  // WriteTxLookupEntries stores a positional metadata for every transaction from
    46  // a block, enabling hash based transaction and receipt lookups.
    47  func WriteTxLookupEntries(db DatabaseWriter, block *types.Block) {
    48  	for _, tx := range block.Transactions() {
    49  		if err := db.Put(txLookupKey(tx.Hash()), block.Hash().Bytes()); err != nil {
    50  			log.Crit("Failed to store transaction lookup entry", "err", err)
    51  		}
    52  	}
    53  }
    54  
    55  // DeleteTxLookupEntry removes all transaction data associated with a hash.
    56  func DeleteTxLookupEntry(db DatabaseDeleter, hash common.Hash) {
    57  	db.Delete(txLookupKey(hash))
    58  }
    59  
    60  // ReadTransaction retrieves a specific transaction from the database, along with
    61  // its added positional metadata.
    62  func ReadTransaction(db DatabaseReader, hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64) {
    63  	blockHash := ReadTxLookupEntry(db, hash)
    64  	if blockHash == (common.Hash{}) {
    65  		return nil, common.Hash{}, 0, 0
    66  	}
    67  	blockNumber := ReadHeaderNumber(db, blockHash)
    68  	if blockNumber == nil {
    69  		return nil, common.Hash{}, 0, 0
    70  	}
    71  	body := ReadBody(db, blockHash, *blockNumber)
    72  	if body == nil {
    73  		log.Error("Transaction referenced missing", "number", blockNumber, "hash", blockHash)
    74  		return nil, common.Hash{}, 0, 0
    75  	}
    76  	for txIndex, tx := range body.Transactions {
    77  		if tx.Hash() == hash {
    78  			return tx, blockHash, *blockNumber, uint64(txIndex)
    79  		}
    80  	}
    81  	log.Error("Transaction not found", "number", blockNumber, "hash", blockHash, "txhash", hash)
    82  	return nil, common.Hash{}, 0, 0
    83  }
    84  
    85  // ReadReceipt retrieves a specific transaction receipt from the database, along with
    86  // its added positional metadata.
    87  func ReadReceipt(db DatabaseReader, hash common.Hash) (*types.Receipt, common.Hash, uint64, uint64) {
    88  	blockHash := ReadTxLookupEntry(db, hash)
    89  	if blockHash == (common.Hash{}) {
    90  		return nil, common.Hash{}, 0, 0
    91  	}
    92  	blockNumber := ReadHeaderNumber(db, blockHash)
    93  	if blockNumber == nil {
    94  		return nil, common.Hash{}, 0, 0
    95  	}
    96  	receipts := ReadReceipts(db, blockHash, *blockNumber)
    97  	for receiptIndex, receipt := range receipts {
    98  		if receipt.TxHash == hash {
    99  			return receipt, blockHash, *blockNumber, uint64(receiptIndex)
   100  		}
   101  	}
   102  	log.Error("Receipt not found", "number", blockNumber, "hash", blockHash, "txhash", hash)
   103  	return nil, common.Hash{}, 0, 0
   104  }
   105  
   106  // ReadBloomBits retrieves the compressed bloom bit vector belonging to the given
   107  // section and bit index from the.
   108  func ReadBloomBits(db DatabaseReader, bit uint, section uint64, head common.Hash) ([]byte, error) {
   109  	return db.Get(bloomBitsKey(bit, section, head))
   110  }
   111  
   112  // WriteBloomBits stores the compressed bloom bits vector belonging to the given
   113  // section and bit index.
   114  func WriteBloomBits(db DatabaseWriter, bit uint, section uint64, head common.Hash, bits []byte) {
   115  	if err := db.Put(bloomBitsKey(bit, section, head), bits); err != nil {
   116  		log.Crit("Failed to store bloom bits", "err", err)
   117  	}
   118  }