Skip to content

Commit

Permalink
core/internal/cltest: refactor MustInsertRandomKey* for type safety (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jmank88 authored Oct 10, 2023
1 parent f3a463f commit feedc9b
Show file tree
Hide file tree
Showing 26 changed files with 202 additions and 250 deletions.
6 changes: 3 additions & 3 deletions core/chains/evm/log/orm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestORM_broadcasts(t *testing.T) {

orm := log.NewORM(db, lggr, cfg.Database(), cltest.FixtureChainID)

_, addr := cltest.MustAddRandomKeyToKeystore(t, ethKeyStore)
_, addr := cltest.MustInsertRandomKey(t, ethKeyStore)
specV2 := cltest.MustInsertV2JobSpec(t, db, addr)

const selectQuery = `SELECT consumed FROM log_broadcasts
Expand Down Expand Up @@ -165,10 +165,10 @@ func TestORM_MarkUnconsumed(t *testing.T) {

orm := log.NewORM(db, lggr, cfg.Database(), cltest.FixtureChainID)

_, addr1 := cltest.MustAddRandomKeyToKeystore(t, ethKeyStore)
_, addr1 := cltest.MustInsertRandomKey(t, ethKeyStore)
job1 := cltest.MustInsertV2JobSpec(t, db, addr1)

_, addr2 := cltest.MustAddRandomKeyToKeystore(t, ethKeyStore)
_, addr2 := cltest.MustInsertRandomKey(t, ethKeyStore)
job2 := cltest.MustInsertV2JobSpec(t, db, addr2)

logBefore := cltest.RandomLog(t)
Expand Down
16 changes: 8 additions & 8 deletions core/chains/evm/monitor/balance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func TestBalanceMonitor_Start(t *testing.T) {
db := pgtest.NewSqlxDB(t)
ethKeyStore := cltest.NewKeyStore(t, db, cfg.Database()).Eth()
ethClient := newEthClientMock(t)
_, k0Addr := cltest.MustInsertRandomKey(t, ethKeyStore, 0)
_, k1Addr := cltest.MustInsertRandomKey(t, ethKeyStore, 0)
_, k1Addr := cltest.MustInsertRandomKey(t, ethKeyStore)
_, k0Addr := cltest.MustInsertRandomKey(t, ethKeyStore)

bm := monitor.NewBalanceMonitor(ethClient, ethKeyStore, logger.TestLogger(t))
defer func() { assert.NoError(t, bm.Close()) }()
Expand Down Expand Up @@ -69,7 +69,7 @@ func TestBalanceMonitor_Start(t *testing.T) {
ethKeyStore := cltest.NewKeyStore(t, db, cfg.Database()).Eth()
ethClient := newEthClientMock(t)

_, k0Addr := cltest.MustInsertRandomKey(t, ethKeyStore, 0)
_, k0Addr := cltest.MustInsertRandomKey(t, ethKeyStore)

bm := monitor.NewBalanceMonitor(ethClient, ethKeyStore, logger.TestLogger(t))
defer func() { assert.NoError(t, bm.Close()) }()
Expand All @@ -89,7 +89,7 @@ func TestBalanceMonitor_Start(t *testing.T) {
ethKeyStore := cltest.NewKeyStore(t, db, cfg.Database()).Eth()
ethClient := newEthClientMock(t)

_, k0Addr := cltest.MustInsertRandomKey(t, ethKeyStore, 0)
_, k0Addr := cltest.MustInsertRandomKey(t, ethKeyStore)

bm := monitor.NewBalanceMonitor(ethClient, ethKeyStore, logger.TestLogger(t))
defer func() { assert.NoError(t, bm.Close()) }()
Expand Down Expand Up @@ -119,7 +119,7 @@ func TestBalanceMonitor_Start(t *testing.T) {
ethKeyStore := cltest.NewKeyStore(t, db, cfg.Database()).Eth()
ethClient := newEthClientMock(t)

_, k0Addr := cltest.MustInsertRandomKey(t, ethKeyStore, 0)
_, k0Addr := cltest.MustInsertRandomKey(t, ethKeyStore)

bm := monitor.NewBalanceMonitor(ethClient, ethKeyStore, logger.TestLogger(t))
defer func() { assert.NoError(t, bm.Close()) }()
Expand All @@ -146,8 +146,8 @@ func TestBalanceMonitor_OnNewLongestChain_UpdatesBalance(t *testing.T) {
ethKeyStore := cltest.NewKeyStore(t, db, cfg.Database()).Eth()
ethClient := newEthClientMock(t)

_, k0Addr := cltest.MustInsertRandomKey(t, ethKeyStore, 0)
_, k1Addr := cltest.MustInsertRandomKey(t, ethKeyStore, 0)
_, k0Addr := cltest.MustInsertRandomKey(t, ethKeyStore)
_, k1Addr := cltest.MustInsertRandomKey(t, ethKeyStore)

bm := monitor.NewBalanceMonitor(ethClient, ethKeyStore, logger.TestLogger(t))
k0bal := big.NewInt(42)
Expand Down Expand Up @@ -197,7 +197,7 @@ func TestBalanceMonitor_FewerRPCCallsWhenBehind(t *testing.T) {
cfg := configtest.NewGeneralConfig(t, nil)
ethKeyStore := cltest.NewKeyStore(t, db, cfg.Database()).Eth()

cltest.MustAddRandomKeyToKeystore(t, ethKeyStore)
cltest.MustInsertRandomKey(t, ethKeyStore)

ethClient := newEthClientMock(t)

Expand Down
38 changes: 19 additions & 19 deletions core/chains/evm/txmgr/broadcaster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestEthBroadcaster_Lifecycle(t *testing.T) {
evmcfg := evmtest.NewChainScopedConfig(t, cfg)
ethClient := evmtest.NewEthClientMockWithDefaultChain(t)
ethKeyStore := cltest.NewKeyStore(t, db, cfg.Database()).Eth()
cltest.MustInsertRandomKeyReturningState(t, ethKeyStore, 0)
cltest.MustInsertRandomKeyReturningState(t, ethKeyStore)
estimator := gasmocks.NewEvmFeeEstimator(t)
txBuilder := txmgr.NewEvmTxAttemptBuilder(*ethClient.ConfiguredChainID(), evmcfg.EVM().GasEstimator(), ethKeyStore, estimator)

Expand Down Expand Up @@ -145,7 +145,7 @@ func TestEthBroadcaster_ProcessUnstartedEthTxs_Success(t *testing.T) {
cfg := configtest.NewTestGeneralConfig(t)
txStore := cltest.NewTestTxStore(t, db, cfg.Database())
ethKeyStore := cltest.NewKeyStore(t, db, cfg.Database()).Eth()
_, fromAddress := cltest.MustInsertRandomKeyReturningState(t, ethKeyStore, 0)
_, fromAddress := cltest.MustInsertRandomKeyReturningState(t, ethKeyStore)

ethClient := evmtest.NewEthClientMockWithDefaultChain(t)
evmcfg := evmtest.NewChainScopedConfig(t, cfg)
Expand All @@ -170,7 +170,7 @@ func TestEthBroadcaster_ProcessUnstartedEthTxs_Success(t *testing.T) {
})

t.Run("eth_txes exist for a different from address", func(t *testing.T) {
_, otherAddress := cltest.MustAddRandomKeyToKeystore(t, ethKeyStore)
_, otherAddress := cltest.MustInsertRandomKey(t, ethKeyStore)
cltest.MustCreateUnstartedTx(t, txStore, otherAddress, toAddress, encodedPayload, gasLimit, value, &cltest.FixtureChainID)
retryable, err := eb.ProcessUnstartedTxs(testutils.Context(t), fromAddress)
assert.NoError(t, err)
Expand Down Expand Up @@ -493,7 +493,7 @@ func TestEthBroadcaster_TransmitChecking(t *testing.T) {
cfg := configtest.NewTestGeneralConfig(t)
txStore := cltest.NewTestTxStore(t, db, cfg.Database())
ethKeyStore := cltest.NewKeyStore(t, db, cfg.Database()).Eth()
_, fromAddress := cltest.MustInsertRandomKeyReturningState(t, ethKeyStore, 0)
_, fromAddress := cltest.MustInsertRandomKeyReturningState(t, ethKeyStore)

ethClient := evmtest.NewEthClientMockWithDefaultChain(t)
evmcfg := evmtest.NewChainScopedConfig(t, cfg)
Expand Down Expand Up @@ -578,7 +578,7 @@ func TestEthBroadcaster_ProcessUnstartedEthTxs_OptimisticLockingOnEthTx(t *testi
evmcfg := txmgr.NewEvmTxmConfig(ccfg.EVM())
ethClient := evmtest.NewEthClientMockWithDefaultChain(t)
ethKeyStore := cltest.NewKeyStore(t, db, cfg.Database()).Eth()
_, fromAddress := cltest.MustInsertRandomKeyReturningState(t, ethKeyStore, 0)
_, fromAddress := cltest.MustInsertRandomKeyReturningState(t, ethKeyStore)
estimator := gasmocks.NewEvmFeeEstimator(t)
txBuilder := txmgr.NewEvmTxAttemptBuilder(*ethClient.ConfiguredChainID(), ccfg.EVM().GasEstimator(), ethKeyStore, estimator)

Expand Down Expand Up @@ -640,7 +640,7 @@ func TestEthBroadcaster_ProcessUnstartedEthTxs_Success_WithMultiplier(t *testing
txStore := cltest.NewTestTxStore(t, db, cfg.Database())

ethKeyStore := cltest.NewKeyStore(t, db, cfg.Database()).Eth()
_, fromAddress := cltest.MustInsertRandomKeyReturningState(t, ethKeyStore, 0)
_, fromAddress := cltest.MustInsertRandomKeyReturningState(t, ethKeyStore)

evmcfg := evmtest.NewChainScopedConfig(t, cfg)

Expand Down Expand Up @@ -687,7 +687,7 @@ func TestEthBroadcaster_ProcessUnstartedEthTxs_ResumingFromCrash(t *testing.T) {
txStore := cltest.NewTestTxStore(t, db, cfg.Database())

ethKeyStore := cltest.NewKeyStore(t, db, cfg.Database()).Eth()
_, fromAddress := cltest.MustInsertRandomKeyReturningState(t, ethKeyStore, nextNonce)
_, fromAddress := cltest.RandomKey{Nonce: nextNonce.Int64()}.MustInsertWithState(t, ethKeyStore)

firstInProgress := txmgr.Tx{
FromAddress: fromAddress,
Expand Down Expand Up @@ -722,7 +722,7 @@ func TestEthBroadcaster_ProcessUnstartedEthTxs_ResumingFromCrash(t *testing.T) {
txStore := cltest.NewTestTxStore(t, db, cfg.Database())

ethKeyStore := cltest.NewKeyStore(t, db, cfg.Database()).Eth()
_, fromAddress := cltest.MustInsertRandomKeyReturningState(t, ethKeyStore, nextNonce)
_, fromAddress := cltest.RandomKey{Nonce: nextNonce.Int64()}.MustInsertWithState(t, ethKeyStore)

ethClient := evmtest.NewEthClientMockWithDefaultChain(t)

Expand Down Expand Up @@ -760,7 +760,7 @@ func TestEthBroadcaster_ProcessUnstartedEthTxs_ResumingFromCrash(t *testing.T) {
txStore := cltest.NewTestTxStore(t, db, cfg.Database())

ethKeyStore := cltest.NewKeyStore(t, db, cfg.Database()).Eth()
_, fromAddress := cltest.MustInsertRandomKeyReturningState(t, ethKeyStore, nextNonce)
_, fromAddress := cltest.RandomKey{Nonce: nextNonce.Int64()}.MustInsertWithState(t, ethKeyStore)

ethClient := evmtest.NewEthClientMockWithDefaultChain(t)

Expand Down Expand Up @@ -798,7 +798,7 @@ func TestEthBroadcaster_ProcessUnstartedEthTxs_ResumingFromCrash(t *testing.T) {
txStore := cltest.NewTestTxStore(t, db, cfg.Database())

ethKeyStore := cltest.NewKeyStore(t, db, cfg.Database()).Eth()
_, fromAddress := cltest.MustInsertRandomKeyReturningState(t, ethKeyStore, nextNonce)
_, fromAddress := cltest.RandomKey{Nonce: nextNonce.Int64()}.MustInsertWithState(t, ethKeyStore)

ethClient := evmtest.NewEthClientMockWithDefaultChain(t)

Expand Down Expand Up @@ -835,7 +835,7 @@ func TestEthBroadcaster_ProcessUnstartedEthTxs_ResumingFromCrash(t *testing.T) {
txStore := cltest.NewTestTxStore(t, db, cfg.Database())

ethKeyStore := cltest.NewKeyStore(t, db, cfg.Database()).Eth()
_, fromAddress := cltest.MustInsertRandomKeyReturningState(t, ethKeyStore, nextNonce)
_, fromAddress := cltest.RandomKey{Nonce: nextNonce.Int64()}.MustInsertWithState(t, ethKeyStore)

ethClient := evmtest.NewEthClientMockWithDefaultChain(t)

Expand Down Expand Up @@ -874,7 +874,7 @@ func TestEthBroadcaster_ProcessUnstartedEthTxs_ResumingFromCrash(t *testing.T) {
txStore := cltest.NewTestTxStore(t, db, cfg.Database())

ethKeyStore := cltest.NewKeyStore(t, db, cfg.Database()).Eth()
_, fromAddress := cltest.MustInsertRandomKeyReturningState(t, ethKeyStore, nextNonce)
_, fromAddress := cltest.RandomKey{Nonce: nextNonce.Int64()}.MustInsertWithState(t, ethKeyStore)

ethClient := evmtest.NewEthClientMockWithDefaultChain(t)

Expand Down Expand Up @@ -911,7 +911,7 @@ func TestEthBroadcaster_ProcessUnstartedEthTxs_ResumingFromCrash(t *testing.T) {
txStore := cltest.NewTestTxStore(t, db, cfg.Database())

ethKeyStore := cltest.NewKeyStore(t, db, cfg.Database()).Eth()
_, fromAddress := cltest.MustInsertRandomKeyReturningState(t, ethKeyStore, nextNonce)
_, fromAddress := cltest.RandomKey{Nonce: nextNonce.Int64()}.MustInsertWithState(t, ethKeyStore)

cfg := configtest.NewGeneralConfig(t, func(c *chainlink.Config, s *chainlink.Secrets) {
// Configured gas price changed
Expand Down Expand Up @@ -982,7 +982,7 @@ func TestEthBroadcaster_ProcessUnstartedEthTxs_Errors(t *testing.T) {
txStore := cltest.NewTestTxStore(t, db, cfg.Database())

ethKeyStore := cltest.NewKeyStore(t, db, cfg.Database()).Eth()
_, fromAddress := cltest.MustInsertRandomKeyReturningState(t, ethKeyStore, 0)
_, fromAddress := cltest.MustInsertRandomKeyReturningState(t, ethKeyStore)

evmcfg := evmtest.NewChainScopedConfig(t, cfg)
ethClient := evmtest.NewEthClientMockWithDefaultChain(t)
Expand Down Expand Up @@ -1682,7 +1682,7 @@ func TestEthBroadcaster_GetNextNonce(t *testing.T) {
cfg := configtest.NewGeneralConfig(t, nil)

ethKeyStore := cltest.NewKeyStore(t, db, cfg.Database()).Eth()
keyState, _ := cltest.MustInsertRandomKeyReturningState(t, ethKeyStore, 0)
keyState, _ := cltest.MustInsertRandomKeyReturningState(t, ethKeyStore)

nonce := getLocalNextNonce(t, ethKeyStore, keyState.Address.Address())
require.NotNil(t, nonce)
Expand All @@ -1694,7 +1694,7 @@ func TestEthBroadcaster_IncrementNextNonce(t *testing.T) {
cfg := configtest.NewGeneralConfig(t, nil)

ethKeyStore := cltest.NewKeyStore(t, db, cfg.Database()).Eth()
keyState, _ := cltest.MustInsertRandomKeyReturningState(t, ethKeyStore, 0)
keyState, _ := cltest.MustInsertRandomKeyReturningState(t, ethKeyStore)

// Cannot increment if supplied nonce doesn't match existing
require.Error(t, ethKeyStore.IncrementNextSequence(keyState.Address.Address(), &cltest.FixtureChainID, evmtypes.Nonce(42)))
Expand Down Expand Up @@ -1733,7 +1733,7 @@ func TestEthBroadcaster_EthTxInsertEventCausesTriggerToFire(t *testing.T) {
evmcfg := evmtest.NewChainScopedConfig(t, cfg)

ethKeyStore := cltest.NewKeyStore(t, db, cfg.Database()).Eth()
_, fromAddress := cltest.MustAddRandomKeyToKeystore(t, ethKeyStore)
_, fromAddress := cltest.MustInsertRandomKey(t, ethKeyStore)
eventBroadcaster := cltest.NewEventBroadcaster(t, evmcfg.Database().URL())
require.NoError(t, eventBroadcaster.Start(testutils.Context(t)))
t.Cleanup(func() { require.NoError(t, eventBroadcaster.Close()) })
Expand Down Expand Up @@ -1761,8 +1761,8 @@ func TestEthBroadcaster_SyncNonce(t *testing.T) {
txStore := cltest.NewTestTxStore(t, db, cfg.Database())

kst := cltest.NewKeyStore(t, db, cfg.Database()).Eth()
_, fromAddress := cltest.MustInsertRandomKeyReturningState(t, kst, true)
_, disabledAddress := cltest.MustInsertRandomKeyReturningState(t, kst, false)
_, fromAddress := cltest.RandomKey{Disabled: false}.MustInsertWithState(t, kst)
_, disabledAddress := cltest.RandomKey{Disabled: true}.MustInsertWithState(t, kst)

ethNodeNonce := uint64(22)

Expand Down
Loading

0 comments on commit feedc9b

Please sign in to comment.