Skip to content

Commit

Permalink
Gas limit estimation feature (smartcontractkit#14041)
Browse files Browse the repository at this point in the history
* Added gas limit estimation feature to EVM gas estimators

* Added changeset

* Fixed linting

* Added new failure tests

* Fixed test

* Fixed mock configs in ccip capabilities

* Fixed configs

* Fixed test

* Refactored GetFee method

* Added EstimatedGasBuffer and addressed feedback

* Fixed linting

* Fixed config doc and tests

* Addressed feedback

* Fixed typo

* Repurposed LimitMultiplier to be used as a buffer for estimated gas

* Updated mocks

* Removed LimitMultiplier if gas estimation fails and updated config docs

* Fixed Broadcaster test

* Enabled uncapped gas limit estimation if provided fee limit is 0

* Updated estimate gas buffer to be a fixed value instead of using LimitMultiplier

* Updated log message

* Updated logs

---------

Co-authored-by: Prashant Yadav <[email protected]>
  • Loading branch information
amit-momin and prashantkumar1982 authored Aug 22, 2024
1 parent c98feb2 commit 8d818ea
Show file tree
Hide file tree
Showing 43 changed files with 621 additions and 116 deletions.
5 changes: 5 additions & 0 deletions .changeset/loud-windows-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"chainlink": minor
---

Added gas limit estimation feature to EVM gas estimators #added
1 change: 1 addition & 0 deletions common/fee/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var (
ErrBumpFeeExceedsLimit = errors.New("fee bump exceeds limit")
ErrBump = errors.New("fee bump failed")
ErrConnectivity = errors.New("transaction propagation issue: transactions are not being mined")
ErrFeeLimitTooLow = errors.New("provided fee limit too low")
)

func IsBumpErr(err error) bool {
Expand Down
7 changes: 6 additions & 1 deletion common/txmgr/broadcaster.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/smartcontractkit/chainlink-common/pkg/utils"

"github.com/smartcontractkit/chainlink/v2/common/client"
commonfee "github.com/smartcontractkit/chainlink/v2/common/fee"
feetypes "github.com/smartcontractkit/chainlink/v2/common/fee/types"
txmgrtypes "github.com/smartcontractkit/chainlink/v2/common/txmgr/types"
"github.com/smartcontractkit/chainlink/v2/common/types"
Expand Down Expand Up @@ -434,7 +435,11 @@ func (eb *Broadcaster[CHAIN_ID, HEAD, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE]) hand
}

attempt, _, _, retryable, err := eb.NewTxAttempt(ctx, *etx, eb.lggr)
if err != nil {
// Mark transaction as fatal if provided gas limit is set too low
if errors.Is(err, commonfee.ErrFeeLimitTooLow) {
etx.Error = null.StringFrom(commonfee.ErrFeeLimitTooLow.Error())
return eb.saveFatallyErroredTransaction(eb.lggr, etx), false
} else if err != nil {
return fmt.Errorf("processUnstartedTxs failed on NewAttempt: %w", err), retryable
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,7 @@ func (g *TestGasEstimatorConfig) LimitJobType() evmconfig.LimitJobType {
func (g *TestGasEstimatorConfig) PriceMaxKey(addr common.Address) *assets.Wei {
return assets.GWei(1)
}
func (g *TestGasEstimatorConfig) EstimateGasLimit() bool { return false }

func (e *TestEvmConfig) GasEstimator() evmconfig.GasEstimator {
return &TestGasEstimatorConfig{bumpThreshold: e.BumpThreshold}
Expand Down
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 @@ -108,6 +108,10 @@ func (g *gasEstimatorConfig) LimitJobType() LimitJobType {
return &limitJobTypeConfig{c: g.c.LimitJobType}
}

func (g *gasEstimatorConfig) EstimateGasLimit() bool {
return *g.c.EstimateGasLimit
}

type limitJobTypeConfig struct {
c toml.GasLimitJobType
}
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 @@ -136,6 +136,7 @@ type GasEstimator interface {
PriceMin() *assets.Wei
Mode() string
PriceMaxKey(gethcommon.Address) *assets.Wei
EstimateGasLimit() bool
}

type LimitJobType interface {
Expand Down
45 changes: 45 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.

14 changes: 9 additions & 5 deletions core/chains/evm/config/toml/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,11 +549,12 @@ type GasEstimator struct {
PriceMax *assets.Wei
PriceMin *assets.Wei

LimitDefault *uint64
LimitMax *uint64
LimitMultiplier *decimal.Decimal
LimitTransfer *uint64
LimitJobType GasLimitJobType `toml:",omitempty"`
LimitDefault *uint64
LimitMax *uint64
LimitMultiplier *decimal.Decimal
LimitTransfer *uint64
LimitJobType GasLimitJobType `toml:",omitempty"`
EstimateGasLimit *bool

BumpMin *assets.Wei
BumpPercent *uint16
Expand Down Expand Up @@ -641,6 +642,9 @@ func (e *GasEstimator) setFrom(f *GasEstimator) {
if v := f.LimitTransfer; v != nil {
e.LimitTransfer = v
}
if v := f.EstimateGasLimit; v != nil {
e.EstimateGasLimit = v
}
if v := f.PriceDefault; v != nil {
e.PriceDefault = v
}
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 @@ -47,6 +47,7 @@ EIP1559DynamicFees = false
FeeCapDefault = '100 gwei'
TipCapDefault = '1'
TipCapMin = '1'
EstimateGasLimit = false

[GasEstimator.BlockHistory]
BatchSize = 25
Expand Down
5 changes: 5 additions & 0 deletions core/chains/evm/gas/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ type MockGasEstimatorConfig struct {
FeeCapDefaultF *assets.Wei
LimitMaxF uint64
ModeF string
EstimateGasLimitF bool
}

func NewMockGasConfig() *MockGasEstimatorConfig {
Expand Down Expand Up @@ -214,3 +215,7 @@ func (m *MockGasEstimatorConfig) LimitMax() uint64 {
func (m *MockGasEstimatorConfig) Mode() string {
return m.ModeF
}

func (m *MockGasEstimatorConfig) EstimateGasLimit() bool {
return m.EstimateGasLimitF
}
76 changes: 40 additions & 36 deletions core/chains/evm/gas/mocks/evm_fee_estimator.go

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

Loading

0 comments on commit 8d818ea

Please sign in to comment.