@@ -25,33 +25,28 @@ import (
2525
2626// ReadTxLookupEntry retrieves the positional metadata associated with a transaction
2727// hash to allow retrieving the transaction or receipt by hash.
28- func ReadTxLookupEntry (db DatabaseReader , hash common.Hash ) ( common.Hash , uint64 , uint64 ) {
28+ func ReadTxLookupEntry (db DatabaseReader , hash common.Hash ) common.Hash {
2929 data , _ := db .Get (txLookupKey (hash ))
3030 if len (data ) == 0 {
31- return common.Hash {}, 0 , 0
31+ return common.Hash {}
3232 }
33- var entry TxLookupEntry
33+ if len (data ) == common .HashLength {
34+ return common .BytesToHash (data )
35+ }
36+ // Probably it's legacy txlookup entry data, try to decode it.
37+ var entry LegacyTxLookupEntry
3438 if err := rlp .DecodeBytes (data , & entry ); err != nil {
35- log .Error ("Invalid transaction lookup entry RLP" , "hash" , hash , "err" , err )
36- return common.Hash {}, 0 , 0
39+ log .Error ("Invalid transaction lookup entry RLP" , "hash" , hash , "blob" , data , " err" , err )
40+ return common.Hash {}
3741 }
38- return entry .BlockHash , entry . BlockIndex , entry . Index
42+ return entry .BlockHash
3943}
4044
4145// WriteTxLookupEntries stores a positional metadata for every transaction from
4246// a block, enabling hash based transaction and receipt lookups.
4347func WriteTxLookupEntries (db DatabaseWriter , block * types.Block ) {
44- for i , tx := range block .Transactions () {
45- entry := TxLookupEntry {
46- BlockHash : block .Hash (),
47- BlockIndex : block .NumberU64 (),
48- Index : uint64 (i ),
49- }
50- data , err := rlp .EncodeToBytes (entry )
51- if err != nil {
52- log .Crit ("Failed to encode transaction lookup entry" , "err" , err )
53- }
54- if err := db .Put (txLookupKey (tx .Hash ()), data ); err != nil {
48+ for _ , tx := range block .Transactions () {
49+ if err := db .Put (txLookupKey (tx .Hash ()), block .Hash ().Bytes ()); err != nil {
5550 log .Crit ("Failed to store transaction lookup entry" , "err" , err )
5651 }
5752 }
@@ -65,31 +60,47 @@ func DeleteTxLookupEntry(db DatabaseDeleter, hash common.Hash) {
6560// ReadTransaction retrieves a specific transaction from the database, along with
6661// its added positional metadata.
6762func ReadTransaction (db DatabaseReader , hash common.Hash ) (* types.Transaction , common.Hash , uint64 , uint64 ) {
68- blockHash , blockNumber , txIndex := ReadTxLookupEntry (db , hash )
63+ blockHash := ReadTxLookupEntry (db , hash )
6964 if blockHash == (common.Hash {}) {
7065 return nil , common.Hash {}, 0 , 0
7166 }
72- body := ReadBody (db , blockHash , blockNumber )
73- if body == nil || len (body .Transactions ) <= int (txIndex ) {
74- log .Error ("Transaction referenced missing" , "number" , blockNumber , "hash" , blockHash , "index" , txIndex )
67+ blockNumber := ReadHeaderNumber (db , blockHash )
68+ if blockNumber == nil {
69+ return nil , common.Hash {}, 0 , 0
70+ }
71+ body := ReadBody (db , blockHash , * blockNumber )
72+ if body == nil {
73+ log .Error ("Transaction referenced missing" , "number" , blockNumber , "hash" , blockHash )
7574 return nil , common.Hash {}, 0 , 0
7675 }
77- return body .Transactions [txIndex ], blockHash , blockNumber , txIndex
76+ for txIndex , tx := range body .Transactions {
77+ if tx .Hash () == hash {
78+ return tx , blockHash , * blockNumber , uint64 (txIndex )
79+ }
80+ }
81+ log .Error ("Transaction not found" , "number" , blockNumber , "hash" , blockHash , "txhash" , hash )
82+ return nil , common.Hash {}, 0 , 0
7883}
7984
8085// ReadReceipt retrieves a specific transaction receipt from the database, along with
8186// its added positional metadata.
8287func ReadReceipt (db DatabaseReader , hash common.Hash ) (* types.Receipt , common.Hash , uint64 , uint64 ) {
83- blockHash , blockNumber , receiptIndex := ReadTxLookupEntry (db , hash )
88+ blockHash := ReadTxLookupEntry (db , hash )
8489 if blockHash == (common.Hash {}) {
8590 return nil , common.Hash {}, 0 , 0
8691 }
87- receipts := ReadReceipts (db , blockHash , blockNumber )
88- if len (receipts ) <= int (receiptIndex ) {
89- log .Error ("Receipt refereced missing" , "number" , blockNumber , "hash" , blockHash , "index" , receiptIndex )
92+ blockNumber := ReadHeaderNumber (db , blockHash )
93+ if blockNumber == nil {
9094 return nil , common.Hash {}, 0 , 0
9195 }
92- return receipts [receiptIndex ], blockHash , blockNumber , receiptIndex
96+ receipts := ReadReceipts (db , blockHash , * blockNumber )
97+ for receiptIndex , receipt := range receipts {
98+ if receipt .TxHash == hash {
99+ return receipt , blockHash , * blockNumber , uint64 (receiptIndex )
100+ }
101+ }
102+ log .Error ("Receipt not found" , "number" , blockNumber , "hash" , blockHash , "txhash" , hash )
103+ return nil , common.Hash {}, 0 , 0
93104}
94105
95106// ReadBloomBits retrieves the compressed bloom bit vector belonging to the given
0 commit comments