Skip to content

Commit

Permalink
Cleanup Core packages from txm tests (#13449)
Browse files Browse the repository at this point in the history
* Cleanup Core packages from txm tests

* Add changeset
  • Loading branch information
dimriou authored Jun 7, 2024
1 parent 0d95942 commit 69a95d8
Show file tree
Hide file tree
Showing 14 changed files with 512 additions and 509 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilly-ladybugs-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"chainlink": patch
---

Cleanup txm tests #internal
5 changes: 5 additions & 0 deletions core/chains/evm/testutils/evmtypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,8 @@ func NewLegacyTransaction(nonce uint64, to common.Address, value *big.Int, gasLi
}
return types.NewTx(&tx)
}

func NewAddressPtr() *common.Address {
a := common.BytesToAddress(randomBytes(20))
return &a
}
55 changes: 27 additions & 28 deletions core/chains/evm/txmgr/attempts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ 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/config/toml"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/gas"
gasmocks "github.com/smartcontractkit/chainlink/v2/core/chains/evm/gas/mocks"
ksmocks "github.com/smartcontractkit/chainlink/v2/core/chains/evm/keystore/mocks"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/testutils"
"github.com/smartcontractkit/chainlink/v2/core/chains/evm/txmgr"
evmtypes "github.com/smartcontractkit/chainlink/v2/core/chains/evm/types"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils/configtest"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest"
"github.com/smartcontractkit/chainlink/v2/core/services/chainlink"
)

func NewEvmAddress() gethcommon.Address {
Expand Down Expand Up @@ -72,7 +72,7 @@ func TestTxm_SignTx(t *testing.T) {
kst := ksmocks.NewEth(t)
kst.On("SignTx", mock.Anything, to, tx, chainID).Return(tx, nil).Once()
cks := txmgr.NewEvmTxAttemptBuilder(*chainID, newFeeConfig(), kst, nil)
hash, rawBytes, err := cks.SignTx(testutils.Context(t), addr, tx)
hash, rawBytes, err := cks.SignTx(tests.Context(t), addr, tx)
require.NoError(t, err)
require.NotNil(t, rawBytes)
require.Equal(t, "0xdd68f554373fdea7ec6713a6e437e7646465d553a6aa0b43233093366cc87ef0", hash.String())
Expand All @@ -83,7 +83,7 @@ func TestTxm_SignTx(t *testing.T) {
kst := ksmocks.NewEth(t)
kst.On("SignTx", mock.Anything, to, tx, chainID).Return(tx, nil).Once()
cks := txmgr.NewEvmTxAttemptBuilder(*chainID, newFeeConfig(), kst, nil)
hash, rawBytes, err := cks.SignTx(testutils.Context(t), addr, tx)
hash, rawBytes, err := cks.SignTx(tests.Context(t), addr, tx)
require.NoError(t, err)
require.NotNil(t, rawBytes)
require.Equal(t, "0xdd68f554373fdea7ec6713a6e437e7646465d553a6aa0b43233093366cc87ef0", hash.String())
Expand All @@ -94,7 +94,7 @@ func TestTxm_SignTx(t *testing.T) {
kst.On("SignTx", mock.Anything, to, tx, chainID).Return(tx, nil).Once()
cks := txmgr.NewEvmTxAttemptBuilder(*chainID, newFeeConfig(), kst, nil)

_, rawBytes, err := cks.SignTx(testutils.Context(t), addr, tx)
_, rawBytes, err := cks.SignTx(tests.Context(t), addr, tx)
require.NoError(t, err)
require.NotNil(t, rawBytes)
require.Equal(t, "0xe42a82015681f294b921f7763960b296b9cbad586ff066a18d749724818e83010203808080", hexutil.Encode(rawBytes))
Expand All @@ -116,7 +116,7 @@ func TestTxm_SignTx(t *testing.T) {
})
kst.On("SignTx", mock.Anything, to, typedTx, chainID).Return(typedTx, nil).Once()
cks := txmgr.NewEvmTxAttemptBuilder(*chainID, newFeeConfig(), kst, nil)
_, rawBytes, err := cks.SignTx(testutils.Context(t), addr, typedTx)
_, rawBytes, err := cks.SignTx(tests.Context(t), addr, typedTx)
require.NoError(t, err)
require.NotNil(t, rawBytes)
require.Equal(t, "0xa702e5802a808081f294b921f7763960b296b9cbad586ff066a18d749724818e83010203c0808080", hexutil.Encode(rawBytes))
Expand All @@ -141,7 +141,7 @@ func TestTxm_NewDynamicFeeTx(t *testing.T) {
feeCfg.priceMax = assets.GWei(200)
cks := txmgr.NewEvmTxAttemptBuilder(*big.NewInt(1), feeCfg, kst, nil)
dynamicFee := gas.DynamicFee{TipCap: assets.GWei(100), FeeCap: assets.GWei(200)}
a, _, err := cks.NewCustomTxAttempt(testutils.Context(t), txmgr.Tx{Sequence: &n, FromAddress: addr}, gas.EvmFee{
a, _, err := cks.NewCustomTxAttempt(tests.Context(t), txmgr.Tx{Sequence: &n, FromAddress: addr}, gas.EvmFee{
DynamicTipCap: dynamicFee.TipCap,
DynamicFeeCap: dynamicFee.FeeCap,
}, 100, 0x2, lggr)
Expand All @@ -155,35 +155,34 @@ func TestTxm_NewDynamicFeeTx(t *testing.T) {
})

t.Run("verifies gas tip and fees", func(t *testing.T) {
tests := []struct {
cases := []struct {
name string
tipcap *assets.Wei
feecap *assets.Wei
setCfg func(*chainlink.Config, *chainlink.Secrets)
setCfg func(c *toml.EVMConfig)
expectError string
}{
{"gas tip = fee cap", assets.GWei(5), assets.GWei(5), nil, ""},
{"gas tip < fee cap", assets.GWei(4), assets.GWei(5), nil, ""},
{"gas tip > fee cap", assets.GWei(6), assets.GWei(5), nil, "gas fee cap must be greater than or equal to gas tip cap (fee cap: 5 gwei, tip cap: 6 gwei)"},
{"fee cap exceeds max allowed", assets.GWei(5), assets.GWei(5), func(c *chainlink.Config, s *chainlink.Secrets) {
c.EVM[0].GasEstimator.PriceMax = assets.GWei(4)
{"fee cap exceeds max allowed", assets.GWei(5), assets.GWei(5), func(c *toml.EVMConfig) {
c.GasEstimator.PriceMax = assets.GWei(4)
}, "specified gas fee cap of 5 gwei would exceed max configured gas price of 4 gwei"},
{"ignores global min gas price", assets.GWei(5), assets.GWei(5), func(c *chainlink.Config, s *chainlink.Secrets) {
c.EVM[0].GasEstimator.PriceMin = assets.GWei(6)
{"ignores global min gas price", assets.GWei(5), assets.GWei(5), func(c *toml.EVMConfig) {
c.GasEstimator.PriceMin = assets.GWei(6)
}, ""},
{"tip cap below min allowed", assets.GWei(5), assets.GWei(5), func(c *chainlink.Config, s *chainlink.Secrets) {
c.EVM[0].GasEstimator.TipCapMin = assets.GWei(6)
{"tip cap below min allowed", assets.GWei(5), assets.GWei(5), func(c *toml.EVMConfig) {
c.GasEstimator.TipCapMin = assets.GWei(6)
}, "specified gas tip cap of 5 gwei is below min configured gas tip of 6 gwei"},
}

for _, tt := range tests {
for _, tt := range cases {
test := tt
t.Run(test.name, func(t *testing.T) {
gcfg := configtest.NewGeneralConfig(t, test.setCfg)
cfg := evmtest.NewChainScopedConfig(t, gcfg)
cfg := testutils.NewTestChainScopedConfig(t, test.setCfg)
cks := txmgr.NewEvmTxAttemptBuilder(*big.NewInt(1), cfg.EVM().GasEstimator(), kst, nil)
dynamicFee := gas.DynamicFee{TipCap: test.tipcap, FeeCap: test.feecap}
_, _, err := cks.NewCustomTxAttempt(testutils.Context(t), txmgr.Tx{Sequence: &n, FromAddress: addr}, gas.EvmFee{
_, _, err := cks.NewCustomTxAttempt(tests.Context(t), txmgr.Tx{Sequence: &n, FromAddress: addr}, gas.EvmFee{
DynamicTipCap: dynamicFee.TipCap,
DynamicFeeCap: dynamicFee.FeeCap,
}, 100, 0x2, lggr)
Expand All @@ -210,7 +209,7 @@ func TestTxm_NewLegacyAttempt(t *testing.T) {

t.Run("creates attempt with fields", func(t *testing.T) {
var n evmtypes.Nonce
a, _, err := cks.NewCustomTxAttempt(testutils.Context(t), txmgr.Tx{Sequence: &n, FromAddress: addr}, gas.EvmFee{Legacy: assets.NewWeiI(25)}, 100, 0x0, lggr)
a, _, err := cks.NewCustomTxAttempt(tests.Context(t), txmgr.Tx{Sequence: &n, FromAddress: addr}, gas.EvmFee{Legacy: assets.NewWeiI(25)}, 100, 0x0, lggr)
require.NoError(t, err)
assert.Equal(t, 100, int(a.ChainSpecificFeeLimit))
assert.NotNil(t, a.TxFee.Legacy)
Expand All @@ -220,7 +219,7 @@ func TestTxm_NewLegacyAttempt(t *testing.T) {
})

t.Run("verifies max gas price", func(t *testing.T) {
_, _, err := cks.NewCustomTxAttempt(testutils.Context(t), txmgr.Tx{FromAddress: addr}, gas.EvmFee{Legacy: assets.NewWeiI(100)}, 100, 0x0, lggr)
_, _, err := cks.NewCustomTxAttempt(tests.Context(t), txmgr.Tx{FromAddress: addr}, gas.EvmFee{Legacy: assets.NewWeiI(100)}, 100, 0x0, lggr)
require.Error(t, err)
assert.Contains(t, err.Error(), fmt.Sprintf("specified gas price of 100 wei would exceed max configured gas price of 50 wei for key %s", addr.String()))
})
Expand All @@ -243,7 +242,7 @@ func TestTxm_NewPurgeAttempt(t *testing.T) {
est.On("BumpFee", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(bumpedFee, uint64(10_000), nil)
cks := txmgr.NewEvmTxAttemptBuilder(*big.NewInt(1), gc, kst, est)
lggr := logger.Test(t)
ctx := testutils.Context(t)
ctx := tests.Context(t)

t.Run("creates legacy purge attempt with fields if previous attempt is legacy", func(t *testing.T) {
n := evmtypes.Nonce(0)
Expand Down Expand Up @@ -318,21 +317,21 @@ func TestTxm_NewCustomTxAttempt_NonRetryableErrors(t *testing.T) {
legacyFee := assets.NewWeiI(100)

t.Run("dynamic fee with legacy tx type", func(t *testing.T) {
_, retryable, err := cks.NewCustomTxAttempt(testutils.Context(t), txmgr.Tx{}, gas.EvmFee{
_, retryable, err := cks.NewCustomTxAttempt(tests.Context(t), txmgr.Tx{}, gas.EvmFee{
DynamicTipCap: dynamicFee.TipCap,
DynamicFeeCap: dynamicFee.FeeCap,
}, 100, 0x0, lggr)
require.Error(t, err)
assert.False(t, retryable)
})
t.Run("legacy fee with dynamic tx type", func(t *testing.T) {
_, retryable, err := cks.NewCustomTxAttempt(testutils.Context(t), txmgr.Tx{}, gas.EvmFee{Legacy: legacyFee}, 100, 0x2, lggr)
_, retryable, err := cks.NewCustomTxAttempt(tests.Context(t), txmgr.Tx{}, gas.EvmFee{Legacy: legacyFee}, 100, 0x2, lggr)
require.Error(t, err)
assert.False(t, retryable)
})

t.Run("invalid type", func(t *testing.T) {
_, retryable, err := cks.NewCustomTxAttempt(testutils.Context(t), txmgr.Tx{}, gas.EvmFee{}, 100, 0xA, lggr)
_, retryable, err := cks.NewCustomTxAttempt(tests.Context(t), txmgr.Tx{}, gas.EvmFee{}, 100, 0xA, lggr)
require.Error(t, err)
assert.False(t, retryable)
})
Expand All @@ -345,7 +344,7 @@ func TestTxm_EvmTxAttemptBuilder_RetryableEstimatorError(t *testing.T) {

kst := ksmocks.NewEth(t)
lggr := logger.Test(t)
ctx := testutils.Context(t)
ctx := tests.Context(t)
cks := txmgr.NewEvmTxAttemptBuilder(*big.NewInt(1), &feeConfig{eip1559DynamicFees: true}, kst, est)

t.Run("NewAttempt", func(t *testing.T) {
Expand Down
Loading

0 comments on commit 69a95d8

Please sign in to comment.