Skip to content

Commit

Permalink
add find latest sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
poopoothegorilla committed Nov 15, 2023
1 parent 5314284 commit 5a908be
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
22 changes: 22 additions & 0 deletions common/txmgr/address_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
12 changes: 7 additions & 5 deletions common/txmgr/inmemory_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5a908be

Please sign in to comment.