Skip to content

Commit

Permalink
Decouple gas tests (#12972)
Browse files Browse the repository at this point in the history
* Add first version of evm utils

* Remove unused context util

* Add WSServer tests

* Add NewLegacyTransaction test

* Update NewTestChainScopedConfig to apply correct defaults

* Decouple gas package tests from core

* Move testutils

* Update paths

* Fix import error

* Add changeset
  • Loading branch information
dimriou authored May 9, 2024
1 parent ee810cd commit 1196df4
Show file tree
Hide file tree
Showing 11 changed files with 318 additions and 290 deletions.
5 changes: 5 additions & 0 deletions .changeset/swift-mugs-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"chainlink": minor
---

Decouple gas tests from core #internal
23 changes: 12 additions & 11 deletions core/chains/evm/gas/arbitrum_estimator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ import (

"github.com/smartcontractkit/chainlink-common/pkg/logger"
"github.com/smartcontractkit/chainlink-common/pkg/services/servicetest"
"github.com/smartcontractkit/chainlink-common/pkg/utils/tests"

"github.com/smartcontractkit/chainlink/v2/core/chains/evm/assets"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/gas"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/gas/mocks"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/gas/rollups"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils"
)

type arbConfig struct {
Expand Down Expand Up @@ -57,7 +58,7 @@ func TestArbitrumEstimator(t *testing.T) {
l1Oracle := rollups.NewArbitrumL1GasOracle(logger.Test(t), feeEstimatorClient)

o := gas.NewArbitrumEstimator(logger.Test(t), &arbConfig{}, feeEstimatorClient, l1Oracle)
_, _, err := o.GetLegacyGas(testutils.Context(t), calldata, gasLimit, maxGasPrice)
_, _, err := o.GetLegacyGas(tests.Context(t), calldata, gasLimit, maxGasPrice)
assert.EqualError(t, err, "estimator is not started")
})

Expand All @@ -83,7 +84,7 @@ func TestArbitrumEstimator(t *testing.T) {

o := gas.NewArbitrumEstimator(logger.Test(t), &arbConfig{v: maxGasLimit, bumpPercent: bumpPercent, bumpMin: bumpMin}, feeEstimatorClient, l1Oracle)
servicetest.RunHealthy(t, o)
gasPrice, chainSpecificGasLimit, err := o.GetLegacyGas(testutils.Context(t), calldata, gasLimit, maxGasPrice)
gasPrice, chainSpecificGasLimit, err := o.GetLegacyGas(tests.Context(t), calldata, gasLimit, maxGasPrice)
require.NoError(t, err)
// Expected price for a standard l2_suggested_estimator would be 42, but we add a fixed gasPriceBufferPercentage.
assert.Equal(t, assets.NewWeiI(42).AddPercentage(gasPriceBufferPercentage), gasPrice)
Expand All @@ -109,7 +110,7 @@ func TestArbitrumEstimator(t *testing.T) {
}).Return(zeros.Bytes(), nil)

servicetest.RunHealthy(t, o)
gasPrice, chainSpecificGasLimit, err := o.GetLegacyGas(testutils.Context(t), calldata, gasLimit, assets.NewWeiI(40))
gasPrice, chainSpecificGasLimit, err := o.GetLegacyGas(tests.Context(t), calldata, gasLimit, assets.NewWeiI(40))
require.Error(t, err)
assert.EqualError(t, err, "estimated gas price: 42 wei is greater than the maximum gas price configured: 40 wei")
assert.Nil(t, gasPrice)
Expand All @@ -135,7 +136,7 @@ func TestArbitrumEstimator(t *testing.T) {
}).Return(zeros.Bytes(), nil)

servicetest.RunHealthy(t, o)
gasPrice, chainSpecificGasLimit, err := o.GetLegacyGas(testutils.Context(t), calldata, gasLimit, assets.NewWeiI(110))
gasPrice, chainSpecificGasLimit, err := o.GetLegacyGas(tests.Context(t), calldata, gasLimit, assets.NewWeiI(110))
assert.EqualError(t, err, "estimated gas price: 120 wei is greater than the maximum gas price configured: 110 wei")
assert.Nil(t, gasPrice)
assert.Equal(t, uint64(0), chainSpecificGasLimit)
Expand All @@ -146,7 +147,7 @@ func TestArbitrumEstimator(t *testing.T) {
l1Oracle := rollups.NewArbitrumL1GasOracle(logger.Test(t), feeEstimatorClient)

o := gas.NewArbitrumEstimator(logger.Test(t), &arbConfig{}, feeEstimatorClient, l1Oracle)
_, _, err := o.BumpLegacyGas(testutils.Context(t), assets.NewWeiI(42), gasLimit, assets.NewWeiI(10), nil)
_, _, err := o.BumpLegacyGas(tests.Context(t), assets.NewWeiI(42), gasLimit, assets.NewWeiI(10), nil)
assert.EqualError(t, err, "estimator is not started")
})

Expand All @@ -167,7 +168,7 @@ func TestArbitrumEstimator(t *testing.T) {

servicetest.RunHealthy(t, o)

_, _, err := o.GetLegacyGas(testutils.Context(t), calldata, gasLimit, maxGasPrice)
_, _, err := o.GetLegacyGas(tests.Context(t), calldata, gasLimit, maxGasPrice)
assert.EqualError(t, err, "failed to estimate gas; gas price not set")
})

Expand All @@ -176,7 +177,7 @@ func TestArbitrumEstimator(t *testing.T) {
l1Oracle := rollups.NewArbitrumL1GasOracle(logger.Test(t), feeEstimatorClient)

o := gas.NewArbitrumEstimator(logger.Test(t), &arbConfig{}, feeEstimatorClient, l1Oracle)
_, err := o.GetDynamicFee(testutils.Context(t), maxGasPrice)
_, err := o.GetDynamicFee(tests.Context(t), maxGasPrice)
assert.EqualError(t, err, "dynamic fees are not implemented for this estimator")
})

Expand All @@ -189,7 +190,7 @@ func TestArbitrumEstimator(t *testing.T) {
FeeCap: assets.NewWeiI(42),
TipCap: assets.NewWeiI(5),
}
_, err := o.BumpDynamicFee(testutils.Context(t), fee, maxGasPrice, nil)
_, err := o.BumpDynamicFee(tests.Context(t), fee, maxGasPrice, nil)
assert.EqualError(t, err, "dynamic fees are not implemented for this estimator")
})

Expand Down Expand Up @@ -221,7 +222,7 @@ func TestArbitrumEstimator(t *testing.T) {

o := gas.NewArbitrumEstimator(logger.Test(t), &arbConfig{v: maxGasLimit, bumpPercent: bumpPercent, bumpMin: bumpMin}, feeEstimatorClient, l1Oracle)
servicetest.RunHealthy(t, o)
gasPrice, chainSpecificGasLimit, err := o.GetLegacyGas(testutils.Context(t), calldata, gasLimit, maxGasPrice)
gasPrice, chainSpecificGasLimit, err := o.GetLegacyGas(tests.Context(t), calldata, gasLimit, maxGasPrice)
require.NoError(t, err)
require.NotNil(t, gasPrice)
// Again, a normal l2_suggested_estimator would return 42, but arbitrum_estimator adds a buffer.
Expand Down Expand Up @@ -256,7 +257,7 @@ func TestArbitrumEstimator(t *testing.T) {

o := gas.NewArbitrumEstimator(logger.Test(t), &arbConfig{v: maxGasLimit, bumpPercent: bumpPercent, bumpMin: bumpMin}, feeEstimatorClient, l1Oracle)
servicetest.RunHealthy(t, o)
gasPrice, chainSpecificGasLimit, err := o.GetLegacyGas(testutils.Context(t), calldata, gasLimit, maxGasPrice)
gasPrice, chainSpecificGasLimit, err := o.GetLegacyGas(tests.Context(t), calldata, gasLimit, maxGasPrice)
require.Error(t, err, "expected error but got (%s, %d)", gasPrice, chainSpecificGasLimit)
})
}
4 changes: 2 additions & 2 deletions core/chains/evm/gas/block_history_estimator.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ type estimatorGasEstimatorConfig interface {
type BlockHistoryEstimator struct {
services.StateMachine
ethClient feeEstimatorClient
chainID big.Int
chainID *big.Int
config chainConfig
eConfig estimatorGasEstimatorConfig
bhConfig BlockHistoryConfig
Expand Down Expand Up @@ -127,7 +127,7 @@ type BlockHistoryEstimator struct {
// NewBlockHistoryEstimator returns a new BlockHistoryEstimator that listens
// for new heads and updates the base gas price dynamically based on the
// configured percentile of gas prices in that block
func NewBlockHistoryEstimator(lggr logger.Logger, ethClient feeEstimatorClient, cfg chainConfig, eCfg estimatorGasEstimatorConfig, bhCfg BlockHistoryConfig, chainID big.Int, l1Oracle rollups.L1Oracle) EvmEstimator {
func NewBlockHistoryEstimator(lggr logger.Logger, ethClient feeEstimatorClient, cfg chainConfig, eCfg estimatorGasEstimatorConfig, bhCfg BlockHistoryConfig, chainID *big.Int, l1Oracle rollups.L1Oracle) EvmEstimator {
ctx, cancel := context.WithCancel(context.Background())

b := &BlockHistoryEstimator{
Expand Down
Loading

0 comments on commit 1196df4

Please sign in to comment.