Skip to content

Commit

Permalink
fix(cannon): Calculate gasPrice for type 2/3 transactions seperately (#…
Browse files Browse the repository at this point in the history
…440)

* fix(cannon): Calculate gasPrice for type 2/3 transactions seperately

* fix(cannon): Calculate gasPrice for type 2/3 transactions seperately
  • Loading branch information
samcm authored Feb 17, 2025
1 parent cee4152 commit 08d1f5c
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions pkg/cannon/deriver/beacon/eth/v2/execution_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/hex"
"fmt"
"math/big"
"strconv"
"time"

Expand Down Expand Up @@ -279,7 +280,11 @@ func (b *ExecutionTransactionDeriver) processSlot(ctx context.Context, slot phas
return nil, fmt.Errorf("failed to get transaction sender: %v", err)
}

gasPrice := transaction.GasPrice()
gasPrice, err := GetGasPrice(block, transaction)
if err != nil {
return nil, fmt.Errorf("failed to get transaction gas price: %v", err)
}

if gasPrice == nil {
return nil, fmt.Errorf("failed to get transaction gas price")
}
Expand All @@ -303,7 +308,7 @@ func (b *ExecutionTransactionDeriver) processSlot(ctx context.Context, slot phas
tx := &xatuethv1.Transaction{
Nonce: wrapperspb.UInt64(transaction.Nonce()),
Gas: wrapperspb.UInt64(transaction.Gas()),
GasPrice: transaction.GasPrice().String(),
GasPrice: gasPrice.String(),
GasTipCap: transaction.GasTipCap().String(),
GasFeeCap: transaction.GasFeeCap().String(),
To: to,
Expand Down Expand Up @@ -410,3 +415,36 @@ func (b *ExecutionTransactionDeriver) createEvent(ctx context.Context, transacti

return decoratedEvent, nil
}

func GetGasPrice(block *spec.VersionedSignedBeaconBlock, transaction *types.Transaction) (*big.Int, error) {
if transaction.Type() == 0 || transaction.Type() == 1 {
return transaction.GasPrice(), nil
}

if transaction.Type() == 2 || transaction.Type() == 3 { // EIP-1559 or Blob transactions
baseFee := new(big.Int)

switch block.Version {
case spec.DataVersionBellatrix:
baseFee = new(big.Int).SetBytes(block.Bellatrix.Message.Body.ExecutionPayload.BaseFeePerGas[:])
case spec.DataVersionDeneb:
executionPayload := block.Deneb.Message.Body.ExecutionPayload
baseFee.SetBytes(executionPayload.BaseFeePerGas.Bytes())
case spec.DataVersionElectra:
executionPayload := block.Electra.Message.Body.ExecutionPayload
baseFee.SetBytes(executionPayload.BaseFeePerGas.Bytes())
default:
return nil, fmt.Errorf("unknown block version: %d", block.Version)
}

// Calculate Effective Gas Price: min(max_fee_per_gas, base_fee + max_priority_fee_per_gas)
gasPrice := new(big.Int).Add(baseFee, transaction.GasTipCap())
if gasPrice.Cmp(transaction.GasFeeCap()) > 0 {
gasPrice = transaction.GasFeeCap()
}

return gasPrice, nil
}

return nil, fmt.Errorf("unknown transaction type: %d", transaction.Type())
}

0 comments on commit 08d1f5c

Please sign in to comment.