@@ -19,12 +19,10 @@ package rawdb
1919import (
2020 "bytes"
2121 "encoding/binary"
22- "errors"
2322 "math/big"
2423
2524 "github.com/ethereum/go-ethereum/common"
2625 "github.com/ethereum/go-ethereum/core/types"
27- "github.com/ethereum/go-ethereum/crypto"
2826 "github.com/ethereum/go-ethereum/ethdb"
2927 "github.com/ethereum/go-ethereum/log"
3028 "github.com/ethereum/go-ethereum/params"
@@ -311,20 +309,16 @@ func ReadRawReceipts(db ethdb.Reader, hash common.Hash, number uint64) types.Rec
311309 if len (data ) == 0 {
312310 return nil
313311 }
314-
315312 // Convert the receipts from their storage form to their internal representation
316313 storageReceipts := []* types.ReceiptForStorage {}
317314 if err := rlp .DecodeBytes (data , & storageReceipts ); err != nil {
318315 log .Error ("Invalid receipt array RLP" , "hash" , hash , "err" , err )
319316 return nil
320317 }
321-
322- var receipts types.Receipts
323- for _ , storageReceipt := range storageReceipts {
324- receipt := (* types .Receipt )(storageReceipt )
325- receipts = append (receipts , receipt )
318+ receipts := make (types.Receipts , len (storageReceipts ))
319+ for i , storageReceipt := range storageReceipts {
320+ receipts [i ] = (* types .Receipt )(storageReceipt )
326321 }
327-
328322 return receipts
329323}
330324
@@ -335,79 +329,24 @@ func ReadRawReceipts(db ethdb.Reader, hash common.Hash, number uint64) types.Rec
335329// The current implementation populates these metadata fields by reading the receipts'
336330// corresponding block body, so if the block body is not found it will return nil even
337331// if the receipt itself is stored.
338- func ReadReceipts (db ethdb.Reader , hash common.Hash , number uint64 ) types.Receipts {
332+ func ReadReceipts (db ethdb.Reader , hash common.Hash , number uint64 , config * params.ChainConfig ) types.Receipts {
333+ // We're deriving many fields from the block body, retrieve beside the receipt
339334 receipts := ReadRawReceipts (db , hash , number )
340335 if receipts == nil {
341- return receipts
336+ return nil
342337 }
343-
344- // Retrieve the block body to populate missing fields for receipts and logs
345338 body := ReadBody (db , hash , number )
346339 if body == nil {
347340 log .Error ("Missing body but have receipt" , "hash" , hash , "number" , number )
348341 return nil
349342 }
350-
351- genesisHash := ReadCanonicalHash (db , 0 )
352- if genesisHash == (common.Hash {}) {
353- log .Error ("Missing genesis hash" )
343+ if err := receipts .DeriveFields (config , hash , number , body .Transactions ); err != nil {
344+ log .Error ("Failed to derive block receipts fields" , "hash" , hash , "number" , number , "err" , err )
354345 return nil
355346 }
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-
365347 return receipts
366348}
367349
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-
411350// WriteReceipts stores all the transaction receipts belonging to a block.
412351func WriteReceipts (db ethdb.Writer , hash common.Hash , number uint64 , receipts types.Receipts ) {
413352 // Convert the receipts into their storage form and serialize them
0 commit comments