Skip to content

Commit

Permalink
implement tests for IsTxFinalized
Browse files Browse the repository at this point in the history
  • Loading branch information
poopoothegorilla committed Mar 20, 2024
1 parent c28a5e7 commit bf5299f
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 4 deletions.
7 changes: 3 additions & 4 deletions common/txmgr/inmemory_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,7 @@ func (ms *inMemoryStore[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) Updat
}
func (ms *inMemoryStore[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) IsTxFinalized(ctx context.Context, blockHeight int64, txID int64, chainID CHAIN_ID) (bool, error) {
if ms.chainID.String() != chainID.String() {
return false, fmt.Errorf("is_tx_finalized: %w", ErrInvalidChainID)
return false, nil
}

txFilter := func(tx *txmgrtypes.Tx[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE]) bool {
Expand All @@ -1020,21 +1020,20 @@ func (ms *inMemoryStore[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) IsTxF
}

for _, attempt := range tx.TxAttempts {
if attempt.Receipts == nil || len(attempt.Receipts) == 0 {
if len(attempt.Receipts) == 0 {
continue
}
// there can only be one receipt per attempt
if attempt.Receipts[0].GetBlockNumber() == nil {
continue
}

return attempt.Receipts[0].GetBlockNumber().Int64() <= (blockHeight - int64(tx.MinConfirmations.Uint32))
}

return false
}
txAttemptFilter := func(attempt *txmgrtypes.TxAttempt[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE]) bool {
return attempt.Receipts != nil && len(attempt.Receipts) > 0
return len(attempt.Receipts) > 0
}
ms.addressStatesLock.RLock()
defer ms.addressStatesLock.RUnlock()
Expand Down
76 changes: 76 additions & 0 deletions core/chains/evm/txmgr/evm_inmemory_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,82 @@ func TestInMemoryStore_PreloadTxes(t *testing.T) {
})
}

func TestInMemoryStore_IsTxFinalized(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("tx not past finality depth", func(t *testing.T) {
// insert the transaction into the persistent store
inTx := cltest.MustInsertConfirmedEthTxWithLegacyAttempt(t, persistentStore, 111, 1, fromAddress)
rec := mustInsertEthReceipt(t, persistentStore, 1, utils.NewHash(), inTx.TxAttempts[0].Hash)
// insert the transaction into the in-memory store
inTx.TxAttempts[0].Receipts = append(inTx.TxAttempts[0].Receipts, evmtxmgr.DbReceiptToEvmReceipt(&rec))
require.NoError(t, inMemoryStore.XXXTestInsertTx(fromAddress, &inTx))

ctx := testutils.Context(t)
blockHeight := int64(2)
expIsFinalized, expErr := persistentStore.IsTxFinalized(ctx, blockHeight, inTx.ID, chainID)
actIsFinalized, actErr := inMemoryStore.IsTxFinalized(ctx, blockHeight, inTx.ID, chainID)
require.NoError(t, expErr)
require.NoError(t, actErr)
assert.Equal(t, expIsFinalized, actIsFinalized)
})

t.Run("tx is past finality depth", func(t *testing.T) {
// insert the transaction into the persistent store
inTx := cltest.MustInsertConfirmedEthTxWithLegacyAttempt(t, persistentStore, 122, 2, fromAddress)
rec := mustInsertEthReceipt(t, persistentStore, 2, utils.NewHash(), inTx.TxAttempts[0].Hash)
// insert the transaction into the in-memory store
inTx.TxAttempts[0].Receipts = append(inTx.TxAttempts[0].Receipts, evmtxmgr.DbReceiptToEvmReceipt(&rec))
require.NoError(t, inMemoryStore.XXXTestInsertTx(fromAddress, &inTx))

ctx := testutils.Context(t)
blockHeight := int64(10)
expIsFinalized, expErr := persistentStore.IsTxFinalized(ctx, blockHeight, inTx.ID, chainID)
actIsFinalized, actErr := inMemoryStore.IsTxFinalized(ctx, blockHeight, inTx.ID, chainID)
require.NoError(t, expErr)
require.NoError(t, actErr)
assert.Equal(t, expIsFinalized, actIsFinalized)
})

t.Run("wrong chain ID", func(t *testing.T) {
// insert the transaction into the persistent store
inTx := cltest.MustInsertConfirmedEthTxWithLegacyAttempt(t, persistentStore, 133, 3, fromAddress)
rec := mustInsertEthReceipt(t, persistentStore, 3, utils.NewHash(), inTx.TxAttempts[0].Hash)
// insert the transaction into the in-memory store
inTx.TxAttempts[0].Receipts = append(inTx.TxAttempts[0].Receipts, evmtxmgr.DbReceiptToEvmReceipt(&rec))
require.NoError(t, inMemoryStore.XXXTestInsertTx(fromAddress, &inTx))

ctx := testutils.Context(t)
blockHeight := int64(10)
wrongChainID := big.NewInt(999)
expIsFinalized, expErr := persistentStore.IsTxFinalized(ctx, blockHeight, inTx.ID, wrongChainID)
actIsFinalized, actErr := inMemoryStore.IsTxFinalized(ctx, blockHeight, inTx.ID, wrongChainID)
require.NoError(t, expErr)
require.NoError(t, actErr)
assert.Equal(t, expIsFinalized, actIsFinalized)
})
}

// 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 bf5299f

Please sign in to comment.