Skip to content

Commit

Permalink
Adjusted to align as close as possible with the ChainWriter interface
Browse files Browse the repository at this point in the history
  • Loading branch information
amit-momin committed May 28, 2024
1 parent 6bf577a commit 6bc39ad
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 86 deletions.
72 changes: 31 additions & 41 deletions common/txmgr/mocks/tx_manager.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 11 additions & 9 deletions common/txmgr/txmgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type TxManager[
FindEarliestUnconfirmedBroadcastTime(ctx context.Context) (nullv4.Time, error)
FindEarliestUnconfirmedTxAttemptBlock(ctx context.Context) (nullv4.Int, error)
CountTransactionsByState(ctx context.Context, state txmgrtypes.TxState) (count uint32, err error)
TxStatusByIdempotencyKey(ctx context.Context, idempotency string) (txmgrtypes.TxState, client.TxError, error)
GetTransactionStatus(ctx context.Context, transactionID uuid.UUID) (state txmgrtypes.TxState, err error)
}

type reset struct {
Expand Down Expand Up @@ -632,14 +632,16 @@ func (b *Txm[CHAIN_ID, HEAD, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) CountTrans
return b.txStore.CountTransactionsByState(ctx, state, b.chainID)
}

func (b *Txm[CHAIN_ID, HEAD, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) TxStatusByIdempotencyKey(ctx context.Context, idempotencyKey string) (state txmgrtypes.TxState, txErr client.TxError, err error) {
tx, _ := b.txStore.FindTxWithIdempotencyKey(ctx, idempotencyKey, b.chainID)
// this check is more complete since a no-rows error returns nil err
func (b *Txm[CHAIN_ID, HEAD, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) GetTransactionStatus(ctx context.Context, transactionID uuid.UUID) (status txmgrtypes.TxState, err error) {
tx, err := b.txStore.FindTxWithIdempotencyKey(ctx, transactionID.String(), b.chainID)
if err != nil {
return status, fmt.Errorf("failed to find transaction with IdempotencyKey %s: %w", transactionID.String(), err)
}
// This check is required since a no-rows error returns nil err
if tx == nil {
return "", txErr, fmt.Errorf("failed to find transaction with IdempotencyKey: %s", idempotencyKey)
return status, fmt.Errorf("failed to find transaction with IdempotencyKey %s", transactionID.String())
}
txErr = b.newTxError(tx.GetError())
return tx.State, txErr, nil
return tx.State, tx.GetError()
}

type NullTxManager[
Expand Down Expand Up @@ -725,8 +727,8 @@ func (n *NullTxManager[CHAIN_ID, HEAD, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE]) Cou
return count, errors.New(n.ErrMsg)
}

func (n *NullTxManager[CHAIN_ID, HEAD, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE]) TxStatusByIdempotencyKey(ctx context.Context, idempotencyKey string) (state txmgrtypes.TxState, txErr client.TxError, err error) {
return "", txErr, nil
func (n *NullTxManager[CHAIN_ID, HEAD, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE]) GetTransactionStatus(ctx context.Context, transactionID uuid.UUID) (state txmgrtypes.TxState, err error) {
return
}

func (b *Txm[CHAIN_ID, HEAD, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) pruneQueueAndCreateTxn(
Expand Down
68 changes: 32 additions & 36 deletions core/chains/evm/txmgr/txmgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -604,59 +604,59 @@ func TestTxm_TxStatusByIdempotencyKey(t *testing.T) {
require.NoError(t, err)

t.Run("returns error if transaction not found", func(t *testing.T) {
idempotencyKey := uuid.New().String()
state, txErr, err := txm.TxStatusByIdempotencyKey(ctx, idempotencyKey)
idempotencyKey := uuid.New()
state, err := txm.GetTransactionStatus(ctx, idempotencyKey)
require.Error(t, err, fmt.Sprintf("failed to find transaction with IdempotencyKey: %s", idempotencyKey))
require.Equal(t, txmgrtypes.TxState(""), state)
require.Nil(t, txErr)
})

t.Run("returns unstarted state", func(t *testing.T) {
idempotencyKey := uuid.New().String()
idempotencyKey := uuid.New()
idempotencyKeyStr := idempotencyKey.String()
_, fromAddress := cltest.MustInsertRandomKey(t, ethKeyStore)
tx := &txmgr.Tx{
IdempotencyKey: &idempotencyKey,
IdempotencyKey: &idempotencyKeyStr,
FromAddress: fromAddress,
EncodedPayload: []byte{1, 2, 3},
FeeLimit: feeLimit,
State: txmgrcommon.TxUnstarted,
}
err := txStore.InsertTx(ctx, tx)
require.NoError(t, err)
state, txErr, err := txm.TxStatusByIdempotencyKey(ctx, idempotencyKey)
state, err := txm.GetTransactionStatus(ctx, idempotencyKey)
require.NoError(t, err)
require.Equal(t, txmgrcommon.TxUnstarted, state)
require.Nil(t, txErr)
})

t.Run("returns in-progress state", func(t *testing.T) {
idempotencyKey := uuid.New().String()
idempotencyKey := uuid.New()
idempotencyKeyStr := idempotencyKey.String()
_, fromAddress := cltest.MustInsertRandomKey(t, ethKeyStore)
nonce := evmtypes.Nonce(0)
tx := &txmgr.Tx{
Sequence: &nonce,
IdempotencyKey: &idempotencyKey,
IdempotencyKey: &idempotencyKeyStr,
FromAddress: fromAddress,
EncodedPayload: []byte{1, 2, 3},
FeeLimit: feeLimit,
State: txmgrcommon.TxInProgress,
}
err := txStore.InsertTx(ctx, tx)
require.NoError(t, err)
state, txErr, err := txm.TxStatusByIdempotencyKey(ctx, idempotencyKey)
state, err := txm.GetTransactionStatus(ctx, idempotencyKey)
require.NoError(t, err)
require.Equal(t, txmgrcommon.TxInProgress, state)
require.Nil(t, txErr)
})

t.Run("returns unconfirmed state", func(t *testing.T) {
idempotencyKey := uuid.New().String()
idempotencyKey := uuid.New()
idempotencyKeyStr := idempotencyKey.String()
_, fromAddress := cltest.MustInsertRandomKey(t, ethKeyStore)
nonce := evmtypes.Nonce(0)
broadcast := time.Now()
tx := &txmgr.Tx{
Sequence: &nonce,
IdempotencyKey: &idempotencyKey,
IdempotencyKey: &idempotencyKeyStr,
FromAddress: fromAddress,
EncodedPayload: []byte{1, 2, 3},
FeeLimit: feeLimit,
Expand All @@ -666,20 +666,20 @@ func TestTxm_TxStatusByIdempotencyKey(t *testing.T) {
}
err := txStore.InsertTx(ctx, tx)
require.NoError(t, err)
state, txErr, err := txm.TxStatusByIdempotencyKey(ctx, idempotencyKey)
state, err := txm.GetTransactionStatus(ctx, idempotencyKey)
require.NoError(t, err)
require.Equal(t, txmgrcommon.TxUnconfirmed, state)
require.Nil(t, txErr)
})

t.Run("returns confirmed state", func(t *testing.T) {
idempotencyKey := uuid.New().String()
idempotencyKey := uuid.New()
idempotencyKeyStr := idempotencyKey.String()
_, fromAddress := cltest.MustInsertRandomKey(t, ethKeyStore)
nonce := evmtypes.Nonce(0)
broadcast := time.Now()
tx := &txmgr.Tx{
Sequence: &nonce,
IdempotencyKey: &idempotencyKey,
IdempotencyKey: &idempotencyKeyStr,
FromAddress: fromAddress,
EncodedPayload: []byte{1, 2, 3},
FeeLimit: feeLimit,
Expand All @@ -689,20 +689,20 @@ func TestTxm_TxStatusByIdempotencyKey(t *testing.T) {
}
err := txStore.InsertTx(ctx, tx)
require.NoError(t, err)
state, txErr, err := txm.TxStatusByIdempotencyKey(ctx, idempotencyKey)
state, err := txm.GetTransactionStatus(ctx, idempotencyKey)
require.NoError(t, err)
require.Equal(t, txmgrcommon.TxConfirmed, state)
require.Nil(t, txErr)
})

t.Run("returns confirmed missing receipt state", func(t *testing.T) {
idempotencyKey := uuid.New().String()
idempotencyKey := uuid.New()
idempotencyKeyStr := idempotencyKey.String()
_, fromAddress := cltest.MustInsertRandomKey(t, ethKeyStore)
nonce := evmtypes.Nonce(0)
broadcast := time.Now()
tx := &txmgr.Tx{
Sequence: &nonce,
IdempotencyKey: &idempotencyKey,
IdempotencyKey: &idempotencyKeyStr,
FromAddress: fromAddress,
EncodedPayload: []byte{1, 2, 3},
FeeLimit: feeLimit,
Expand All @@ -712,20 +712,20 @@ func TestTxm_TxStatusByIdempotencyKey(t *testing.T) {
}
err := txStore.InsertTx(ctx, tx)
require.NoError(t, err)
state, txErr, err := txm.TxStatusByIdempotencyKey(ctx, idempotencyKey)
state, err := txm.GetTransactionStatus(ctx, idempotencyKey)
require.NoError(t, err)
require.Equal(t, txmgrcommon.TxConfirmedMissingReceipt, state)
require.Nil(t, txErr)
})

t.Run("returns fatal error state with terminally stuck error", func(t *testing.T) {
idempotencyKey := uuid.New().String()
idempotencyKey := uuid.New()
idempotencyKeyStr := idempotencyKey.String()
_, fromAddress := cltest.MustInsertRandomKey(t, ethKeyStore)
nonce := evmtypes.Nonce(0)
broadcast := time.Now()
tx := &txmgr.Tx{
Sequence: &nonce,
IdempotencyKey: &idempotencyKey,
IdempotencyKey: &idempotencyKeyStr,
FromAddress: fromAddress,
EncodedPayload: []byte{1, 2, 3},
FeeLimit: feeLimit,
Expand All @@ -736,20 +736,18 @@ func TestTxm_TxStatusByIdempotencyKey(t *testing.T) {
}
err := txStore.InsertTx(ctx, tx)
require.NoError(t, err)
state, txErr, err := txm.TxStatusByIdempotencyKey(ctx, idempotencyKey)
require.NoError(t, err)
state, err := txm.GetTransactionStatus(ctx, idempotencyKey)
require.Equal(t, txmgrcommon.TxFatalError, state)
require.NotNil(t, txErr)
require.Equal(t, client.TerminallyStuckMsg, txErr.Error())
require.Equal(t, true, txErr.IsTerminallyStuck())
require.Error(t, err, client.TerminallyStuckMsg)
})

t.Run("returns fatal error state with other error", func(t *testing.T) {
idempotencyKey := uuid.New().String()
idempotencyKey := uuid.New()
idempotencyKeyStr := idempotencyKey.String()
_, fromAddress := cltest.MustInsertRandomKey(t, ethKeyStore)
errorMsg := "something went wrong"
tx := &txmgr.Tx{
IdempotencyKey: &idempotencyKey,
IdempotencyKey: &idempotencyKeyStr,
FromAddress: fromAddress,
EncodedPayload: []byte{1, 2, 3},
FeeLimit: feeLimit,
Expand All @@ -758,11 +756,9 @@ func TestTxm_TxStatusByIdempotencyKey(t *testing.T) {
}
err := txStore.InsertTx(ctx, tx)
require.NoError(t, err)
state, txErr, err := txm.TxStatusByIdempotencyKey(ctx, idempotencyKey)
require.NoError(t, err)
state, err := txm.GetTransactionStatus(ctx, idempotencyKey)
require.Equal(t, txmgrcommon.TxFatalError, state)
require.NotNil(t, txErr)
require.Equal(t, errorMsg, txErr.Error())
require.Error(t, err, errorMsg)
})
}

Expand Down

0 comments on commit 6bc39ad

Please sign in to comment.