[#607] network: Do not return error from Address.HostAddrString method

Panic if internal `manet.DialArgs` call returns error since this is
unexpected according to `AddressFromString` implementation.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-06-16 14:08:07 +03:00 committed by Leonard Lyubich
parent 4c2d8d5ac9
commit 35f81729e4
6 changed files with 15 additions and 36 deletions

View file

@ -257,13 +257,8 @@ func getSDKClient(key *ecdsa.PrivateKey) (client.Client, error) {
return nil, err return nil, err
} }
hostAddr, err := netAddr.HostAddrString()
if err != nil {
return nil, errInvalidEndpoint
}
options := []client.Option{ options := []client.Option{
client.WithAddress(hostAddr), client.WithAddress(netAddr.HostAddrString()),
client.WithDefaultPrivateKey(key), client.WithDefaultPrivateKey(key),
} }

View file

@ -64,13 +64,17 @@ func (a Address) IPAddrString() (string, error) {
} }
// HostAddrString returns host address in string format. // HostAddrString returns host address in string format.
func (a Address) HostAddrString() (string, error) { //
// Panics if host address cannot be fetched from Address.
func (a Address) HostAddrString() string {
_, host, err := manet.DialArgs(a.ma) _, host, err := manet.DialArgs(a.ma)
if err != nil { if err != nil {
return "", fmt.Errorf("could not get host addr: %w", err) // the only correct way to construct Address is AddressFromString
// which makes this error appear unexpected
panic(fmt.Errorf("could not get host addr: %w", err))
} }
return host, nil return host
} }
// AddressFromString restores address from a string representation. // AddressFromString restores address from a string representation.

View file

@ -62,8 +62,7 @@ func TestAddress_HostAddrString(t *testing.T) {
for _, testcase := range testcases { for _, testcase := range testcases {
addr := Address{testcase.ma} addr := Address{testcase.ma}
got, err := addr.HostAddrString() got := addr.HostAddrString()
require.NoError(t, err)
require.Equal(t, testcase.exp, got) require.Equal(t, testcase.exp, got)
} }
@ -76,8 +75,7 @@ func TestAddress_HostAddrString(t *testing.T) {
for _, testcase := range testcases { for _, testcase := range testcases {
addr := Address{testcase} addr := Address{testcase}
_, err := addr.HostAddrString() require.Panics(t, func() { addr.HostAddrString() })
require.Error(t, err)
} }
}) })
} }

View file

@ -2,7 +2,6 @@ package cache
import ( import (
"crypto/tls" "crypto/tls"
"fmt"
"sync" "sync"
"github.com/nspcc-dev/neofs-api-go/pkg/client" "github.com/nspcc-dev/neofs-api-go/pkg/client"
@ -55,12 +54,7 @@ func (c *ClientCache) Get(netAddr *network.Address) (client.Client, error) {
return cli, nil return cli, nil
} }
hostAddr, err := netAddr.HostAddrString() opts := append(c.opts, client.WithAddress(netAddr.HostAddrString()))
if err != nil {
return nil, fmt.Errorf("could not parse address as a string: %w", err)
}
opts := append(c.opts, client.WithAddress(hostAddr))
if netAddr.TLSEnabled() { if netAddr.TLSEnabled() {
opts = append(opts, client.WithTLSConfig(&tls.Config{})) opts = append(opts, client.WithTLSConfig(&tls.Config{}))

View file

@ -83,12 +83,7 @@ func (p *testPlacementBuilder) BuildPlacement(addr *objectSDK.Address, _ *netmap
} }
func (c *testClientCache) get(mAddr *network.Address) (getClient, error) { func (c *testClientCache) get(mAddr *network.Address) (getClient, error) {
hostAddr, err := mAddr.HostAddrString() v, ok := c.clients[mAddr.HostAddrString()]
if err != nil {
return nil, err
}
v, ok := c.clients[hostAddr]
if !ok { if !ok {
return nil, errors.New("could not construct client") return nil, errors.New("could not construct client")
} }
@ -415,8 +410,7 @@ func testNodeMatrix(t testing.TB, dim []int) ([]netmap.Nodes, [][]string) {
na, err := network.AddressFromString(a) na, err := network.AddressFromString(a)
require.NoError(t, err) require.NoError(t, err)
as[j], err = na.HostAddrString() as[j] = na.HostAddrString()
require.NoError(t, err)
ni := netmap.NewNodeInfo() ni := netmap.NewNodeInfo()
ni.SetAddress(a) ni.SetAddress(a)

View file

@ -85,12 +85,7 @@ func (p *testPlacementBuilder) BuildPlacement(addr *objectSDK.Address, _ *netmap
} }
func (c *testClientCache) get(mAddr *network.Address) (searchClient, error) { func (c *testClientCache) get(mAddr *network.Address) (searchClient, error) {
hostAddr, err := mAddr.HostAddrString() v, ok := c.clients[mAddr.HostAddrString()]
if err != nil {
return nil, err
}
v, ok := c.clients[hostAddr]
if !ok { if !ok {
return nil, errors.New("could not construct client") return nil, errors.New("could not construct client")
} }
@ -209,8 +204,7 @@ func testNodeMatrix(t testing.TB, dim []int) ([]netmap.Nodes, [][]string) {
na, err := network.AddressFromString(a) na, err := network.AddressFromString(a)
require.NoError(t, err) require.NoError(t, err)
as[j], err = na.HostAddrString() as[j] = na.HostAddrString()
require.NoError(t, err)
ni := netmap.NewNodeInfo() ni := netmap.NewNodeInfo()
ni.SetAddress(a) ni.SetAddress(a)