Skip to content

Commit

Permalink
address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Farber98 committed Dec 10, 2024
1 parent 6bcdd8f commit 50dd10f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
26 changes: 13 additions & 13 deletions pkg/solana/txm/pendingtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ type PendingTxContext interface {
Remove(id string) (string, error)
// ListAllSigs returns all of the signatures being tracked for all transactions not yet finalized or errored
ListAllSigs() []solana.Signature
// ListAllExpiredBroadcastedTxs returns all the txes that are in broadcasted state and have expired for given block height compared against their lastValidBlockHeight.
// Passing maxUint64 as currHeight will return all broadcasted txes.
ListAllExpiredBroadcastedTxs(currBlockHeight uint64) []pendingTx
// ListAllExpiredBroadcastedTxs returns all the txes that are in broadcasted state and have expired for given block number compared against lastValidBlockHeight (last valid block number)
// Passing maxUint64 as currBlockNumber will return all broadcasted txes.
ListAllExpiredBroadcastedTxs(currBlockNumber uint64) []pendingTx
// Expired returns whether or not confirmation timeout amount of time has passed since creation
Expired(sig solana.Signature, confirmationTimeout time.Duration) bool
// OnProcessed marks transactions as Processed
Expand All @@ -57,7 +57,7 @@ type pendingTx struct {
id string
createTs time.Time
state TxState
lastValidBlockHeight uint64 // to track expiration
lastValidBlockHeight uint64 // to track expiration, equivalent to last valid block number.
}

// finishedTx is used to store minimal info specifically for finalized or errored transactions for external status checks
Expand Down Expand Up @@ -212,18 +212,18 @@ func (c *pendingTxContext) ListAllSigs() []solana.Signature {
return maps.Keys(c.sigToID)
}

// ListAllExpiredBroadcastedTxs returns all the txes that are in broadcasted state and have expired for given block height compared against their lastValidBlockHeight.
// Passing maxUint64 as currHeight will return all broadcasted txes.
func (c *pendingTxContext) ListAllExpiredBroadcastedTxs(currBlockHeight uint64) []pendingTx {
// ListAllExpiredBroadcastedTxs returns all the txes that are in broadcasted state and have expired for given block number compared against lastValidBlockHeight (last valid block number)
// Passing maxUint64 as currBlockNumber will return all broadcasted txes.
func (c *pendingTxContext) ListAllExpiredBroadcastedTxs(currBlockNumber uint64) []pendingTx {
c.lock.RLock()
defer c.lock.RUnlock()
broadcastedTxes := make([]pendingTx, 0, len(c.broadcastedProcessedTxs)) // worst case, all of them
expiredBroadcastedTxs := make([]pendingTx, 0, len(c.broadcastedProcessedTxs)) // worst case, all of them
for _, tx := range c.broadcastedProcessedTxs {
if tx.state == Broadcasted && tx.lastValidBlockHeight < currBlockHeight {
broadcastedTxes = append(broadcastedTxes, tx)
if tx.state == Broadcasted && tx.lastValidBlockHeight < currBlockNumber {
expiredBroadcastedTxs = append(expiredBroadcastedTxs, tx)
}
}
return broadcastedTxes
return expiredBroadcastedTxs
}

// Expired returns if the timeout for trying to confirm a signature has been reached
Expand Down Expand Up @@ -614,8 +614,8 @@ func (c *pendingTxContextWithProm) ListAllSigs() []solana.Signature {
return sigs
}

func (c *pendingTxContextWithProm) ListAllExpiredBroadcastedTxs(currBlockHeight uint64) []pendingTx {
return c.pendingTx.ListAllExpiredBroadcastedTxs(currBlockHeight)
func (c *pendingTxContextWithProm) ListAllExpiredBroadcastedTxs(currBlockNumber uint64) []pendingTx {
return c.pendingTx.ListAllExpiredBroadcastedTxs(currBlockNumber)
}

func (c *pendingTxContextWithProm) Expired(sig solana.Signature, lifespan time.Duration) bool {
Expand Down
6 changes: 3 additions & 3 deletions pkg/solana/txm/txm.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (txm *Txm) run() {
// It builds, signs and sends the initial tx with a new valid blockhash, and starts a retry routine with fee bumping if needed.
// The function returns the signed transaction, its ID, and the initial signature for use in simulation.
func (txm *Txm) sendWithRetry(ctx context.Context, msg pendingTx) (solanaGo.Transaction, string, solanaGo.Signature, error) {
// Assign new blockhash and lastValidBlockHeight to the transaction
// Assign new blockhash and lastValidBlockHeight (last valid block number) to the transaction
// This is essential for tracking transaction rebroadcast
// Only the initial transaction should be sent with the updated blockhash
client, err := txm.client.Get()
Expand Down Expand Up @@ -567,15 +567,15 @@ func (txm *Txm) handleFinalizedSignatureStatus(sig solanaGo.Signature) {
}

// rebroadcastExpiredTxs attempts to rebroadcast all transactions that are in broadcasted state and have expired.
// An expired tx is one where it's blockhash lastValidBlockHeight is smaller than the current slot height.
// An expired tx is one where it's blockhash lastValidBlockHeight (last valid block number) is smaller than the current block height (block number).
// If any error occurs during rebroadcast attempt, they are discarded, and the function continues with the next transaction.
func (txm *Txm) rebroadcastExpiredTxs(ctx context.Context, client client.ReaderWriter) {
currBlock, err := client.GetLatestBlock(ctx)
if err != nil || currBlock == nil || currBlock.BlockHeight == nil {
txm.lggr.Errorw("failed to get current block height", "error", err)
return
}
// Rebroadcast all expired txes
// Rebroadcast all expired txes using currBlockHeight (current block number)
for _, tx := range txm.txs.ListAllExpiredBroadcastedTxs(*currBlock.BlockHeight) {
txm.lggr.Debugw("transaction expired, rebroadcasting", "id", tx.id, "signature", tx.signatures, "lastValidBlockHeight", tx.lastValidBlockHeight, "currentBlockHeight", *currBlock.BlockHeight)
// Removes all signatures associated to tx and cancels context.
Expand Down

0 comments on commit 50dd10f

Please sign in to comment.