Skip to content

Commit b1f7db1

Browse files
committed
WS client constructor
1 parent c21c211 commit b1f7db1

5 files changed

Lines changed: 27 additions & 19 deletions

File tree

apps/evm/cmd/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ var RunCmd = &cobra.Command{
6060
return err
6161
}
6262

63-
blobClient, err := blobrpc.NewClient(context.Background(), nodeConfig.DA.Address, nodeConfig.DA.AuthToken, "")
63+
blobClient, err := blobrpc.NewWSClient(context.Background(), nodeConfig.DA.Address, nodeConfig.DA.AuthToken, "")
6464
if err != nil {
6565
return fmt.Errorf("failed to create blob client: %w", err)
6666
}

apps/grpc/cmd/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func createSequencer(
108108
genesis genesis.Genesis,
109109
executor execution.Executor,
110110
) (coresequencer.Sequencer, error) {
111-
blobClient, err := blobrpc.NewClient(ctx, nodeConfig.DA.Address, nodeConfig.DA.AuthToken, "")
111+
blobClient, err := blobrpc.NewWSClient(ctx, nodeConfig.DA.Address, nodeConfig.DA.AuthToken, "")
112112
if err != nil {
113113
return nil, fmt.Errorf("failed to create blob client: %w", err)
114114
}

apps/testapp/cmd/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func createSequencer(
111111
genesis genesis.Genesis,
112112
executor execution.Executor,
113113
) (coresequencer.Sequencer, error) {
114-
blobClient, err := blobrpc.NewClient(ctx, nodeConfig.DA.Address, nodeConfig.DA.AuthToken, "")
114+
blobClient, err := blobrpc.NewWSClient(ctx, nodeConfig.DA.Address, nodeConfig.DA.AuthToken, "")
115115
if err != nil {
116116
return nil, fmt.Errorf("failed to create blob client: %w", err)
117117
}

pkg/cmd/run_node.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,8 @@ func StartNode(
107107
}()
108108
}
109109

110-
blobClient, err := blobrpc.NewClient(ctx, nodeConfig.DA.Address, nodeConfig.DA.AuthToken, "")
111-
if err != nil {
112-
return fmt.Errorf("failed to create blob client: %w", err)
113-
}
114-
defer blobClient.Close()
115-
daClient := block.NewDAClient(blobClient, nodeConfig, logger)
116-
117-
// create a new remote signer
110+
// Validate and load signer first (before attempting DA connection, which may fail
111+
// eagerly over WebSocket if no DA server is running).
118112
var signer signer.Signer
119113
if nodeConfig.Signer.SignerType == "file" && (nodeConfig.Node.Aggregator && !nodeConfig.Node.BasedSequencer) {
120114
// Get passphrase file path
@@ -152,6 +146,13 @@ func StartNode(
152146
return fmt.Errorf("unknown signer type: %s", nodeConfig.Signer.SignerType)
153147
}
154148

149+
blobClient, err := blobrpc.NewWSClient(ctx, nodeConfig.DA.Address, nodeConfig.DA.AuthToken, "")
150+
if err != nil {
151+
return fmt.Errorf("failed to create blob client: %w", err)
152+
}
153+
defer blobClient.Close()
154+
daClient := block.NewDAClient(blobClient, nodeConfig, logger)
155+
155156
// sanity check for based sequencer
156157
if nodeConfig.Node.BasedSequencer && genesis.DAStartHeight == 0 {
157158
return fmt.Errorf("based sequencing requires DAStartHeight to be set in genesis. This value should be identical for all nodes of the chain")

pkg/da/jsonrpc/client.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,16 @@ func (c *Client) Close() {
2525
}
2626

2727
// httpToWS converts an HTTP(S) URL to a WebSocket URL.
28-
// go-jsonrpc requires WebSocket for channel-based subscriptions (e.g. Subscribe).
29-
// WebSocket connections also support regular RPC calls, so this is backward-compatible.
3028
func httpToWS(addr string) string {
3129
addr = strings.Replace(addr, "https://", "wss://", 1)
3230
addr = strings.Replace(addr, "http://", "ws://", 1)
3331
return addr
3432
}
3533

36-
// NewClient connects to the celestia-node RPC endpoint
34+
// NewClient connects to the DA RPC endpoint using the address as-is.
35+
// Uses HTTP by default (lazy connection — only connects on first RPC call).
36+
// Does NOT support channel-based subscriptions (e.g. Subscribe).
37+
// For subscription support, use NewWSClient instead.
3738
func NewClient(ctx context.Context, addr, token string, authHeaderName string) (*Client, error) {
3839
var httpHeader http.Header
3940
if token != "" {
@@ -43,19 +44,16 @@ func NewClient(ctx context.Context, addr, token string, authHeaderName string) (
4344
httpHeader = http.Header{authHeaderName: []string{fmt.Sprintf("Bearer %s", token)}}
4445
}
4546

46-
// Use WebSocket so that channel-based subscriptions (blob.Subscribe) work.
47-
wsAddr := httpToWS(addr)
48-
4947
var cl Client
5048

5149
// Connect to the blob namespace
52-
blobCloser, err := jsonrpc.NewClient(ctx, wsAddr, "blob", &cl.Blob.Internal, httpHeader)
50+
blobCloser, err := jsonrpc.NewClient(ctx, addr, "blob", &cl.Blob.Internal, httpHeader)
5351
if err != nil {
5452
return nil, fmt.Errorf("failed to connect to blob namespace: %w", err)
5553
}
5654

5755
// Connect to the header namespace
58-
headerCloser, err := jsonrpc.NewClient(ctx, wsAddr, "header", &cl.Header.Internal, httpHeader)
56+
headerCloser, err := jsonrpc.NewClient(ctx, addr, "header", &cl.Header.Internal, httpHeader)
5957
if err != nil {
6058
blobCloser()
6159
return nil, fmt.Errorf("failed to connect to header namespace: %w", err)
@@ -70,6 +68,15 @@ func NewClient(ctx context.Context, addr, token string, authHeaderName string) (
7068
return &cl, nil
7169
}
7270

71+
// NewWSClient connects to the DA RPC endpoint over WebSocket.
72+
// Automatically converts http:// to ws:// (and https:// to wss://).
73+
// Supports channel-based subscriptions (e.g. Subscribe).
74+
// Note: WebSocket connections are eager — they connect at creation time
75+
// and will fail immediately if the server is unavailable.
76+
func NewWSClient(ctx context.Context, addr, token string, authHeaderName string) (*Client, error) {
77+
return NewClient(ctx, httpToWS(addr), token, authHeaderName)
78+
}
79+
7380
// BlobAPI mirrors celestia-node's blob module (nodebuilder/blob/blob.go).
7481
// jsonrpc.NewClient wires Internal.* to RPC stubs.
7582
type BlobAPI struct {

0 commit comments

Comments
 (0)