diff --git a/common/txmgr/address_state.go b/common/txmgr/address_state.go index 1d97385d8b4..00d910db8a9 100644 --- a/common/txmgr/address_state.go +++ b/common/txmgr/address_state.go @@ -144,6 +144,28 @@ func (as *AddressState[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) findTx return as.idempotencyKeyToTx[key] } +func (as *AddressState[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) findLatestSequence() SEQ { + as.lock.RLock() + defer as.lock.RUnlock() + + var maxSeq SEQ + if as.inprogress != nil && as.inprogress.Sequence != nil { + if (*as.inprogress.Sequence).Int64() > maxSeq.Int64() { + maxSeq = *as.inprogress.Sequence + } + } + for _, tx := range as.unconfirmed { + if tx.Sequence == nil { + continue + } + if (*tx.Sequence).Int64() > maxSeq.Int64() { + maxSeq = *tx.Sequence + } + } + + return maxSeq +} + func (as *AddressState[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) peekNextUnstartedTx() (*txmgrtypes.Tx[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE], error) { as.lock.RLock() defer as.lock.RUnlock() diff --git a/common/txmgr/inmemory_store.go b/common/txmgr/inmemory_store.go index d5b77b4b91a..2c69d10ae59 100644 --- a/common/txmgr/inmemory_store.go +++ b/common/txmgr/inmemory_store.go @@ -20,6 +20,8 @@ var ( ErrExistingIdempotencyKey = fmt.Errorf("transaction with idempotency key already exists") // ErrAddressNotFound is returned when an address is not found ErrAddressNotFound = fmt.Errorf("address not found") + // ErrSequenceNotFound is returned when a sequence is not found + ErrSequenceNotFound = fmt.Errorf("sequence not found") ) // Store and update all transaction state as files @@ -200,14 +202,14 @@ func (ms *InMemoryStore[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) FindL if ms.chainID.String() != chainID.String() { return seq, fmt.Errorf("find_latest_sequence: %w", ErrInvalidChainID) } - if _, ok := ms.addressStates[fromAddress]; !ok { + as, ok := ms.addressStates[fromAddress] + if !ok { return seq, fmt.Errorf("find_latest_sequence: %w", ErrAddressNotFound) } - // TODO(jtw): replace with inmemory store and use initialization at the start - seq, err = ms.txStore.FindLatestSequence(ctx, fromAddress, chainID) - if err != nil { - return seq, fmt.Errorf("find_latest_sequence: %w", err) + seq = as.findLatestSequence() + if seq.Int64() == 0 { + return seq, fmt.Errorf("find_latest_sequence: %w", ErrSequenceNotFound) } return seq, nil