@@ -35,15 +35,14 @@ import (
3535 "github.com/ethereum/go-ethereum/core/state"
3636 "github.com/ethereum/go-ethereum/core/types"
3737 "github.com/ethereum/go-ethereum/core/vm"
38- "github.com/ethereum/go-ethereum/crypto"
3938 "github.com/ethereum/go-ethereum/ethdb"
4039 "github.com/ethereum/go-ethereum/event"
4140 "github.com/ethereum/go-ethereum/log"
4241 "github.com/ethereum/go-ethereum/metrics"
4342 "github.com/ethereum/go-ethereum/params"
4443 "github.com/ethereum/go-ethereum/rlp"
4544 "github.com/ethereum/go-ethereum/trie"
46- lru "github.com/hashicorp/golang-lru"
45+ "github.com/hashicorp/golang-lru"
4746)
4847
4948var (
@@ -79,12 +78,19 @@ const (
7978
8079 // BlockChainVersion ensures that an incompatible database forces a resync from scratch.
8180 //
82- // During the process of upgrading the database version from 3 to 4,
83- // the following incompatible database changes were added.
84- // * the `BlockNumber`, `TxHash`, `TxIndex`, `BlockHash` and `Index` fields of log are deleted
85- // * the `Bloom` field of receipt is deleted
86- // * the `BlockIndex` and `TxIndex` fields of txlookup are deleted
87- BlockChainVersion uint64 = 4
81+ // Changelog:
82+ //
83+ // - Version 4
84+ // The following incompatible database changes were added:
85+ // * the `BlockNumber`, `TxHash`, `TxIndex`, `BlockHash` and `Index` fields of log are deleted
86+ // * the `Bloom` field of receipt is deleted
87+ // * the `BlockIndex` and `TxIndex` fields of txlookup are deleted
88+ // - Version 5
89+ // The following incompatible database changes were added:
90+ // * the `TxHash`, `GasCost`, and `ContractAddress` fields are no longer stored for a receipt
91+ // * the `TxHash`, `GasCost`, and `ContractAddress` fields are computed by looking up the
92+ // receipts' corresponding block
93+ BlockChainVersion uint64 = 5
8894)
8995
9096// CacheConfig contains the configuration values for the trie caching/pruning
@@ -647,7 +653,7 @@ func (bc *BlockChain) GetReceiptsByHash(hash common.Hash) types.Receipts {
647653 if number == nil {
648654 return nil
649655 }
650- receipts := rawdb .ReadReceipts (bc .db , hash , * number )
656+ receipts := rawdb .ReadReceipts (bc .db , hash , * number , bc . chainConfig )
651657 if receipts == nil {
652658 return nil
653659 }
@@ -784,49 +790,6 @@ func (bc *BlockChain) Rollback(chain []common.Hash) {
784790 }
785791}
786792
787- // SetReceiptsData computes all the non-consensus fields of the receipts
788- func SetReceiptsData (config * params.ChainConfig , block * types.Block , receipts types.Receipts ) error {
789- signer := types .MakeSigner (config , block .Number ())
790-
791- transactions , logIndex := block .Transactions (), uint (0 )
792- if len (transactions ) != len (receipts ) {
793- return errors .New ("transaction and receipt count mismatch" )
794- }
795-
796- for j := 0 ; j < len (receipts ); j ++ {
797- // The transaction hash can be retrieved from the transaction itself
798- receipts [j ].TxHash = transactions [j ].Hash ()
799-
800- // block location fields
801- receipts [j ].BlockHash = block .Hash ()
802- receipts [j ].BlockNumber = block .Number ()
803- receipts [j ].TransactionIndex = uint (j )
804-
805- // The contract address can be derived from the transaction itself
806- if transactions [j ].To () == nil {
807- // Deriving the signer is expensive, only do if it's actually needed
808- from , _ := types .Sender (signer , transactions [j ])
809- receipts [j ].ContractAddress = crypto .CreateAddress (from , transactions [j ].Nonce ())
810- }
811- // The used gas can be calculated based on previous receipts
812- if j == 0 {
813- receipts [j ].GasUsed = receipts [j ].CumulativeGasUsed
814- } else {
815- receipts [j ].GasUsed = receipts [j ].CumulativeGasUsed - receipts [j - 1 ].CumulativeGasUsed
816- }
817- // The derived log fields can simply be set from the block and transaction
818- for k := 0 ; k < len (receipts [j ].Logs ); k ++ {
819- receipts [j ].Logs [k ].BlockNumber = block .NumberU64 ()
820- receipts [j ].Logs [k ].BlockHash = block .Hash ()
821- receipts [j ].Logs [k ].TxHash = receipts [j ].TxHash
822- receipts [j ].Logs [k ].TxIndex = uint (j )
823- receipts [j ].Logs [k ].Index = logIndex
824- logIndex ++
825- }
826- }
827- return nil
828- }
829-
830793// InsertReceiptChain attempts to complete an already existing header chain with
831794// transaction and receipt data.
832795func (bc * BlockChain ) InsertReceiptChain (blockChain types.Blocks , receiptChain []types.Receipts ) (int , error ) {
@@ -865,8 +828,8 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [
865828 continue
866829 }
867830 // Compute all the non-consensus fields of the receipts
868- if err := SetReceiptsData (bc .chainConfig , block , receipts ); err != nil {
869- return i , fmt .Errorf ("failed to set receipts data: %v" , err )
831+ if err := receipts . DeriveFields (bc .chainConfig , block . Hash (), block . NumberU64 (), block . Transactions () ); err != nil {
832+ return i , fmt .Errorf ("failed to derive receipts data: %v" , err )
870833 }
871834 // Write all the data out into the database
872835 rawdb .WriteBody (batch , block .Hash (), block .NumberU64 (), block .Body ())
@@ -1488,7 +1451,7 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
14881451 if number == nil {
14891452 return
14901453 }
1491- receipts := rawdb .ReadReceipts (bc .db , hash , * number )
1454+ receipts := rawdb .ReadReceipts (bc .db , hash , * number , bc . chainConfig )
14921455 for _ , receipt := range receipts {
14931456 for _ , log := range receipt .Logs {
14941457 l := * log
0 commit comments