Skip to content

Commit

Permalink
fix some state tracking instances
Browse files Browse the repository at this point in the history
  • Loading branch information
Farber98 committed Nov 29, 2024
1 parent 4fd327a commit 2b29c33
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
32 changes: 17 additions & 15 deletions pkg/solana/txm/pendingtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ type finishedTx struct {
}

type txInfo struct {
id string
status TxState
id string
state TxState
}

var _ PendingTxContext = &pendingTxContext{}
Expand Down Expand Up @@ -127,7 +127,7 @@ func (c *pendingTxContext) New(tx pendingTx, sig solana.Signature, cancel contex
}
// save cancel func
c.cancelBy[tx.id] = cancel
c.sigToTxInfo[sig] = txInfo{id: tx.id, status: Broadcasted}
c.sigToTxInfo[sig] = txInfo{id: tx.id, state: Broadcasted}
// add signature to tx
tx.signatures = append(tx.signatures, sig)
tx.createTs = time.Now()
Expand Down Expand Up @@ -164,7 +164,7 @@ func (c *pendingTxContext) AddSignature(id string, sig solana.Signature) error {
if _, exists := c.broadcastedProcessedTxs[id]; !exists {
return "", ErrTransactionNotFound
}
c.sigToTxInfo[sig] = txInfo{id: id, status: Broadcasted}
c.sigToTxInfo[sig] = txInfo{id: id, state: Broadcasted}
tx := c.broadcastedProcessedTxs[id]
// save new signature
tx.signatures = append(tx.signatures, sig)
Expand Down Expand Up @@ -299,9 +299,10 @@ func (c *pendingTxContext) OnProcessed(sig solana.Signature) (string, error) {
if !exists {
return info.id, ErrTransactionNotFound
}
// update tx state to Processed
tx.state = Processed
// save updated tx back to the broadcasted map
// update sig and tx to Processed
info.state, tx.state = Processed, Processed
// save updated sig and tx back to the maps
c.sigToTxInfo[sig] = info
c.broadcastedProcessedTxs[info.id] = tx
return info.id, nil
})
Expand Down Expand Up @@ -343,8 +344,9 @@ func (c *pendingTxContext) OnConfirmed(sig solana.Signature) (string, error) {
cancel() // cancel context
delete(c.cancelBy, info.id)
}
// update tx state to Confirmed
tx.state = Confirmed
// update sig and tx state to Confirmed
info.state, tx.state = Confirmed, Confirmed
c.sigToTxInfo[sig] = info
// move tx to confirmed map
c.confirmedTxs[info.id] = tx
// remove tx from broadcasted map
Expand Down Expand Up @@ -585,7 +587,7 @@ func (c *pendingTxContext) UpdateSignatureStatus(sig solana.Signature, newStatus
if !exists {
return ErrSigDoesNotExist
}
if info.status == newStatus {
if info.state == newStatus {
return ErrAlreadyInExpectedState
}
return nil
Expand All @@ -600,11 +602,11 @@ func (c *pendingTxContext) UpdateSignatureStatus(sig solana.Signature, newStatus
if !exists {
return "", ErrSigDoesNotExist
}
if info.status == newStatus {
// Another goroutine might have updated the status; no action needed
if info.state == newStatus {
// no action needed
return "", ErrAlreadyInExpectedState
}
info.status = newStatus
info.state = newStatus
c.sigToTxInfo[sig] = info
return "", nil
})
Expand Down Expand Up @@ -654,8 +656,8 @@ func (c *pendingTxContext) OnReorg(sig solana.Signature) (pendingTx, error) {
return "", ErrTransactionNotFound
}

// Reset the signature and tx status for retrying
info.status, pTx.state = Broadcasted, Broadcasted
// Reset the signature status and tx for retrying
info.state, pTx.state = Broadcasted, Broadcasted
c.sigToTxInfo[sig] = info
return "", nil
})
Expand Down
4 changes: 2 additions & 2 deletions pkg/solana/txm/txm.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,8 +542,8 @@ func (txm *Txm) handleReorg(ctx context.Context, sig solanaGo.Signature, status
// Check if tx has been reorged by detecting if we had a status regression
// If so, we'll handle the reorg by updating the status in our in-memory layer and retrying the transaction for that sig.
currentTxState := convertStatus(status)
if isStatusRegression(txInfo.status, currentTxState) {
txm.lggr.Warnw("potential re-org detected for transaction", "txID", txInfo.id, "signature", sig, "previousStatus", txInfo.status, "currentStatus", currentTxState)
if isStatusRegression(txInfo.state, currentTxState) {
txm.lggr.Warnw("potential re-org detected for transaction", "txID", txInfo.id, "signature", sig, "previousStatus", txInfo.state, "currentStatus", currentTxState)
// Update status for the tx associated to this sig in our in-memory layer with last seen on-chain status.
_, err = txm.txs.UpdateSignatureStatus(sig, currentTxState)
if err != nil {
Expand Down

0 comments on commit 2b29c33

Please sign in to comment.