Skip to content

Commit

Permalink
Merge branch 'universal_estimator' of github.com:smartcontractkit/cha…
Browse files Browse the repository at this point in the history
…inlink into feeHistory_test
  • Loading branch information
huangzhen1997 committed Aug 29, 2024
2 parents 2ddeb2a + 034deb3 commit add10e3
Show file tree
Hide file tree
Showing 21 changed files with 161 additions and 269 deletions.
4 changes: 0 additions & 4 deletions core/chains/evm/config/chain_scoped_gas_estimator.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,3 @@ type feeHistoryConfig struct {
func (u *feeHistoryConfig) CacheTimeout() time.Duration {
return u.c.CacheTimeout.Duration()
}

func (u *feeHistoryConfig) HasMempool() bool {
return *u.c.HasMempool
}
1 change: 0 additions & 1 deletion core/chains/evm/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ type BlockHistory interface {

type FeeHistory interface {
CacheTimeout() time.Duration
HasMempool() bool
}

type Workflow interface {
Expand Down
1 change: 0 additions & 1 deletion core/chains/evm/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,6 @@ func TestChainScopedConfig_FeeHistory(t *testing.T) {

u := cfg.EVM().GasEstimator().FeeHistory()
assert.Equal(t, 10*time.Second, u.CacheTimeout())
assert.Equal(t, true, u.HasMempool())
}

func TestChainScopedConfig_GasEstimator(t *testing.T) {
Expand Down
4 changes: 0 additions & 4 deletions core/chains/evm/config/toml/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -728,16 +728,12 @@ func (e *BlockHistoryEstimator) setFrom(f *BlockHistoryEstimator) {

type FeeHistoryEstimator struct {
CacheTimeout *commonconfig.Duration
HasMempool *bool
}

func (u *FeeHistoryEstimator) setFrom(f *FeeHistoryEstimator) {
if v := f.CacheTimeout; v != nil {
u.CacheTimeout = v
}
if v := f.HasMempool; v != nil {
u.HasMempool = v
}
}

type KeySpecificConfig []KeySpecific
Expand Down
1 change: 0 additions & 1 deletion core/chains/evm/config/toml/defaults/fallback.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ TransactionPercentile = 60

[GasEstimator.FeeHistory]
CacheTimeout = '10s'
HasMempool = true

[HeadTracker]
HistoryDepth = 100
Expand Down
274 changes: 142 additions & 132 deletions core/chains/evm/gas/fee_history_estimator.go

Large diffs are not rendered by default.

54 changes: 18 additions & 36 deletions core/chains/evm/gas/fee_history_estimator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,6 @@ func TestFeeHistoryEstimatorLifecycle(t *testing.T) {
assert.ErrorContains(t, u.Start(tests.Context(t)), "RewardPercentile")
})

t.Run("fails to start if BlockHistorySize is 0 in EIP-1559", func(t *testing.T) {
cfg := gas.FeeHistoryEstimatorConfig{
BumpPercent: 20,
RewardPercentile: 10,
BlockHistorySize: 0,
EIP1559: true,
}

u := gas.NewFeeHistoryEstimator(logger.Test(t), nil, cfg, chainID, nil)
assert.ErrorContains(t, u.Start(tests.Context(t)), "BlockHistorySize")
})

t.Run("starts if configs are correct", func(t *testing.T) {
client := mocks.NewFeeHistoryEstimatorClient(t)
client.On("SuggestGasPrice", mock.Anything).Return(big.NewInt(10), nil).Maybe()
Expand Down Expand Up @@ -97,7 +85,7 @@ func TestFeeHistoryEstimatorGetLegacyGas(t *testing.T) {
cfg := gas.FeeHistoryEstimatorConfig{}

u := gas.NewFeeHistoryEstimator(logger.Test(t), client, cfg, chainID, nil)
_, err := u.FetchGasPrice()
err := u.RefreshGasPrice()
assert.NoError(t, err)
gasPrice, _, err := u.GetLegacyGas(tests.Context(t), nil, gasLimit, maxPrice)
assert.NoError(t, err)
Expand All @@ -112,7 +100,7 @@ func TestFeeHistoryEstimatorGetLegacyGas(t *testing.T) {

maxPrice := assets.NewWeiI(1)
u := gas.NewFeeHistoryEstimator(logger.Test(t), client, cfg, chainID, nil)
_, err := u.FetchGasPrice()
err := u.RefreshGasPrice()
assert.NoError(t, err)
gas1, _, err := u.GetLegacyGas(tests.Context(t), nil, gasLimit, maxPrice)
assert.NoError(t, err)
Expand Down Expand Up @@ -145,7 +133,7 @@ func TestFeeHistoryEstimatorBumpLegacyGas(t *testing.T) {
cfg := gas.FeeHistoryEstimatorConfig{BumpPercent: 50}

u := gas.NewFeeHistoryEstimator(logger.Test(t), client, cfg, chainID, nil)
_, err := u.FetchGasPrice()
err := u.RefreshGasPrice()
assert.NoError(t, err)
gasPrice, _, err := u.BumpLegacyGas(tests.Context(t), originalGasPrice, gasLimit, maxPrice, nil)
assert.NoError(t, err)
Expand Down Expand Up @@ -186,7 +174,7 @@ func TestFeeHistoryEstimatorBumpLegacyGas(t *testing.T) {
cfg := gas.FeeHistoryEstimatorConfig{}

u := gas.NewFeeHistoryEstimator(logger.Test(t), client, cfg, chainID, nil)
_, err := u.FetchGasPrice()
err := u.RefreshGasPrice()
assert.NoError(t, err)
gas, _, err := u.BumpLegacyGas(tests.Context(t), originalGasPrice, gasLimit, maxPrice, nil)
assert.NoError(t, err)
Expand All @@ -202,7 +190,7 @@ func TestFeeHistoryEstimatorBumpLegacyGas(t *testing.T) {

maxPrice := assets.NewWeiI(14)
u := gas.NewFeeHistoryEstimator(logger.Test(t), client, cfg, chainID, nil)
_, err := u.FetchGasPrice()
err := u.RefreshGasPrice()
assert.NoError(t, err)
gas, _, err := u.BumpLegacyGas(tests.Context(t), originalGasPrice, gasLimit, maxPrice, nil)
assert.NoError(t, err)
Expand All @@ -218,7 +206,7 @@ func TestFeeHistoryEstimatorBumpLegacyGas(t *testing.T) {

maxPrice := assets.NewWeiI(14)
u := gas.NewFeeHistoryEstimator(logger.Test(t), client, cfg, chainID, nil)
_, err := u.FetchGasPrice()
err := u.RefreshGasPrice()
assert.NoError(t, err)
gas, _, err := u.BumpLegacyGas(tests.Context(t), originalGasPrice, gasLimit, maxPrice, nil)
assert.NoError(t, err)
Expand All @@ -235,7 +223,7 @@ func TestFeeHistoryEstimatorBumpLegacyGas(t *testing.T) {
// Price will be capped by the max price
maxPrice := assets.NewWeiI(101)
u := gas.NewFeeHistoryEstimator(logger.Test(t), client, cfg, chainID, nil)
_, err := u.FetchGasPrice()
err := u.RefreshGasPrice()
assert.NoError(t, err)
_, _, err = u.BumpLegacyGas(tests.Context(t), originalGasPrice, gasLimit, maxPrice, nil)
assert.Error(t, err)
Expand Down Expand Up @@ -269,7 +257,7 @@ func TestFeeHistoryEstimatorGetDynamicFee(t *testing.T) {
maxFee := (*assets.Wei)(baseFee).AddPercentage(gas.BaseFeeBufferPercentage).Add((*assets.Wei)(avrgPriorityFee))

u := gas.NewFeeHistoryEstimator(logger.Test(t), client, cfg, chainID, nil)
_, err := u.FetchDynamicPrice()
err := u.RefreshDynamicPrice()
assert.NoError(t, err)
dynamicFee, err := u.GetDynamicFee(tests.Context(t), maxPrice)
assert.NoError(t, err)
Expand Down Expand Up @@ -314,7 +302,7 @@ func TestFeeHistoryEstimatorGetDynamicFee(t *testing.T) {
cfg := gas.FeeHistoryEstimatorConfig{BlockHistorySize: 1}

u := gas.NewFeeHistoryEstimator(logger.Test(t), client, cfg, chainID, nil)
_, err := u.FetchDynamicPrice()
err := u.RefreshDynamicPrice()
assert.NoError(t, err)
dynamicFee, err := u.GetDynamicFee(tests.Context(t), maxPrice)
assert.NoError(t, err)
Expand Down Expand Up @@ -348,14 +336,13 @@ func TestFeeHistoryEstimatorBumpDynamicFee(t *testing.T) {
cfg := gas.FeeHistoryEstimatorConfig{
BlockHistorySize: 2,
BumpPercent: 50,
HasMempool: true,
}

expectedFeeCap := originalFee.FeeCap.AddPercentage(cfg.BumpPercent)
expectedTipCap := originalFee.TipCap.AddPercentage(cfg.BumpPercent)

u := gas.NewFeeHistoryEstimator(logger.Test(t), client, cfg, chainID, nil)
_, err := u.FetchDynamicPrice()
err := u.RefreshDynamicPrice()
assert.NoError(t, err)
dynamicFee, err := u.BumpDynamicFee(tests.Context(t), originalFee, globalMaxPrice, nil)
assert.NoError(t, err)
Expand Down Expand Up @@ -414,11 +401,10 @@ func TestFeeHistoryEstimatorBumpDynamicFee(t *testing.T) {
cfg := gas.FeeHistoryEstimatorConfig{
BlockHistorySize: 1,
BumpPercent: 50,
HasMempool: true,
}

u := gas.NewFeeHistoryEstimator(logger.Test(t), client, cfg, chainID, nil)
_, err := u.FetchDynamicPrice()
err := u.RefreshDynamicPrice()
assert.NoError(t, err)
bumpedFee, err := u.BumpDynamicFee(tests.Context(t), originalFee, globalMaxPrice, nil)
assert.NoError(t, err)
Expand Down Expand Up @@ -447,11 +433,10 @@ func TestFeeHistoryEstimatorBumpDynamicFee(t *testing.T) {
cfg := gas.FeeHistoryEstimatorConfig{
BlockHistorySize: 1,
BumpPercent: 50,
HasMempool: true,
}

u := gas.NewFeeHistoryEstimator(logger.Test(t), client, cfg, chainID, nil)
_, err := u.FetchDynamicPrice()
err := u.RefreshDynamicPrice()
assert.NoError(t, err)
_, err = u.BumpDynamicFee(tests.Context(t), originalFee, globalMaxPrice, nil)
assert.Error(t, err)
Expand Down Expand Up @@ -479,11 +464,10 @@ func TestFeeHistoryEstimatorBumpDynamicFee(t *testing.T) {
cfg := gas.FeeHistoryEstimatorConfig{
BlockHistorySize: 1,
BumpPercent: 50,
HasMempool: true,
}

u := gas.NewFeeHistoryEstimator(logger.Test(t), client, cfg, chainID, nil)
_, err := u.FetchDynamicPrice()
err := u.RefreshDynamicPrice()
assert.NoError(t, err)
bumpedFee, err := u.BumpDynamicFee(tests.Context(t), originalFee, maxPrice, nil)
assert.NoError(t, err)
Expand Down Expand Up @@ -516,13 +500,13 @@ func TestFeeHistoryEstimatorBumpDynamicFee(t *testing.T) {
}

u := gas.NewFeeHistoryEstimator(logger.Test(t), client, cfg, chainID, nil)
_, err := u.FetchDynamicPrice()
err := u.RefreshDynamicPrice()
assert.NoError(t, err)
_, err = u.BumpDynamicFee(tests.Context(t), originalFee, maxPrice, nil)
assert.Error(t, err)
})

t.Run("ignores maxPriorityFeePerGas if there is no mempool", func(t *testing.T) {
t.Run("ignores maxPriorityFeePerGas if there is no mempool and forces refetch", func(t *testing.T) {
client := mocks.NewFeeHistoryEstimatorClient(t)
originalFee := gas.DynamicFee{
FeeCap: assets.NewWeiI(40),
Expand All @@ -541,17 +525,15 @@ func TestFeeHistoryEstimatorBumpDynamicFee(t *testing.T) {
client.On("FeeHistory", mock.Anything, mock.Anything, mock.Anything).Return(feeHistoryResult, nil).Once()

cfg := gas.FeeHistoryEstimatorConfig{
BlockHistorySize: 1,
BlockHistorySize: 0,
BumpPercent: 20,
HasMempool: false,
}

maxFeePerGas := assets.NewWei(baseFee).AddPercentage(gas.BaseFeeBufferPercentage)
u := gas.NewFeeHistoryEstimator(logger.Test(t), client, cfg, chainID, nil)
_, err := u.FetchDynamicPrice()
assert.NoError(t, err)
bumpedFee, err := u.BumpDynamicFee(tests.Context(t), originalFee, globalMaxPrice, nil)
assert.NoError(t, err)
assert.Equal(t, assets.NewWeiI(0), (*assets.Wei)(maxPriorityFeePerGas))
assert.Equal(t, originalFee.FeeCap.AddPercentage(20), bumpedFee.FeeCap)
assert.Equal(t, maxFeePerGas, bumpedFee.FeeCap)
})
}
3 changes: 1 addition & 2 deletions core/chains/evm/gas/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ func NewEstimator(lggr logger.Logger, ethClient feeEstimatorClient, cfg Config,
EIP1559: geCfg.EIP1559DynamicFees(),
BlockHistorySize: uint64(geCfg.BlockHistory().BlockHistorySize()),
RewardPercentile: float64(geCfg.BlockHistory().TransactionPercentile()),
HasMempool: geCfg.FeeHistory().HasMempool(),
}
return NewFeeHistoryEstimator(lggr, ethClient, ccfg, ethClient.ConfiguredChainID(), l1Oracle)
}
Expand Down Expand Up @@ -404,7 +403,7 @@ func (e *evmFeeEstimator) estimateFeeLimit(ctx context.Context, feeLimit uint64,
e.lggr.Debugw("estimated gas limit with buffer exceeds the provided gas limit with multiplier. falling back to the provided gas limit with multiplier", "estimatedGasLimit", estimatedFeeLimit, "providedGasLimitWithMultiplier", providedGasLimit)
estimatedFeeLimit = providedGasLimit
}

return
}

Expand Down
1 change: 0 additions & 1 deletion core/chains/evm/txmgr/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ type TestFeeHistoryConfig struct {
}

func (b *TestFeeHistoryConfig) CacheTimeout() time.Duration { return 0 * time.Second }
func (b *TestFeeHistoryConfig) HasMempool() bool { return true }

type transactionsConfig struct {
evmconfig.Transactions
Expand Down
3 changes: 0 additions & 3 deletions core/config/docs/chains-evm.toml
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,6 @@ TransactionPercentile = 60 # Default
# the timeout. The estimator is already adding a buffer to account for a potential increase in prices within one or two blocks. On the other hand, slower frequency will fail to refresh
# the prices and end up in stale values.
CacheTimeout = '10s' # Default
# HasMempool should be set to true if the estimator is making RPC calls to a network that supports a transaction mempool. This is only relevant for EIP-1559 estimations and it forces a
# minimum bumping check on maxPriorityFeePerGas and a connectivity check. For chains that don't have a mempool and process transactions in a FCFS manner, the two checks are omitted.
HasMempool = true # Default

# The head tracker continually listens for new heads from the chain.
#
Expand Down
2 changes: 0 additions & 2 deletions core/services/chainlink/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,6 @@ func TestConfig_Marshal(t *testing.T) {
},
FeeHistory: evmcfg.FeeHistoryEstimator{
CacheTimeout: &second,
HasMempool: ptr(true),
},
},

Expand Down Expand Up @@ -1057,7 +1056,6 @@ TransactionPercentile = 15
[EVM.GasEstimator.FeeHistory]
CacheTimeout = '1s'
HasMempool = true
[EVM.HeadTracker]
HistoryDepth = 15
Expand Down
1 change: 0 additions & 1 deletion core/services/chainlink/testdata/config-full.toml
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,6 @@ TransactionPercentile = 15

[EVM.GasEstimator.FeeHistory]
CacheTimeout = '1s'
HasMempool = true

[EVM.HeadTracker]
HistoryDepth = 15
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,6 @@ TransactionPercentile = 50

[EVM.GasEstimator.FeeHistory]
CacheTimeout = '10s'
HasMempool = true

[EVM.HeadTracker]
HistoryDepth = 100
Expand Down Expand Up @@ -426,7 +425,6 @@ TransactionPercentile = 50

[EVM.GasEstimator.FeeHistory]
CacheTimeout = '10s'
HasMempool = true

[EVM.HeadTracker]
HistoryDepth = 100
Expand Down Expand Up @@ -525,7 +523,6 @@ TransactionPercentile = 60

[EVM.GasEstimator.FeeHistory]
CacheTimeout = '10s'
HasMempool = true

[EVM.HeadTracker]
HistoryDepth = 2000
Expand Down
1 change: 0 additions & 1 deletion core/web/resolver/testdata/config-full.toml
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,6 @@ TransactionPercentile = 15

[EVM.GasEstimator.FeeHistory]
CacheTimeout = '1s'
HasMempool = true

[EVM.HeadTracker]
HistoryDepth = 15
Expand Down
3 changes: 0 additions & 3 deletions core/web/resolver/testdata/config-multi-chain-effective.toml
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,6 @@ TransactionPercentile = 50

[EVM.GasEstimator.FeeHistory]
CacheTimeout = '10s'
HasMempool = true

[EVM.HeadTracker]
HistoryDepth = 100
Expand Down Expand Up @@ -426,7 +425,6 @@ TransactionPercentile = 50

[EVM.GasEstimator.FeeHistory]
CacheTimeout = '10s'
HasMempool = true

[EVM.HeadTracker]
HistoryDepth = 100
Expand Down Expand Up @@ -525,7 +523,6 @@ TransactionPercentile = 60

[EVM.GasEstimator.FeeHistory]
CacheTimeout = '10s'
HasMempool = true

[EVM.HeadTracker]
HistoryDepth = 2000
Expand Down
Loading

0 comments on commit add10e3

Please sign in to comment.