From 1e82f7b855e8defcec3d8a0ce4b0ae32ddc9294b Mon Sep 17 00:00:00 2001 From: David Date: Wed, 10 Apr 2024 15:55:07 +0800 Subject: [PATCH 1/5] feat(rpc): keep retrying when connecting to endpoints --- pkg/rpc/client.go | 68 ++++++++++++++++++++++++++++++----------------- 1 file changed, 43 insertions(+), 25 deletions(-) diff --git a/pkg/rpc/client.go b/pkg/rpc/client.go index 3aa2b0a51..39ddfc2e2 100644 --- a/pkg/rpc/client.go +++ b/pkg/rpc/client.go @@ -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" ) @@ -48,18 +50,49 @@ 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. + backoff.Retry(func() error { + ctxWithTimeout, cancel := ctxWithTimeoutOrDefault(ctx, defaultTimeout) + defer cancel() - l2Client, err := NewEthClient(ctxWithTimeout, cfg.L2Endpoint, cfg.Timeout) - if err != nil { - return nil, err - } + 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)) + + ctxWithTimeout, cancel := ctxWithTimeoutOrDefault(ctx, defaultTimeout) + defer cancel() taikoL1, err := bindings.NewTaikoL1Client(cfg.TaikoL1Address, l1Client) if err != nil { @@ -96,21 +129,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, From fa87906b6643adbb3b85ac1d841aae4d356503ec Mon Sep 17 00:00:00 2001 From: David Date: Wed, 10 Apr 2024 15:57:56 +0800 Subject: [PATCH 2/5] fix: fix lint --- pkg/rpc/client.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pkg/rpc/client.go b/pkg/rpc/client.go index 39ddfc2e2..832747b72 100644 --- a/pkg/rpc/client.go +++ b/pkg/rpc/client.go @@ -59,7 +59,7 @@ func NewClient(ctx context.Context, cfg *ClientConfig) (*Client, error) { ) // Keep retrying to connect to the RPC endpoints until success or context is cancelled. - backoff.Retry(func() error { + if err := backoff.Retry(func() error { ctxWithTimeout, cancel := ctxWithTimeoutOrDefault(ctx, defaultTimeout) defer cancel() @@ -89,7 +89,9 @@ func NewClient(ctx context.Context, cfg *ClientConfig) (*Client, error) { } return nil - }, backoff.WithContext(backoff.NewExponentialBackOff(), ctx)) + }, backoff.WithContext(backoff.NewExponentialBackOff(), ctx)); err != nil { + return nil, err + } ctxWithTimeout, cancel := ctxWithTimeoutOrDefault(ctx, defaultTimeout) defer cancel() From f1b78f1a186083de047e57e43c99962edb72c947 Mon Sep 17 00:00:00 2001 From: David Date: Wed, 10 Apr 2024 16:17:02 +0800 Subject: [PATCH 3/5] feat(rpc): keep retrying when connecting to endpoints --- pkg/rpc/client.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pkg/rpc/client.go b/pkg/rpc/client.go index 832747b72..7380e03fd 100644 --- a/pkg/rpc/client.go +++ b/pkg/rpc/client.go @@ -2,6 +2,7 @@ package rpc import ( "context" + "os" "time" "github.com/cenkalti/backoff/v4" @@ -73,7 +74,8 @@ func NewClient(ctx context.Context, cfg *ClientConfig) (*Client, error) { return err } - if cfg.L1BeaconEndpoint != "" { + // NOTE: when running tests, we do not have a L1 beacon endpoint. + if cfg.L1BeaconEndpoint != "" && os.Getenv("RUN_TESTS") == "" { 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 From a5fd63e2b4c011b9a2c160b52167c9ad485b4291 Mon Sep 17 00:00:00 2001 From: David Date: Wed, 10 Apr 2024 17:51:40 +0800 Subject: [PATCH 4/5] test: fix tests --- driver/config_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/driver/config_test.go b/driver/config_test.go index 5232aa03f..2d528cb72 100644 --- a/driver/config_test.go +++ b/driver/config_test.go @@ -38,7 +38,7 @@ func (s *DriverTestSuite) TestNewConfigFromCliContext() { s.NotEmpty(c.JwtSecret) s.True(c.P2PSyncVerifiedBlocks) s.Equal(l2CheckPoint, c.L2CheckPoint) - s.NotNil(new(Driver).InitFromCli(context.Background(), ctx)) + s.Nil(new(Driver).InitFromCli(context.Background(), ctx)) return err } From 6bd9708961c0dfb566e7cc02c6ff7af8dc7a0717 Mon Sep 17 00:00:00 2001 From: David Date: Wed, 10 Apr 2024 18:09:42 +0800 Subject: [PATCH 5/5] test: fix tests --- prover/prover_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/prover/prover_test.go b/prover/prover_test.go index bcf38376f..8caf93479 100644 --- a/prover/prover_test.go +++ b/prover/prover_test.go @@ -125,7 +125,7 @@ func (s *ProverTestSuite) TestInitError() { p := new(Prover) - s.ErrorContains(InitFromConfig(ctx, p, &Config{ + s.NotNil(InitFromConfig(ctx, p, &Config{ L1WsEndpoint: os.Getenv("L1_NODE_WS_ENDPOINT"), L1HttpEndpoint: os.Getenv("L1_NODE_HTTP_ENDPOINT"), L2WsEndpoint: os.Getenv("L2_EXECUTION_ENGINE_WS_ENDPOINT"), @@ -155,7 +155,7 @@ func (s *ProverTestSuite) TestInitError() { TxSendTimeout: txmgr.DefaultBatcherFlagValues.TxSendTimeout, TxNotInMempoolTimeout: txmgr.DefaultBatcherFlagValues.TxNotInMempoolTimeout, }, - }), "dial tcp:") + })) } func (s *ProverTestSuite) TestOnBlockProposed() {