github.com/MaynardMiner/ethereumprogpow@v1.8.23/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/ethereumprogpow/ethereumprogpow/common"
    21  	"github.com/ethereumprogpow/ethereumprogpow/core/types"
    22  	"github.com/ethereumprogpow/ethereumprogpow/log"
    23  	"github.com/ethereumprogpow/ethereumprogpow/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, uint64, uint64) {
    29  	data, _ := db.Get(txLookupKey(hash))
    30  	if len(data) == 0 {
    31  		return common.Hash{}, 0, 0
    32  	}
    33  	var entry TxLookupEntry
    34  	if err := rlp.DecodeBytes(data, &entry); err != nil {
    35  		log.Error("Invalid transaction lookup entry RLP", "hash", hash, "err", err)
    36  		return common.Hash{}, 0, 0
    37  	}
    38  	return entry.BlockHash, entry.BlockIndex, entry.Index
    39  }
    40  
    41  // WriteTxLookupEntries stores a positional metadata for every transaction from
    42  // a block, enabling hash based transaction and receipt lookups.
    43  func WriteTxLookupEntries(db DatabaseWriter, block *types.Block) {
    44  	for i, tx := range block.Transactions() {
    45  		entry := TxLookupEntry{
    46  			BlockHash:  block.Hash(),
    47  			BlockIndex: block.NumberU64(),
    48  			Index:      uint64(i),
    49  		}
    50  		data, err := rlp.EncodeToBytes(entry)
    51  		if err != nil {
    52  			log.Crit("Failed to encode transaction lookup entry", "err", err)
    53  		}
    54  		if err := db.Put(txLookupKey(tx.Hash()), data); err != nil {
    55  			log.Crit("Failed to store transaction lookup entry", "err", err)
    56  		}
    57  	}
    58  }
    59  
    60  // DeleteTxLookupEntry removes all transaction data associated with a hash.
    61  func DeleteTxLookupEntry(db DatabaseDeleter, hash common.Hash) {
    62  	db.Delete(txLookupKey(hash))
    63  }
    64  
    65  // ReadTransaction retrieves a specific transaction from the database, along with
    66  // its added positional metadata.
    67  func ReadTransaction(db DatabaseReader, hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64) {
    68  	blockHash, blockNumber, txIndex := ReadTxLookupEntry(db, hash)
    69  	if blockHash == (common.Hash{}) {
    70  		return nil, common.Hash{}, 0, 0
    71  	}
    72  	body := ReadBody(db, blockHash, blockNumber)
    73  	if body == nil || len(body.Transactions) <= int(txIndex) {
    74  		log.Error("Transaction referenced missing", "number", blockNumber, "hash", blockHash, "index", txIndex)
    75  		return nil, common.Hash{}, 0, 0
    76  	}
    77  	return body.Transactions[txIndex], blockHash, blockNumber, txIndex
    78  }
    79  
    80  // ReadReceipt retrieves a specific transaction receipt from the database, along with
    81  // its added positional metadata.
    82  func ReadReceipt(db DatabaseReader, hash common.Hash) (*types.Receipt, common.Hash, uint64, uint64) {
    83  	blockHash, blockNumber, receiptIndex := ReadTxLookupEntry(db, hash)
    84  	if blockHash == (common.Hash{}) {
    85  		return nil, common.Hash{}, 0, 0
    86  	}
    87  	receipts := ReadReceipts(db, blockHash, blockNumber)
    88  	if len(receipts) <= int(receiptIndex) {
    89  		log.Error("Receipt refereced missing", "number", blockNumber, "hash", blockHash, "index", receiptIndex)
    90  		return nil, common.Hash{}, 0, 0
    91  	}
    92  	return receipts[receiptIndex], blockHash, blockNumber, receiptIndex
    93  }
    94  
    95  // ReadBloomBits retrieves the compressed bloom bit vector belonging to the given
    96  // section and bit index from the.
    97  func ReadBloomBits(db DatabaseReader, bit uint, section uint64, head common.Hash) ([]byte, error) {
    98  	return db.Get(bloomBitsKey(bit, section, head))
    99  }
   100  
   101  // WriteBloomBits stores the compressed bloom bits vector belonging to the given
   102  // section and bit index.
   103  func WriteBloomBits(db DatabaseWriter, bit uint, section uint64, head common.Hash, bits []byte) {
   104  	if err := db.Put(bloomBitsKey(bit, section, head), bits); err != nil {
   105  		log.Crit("Failed to store bloom bits", "err", err)
   106  	}
   107  }