Skip to content

Commit

Permalink
(Optimism): Introduce a new config to limit max gas cost | Optimism
Browse files Browse the repository at this point in the history
  • Loading branch information
gemene committed Jun 12, 2024
1 parent 9f6e454 commit 7e55515
Show file tree
Hide file tree
Showing 12 changed files with 139 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/clean-humans-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"chainlink": patch
---

#added limit max gas cost for Optimism
4 changes: 4 additions & 0 deletions core/chains/evm/config/chain_scoped_gas_estimator.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ func (g *gasEstimatorConfig) BumpMin() *assets.Wei {
return g.c.BumpMin
}

func (g *gasEstimatorConfig) CostMax() *assets.Wei {
return g.c.CostMax
}

func (g *gasEstimatorConfig) FeeCapDefault() *assets.Wei {
return g.c.FeeCapDefault
}
Expand Down
1 change: 1 addition & 0 deletions core/chains/evm/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ type GasEstimator interface {
LimitMax() uint64
LimitMultiplier() float32
LimitTransfer() uint64
CostMax() *assets.Wei
PriceDefault() *assets.Wei
TipCapDefault() *assets.Wei
TipCapMin() *assets.Wei
Expand Down
20 changes: 20 additions & 0 deletions core/chains/evm/config/mocks/gas_estimator.go

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

5 changes: 5 additions & 0 deletions core/chains/evm/config/toml/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,8 @@ type GasEstimator struct {
TipCapDefault *assets.Wei
TipCapMin *assets.Wei

CostMax *assets.Wei

BlockHistory BlockHistoryEstimator `toml:",omitempty"`
}

Expand Down Expand Up @@ -645,6 +647,9 @@ func (e *GasEstimator) setFrom(f *GasEstimator) {
if v := f.PriceMin; v != nil {
e.PriceMin = v
}
if v := f.CostMax; v != nil {
e.CostMax = v
}
e.LimitJobType.setFrom(&f.LimitJobType)
e.BlockHistory.setFrom(&f.BlockHistory)
}
Expand Down
1 change: 1 addition & 0 deletions core/chains/evm/config/toml/defaults/Optimism_Sepolia.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ MinIncomingConfirmations = 1
EIP1559DynamicFees = true
PriceMin = '1 wei'
BumpMin = '100 wei'
CostMax = '1 ether'

[GasEstimator.BlockHistory]
BlockHistorySize = 60
Expand Down
1 change: 1 addition & 0 deletions core/chains/evm/config/toml/defaults/fallback.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ EIP1559DynamicFees = false
FeeCapDefault = '100 gwei'
TipCapDefault = '1'
TipCapMin = '1'
CostMax = '0 wei'

[GasEstimator.BlockHistory]
BatchSize = 25
Expand Down
3 changes: 3 additions & 0 deletions core/chains/evm/gas/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ type MockGasEstimatorConfig struct {
PriceMinF *assets.Wei
PriceDefaultF *assets.Wei
FeeCapDefaultF *assets.Wei
CostMaxF *assets.Wei
LimitMaxF uint64
ModeF string
}
Expand All @@ -163,6 +164,8 @@ func (m *MockGasEstimatorConfig) BumpMin() *assets.Wei {
return m.BumpMinF
}

func (m *MockGasEstimatorConfig) CostMax() *assets.Wei { return m.CostMaxF }

func (m *MockGasEstimatorConfig) EIP1559DynamicFees() bool {
return m.EIP1559DynamicFeesF
}
Expand Down
29 changes: 27 additions & 2 deletions core/chains/evm/gas/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,29 @@ func (e *evmFeeEstimator) L1Oracle() rollups.L1Oracle {
return e.EvmEstimator.L1Oracle()
}

func (e *evmFeeEstimator) getAdjustedFeeToMaxGasCost(fee *assets.Wei) *assets.Wei {
if e.geCfg.CostMax() != nil {
return fee
}
maxGasCost := e.geCfg.CostMax().ToInt().Uint64()
if maxGasCost == 0 {
return fee
}
totalGasCost, err := commonfee.ApplyMultiplier(fee.ToInt().Uint64(), e.geCfg.LimitMultiplier())
if err != nil {
return fee
}
if totalGasCost <= maxGasCost {
return fee
}
newFee, err := commonfee.ApplyMultiplier(maxGasCost, 1/e.geCfg.LimitMultiplier())
if err != nil {
return fee
}
e.lggr.Warnf("estimated gas cost %d exceeds maximum allowed gas cost %d. old fee adjused from %d to %d", totalGasCost, maxGasCost, fee.ToInt().Uint64(), newFee)
return assets.NewWei(new(big.Int).SetUint64(newFee))
}

func (e *evmFeeEstimator) GetFee(ctx context.Context, calldata []byte, feeLimit uint64, maxFeePrice *assets.Wei, opts ...feetypes.Opt) (fee EvmFee, chainSpecificFeeLimit uint64, err error) {
// get dynamic fee
if e.EIP1559Enabled {
Expand All @@ -271,14 +294,14 @@ func (e *evmFeeEstimator) GetFee(ctx context.Context, calldata []byte, feeLimit
fee.DynamicTipCap = dynamicFee.TipCap
return
}

// get legacy fee
fee.Legacy, chainSpecificFeeLimit, err = e.EvmEstimator.GetLegacyGas(ctx, calldata, feeLimit, maxFeePrice, opts...)
if err != nil {
return
}
chainSpecificFeeLimit, err = commonfee.ApplyMultiplier(chainSpecificFeeLimit, e.geCfg.LimitMultiplier())

chainSpecificFeeLimit, err = commonfee.ApplyMultiplier(chainSpecificFeeLimit, e.geCfg.LimitMultiplier())
fee.Legacy = e.getAdjustedFeeToMaxGasCost(fee.Legacy)
return
}

Expand Down Expand Up @@ -331,6 +354,7 @@ func (e *evmFeeEstimator) BumpFee(ctx context.Context, originalFee EvmFee, feeLi
return
}
chainSpecificFeeLimit, err = commonfee.ApplyMultiplier(chainSpecificFeeLimit, e.geCfg.LimitMultiplier())
bumpedFee.Legacy = e.getAdjustedFeeToMaxGasCost(bumpedFee.Legacy)
return
}

Expand All @@ -348,6 +372,7 @@ type GasEstimatorConfig interface {
BumpPercent() uint16
BumpThreshold() uint64
BumpMin() *assets.Wei
CostMax() *assets.Wei
FeeCapDefault() *assets.Wei
LimitMax() uint64
LimitMultiplier() float32
Expand Down
4 changes: 3 additions & 1 deletion core/chains/evm/txmgr/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ func (g *TestGasEstimatorConfig) LimitJobType() evmconfig.LimitJobType {
func (g *TestGasEstimatorConfig) PriceMaxKey(addr common.Address) *assets.Wei {
return assets.NewWeiI(42)
}

func (g *TestGasEstimatorConfig) CostMax() *assets.Wei {
return assets.NewWeiI(1000000000000000000)
}
func (e *TestEvmConfig) GasEstimator() evmconfig.GasEstimator {
return &TestGasEstimatorConfig{bumpThreshold: e.BumpThreshold}
}
Expand Down
3 changes: 3 additions & 0 deletions core/config/docs/chains-evm.toml
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,9 @@ TipCapDefault = '1 wei' # Default
# (Only applies to EIP-1559 transactions)
TipCapMin = '1 wei' # Default

# CostMax is the maximum gas cost allowed for a single Tx. O means that there is no limit.
CostMax = '0 wei' # Default

[EVM.GasEstimator.LimitJobType]
# OCR overrides LimitDefault for OCR jobs.
OCR = 100_000 # Example
Expand Down
Loading

0 comments on commit 7e55515

Please sign in to comment.