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

feat(rpc): keep retrying when connecting to endpoints #708

Merged
merged 5 commits into from
Apr 10, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 43 additions & 23 deletions pkg/rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"context"
"time"

"github.com/cenkalti/backoff/v4"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
"github.com/taikoxyz/taiko-client/bindings"
)

Expand Down Expand Up @@ -48,19 +50,52 @@ type ClientConfig struct {

// NewClient initializes all RPC clients used by Taiko client software.
func NewClient(ctx context.Context, cfg *ClientConfig) (*Client, error) {
ctxWithTimeout, cancel := ctxWithTimeoutOrDefault(ctx, defaultTimeout)
defer cancel()
var (
l1Client *EthClient
l2Client *EthClient
l1BeaconClient *BeaconClient
l2CheckPoint *EthClient
err error
)

l1Client, err := NewEthClient(ctxWithTimeout, cfg.L1Endpoint, cfg.Timeout)
if err != nil {
return nil, err
}
// Keep retrying to connect to the RPC endpoints until success or context is cancelled.
if err := backoff.Retry(func() error {
ctxWithTimeout, cancel := ctxWithTimeoutOrDefault(ctx, defaultTimeout)
defer cancel()

l2Client, err := NewEthClient(ctxWithTimeout, cfg.L2Endpoint, cfg.Timeout)
if err != nil {
if l1Client, err = NewEthClient(ctxWithTimeout, cfg.L1Endpoint, cfg.Timeout); err != nil {
log.Error("Failed to connect to L1 endpoint, retrying", "endpoint", cfg.L1Endpoint, "err", err)
return err
}

if l2Client, err = NewEthClient(ctxWithTimeout, cfg.L2Endpoint, cfg.Timeout); err != nil {
log.Error("Failed to connect to L2 endpoint, retrying", "endpoint", cfg.L2Endpoint, "err", err)
return err
}

if cfg.L1BeaconEndpoint != "" {
if l1BeaconClient, err = NewBeaconClient(cfg.L1BeaconEndpoint, defaultTimeout); err != nil {
log.Error("Failed to connect to L1 beacon endpoint, retrying", "endpoint", cfg.L1BeaconEndpoint, "err", err)
return err
}
}

if cfg.L2CheckPoint != "" {
l2CheckPoint, err = NewEthClient(ctxWithTimeout, cfg.L2CheckPoint, cfg.Timeout)
if err != nil {
log.Error("Failed to connect to L2 checkpoint endpoint, retrying", "endpoint", cfg.L2CheckPoint, "err", err)
return err
}
}

return nil
}, backoff.WithContext(backoff.NewExponentialBackOff(), ctx)); err != nil {
return nil, err
}

ctxWithTimeout, cancel := ctxWithTimeoutOrDefault(ctx, defaultTimeout)
defer cancel()

taikoL1, err := bindings.NewTaikoL1Client(cfg.TaikoL1Address, l1Client)
if err != nil {
return nil, err
Expand Down Expand Up @@ -96,21 +131,6 @@ func NewClient(ctx context.Context, cfg *ClientConfig) (*Client, error) {
}
}

var l1BeaconClient *BeaconClient
if cfg.L1BeaconEndpoint != "" {
if l1BeaconClient, err = NewBeaconClient(cfg.L1BeaconEndpoint, defaultTimeout); err != nil {
return nil, err
}
}

var l2CheckPoint *EthClient
if cfg.L2CheckPoint != "" {
l2CheckPoint, err = NewEthClient(ctxWithTimeout, cfg.L2CheckPoint, cfg.Timeout)
if err != nil {
return nil, err
}
}

client := &Client{
L1: l1Client,
L1Beacon: l1BeaconClient,
Expand Down
Loading