Skip to content
This repository has been archived by the owner on May 11, 2024. It is now read-only.

Commit

Permalink
fix(pkg): set more RPC context timeout (#356)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidtaikocha committed Aug 8, 2023
1 parent 1b77549 commit e868482
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 116 deletions.
2 changes: 2 additions & 0 deletions driver/anchor_tx_constructor/anchor_tx_constructor.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ func (c *AnchorTxConstructor) transactOpts(
return nil, err
}

log.Info("Golden touch account nonce", "nonce", nonce)

return &bind.TransactOpts{
From: c.goldenTouchAddress,
Signer: func(address common.Address, tx *types.Transaction) (*types.Transaction, error) {
Expand Down
29 changes: 16 additions & 13 deletions pkg/rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,23 @@ type ClientConfig struct {

// NewClient initializes all RPC clients used by Taiko client softwares.
func NewClient(ctx context.Context, cfg *ClientConfig) (*Client, error) {
l1EthClient, err := DialClientWithBackoff(ctx, cfg.L1Endpoint, cfg.RetryInterval)
ctxWithTimeout, cancel := ctxWithTimeoutOrDefault(ctx, defaultTimeout)
defer cancel()

l1EthClient, err := DialClientWithBackoff(ctxWithTimeout, cfg.L1Endpoint, cfg.RetryInterval)
if err != nil {
return nil, err
}

l2EthClient, err := DialClientWithBackoff(ctx, cfg.L2Endpoint, cfg.RetryInterval)
l2EthClient, err := DialClientWithBackoff(ctxWithTimeout, cfg.L2Endpoint, cfg.RetryInterval)
if err != nil {
return nil, err
}

var l1RPC *EthClient
var l2RPC *EthClient

var (
l1RPC *EthClient
l2RPC *EthClient
)
if cfg.Timeout != nil {
l1RPC = NewEthClientWithTimeout(l1EthClient, *cfg.Timeout)
l2RPC = NewEthClientWithTimeout(l2EthClient, *cfg.Timeout)
Expand All @@ -97,12 +101,12 @@ func NewClient(ctx context.Context, cfg *ClientConfig) (*Client, error) {
return nil, err
}

stateVars, err := taikoL1.GetStateVariables(&bind.CallOpts{Context: ctx})
stateVars, err := taikoL1.GetStateVariables(&bind.CallOpts{Context: ctxWithTimeout})
if err != nil {
return nil, err
}

isArchive, err := IsArchiveNode(ctx, l1RPC, stateVars.GenesisHeight)
isArchive, err := IsArchiveNode(ctxWithTimeout, l1RPC, stateVars.GenesisHeight)
if err != nil {
return nil, err
}
Expand All @@ -121,12 +125,12 @@ func NewClient(ctx context.Context, cfg *ClientConfig) (*Client, error) {
return nil, err
}

l1ChainID, err := l1RPC.ChainID(ctx)
l1ChainID, err := l1RPC.ChainID(ctxWithTimeout)
if err != nil {
return nil, err
}

l2ChainID, err := l2RPC.ChainID(ctx)
l2ChainID, err := l2RPC.ChainID(ctxWithTimeout)
if err != nil {
return nil, err
}
Expand All @@ -136,7 +140,7 @@ func NewClient(ctx context.Context, cfg *ClientConfig) (*Client, error) {
var l2AuthRPC *EngineClient
if len(cfg.L2EngineEndpoint) != 0 && len(cfg.JwtSecret) != 0 {
if l2AuthRPC, err = DialEngineClientWithBackoff(
ctx,
ctxWithTimeout,
cfg.L2EngineEndpoint,
cfg.JwtSecret,
cfg.RetryInterval,
Expand All @@ -147,8 +151,7 @@ func NewClient(ctx context.Context, cfg *ClientConfig) (*Client, error) {

var l2CheckPoint *EthClient
if len(cfg.L2CheckPoint) != 0 {
l2CheckPointEthClient, err := DialClientWithBackoff(ctx, cfg.L2CheckPoint, cfg.RetryInterval)

l2CheckPointEthClient, err := DialClientWithBackoff(ctxWithTimeout, cfg.L2CheckPoint, cfg.RetryInterval)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -176,7 +179,7 @@ func NewClient(ctx context.Context, cfg *ClientConfig) (*Client, error) {
L2ChainID: l2ChainID,
}

if err := client.ensureGenesisMatched(ctx); err != nil {
if err := client.ensureGenesisMatched(ctxWithTimeout); err != nil {
return nil, err
}

Expand Down
17 changes: 13 additions & 4 deletions pkg/rpc/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ func DialClientWithBackoff(ctx context.Context, url string, retryInterval time.D
var client *ethclient.Client
if err := backoff.Retry(
func() (err error) {
client, err = ethclient.DialContext(ctx, url)
ctxWithTimeout, cancel := ctxWithTimeoutOrDefault(ctx, defaultTimeout)
defer cancel()

client, err = ethclient.DialContext(ctxWithTimeout, url)
if err != nil {
log.Error("Dial ethclient error", "url", url, "error", err)
}
Expand All @@ -45,7 +48,10 @@ func DialEngineClientWithBackoff(
var engineClient *EngineClient
if err := backoff.Retry(
func() (err error) {
client, err := DialEngineClient(ctx, url, jwtSecret)
ctxWithTimeout, cancel := ctxWithTimeoutOrDefault(ctx, defaultTimeout)
defer cancel()

client, err := DialEngineClient(ctxWithTimeout, url, jwtSecret)
if err != nil {
log.Error("Dial engine client error", "url", url, "error", err)
return err
Expand All @@ -65,6 +71,9 @@ func DialEngineClientWithBackoff(
// DialEngineClient initializes an RPC connection with authentication headers.
// Taken from https://github.com/prysmaticlabs/prysm/blob/v2.1.4/beacon-chain/execution/rpc_connection.go#L151
func DialEngineClient(ctx context.Context, endpointUrl string, jwtSecret string) (*rpc.Client, error) {
ctxWithTimeout, cancel := ctxWithTimeoutOrDefault(ctx, defaultTimeout)
defer cancel()

endpoint := network.Endpoint{
Url: endpointUrl,
Auth: network.AuthorizationData{
Expand All @@ -81,12 +90,12 @@ func DialEngineClient(ctx context.Context, endpointUrl string, jwtSecret string)
}
switch u.Scheme {
case "http", "https":
client, err = rpc.DialOptions(ctx, endpoint.Url, rpc.WithHTTPClient(endpoint.HttpClient()))
client, err = rpc.DialOptions(ctxWithTimeout, endpoint.Url, rpc.WithHTTPClient(endpoint.HttpClient()))
if err != nil {
return nil, err
}
case "":
client, err = rpc.DialIPC(ctx, endpoint.Url)
client, err = rpc.DialIPC(ctxWithTimeout, endpoint.Url)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit e868482

Please sign in to comment.