Skip to content

Commit 721b87d

Browse files
authored
Merge pull request ethereum#22 from uprendis/feature/optimize-receipts-deriving
Receipts.DeriveFields accepts Signer instead of ChainConfig
2 parents 4ea903e + fba01df commit 721b87d

File tree

4 files changed

+5
-8
lines changed

4 files changed

+5
-8
lines changed

core/rawdb/accessors_chain.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ func ReadReceipts(db ethdb.Reader, hash common.Hash, number uint64, config *para
600600
log.Error("Missing body but have receipt", "hash", hash, "number", number)
601601
return nil
602602
}
603-
if err := receipts.DeriveFields(config, hash, number, body.Transactions); err != nil {
603+
if err := receipts.DeriveFields(types.MakeSigner(config, new(big.Int).SetUint64(number)), hash, number, body.Transactions); err != nil {
604604
log.Error("Failed to derive block receipts fields", "hash", hash, "number", number, "err", err)
605605
return nil
606606
}

core/types/receipt.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"github.com/ethereum/go-ethereum/common"
2828
"github.com/ethereum/go-ethereum/common/hexutil"
2929
"github.com/ethereum/go-ethereum/crypto"
30-
"github.com/ethereum/go-ethereum/params"
3130
"github.com/ethereum/go-ethereum/rlp"
3231
)
3332

@@ -354,9 +353,7 @@ func (rs Receipts) EncodeIndex(i int, w *bytes.Buffer) {
354353

355354
// DeriveFields fills the receipts with their computed fields based on consensus
356355
// data and contextual infos like containing block and transactions.
357-
func (r Receipts) DeriveFields(config *params.ChainConfig, hash common.Hash, number uint64, txs Transactions) error {
358-
signer := MakeSigner(config, new(big.Int).SetUint64(number))
359-
356+
func (r Receipts) DeriveFields(signer Signer, hash common.Hash, number uint64, txs Transactions) error {
360357
logIndex := uint(0)
361358
if len(txs) != len(r) {
362359
return errors.New("transaction and receipt count mismatch")
@@ -372,7 +369,7 @@ func (r Receipts) DeriveFields(config *params.ChainConfig, hash common.Hash, num
372369
r[i].TransactionIndex = uint(i)
373370

374371
// The contract address can be derived from the transaction itself
375-
if txs[i].To() == nil {
372+
if signer != nil && txs[i].To() == nil {
376373
// Deriving the signer is expensive, only do if it's actually needed
377374
from, _ := Sender(signer, txs[i])
378375
r[i].ContractAddress = crypto.CreateAddress(from, txs[i].Nonce())

core/types/receipt_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ func TestDeriveFields(t *testing.T) {
229229
hash := common.BytesToHash([]byte{0x03, 0x14})
230230

231231
clearComputedFieldsOnReceipts(t, receipts)
232-
if err := receipts.DeriveFields(params.TestChainConfig, hash, number.Uint64(), txs); err != nil {
232+
if err := receipts.DeriveFields(MakeSigner(params.TestChainConfig, number), hash, number.Uint64(), txs); err != nil {
233233
t.Fatalf("DeriveFields(...) = %v, want <nil>", err)
234234
}
235235
// Iterate over all the computed fields and check that they're correct

light/odr_util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func GetBlockReceipts(ctx context.Context, odr OdrBackend, hash common.Hash, num
175175
genesis := rawdb.ReadCanonicalHash(odr.Database(), 0)
176176
config := rawdb.ReadChainConfig(odr.Database(), genesis)
177177

178-
if err := receipts.DeriveFields(config, block.Hash(), block.NumberU64(), block.Transactions()); err != nil {
178+
if err := receipts.DeriveFields(types.MakeSigner(config, new(big.Int).SetUint64(number)), block.Hash(), block.NumberU64(), block.Transactions()); err != nil {
179179
return nil, err
180180
}
181181
rawdb.WriteReceipts(odr.Database(), hash, number, receipts)

0 commit comments

Comments
 (0)