Skip to content

Commit

Permalink
core/types: add EffectiveGasPrice in Receipt (ethereum#26713)
Browse files Browse the repository at this point in the history
  • Loading branch information
gzliudan committed Jun 19, 2024
1 parent 64ecfb2 commit 5b29314
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 19 deletions.
10 changes: 8 additions & 2 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1045,16 +1045,19 @@ func (bc *BlockChain) Rollback(chain []common.Hash) {

// SetReceiptsData computes all the non-consensus fields of the receipts
func SetReceiptsData(config *params.ChainConfig, block *types.Block, receipts types.Receipts) error {
signer := types.MakeSigner(config, block.Number())

transactions, logIndex := block.Transactions(), uint(0)
if len(transactions) != len(receipts) {
return errors.New("transaction and receipt count mismatch")
}

signer := types.MakeSigner(config, block.Number())
baseFee := block.BaseFee()

for j := 0; j < len(receipts); j++ {
// The transaction hash can be retrieved from the transaction itself
receipts[j].Type = transactions[j].Type()
receipts[j].TxHash = transactions[j].Hash()
receipts[j].EffectiveGasPrice = transactions[j].EffectiveGasPrice(new(big.Int), baseFee)

// block location fields
receipts[j].BlockHash = block.Hash()
Expand All @@ -1066,7 +1069,10 @@ func SetReceiptsData(config *params.ChainConfig, block *types.Block, receipts ty
// Deriving the signer is expensive, only do if it's actually needed
from, _ := types.Sender(signer, transactions[j])
receipts[j].ContractAddress = crypto.CreateAddress(from, transactions[j].Nonce())
} else {
receipts[j].ContractAddress = common.Address{}
}

// The used gas can be calculated based on previous receipts
if j == 0 {
receipts[j].GasUsed = receipts[j].CumulativeGasUsed
Expand Down
6 changes: 6 additions & 0 deletions core/types/gen_receipt_json.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions core/types/receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ type Receipt struct {
Logs []*Log `json:"logs" gencodec:"required"`

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

// Inclusion information: These fields provide information about the inclusion of the
// transaction corresponding to this receipt.
Expand Down
12 changes: 12 additions & 0 deletions core/types/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ type TxData interface {

rawSignatureValues() (v, r, s *big.Int)
setSignatureValues(chainID, v, r, s *big.Int)

// effectiveGasPrice computes the gas price paid by the transaction, given
// the inclusion block baseFee.
//
// Unlike other TxData methods, the returned *big.Int should be an independent
// copy of the computed value, i.e. callers are allowed to mutate the result.
// Method implementations can use 'dst' to store the result.
effectiveGasPrice(dst *big.Int, baseFee *big.Int) *big.Int
}

// EncodeRLP implements rlp.Encoder
Expand Down Expand Up @@ -412,6 +420,10 @@ func (tx *Transaction) Size() common.StorageSize {
return common.StorageSize(c)
}

func (tx *Transaction) EffectiveGasPrice(dst *big.Int, baseFee *big.Int) *big.Int {
return tx.inner.effectiveGasPrice(dst, baseFee)
}

// AsMessage returns the transaction as a core.Message.
func (tx *Transaction) AsMessage(s Signer, baseFee *big.Int, balanceFee *big.Int, number *big.Int) (Message, error) {
msg := Message{
Expand Down
4 changes: 4 additions & 0 deletions core/types/tx_access_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ func (tx *AccessListTx) value() *big.Int { return tx.Value }
func (tx *AccessListTx) nonce() uint64 { return tx.Nonce }
func (tx *AccessListTx) to() *common.Address { return tx.To }

func (tx *AccessListTx) effectiveGasPrice(dst *big.Int, baseFee *big.Int) *big.Int {
return dst.Set(tx.GasPrice)
}

func (tx *AccessListTx) rawSignatureValues() (v, r, s *big.Int) {
return tx.V, tx.R, tx.S
}
Expand Down
11 changes: 11 additions & 0 deletions core/types/tx_dynamic_fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@ func (tx *DynamicFeeTx) value() *big.Int { return tx.Value }
func (tx *DynamicFeeTx) nonce() uint64 { return tx.Nonce }
func (tx *DynamicFeeTx) to() *common.Address { return tx.To }

func (tx *DynamicFeeTx) effectiveGasPrice(dst *big.Int, baseFee *big.Int) *big.Int {
if baseFee == nil {
return dst.Set(tx.GasFeeCap)
}
tip := dst.Sub(tx.GasFeeCap, baseFee)
if tip.Cmp(tx.GasTipCap) > 0 {
tip.Set(tx.GasTipCap)
}
return tip.Add(tip, baseFee)
}

func (tx *DynamicFeeTx) rawSignatureValues() (v, r, s *big.Int) {
return tx.V, tx.R, tx.S
}
Expand Down
4 changes: 4 additions & 0 deletions core/types/tx_legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ func (tx *LegacyTx) value() *big.Int { return tx.Value }
func (tx *LegacyTx) nonce() uint64 { return tx.Nonce }
func (tx *LegacyTx) to() *common.Address { return tx.To }

func (tx *LegacyTx) effectiveGasPrice(dst *big.Int, baseFee *big.Int) *big.Int {
return dst.Set(tx.GasPrice)
}

func (tx *LegacyTx) rawSignatureValues() (v, r, s *big.Int) {
return tx.V, tx.R, tx.S
}
Expand Down
16 changes: 3 additions & 13 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2217,18 +2217,9 @@ func (s *PublicTransactionPoolAPI) GetTransactionReceipt(ctx context.Context, ha
"logs": receipt.Logs,
"logsBloom": receipt.Bloom,
"type": hexutil.Uint(tx.Type()),
"effectiveGasPrice": (*hexutil.Big)(receipt.EffectiveGasPrice),
}
// Assign the effective gas price paid
if !s.b.ChainConfig().IsEIP1559(bigblock) {
fields["effectiveGasPrice"] = hexutil.Uint64(tx.GasPrice().Uint64())
} else {
header, err := s.b.HeaderByHash(ctx, blockHash)
if err != nil {
return nil, err
}
gasPrice := new(big.Int).Add(header.BaseFee, tx.EffectiveGasTipValue(header.BaseFee))
fields["effectiveGasPrice"] = hexutil.Uint64(gasPrice.Uint64())
}

// Assign receipt status or post state.
if len(receipt.PostState) > 0 {
fields["root"] = hexutil.Bytes(receipt.PostState)
Expand Down Expand Up @@ -2262,8 +2253,7 @@ func marshalReceipt(receipt *types.Receipt, blockHash common.Hash, blockNumber u
"logs": receipt.Logs,
"logsBloom": receipt.Bloom,
"type": hexutil.Uint(tx.Type()),
// uncomment below line after EIP-1559
// TODO: "effectiveGasPrice": (*hexutil.Big)(receipt.EffectiveGasPrice),
"effectiveGasPrice": (*hexutil.Big)(receipt.EffectiveGasPrice),
}

// Assign receipt status or post state.
Expand Down

0 comments on commit 5b29314

Please sign in to comment.