Skip to content

Commit

Permalink
implement tests for PreloadTxes
Browse files Browse the repository at this point in the history
  • Loading branch information
poopoothegorilla committed Mar 20, 2024
1 parent 2011e06 commit c28a5e7
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion common/txmgr/inmemory_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -973,7 +973,7 @@ func (ms *inMemoryStore[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) Prelo
defer ms.addressStatesLock.RUnlock()
as, ok := ms.addressStates[attempts[0].Tx.FromAddress]
if !ok {
return fmt.Errorf("preload_txes: %w", ErrAddressNotFound)
return nil
}

txIDs := make([]int64, len(attempts))
Expand Down
46 changes: 46 additions & 0 deletions core/chains/evm/txmgr/evm_inmemory_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,52 @@ func TestInMemoryStore_LoadTxAttempts(t *testing.T) {
})
}

func TestInMemoryStore_PreloadTxes(t *testing.T) {
t.Parallel()

db := pgtest.NewSqlxDB(t)
_, dbcfg, evmcfg := evmtxmgr.MakeTestConfigs(t)
persistentStore := cltest.NewTestTxStore(t, db, dbcfg)
kst := cltest.NewKeyStore(t, db, dbcfg)
_, fromAddress := cltest.MustInsertRandomKey(t, kst.Eth())

ethClient := evmtest.NewEthClientMockWithDefaultChain(t)
lggr := logger.TestSugared(t)
chainID := ethClient.ConfiguredChainID()
ctx := context.Background()

inMemoryStore, err := commontxmgr.NewInMemoryStore[
*big.Int,
common.Address, common.Hash, common.Hash,
*evmtypes.Receipt,
evmtypes.Nonce,
evmgas.EvmFee,
](ctx, lggr, chainID, kst.Eth(), persistentStore, evmcfg.Transactions())
require.NoError(t, err)

t.Run("load transaction", func(t *testing.T) {
// insert the transaction into the persistent store
inTx := cltest.MustInsertUnconfirmedEthTxWithBroadcastLegacyAttempt(t, persistentStore, int64(7), fromAddress)
// insert the transaction into the in-memory store
require.NoError(t, inMemoryStore.XXXTestInsertTx(fromAddress, &inTx))

ctx := testutils.Context(t)
expAttempts := []evmtxmgr.TxAttempt{{ID: 0, TxID: inTx.ID}}
expErr := persistentStore.PreloadTxes(ctx, expAttempts)
require.Equal(t, 1, len(expAttempts))
expAttempt := expAttempts[0]

actAttempts := []evmtxmgr.TxAttempt{{ID: 0, TxID: inTx.ID}}
actErr := inMemoryStore.PreloadTxes(ctx, actAttempts)
require.Equal(t, 1, len(actAttempts))
actAttempt := actAttempts[0]

require.NoError(t, expErr)
require.NoError(t, actErr)
assertTxAttemptEqual(t, expAttempt, actAttempt)
})
}

// assertTxEqual asserts that two transactions are equal
func assertTxEqual(t *testing.T, exp, act evmtxmgr.Tx) {
assert.Equal(t, exp.ID, act.ID)
Expand Down

0 comments on commit c28a5e7

Please sign in to comment.