github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/core/rawdb/accessors_indexes.go (about)

     1  //  Copyright 2018 The go-ethereum Authors
     2  //  Copyright 2019 The go-aigar Authors
     3  //  This file is part of the go-aigar library.
     4  //
     5  //  The go-aigar library is free software: you can redistribute it and/or modify
     6  //  it under the terms of the GNU Lesser General Public License as published by
     7  //  the Free Software Foundation, either version 3 of the License, or
     8  //  (at your option) any later version.
     9  //
    10  //  The go-aigar library is distributed in the hope that it will be useful,
    11  //  but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  //  GNU Lesser General Public License for more details.
    14  //
    15  //  You should have received a copy of the GNU Lesser General Public License
    16  //  along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package rawdb
    19  
    20  import (
    21  	"math/big"
    22  
    23  	"github.com/AigarNetwork/aigar/common"
    24  	"github.com/AigarNetwork/aigar/core/types"
    25  	"github.com/AigarNetwork/aigar/ethdb"
    26  	"github.com/AigarNetwork/aigar/log"
    27  	"github.com/AigarNetwork/aigar/params"
    28  	"github.com/AigarNetwork/aigar/rlp"
    29  )
    30  
    31  // ReadTxLookupEntry retrieves the positional metadata associated with a transaction
    32  // hash to allow retrieving the transaction or receipt by hash.
    33  func ReadTxLookupEntry(db ethdb.Reader, hash common.Hash) *uint64 {
    34  	data, _ := db.Get(txLookupKey(hash))
    35  	if len(data) == 0 {
    36  		return nil
    37  	}
    38  	// Database v6 tx lookup just stores the block number
    39  	if len(data) < common.HashLength {
    40  		number := new(big.Int).SetBytes(data).Uint64()
    41  		return &number
    42  	}
    43  	// Database v4-v5 tx lookup format just stores the hash
    44  	if len(data) == common.HashLength {
    45  		return ReadHeaderNumber(db, common.BytesToHash(data))
    46  	}
    47  	// Finally try database v3 tx lookup format
    48  	var entry LegacyTxLookupEntry
    49  	if err := rlp.DecodeBytes(data, &entry); err != nil {
    50  		log.Error("Invalid transaction lookup entry RLP", "hash", hash, "blob", data, "err", err)
    51  		return nil
    52  	}
    53  	return &entry.BlockIndex
    54  }
    55  
    56  // WriteTxLookupEntries stores a positional metadata for every transaction from
    57  // a block, enabling hash based transaction and receipt lookups.
    58  func WriteTxLookupEntries(db ethdb.KeyValueWriter, block *types.Block) {
    59  	number := block.Number().Bytes()
    60  	for _, tx := range block.Transactions() {
    61  		if err := db.Put(txLookupKey(tx.Hash()), number); err != nil {
    62  			log.Crit("Failed to store transaction lookup entry", "err", err)
    63  		}
    64  	}
    65  }
    66  
    67  // DeleteTxLookupEntry removes all transaction data associated with a hash.
    68  func DeleteTxLookupEntry(db ethdb.KeyValueWriter, hash common.Hash) {
    69  	db.Delete(txLookupKey(hash))
    70  }
    71  
    72  // ReadTransaction retrieves a specific transaction from the database, along with
    73  // its added positional metadata.
    74  func ReadTransaction(db ethdb.Reader, hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64) {
    75  	blockNumber := ReadTxLookupEntry(db, hash)
    76  	if blockNumber == nil {
    77  		return nil, common.Hash{}, 0, 0
    78  	}
    79  	blockHash := ReadCanonicalHash(db, *blockNumber)
    80  	if blockHash == (common.Hash{}) {
    81  		return nil, common.Hash{}, 0, 0
    82  	}
    83  	body := ReadBody(db, blockHash, *blockNumber)
    84  	if body == nil {
    85  		log.Error("Transaction referenced missing", "number", blockNumber, "hash", blockHash)
    86  		return nil, common.Hash{}, 0, 0
    87  	}
    88  	for txIndex, tx := range body.Transactions {
    89  		if tx.Hash() == hash {
    90  			return tx, blockHash, *blockNumber, uint64(txIndex)
    91  		}
    92  	}
    93  	log.Error("Transaction not found", "number", blockNumber, "hash", blockHash, "txhash", hash)
    94  	return nil, common.Hash{}, 0, 0
    95  }
    96  
    97  // ReadReceipt retrieves a specific transaction receipt from the database, along with
    98  // its added positional metadata.
    99  func ReadReceipt(db ethdb.Reader, hash common.Hash, config *params.ChainConfig) (*types.Receipt, common.Hash, uint64, uint64) {
   100  	// Retrieve the context of the receipt based on the transaction hash
   101  	blockNumber := ReadTxLookupEntry(db, hash)
   102  	if blockNumber == nil {
   103  		return nil, common.Hash{}, 0, 0
   104  	}
   105  	blockHash := ReadCanonicalHash(db, *blockNumber)
   106  	if blockHash == (common.Hash{}) {
   107  		return nil, common.Hash{}, 0, 0
   108  	}
   109  	// Read all the receipts from the block and return the one with the matching hash
   110  	receipts := ReadReceipts(db, blockHash, *blockNumber, config)
   111  	for receiptIndex, receipt := range receipts {
   112  		if receipt.TxHash == hash {
   113  			return receipt, blockHash, *blockNumber, uint64(receiptIndex)
   114  		}
   115  	}
   116  	log.Error("Receipt not found", "number", blockNumber, "hash", blockHash, "txhash", hash)
   117  	return nil, common.Hash{}, 0, 0
   118  }
   119  
   120  // ReadBloomBits retrieves the compressed bloom bit vector belonging to the given
   121  // section and bit index from the.
   122  func ReadBloomBits(db ethdb.KeyValueReader, bit uint, section uint64, head common.Hash) ([]byte, error) {
   123  	return db.Get(bloomBitsKey(bit, section, head))
   124  }
   125  
   126  // WriteBloomBits stores the compressed bloom bits vector belonging to the given
   127  // section and bit index.
   128  func WriteBloomBits(db ethdb.KeyValueWriter, bit uint, section uint64, head common.Hash, bits []byte) {
   129  	if err := db.Put(bloomBitsKey(bit, section, head), bits); err != nil {
   130  		log.Crit("Failed to store bloom bits", "err", err)
   131  	}
   132  }