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