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

Commit

Permalink
Merge pull request ethereum-optimism#6705 from ethereum-optimism/jg/f…
Browse files Browse the repository at this point in the history
…ix_op_node_dial

op-node: Make it resilient to not yet ready RPC URLs
  • Loading branch information
OptimismBot authored Aug 10, 2023
2 parents d08d4ce + 5f587f0 commit 595d591
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 30 deletions.
File renamed without changes.
24 changes: 20 additions & 4 deletions op-node/client/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package client
import (
"context"
"fmt"
"net"
"net/url"
"regexp"
"time"

Expand Down Expand Up @@ -103,17 +105,31 @@ func NewRPC(ctx context.Context, lgr log.Logger, addr string, opts ...RPCOption)
func dialRPCClientWithBackoff(ctx context.Context, log log.Logger, addr string, attempts int, opts ...rpc.ClientOption) (*rpc.Client, error) {
bOff := backoff.Exponential()
return backoff.Do(ctx, attempts, bOff, func() (*rpc.Client, error) {
if !IsURLAvailable(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, opts...)
if err != nil {
if client == nil {
return nil, fmt.Errorf("failed to dial address (%s): %w", addr, err)
}
log.Warn("failed to dial address, but may connect later", "addr", addr, "err", err)
return nil, fmt.Errorf("failed to dial address (%s): %w", addr, err)
}
return client, nil
})
}

func IsURLAvailable(address string) bool {
u, err := url.Parse(address)
if err != nil {
return false
}
conn, err := net.DialTimeout("tcp", u.Host, 5*time.Second)
if err != nil {
return false
}
conn.Close()
return true
}

// BaseRPCClient is a wrapper around a concrete *rpc.Client instance to make it compliant
// with the client.RPC interface.
// It sets a timeout of 10s on CallContext & 20s on BatchCallContext made through it.
Expand Down
20 changes: 10 additions & 10 deletions op-node/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,32 +76,32 @@ func New(ctx context.Context, cfg *Config, log log.Logger, snapshotLog log.Logge

func (n *OpNode) init(ctx context.Context, cfg *Config, snapshotLog log.Logger) error {
if err := n.initTracer(ctx, cfg); err != nil {
return err
return fmt.Errorf("failed to init the trace: %w", err)
}
if err := n.initL1(ctx, cfg); err != nil {
return err
return fmt.Errorf("failed to init L1: %w", err)
}
if err := n.initRuntimeConfig(ctx, cfg); err != nil {
return err
return fmt.Errorf("failed to init the runtime config: %w", err)
}
if err := n.initL2(ctx, cfg, snapshotLog); err != nil {
return err
return fmt.Errorf("failed to init L2: %w", err)
}
if err := n.initRPCSync(ctx, cfg); err != nil {
return err
return fmt.Errorf("failed to init RPC sync: %w", err)
}
if err := n.initP2PSigner(ctx, cfg); err != nil {
return err
return fmt.Errorf("failed to init the P2P signer: %w", err)
}
if err := n.initP2P(ctx, cfg); err != nil {
return err
return fmt.Errorf("failed to init the P2P stack: %w", err)
}
// Only expose the server at the end, ensuring all RPC backend components are initialized.
if err := n.initRPCServer(ctx, cfg); err != nil {
return err
return fmt.Errorf("failed to init the RPC server: %w", err)
}
if err := n.initMetricsServer(ctx, cfg); err != nil {
return err
return fmt.Errorf("failed to init the metrics server: %w", err)
}
return nil
}
Expand All @@ -128,7 +128,7 @@ func (n *OpNode) initL1(ctx context.Context, cfg *Config) error {
}

if err := cfg.Rollup.ValidateL1Config(ctx, n.l1Source); err != nil {
return err
return fmt.Errorf("failed to validate the L1 config: %w", err)
}

// Keep subscribed to the L1 heads, which keeps the L1 maintainer pointing to the best headers to sync
Expand Down
17 changes: 1 addition & 16 deletions op-service/client/dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package client
import (
"context"
"fmt"
"net"
"net/url"
"time"

"github.com/ethereum-optimism/optimism/op-node/client"
Expand Down Expand Up @@ -53,7 +51,7 @@ func DialRollupClientWithTimeout(timeout time.Duration, log log.Logger, url stri
func dialRPCClientWithBackoff(ctx context.Context, log log.Logger, addr string) (*rpc.Client, error) {
bOff := backoff.Fixed(defaultRetryTime)
return backoff.Do(ctx, defaultRetryCount, bOff, func() (*rpc.Client, error) {
if !IsURLAvailable(addr) {
if !client.IsURLAvailable(addr) {
log.Warn("failed to dial address, but may connect later", "addr", addr)
return nil, fmt.Errorf("address unavailable (%s)", addr)
}
Expand All @@ -64,16 +62,3 @@ func dialRPCClientWithBackoff(ctx context.Context, log log.Logger, addr string)
return client, nil
})
}

func IsURLAvailable(address string) bool {
u, err := url.Parse(address)
if err != nil {
return false
}
conn, err := net.DialTimeout("tcp", u.Host, 5*time.Second)
if err != nil {
return false
}
conn.Close()
return true
}

0 comments on commit 595d591

Please sign in to comment.