Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
poopoothegorilla committed Apr 3, 2024
1 parent a186281 commit 7de69a4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
46 changes: 27 additions & 19 deletions common/txmgr/address_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,40 +420,40 @@ func (as *addressState[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) moveUn
if !ok || tx == nil {
return fmt.Errorf("move_unconfirmed_to_confirmed_missing_receipt: no unconfirmed transaction with ID %d", txID)
}
tx.State = TxConfirmedMissingReceipt

as.confirmedMissingReceiptTxs[tx.ID] = tx
delete(as.unconfirmedTxs, tx.ID)
as._moveUnconfirmedToConfirmedMissingReceipt(tx)

return nil
}

// moveInProgressToConfirmedMissingReceipt moves transaction to the confirmed missing receipt state.
func (as *addressState[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) moveTxToConfirmedMissingReceipt(txAttempt txmgrtypes.TxAttempt[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE], broadcastAt time.Time) error {
// moveTxAttemptToBroadcast moves the transaction attempt to the broadcast state.
func (as *addressState[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) moveTxAttemptToBroadcast(
txAttempt txmgrtypes.TxAttempt[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE],
broadcastAt time.Time,
) error {
as.Lock()
defer as.Unlock()

tx, ok := as.allTxs[txAttempt.TxID]
if !ok {
if !ok || tx == nil {
return fmt.Errorf("move_in_progress_to_confirmed_missing_receipt: no transaction with ID %d", txAttempt.TxID)
}
if len(tx.TxAttempts) == 0 {
return fmt.Errorf("move_in_progress_to_confirmed_missing_receipt: no attempts for transaction with ID %d", tx.ID)
}
if tx.BroadcastAt != nil && tx.BroadcastAt.Before(broadcastAt) {
tx.BroadcastAt = &broadcastAt
}

var found bool
for i := 0; i < len(tx.TxAttempts); i++ {
if tx.TxAttempts[i].ID == txAttempt.ID {
attempt := tx.TxAttempts[i]
attempt.State = txmgrtypes.TxAttemptBroadcast
tx.TxAttempts[i].State = txmgrtypes.TxAttemptBroadcast
as.attemptHashToTxAttempt[txAttempt.Hash] = &tx.TxAttempts[i]
found = true
break
}
}
tx.State = TxConfirmedMissingReceipt

as.confirmedMissingReceiptTxs[tx.ID] = tx
delete(as.unconfirmedTxs, tx.ID)
if !found {
return fmt.Errorf("move_in_progress_to_confirmed_missing_receipt: transaction with ID %d has no attempt with ID: %q", txAttempt.TxID, txAttempt.ID)
}
if tx.BroadcastAt != nil && tx.BroadcastAt.Before(broadcastAt) {
tx.BroadcastAt = &broadcastAt
}
as._moveUnconfirmedToConfirmedMissingReceipt(tx)

return nil
}
Expand Down Expand Up @@ -582,3 +582,11 @@ func (as *addressState[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) _moveT
tx.Error = txError
as.fatalErroredTxs[tx.ID] = tx
}

func (as *addressState[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) _moveUnconfirmedToConfirmedMissingReceipt(
tx *txmgrtypes.Tx[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE],
) {
tx.State = TxConfirmedMissingReceipt
as.confirmedMissingReceiptTxs[tx.ID] = tx
delete(as.unconfirmedTxs, tx.ID)
}
12 changes: 4 additions & 8 deletions common/txmgr/inmemory_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,19 +384,15 @@ func (ms *inMemoryStore[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) SaveC
return fmt.Errorf("expected state to be in_progress")
}

var tx *txmgrtypes.Tx[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE]
var as *addressState[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]
filter := func(tx *txmgrtypes.Tx[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE]) bool { return true }
for _, vas := range ms.addressStates {
txs := vas.findTxs(nil, filter, attempt.TxID)
if len(txs) != 0 {
tx = &txs[0]
if vas.hasTx(attempt.TxID) {
as = vas
break
}
}
if tx == nil {
return fmt.Errorf("save_confirmed_missing_receipt_attempt: %w", ErrTxnNotFound)
if as == nil {
return fmt.Errorf("save_confirmed_missing_receipt_attempt: %w: %q", ErrTxnNotFound, attempt.TxID)
}

// Persist to persistent storage
Expand All @@ -405,7 +401,7 @@ func (ms *inMemoryStore[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) SaveC
}

// Update in memory store
return as.moveTxToConfirmedMissingReceipt(*attempt, broadcastAt)
return as.moveTxAttemptToBroadcast(*attempt, broadcastAt)
}
func (ms *inMemoryStore[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, R, SEQ, FEE]) SaveInProgressAttempt(ctx context.Context, attempt *txmgrtypes.TxAttempt[CHAIN_ID, ADDR, TX_HASH, BLOCK_HASH, SEQ, FEE]) error {
return nil
Expand Down

0 comments on commit 7de69a4

Please sign in to comment.