Skip to content

Commit

Permalink
feat: added timeout over WaitForBlockCompletion
Browse files Browse the repository at this point in the history
  • Loading branch information
Yashk767 committed Oct 17, 2024
1 parent bccd0c8 commit e16733e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
1 change: 1 addition & 0 deletions core/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var NilHash = common.Hash{0x00}
const (
BlockCompletionAttempts = 4
BlockCompletionAttemptRetryDelay = 2
BlockCompletionTimeout = 15
)

//Following are the default config values for all the config parameters
Expand Down
32 changes: 22 additions & 10 deletions utils/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,31 @@ func (*UtilsStruct) CheckTransactionReceipt(client *ethclient.Client, _txHash st
}

func (*UtilsStruct) WaitForBlockCompletion(client *ethclient.Client, hashToRead string) error {
ctx, cancel := context.WithTimeout(context.Background(), core.BlockCompletionTimeout*time.Second)
defer cancel()

for i := 0; i < core.BlockCompletionAttempts; i++ {
log.Debug("Checking if transaction is mined....")
transactionStatus := UtilsInterface.CheckTransactionReceipt(client, hashToRead)
if transactionStatus == 0 {
err := errors.New("transaction mining unsuccessful")
log.Error(err)
return err
} else if transactionStatus == 1 {
log.Info("Transaction mined successfully")
return nil
select {
case <-ctx.Done():
log.Error("Timeout: WaitForBlockCompletion took too long")
return errors.New("timeout exceeded for transaction mining")
default:
log.Debug("Checking if transaction is mined....")
transactionStatus := UtilsInterface.CheckTransactionReceipt(client, hashToRead)

if transactionStatus == 0 {
err := errors.New("transaction mining unsuccessful")
log.Error(err)
return err
} else if transactionStatus == 1 {
log.Info("Transaction mined successfully")
return nil
}

time.Sleep(core.BlockCompletionAttemptRetryDelay * time.Second)
}
Time.Sleep(core.BlockCompletionAttemptRetryDelay * time.Second)
}

log.Info("Max retries for WaitForBlockCompletion attempted!")
return errors.New("maximum attempts failed for transaction mining")
}
Expand Down

0 comments on commit e16733e

Please sign in to comment.