Commit d949564d authored by zhiqiangxu's avatar zhiqiangxu Committed by GitHub

add `CheckAndDial` to avoid duplicate code (#13146)

* Don't repeat yourself

* add a log for loadBlocksIntoState

* op-batcher: fix log in batcher/driver.go

* modify log

---------
Co-authored-by: default avatarprotolambda <proto@protolambda.com>
Co-authored-by: default avatarMatthew Slipper <me@matthewslipper.com>
parent bb6f5001
...@@ -248,6 +248,12 @@ func (l *BatchSubmitter) loadBlocksIntoState(ctx context.Context, start, end uin ...@@ -248,6 +248,12 @@ func (l *BatchSubmitter) loadBlocksIntoState(ctx context.Context, start, end uin
if end < start { if end < start {
return fmt.Errorf("start number is > end number %d,%d", start, end) return fmt.Errorf("start number is > end number %d,%d", start, end)
} }
// we don't want to print it in the 1-block case as `loadBlockIntoState` already does
if end > start {
l.Log.Info("Loading range of multiple blocks into state", "start", start, "end", end)
}
var latestBlock *types.Block var latestBlock *types.Block
// Add all blocks to "state" // Add all blocks to "state"
for i := start; i <= end; i++ { for i := start; i <= end; i++ {
......
...@@ -152,18 +152,22 @@ func dialRPCClientWithBackoff(ctx context.Context, log log.Logger, addr string, ...@@ -152,18 +152,22 @@ func dialRPCClientWithBackoff(ctx context.Context, log log.Logger, addr string,
bOff = retry.Fixed(cfg.fixedDialBackoff) bOff = retry.Fixed(cfg.fixedDialBackoff)
} }
return retry.Do(ctx, cfg.backoffAttempts, bOff, func() (*rpc.Client, error) { return retry.Do(ctx, cfg.backoffAttempts, bOff, func() (*rpc.Client, error) {
if !IsURLAvailable(ctx, addr) { return CheckAndDial(ctx, log, addr, cfg.gethRPCOptions...)
log.Warn("failed to dial address, but may connect later", "addr", addr)
return nil, fmt.Errorf("address unavailable (%s)", addr)
}
client, err := rpc.DialOptions(ctx, addr, cfg.gethRPCOptions...)
if err != nil {
return nil, fmt.Errorf("failed to dial address (%s): %w", addr, err)
}
return client, nil
}) })
} }
func CheckAndDial(ctx context.Context, log log.Logger, addr string, options ...rpc.ClientOption) (*rpc.Client, error) {
if !IsURLAvailable(ctx, addr) {
log.Warn("failed to dial address, but may connect later", "addr", addr)
return nil, fmt.Errorf("address unavailable (%s)", addr)
}
client, err := rpc.DialOptions(ctx, addr, options...)
if err != nil {
return nil, fmt.Errorf("failed to dial address (%s): %w", addr, err)
}
return client, nil
}
func IsURLAvailable(ctx context.Context, address string) bool { func IsURLAvailable(ctx context.Context, address string) bool {
u, err := url.Parse(address) u, err := url.Parse(address)
if err != nil { if err != nil {
......
...@@ -2,7 +2,6 @@ package dial ...@@ -2,7 +2,6 @@ package dial
import ( import (
"context" "context"
"fmt"
"time" "time"
"github.com/ethereum-optimism/optimism/op-service/client" "github.com/ethereum-optimism/optimism/op-service/client"
...@@ -72,13 +71,5 @@ func dialRPCClientWithBackoff(ctx context.Context, log log.Logger, addr string) ...@@ -72,13 +71,5 @@ func dialRPCClientWithBackoff(ctx context.Context, log log.Logger, addr string)
// Dials a JSON-RPC endpoint once. // Dials a JSON-RPC endpoint once.
func dialRPCClient(ctx context.Context, log log.Logger, addr string) (*rpc.Client, error) { func dialRPCClient(ctx context.Context, log log.Logger, addr string) (*rpc.Client, error) {
if !client.IsURLAvailable(ctx, addr) { return client.CheckAndDial(ctx, log, addr)
log.Warn("failed to dial address, but may connect later", "addr", addr)
return nil, fmt.Errorf("address unavailable (%s)", addr)
}
client, err := rpc.DialOptions(ctx, addr)
if err != nil {
return nil, fmt.Errorf("failed to dial address (%s): %w", addr, err)
}
return client, nil
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment