Skip to content

Commit

Permalink
Merge pull request ethereum#22 from uprendis/feature/optimize-receipt…
Browse files Browse the repository at this point in the history
…s-deriving

Receipts.DeriveFields accepts Signer instead of ChainConfig
  • Loading branch information
quan8 authored Nov 17, 2021
2 parents 4ea903e + fba01df commit 721b87d
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 8 deletions.
2 changes: 1 addition & 1 deletion core/rawdb/accessors_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
7 changes: 2 additions & 5 deletions core/types/receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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")
Expand All @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion core/types/receipt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <nil>", err)
}
// Iterate over all the computed fields and check that they're correct
Expand Down
2 changes: 1 addition & 1 deletion light/odr_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 721b87d

Please sign in to comment.