Skip to content

Commit 72ca378

Browse files
gupadhyayawalldiss
andauthored
fix: normalize grpc addr (#4689)
Co-authored-by: Vlad <[email protected]>
1 parent 90ce068 commit 72ca378

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

api/client/grpc.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package client
33
import (
44
"context"
55
"crypto/tls"
6+
"net"
67
"time"
78

89
grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/retry"
@@ -27,9 +28,9 @@ type CoreGRPCConfig struct {
2728
AuthToken string
2829
}
2930

30-
// Validate performs basic validation of the config.
3131
func (cfg *CoreGRPCConfig) Validate() error {
32-
_, err := utils.SanitizeAddr(cfg.Addr)
32+
normalized := utils.NormalizeGRPCAddress(cfg.Addr)
33+
_, _, err := net.SplitHostPort(normalized)
3334
return err
3435
}
3536

@@ -76,7 +77,8 @@ func grpcClient(cfg CoreGRPCConfig) (*grpc.ClientConn, error) {
7677
grpc.WithChainStreamInterceptor(authStreamInterceptor(cfg.AuthToken), retryStreamInterceptor),
7778
)
7879
}
79-
return grpc.NewClient(cfg.Addr, opts...)
80+
normalizedAddr := utils.NormalizeGRPCAddress(cfg.Addr)
81+
return grpc.NewClient(normalizedAddr, opts...)
8082
}
8183

8284
func authInterceptor(xtoken string) grpc.UnaryClientInterceptor {

libs/utils/address.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ func NormalizeAddress(addr string) string {
1919
return addr
2020
}
2121

22+
func NormalizeGRPCAddress(addr string) string {
23+
addr = NormalizeAddress(addr)
24+
if idx := strings.Index(addr, "/"); idx != -1 {
25+
addr = addr[:idx]
26+
}
27+
return addr
28+
}
29+
2230
// SanitizeAddr trims leading protocol scheme and port from the given
2331
// IP address or hostname if present.
2432
func SanitizeAddr(addr string) (string, error) {

libs/utils/address_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,28 @@ func TestValidateAddr(t *testing.T) {
7575
})
7676
}
7777
}
78+
79+
func TestNormalizeGRPCAddress(t *testing.T) {
80+
tests := []struct {
81+
addr string
82+
want string
83+
}{
84+
{addr: "https://your-quicknode-url.celestia-mocha.quiknode.pro:9090", want: "your-quicknode-url.celestia-mocha.quiknode.pro:9090"},
85+
{addr: "http://localhost:9090", want: "localhost:9090"},
86+
{addr: "https://host.example.com:9090/some/path", want: "host.example.com:9090"},
87+
{addr: "host.example.com:9090/some/path", want: "host.example.com:9090"},
88+
{addr: "localhost:9090", want: "localhost:9090"},
89+
{addr: "localhost", want: "localhost"},
90+
{addr: "https://localhost", want: "localhost"},
91+
{addr: "tcp://localhost:9090", want: "localhost:9090"},
92+
{addr: "[::1]:9090", want: "[::1]:9090"},
93+
{addr: "https://[::1]:9090", want: "[::1]:9090"},
94+
}
95+
96+
for _, tt := range tests {
97+
t.Run(tt.addr, func(t *testing.T) {
98+
got := NormalizeGRPCAddress(tt.addr)
99+
require.Equal(t, tt.want, got)
100+
})
101+
}
102+
}

0 commit comments

Comments
 (0)