Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(libs/utils): rely on DNS for rpc/grpc requests to core #3624

Merged
merged 4 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions libs/utils/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"net"
"net/netip"
"strings"
)

Expand All @@ -24,19 +25,21 @@ func SanitizeAddr(addr string) (string, error) {
return addr, nil
}

// ValidateAddr sanitizes the given address and verifies that it is a valid IP or hostname. The
// sanitized address is returned.
// ValidateAddr sanitizes the given address and verifies that it
// is a valid IP or hostname. The sanitized address is returned.
func ValidateAddr(addr string) (string, error) {
addr, err := SanitizeAddr(addr)
if err != nil {
return addr, err
}

ip := net.ParseIP(addr)
if ip != nil {
return addr, nil
// if the address is a valid IP, return it
ip, err := netip.ParseAddr(addr)
if err == nil {
return ip.String(), nil
}

// if the address is not a valid IP, resolve it
resolved, err := net.ResolveIPAddr("ip4", addr)
if err != nil {
return addr, err
Expand Down
2 changes: 2 additions & 0 deletions libs/utils/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ func TestValidateAddr(t *testing.T) {
{addr: "https://celestia.org", want: want{unresolved: true}},
// Testcase: hostname is valid, but no schema
{addr: "celestia.org", want: want{unresolved: true}},
// Testcase: localhost
{addr: "localhost", want: want{addr: "127.0.0.1"}},
}

for _, tt := range tests {
Expand Down
2 changes: 1 addition & 1 deletion nodebuilder/core/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (cfg *Config) Validate() error {
return fmt.Errorf("nodebuilder/core: grpc port is not set")
}

ip, err := utils.ValidateAddr(cfg.IP)
ip, err := utils.SanitizeAddr(cfg.IP)
if err != nil {
return err
}
Expand Down
13 changes: 11 additions & 2 deletions nodebuilder/core/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ func TestValidate(t *testing.T) {
cfg: Config{},
expectErr: false,
},
{
name: "hostname preserved",
cfg: Config{
IP: "celestia.org",
RPCPort: DefaultRPCPort,
GRPCPort: DefaultGRPCPort,
},
expectErr: false,
},
{
name: "missing RPC port",
cfg: Config{
Expand All @@ -43,13 +52,13 @@ func TestValidate(t *testing.T) {
expectErr: true,
},
{
name: "invalid IP",
name: "invalid IP, but will be accepted as host and not raise an error",
cfg: Config{
IP: "invalid-ip",
RPCPort: DefaultRPCPort,
GRPCPort: DefaultGRPCPort,
},
expectErr: true,
expectErr: false,
},
{
name: "invalid RPC port",
Expand Down
Loading