@@ -19,12 +19,15 @@ package rawdb
1919import (
2020 "bytes"
2121 "encoding/binary"
22+ "errors"
2223 "math/big"
2324
2425 "github.com/ethereum/go-ethereum/common"
2526 "github.com/ethereum/go-ethereum/core/types"
27+ "github.com/ethereum/go-ethereum/crypto"
2628 "github.com/ethereum/go-ethereum/ethdb"
2729 "github.com/ethereum/go-ethereum/log"
30+ "github.com/ethereum/go-ethereum/params"
2831 "github.com/ethereum/go-ethereum/rlp"
2932)
3033
@@ -299,39 +302,112 @@ func ReadReceiptsRLP(db ethdb.Reader, hash common.Hash, number uint64) rlp.RawVa
299302 return data
300303}
301304
302- // ReadReceipts retrieves all the transaction receipts belonging to a block.
303- func ReadReceipts (db ethdb.Reader , hash common.Hash , number uint64 ) types.Receipts {
305+ // ReadRawReceipts retrieves all the transaction receipts belonging to a block.
306+ // The receipt metadata fields are not guaranteed to be populated, so they
307+ // should not be used. Use ReadReceipts instead if the metadata is needed.
308+ func ReadRawReceipts (db ethdb.Reader , hash common.Hash , number uint64 ) types.Receipts {
304309 // Retrieve the flattened receipt slice
305310 data := ReadReceiptsRLP (db , hash , number )
306311 if len (data ) == 0 {
307312 return nil
308313 }
314+
309315 // Convert the receipts from their storage form to their internal representation
310316 storageReceipts := []* types.ReceiptForStorage {}
311317 if err := rlp .DecodeBytes (data , & storageReceipts ); err != nil {
312318 log .Error ("Invalid receipt array RLP" , "hash" , hash , "err" , err )
313319 return nil
314320 }
315- receipts := make (types.Receipts , len (storageReceipts ))
316- logIndex := uint (0 )
317- for i , receipt := range storageReceipts {
318- // Assemble deriving fields for log.
319- for _ , log := range receipt .Logs {
320- log .TxHash = receipt .TxHash
321- log .BlockHash = hash
322- log .BlockNumber = number
323- log .TxIndex = uint (i )
324- log .Index = logIndex
325- logIndex += 1
326- }
327- receipts [i ] = (* types .Receipt )(receipt )
328- receipts [i ].BlockHash = hash
329- receipts [i ].BlockNumber = big .NewInt (0 ).SetUint64 (number )
330- receipts [i ].TransactionIndex = uint (i )
321+
322+ var receipts types.Receipts
323+ for _ , storageReceipt := range storageReceipts {
324+ receipt := (* types .Receipt )(storageReceipt )
325+ receipts = append (receipts , receipt )
331326 }
327+
332328 return receipts
333329}
334330
331+ // ReadReceipts retrieves all the transaction receipts belonging to a block, including
332+ // its correspoinding metadata fields. If it is unable to populate these metadata
333+ // fields then nil is returned.
334+ //
335+ // The current implementation populates these metadata fields by reading the receipts'
336+ // corresponding block body, so if the block body is not found it will return nil even
337+ // if the receipt itself is stored.
338+ func ReadReceipts (db ethdb.Reader , hash common.Hash , number uint64 ) types.Receipts {
339+ receipts := ReadRawReceipts (db , hash , number )
340+ if receipts == nil {
341+ return receipts
342+ }
343+
344+ // Retrieve the block body to populate missing fields for receipts and logs
345+ body := ReadBody (db , hash , number )
346+ if body == nil {
347+ log .Error ("Missing body but have receipt" , "hash" , hash , "number" , number )
348+ return nil
349+ }
350+
351+ genesisHash := ReadCanonicalHash (db , 0 )
352+ if genesisHash == (common.Hash {}) {
353+ log .Error ("Missing genesis hash" )
354+ return nil
355+ }
356+
357+ config := ReadChainConfig (db , genesisHash )
358+ if config == nil {
359+ log .Error ("Missing chain config " , "hash" , hash )
360+ return nil
361+ }
362+
363+ SetReceiptsData (config , hash , big .NewInt (int64 (number )), body , receipts )
364+
365+ return receipts
366+ }
367+
368+ // SetReceiptsData computes all the non-consensus fields of the receipts
369+ func SetReceiptsData (config * params.ChainConfig , blockHash common.Hash , blockNumber * big.Int , body * types.Body , receipts types.Receipts ) error {
370+ signer := types .MakeSigner (config , blockNumber )
371+
372+ transactions , logIndex := body .Transactions , uint (0 )
373+ if len (transactions ) != len (receipts ) {
374+ return errors .New ("transaction and receipt count mismatch" )
375+ }
376+
377+ for j := 0 ; j < len (receipts ); j ++ {
378+ // The transaction hash can be retrieved from the transaction itself
379+ receipts [j ].TxHash = transactions [j ].Hash ()
380+
381+ // block location fields
382+ receipts [j ].BlockHash = blockHash
383+ receipts [j ].BlockNumber = blockNumber
384+ receipts [j ].TransactionIndex = uint (j )
385+
386+ // The contract address can be derived from the transaction itself
387+ if transactions [j ].To () == nil {
388+ // Deriving the signer is expensive, only do if it's actually needed
389+ from , _ := types .Sender (signer , transactions [j ])
390+ receipts [j ].ContractAddress = crypto .CreateAddress (from , transactions [j ].Nonce ())
391+ }
392+ // The used gas can be calculated based on previous receipts
393+ if j == 0 {
394+ receipts [j ].GasUsed = receipts [j ].CumulativeGasUsed
395+ } else {
396+ receipts [j ].GasUsed = receipts [j ].CumulativeGasUsed - receipts [j - 1 ].CumulativeGasUsed
397+ }
398+ // The derived log fields can simply be set from the block and transaction
399+ for k := 0 ; k < len (receipts [j ].Logs ); k ++ {
400+ receipts [j ].Logs [k ].BlockNumber = blockNumber .Uint64 ()
401+ receipts [j ].Logs [k ].BlockHash = blockHash
402+ receipts [j ].Logs [k ].TxHash = receipts [j ].TxHash
403+ receipts [j ].Logs [k ].TxIndex = uint (j )
404+ receipts [j ].Logs [k ].Index = logIndex
405+ logIndex ++
406+ }
407+ }
408+ return nil
409+ }
410+
335411// WriteReceipts stores all the transaction receipts belonging to a block.
336412func WriteReceipts (db ethdb.Writer , hash common.Hash , number uint64 , receipts types.Receipts ) {
337413 // Convert the receipts into their storage form and serialize them
0 commit comments