Skip to content

Commit

Permalink
Merge branch 'main' into hlib/update-libp2p-028
Browse files Browse the repository at this point in the history
  • Loading branch information
Wondertan authored Jun 13, 2023
2 parents 84c2ec0 + c41738a commit ce1d252
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 35 deletions.
9 changes: 6 additions & 3 deletions libs/utils/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ func ValidateAddr(addr string) (string, error) {
}

if ip := net.ParseIP(addr); ip == nil {
_, err = net.LookupHost(addr)
addrs, err := net.LookupHost(addr)
if err != nil {
return addr, fmt.Errorf("could not resolve hostname or ip: %w", err)
return addr, fmt.Errorf("could not resolve %v: %w", addr, err)
}
if len(addrs) == 0 {
return addr, fmt.Errorf("no IP addresses found for DNS record: %v", addr)
}
addr = addrs[0]
}

return addr, nil
}
35 changes: 29 additions & 6 deletions libs/utils/address_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package utils

import (
"net"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -13,8 +14,12 @@ func TestSanitizeAddr(t *testing.T) {
}{
// Testcase: trims protocol prefix
{addr: "http://celestia.org", want: "celestia.org"},
// Testcase: protocol prefix trimmed already
{addr: "celestia.org", want: "celestia.org"},
// Testcase: trims protocol prefix, and trims port and trailing slash suffix
{addr: "tcp://192.168.42.42:5050/", want: "192.168.42.42"},
// Testcase: invariant ip
{addr: "192.168.42.42", want: "192.168.42.42"},
}

for _, tt := range tests {
Expand All @@ -27,23 +32,41 @@ func TestSanitizeAddr(t *testing.T) {
}

func TestValidateAddr(t *testing.T) {
type want struct {
addr string
unresolved bool
}
var tests = []struct {
addr string
want string
want want
}{
// Testcase: ip is valid
{addr: "192.168.42.42:5050", want: "192.168.42.42"},
// Testcase: hostname is valid
{addr: "https://celestia.org", want: "celestia.org"},
{addr: "192.168.42.42:5050", want: want{addr: "192.168.42.42"}},
// Testcase: ip is valid, no port
{addr: "192.168.42.42", want: want{addr: "192.168.42.42"}},
// Testcase: resolves localhost
{addr: "http://localhost:8080/", want: "localhost"},
{addr: "http://localhost:8080/", want: want{unresolved: true}},
// Testcase: hostname is valid
{addr: "https://celestia.org", want: want{unresolved: true}},
// Testcase: hostname is valid, but no schema
{addr: "celestia.org", want: want{unresolved: true}},
}

for _, tt := range tests {
t.Run(tt.addr, func(t *testing.T) {
got, err := ValidateAddr(tt.addr)
require.NoError(t, err)
require.Equal(t, tt.want, got)

// validate that returned value is ip
if ip := net.ParseIP(got); ip == nil {
t.Fatalf("empty ip")
}

if tt.want.unresolved {
// unresolved addr has no addr to compare with
return
}
require.Equal(t, tt.want.addr, got)
})
}
}
27 changes: 6 additions & 21 deletions nodebuilder/core/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package core

import (
"fmt"
"net"
"net/url"

"github.com/spf13/cobra"
flag "github.com/spf13/pflag"
Expand Down Expand Up @@ -40,7 +38,10 @@ func Flags() *flag.FlagSet {
}

// ParseFlags parses Core flags from the given cmd and saves them to the passed config.
func ParseFlags(cmd *cobra.Command, cfg *Config) error {
func ParseFlags(
cmd *cobra.Command,
cfg *Config,
) error {
coreIP := cmd.Flag(coreFlag).Value.String()
if coreIP == "" {
if cmd.Flag(coreGRPCFlag).Changed || cmd.Flag(coreRPCFlag).Changed {
Expand All @@ -49,27 +50,11 @@ func ParseFlags(cmd *cobra.Command, cfg *Config) error {
return nil
}

ip := net.ParseIP(coreIP)
if ip == nil {
u, err := url.Parse(coreIP)
if err != nil {
return fmt.Errorf("failed to parse url: %w", err)
}
ips, err := net.LookupIP(u.Host)
if err != nil {
return fmt.Errorf("failed to resolve DNS record: %v", err)
}
if len(ips) == 0 {
return fmt.Errorf("no IP addresses found for DNS record")
}
ip = ips[0]
}

rpc := cmd.Flag(coreRPCFlag).Value.String()
grpc := cmd.Flag(coreGRPCFlag).Value.String()

cfg.IP = ip.String()
cfg.IP = coreIP
cfg.RPCPort = rpc
cfg.GRPCPort = grpc
return nil
return cfg.Validate()
}
6 changes: 3 additions & 3 deletions nodebuilder/p2p/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ var bootstrapList = map[Network][]string{
"/dns4/da-full-2-arabica-8.celestia-arabica.com/tcp/2121/p2p/12D3KooWCs3wFmqwPn1u8pNU4BGsvLsob1ShTzvps8qEtTRuuuK5",
},
Mocha: {
"/dns4/andromeda.celestia-devops.dev/tcp/2121/p2p/12D3KooWKvPXtV1yaQ6e3BRNUHa5Phh8daBwBi3KkGaSSkUPys6D",
"/dns4/libra.celestia-devops.dev/tcp/2121/p2p/12D3KooWK5aDotDcLsabBmWDazehQLMsDkRyARm1k7f1zGAXqbt4",
"/dns4/norma.celestia-devops.dev/tcp/2121/p2p/12D3KooWHYczJDVNfYVkLcNHPTDKCeiVvRhg8Q9JU3bE3m9eEVyY",
"/dns4/bootstr-mocha-1.celestia-mocha.com/tcp/2121/p2p/12D3KooWDRSJMbH3PS4dRDa11H7Tk615aqTUgkeEKz4pwd4sS6fN",
"/dns4/bootstr-mocha-2.celestia-mocha.com/tcp/2121/p2p/12D3KooWEk7cxtjQCC7kC84Uhs2j6dAHjdbwYnPcvUAqmj6Zsry2",
"/dns4/bootstr-mocha-3.celestia-mocha.com/tcp/2121/p2p/12D3KooWBE4QcFXZzENf2VRo6Y5LBvp9gzmpYRHKCvgGzEYj7Hdn",
},
BlockspaceRace: {
"/dns4/bootstr-incent-3.celestia.tools/tcp/2121/p2p/12D3KooWNzdKcHagtvvr6qtjcPTAdCN6ZBiBLH8FBHbihxqu4GZx",
Expand Down
2 changes: 1 addition & 1 deletion nodebuilder/p2p/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func GenesisFor(net Network) (string, error) {
// NOTE: Every time we add a new long-running network, its genesis hash has to be added here.
var genesisList = map[Network]string{
Arabica: "E5D620B5BE7873222DCD83464C285FD0F215C209393E7481F9A5979280AD6CA2",
Mocha: "8038B21032C941372ED601699857043C12E5CC7D5945DCEEA4567D11B5712526",
Mocha: "1181AF8EAE5DDF3CBBFF3BF3CC44C5B795DF5094F5A0CC0AE52921ECCA0AF3C8",
BlockspaceRace: "1A8491A72F73929680DAA6C93E3B593579261B2E76536BFA4F5B97D6FE76E088",
Private: "",
}
2 changes: 1 addition & 1 deletion nodebuilder/p2p/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const (
// Arabica testnet. See: celestiaorg/networks.
Arabica Network = "arabica-8"
// Mocha testnet. See: celestiaorg/networks.
Mocha Network = "mocha"
Mocha Network = "mocha-2"
// BlockspaceRace testnet. See: https://docs.celestia.org/nodes/blockspace-race/.
BlockspaceRace Network = "blockspacerace-0"
// Private can be used to set up any private network, including local testing setups.
Expand Down

0 comments on commit ce1d252

Please sign in to comment.