Skip to content

Commit

Permalink
Merge pull request #12228 from smartcontractkit/jtw/step-3-03-update-…
Browse files Browse the repository at this point in the history
…txs-unconfirmed

TXM In-memory: step 3-03-UpdateTxsUnconfirmed
  • Loading branch information
poopoothegorilla authored Apr 3, 2024
2 parents 14b935c + e9aaffe commit 289e524
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
21 changes: 20 additions & 1 deletion common/txmgr/inmemory_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"math/big"
"sync"
"time"

"github.com/google/uuid"
Expand Down Expand Up @@ -47,7 +48,8 @@ type inMemoryStore[
keyStore txmgrtypes.KeyStore[ADDR, CHAIN_ID, SEQ]
persistentTxStore txmgrtypes.TxStore[ADDR, CHAIN_ID, TX_HASH, BLOCK_HASH, R, SEQ, FEE]

addressStates map[ADDR]*addressState[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]
addressStatesLock sync.RWMutex
addressStates map[ADDR]*addressState[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]
}

// NewInMemoryStore returns a new inMemoryStore
Expand Down Expand Up @@ -215,6 +217,23 @@ func (ms *inMemoryStore[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) Updat

// UpdateTxsUnconfirmed updates the unconfirmed transactions for a given set of ids
func (ms *inMemoryStore[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) UpdateTxsUnconfirmed(ctx context.Context, txIDs []int64) error {
// Persist to persistent storage
if err := ms.persistentTxStore.UpdateTxsUnconfirmed(ctx, txIDs); err != nil {
return err
}

// Update in memory store
ms.addressStatesLock.RLock()
defer ms.addressStatesLock.RUnlock()

for _, as := range ms.addressStates {
for _, txID := range txIDs {
if err := as.moveConfirmedMissingReceiptToUnconfirmed(txID); err != nil {
continue
}
}
}

return nil
}

Expand Down
62 changes: 62 additions & 0 deletions core/chains/evm/txmgr/evm_inmemory_store_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,76 @@
package txmgr_test

import (
"math/big"
"testing"
"time"

"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/smartcontractkit/chainlink-common/pkg/logger"
commontxmgr "github.com/smartcontractkit/chainlink/v2/common/txmgr"
txmgrcommon "github.com/smartcontractkit/chainlink/v2/common/txmgr"

evmgas "github.com/smartcontractkit/chainlink/v2/core/chains/evm/gas"
evmtxmgr "github.com/smartcontractkit/chainlink/v2/core/chains/evm/txmgr"
evmtypes "github.com/smartcontractkit/chainlink/v2/core/chains/evm/types"
"github.com/smartcontractkit/chainlink/v2/core/internal/cltest"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils/evmtest"
"github.com/smartcontractkit/chainlink/v2/core/internal/testutils/pgtest"
)

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

t.Run("successfully updates transactions to unconfirmed", func(t *testing.T) {
db := pgtest.NewSqlxDB(t)
_, dbcfg, evmcfg := evmtxmgr.MakeTestConfigs(t)
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 := testutils.Context(t)

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)

// Insert a transaction into persistent store
originalBroadcastAt := time.Unix(1616509100, 0)
inTx := mustInsertConfirmedMissingReceiptEthTxWithLegacyAttempt(
t, persistentStore, 0, 1, originalBroadcastAt, fromAddress)
assert.Equal(t, txmgrcommon.TxConfirmedMissingReceipt, inTx.State)
// Insert the transaction into the in-memory store
require.NoError(t, inMemoryStore.XXXTestInsertTx(fromAddress, &inTx))

// Update the transaction to unconfirmed
require.NoError(t, inMemoryStore.UpdateTxsUnconfirmed(ctx, []int64{inTx.ID}))

expTx, err := persistentStore.FindTxWithAttempts(ctx, inTx.ID)
require.NoError(t, err)
assert.Equal(t, commontxmgr.TxUnconfirmed, expTx.State)
assert.Equal(t, 1, len(expTx.TxAttempts))

fn := func(tx *evmtxmgr.Tx) bool { return true }
actTxs := inMemoryStore.XXXTestFindTxs(nil, fn, inTx.ID)
require.Equal(t, 1, len(actTxs))
actTx := actTxs[0]
assertTxEqual(t, expTx, actTx)
assert.Equal(t, commontxmgr.TxUnconfirmed, actTx.State)
})
}

// 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 289e524

Please sign in to comment.