Skip to content

Commit 5c930eb

Browse files
committed
Remove timeout when waiting for tx to be included in a block
1 parent db05379 commit 5c930eb

4 files changed

Lines changed: 25 additions & 24 deletions

File tree

bindings/utils/wait.go

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package utils
33
import (
44
"context"
55
"errors"
6-
"fmt"
76
"time"
87

98
"github.com/ethereum/go-ethereum/accounts/abi/bind"
@@ -12,32 +11,30 @@ import (
1211
"github.com/rocket-pool/smartnode/bindings/rocketpool"
1312
)
1413

15-
// Wait for a transaction to get mined
16-
func WaitForTransaction(client rocketpool.ExecutionClient, hash common.Hash) (*types.Receipt, error) {
17-
14+
// Wait for a transaction to get included, respecting the provided context for cancellation.
15+
// The transaction lookup retries indefinitely (with 1-second pauses) until found or ctx is done.
16+
func WaitForTransactionWithContext(ctx context.Context, client rocketpool.ExecutionClient, hash common.Hash) (*types.Receipt, error) {
1817
var tx *types.Transaction
19-
var err error
2018

21-
// Get the transaction from its hash, retrying for 30 sec if it wasn't found
22-
for i := 0; i < 30; i++ {
23-
if i == 29 {
24-
return nil, fmt.Errorf("Transaction not found after 30 seconds.")
19+
// Get the transaction from its hash, retrying until found or ctx is cancelled.
20+
for {
21+
var err error
22+
tx, _, err = client.TransactionByHash(ctx, hash)
23+
if err == nil {
24+
break
2525
}
26-
27-
tx, _, err = client.TransactionByHash(context.Background(), hash)
28-
if err != nil {
29-
if err.Error() == "not found" {
30-
time.Sleep(1 * time.Second)
31-
continue
32-
}
26+
if err.Error() != "not found" {
3327
return nil, err
34-
} else {
35-
break
28+
}
29+
select {
30+
case <-ctx.Done():
31+
return nil, ctx.Err()
32+
case <-time.After(1 * time.Second):
3633
}
3734
}
3835

3936
// Wait for transaction to be mined
40-
txReceipt, err := bind.WaitMined(context.Background(), client, tx)
37+
txReceipt, err := bind.WaitMined(ctx, client, tx)
4138
if err != nil {
4239
return nil, err
4340
}
@@ -47,6 +44,10 @@ func WaitForTransaction(client rocketpool.ExecutionClient, hash common.Hash) (*t
4744
return txReceipt, errors.New("Transaction failed with status 0")
4845
}
4946

50-
// Return
5147
return txReceipt, nil
5248
}
49+
50+
// Wait for a transaction to get mined
51+
func WaitForTransaction(client rocketpool.ExecutionClient, hash common.Hash) (*types.Receipt, error) {
52+
return WaitForTransactionWithContext(context.Background(), client, hash)
53+
}

rocketpool/api/wait.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func RegisterWaitRoute(mux *http.ServeMux, c *cli.Command) {
2323
return
2424
}
2525
response := apitypes.APIResponse{}
26-
_, err = utils.WaitForTransaction(rp.Client, hash)
26+
_, err = utils.WaitForTransactionWithContext(r.Context(), rp.Client, hash)
2727
apiutils.WriteResponse(w, &response, err)
2828
})
2929
}

shared/services/rocketpool/api.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package rocketpool
22

33
import (
4+
"context"
45
"fmt"
56
"net/url"
67

@@ -9,9 +10,9 @@ import (
910
"github.com/rocket-pool/smartnode/shared/types/api"
1011
)
1112

12-
// Wait for a transaction
13+
// Wait for a transaction — no timeout; blocks until the tx is included or the caller cancels.
1314
func (c *Client) WaitForTransaction(txHash common.Hash) (api.APIResponse, error) {
14-
responseBytes, err := c.callHTTPAPI("GET", "/api/wait", url.Values{"txHash": {txHash.Hex()}})
15+
responseBytes, err := c.callHTTPAPICtx(context.Background(), "GET", "/api/wait", url.Values{"txHash": {txHash.Hex()}})
1516
if err != nil {
1617
return api.APIResponse{}, fmt.Errorf("Error waiting for tx: %w", err)
1718
}

shared/services/rocketpool/client.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1253,7 +1253,6 @@ func (c *Client) getAPIURL() string {
12531253
// path is the URL path, e.g. "/api/node/status".
12541254
// params are appended as query string parameters for GET or as a form body for POST.
12551255
// The response body is returned as-is; callers unmarshal it the same way
1256-
// The response body is returned as-is; callers unmarshal it.
12571256
func (c *Client) callHTTPAPI(method, path string, params url.Values) ([]byte, error) {
12581257
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
12591258
defer cancel()

0 commit comments

Comments
 (0)