From fba01dfbfd69c97b3eb358bad9cd401f550482d0 Mon Sep 17 00:00:00 2001 From: Egor Lysenko Date: Tue, 16 Nov 2021 11:15:46 +0700 Subject: [PATCH] Receipts.DeriveFields accepts Signer instead of ChainConfig --- core/rawdb/accessors_chain.go | 2 +- core/types/receipt.go | 7 ++----- core/types/receipt_test.go | 2 +- light/odr_util.go | 2 +- 4 files changed, 5 insertions(+), 8 deletions(-) diff --git a/core/rawdb/accessors_chain.go b/core/rawdb/accessors_chain.go index 76132bf37e68..862c2974fff9 100644 --- a/core/rawdb/accessors_chain.go +++ b/core/rawdb/accessors_chain.go @@ -600,7 +600,7 @@ func ReadReceipts(db ethdb.Reader, hash common.Hash, number uint64, config *para log.Error("Missing body but have receipt", "hash", hash, "number", number) return nil } - if err := receipts.DeriveFields(config, hash, number, body.Transactions); err != nil { + if err := receipts.DeriveFields(types.MakeSigner(config, new(big.Int).SetUint64(number)), hash, number, body.Transactions); err != nil { log.Error("Failed to derive block receipts fields", "hash", hash, "number", number, "err", err) return nil } diff --git a/core/types/receipt.go b/core/types/receipt.go index 6141740f2f13..d856f026fa95 100644 --- a/core/types/receipt.go +++ b/core/types/receipt.go @@ -27,7 +27,6 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/crypto" - "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rlp" ) @@ -354,9 +353,7 @@ func (rs Receipts) EncodeIndex(i int, w *bytes.Buffer) { // DeriveFields fills the receipts with their computed fields based on consensus // data and contextual infos like containing block and transactions. -func (r Receipts) DeriveFields(config *params.ChainConfig, hash common.Hash, number uint64, txs Transactions) error { - signer := MakeSigner(config, new(big.Int).SetUint64(number)) - +func (r Receipts) DeriveFields(signer Signer, hash common.Hash, number uint64, txs Transactions) error { logIndex := uint(0) if len(txs) != len(r) { return errors.New("transaction and receipt count mismatch") @@ -372,7 +369,7 @@ func (r Receipts) DeriveFields(config *params.ChainConfig, hash common.Hash, num r[i].TransactionIndex = uint(i) // The contract address can be derived from the transaction itself - if txs[i].To() == nil { + if signer != nil && txs[i].To() == nil { // Deriving the signer is expensive, only do if it's actually needed from, _ := Sender(signer, txs[i]) r[i].ContractAddress = crypto.CreateAddress(from, txs[i].Nonce()) diff --git a/core/types/receipt_test.go b/core/types/receipt_test.go index 22a316c2374b..b2e0e0c56ef8 100644 --- a/core/types/receipt_test.go +++ b/core/types/receipt_test.go @@ -229,7 +229,7 @@ func TestDeriveFields(t *testing.T) { hash := common.BytesToHash([]byte{0x03, 0x14}) clearComputedFieldsOnReceipts(t, receipts) - if err := receipts.DeriveFields(params.TestChainConfig, hash, number.Uint64(), txs); err != nil { + if err := receipts.DeriveFields(MakeSigner(params.TestChainConfig, number), hash, number.Uint64(), txs); err != nil { t.Fatalf("DeriveFields(...) = %v, want ", err) } // Iterate over all the computed fields and check that they're correct diff --git a/light/odr_util.go b/light/odr_util.go index bbbcdbce2135..6bca5c695ad8 100644 --- a/light/odr_util.go +++ b/light/odr_util.go @@ -175,7 +175,7 @@ func GetBlockReceipts(ctx context.Context, odr OdrBackend, hash common.Hash, num genesis := rawdb.ReadCanonicalHash(odr.Database(), 0) config := rawdb.ReadChainConfig(odr.Database(), genesis) - if err := receipts.DeriveFields(config, block.Hash(), block.NumberU64(), block.Transactions()); err != nil { + if err := receipts.DeriveFields(types.MakeSigner(config, new(big.Int).SetUint64(number)), block.Hash(), block.NumberU64(), block.Transactions()); err != nil { return nil, err } rawdb.WriteReceipts(odr.Database(), hash, number, receipts)