Skip to content

Commit

Permalink
recover stale transactions when awaiting receipt
Browse files Browse the repository at this point in the history
  • Loading branch information
pk910 committed Jan 17, 2024
1 parent 7d564ca commit cd1cfe4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/coordinator/wallet/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,8 @@ func (manager *Manager) processBlockTransactions(block *execution.Block) {
}
}
}

for _, wallet := range wallets {
wallet.processStaleConfirmations(block)
}
}
35 changes: 35 additions & 0 deletions pkg/coordinator/wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Wallet struct {

confirmedNonce uint64
confirmedBalance *big.Int
lastConfirmation uint64

txNonceChans map[uint64]*nonceStatus
txNonceMutex sync.Mutex
Expand Down Expand Up @@ -246,6 +247,8 @@ func (wallet *Wallet) processTransactionInclusion(block *execution.Block, tx *ty
return
}

wallet.lastConfirmation = block.Number

if receipt != nil {
wallet.confirmedBalance = wallet.confirmedBalance.Sub(wallet.confirmedBalance, tx.Value())
txFee := new(big.Int).Mul(receipt.EffectiveGasPrice, big.NewInt(int64(receipt.GasUsed)))
Expand All @@ -271,6 +274,38 @@ func (wallet *Wallet) processTransactionInclusion(block *execution.Block, tx *ty
}
}

func (wallet *Wallet) processStaleConfirmations(block *execution.Block) {
if !wallet.isReady {
return
}

if len(wallet.txNonceChans) > 0 && block.Number > wallet.lastConfirmation+10 {
wallet.lastConfirmation = block.Number
clients := block.GetSeenBy()
client := clients[0]

lastNonce, err := client.GetRPCClient().GetNonceAt(context.Background(), wallet.address, big.NewInt(int64(block.Number)))
if err != nil {
return
}

wallet.txNonceMutex.Lock()
defer wallet.txNonceMutex.Unlock()

if wallet.confirmedNonce >= lastNonce {
return
}

for n := range wallet.txNonceChans {
if n < lastNonce {
logrus.WithError(err).Warnf("recovering stale confirmed transactions for %v (nonce %v)", wallet.address.String(), n)
close(wallet.txNonceChans[n].channel)
delete(wallet.txNonceChans, n)
}
}
}
}

func (wallet *Wallet) processTransactionReceival(_ *execution.Block, tx *types.Transaction) {
if !wallet.isReady {
return
Expand Down

0 comments on commit cd1cfe4

Please sign in to comment.