Skip to content

Commit

Permalink
implement tests for FindTxsRequiringGasBump
Browse files Browse the repository at this point in the history
  • Loading branch information
poopoothegorilla committed Mar 21, 2024
1 parent bf5299f commit 47c1e9a
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 15 deletions.
31 changes: 16 additions & 15 deletions common/txmgr/inmemory_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,7 @@ func (ms *inMemoryStore[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) IsTxF

func (ms *inMemoryStore[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) FindTxsRequiringGasBump(ctx context.Context, address ADDR, blockNum, gasBumpThreshold, depth int64, chainID CHAIN_ID) ([]*txmgrtypes.Tx[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE], error) {
if ms.chainID.String() != chainID.String() {
return nil, fmt.Errorf("find_txs_requiring_gas_bump: %w", ErrInvalidChainID)
return nil, nil
}
if gasBumpThreshold == 0 {
return nil, nil
Expand All @@ -1059,31 +1059,32 @@ func (ms *inMemoryStore[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) FindT
defer ms.addressStatesLock.RUnlock()
as, ok := ms.addressStates[address]
if !ok {
return nil, fmt.Errorf("find_txs_requiring_gas_bump: %w", ErrAddressNotFound)
return nil, nil
}

filter := func(tx *txmgrtypes.Tx[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE]) bool {
if tx.TxAttempts == nil || len(tx.TxAttempts) == 0 {
return false
}
attempt := tx.TxAttempts[0]
if *attempt.BroadcastBeforeBlockNum <= blockNum ||
attempt.State == txmgrtypes.TxAttemptBroadcast {
return false
if len(tx.TxAttempts) == 0 {
return true
}

if tx.State != TxUnconfirmed ||
attempt.ID != 0 {
return false
for _, attempt := range tx.TxAttempts {
if attempt.BroadcastBeforeBlockNum == nil || *attempt.BroadcastBeforeBlockNum > blockNum-gasBumpThreshold || attempt.State != txmgrtypes.TxAttemptBroadcast {
return false
}
}

return true
}
states := []txmgrtypes.TxState{TxUnconfirmed}
txs := as.findTxs(states, filter)

// sort by sequence ASC
sort.Slice(txs, func(i, j int) bool {
return (*txs[i].Sequence).Int64() < (*txs[j].Sequence).Int64()
slices.SortFunc(txs, func(a, b txmgrtypes.Tx[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE]) int {
aSequence, bSequence := a.Sequence, b.Sequence
if aSequence == nil || bSequence == nil {
return 0
}

return cmp.Compare((*aSequence).Int64(), (*bSequence).Int64())
})

if depth > 0 {
Expand Down
57 changes: 57 additions & 0 deletions core/chains/evm/txmgr/evm_inmemory_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package txmgr_test

import (
"context"
"fmt"
"math/big"
"testing"
"time"
Expand Down Expand Up @@ -1223,6 +1224,62 @@ func TestInMemoryStore_IsTxFinalized(t *testing.T) {
})
}

func TestInMemoryStore_FindTxsRequiringGasBump(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("gets transactions requiring gas bumping", func(t *testing.T) {
currentBlockNum := int64(10)

// insert the transaction into the persistent store
inTx_0 := mustInsertUnconfirmedEthTxWithAttemptState(t, persistentStore, 1, fromAddress, txmgrtypes.TxAttemptBroadcast)
require.NoError(t, persistentStore.SetBroadcastBeforeBlockNum(testutils.Context(t), currentBlockNum, chainID))
inTx_1 := mustInsertUnconfirmedEthTxWithAttemptState(t, persistentStore, 2, fromAddress, txmgrtypes.TxAttemptBroadcast)
require.NoError(t, persistentStore.SetBroadcastBeforeBlockNum(testutils.Context(t), currentBlockNum+1, chainID))
// insert the transaction into the in-memory store
inTx_0.TxAttempts[0].BroadcastBeforeBlockNum = &currentBlockNum
require.NoError(t, inMemoryStore.XXXTestInsertTx(fromAddress, &inTx_0))
tempCurrentBlockNum := currentBlockNum + 1
inTx_1.TxAttempts[0].BroadcastBeforeBlockNum = &tempCurrentBlockNum
require.NoError(t, inMemoryStore.XXXTestInsertTx(fromAddress, &inTx_1))

ctx := testutils.Context(t)
newBlock := int64(12)
gasBumpThreshold := int64(2)
expTxs, expErr := persistentStore.FindTxsRequiringGasBump(ctx, fromAddress, newBlock, gasBumpThreshold, 0, chainID)
actTxs, actErr := inMemoryStore.FindTxsRequiringGasBump(ctx, fromAddress, newBlock, gasBumpThreshold, 0, chainID)
require.NoError(t, expErr)
fmt.Println("EXPTX", expTxs[0].ID)
for i := 0; i < len(expTxs[0].TxAttempts); i++ {
fmt.Println("EXPTX_ATTEMPT", expTxs[0].TxAttempts[i].ID)
}
require.NoError(t, actErr)
require.Equal(t, len(expTxs), len(actTxs))
for i := 0; i < len(expTxs); i++ {
assertTxEqual(t, *expTxs[i], *actTxs[i])
}
})
}

// 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 47c1e9a

Please sign in to comment.