Skip to content

Commit 8718614

Browse files
fjlPulsarAI
andauthored
core/types: add EffectiveGasPrice in Receipt (#26713)
This change adds a struct field EffectiveGasPrice in types.Receipt. The field is present in RPC responses, but not in the Go struct, and thus can't easily be accessed via ethclient. Co-authored-by: PulsarAI <dev@pulsar-systems.fi>
1 parent 4c23fe9 commit 8718614

File tree

11 files changed

+214
-124
lines changed

11 files changed

+214
-124
lines changed

core/blockchain.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2049,7 +2049,7 @@ func (bc *BlockChain) recoverAncestors(block *types.Block) (common.Hash, error)
20492049
// the processing of a block. These logs are later announced as deleted or reborn.
20502050
func (bc *BlockChain) collectLogs(b *types.Block, removed bool) []*types.Log {
20512051
receipts := rawdb.ReadRawReceipts(bc.db, b.Hash(), b.NumberU64())
2052-
receipts.DeriveFields(bc.chainConfig, b.Hash(), b.NumberU64(), b.Transactions())
2052+
receipts.DeriveFields(bc.chainConfig, b.Hash(), b.NumberU64(), b.BaseFee(), b.Transactions())
20532053

20542054
var logs []*types.Log
20552055
for _, receipt := range receipts {

core/rawdb/accessors_chain.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,14 @@ func ReadReceipts(db ethdb.Reader, hash common.Hash, number uint64, config *para
636636
log.Error("Missing body but have receipt", "hash", hash, "number", number)
637637
return nil
638638
}
639-
if err := receipts.DeriveFields(config, hash, number, body.Transactions); err != nil {
639+
header := ReadHeader(db, hash, number)
640+
var baseFee *big.Int
641+
if header == nil {
642+
baseFee = big.NewInt(0)
643+
} else {
644+
baseFee = header.BaseFee
645+
}
646+
if err := receipts.DeriveFields(config, hash, number, baseFee, body.Transactions); err != nil {
640647
log.Error("Failed to derive block receipts fields", "hash", hash, "number", number, "err", err)
641648
return nil
642649
}

core/types/gen_receipt_json.go

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/types/receipt.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ type Receipt struct {
5959
Logs []*Log `json:"logs" gencodec:"required"`
6060

6161
// Implementation fields: These fields are added by geth when processing a transaction.
62-
// They are stored in the chain database.
63-
TxHash common.Hash `json:"transactionHash" gencodec:"required"`
64-
ContractAddress common.Address `json:"contractAddress"`
65-
GasUsed uint64 `json:"gasUsed" gencodec:"required"`
62+
TxHash common.Hash `json:"transactionHash" gencodec:"required"`
63+
ContractAddress common.Address `json:"contractAddress"`
64+
GasUsed uint64 `json:"gasUsed" gencodec:"required"`
65+
EffectiveGasPrice *big.Int `json:"effectiveGasPrice"`
6666

6767
// Inclusion information: These fields provide information about the inclusion of the
6868
// transaction corresponding to this receipt.
@@ -313,7 +313,7 @@ func (rs Receipts) EncodeIndex(i int, w *bytes.Buffer) {
313313

314314
// DeriveFields fills the receipts with their computed fields based on consensus
315315
// data and contextual infos like containing block and transactions.
316-
func (rs Receipts) DeriveFields(config *params.ChainConfig, hash common.Hash, number uint64, txs Transactions) error {
316+
func (rs Receipts) DeriveFields(config *params.ChainConfig, hash common.Hash, number uint64, baseFee *big.Int, txs []*Transaction) error {
317317
signer := MakeSigner(config, new(big.Int).SetUint64(number))
318318

319319
logIndex := uint(0)
@@ -325,6 +325,8 @@ func (rs Receipts) DeriveFields(config *params.ChainConfig, hash common.Hash, nu
325325
rs[i].Type = txs[i].Type()
326326
rs[i].TxHash = txs[i].Hash()
327327

328+
rs[i].EffectiveGasPrice = txs[i].inner.effectiveGasPrice(new(big.Int), baseFee)
329+
328330
// block location fields
329331
rs[i].BlockHash = hash
330332
rs[i].BlockNumber = new(big.Int).SetUint64(number)
@@ -335,13 +337,17 @@ func (rs Receipts) DeriveFields(config *params.ChainConfig, hash common.Hash, nu
335337
// Deriving the signer is expensive, only do if it's actually needed
336338
from, _ := Sender(signer, txs[i])
337339
rs[i].ContractAddress = crypto.CreateAddress(from, txs[i].Nonce())
340+
} else {
341+
rs[i].ContractAddress = common.Address{}
338342
}
343+
339344
// The used gas can be calculated based on previous r
340345
if i == 0 {
341346
rs[i].GasUsed = rs[i].CumulativeGasUsed
342347
} else {
343348
rs[i].GasUsed = rs[i].CumulativeGasUsed - rs[i-1].CumulativeGasUsed
344349
}
350+
345351
// The derived log fields can simply be set from the block and transaction
346352
for j := 0; j < len(rs[i].Logs); j++ {
347353
rs[i].Logs[j].BlockNumber = number

0 commit comments

Comments
 (0)