From 9ac73ba3ccc1e262bc87d3046d7ad1e26bcd8d87 Mon Sep 17 00:00:00 2001 From: Gavin Yu Date: Fri, 22 Mar 2024 14:00:46 +0800 Subject: [PATCH] refactor(pkg): remove timeout in `WaitTillL2ExecutionEngineSynced` (#654) Co-authored-by: maskpp Co-authored-by: David --- pkg/rpc/methods.go | 10 +++++++--- pkg/rpc/methods_test.go | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/pkg/rpc/methods.go b/pkg/rpc/methods.go index 947b7bfa9..5ba019144 100644 --- a/pkg/rpc/methods.go +++ b/pkg/rpc/methods.go @@ -81,15 +81,18 @@ func (c *Client) ensureGenesisMatched(ctx context.Context) error { // WaitTillL2ExecutionEngineSynced keeps waiting until the L2 execution engine is fully synced. func (c *Client) WaitTillL2ExecutionEngineSynced(ctx context.Context) error { - if ctx.Err() != nil { + if ctx.Err() != nil && !errors.Is(ctx.Err(), context.DeadlineExceeded) { return ctx.Err() } + start := time.Now() return backoff.Retry( func() error { - if ctx.Err() != nil { + if ctx.Err() != nil && !errors.Is(ctx.Err(), context.DeadlineExceeded) { return ctx.Err() } - progress, err := c.L2ExecutionEngineSyncProgress(ctx) + newCtx, cancel := context.WithTimeout(ctx, defaultTimeout) + defer cancel() + progress, err := c.L2ExecutionEngineSyncProgress(newCtx) if err != nil { log.Error("Fetch L2 execution engine sync progress error", "error", err) return err @@ -101,6 +104,7 @@ func (c *Client) WaitTillL2ExecutionEngineSynced(ctx context.Context) error { "currentBlockID", progress.CurrentBlockID, "highestBlockID", progress.HighestBlockID, "progress", progress.SyncProgress, + "time", time.Since(start), ) return errSyncing } diff --git a/pkg/rpc/methods_test.go b/pkg/rpc/methods_test.go index 4cc388b80..9d1a4e42d 100644 --- a/pkg/rpc/methods_test.go +++ b/pkg/rpc/methods_test.go @@ -4,12 +4,14 @@ import ( "context" "crypto/rand" "testing" + "time" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" "github.com/stretchr/testify/require" "github.com/taikoxyz/taiko-client/bindings/encoding" + "golang.org/x/sync/errgroup" ) var ( @@ -115,6 +117,24 @@ func TestWaitTillL2ExecutionEngineSyncedContextErr(t *testing.T) { require.ErrorContains(t, err, "context canceled") } +func TestWaitTillL2ExecutionEngineSyncedTimeoutErr(t *testing.T) { + client := newTestClient(t) + ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) + defer cancel() + time.Sleep(1 * time.Second) + g, ctx := errgroup.WithContext(ctx) + g.Go(func() error { + err := client.WaitTillL2ExecutionEngineSynced(ctx) + if err != nil { + return err + } + return nil + }) + cancel() + err := g.Wait() + require.ErrorContains(t, err, "context canceled") +} + func TestGetPoolContentValid(t *testing.T) { client := newTestClient(t) configs, err := client.TaikoL1.GetConfig(&bind.CallOpts{Context: context.Background()})