Skip to content

Commit

Permalink
Fixed the issue where gas price or 1559 is equal to 0 but estimateGas…
Browse files Browse the repository at this point in the history
… is wrong
  • Loading branch information
Ivan Wang committed Mar 22, 2024
1 parent d75477f commit e221687
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
7 changes: 7 additions & 0 deletions core/txpool/txpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,13 @@ func (pool *TxPool) validateTx(tx *types.Transaction, local bool) error {
return core.ErrInsufficientGasForL1Cost
}
} else if tx.Type() == types.DynamicFeeTxType {
// When feecap is smaller than basefee, submission is meaningless.
// Report an error quickly instead of getting stuck in txpool.
if tx.GasFeeCap().Cmp(baseFee) < 0 { // Consistent with legacy tx verification
return fmt.Errorf("%w: address %v, maxFeePerGas: %s baseFee: %s", core.ErrFeeCapTooLow,
from.Hex(), tx.GasFeeCap(), baseFee)
}

// dynamicBaseFeeTxL1Cost gas used to cover L1 Cost for dynamic fee tx
effectiveGas := cmath.BigMin(new(big.Int).Add(tx.GasTipCap(), baseFee), tx.GasFeeCap())
dynamicFeeTxL1Cost := new(big.Int).Mul(effectiveGas, gasRemaining)
Expand Down
4 changes: 3 additions & 1 deletion internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1252,7 +1252,9 @@ func DoEstimateGas(ctx context.Context, b Backend, args TransactionArgs, blockNr
}

runMode := core.GasEstimationMode
if args.GasPrice == nil && args.MaxFeePerGas == nil && args.MaxPriorityFeePerGas == nil {
if (args.GasPrice == nil || args.GasPrice.ToInt().Sign() == 0) && // GasPrice is nil or zero AND
(args.MaxFeePerGas == nil || args.MaxFeePerGas.ToInt().Sign() == 0) && // MaxFeePerGas is nil or zero AND
(args.MaxPriorityFeePerGas == nil || args.MaxPriorityFeePerGas.ToInt().Sign() == 0) { // MaxPriorityFeePerGas is nil or zero
runMode = core.GasEstimationWithSkipCheckBalanceMode
}

Expand Down
2 changes: 1 addition & 1 deletion internal/ethapi/transaction_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func (args *TransactionArgs) ToMessage(globalGasCap uint64, baseFee *big.Int, ru
// use suggested gasPrice for estimateGas to calculate gasUsed
if runMode == core.GasEstimationMode || runMode == core.GasEstimationWithSkipCheckBalanceMode {
// use default gasPrice if user does not set gasPrice or gasPrice is 0
if args.GasPrice == nil && gasPrice.Cmp(common.Big0) == 0 {
if args.GasPrice == nil || gasPrice.Cmp(common.Big0) == 0 {
gasPrice = gasPriceForEstimate.ToInt()
}
// use gasTipCap to set gasFeeCap
Expand Down

0 comments on commit e221687

Please sign in to comment.