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