diff --git a/.changeset/swift-mugs-push.md b/.changeset/swift-mugs-push.md new file mode 100644 index 00000000000..506f43efc81 --- /dev/null +++ b/.changeset/swift-mugs-push.md @@ -0,0 +1,5 @@ +--- +"chainlink": minor +--- + +Decouple gas tests from core #internal diff --git a/core/chains/evm/gas/arbitrum_estimator_test.go b/core/chains/evm/gas/arbitrum_estimator_test.go index 54d7fc333e3..289025fcdab 100644 --- a/core/chains/evm/gas/arbitrum_estimator_test.go +++ b/core/chains/evm/gas/arbitrum_estimator_test.go @@ -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 { @@ -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") }) @@ -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) @@ -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) @@ -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) @@ -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") }) @@ -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") }) @@ -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") }) @@ -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") }) @@ -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. @@ -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) }) } diff --git a/core/chains/evm/gas/block_history_estimator.go b/core/chains/evm/gas/block_history_estimator.go index 0ae067e45bf..82f1c46fff8 100644 --- a/core/chains/evm/gas/block_history_estimator.go +++ b/core/chains/evm/gas/block_history_estimator.go @@ -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 @@ -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{ diff --git a/core/chains/evm/gas/block_history_estimator_test.go b/core/chains/evm/gas/block_history_estimator_test.go index 1eeedf43896..730bfcab7e1 100644 --- a/core/chains/evm/gas/block_history_estimator_test.go +++ b/core/chains/evm/gas/block_history_estimator_test.go @@ -19,19 +19,20 @@ import ( "go.uber.org/zap/zapcore" "github.com/smartcontractkit/chainlink-common/pkg/logger" + "github.com/smartcontractkit/chainlink-common/pkg/utils/tests" + "github.com/smartcontractkit/chainlink/v2/common/config" commonfee "github.com/smartcontractkit/chainlink/v2/common/fee" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/assets" evmclient "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client" + evmmocks "github.com/smartcontractkit/chainlink/v2/core/chains/evm/client/mocks" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/gas" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/gas/rollups" rollupMocks "github.com/smartcontractkit/chainlink/v2/core/chains/evm/gas/rollups/mocks" + "github.com/smartcontractkit/chainlink/v2/core/chains/evm/testutils" evmtypes "github.com/smartcontractkit/chainlink/v2/core/chains/evm/types" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/utils" ubig "github.com/smartcontractkit/chainlink/v2/core/chains/evm/utils/big" - "github.com/smartcontractkit/chainlink/v2/core/internal/cltest" - "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" - "github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest" ) func NewEvmHash() common.Hash { @@ -44,12 +45,12 @@ func newBlockHistoryConfig() *gas.MockBlockHistoryConfig { return c } -func newBlockHistoryEstimatorWithChainID(t *testing.T, c evmclient.Client, cfg gas.Config, gCfg gas.GasEstimatorConfig, bhCfg gas.BlockHistoryConfig, cid big.Int, l1Oracle rollups.L1Oracle) gas.EvmEstimator { +func newBlockHistoryEstimatorWithChainID(t *testing.T, c evmclient.Client, cfg gas.Config, gCfg gas.GasEstimatorConfig, bhCfg gas.BlockHistoryConfig, cid *big.Int, l1Oracle rollups.L1Oracle) gas.EvmEstimator { return gas.NewBlockHistoryEstimator(logger.Test(t), c, cfg, gCfg, bhCfg, cid, l1Oracle) } func newBlockHistoryEstimator(t *testing.T, c evmclient.Client, cfg gas.Config, gCfg gas.GasEstimatorConfig, bhCfg gas.BlockHistoryConfig, l1Oracle rollups.L1Oracle) *gas.BlockHistoryEstimator { - iface := newBlockHistoryEstimatorWithChainID(t, c, cfg, gCfg, bhCfg, cltest.FixtureChainID, l1Oracle) + iface := newBlockHistoryEstimatorWithChainID(t, c, cfg, gCfg, bhCfg, testutils.FixtureChainID, l1Oracle) return gas.BlockHistoryEstimatorFromInterface(iface) } @@ -78,7 +79,7 @@ func TestBlockHistoryEstimator_Start(t *testing.T) { bhCfg.TransactionPercentileF = percentile t.Run("loads initial state", func(t *testing.T) { - ethClient := evmtest.NewEthClientMockWithDefaultChain(t) + ethClient := testutils.NewEthClientMockWithDefaultChain(t) l1Oracle := rollupMocks.NewL1Oracle(t) bhe := newBlockHistoryEstimator(t, ethClient, cfg, geCfg, bhCfg, l1Oracle) @@ -101,7 +102,7 @@ func TestBlockHistoryEstimator_Start(t *testing.T) { } }).Once() - err := bhe.Start(testutils.Context(t)) + err := bhe.Start(tests.Context(t)) require.NoError(t, err) assert.Len(t, gas.GetRollingBlockHistory(bhe), 2) @@ -123,7 +124,7 @@ func TestBlockHistoryEstimator_Start(t *testing.T) { bhCfg2.TransactionPercentileF = percentile cfg2 := gas.NewMockConfig() - ethClient := evmtest.NewEthClientMockWithDefaultChain(t) + ethClient := testutils.NewEthClientMockWithDefaultChain(t) l1Oracle := rollupMocks.NewL1Oracle(t) bhe := newBlockHistoryEstimator(t, ethClient, cfg2, geCfg2, bhCfg2, l1Oracle) @@ -147,7 +148,7 @@ func TestBlockHistoryEstimator_Start(t *testing.T) { b[0].Method == "eth_getBlockByNumber" && b[0].Args[0] == gas.Int64ToHex(41) && b[0].Args[1].(bool) && reflect.TypeOf(b[0].Result) == reflect.TypeOf(&evmtypes.Block{}) })).Return(pkgerrors.Wrap(context.DeadlineExceeded, "some error message")).Once() - err := bhe.Start(testutils.Context(t)) + err := bhe.Start(tests.Context(t)) require.NoError(t, err) require.Len(t, gas.GetRollingBlockHistory(bhe), 1) @@ -157,7 +158,7 @@ func TestBlockHistoryEstimator_Start(t *testing.T) { }) t.Run("boots even if initial batch call returns nothing", func(t *testing.T) { - ethClient := evmtest.NewEthClientMockWithDefaultChain(t) + ethClient := testutils.NewEthClientMockWithDefaultChain(t) l1Oracle := rollupMocks.NewL1Oracle(t) bhe := newBlockHistoryEstimator(t, ethClient, cfg, geCfg, bhCfg, l1Oracle) @@ -168,7 +169,7 @@ func TestBlockHistoryEstimator_Start(t *testing.T) { return len(b) == int(historySize) })).Return(nil) - err := bhe.Start(testutils.Context(t)) + err := bhe.Start(tests.Context(t)) require.NoError(t, err) // non-eip1559 block @@ -176,29 +177,29 @@ func TestBlockHistoryEstimator_Start(t *testing.T) { }) t.Run("starts anyway if fetching latest head fails", func(t *testing.T) { - ethClient := evmtest.NewEthClientMockWithDefaultChain(t) + ethClient := testutils.NewEthClientMockWithDefaultChain(t) l1Oracle := rollupMocks.NewL1Oracle(t) bhe := newBlockHistoryEstimator(t, ethClient, cfg, geCfg, bhCfg, l1Oracle) ethClient.On("HeadByNumber", mock.Anything, (*big.Int)(nil)).Return(nil, pkgerrors.New("something exploded")) - err := bhe.Start(testutils.Context(t)) + err := bhe.Start(tests.Context(t)) require.NoError(t, err) assert.Nil(t, gas.GetLatestBaseFee(bhe)) - _, _, err = bhe.GetLegacyGas(testutils.Context(t), make([]byte, 0), 100, maxGasPrice) + _, _, err = bhe.GetLegacyGas(tests.Context(t), make([]byte, 0), 100, maxGasPrice) require.Error(t, err) require.Contains(t, err.Error(), "has not finished the first gas estimation yet, likely because a failure on start") - _, err = bhe.GetDynamicFee(testutils.Context(t), maxGasPrice) + _, err = bhe.GetDynamicFee(tests.Context(t), maxGasPrice) require.Error(t, err) require.Contains(t, err.Error(), "has not finished the first gas estimation yet, likely because a failure on start") }) t.Run("starts anyway if fetching first fetch fails, but errors on estimation", func(t *testing.T) { - ethClient := evmtest.NewEthClientMockWithDefaultChain(t) + ethClient := testutils.NewEthClientMockWithDefaultChain(t) l1Oracle := rollupMocks.NewL1Oracle(t) bhe := newBlockHistoryEstimator(t, ethClient, cfg, geCfg, bhCfg, l1Oracle) @@ -207,22 +208,22 @@ func TestBlockHistoryEstimator_Start(t *testing.T) { ethClient.On("HeadByNumber", mock.Anything, (*big.Int)(nil)).Return(h, nil) ethClient.On("BatchCallContext", mock.Anything, mock.Anything).Return(pkgerrors.New("something went wrong")) - err := bhe.Start(testutils.Context(t)) + err := bhe.Start(tests.Context(t)) require.NoError(t, err) assert.Equal(t, assets.NewWeiI(420), gas.GetLatestBaseFee(bhe)) - _, _, err = bhe.GetLegacyGas(testutils.Context(t), make([]byte, 0), 100, maxGasPrice) + _, _, err = bhe.GetLegacyGas(tests.Context(t), make([]byte, 0), 100, maxGasPrice) require.Error(t, err) require.Contains(t, err.Error(), "has not finished the first gas estimation yet, likely because a failure on start") - _, err = bhe.GetDynamicFee(testutils.Context(t), maxGasPrice) + _, err = bhe.GetDynamicFee(tests.Context(t), maxGasPrice) require.Error(t, err) require.Contains(t, err.Error(), "has not finished the first gas estimation yet, likely because a failure on start") }) t.Run("returns error if main context is cancelled", func(t *testing.T) { - ethClient := evmtest.NewEthClientMockWithDefaultChain(t) + ethClient := testutils.NewEthClientMockWithDefaultChain(t) l1Oracle := rollupMocks.NewL1Oracle(t) bhe := newBlockHistoryEstimator(t, ethClient, cfg, geCfg, bhCfg, l1Oracle) @@ -231,7 +232,7 @@ func TestBlockHistoryEstimator_Start(t *testing.T) { ethClient.On("HeadByNumber", mock.Anything, (*big.Int)(nil)).Return(h, nil) ethClient.On("BatchCallContext", mock.Anything, mock.Anything).Return(pkgerrors.New("this error doesn't matter")) - ctx, cancel := context.WithCancel(testutils.Context(t)) + ctx, cancel := context.WithCancel(tests.Context(t)) cancel() err := bhe.Start(ctx) require.Error(t, err) @@ -239,7 +240,7 @@ func TestBlockHistoryEstimator_Start(t *testing.T) { }) t.Run("starts anyway even if the fetch context is cancelled due to taking longer than the MaxStartTime", func(t *testing.T) { - ethClient := evmtest.NewEthClientMockWithDefaultChain(t) + ethClient := testutils.NewEthClientMockWithDefaultChain(t) l1Oracle := rollupMocks.NewL1Oracle(t) bhe := newBlockHistoryEstimator(t, ethClient, cfg, geCfg, bhCfg, l1Oracle) @@ -250,16 +251,16 @@ func TestBlockHistoryEstimator_Start(t *testing.T) { time.Sleep(gas.MaxStartTime + 1*time.Second) }) - err := bhe.Start(testutils.Context(t)) + err := bhe.Start(tests.Context(t)) require.NoError(t, err) assert.Equal(t, assets.NewWeiI(420), gas.GetLatestBaseFee(bhe)) - _, _, err = bhe.GetLegacyGas(testutils.Context(t), make([]byte, 0), 100, maxGasPrice) + _, _, err = bhe.GetLegacyGas(tests.Context(t), make([]byte, 0), 100, maxGasPrice) require.Error(t, err) require.Contains(t, err.Error(), "has not finished the first gas estimation yet, likely because a failure on start") - _, err = bhe.GetDynamicFee(testutils.Context(t), maxGasPrice) + _, err = bhe.GetDynamicFee(tests.Context(t), maxGasPrice) require.Error(t, err) require.Contains(t, err.Error(), "has not finished the first gas estimation yet, likely because a failure on start") }) @@ -277,14 +278,14 @@ func TestBlockHistoryEstimator_OnNewLongestChain(t *testing.T) { assert.Nil(t, gas.GetLatestBaseFee(bhe)) // non EIP-1559 block - h := cltest.Head(1) - bhe.OnNewLongestChain(testutils.Context(t), h) + h := testutils.Head(1) + bhe.OnNewLongestChain(tests.Context(t), h) assert.Nil(t, gas.GetLatestBaseFee(bhe)) // EIP-1559 block - h = cltest.Head(2) + h = testutils.Head(2) h.BaseFeePerGas = assets.NewWeiI(500) - bhe.OnNewLongestChain(testutils.Context(t), h) + bhe.OnNewLongestChain(tests.Context(t), h) assert.Equal(t, assets.NewWeiI(500), gas.GetLatestBaseFee(bhe)) } @@ -293,7 +294,7 @@ func TestBlockHistoryEstimator_FetchBlocks(t *testing.T) { t.Parallel() t.Run("with history size of 0, errors", func(t *testing.T) { - ethClient := evmtest.NewEthClientMockWithDefaultChain(t) + ethClient := testutils.NewEthClientMockWithDefaultChain(t) l1Oracle := rollupMocks.NewL1Oracle(t) cfg := gas.NewMockConfig() @@ -309,14 +310,14 @@ func TestBlockHistoryEstimator_FetchBlocks(t *testing.T) { bhe := newBlockHistoryEstimator(t, ethClient, cfg, geCfg, bhCfg, l1Oracle) - head := cltest.Head(42) - err := bhe.FetchBlocks(testutils.Context(t), head) + head := testutils.Head(42) + err := bhe.FetchBlocks(tests.Context(t), head) require.Error(t, err) require.EqualError(t, err, "BlockHistoryEstimator: history size must be > 0, got: 0") }) t.Run("with current block height less than block delay does nothing", func(t *testing.T) { - ethClient := evmtest.NewEthClientMockWithDefaultChain(t) + ethClient := testutils.NewEthClientMockWithDefaultChain(t) l1Oracle := rollupMocks.NewL1Oracle(t) cfg := gas.NewMockConfig() @@ -332,15 +333,15 @@ func TestBlockHistoryEstimator_FetchBlocks(t *testing.T) { bhe := newBlockHistoryEstimator(t, ethClient, cfg, geCfg, bhCfg, l1Oracle) for i := -1; i < 3; i++ { - head := cltest.Head(i) - err := bhe.FetchBlocks(testutils.Context(t), head) + head := testutils.Head(i) + err := bhe.FetchBlocks(tests.Context(t), head) require.Error(t, err) require.EqualError(t, err, fmt.Sprintf("BlockHistoryEstimator: cannot fetch, current block height %v is lower than EVM.RPCBlockQueryDelay=3", i)) } }) t.Run("with error retrieving blocks returns error", func(t *testing.T) { - ethClient := evmtest.NewEthClientMockWithDefaultChain(t) + ethClient := testutils.NewEthClientMockWithDefaultChain(t) l1Oracle := rollupMocks.NewL1Oracle(t) cfg := gas.NewMockConfig() @@ -358,13 +359,13 @@ func TestBlockHistoryEstimator_FetchBlocks(t *testing.T) { ethClient.On("BatchCallContext", mock.Anything, mock.Anything).Return(pkgerrors.New("something exploded")) - err := bhe.FetchBlocks(testutils.Context(t), cltest.Head(42)) + err := bhe.FetchBlocks(tests.Context(t), testutils.Head(42)) require.Error(t, err) assert.EqualError(t, err, "BlockHistoryEstimator#fetchBlocks error fetching blocks with BatchCallContext: something exploded") }) t.Run("batch fetches heads and transactions and sets them on the block history estimator instance", func(t *testing.T) { - ethClient := evmtest.NewEthClientMockWithDefaultChain(t) + ethClient := testutils.NewEthClientMockWithDefaultChain(t) l1Oracle := rollupMocks.NewL1Oracle(t) cfg := gas.NewMockConfig() @@ -385,17 +386,17 @@ func TestBlockHistoryEstimator_FetchBlocks(t *testing.T) { b41 := evmtypes.Block{ Number: 41, Hash: utils.NewHash(), - Transactions: cltest.LegacyTransactionsFromGasPrices(1, 2), + Transactions: legacyTransactionsFromGasPrices(1, 2), } b42 := evmtypes.Block{ Number: 42, Hash: utils.NewHash(), - Transactions: cltest.LegacyTransactionsFromGasPrices(3), + Transactions: legacyTransactionsFromGasPrices(3), } b43 := evmtypes.Block{ Number: 43, Hash: utils.NewHash(), - Transactions: cltest.LegacyTransactionsFromGasPrices(), + Transactions: legacyTransactionsFromGasPrices(), } ethClient.On("BatchCallContext", mock.Anything, mock.MatchedBy(func(b []rpc.BatchElem) bool { @@ -416,7 +417,7 @@ func TestBlockHistoryEstimator_FetchBlocks(t *testing.T) { elems[0].Result = &b41 }) - err := bhe.FetchBlocks(testutils.Context(t), cltest.Head(43)) + err := bhe.FetchBlocks(tests.Context(t), testutils.Head(43)) require.NoError(t, err) require.Len(t, gas.GetRollingBlockHistory(bhe), 2) @@ -431,7 +432,7 @@ func TestBlockHistoryEstimator_FetchBlocks(t *testing.T) { b44 := evmtypes.Block{ Number: 44, Hash: utils.NewHash(), - Transactions: cltest.LegacyTransactionsFromGasPrices(4), + Transactions: legacyTransactionsFromGasPrices(4), } // We are gonna refetch blocks 42 and 44 @@ -446,8 +447,8 @@ func TestBlockHistoryEstimator_FetchBlocks(t *testing.T) { elems[1].Result = &b42 }) - head := evmtypes.NewHead(big.NewInt(44), b44.Hash, b43.Hash, uint64(time.Now().Unix()), ubig.New(&cltest.FixtureChainID)) - err = bhe.FetchBlocks(testutils.Context(t), &head) + head := evmtypes.NewHead(big.NewInt(44), b44.Hash, b43.Hash, uint64(time.Now().Unix()), ubig.New(testutils.FixtureChainID)) + err = bhe.FetchBlocks(tests.Context(t), &head) require.NoError(t, err) require.Len(t, gas.GetRollingBlockHistory(bhe), 3) @@ -460,7 +461,7 @@ func TestBlockHistoryEstimator_FetchBlocks(t *testing.T) { }) t.Run("does not refetch blocks below EVM.FinalityDepth", func(t *testing.T) { - ethClient := evmtest.NewEthClientMockWithDefaultChain(t) + ethClient := testutils.NewEthClientMockWithDefaultChain(t) l1Oracle := rollupMocks.NewL1Oracle(t) cfg := gas.NewMockConfig() @@ -480,12 +481,12 @@ func TestBlockHistoryEstimator_FetchBlocks(t *testing.T) { b0 := evmtypes.Block{ Number: 0, Hash: utils.NewHash(), - Transactions: cltest.LegacyTransactionsFromGasPrices(9001), + Transactions: legacyTransactionsFromGasPrices(9001), } b1 := evmtypes.Block{ Number: 1, Hash: utils.NewHash(), - Transactions: cltest.LegacyTransactionsFromGasPrices(9002), + Transactions: legacyTransactionsFromGasPrices(9002), } blocks := []evmtypes.Block{b0, b1} @@ -494,12 +495,12 @@ func TestBlockHistoryEstimator_FetchBlocks(t *testing.T) { b2 := evmtypes.Block{ Number: 2, Hash: utils.NewHash(), - Transactions: cltest.LegacyTransactionsFromGasPrices(1, 2), + Transactions: legacyTransactionsFromGasPrices(1, 2), } b3 := evmtypes.Block{ Number: 3, Hash: utils.NewHash(), - Transactions: cltest.LegacyTransactionsFromGasPrices(1, 2), + Transactions: legacyTransactionsFromGasPrices(1, 2), } ethClient.On("BatchCallContext", mock.Anything, mock.MatchedBy(func(b []rpc.BatchElem) bool { @@ -512,10 +513,10 @@ func TestBlockHistoryEstimator_FetchBlocks(t *testing.T) { elems[1].Result = &b2 }) - head2 := evmtypes.NewHead(big.NewInt(2), b2.Hash, b1.Hash, uint64(time.Now().Unix()), ubig.New(&cltest.FixtureChainID)) - head3 := evmtypes.NewHead(big.NewInt(3), b3.Hash, b2.Hash, uint64(time.Now().Unix()), ubig.New(&cltest.FixtureChainID)) + head2 := evmtypes.NewHead(big.NewInt(2), b2.Hash, b1.Hash, uint64(time.Now().Unix()), ubig.New(testutils.FixtureChainID)) + head3 := evmtypes.NewHead(big.NewInt(3), b3.Hash, b2.Hash, uint64(time.Now().Unix()), ubig.New(testutils.FixtureChainID)) head3.Parent = &head2 - err := bhe.FetchBlocks(testutils.Context(t), &head3) + err := bhe.FetchBlocks(tests.Context(t), &head3) require.NoError(t, err) require.Len(t, gas.GetRollingBlockHistory(bhe), 3) @@ -525,7 +526,7 @@ func TestBlockHistoryEstimator_FetchBlocks(t *testing.T) { }) t.Run("replaces blocks on re-org within EVM.FinalityDepth", func(t *testing.T) { - ethClient := evmtest.NewEthClientMockWithDefaultChain(t) + ethClient := testutils.NewEthClientMockWithDefaultChain(t) l1Oracle := rollupMocks.NewL1Oracle(t) cfg := gas.NewMockConfig() @@ -545,30 +546,30 @@ func TestBlockHistoryEstimator_FetchBlocks(t *testing.T) { b0 := evmtypes.Block{ Number: 0, Hash: utils.NewHash(), - Transactions: cltest.LegacyTransactionsFromGasPrices(9001), + Transactions: legacyTransactionsFromGasPrices(9001), } b1 := evmtypes.Block{ Number: 1, Hash: utils.NewHash(), - Transactions: cltest.LegacyTransactionsFromGasPrices(9002), + Transactions: legacyTransactionsFromGasPrices(9002), } b2 := evmtypes.Block{ Number: 2, Hash: utils.NewHash(), - Transactions: cltest.LegacyTransactionsFromGasPrices(1, 2), + Transactions: legacyTransactionsFromGasPrices(1, 2), } b3 := evmtypes.Block{ Number: 3, Hash: utils.NewHash(), - Transactions: cltest.LegacyTransactionsFromGasPrices(1, 2), + Transactions: legacyTransactionsFromGasPrices(1, 2), } blocks := []evmtypes.Block{b0, b1, b2, b3} gas.SetRollingBlockHistory(bhe, blocks) // RE-ORG, head2 and head3 have different hash than saved b2 and b3 - head2 := evmtypes.NewHead(big.NewInt(2), utils.NewHash(), b1.Hash, uint64(time.Now().Unix()), ubig.New(&cltest.FixtureChainID)) - head3 := evmtypes.NewHead(big.NewInt(3), utils.NewHash(), head2.Hash, uint64(time.Now().Unix()), ubig.New(&cltest.FixtureChainID)) + head2 := evmtypes.NewHead(big.NewInt(2), utils.NewHash(), b1.Hash, uint64(time.Now().Unix()), ubig.New(testutils.FixtureChainID)) + head3 := evmtypes.NewHead(big.NewInt(3), utils.NewHash(), head2.Hash, uint64(time.Now().Unix()), ubig.New(testutils.FixtureChainID)) head3.Parent = &head2 ethClient.On("BatchCallContext", mock.Anything, mock.MatchedBy(func(b []rpc.BatchElem) bool { @@ -585,7 +586,7 @@ func TestBlockHistoryEstimator_FetchBlocks(t *testing.T) { elems[0].Result = &b3New }) - err := bhe.FetchBlocks(testutils.Context(t), &head3) + err := bhe.FetchBlocks(tests.Context(t), &head3) require.NoError(t, err) require.Len(t, gas.GetRollingBlockHistory(bhe), 3) @@ -598,7 +599,7 @@ func TestBlockHistoryEstimator_FetchBlocks(t *testing.T) { }) t.Run("uses locally cached blocks if they are in the chain", func(t *testing.T) { - ethClient := evmtest.NewEthClientMockWithDefaultChain(t) + ethClient := testutils.NewEthClientMockWithDefaultChain(t) l1Oracle := rollupMocks.NewL1Oracle(t) cfg := gas.NewMockConfig() @@ -618,33 +619,33 @@ func TestBlockHistoryEstimator_FetchBlocks(t *testing.T) { b0 := evmtypes.Block{ Number: 0, Hash: utils.NewHash(), - Transactions: cltest.LegacyTransactionsFromGasPrices(9001), + Transactions: legacyTransactionsFromGasPrices(9001), } b1 := evmtypes.Block{ Number: 1, Hash: utils.NewHash(), - Transactions: cltest.LegacyTransactionsFromGasPrices(9002), + Transactions: legacyTransactionsFromGasPrices(9002), } b2 := evmtypes.Block{ Number: 2, Hash: utils.NewHash(), - Transactions: cltest.LegacyTransactionsFromGasPrices(1, 2), + Transactions: legacyTransactionsFromGasPrices(1, 2), } b3 := evmtypes.Block{ Number: 3, Hash: utils.NewHash(), - Transactions: cltest.LegacyTransactionsFromGasPrices(1, 2), + Transactions: legacyTransactionsFromGasPrices(1, 2), } blocks := []evmtypes.Block{b0, b1, b2, b3} gas.SetRollingBlockHistory(bhe, blocks) // head2 and head3 have identical hash to saved blocks - head2 := evmtypes.NewHead(big.NewInt(2), b2.Hash, b1.Hash, uint64(time.Now().Unix()), ubig.New(&cltest.FixtureChainID)) - head3 := evmtypes.NewHead(big.NewInt(3), b3.Hash, head2.Hash, uint64(time.Now().Unix()), ubig.New(&cltest.FixtureChainID)) + head2 := evmtypes.NewHead(big.NewInt(2), b2.Hash, b1.Hash, uint64(time.Now().Unix()), ubig.New(testutils.FixtureChainID)) + head3 := evmtypes.NewHead(big.NewInt(3), b3.Hash, head2.Hash, uint64(time.Now().Unix()), ubig.New(testutils.FixtureChainID)) head3.Parent = &head2 - err := bhe.FetchBlocks(testutils.Context(t), &head3) + err := bhe.FetchBlocks(tests.Context(t), &head3) require.NoError(t, err) require.Len(t, gas.GetRollingBlockHistory(bhe), 3) @@ -657,7 +658,7 @@ func TestBlockHistoryEstimator_FetchBlocks(t *testing.T) { }) t.Run("fetches max(BlockHistoryEstimatorCheckInclusionBlocks, BlockHistoryEstimatorBlockHistorySize)", func(t *testing.T) { - ethClient := evmtest.NewEthClientMockWithDefaultChain(t) + ethClient := testutils.NewEthClientMockWithDefaultChain(t) l1Oracle := rollupMocks.NewL1Oracle(t) cfg := gas.NewMockConfig() @@ -679,12 +680,12 @@ func TestBlockHistoryEstimator_FetchBlocks(t *testing.T) { b42 := evmtypes.Block{ Number: 42, Hash: utils.NewHash(), - Transactions: cltest.LegacyTransactionsFromGasPrices(3), + Transactions: legacyTransactionsFromGasPrices(3), } b43 := evmtypes.Block{ Number: 43, Hash: utils.NewHash(), - Transactions: cltest.LegacyTransactionsFromGasPrices(), + Transactions: legacyTransactionsFromGasPrices(), } ethClient.On("BatchCallContext", mock.Anything, mock.MatchedBy(func(b []rpc.BatchElem) bool { @@ -697,7 +698,7 @@ func TestBlockHistoryEstimator_FetchBlocks(t *testing.T) { elems[1].Result = &b42 }) - err := bhe.FetchBlocks(testutils.Context(t), cltest.Head(43)) + err := bhe.FetchBlocks(tests.Context(t), testutils.Head(43)) require.NoError(t, err) require.Len(t, gas.GetRollingBlockHistory(bhe), 2) @@ -711,7 +712,7 @@ func TestBlockHistoryEstimator_FetchBlocks(t *testing.T) { func TestBlockHistoryEstimator_FetchBlocksAndRecalculate_NoEIP1559(t *testing.T) { t.Parallel() - ethClient := evmtest.NewEthClientMockWithDefaultChain(t) + ethClient := testutils.NewEthClientMockWithDefaultChain(t) l1Oracle := rollupMocks.NewL1Oracle(t) cfg := gas.NewMockConfig() @@ -731,17 +732,17 @@ func TestBlockHistoryEstimator_FetchBlocksAndRecalculate_NoEIP1559(t *testing.T) b1 := evmtypes.Block{ Number: 1, Hash: utils.NewHash(), - Transactions: cltest.LegacyTransactionsFromGasPrices(1), + Transactions: legacyTransactionsFromGasPrices(1), } b2 := evmtypes.Block{ Number: 2, Hash: utils.NewHash(), - Transactions: cltest.LegacyTransactionsFromGasPrices(2), + Transactions: legacyTransactionsFromGasPrices(2), } b3 := evmtypes.Block{ Number: 3, Hash: utils.NewHash(), - Transactions: cltest.LegacyTransactionsFromGasPrices(200, 300, 100, 100, 100, 100), + Transactions: legacyTransactionsFromGasPrices(200, 300, 100, 100, 100, 100), } ethClient.On("BatchCallContext", mock.Anything, mock.MatchedBy(func(b []rpc.BatchElem) bool { @@ -756,7 +757,7 @@ func TestBlockHistoryEstimator_FetchBlocksAndRecalculate_NoEIP1559(t *testing.T) elems[2].Result = &b1 }) - bhe.FetchBlocksAndRecalculate(testutils.Context(t), cltest.Head(3)) + bhe.FetchBlocksAndRecalculate(tests.Context(t), testutils.Head(3)) price := gas.GetGasPrice(bhe) require.Equal(t, assets.NewWeiI(100), price) @@ -771,7 +772,7 @@ func TestBlockHistoryEstimator_Recalculate_NoEIP1559(t *testing.T) { minGasPrice := assets.NewWeiI(10) t.Run("does not crash or set gas price to zero if there are no transactions", func(t *testing.T) { - ethClient := evmtest.NewEthClientMockWithDefaultChain(t) + ethClient := testutils.NewEthClientMockWithDefaultChain(t) l1Oracle := rollupMocks.NewL1Oracle(t) cfg := gas.NewMockConfig() @@ -786,19 +787,19 @@ func TestBlockHistoryEstimator_Recalculate_NoEIP1559(t *testing.T) { blocks := []evmtypes.Block{} gas.SetRollingBlockHistory(bhe, blocks) - bhe.Recalculate(cltest.Head(1)) + bhe.Recalculate(testutils.Head(1)) blocks = []evmtypes.Block{{}} gas.SetRollingBlockHistory(bhe, blocks) - bhe.Recalculate(cltest.Head(1)) + bhe.Recalculate(testutils.Head(1)) blocks = []evmtypes.Block{{Transactions: []evmtypes.Transaction{}}} gas.SetRollingBlockHistory(bhe, blocks) - bhe.Recalculate(cltest.Head(1)) + bhe.Recalculate(testutils.Head(1)) }) t.Run("sets gas price to EVM.GasEstimator.PriceMax if the calculation would otherwise exceed it", func(t *testing.T) { - ethClient := evmtest.NewEthClientMockWithDefaultChain(t) + ethClient := testutils.NewEthClientMockWithDefaultChain(t) l1Oracle := rollupMocks.NewL1Oracle(t) cfg := gas.NewMockConfig() @@ -817,25 +818,25 @@ func TestBlockHistoryEstimator_Recalculate_NoEIP1559(t *testing.T) { { Number: 0, Hash: utils.NewHash(), - Transactions: cltest.LegacyTransactionsFromGasPrices(9001), + Transactions: legacyTransactionsFromGasPrices(9001), }, { Number: 1, Hash: utils.NewHash(), - Transactions: cltest.LegacyTransactionsFromGasPrices(9002), + Transactions: legacyTransactionsFromGasPrices(9002), }, } gas.SetRollingBlockHistory(bhe, blocks) - bhe.Recalculate(cltest.Head(1)) + bhe.Recalculate(testutils.Head(1)) price := gas.GetGasPrice(bhe) require.Equal(t, maxGasPrice, price) }) t.Run("sets gas price to EVM.GasEstimator.PriceMin if the calculation would otherwise fall below it", func(t *testing.T) { - ethClient := evmtest.NewEthClientMockWithDefaultChain(t) + ethClient := testutils.NewEthClientMockWithDefaultChain(t) l1Oracle := rollupMocks.NewL1Oracle(t) cfg := gas.NewMockConfig() @@ -854,25 +855,25 @@ func TestBlockHistoryEstimator_Recalculate_NoEIP1559(t *testing.T) { { Number: 0, Hash: utils.NewHash(), - Transactions: cltest.LegacyTransactionsFromGasPrices(5), + Transactions: legacyTransactionsFromGasPrices(5), }, { Number: 1, Hash: utils.NewHash(), - Transactions: cltest.LegacyTransactionsFromGasPrices(7), + Transactions: legacyTransactionsFromGasPrices(7), }, } gas.SetRollingBlockHistory(bhe, blocks) - bhe.Recalculate(cltest.Head(1)) + bhe.Recalculate(testutils.Head(1)) price := gas.GetGasPrice(bhe) require.Equal(t, minGasPrice, price) }) t.Run("ignores any transaction with a zero gas limit", func(t *testing.T) { - ethClient := evmtest.NewEthClientMockWithDefaultChain(t) + ethClient := testutils.NewEthClientMockWithDefaultChain(t) l1Oracle := rollupMocks.NewL1Oracle(t) cfg := gas.NewMockConfig() @@ -895,7 +896,7 @@ func TestBlockHistoryEstimator_Recalculate_NoEIP1559(t *testing.T) { Number: 0, Hash: b1Hash, ParentHash: common.Hash{}, - Transactions: cltest.LegacyTransactionsFromGasPrices(50), + Transactions: legacyTransactionsFromGasPrices(50), }, { Number: 1, @@ -913,7 +914,7 @@ func TestBlockHistoryEstimator_Recalculate_NoEIP1559(t *testing.T) { gas.SetRollingBlockHistory(bhe, blocks) - bhe.Recalculate(cltest.Head(2)) + bhe.Recalculate(testutils.Head(2)) price := gas.GetGasPrice(bhe) require.Equal(t, assets.NewWeiI(70), price) @@ -921,7 +922,7 @@ func TestBlockHistoryEstimator_Recalculate_NoEIP1559(t *testing.T) { t.Run("takes into account zero priced transactions if chain is not Gnosis", func(t *testing.T) { // Because everyone loves free gas! - ethClient := evmtest.NewEthClientMockWithDefaultChain(t) + ethClient := testutils.NewEthClientMockWithDefaultChain(t) l1Oracle := rollupMocks.NewL1Oracle(t) cfg := gas.NewMockConfig() @@ -943,20 +944,20 @@ func TestBlockHistoryEstimator_Recalculate_NoEIP1559(t *testing.T) { Number: 0, Hash: b1Hash, ParentHash: common.Hash{}, - Transactions: cltest.LegacyTransactionsFromGasPrices(0, 0, 25, 50, 100), + Transactions: legacyTransactionsFromGasPrices(0, 0, 25, 50, 100), }, } gas.SetRollingBlockHistory(bhe, blocks) - bhe.Recalculate(cltest.Head(0)) + bhe.Recalculate(testutils.Head(0)) price := gas.GetGasPrice(bhe) require.Equal(t, assets.NewWeiI(25), price) }) t.Run("ignores zero priced transactions only on Gnosis", func(t *testing.T) { - ethClient := evmtest.NewEthClientMock(t) + ethClient := evmmocks.NewClient(t) l1Oracle := rollupMocks.NewL1Oracle(t) cfg := gas.NewMockConfig() @@ -979,35 +980,35 @@ func TestBlockHistoryEstimator_Recalculate_NoEIP1559(t *testing.T) { Number: 0, Hash: b1Hash, ParentHash: common.Hash{}, - Transactions: cltest.LegacyTransactionsFromGasPrices(0, 0, 0, 0, 80), + Transactions: legacyTransactionsFromGasPrices(0, 0, 0, 0, 80), }, } gas.SetRollingBlockHistory(bhe, blocks) // chainType is not set - GasEstimator should not ignore zero priced transactions and instead default to PriceMin==11 - bhe.Recalculate(cltest.Head(0)) + bhe.Recalculate(testutils.Head(0)) require.Equal(t, assets.NewWeiI(11), gas.GetGasPrice(bhe)) // Set chainType to Gnosis - GasEstimator should now ignore zero priced transactions cfg.ChainTypeF = string(config.ChainGnosis) - bhe.Recalculate(cltest.Head(0)) + bhe.Recalculate(testutils.Head(0)) require.Equal(t, assets.NewWeiI(80), gas.GetGasPrice(bhe)) // Same for xDai (deprecated) cfg.ChainTypeF = string(config.ChainXDai) - bhe.Recalculate(cltest.Head(0)) + bhe.Recalculate(testutils.Head(0)) require.Equal(t, assets.NewWeiI(80), gas.GetGasPrice(bhe)) // And for X Layer cfg.ChainTypeF = string(config.ChainXLayer) - bhe.Recalculate(cltest.Head(0)) + bhe.Recalculate(testutils.Head(0)) require.Equal(t, assets.NewWeiI(80), gas.GetGasPrice(bhe)) }) t.Run("handles unreasonably large gas prices (larger than a 64 bit int can hold)", func(t *testing.T) { // Seems unlikely we will ever experience gas prices > 9 Petawei on mainnet (praying to the eth Gods 🙏) // But other chains could easily use a different base of account - ethClient := evmtest.NewEthClientMockWithDefaultChain(t) + ethClient := testutils.NewEthClientMockWithDefaultChain(t) l1Oracle := rollupMocks.NewL1Oracle(t) cfg := gas.NewMockConfig() @@ -1049,14 +1050,14 @@ func TestBlockHistoryEstimator_Recalculate_NoEIP1559(t *testing.T) { gas.SetRollingBlockHistory(bhe, blocks) - bhe.Recalculate(cltest.Head(0)) + bhe.Recalculate(testutils.Head(0)) price := gas.GetGasPrice(bhe) require.Equal(t, reasonablyHugeGasPrice, price) }) t.Run("doesn't panic if gas price is nil (although I'm still unsure how this can happen)", func(t *testing.T) { - ethClient := evmtest.NewEthClientMockWithDefaultChain(t) + ethClient := testutils.NewEthClientMockWithDefaultChain(t) l1Oracle := rollupMocks.NewL1Oracle(t) cfg := gas.NewMockConfig() @@ -1087,7 +1088,7 @@ func TestBlockHistoryEstimator_Recalculate_NoEIP1559(t *testing.T) { gas.SetRollingBlockHistory(bhe, blocks) - bhe.Recalculate(cltest.Head(0)) + bhe.Recalculate(testutils.Head(0)) price := gas.GetGasPrice(bhe) require.Equal(t, assets.NewWeiI(100), price) @@ -1104,7 +1105,7 @@ func TestBlockHistoryEstimator_Recalculate_EIP1559(t *testing.T) { maxGasPrice := assets.NewWeiI(100) t.Run("does not crash or set gas price to zero if there are no transactions", func(t *testing.T) { - ethClient := evmtest.NewEthClientMockWithDefaultChain(t) + ethClient := testutils.NewEthClientMockWithDefaultChain(t) l1Oracle := rollupMocks.NewL1Oracle(t) cfg := gas.NewMockConfig() @@ -1119,31 +1120,31 @@ func TestBlockHistoryEstimator_Recalculate_EIP1559(t *testing.T) { blocks := []evmtypes.Block{} gas.SetRollingBlockHistory(bhe, blocks) - bhe.Recalculate(cltest.Head(1)) + bhe.Recalculate(testutils.Head(1)) blocks = []evmtypes.Block{{}} // No base fee (doesn't crash) gas.SetRollingBlockHistory(bhe, blocks) - bhe.Recalculate(cltest.Head(1)) + bhe.Recalculate(testutils.Head(1)) blocks = []evmtypes.Block{newBlockWithBaseFee()} gas.SetRollingBlockHistory(bhe, blocks) - bhe.Recalculate(cltest.Head(1)) + bhe.Recalculate(testutils.Head(1)) empty := newBlockWithBaseFee() empty.Transactions = []evmtypes.Transaction{} blocks = []evmtypes.Block{empty} gas.SetRollingBlockHistory(bhe, blocks) - bhe.Recalculate(cltest.Head(1)) + bhe.Recalculate(testutils.Head(1)) withOnlyLegacyTransactions := newBlockWithBaseFee() - withOnlyLegacyTransactions.Transactions = cltest.LegacyTransactionsFromGasPrices(9001) + withOnlyLegacyTransactions.Transactions = legacyTransactionsFromGasPrices(9001) blocks = []evmtypes.Block{withOnlyLegacyTransactions} gas.SetRollingBlockHistory(bhe, blocks) - bhe.Recalculate(cltest.Head(1)) + bhe.Recalculate(testutils.Head(1)) }) t.Run("does not set tip higher than EVM.GasEstimator.PriceMax", func(t *testing.T) { - ethClient := evmtest.NewEthClientMockWithDefaultChain(t) + ethClient := testutils.NewEthClientMockWithDefaultChain(t) l1Oracle := rollupMocks.NewL1Oracle(t) cfg := gas.NewMockConfig() @@ -1164,26 +1165,26 @@ func TestBlockHistoryEstimator_Recalculate_EIP1559(t *testing.T) { BaseFeePerGas: assets.NewWeiI(1), Number: 0, Hash: utils.NewHash(), - Transactions: cltest.DynamicFeeTransactionsFromTipCaps(9001), + Transactions: dynamicFeeTransactionsFromTipCaps(9001), }, { BaseFeePerGas: assets.NewWeiI(1), Number: 1, Hash: utils.NewHash(), - Transactions: cltest.DynamicFeeTransactionsFromTipCaps(9002), + Transactions: dynamicFeeTransactionsFromTipCaps(9002), }, } gas.SetRollingBlockHistory(bhe, blocks) - bhe.Recalculate(cltest.Head(1)) + bhe.Recalculate(testutils.Head(1)) tipCap := gas.GetTipCap(bhe) require.Equal(t, tipCap.Int64(), maxGasPrice.Int64()) }) t.Run("sets tip cap to EVM.GasEstimator.TipCapMin if the calculation would otherwise fall below it", func(t *testing.T) { - ethClient := evmtest.NewEthClientMockWithDefaultChain(t) + ethClient := testutils.NewEthClientMockWithDefaultChain(t) l1Oracle := rollupMocks.NewL1Oracle(t) cfg := gas.NewMockConfig() @@ -1204,26 +1205,26 @@ func TestBlockHistoryEstimator_Recalculate_EIP1559(t *testing.T) { BaseFeePerGas: assets.NewWeiI(1), Number: 0, Hash: utils.NewHash(), - Transactions: cltest.DynamicFeeTransactionsFromTipCaps(5), + Transactions: dynamicFeeTransactionsFromTipCaps(5), }, { BaseFeePerGas: assets.NewWeiI(1), Number: 1, Hash: utils.NewHash(), - Transactions: cltest.DynamicFeeTransactionsFromTipCaps(7), + Transactions: dynamicFeeTransactionsFromTipCaps(7), }, } gas.SetRollingBlockHistory(bhe, blocks) - bhe.Recalculate(cltest.Head(1)) + bhe.Recalculate(testutils.Head(1)) price := gas.GetTipCap(bhe) require.Equal(t, assets.NewWeiI(10), price) }) t.Run("ignores any transaction with a zero gas limit", func(t *testing.T) { - ethClient := evmtest.NewEthClientMockWithDefaultChain(t) + ethClient := testutils.NewEthClientMockWithDefaultChain(t) l1Oracle := rollupMocks.NewL1Oracle(t) cfg := gas.NewMockConfig() @@ -1247,7 +1248,7 @@ func TestBlockHistoryEstimator_Recalculate_EIP1559(t *testing.T) { Number: 0, Hash: b1Hash, ParentHash: common.Hash{}, - Transactions: cltest.LegacyTransactionsFromGasPrices(50), + Transactions: legacyTransactionsFromGasPrices(50), }, { BaseFeePerGas: assets.NewWeiI(10), @@ -1266,14 +1267,14 @@ func TestBlockHistoryEstimator_Recalculate_EIP1559(t *testing.T) { gas.SetRollingBlockHistory(bhe, blocks) - bhe.Recalculate(cltest.Head(2)) + bhe.Recalculate(testutils.Head(2)) price := gas.GetTipCap(bhe) require.Equal(t, assets.NewWeiI(60), price) }) t.Run("respects minimum gas tip cap", func(t *testing.T) { - ethClient := evmtest.NewEthClientMockWithDefaultChain(t) + ethClient := testutils.NewEthClientMockWithDefaultChain(t) l1Oracle := rollupMocks.NewL1Oracle(t) cfg := gas.NewMockConfig() @@ -1297,13 +1298,13 @@ func TestBlockHistoryEstimator_Recalculate_EIP1559(t *testing.T) { Number: 0, Hash: b1Hash, ParentHash: common.Hash{}, - Transactions: cltest.DynamicFeeTransactionsFromTipCaps(0, 0, 0, 0, 100), + Transactions: dynamicFeeTransactionsFromTipCaps(0, 0, 0, 0, 100), }, } gas.SetRollingBlockHistory(bhe, blocks) - bhe.Recalculate(cltest.Head(0)) + bhe.Recalculate(testutils.Head(0)) price := gas.GetTipCap(bhe) assert.Equal(t, assets.NewWeiI(1), price) @@ -1311,7 +1312,7 @@ func TestBlockHistoryEstimator_Recalculate_EIP1559(t *testing.T) { t.Run("allows to set zero tip cap if minimum allows it", func(t *testing.T) { // Because everyone loves *cheap* gas! - ethClient := evmtest.NewEthClientMockWithDefaultChain(t) + ethClient := testutils.NewEthClientMockWithDefaultChain(t) l1Oracle := rollupMocks.NewL1Oracle(t) cfg := gas.NewMockConfig() @@ -1335,13 +1336,13 @@ func TestBlockHistoryEstimator_Recalculate_EIP1559(t *testing.T) { Number: 0, Hash: b1Hash, ParentHash: common.Hash{}, - Transactions: cltest.DynamicFeeTransactionsFromTipCaps(0, 0, 0, 0, 100), + Transactions: dynamicFeeTransactionsFromTipCaps(0, 0, 0, 0, 100), }, } gas.SetRollingBlockHistory(bhe, blocks) - bhe.Recalculate(cltest.Head(0)) + bhe.Recalculate(testutils.Head(0)) price := gas.GetTipCap(bhe) require.Equal(t, assets.NewWeiI(0), price) @@ -1349,7 +1350,7 @@ func TestBlockHistoryEstimator_Recalculate_EIP1559(t *testing.T) { } func TestBlockHistoryEstimator_IsUsable(t *testing.T) { - ethClient := evmtest.NewEthClientMockWithDefaultChain(t) + ethClient := testutils.NewEthClientMockWithDefaultChain(t) l1Oracle := rollupMocks.NewL1Oracle(t) cfg := gas.NewMockConfig() @@ -1433,7 +1434,7 @@ func TestBlockHistoryEstimator_IsUsable(t *testing.T) { } func TestBlockHistoryEstimator_EffectiveTipCap(t *testing.T) { - ethClient := evmtest.NewEthClientMockWithDefaultChain(t) + ethClient := testutils.NewEthClientMockWithDefaultChain(t) l1Oracle := rollupMocks.NewL1Oracle(t) cfg := gas.NewMockConfig() @@ -1495,7 +1496,7 @@ func TestBlockHistoryEstimator_EffectiveTipCap(t *testing.T) { } func TestBlockHistoryEstimator_EffectiveGasPrice(t *testing.T) { - ethClient := evmtest.NewEthClientMockWithDefaultChain(t) + ethClient := testutils.NewEthClientMockWithDefaultChain(t) l1Oracle := rollupMocks.NewL1Oracle(t) cfg := gas.NewMockConfig() @@ -1857,21 +1858,21 @@ func TestBlockHistoryEstimator_GetLegacyGas(t *testing.T) { { Number: 0, Hash: utils.NewHash(), - Transactions: cltest.LegacyTransactionsFromGasPrices(1000), + Transactions: legacyTransactionsFromGasPrices(1000), }, { Number: 1, Hash: utils.NewHash(), - Transactions: cltest.LegacyTransactionsFromGasPrices(1200), + Transactions: legacyTransactionsFromGasPrices(1200), }, } gas.SetRollingBlockHistory(bhe, blocks) - bhe.Recalculate(cltest.Head(1)) + bhe.Recalculate(testutils.Head(1)) gas.SimulateStart(t, bhe) t.Run("if gas price is lower than global max and user specified max gas price", func(t *testing.T) { - fee, limit, err := bhe.GetLegacyGas(testutils.Context(t), make([]byte, 0), 10000, maxGasPrice) + fee, limit, err := bhe.GetLegacyGas(tests.Context(t), make([]byte, 0), 10000, maxGasPrice) require.NoError(t, err) assert.Equal(t, assets.NewWeiI(1000), fee) @@ -1879,7 +1880,7 @@ func TestBlockHistoryEstimator_GetLegacyGas(t *testing.T) { }) t.Run("if gas price is higher than user-specified max", func(t *testing.T) { - fee, limit, err := bhe.GetLegacyGas(testutils.Context(t), make([]byte, 0), 10000, assets.NewWeiI(800)) + fee, limit, err := bhe.GetLegacyGas(tests.Context(t), make([]byte, 0), 10000, assets.NewWeiI(800)) require.NoError(t, err) assert.Equal(t, assets.NewWeiI(800), fee) @@ -1897,11 +1898,11 @@ func TestBlockHistoryEstimator_GetLegacyGas(t *testing.T) { bhe = newBlockHistoryEstimator(t, nil, cfg, geCfg, bhCfg, l1Oracle) gas.SetRollingBlockHistory(bhe, blocks) - bhe.Recalculate(cltest.Head(1)) + bhe.Recalculate(testutils.Head(1)) gas.SimulateStart(t, bhe) t.Run("if gas price is higher than global max", func(t *testing.T) { - fee, limit, err := bhe.GetLegacyGas(testutils.Context(t), make([]byte, 0), 10000, maxGasPrice) + fee, limit, err := bhe.GetLegacyGas(tests.Context(t), make([]byte, 0), 10000, maxGasPrice) require.NoError(t, err) assert.Equal(t, assets.NewWeiI(700), fee) @@ -1931,7 +1932,7 @@ func TestBlockHistoryEstimator_UseDefaultPriceAsFallback(t *testing.T) { geCfg.PriceMaxF = assets.NewWeiI(1000000) geCfg.PriceDefaultF = assets.NewWeiI(100) - ethClient := evmtest.NewEthClientMockWithDefaultChain(t) + ethClient := testutils.NewEthClientMockWithDefaultChain(t) l1Oracle := rollupMocks.NewL1Oracle(t) bhe := newBlockHistoryEstimator(t, ethClient, cfg, geCfg, bhCfg, l1Oracle) @@ -1956,14 +1957,14 @@ func TestBlockHistoryEstimator_UseDefaultPriceAsFallback(t *testing.T) { elems[2].Result = &evmtypes.Block{ Number: 40, Hash: utils.NewHash(), - Transactions: cltest.LegacyTransactionsFromGasPricesTxType(specialTxTypeCode, 1), + Transactions: legacyTransactionsFromGasPricesTxType(specialTxTypeCode, 1), } }).Once() - err := bhe.Start(testutils.Context(t)) + err := bhe.Start(tests.Context(t)) require.NoError(t, err) - fee, limit, err := bhe.GetLegacyGas(testutils.Context(t), make([]byte, 0), 10000, assets.NewWeiI(800)) + fee, limit, err := bhe.GetLegacyGas(tests.Context(t), make([]byte, 0), 10000, assets.NewWeiI(800)) require.NoError(t, err) require.Equal(t, geCfg.PriceDefault(), fee) assert.Equal(t, 10000, int(limit)) @@ -1984,7 +1985,7 @@ func TestBlockHistoryEstimator_UseDefaultPriceAsFallback(t *testing.T) { geCfg.TipCapDefaultF = assets.NewWeiI(50) geCfg.BumpThresholdF = uint64(1) - ethClient := evmtest.NewEthClientMockWithDefaultChain(t) + ethClient := testutils.NewEthClientMockWithDefaultChain(t) l1Oracle := rollupMocks.NewL1Oracle(t) bhe := newBlockHistoryEstimator(t, ethClient, cfg, geCfg, bhCfg, l1Oracle) @@ -2009,13 +2010,13 @@ func TestBlockHistoryEstimator_UseDefaultPriceAsFallback(t *testing.T) { elems[2].Result = &evmtypes.Block{ Number: 40, Hash: utils.NewHash(), - Transactions: cltest.DynamicFeeTransactionsFromTipCapsTxType(specialTxTypeCode, 1), + Transactions: dynamicFeeTransactionsFromTipCapsTxType(specialTxTypeCode, 1), } }).Once() - err := bhe.Start(testutils.Context(t)) + err := bhe.Start(tests.Context(t)) require.NoError(t, err) - fee, err := bhe.GetDynamicFee(testutils.Context(t), assets.NewWeiI(200)) + fee, err := bhe.GetDynamicFee(tests.Context(t), assets.NewWeiI(200)) require.NoError(t, err) assert.Equal(t, gas.DynamicFee{FeeCap: assets.NewWeiI(114), TipCap: geCfg.TipCapDefault()}, fee) @@ -2045,24 +2046,24 @@ func TestBlockHistoryEstimator_GetDynamicFee(t *testing.T) { BaseFeePerGas: assets.NewWeiI(88889), Number: 0, Hash: utils.NewHash(), - Transactions: cltest.DynamicFeeTransactionsFromTipCaps(5000, 6000, 6000), + Transactions: dynamicFeeTransactionsFromTipCaps(5000, 6000, 6000), }, { BaseFeePerGas: assets.NewWeiI(100000), Number: 1, Hash: utils.NewHash(), - Transactions: cltest.DynamicFeeTransactionsFromTipCaps(10000), + Transactions: dynamicFeeTransactionsFromTipCaps(10000), }, } gas.SetRollingBlockHistory(bhe, blocks) - bhe.Recalculate(cltest.Head(1)) + bhe.Recalculate(testutils.Head(1)) gas.SimulateStart(t, bhe) t.Run("if estimator is missing base fee and gas bumping is enabled", func(t *testing.T) { geCfg.BumpThresholdF = uint64(1) - _, err := bhe.GetDynamicFee(testutils.Context(t), maxGasPrice) + _, err := bhe.GetDynamicFee(tests.Context(t), maxGasPrice) require.Error(t, err) assert.Contains(t, err.Error(), "BlockHistoryEstimator: no value for latest block base fee; cannot estimate EIP-1559 base fee. Are you trying to run with EIP1559 enabled on a non-EIP1559 chain?") }) @@ -2070,19 +2071,19 @@ func TestBlockHistoryEstimator_GetDynamicFee(t *testing.T) { t.Run("if estimator is missing base fee and gas bumping is disabled", func(t *testing.T) { geCfg.BumpThresholdF = uint64(0) - fee, err := bhe.GetDynamicFee(testutils.Context(t), maxGasPrice) + fee, err := bhe.GetDynamicFee(tests.Context(t), maxGasPrice) require.NoError(t, err) assert.Equal(t, gas.DynamicFee{FeeCap: maxGasPrice, TipCap: assets.NewWeiI(6000)}, fee) }) - h := cltest.Head(1) + h := testutils.Head(1) h.BaseFeePerGas = assets.NewWeiI(112500) - bhe.OnNewLongestChain(testutils.Context(t), h) + bhe.OnNewLongestChain(tests.Context(t), h) t.Run("if gas bumping is enabled", func(t *testing.T) { geCfg.BumpThresholdF = uint64(1) - fee, err := bhe.GetDynamicFee(testutils.Context(t), maxGasPrice) + fee, err := bhe.GetDynamicFee(tests.Context(t), maxGasPrice) require.NoError(t, err) assert.Equal(t, gas.DynamicFee{FeeCap: assets.NewWeiI(186203), TipCap: assets.NewWeiI(6000)}, fee) @@ -2091,7 +2092,7 @@ func TestBlockHistoryEstimator_GetDynamicFee(t *testing.T) { t.Run("if gas bumping is disabled", func(t *testing.T) { geCfg.BumpThresholdF = uint64(0) - fee, err := bhe.GetDynamicFee(testutils.Context(t), maxGasPrice) + fee, err := bhe.GetDynamicFee(tests.Context(t), maxGasPrice) require.NoError(t, err) assert.Equal(t, gas.DynamicFee{FeeCap: maxGasPrice, TipCap: assets.NewWeiI(6000)}, fee) @@ -2100,7 +2101,7 @@ func TestBlockHistoryEstimator_GetDynamicFee(t *testing.T) { t.Run("if gas bumping is enabled and local max gas price set", func(t *testing.T) { geCfg.BumpThresholdF = uint64(1) - fee, err := bhe.GetDynamicFee(testutils.Context(t), assets.NewWeiI(180000)) + fee, err := bhe.GetDynamicFee(tests.Context(t), assets.NewWeiI(180000)) require.NoError(t, err) assert.Equal(t, gas.DynamicFee{FeeCap: assets.NewWeiI(180000), TipCap: assets.NewWeiI(6000)}, fee) @@ -2109,20 +2110,20 @@ func TestBlockHistoryEstimator_GetDynamicFee(t *testing.T) { t.Run("if bump threshold is 0 and local max gas price set", func(t *testing.T) { geCfg.BumpThresholdF = uint64(0) - fee, err := bhe.GetDynamicFee(testutils.Context(t), assets.NewWeiI(100)) + fee, err := bhe.GetDynamicFee(tests.Context(t), assets.NewWeiI(100)) require.NoError(t, err) assert.Equal(t, gas.DynamicFee{FeeCap: assets.NewWeiI(100), TipCap: assets.NewWeiI(6000)}, fee) }) - h = cltest.Head(1) + h = testutils.Head(1) h.BaseFeePerGas = assets.NewWeiI(900000) - bhe.OnNewLongestChain(testutils.Context(t), h) + bhe.OnNewLongestChain(tests.Context(t), h) t.Run("if gas bumping is enabled and global max gas price lower than local max gas price", func(t *testing.T) { geCfg.BumpThresholdF = uint64(1) - fee, err := bhe.GetDynamicFee(testutils.Context(t), assets.NewWeiI(1200000)) + fee, err := bhe.GetDynamicFee(tests.Context(t), assets.NewWeiI(1200000)) require.NoError(t, err) assert.Equal(t, gas.DynamicFee{FeeCap: assets.NewWeiI(1000000), TipCap: assets.NewWeiI(6000)}, fee) @@ -2139,7 +2140,7 @@ func TestBlockHistoryEstimator_CheckConnectivity(t *testing.T) { l1Oracle := rollupMocks.NewL1Oracle(t) bhe := gas.BlockHistoryEstimatorFromInterface( - gas.NewBlockHistoryEstimator(lggr, nil, cfg, geCfg, bhCfg, *testutils.NewRandomEVMChainID(), l1Oracle), + gas.NewBlockHistoryEstimator(lggr, nil, cfg, geCfg, bhCfg, testutils.NewRandomEVMChainID(), l1Oracle), ) attempts := []gas.EvmPriorAttempt{ @@ -2150,17 +2151,17 @@ func TestBlockHistoryEstimator_CheckConnectivity(t *testing.T) { err := bhe.CheckConnectivity(attempts) require.NoError(t, err) - testutils.WaitForLogMessage(t, obs, "Latest block is unknown; skipping inclusion check") + tests.AssertLogEventually(t, obs, "Latest block is unknown; skipping inclusion check") }) - h := cltest.Head(1) + h := testutils.Head(1) h.BaseFeePerGas = assets.NewWeiI(112500) - bhe.OnNewLongestChain(testutils.Context(t), h) + bhe.OnNewLongestChain(tests.Context(t), h) b0 := evmtypes.Block{ Number: 0, Hash: utils.NewHash(), - Transactions: cltest.LegacyTransactionsFromGasPrices(), + Transactions: legacyTransactionsFromGasPrices(), } gas.SetRollingBlockHistory(bhe, []evmtypes.Block{b0}) @@ -2168,7 +2169,7 @@ func TestBlockHistoryEstimator_CheckConnectivity(t *testing.T) { err := bhe.CheckConnectivity(attempts) require.NoError(t, err) - testutils.WaitForLogMessage(t, obs, "Block history in memory with length 1 is insufficient to determine whether transaction should have been included within the past 4 blocks") + tests.AssertLogEventually(t, obs, "Block history in memory with length 1 is insufficient to determine whether transaction should have been included within the past 4 blocks") }) t.Run("skips connectivity check if attempts is nil or empty", func(t *testing.T) { @@ -2180,24 +2181,24 @@ func TestBlockHistoryEstimator_CheckConnectivity(t *testing.T) { Number: 1, Hash: utils.NewHash(), ParentHash: b0.Hash, - Transactions: cltest.LegacyTransactionsFromGasPrices(), + Transactions: legacyTransactionsFromGasPrices(), } b2 := evmtypes.Block{ Number: 2, Hash: utils.NewHash(), ParentHash: b1.Hash, - Transactions: cltest.LegacyTransactionsFromGasPrices(), + Transactions: legacyTransactionsFromGasPrices(), } b3 := evmtypes.Block{ Number: 3, Hash: utils.NewHash(), ParentHash: b2.Hash, - Transactions: cltest.LegacyTransactionsFromGasPrices(), + Transactions: legacyTransactionsFromGasPrices(), } gas.SetRollingBlockHistory(bhe, []evmtypes.Block{b0, b1, b2, b3}) - h = cltest.Head(5) + h = testutils.Head(5) h.BaseFeePerGas = assets.NewWeiI(112500) - bhe.OnNewLongestChain(testutils.Context(t), h) + bhe.OnNewLongestChain(tests.Context(t), h) t.Run("returns error if one of the supplied attempts is missing BroadcastBeforeBlockNum", func(t *testing.T) { err := bhe.CheckConnectivity(attempts) @@ -2225,40 +2226,40 @@ func TestBlockHistoryEstimator_CheckConnectivity(t *testing.T) { err := bhe.CheckConnectivity(attempts) require.NoError(t, err) - testutils.WaitForLogMessage(t, obs, fmt.Sprintf("no suitable transactions found to verify if transaction %s has been included within expected inclusion blocks of 4", hash)) + tests.AssertLogEventually(t, obs, fmt.Sprintf("no suitable transactions found to verify if transaction %s has been included within expected inclusion blocks of 4", hash)) }) t.Run("in legacy mode", func(t *testing.T) { b0 = evmtypes.Block{ Number: 0, Hash: utils.NewHash(), - Transactions: cltest.LegacyTransactionsFromGasPrices(1000), + Transactions: legacyTransactionsFromGasPrices(1000), } b1 = evmtypes.Block{ Number: 1, Hash: utils.NewHash(), ParentHash: b0.Hash, - Transactions: cltest.LegacyTransactionsFromGasPrices(2, 3, 4, 5, 6), + Transactions: legacyTransactionsFromGasPrices(2, 3, 4, 5, 6), } b2 = evmtypes.Block{ Number: 2, Hash: utils.NewHash(), ParentHash: b1.Hash, - Transactions: cltest.LegacyTransactionsFromGasPrices(4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9), + Transactions: legacyTransactionsFromGasPrices(4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9), } b3 = evmtypes.Block{ Number: 3, Hash: utils.NewHash(), ParentHash: b2.Hash, - Transactions: cltest.LegacyTransactionsFromGasPrices(3, 4, 5, 6, 7), + Transactions: legacyTransactionsFromGasPrices(3, 4, 5, 6, 7), } gas.SetRollingBlockHistory(bhe, []evmtypes.Block{b0, b1, b2, b3}) attempts = []gas.EvmPriorAttempt{ - {TxType: 0x0, TxHash: NewEvmHash(), GasPrice: assets.NewWeiI(1000), BroadcastBeforeBlockNum: testutils.Ptr(int64(4))}, // This is very expensive but will be ignored due to BroadcastBeforeBlockNum being too recent - {TxType: 0x0, TxHash: NewEvmHash(), GasPrice: assets.NewWeiI(3), BroadcastBeforeBlockNum: testutils.Ptr(int64(0))}, - {TxType: 0x0, TxHash: NewEvmHash(), GasPrice: assets.NewWeiI(5), BroadcastBeforeBlockNum: testutils.Ptr(int64(1))}, - {TxType: 0x0, TxHash: NewEvmHash(), GasPrice: assets.NewWeiI(7), BroadcastBeforeBlockNum: testutils.Ptr(int64(1))}, + {TxType: 0x0, TxHash: NewEvmHash(), GasPrice: assets.NewWeiI(1000), BroadcastBeforeBlockNum: ptr(int64(4))}, // This is very expensive but will be ignored due to BroadcastBeforeBlockNum being too recent + {TxType: 0x0, TxHash: NewEvmHash(), GasPrice: assets.NewWeiI(3), BroadcastBeforeBlockNum: ptr(int64(0))}, + {TxType: 0x0, TxHash: NewEvmHash(), GasPrice: assets.NewWeiI(5), BroadcastBeforeBlockNum: ptr(int64(1))}, + {TxType: 0x0, TxHash: NewEvmHash(), GasPrice: assets.NewWeiI(7), BroadcastBeforeBlockNum: ptr(int64(1))}, } t.Run("passes check if all blocks have percentile price higher or exactly at the highest transaction gas price", func(t *testing.T) { @@ -2301,13 +2302,13 @@ func TestBlockHistoryEstimator_CheckConnectivity(t *testing.T) { BaseFeePerGas: assets.NewWeiI(1), Number: 3, Hash: utils.NewHash(), - Transactions: append(cltest.LegacyTransactionsFromGasPrices(1, 2, 3, 4, 5), cltest.DynamicFeeTransactionsFromTipCaps(6, 7, 8, 9, 10)...), + Transactions: append(legacyTransactionsFromGasPrices(1, 2, 3, 4, 5), dynamicFeeTransactionsFromTipCaps(6, 7, 8, 9, 10)...), } gas.SetRollingBlockHistory(bhe, []evmtypes.Block{b0}) attempts = []gas.EvmPriorAttempt{ - {TxType: 0x2, TxHash: NewEvmHash(), DynamicFee: gas.DynamicFee{FeeCap: assets.NewWeiI(1), TipCap: assets.NewWeiI(3)}, BroadcastBeforeBlockNum: testutils.Ptr(int64(0))}, - {TxType: 0x0, TxHash: NewEvmHash(), GasPrice: assets.NewWeiI(10), BroadcastBeforeBlockNum: testutils.Ptr(int64(0))}, + {TxType: 0x2, TxHash: NewEvmHash(), DynamicFee: gas.DynamicFee{FeeCap: assets.NewWeiI(1), TipCap: assets.NewWeiI(3)}, BroadcastBeforeBlockNum: ptr(int64(0))}, + {TxType: 0x0, TxHash: NewEvmHash(), GasPrice: assets.NewWeiI(10), BroadcastBeforeBlockNum: ptr(int64(0))}, } t.Run("passes check if both transactions are ok", func(t *testing.T) { @@ -2328,8 +2329,8 @@ func TestBlockHistoryEstimator_CheckConnectivity(t *testing.T) { }) attempts = []gas.EvmPriorAttempt{ - {TxType: 0x2, TxHash: NewEvmHash(), DynamicFee: gas.DynamicFee{FeeCap: assets.NewWeiI(11), TipCap: assets.NewWeiI(10)}, BroadcastBeforeBlockNum: testutils.Ptr(int64(0))}, - {TxType: 0x0, TxHash: NewEvmHash(), GasPrice: assets.NewWeiI(3), BroadcastBeforeBlockNum: testutils.Ptr(int64(0))}, + {TxType: 0x2, TxHash: NewEvmHash(), DynamicFee: gas.DynamicFee{FeeCap: assets.NewWeiI(11), TipCap: assets.NewWeiI(10)}, BroadcastBeforeBlockNum: ptr(int64(0))}, + {TxType: 0x0, TxHash: NewEvmHash(), GasPrice: assets.NewWeiI(3), BroadcastBeforeBlockNum: ptr(int64(0))}, } t.Run("fails check if dynamic fee transaction fails", func(t *testing.T) { @@ -2349,37 +2350,37 @@ func TestBlockHistoryEstimator_CheckConnectivity(t *testing.T) { BaseFeePerGas: assets.NewWeiI(5), Number: 0, Hash: utils.NewHash(), - Transactions: cltest.DynamicFeeTransactionsFromTipCaps(1000), + Transactions: dynamicFeeTransactionsFromTipCaps(1000), } b1 = evmtypes.Block{ BaseFeePerGas: assets.NewWeiI(8), Number: 1, Hash: utils.NewHash(), ParentHash: b0.Hash, - Transactions: cltest.DynamicFeeTransactionsFromTipCaps(2, 3, 4, 5, 6), + Transactions: dynamicFeeTransactionsFromTipCaps(2, 3, 4, 5, 6), } b2 = evmtypes.Block{ BaseFeePerGas: assets.NewWeiI(13), Number: 2, Hash: utils.NewHash(), ParentHash: b1.Hash, - Transactions: cltest.DynamicFeeTransactionsFromTipCaps(4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9), + Transactions: dynamicFeeTransactionsFromTipCaps(4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9), } b3 = evmtypes.Block{ BaseFeePerGas: assets.NewWeiI(21), Number: 3, Hash: utils.NewHash(), ParentHash: b2.Hash, - Transactions: cltest.DynamicFeeTransactionsFromTipCaps(3, 4, 5, 6, 7), + Transactions: dynamicFeeTransactionsFromTipCaps(3, 4, 5, 6, 7), } blocks := []evmtypes.Block{b0, b1, b2, b3} gas.SetRollingBlockHistory(bhe, blocks) attempts = []gas.EvmPriorAttempt{ - {TxType: 0x2, TxHash: NewEvmHash(), DynamicFee: gas.DynamicFee{FeeCap: assets.NewWeiI(30), TipCap: assets.NewWeiI(1000)}, BroadcastBeforeBlockNum: testutils.Ptr(int64(4))}, // This is very expensive but will be ignored due to BroadcastBeforeBlockNum being too recent - {TxType: 0x2, TxHash: NewEvmHash(), DynamicFee: gas.DynamicFee{FeeCap: assets.NewWeiI(30), TipCap: assets.NewWeiI(3)}, BroadcastBeforeBlockNum: testutils.Ptr(int64(0))}, - {TxType: 0x2, TxHash: NewEvmHash(), DynamicFee: gas.DynamicFee{FeeCap: assets.NewWeiI(30), TipCap: assets.NewWeiI(5)}, BroadcastBeforeBlockNum: testutils.Ptr(int64(1))}, - {TxType: 0x2, TxHash: NewEvmHash(), DynamicFee: gas.DynamicFee{FeeCap: assets.NewWeiI(30), TipCap: assets.NewWeiI(7)}, BroadcastBeforeBlockNum: testutils.Ptr(int64(1))}, + {TxType: 0x2, TxHash: NewEvmHash(), DynamicFee: gas.DynamicFee{FeeCap: assets.NewWeiI(30), TipCap: assets.NewWeiI(1000)}, BroadcastBeforeBlockNum: ptr(int64(4))}, // This is very expensive but will be ignored due to BroadcastBeforeBlockNum being too recent + {TxType: 0x2, TxHash: NewEvmHash(), DynamicFee: gas.DynamicFee{FeeCap: assets.NewWeiI(30), TipCap: assets.NewWeiI(3)}, BroadcastBeforeBlockNum: ptr(int64(0))}, + {TxType: 0x2, TxHash: NewEvmHash(), DynamicFee: gas.DynamicFee{FeeCap: assets.NewWeiI(30), TipCap: assets.NewWeiI(5)}, BroadcastBeforeBlockNum: ptr(int64(1))}, + {TxType: 0x2, TxHash: NewEvmHash(), DynamicFee: gas.DynamicFee{FeeCap: assets.NewWeiI(30), TipCap: assets.NewWeiI(7)}, BroadcastBeforeBlockNum: ptr(int64(1))}, } t.Run("passes check if all blocks have 90th percentile price higher than highest transaction tip cap", func(t *testing.T) { @@ -2413,7 +2414,7 @@ func TestBlockHistoryEstimator_CheckConnectivity(t *testing.T) { bhCfg.CheckInclusionPercentileF = 5 attempts = []gas.EvmPriorAttempt{ - {TxType: 0x2, TxHash: NewEvmHash(), DynamicFee: gas.DynamicFee{FeeCap: assets.NewWeiI(4), TipCap: assets.NewWeiI(7)}, BroadcastBeforeBlockNum: testutils.Ptr(int64(1))}, + {TxType: 0x2, TxHash: NewEvmHash(), DynamicFee: gas.DynamicFee{FeeCap: assets.NewWeiI(4), TipCap: assets.NewWeiI(7)}, BroadcastBeforeBlockNum: ptr(int64(1))}, } err := bhe.CheckConnectivity(attempts) @@ -2443,17 +2444,17 @@ func TestBlockHistoryEstimator_Bumps(t *testing.T) { b1 := evmtypes.Block{ Number: 1, Hash: utils.NewHash(), - Transactions: cltest.LegacyTransactionsFromGasPrices(1), + Transactions: legacyTransactionsFromGasPrices(1), } gas.SetRollingBlockHistory(bhe, []evmtypes.Block{b1}) - head := cltest.Head(1) - bhe.OnNewLongestChain(testutils.Context(t), head) + head := testutils.Head(1) + bhe.OnNewLongestChain(tests.Context(t), head) attempts := []gas.EvmPriorAttempt{ - {TxType: 0x0, TxHash: NewEvmHash(), GasPrice: assets.NewWeiI(1000), BroadcastBeforeBlockNum: testutils.Ptr(int64(0))}, + {TxType: 0x0, TxHash: NewEvmHash(), GasPrice: assets.NewWeiI(1000), BroadcastBeforeBlockNum: ptr(int64(0))}, } - _, _, err := bhe.BumpLegacyGas(testutils.Context(t), assets.NewWeiI(42), 100000, maxGasPrice, attempts) + _, _, err := bhe.BumpLegacyGas(tests.Context(t), assets.NewWeiI(42), 100000, maxGasPrice, attempts) require.Error(t, err) assert.True(t, pkgerrors.Is(err, commonfee.ErrConnectivity)) assert.Contains(t, err.Error(), fmt.Sprintf("transaction %s has gas price of 1 kwei, which is above percentile=10%% (percentile price: 1 wei) for blocks 1 thru 1 (checking 1 blocks)", attempts[0].TxHash)) @@ -2471,7 +2472,7 @@ func TestBlockHistoryEstimator_Bumps(t *testing.T) { bhe := newBlockHistoryEstimator(t, nil, cfg, geCfg, bhCfg, l1Oracle) t.Run("ignores nil current gas price", func(t *testing.T) { - gasPrice, gasLimit, err := bhe.BumpLegacyGas(testutils.Context(t), assets.NewWeiI(42), 100000, maxGasPrice, nil) + gasPrice, gasLimit, err := bhe.BumpLegacyGas(tests.Context(t), assets.NewWeiI(42), 100000, maxGasPrice, nil) require.NoError(t, err) expectedGasPrice, err := gas.BumpLegacyGasPriceOnly(geCfg, logger.TestSugared(t), nil, assets.NewWeiI(42), maxGasPrice) @@ -2482,7 +2483,7 @@ func TestBlockHistoryEstimator_Bumps(t *testing.T) { }) t.Run("ignores current gas price > max gas price", func(t *testing.T) { - gasPrice, gasLimit, err := bhe.BumpLegacyGas(testutils.Context(t), assets.NewWeiI(42), 100000, maxGasPrice, nil) + gasPrice, gasLimit, err := bhe.BumpLegacyGas(tests.Context(t), assets.NewWeiI(42), 100000, maxGasPrice, nil) require.NoError(t, err) massive := assets.NewWeiI(100000000000000) @@ -2498,7 +2499,7 @@ func TestBlockHistoryEstimator_Bumps(t *testing.T) { t.Run("ignores current gas price < bumped gas price", func(t *testing.T) { gas.SetGasPrice(bhe, assets.NewWeiI(191)) - gasPrice, gasLimit, err := bhe.BumpLegacyGas(testutils.Context(t), assets.NewWeiI(42), 100000, maxGasPrice, nil) + gasPrice, gasLimit, err := bhe.BumpLegacyGas(tests.Context(t), assets.NewWeiI(42), 100000, maxGasPrice, nil) require.NoError(t, err) assert.Equal(t, 100000, int(gasLimit)) @@ -2508,7 +2509,7 @@ func TestBlockHistoryEstimator_Bumps(t *testing.T) { t.Run("uses current gas price > bumped gas price", func(t *testing.T) { gas.SetGasPrice(bhe, assets.NewWeiI(193)) - gasPrice, gasLimit, err := bhe.BumpLegacyGas(testutils.Context(t), assets.NewWeiI(42), 100000, maxGasPrice, nil) + gasPrice, gasLimit, err := bhe.BumpLegacyGas(tests.Context(t), assets.NewWeiI(42), 100000, maxGasPrice, nil) require.NoError(t, err) assert.Equal(t, 100000, int(gasLimit)) @@ -2518,7 +2519,7 @@ func TestBlockHistoryEstimator_Bumps(t *testing.T) { t.Run("bumped gas price > max gas price", func(t *testing.T) { gas.SetGasPrice(bhe, assets.NewWeiI(191)) - gasPrice, gasLimit, err := bhe.BumpLegacyGas(testutils.Context(t), assets.NewWeiI(42), 100000, assets.NewWeiI(100), nil) + gasPrice, gasLimit, err := bhe.BumpLegacyGas(tests.Context(t), assets.NewWeiI(42), 100000, assets.NewWeiI(100), nil) require.Error(t, err) assert.Nil(t, gasPrice) @@ -2529,7 +2530,7 @@ func TestBlockHistoryEstimator_Bumps(t *testing.T) { t.Run("current gas price > max gas price", func(t *testing.T) { gas.SetGasPrice(bhe, assets.NewWeiI(193)) - gasPrice, gasLimit, err := bhe.BumpLegacyGas(testutils.Context(t), assets.NewWeiI(42), 100000, assets.NewWeiI(100), nil) + gasPrice, gasLimit, err := bhe.BumpLegacyGas(tests.Context(t), assets.NewWeiI(42), 100000, assets.NewWeiI(100), nil) require.Error(t, err) assert.Nil(t, gasPrice) @@ -2556,17 +2557,17 @@ func TestBlockHistoryEstimator_Bumps(t *testing.T) { BaseFeePerGas: assets.NewWeiI(1), Number: 1, Hash: utils.NewHash(), - Transactions: cltest.DynamicFeeTransactionsFromTipCaps(1), + Transactions: dynamicFeeTransactionsFromTipCaps(1), } gas.SetRollingBlockHistory(bhe, []evmtypes.Block{b1}) - head := cltest.Head(1) - bhe.OnNewLongestChain(testutils.Context(t), head) + head := testutils.Head(1) + bhe.OnNewLongestChain(tests.Context(t), head) originalFee := gas.DynamicFee{FeeCap: assets.NewWeiI(100), TipCap: assets.NewWeiI(25)} attempts := []gas.EvmPriorAttempt{ - {TxType: 0x2, TxHash: NewEvmHash(), DynamicFee: gas.DynamicFee{TipCap: originalFee.TipCap, FeeCap: originalFee.FeeCap}, BroadcastBeforeBlockNum: testutils.Ptr(int64(0))}} + {TxType: 0x2, TxHash: NewEvmHash(), DynamicFee: gas.DynamicFee{TipCap: originalFee.TipCap, FeeCap: originalFee.FeeCap}, BroadcastBeforeBlockNum: ptr(int64(0))}} - _, err := bhe.BumpDynamicFee(testutils.Context(t), originalFee, maxGasPrice, attempts) + _, err := bhe.BumpDynamicFee(tests.Context(t), originalFee, maxGasPrice, attempts) require.Error(t, err) assert.True(t, pkgerrors.Is(err, commonfee.ErrConnectivity)) assert.Contains(t, err.Error(), fmt.Sprintf("transaction %s has tip cap of 25 wei, which is above percentile=10%% (percentile tip cap: 1 wei) for blocks 1 thru 1 (checking 1 blocks)", attempts[0].TxHash)) @@ -2588,7 +2589,7 @@ func TestBlockHistoryEstimator_Bumps(t *testing.T) { t.Run("when current tip cap is nil", func(t *testing.T) { originalFee := gas.DynamicFee{FeeCap: assets.NewWeiI(100), TipCap: assets.NewWeiI(25)} - fee, err := bhe.BumpDynamicFee(testutils.Context(t), originalFee, maxGasPrice, nil) + fee, err := bhe.BumpDynamicFee(tests.Context(t), originalFee, maxGasPrice, nil) require.NoError(t, err) assert.Equal(t, gas.DynamicFee{FeeCap: assets.NewWeiI(250), TipCap: assets.NewWeiI(202)}, fee) @@ -2597,7 +2598,7 @@ func TestBlockHistoryEstimator_Bumps(t *testing.T) { gas.SetTipCap(bhe, assets.NewWeiI(201)) originalFee := gas.DynamicFee{FeeCap: assets.NewWeiI(100), TipCap: assets.NewWeiI(25)} - fee, err := bhe.BumpDynamicFee(testutils.Context(t), originalFee, maxGasPrice, nil) + fee, err := bhe.BumpDynamicFee(tests.Context(t), originalFee, maxGasPrice, nil) require.NoError(t, err) assert.Equal(t, gas.DynamicFee{FeeCap: assets.NewWeiI(250), TipCap: assets.NewWeiI(202)}, fee) @@ -2606,7 +2607,7 @@ func TestBlockHistoryEstimator_Bumps(t *testing.T) { gas.SetTipCap(bhe, assets.NewWeiI(203)) originalFee := gas.DynamicFee{FeeCap: assets.NewWeiI(100), TipCap: assets.NewWeiI(25)} - fee, err := bhe.BumpDynamicFee(testutils.Context(t), originalFee, maxGasPrice, nil) + fee, err := bhe.BumpDynamicFee(tests.Context(t), originalFee, maxGasPrice, nil) require.NoError(t, err) assert.Equal(t, gas.DynamicFee{FeeCap: assets.NewWeiI(250), TipCap: assets.NewWeiI(203)}, fee) @@ -2615,7 +2616,7 @@ func TestBlockHistoryEstimator_Bumps(t *testing.T) { gas.SetTipCap(bhe, assets.NewWeiI(1000000000000000)) originalFee := gas.DynamicFee{FeeCap: assets.NewWeiI(100), TipCap: assets.NewWeiI(25)} - fee, err := bhe.BumpDynamicFee(testutils.Context(t), originalFee, maxGasPrice, nil) + fee, err := bhe.BumpDynamicFee(tests.Context(t), originalFee, maxGasPrice, nil) require.NoError(t, err) assert.Equal(t, gas.DynamicFee{FeeCap: assets.NewWeiI(250), TipCap: assets.NewWeiI(202)}, fee) @@ -2625,7 +2626,7 @@ func TestBlockHistoryEstimator_Bumps(t *testing.T) { gas.SetTipCap(bhe, assets.NewWeiI(203)) originalFee := gas.DynamicFee{FeeCap: assets.NewWeiI(100), TipCap: assets.NewWeiI(990000)} - fee, err := bhe.BumpDynamicFee(testutils.Context(t), originalFee, maxGasPrice, nil) + fee, err := bhe.BumpDynamicFee(tests.Context(t), originalFee, maxGasPrice, nil) require.Error(t, err) assert.Equal(t, gas.DynamicFee{}, fee) @@ -2636,7 +2637,7 @@ func TestBlockHistoryEstimator_Bumps(t *testing.T) { gas.SetTipCap(bhe, assets.NewWeiI(203)) originalFee := gas.DynamicFee{FeeCap: assets.NewWeiI(990000), TipCap: assets.NewWeiI(25)} - fee, err := bhe.BumpDynamicFee(testutils.Context(t), originalFee, maxGasPrice, nil) + fee, err := bhe.BumpDynamicFee(tests.Context(t), originalFee, maxGasPrice, nil) require.Error(t, err) assert.Equal(t, gas.DynamicFee{}, fee) @@ -2644,3 +2645,35 @@ func TestBlockHistoryEstimator_Bumps(t *testing.T) { }) }) } + +// ptr takes pointer of anything +func ptr[T any](v T) *T { + return &v +} + +// LegacyTransactionsFromGasPrices returns transactions matching the given gas prices +func legacyTransactionsFromGasPrices(gasPrices ...int64) []evmtypes.Transaction { + return legacyTransactionsFromGasPricesTxType(0x0, gasPrices...) +} + +func legacyTransactionsFromGasPricesTxType(code evmtypes.TxType, gasPrices ...int64) []evmtypes.Transaction { + txs := make([]evmtypes.Transaction, len(gasPrices)) + for i, gasPrice := range gasPrices { + txs[i] = evmtypes.Transaction{Type: code, GasPrice: assets.NewWeiI(gasPrice), GasLimit: 42} + } + return txs +} + +// dynamicFeeTransactionsFromTipCaps returns EIP-1559 transactions with the +// given TipCaps (FeeCap is arbitrary) +func dynamicFeeTransactionsFromTipCaps(tipCaps ...int64) []evmtypes.Transaction { + return dynamicFeeTransactionsFromTipCapsTxType(0x02, tipCaps...) +} + +func dynamicFeeTransactionsFromTipCapsTxType(code evmtypes.TxType, tipCaps ...int64) []evmtypes.Transaction { + txs := make([]evmtypes.Transaction, len(tipCaps)) + for i, tipCap := range tipCaps { + txs[i] = evmtypes.Transaction{Type: code, MaxPriorityFeePerGas: assets.NewWeiI(tipCap), GasLimit: 42, MaxFeePerGas: assets.GWei(5000)} + } + return txs +} diff --git a/core/chains/evm/gas/fixed_price_estimator_test.go b/core/chains/evm/gas/fixed_price_estimator_test.go index 9c68f9d2fbc..80641ae3cc5 100644 --- a/core/chains/evm/gas/fixed_price_estimator_test.go +++ b/core/chains/evm/gas/fixed_price_estimator_test.go @@ -7,10 +7,11 @@ import ( "github.com/stretchr/testify/require" "github.com/smartcontractkit/chainlink-common/pkg/logger" + "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" rollupMocks "github.com/smartcontractkit/chainlink/v2/core/chains/evm/gas/rollups/mocks" - "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" ) type blockHistoryConfig struct { @@ -34,7 +35,7 @@ func Test_FixedPriceEstimator(t *testing.T) { config.PriceDefaultF = assets.NewWeiI(42) config.PriceMaxF = maxGasPrice - gasPrice, gasLimit, err := f.GetLegacyGas(testutils.Context(t), nil, 100000, maxGasPrice) + gasPrice, gasLimit, err := f.GetLegacyGas(tests.Context(t), nil, 100000, maxGasPrice) require.NoError(t, err) assert.Equal(t, 100000, int(gasLimit)) assert.Equal(t, assets.NewWeiI(42), gasPrice) @@ -48,7 +49,7 @@ func Test_FixedPriceEstimator(t *testing.T) { f := gas.NewFixedPriceEstimator(config, nil, &blockHistoryConfig{}, logger.Test(t), l1Oracle) - gasPrice, gasLimit, err := f.GetLegacyGas(testutils.Context(t), nil, 100000, assets.NewWeiI(30)) + gasPrice, gasLimit, err := f.GetLegacyGas(tests.Context(t), nil, 100000, assets.NewWeiI(30)) require.NoError(t, err) assert.Equal(t, 100000, int(gasLimit)) assert.Equal(t, assets.NewWeiI(30), gasPrice) @@ -61,7 +62,7 @@ func Test_FixedPriceEstimator(t *testing.T) { l1Oracle := rollupMocks.NewL1Oracle(t) f := gas.NewFixedPriceEstimator(config, nil, &blockHistoryConfig{}, logger.Test(t), l1Oracle) - gasPrice, gasLimit, err := f.GetLegacyGas(testutils.Context(t), nil, 100000, assets.NewWeiI(30)) + gasPrice, gasLimit, err := f.GetLegacyGas(tests.Context(t), nil, 100000, assets.NewWeiI(30)) require.NoError(t, err) assert.Equal(t, 100000, int(gasLimit)) assert.Equal(t, assets.NewWeiI(20), gasPrice) @@ -78,7 +79,7 @@ func Test_FixedPriceEstimator(t *testing.T) { lggr := logger.TestSugared(t) f := gas.NewFixedPriceEstimator(config, nil, &blockHistoryConfig{}, lggr, l1Oracle) - gasPrice, gasLimit, err := f.BumpLegacyGas(testutils.Context(t), assets.NewWeiI(42), 100000, maxGasPrice, nil) + gasPrice, gasLimit, err := f.BumpLegacyGas(tests.Context(t), assets.NewWeiI(42), 100000, maxGasPrice, nil) require.NoError(t, err) expectedGasPrice, err := gas.BumpLegacyGasPriceOnly(config, lggr, nil, assets.NewWeiI(42), maxGasPrice) @@ -99,7 +100,7 @@ func Test_FixedPriceEstimator(t *testing.T) { lggr := logger.Test(t) f := gas.NewFixedPriceEstimator(config, nil, &blockHistoryConfig{}, lggr, l1Oracle) - fee, err := f.GetDynamicFee(testutils.Context(t), maxGasPrice) + fee, err := f.GetDynamicFee(tests.Context(t), maxGasPrice) require.NoError(t, err) assert.Equal(t, assets.NewWeiI(52), fee.TipCap) @@ -108,14 +109,14 @@ func Test_FixedPriceEstimator(t *testing.T) { // Gas bumping disabled config.BumpThresholdF = uint64(0) - fee, err = f.GetDynamicFee(testutils.Context(t), maxGasPrice) + fee, err = f.GetDynamicFee(tests.Context(t), maxGasPrice) require.NoError(t, err) assert.Equal(t, assets.NewWeiI(52), fee.TipCap) assert.Equal(t, maxGasPrice, fee.FeeCap) // override max gas price - fee, err = f.GetDynamicFee(testutils.Context(t), assets.NewWeiI(10)) + fee, err = f.GetDynamicFee(tests.Context(t), assets.NewWeiI(10)) require.NoError(t, err) assert.Equal(t, assets.NewWeiI(52), fee.TipCap) @@ -134,7 +135,7 @@ func Test_FixedPriceEstimator(t *testing.T) { f := gas.NewFixedPriceEstimator(config, nil, &blockHistoryConfig{}, lggr, l1Oracle) originalFee := gas.DynamicFee{FeeCap: assets.NewWeiI(100), TipCap: assets.NewWeiI(25)} - fee, err := f.BumpDynamicFee(testutils.Context(t), originalFee, maxGasPrice, nil) + fee, err := f.BumpDynamicFee(tests.Context(t), originalFee, maxGasPrice, nil) require.NoError(t, err) expectedFee, err := gas.BumpDynamicFeeOnly(config, 0, lggr, nil, nil, originalFee, maxGasPrice) diff --git a/core/chains/evm/gas/models.go b/core/chains/evm/gas/models.go index c50e19373f1..68fc7378d31 100644 --- a/core/chains/evm/gas/models.go +++ b/core/chains/evm/gas/models.go @@ -89,7 +89,7 @@ func NewEstimator(lggr logger.Logger, ethClient feeEstimatorClient, cfg Config, } case "BlockHistory": newEstimator = func(l logger.Logger) EvmEstimator { - return NewBlockHistoryEstimator(lggr, ethClient, cfg, geCfg, bh, *ethClient.ConfiguredChainID(), l1Oracle) + return NewBlockHistoryEstimator(lggr, ethClient, cfg, geCfg, bh, ethClient.ConfiguredChainID(), l1Oracle) } case "FixedPrice": newEstimator = func(l logger.Logger) EvmEstimator { diff --git a/core/chains/evm/gas/models_test.go b/core/chains/evm/gas/models_test.go index 722beb8021a..f4366912632 100644 --- a/core/chains/evm/gas/models_test.go +++ b/core/chains/evm/gas/models_test.go @@ -10,18 +10,19 @@ import ( "github.com/stretchr/testify/require" "github.com/smartcontractkit/chainlink-common/pkg/logger" + "github.com/smartcontractkit/chainlink-common/pkg/utils/tests" + "github.com/smartcontractkit/chainlink/v2/common/config" "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" rollupMocks "github.com/smartcontractkit/chainlink/v2/core/chains/evm/gas/rollups/mocks" - "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" ) func TestWrappedEvmEstimator(t *testing.T) { t.Parallel() - ctx := testutils.Context(t) + ctx := tests.Context(t) // fee values gasLimit := uint64(10) diff --git a/core/chains/evm/gas/rollups/l1_oracle_test.go b/core/chains/evm/gas/rollups/l1_oracle_test.go index 6efdda6bcff..b49f92909d6 100644 --- a/core/chains/evm/gas/rollups/l1_oracle_test.go +++ b/core/chains/evm/gas/rollups/l1_oracle_test.go @@ -16,12 +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/common/config" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/assets" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/gas/rollups/mocks" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/utils" - "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" ) func TestL1Oracle(t *testing.T) { @@ -42,7 +42,7 @@ func TestL1Oracle_GasPrice(t *testing.T) { oracle := NewL1GasOracle(logger.Test(t), ethClient, config.ChainOptimismBedrock) - _, err := oracle.GasPrice(testutils.Context(t)) + _, err := oracle.GasPrice(tests.Context(t)) assert.EqualError(t, err, "L1GasOracle is not started; cannot estimate gas") }) @@ -65,7 +65,7 @@ func TestL1Oracle_GasPrice(t *testing.T) { oracle := NewL1GasOracle(logger.Test(t), ethClient, config.ChainArbitrum) servicetest.RunHealthy(t, oracle) - gasPrice, err := oracle.GasPrice(testutils.Context(t)) + gasPrice, err := oracle.GasPrice(tests.Context(t)) require.NoError(t, err) assert.Equal(t, assets.NewWei(l1BaseFee), gasPrice) @@ -104,7 +104,7 @@ func TestL1Oracle_GasPrice(t *testing.T) { oracle := newOpStackL1GasOracle(logger.Test(t), ethClient, config.ChainKroma, KromaGasOracleAddress) servicetest.RunHealthy(t, oracle) - gasPrice, err := oracle.GasPrice(testutils.Context(t)) + gasPrice, err := oracle.GasPrice(tests.Context(t)) require.NoError(t, err) assert.Equal(t, assets.NewWei(l1BaseFee), gasPrice) @@ -143,7 +143,7 @@ func TestL1Oracle_GasPrice(t *testing.T) { oracle := newOpStackL1GasOracle(logger.Test(t), ethClient, config.ChainOptimismBedrock, OPGasOracleAddress) servicetest.RunHealthy(t, oracle) - gasPrice, err := oracle.GasPrice(testutils.Context(t)) + gasPrice, err := oracle.GasPrice(tests.Context(t)) require.NoError(t, err) assert.Equal(t, assets.NewWei(l1BaseFee), gasPrice) @@ -179,10 +179,10 @@ func TestL1Oracle_GasPrice(t *testing.T) { }).Return(common.BigToHash(l1BaseFee).Bytes(), nil) oracle := NewL1GasOracle(logger.Test(t), ethClient, config.ChainScroll) - require.NoError(t, oracle.Start(testutils.Context(t))) + require.NoError(t, oracle.Start(tests.Context(t))) t.Cleanup(func() { assert.NoError(t, oracle.Close()) }) - gasPrice, err := oracle.GasPrice(testutils.Context(t)) + gasPrice, err := oracle.GasPrice(tests.Context(t)) require.NoError(t, err) assert.Equal(t, assets.NewWei(l1BaseFee), gasPrice) @@ -224,7 +224,7 @@ func TestL1Oracle_GetGasCost(t *testing.T) { oracle := NewL1GasOracle(logger.Test(t), ethClient, config.ChainArbitrum) - gasCost, err := oracle.GetGasCost(testutils.Context(t), tx, blockNum) + gasCost, err := oracle.GetGasCost(tests.Context(t), tx, blockNum) require.NoError(t, err) require.Equal(t, assets.NewWei(l1GasCost), gasCost) }) @@ -236,7 +236,7 @@ func TestL1Oracle_GetGasCost(t *testing.T) { ethClient := mocks.NewL1OracleClient(t) oracle := NewL1GasOracle(logger.Test(t), ethClient, config.ChainKroma) - _, err := oracle.GetGasCost(testutils.Context(t), tx, blockNum) + _, err := oracle.GetGasCost(tests.Context(t), tx, blockNum) require.Error(t, err, "L1 gas cost not supported for this chain: kroma") }) @@ -270,7 +270,7 @@ func TestL1Oracle_GetGasCost(t *testing.T) { oracle := NewL1GasOracle(logger.Test(t), ethClient, config.ChainOptimismBedrock) - gasCost, err := oracle.GetGasCost(testutils.Context(t), tx, blockNum) + gasCost, err := oracle.GetGasCost(tests.Context(t), tx, blockNum) require.NoError(t, err) require.Equal(t, assets.NewWei(l1GasCost), gasCost) }) @@ -305,7 +305,7 @@ func TestL1Oracle_GetGasCost(t *testing.T) { oracle := NewL1GasOracle(logger.Test(t), ethClient, config.ChainScroll) - gasCost, err := oracle.GetGasCost(testutils.Context(t), tx, blockNum) + gasCost, err := oracle.GetGasCost(tests.Context(t), tx, blockNum) require.NoError(t, err) require.Equal(t, assets.NewWei(l1GasCost), gasCost) }) diff --git a/core/chains/evm/gas/rollups/op_l1_oracle_test.go b/core/chains/evm/gas/rollups/op_l1_oracle_test.go index 36e8700faff..2ed8a049653 100644 --- a/core/chains/evm/gas/rollups/op_l1_oracle_test.go +++ b/core/chains/evm/gas/rollups/op_l1_oracle_test.go @@ -16,10 +16,10 @@ import ( "github.com/stretchr/testify/require" "github.com/smartcontractkit/chainlink-common/pkg/logger" + "github.com/smartcontractkit/chainlink-common/pkg/utils/tests" "github.com/smartcontractkit/chainlink/v2/common/config" "github.com/smartcontractkit/chainlink/v2/core/chains/evm/gas/rollups/mocks" - "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" ) func TestDAPriceReader_ReadV1GasPrice(t *testing.T) { @@ -88,7 +88,7 @@ func TestDAPriceReader_ReadV1GasPrice(t *testing.T) { } oracle := newOpStackL1GasOracle(logger.Test(t), ethClient, config.ChainOptimismBedrock, oracleAddress) - gasPrice, err := oracle.GetDAGasPrice(testutils.Context(t)) + gasPrice, err := oracle.GetDAGasPrice(tests.Context(t)) if tc.returnBadData { assert.Error(t, err) @@ -156,7 +156,7 @@ func TestDAPriceReader_ReadEcotoneGasPrice(t *testing.T) { }).Return(nil).Once() oracle := newOpStackL1GasOracle(logger.Test(t), ethClient, config.ChainOptimismBedrock, oracleAddress) - gasPrice, err := oracle.GetDAGasPrice(testutils.Context(t)) + gasPrice, err := oracle.GetDAGasPrice(tests.Context(t)) require.NoError(t, err) assert.Equal(t, l1BaseFee, gasPrice) }) @@ -171,7 +171,7 @@ func TestDAPriceReader_ReadEcotoneGasPrice(t *testing.T) { }).Return(nil).Once() oracle := newOpStackL1GasOracle(logger.Test(t), ethClient, config.ChainOptimismBedrock, oracleAddress) - _, err := oracle.GetDAGasPrice(testutils.Context(t)) + _, err := oracle.GetDAGasPrice(tests.Context(t)) assert.Error(t, err) }) @@ -180,7 +180,7 @@ func TestDAPriceReader_ReadEcotoneGasPrice(t *testing.T) { ethClient.On("BatchCallContext", mock.Anything, mock.IsType([]rpc.BatchElem{})).Return(fmt.Errorf("revert")).Once() oracle := newOpStackL1GasOracle(logger.Test(t), ethClient, config.ChainOptimismBedrock, oracleAddress) - _, err := oracle.GetDAGasPrice(testutils.Context(t)) + _, err := oracle.GetDAGasPrice(tests.Context(t)) assert.Error(t, err) }) @@ -194,7 +194,7 @@ func TestDAPriceReader_ReadEcotoneGasPrice(t *testing.T) { }).Return(nil).Once() oracle := newOpStackL1GasOracle(logger.Test(t), ethClient, config.ChainOptimismBedrock, oracleAddress) - _, err := oracle.GetDAGasPrice(testutils.Context(t)) + _, err := oracle.GetDAGasPrice(tests.Context(t)) assert.Error(t, err) }) } diff --git a/core/chains/evm/gas/suggested_price_estimator_test.go b/core/chains/evm/gas/suggested_price_estimator_test.go index 4f3c4d307d6..e6dbb80df92 100644 --- a/core/chains/evm/gas/suggested_price_estimator_test.go +++ b/core/chains/evm/gas/suggested_price_estimator_test.go @@ -12,11 +12,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" rollupMocks "github.com/smartcontractkit/chainlink/v2/core/chains/evm/gas/rollups/mocks" - "github.com/smartcontractkit/chainlink/v2/core/internal/testutils" ) func TestSuggestedPriceEstimator(t *testing.T) { @@ -34,7 +35,7 @@ func TestSuggestedPriceEstimator(t *testing.T) { l1Oracle := rollupMocks.NewL1Oracle(t) o := gas.NewSuggestedPriceEstimator(logger.Test(t), feeEstimatorClient, cfg, 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") }) @@ -49,7 +50,7 @@ func TestSuggestedPriceEstimator(t *testing.T) { o := gas.NewSuggestedPriceEstimator(logger.Test(t), feeEstimatorClient, cfg, 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) assert.Equal(t, assets.NewWeiI(42), gasPrice) assert.Equal(t, gasLimit, chainSpecificGasLimit) @@ -67,7 +68,7 @@ func TestSuggestedPriceEstimator(t *testing.T) { }) 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) @@ -86,7 +87,7 @@ func TestSuggestedPriceEstimator(t *testing.T) { }) 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) @@ -102,7 +103,7 @@ func TestSuggestedPriceEstimator(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") }) @@ -111,7 +112,7 @@ func TestSuggestedPriceEstimator(t *testing.T) { l1Oracle := rollupMocks.NewL1Oracle(t) o := gas.NewSuggestedPriceEstimator(logger.Test(t), feeEstimatorClient, cfg, 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") }) @@ -120,7 +121,7 @@ func TestSuggestedPriceEstimator(t *testing.T) { l1Oracle := rollupMocks.NewL1Oracle(t) o := gas.NewSuggestedPriceEstimator(logger.Test(t), feeEstimatorClient, cfg, l1Oracle) - _, _, err := o.BumpLegacyGas(testutils.Context(t), assets.NewWeiI(42), gasLimit, maxGasPrice, nil) + _, _, err := o.BumpLegacyGas(tests.Context(t), assets.NewWeiI(42), gasLimit, maxGasPrice, nil) assert.EqualError(t, err, "estimator is not started") }) @@ -133,7 +134,7 @@ func TestSuggestedPriceEstimator(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") }) @@ -148,7 +149,7 @@ func TestSuggestedPriceEstimator(t *testing.T) { o := gas.NewSuggestedPriceEstimator(logger.Test(t), feeEstimatorClient, cfg, l1Oracle) servicetest.RunHealthy(t, o) - gasPrice, chainSpecificGasLimit, err := o.BumpLegacyGas(testutils.Context(t), assets.NewWeiI(10), gasLimit, maxGasPrice, nil) + gasPrice, chainSpecificGasLimit, err := o.BumpLegacyGas(tests.Context(t), assets.NewWeiI(10), gasLimit, maxGasPrice, nil) require.NoError(t, err) assert.Equal(t, assets.NewWeiI(44), gasPrice) assert.Equal(t, gasLimit, chainSpecificGasLimit) @@ -166,7 +167,7 @@ func TestSuggestedPriceEstimator(t *testing.T) { testCfg := &gas.MockGasEstimatorConfig{BumpPercentF: 1, BumpMinF: assets.NewWei(big.NewInt(1)), BumpThresholdF: 1, LimitMultiplierF: 1} o := gas.NewSuggestedPriceEstimator(logger.Test(t), feeEstimatorClient, testCfg, l1Oracle) servicetest.RunHealthy(t, o) - gasPrice, chainSpecificGasLimit, err := o.BumpLegacyGas(testutils.Context(t), assets.NewWeiI(10), gasLimit, maxGasPrice, nil) + gasPrice, chainSpecificGasLimit, err := o.BumpLegacyGas(tests.Context(t), assets.NewWeiI(10), gasLimit, maxGasPrice, nil) require.NoError(t, err) assert.Equal(t, assets.NewWeiI(41), gasPrice) assert.Equal(t, gasLimit, chainSpecificGasLimit) @@ -183,7 +184,7 @@ func TestSuggestedPriceEstimator(t *testing.T) { o := gas.NewSuggestedPriceEstimator(logger.Test(t), feeEstimatorClient, cfg, l1Oracle) servicetest.RunHealthy(t, o) - gasPrice, chainSpecificGasLimit, err := o.BumpLegacyGas(testutils.Context(t), assets.NewWeiI(10), gasLimit, maxGasPrice, nil) + gasPrice, chainSpecificGasLimit, err := o.BumpLegacyGas(tests.Context(t), assets.NewWeiI(10), gasLimit, maxGasPrice, nil) require.NoError(t, err) assert.Equal(t, assets.NewWeiI(10), gasPrice) assert.Equal(t, gasLimit, chainSpecificGasLimit) @@ -201,7 +202,7 @@ func TestSuggestedPriceEstimator(t *testing.T) { }) servicetest.RunHealthy(t, o) - gasPrice, chainSpecificGasLimit, err := o.BumpLegacyGas(testutils.Context(t), assets.NewWeiI(10), gasLimit, assets.NewWeiI(40), nil) + gasPrice, chainSpecificGasLimit, err := o.BumpLegacyGas(tests.Context(t), assets.NewWeiI(10), gasLimit, assets.NewWeiI(40), nil) 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) @@ -220,7 +221,7 @@ func TestSuggestedPriceEstimator(t *testing.T) { }) servicetest.RunHealthy(t, o) - gasPrice, chainSpecificGasLimit, err := o.BumpLegacyGas(testutils.Context(t), assets.NewWeiI(10), gasLimit, assets.NewWeiI(40), nil) + gasPrice, chainSpecificGasLimit, err := o.BumpLegacyGas(tests.Context(t), assets.NewWeiI(10), gasLimit, assets.NewWeiI(40), nil) require.NoError(t, err) assert.Equal(t, assets.NewWeiI(40), gasPrice) assert.Equal(t, gasLimit, chainSpecificGasLimit) @@ -236,7 +237,7 @@ func TestSuggestedPriceEstimator(t *testing.T) { servicetest.RunHealthy(t, o) - _, _, err := o.BumpLegacyGas(testutils.Context(t), assets.NewWeiI(10), gasLimit, maxGasPrice, nil) + _, _, err := o.BumpLegacyGas(tests.Context(t), assets.NewWeiI(10), gasLimit, maxGasPrice, nil) assert.EqualError(t, err, "failed to refresh and return gas; gas price not set") }) @@ -254,7 +255,7 @@ func TestSuggestedPriceEstimator(t *testing.T) { servicetest.RunHealthy(t, o) - gasPrice, chainSpecificGasLimit, err := o.BumpLegacyGas(testutils.Context(t), assets.NewWeiI(10), gasLimit, maxGasPrice, nil) + gasPrice, chainSpecificGasLimit, err := o.BumpLegacyGas(tests.Context(t), assets.NewWeiI(10), gasLimit, maxGasPrice, nil) require.NoError(t, err) assert.Equal(t, assets.NewWeiI(44), gasPrice) assert.Equal(t, gasLimit, chainSpecificGasLimit) diff --git a/core/internal/cltest/cltest.go b/core/internal/cltest/cltest.go index ca9c1bdfcd5..f6a127a309d 100644 --- a/core/internal/cltest/cltest.go +++ b/core/internal/cltest/cltest.go @@ -1030,20 +1030,6 @@ func LegacyTransactionsFromGasPricesTxType(code evmtypes.TxType, gasPrices ...in return txs } -// DynamicFeeTransactionsFromTipCaps returns EIP-1559 transactions with the -// given TipCaps (FeeCap is arbitrary) -func DynamicFeeTransactionsFromTipCaps(tipCaps ...int64) []evmtypes.Transaction { - return DynamicFeeTransactionsFromTipCapsTxType(0x02, tipCaps...) -} - -func DynamicFeeTransactionsFromTipCapsTxType(code evmtypes.TxType, tipCaps ...int64) []evmtypes.Transaction { - txs := make([]evmtypes.Transaction, len(tipCaps)) - for i, tipCap := range tipCaps { - txs[i] = evmtypes.Transaction{Type: code, MaxPriorityFeePerGas: assets.NewWeiI(tipCap), GasLimit: 42, MaxFeePerGas: assets.GWei(5000)} - } - return txs -} - type TransactionReceipter interface { TransactionReceipt(context.Context, common.Hash) (*types.Receipt, error) }