Skip to content

Commit e595d23

Browse files
committed
feat: added timeout over WaitForBlockCompletion
1 parent 7fe98c7 commit e595d23

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

core/constants.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var NilHash = common.Hash{0x00}
2727
const (
2828
BlockCompletionAttempts = 4
2929
BlockCompletionAttemptRetryDelay = 2
30+
BlockCompletionTimeout = 15
3031
)
3132

3233
//Following are the default config values for all the config parameters

utils/common.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,31 @@ func (*UtilsStruct) CheckTransactionReceipt(client *ethclient.Client, _txHash st
7777
}
7878

7979
func (*UtilsStruct) WaitForBlockCompletion(client *ethclient.Client, hashToRead string) error {
80+
ctx, cancel := context.WithTimeout(context.Background(), core.BlockCompletionTimeout*time.Second)
81+
defer cancel()
82+
8083
for i := 0; i < core.BlockCompletionAttempts; i++ {
81-
log.Debug("Checking if transaction is mined....")
82-
transactionStatus := UtilsInterface.CheckTransactionReceipt(client, hashToRead)
83-
if transactionStatus == 0 {
84-
err := errors.New("transaction mining unsuccessful")
85-
log.Error(err)
86-
return err
87-
} else if transactionStatus == 1 {
88-
log.Info("Transaction mined successfully")
89-
return nil
84+
select {
85+
case <-ctx.Done():
86+
log.Error("Timeout: WaitForBlockCompletion took too long")
87+
return errors.New("timeout exceeded for transaction mining")
88+
default:
89+
log.Debug("Checking if transaction is mined....")
90+
transactionStatus := UtilsInterface.CheckTransactionReceipt(client, hashToRead)
91+
92+
if transactionStatus == 0 {
93+
err := errors.New("transaction mining unsuccessful")
94+
log.Error(err)
95+
return err
96+
} else if transactionStatus == 1 {
97+
log.Info("Transaction mined successfully")
98+
return nil
99+
}
100+
101+
time.Sleep(core.BlockCompletionAttemptRetryDelay * time.Second)
90102
}
91-
Time.Sleep(core.BlockCompletionAttemptRetryDelay * time.Second)
92103
}
104+
93105
log.Info("Max retries for WaitForBlockCompletion attempted!")
94106
return errors.New("maximum attempts failed for transaction mining")
95107
}

0 commit comments

Comments
 (0)