Skip to content

Commit

Permalink
make DefaultGasTip configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
colinlyguo committed Dec 5, 2024
1 parent a8641b2 commit d1a249e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
7 changes: 7 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,10 @@ var (
Usage: "Number of pending transactions to consider the network congested and suggest a minimum tip cap",
Value: ethconfig.Defaults.GPO.CongestedThreshold,
}
GpoDefaultGasTipFlag = cli.Int64Flag{
Name: "gpo.defaultgastip",
Usage: "Default minimum tip cap (in wei) to be used after Curie fork (EIP-1559)",
}

// Metrics flags
MetricsEnabledFlag = cli.BoolFlag{
Expand Down Expand Up @@ -1450,6 +1454,9 @@ func setGPO(ctx *cli.Context, cfg *gasprice.Config, light bool) {
if ctx.GlobalIsSet(GpoCongestionThresholdFlag.Name) {
cfg.CongestedThreshold = ctx.GlobalInt(GpoCongestionThresholdFlag.Name)
}
if ctx.GlobalIsSet(GpoDefaultGasTipFlag.Name) {
cfg.DefaultGasTip = big.NewInt(ctx.GlobalInt64(GpoDefaultGasTipFlag.Name))
}
}

func setTxPool(ctx *cli.Context, cfg *core.TxPoolConfig) {
Expand Down
25 changes: 17 additions & 8 deletions eth/gasprice/gasprice.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ var (
DefaultMaxPrice = big.NewInt(500 * params.GWei)
DefaultIgnorePrice = big.NewInt(1 * params.Wei)
DefaultBasePrice = big.NewInt(0)
// DefaultGasTip is set to 100 wei instead of 1 wei for the following reasons:
// 1. It prevents issues with very low tip values (e.g., 1 wei):
// - Transaction pools commonly prevent replacing transactions with the same tip cap.
// - Extremely low tips like 1 wei may fail to increase due to rounding in integer arithmetic of SDK implementations (e.g. 1 wei * 1.5 = 1 wei).
// 2. The cost of gas tip 100 wei is negligible compared to base fee normally.
DefaultGasTip = big.NewInt(100) // Default minimum tip cap in wei (used after Curie/EIP-1559).
)

type Config struct {
Expand All @@ -52,6 +58,7 @@ type Config struct {
IgnorePrice *big.Int `toml:",omitempty"`
CongestedThreshold int // Number of pending transactions to consider the network congested and suggest a minimum tip cap.
DefaultBasePrice *big.Int `toml:",omitempty"` // Base price to set when CongestedThreshold is reached before Curie (EIP 1559).
DefaultGasTip *big.Int `toml:",omitempty"` // Default minimum tip cap to use after Curie (EIP 1559).
}

// OracleBackend includes all necessary background APIs for oracle.
Expand Down Expand Up @@ -82,6 +89,7 @@ type Oracle struct {
maxHeaderHistory, maxBlockHistory int
congestedThreshold int // Number of pending transactions to consider the network congested and suggest a minimum tip cap.
defaultBasePrice *big.Int // Base price to set when CongestedThreshold is reached before Curie (EIP 1559).
defaultGasTip *big.Int // Default gas tip to suggest when the network is not congested.
historyCache *lru.Cache
}

Expand Down Expand Up @@ -133,6 +141,11 @@ func NewOracle(backend OracleBackend, params Config) *Oracle {
defaultBasePrice = DefaultBasePrice
log.Warn("Sanitizing invalid gasprice oracle default base price", "provided", params.DefaultBasePrice, "updated", defaultBasePrice)
}
defaultGasTip := params.DefaultGasTip
if defaultGasTip == nil || defaultGasTip.Int64() < 0 {
defaultGasTip = DefaultGasTip
log.Warn("Sanitizing invalid gasprice oracle default gas tip", "provided", params.DefaultGasTip, "updated", defaultGasTip)
}

cache, _ := lru.New(2048)
headEvent := make(chan core.ChainHeadEvent, 1)
Expand Down Expand Up @@ -195,14 +208,10 @@ func (oracle *Oracle) SuggestTipCap(ctx context.Context) (*big.Int, error) {
// high-priced txs are causing the suggested tip cap to be high.
pendingTxCount, _ := oracle.backend.StatsWithMinBaseFee(head.BaseFee)
if pendingTxCount < oracle.congestedThreshold {
// Before Curie (EIP-1559), we need to return the total suggested gas price (base fee + tip cap).
// After Curie, a minimum tip cap of 100 wei is used to avoid issues with extremely low values like 1 wei:
// 1. txpool protections:
// - txpool commonly prevents replacing transactions with the same tip cap.
// - A 1 wei tip may fail to increase (e.g., 1.1x rounds down to 1 wei) due to integer arithmetic.
// 2. Negligible cost:
// - Both 100 wei and 1 wei are almost zero in cost, so this change has no noticeable impact on users.
price := big.NewInt(100)
// Determine the suggested gas price based on the network configuration:
// - Before Curie (EIP-1559): total gas price = base fee + tip cap.
// - After Curie (EIP-1559): use the default tip cap.
price := oracle.defaultGasTip
if !oracle.backend.ChainConfig().IsCurie(head.Number) {
price = oracle.defaultBasePrice
}
Expand Down

0 comments on commit d1a249e

Please sign in to comment.