@@ -632,6 +632,37 @@ func (self *BlockChain) Rollback(chain []common.Hash) {
632632 }
633633}
634634
635+ // SetReceiptsData computes all the non-consensus fields of the receipts
636+ func SetReceiptsData (block * types.Block , receipts types.Receipts ) {
637+ transactions , logIndex := block .Transactions (), uint (0 )
638+
639+ for j := 0 ; j < len (receipts ); j ++ {
640+ // The transaction hash can be retrieved from the transaction itself
641+ receipts [j ].TxHash = transactions [j ].Hash ()
642+
643+ // The contract address can be derived from the transaction itself
644+ if MessageCreatesContract (transactions [j ]) {
645+ from , _ := transactions [j ].From ()
646+ receipts [j ].ContractAddress = crypto .CreateAddress (from , transactions [j ].Nonce ())
647+ }
648+ // The used gas can be calculated based on previous receipts
649+ if j == 0 {
650+ receipts [j ].GasUsed = new (big.Int ).Set (receipts [j ].CumulativeGasUsed )
651+ } else {
652+ receipts [j ].GasUsed = new (big.Int ).Sub (receipts [j ].CumulativeGasUsed , receipts [j - 1 ].CumulativeGasUsed )
653+ }
654+ // The derived log fields can simply be set from the block and transaction
655+ for k := 0 ; k < len (receipts [j ].Logs ); k ++ {
656+ receipts [j ].Logs [k ].BlockNumber = block .NumberU64 ()
657+ receipts [j ].Logs [k ].BlockHash = block .Hash ()
658+ receipts [j ].Logs [k ].TxHash = receipts [j ].TxHash
659+ receipts [j ].Logs [k ].TxIndex = uint (j )
660+ receipts [j ].Logs [k ].Index = logIndex
661+ logIndex ++
662+ }
663+ }
664+ }
665+
635666// InsertReceiptChain attempts to complete an already existing header chain with
636667// transaction and receipt data.
637668func (self * BlockChain ) InsertReceiptChain (blockChain types.Blocks , receiptChain []types.Receipts ) (int , error ) {
@@ -673,32 +704,7 @@ func (self *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain
673704 continue
674705 }
675706 // Compute all the non-consensus fields of the receipts
676- transactions , logIndex := block .Transactions (), uint (0 )
677- for j := 0 ; j < len (receipts ); j ++ {
678- // The transaction hash can be retrieved from the transaction itself
679- receipts [j ].TxHash = transactions [j ].Hash ()
680-
681- // The contract address can be derived from the transaction itself
682- if MessageCreatesContract (transactions [j ]) {
683- from , _ := transactions [j ].From ()
684- receipts [j ].ContractAddress = crypto .CreateAddress (from , transactions [j ].Nonce ())
685- }
686- // The used gas can be calculated based on previous receipts
687- if j == 0 {
688- receipts [j ].GasUsed = new (big.Int ).Set (receipts [j ].CumulativeGasUsed )
689- } else {
690- receipts [j ].GasUsed = new (big.Int ).Sub (receipts [j ].CumulativeGasUsed , receipts [j - 1 ].CumulativeGasUsed )
691- }
692- // The derived log fields can simply be set from the block and transaction
693- for k := 0 ; k < len (receipts [j ].Logs ); k ++ {
694- receipts [j ].Logs [k ].BlockNumber = block .NumberU64 ()
695- receipts [j ].Logs [k ].BlockHash = block .Hash ()
696- receipts [j ].Logs [k ].TxHash = receipts [j ].TxHash
697- receipts [j ].Logs [k ].TxIndex = uint (j )
698- receipts [j ].Logs [k ].Index = logIndex
699- logIndex ++
700- }
701- }
707+ SetReceiptsData (block , receipts )
702708 // Write all the data out into the database
703709 if err := WriteBody (self .chainDb , block .Hash (), block .NumberU64 (), block .Body ()); err != nil {
704710 errs [index ] = fmt .Errorf ("failed to write block body: %v" , err )
0 commit comments