github.com/ethereum-optimism/optimism/l2geth@v0.0.0-20230612200230-50b04ade19e3/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-optimism/optimism/l2geth/common" 23 "github.com/ethereum-optimism/optimism/l2geth/core/types" 24 "github.com/ethereum-optimism/optimism/l2geth/ethdb" 25 "github.com/ethereum-optimism/optimism/l2geth/log" 26 "github.com/ethereum-optimism/optimism/l2geth/params" 27 "github.com/ethereum-optimism/optimism/l2geth/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 // UsingOVM 90 // Read the transaction meta from the database and attach it 91 // to the transaction. Since there is 1 transaction per block, the 92 // blocknumber is used as the key. 93 txMeta := ReadTransactionMeta(db, *blockNumber) 94 if txMeta != nil { 95 tx.SetTransactionMeta(txMeta) 96 } 97 98 return tx, blockHash, *blockNumber, uint64(txIndex) 99 } 100 } 101 log.Error("Transaction not found", "number", blockNumber, "hash", blockHash, "txhash", hash) 102 return nil, common.Hash{}, 0, 0 103 } 104 105 // ReadReceipt retrieves a specific transaction receipt from the database, along with 106 // its added positional metadata. 107 func ReadReceipt(db ethdb.Reader, hash common.Hash, config *params.ChainConfig) (*types.Receipt, common.Hash, uint64, uint64) { 108 // Retrieve the context of the receipt based on the transaction hash 109 blockNumber := ReadTxLookupEntry(db, hash) 110 if blockNumber == nil { 111 return nil, common.Hash{}, 0, 0 112 } 113 blockHash := ReadCanonicalHash(db, *blockNumber) 114 if blockHash == (common.Hash{}) { 115 return nil, common.Hash{}, 0, 0 116 } 117 // Read all the receipts from the block and return the one with the matching hash 118 receipts := ReadReceipts(db, blockHash, *blockNumber, config) 119 for receiptIndex, receipt := range receipts { 120 if receipt.TxHash == hash { 121 return receipt, blockHash, *blockNumber, uint64(receiptIndex) 122 } 123 } 124 log.Error("Receipt not found", "number", blockNumber, "hash", blockHash, "txhash", hash) 125 return nil, common.Hash{}, 0, 0 126 } 127 128 // ReadBloomBits retrieves the compressed bloom bit vector belonging to the given 129 // section and bit index from the. 130 func ReadBloomBits(db ethdb.KeyValueReader, bit uint, section uint64, head common.Hash) ([]byte, error) { 131 return db.Get(bloomBitsKey(bit, section, head)) 132 } 133 134 // WriteBloomBits stores the compressed bloom bits vector belonging to the given 135 // section and bit index. 136 func WriteBloomBits(db ethdb.KeyValueWriter, bit uint, section uint64, head common.Hash, bits []byte) { 137 if err := db.Put(bloomBitsKey(bit, section, head), bits); err != nil { 138 log.Crit("Failed to store bloom bits", "err", err) 139 } 140 }