github.com/mapprotocol/go-ethereum@v1.9.7/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/ethereum/go-ethereum/common" 23 "github.com/ethereum/go-ethereum/core/types" 24 "github.com/ethereum/go-ethereum/ethdb" 25 "github.com/ethereum/go-ethereum/log" 26 "github.com/ethereum/go-ethereum/params" 27 "github.com/ethereum/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 // DeleteTxLookupEntry removes all transaction data associated with a hash. 67 func DeleteTxLookupEntry(db ethdb.KeyValueWriter, hash common.Hash) { 68 db.Delete(txLookupKey(hash)) 69 } 70 71 // ReadTransaction retrieves a specific transaction from the database, along with 72 // its added positional metadata. 73 func ReadTransaction(db ethdb.Reader, hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64) { 74 blockNumber := ReadTxLookupEntry(db, hash) 75 if blockNumber == nil { 76 return nil, common.Hash{}, 0, 0 77 } 78 blockHash := ReadCanonicalHash(db, *blockNumber) 79 if blockHash == (common.Hash{}) { 80 return nil, common.Hash{}, 0, 0 81 } 82 body := ReadBody(db, blockHash, *blockNumber) 83 if body == nil { 84 log.Error("Transaction referenced missing", "number", blockNumber, "hash", blockHash) 85 return nil, common.Hash{}, 0, 0 86 } 87 for txIndex, tx := range body.Transactions { 88 if tx.Hash() == hash { 89 return tx, blockHash, *blockNumber, uint64(txIndex) 90 } 91 } 92 log.Error("Transaction not found", "number", blockNumber, "hash", blockHash, "txhash", hash) 93 return nil, common.Hash{}, 0, 0 94 } 95 96 // ReadReceipt retrieves a specific transaction receipt from the database, along with 97 // its added positional metadata. 98 func ReadReceipt(db ethdb.Reader, hash common.Hash, config *params.ChainConfig) (*types.Receipt, common.Hash, uint64, uint64) { 99 // Retrieve the context of the receipt based on the transaction hash 100 blockNumber := ReadTxLookupEntry(db, hash) 101 if blockNumber == nil { 102 return nil, common.Hash{}, 0, 0 103 } 104 blockHash := ReadCanonicalHash(db, *blockNumber) 105 if blockHash == (common.Hash{}) { 106 return nil, common.Hash{}, 0, 0 107 } 108 // Read all the receipts from the block and return the one with the matching hash 109 receipts := ReadReceipts(db, blockHash, *blockNumber, config) 110 for receiptIndex, receipt := range receipts { 111 if receipt.TxHash == hash { 112 return receipt, blockHash, *blockNumber, uint64(receiptIndex) 113 } 114 } 115 log.Error("Receipt not found", "number", blockNumber, "hash", blockHash, "txhash", hash) 116 return nil, common.Hash{}, 0, 0 117 } 118 119 // ReadBloomBits retrieves the compressed bloom bit vector belonging to the given 120 // section and bit index from the. 121 func ReadBloomBits(db ethdb.KeyValueReader, bit uint, section uint64, head common.Hash) ([]byte, error) { 122 return db.Get(bloomBitsKey(bit, section, head)) 123 } 124 125 // WriteBloomBits stores the compressed bloom bits vector belonging to the given 126 // section and bit index. 127 func WriteBloomBits(db ethdb.KeyValueWriter, bit uint, section uint64, head common.Hash, bits []byte) { 128 if err := db.Put(bloomBitsKey(bit, section, head), bits); err != nil { 129 log.Crit("Failed to store bloom bits", "err", err) 130 } 131 }