Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
poopoothegorilla committed Mar 21, 2024
1 parent 9352c8e commit e30f622
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
13 changes: 5 additions & 8 deletions common/txmgr/inmemory_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,10 @@ func (ms *inMemoryStore[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) SaveS
ms.addressStatesLock.RLock()
defer ms.addressStatesLock.RUnlock()

if attempt.State != txmgrtypes.TxAttemptInProgress {
return fmt.Errorf("expected state to be in_progress")
}

var tx *txmgrtypes.Tx[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE]
var as *addressState[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]
filter := func(tx *txmgrtypes.Tx[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE]) bool { return true }
Expand All @@ -348,21 +352,14 @@ func (ms *inMemoryStore[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) SaveS
return fmt.Errorf("save_sent_attempt: %w", ErrTxnNotFound)
}

if attempt.State != txmgrtypes.TxAttemptInProgress {
return fmt.Errorf("expected state to be in_progress")
}

// Persist to persistent storage
if err := ms.persistentTxStore.SaveSentAttempt(ctx, timeout, attempt, broadcastAt); err != nil {
return err
}

// Update in memory store
fn := func(tx *txmgrtypes.Tx[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE]) {
if tx.ID != attempt.TxID {
return
}
if tx.TxAttempts == nil || len(tx.TxAttempts) == 0 {
if len(tx.TxAttempts) == 0 {
return
}
if tx.BroadcastAt != nil && tx.BroadcastAt.Before(broadcastAt) {
Expand Down
17 changes: 8 additions & 9 deletions core/chains/evm/txmgr/evm_inmemory_store_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package txmgr_test

import (
"context"
"math/big"
"testing"
"time"
Expand All @@ -28,14 +27,14 @@ func TestInMemoryStore_SaveSentAttempt(t *testing.T) {

db := pgtest.NewSqlxDB(t)
_, dbcfg, evmcfg := evmtxmgr.MakeTestConfigs(t)
persistentStore := cltest.NewTestTxStore(t, db, dbcfg)
persistentStore := cltest.NewTestTxStore(t, db)
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()
ctx := testutils.Context(t)

inMemoryStore, err := commontxmgr.NewInMemoryStore[
*big.Int,
Expand All @@ -56,14 +55,14 @@ func TestInMemoryStore_SaveSentAttempt(t *testing.T) {
require.NoError(t, inMemoryStore.XXXTestInsertTx(fromAddress, &inTx))

err := inMemoryStore.SaveSentAttempt(
testutils.Context(t),
ctx,
defaultDuration,
&inTx.TxAttempts[0],
now,
)
require.NoError(t, err)

expTx, err := persistentStore.FindTxWithAttempts(inTx.ID)
expTx, err := persistentStore.FindTxWithAttempts(ctx, inTx.ID)
require.NoError(t, err)
fn := func(tx *evmtxmgr.Tx) bool { return true }
actTxs := inMemoryStore.XXXTestFindTxs(nil, fn, inTx.ID)
Expand All @@ -74,16 +73,16 @@ func TestInMemoryStore_SaveSentAttempt(t *testing.T) {

// wrong tx id
inTx.TxAttempts[0].TxID = 123
actErr := inMemoryStore.SaveSentAttempt(testutils.Context(t), defaultDuration, &inTx.TxAttempts[0], now)
expErr := persistentStore.SaveSentAttempt(testutils.Context(t), defaultDuration, &inTx.TxAttempts[0], now)
actErr := inMemoryStore.SaveSentAttempt(ctx, defaultDuration, &inTx.TxAttempts[0], now)
expErr := persistentStore.SaveSentAttempt(ctx, defaultDuration, &inTx.TxAttempts[0], now)
assert.Error(t, actErr)
assert.Error(t, expErr)
inTx.TxAttempts[0].TxID = inTx.ID // reset

// wrong attempt state
inTx.TxAttempts[0].State = txmgrtypes.TxAttemptBroadcast
actErr = inMemoryStore.SaveSentAttempt(testutils.Context(t), defaultDuration, &inTx.TxAttempts[0], now)
expErr = persistentStore.SaveSentAttempt(testutils.Context(t), defaultDuration, &inTx.TxAttempts[0], now)
actErr = inMemoryStore.SaveSentAttempt(ctx, defaultDuration, &inTx.TxAttempts[0], now)
expErr = persistentStore.SaveSentAttempt(ctx, defaultDuration, &inTx.TxAttempts[0], now)
assert.Error(t, actErr)
assert.Error(t, expErr)
inTx.TxAttempts[0].State = txmgrtypes.TxAttemptInProgress // reset
Expand Down

0 comments on commit e30f622

Please sign in to comment.