From c609b186989e062af842b5491538a98475ab6ade Mon Sep 17 00:00:00 2001 From: Anton Kaliaev Date: Thu, 7 Dec 2017 13:17:09 -0600 Subject: [PATCH] tolerate unresolvable seeds (Refs #880) --- p2p/netaddress.go | 17 +++++++++-------- p2p/netaddress_test.go | 8 +++----- p2p/switch.go | 6 +++--- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/p2p/netaddress.go b/p2p/netaddress.go index d424f8c32..9cb7dd2c3 100644 --- a/p2p/netaddress.go +++ b/p2p/netaddress.go @@ -5,8 +5,8 @@ package p2p import ( - "errors" "flag" + "fmt" "net" "strconv" "time" @@ -45,7 +45,6 @@ func NewNetAddress(addr net.Addr) *NetAddress { // address in the form of "IP:Port". Also resolves the host if host // is not an IP. func NewNetAddressString(addr string) (*NetAddress, error) { - host, portStr, err := net.SplitHostPort(addr) if err != nil { return nil, err @@ -73,16 +72,18 @@ func NewNetAddressString(addr string) (*NetAddress, error) { // NewNetAddressStrings returns an array of NetAddress'es build using // the provided strings. -func NewNetAddressStrings(addrs []string) ([]*NetAddress, error) { - netAddrs := make([]*NetAddress, len(addrs)) - for i, addr := range addrs { +func NewNetAddressStrings(addrs []string) ([]*NetAddress, []error) { + netAddrs := make([]*NetAddress, 0) + errs := make([]error, 0) + for _, addr := range addrs { netAddr, err := NewNetAddressString(addr) if err != nil { - return nil, errors.New(cmn.Fmt("Error in address %s: %v", addr, err)) + errs = append(errs, fmt.Errorf("Error in address %s: %v", addr, err)) + } else { + netAddrs = append(netAddrs, netAddr) } - netAddrs[i] = netAddr } - return netAddrs, nil + return netAddrs, errs } // NewNetAddressIPPort returns a new NetAddress using the provided IP diff --git a/p2p/netaddress_test.go b/p2p/netaddress_test.go index 7e899a314..db6147500 100644 --- a/p2p/netaddress_test.go +++ b/p2p/netaddress_test.go @@ -51,11 +51,9 @@ func TestNewNetAddressString(t *testing.T) { } func TestNewNetAddressStrings(t *testing.T) { - assert, require := assert.New(t), require.New(t) - addrs, err := NewNetAddressStrings([]string{"127.0.0.1:8080", "127.0.0.2:8080"}) - require.Nil(err) - - assert.Equal(2, len(addrs)) + addrs, errs := NewNetAddressStrings([]string{"127.0.0.1:8080", "127.0.0.2:8080"}) + assert.Len(t, errs, 0) + assert.Equal(t, 2, len(addrs)) } func TestNewNetAddressIPPort(t *testing.T) { diff --git a/p2p/switch.go b/p2p/switch.go index f41b82959..4fdaec6ec 100644 --- a/p2p/switch.go +++ b/p2p/switch.go @@ -298,9 +298,9 @@ func (sw *Switch) startInitPeer(peer *peer) { // DialSeeds dials a list of seeds asynchronously in random order. func (sw *Switch) DialSeeds(addrBook *AddrBook, seeds []string) error { - netAddrs, err := NewNetAddressStrings(seeds) - if err != nil { - return err + netAddrs, errs := NewNetAddressStrings(seeds) + for _, err := range errs { + sw.Logger.Error("Error in seed's address", "err", err) } if addrBook != nil {