diff --git a/cmd/neofs-cli/modules/root.go b/cmd/neofs-cli/modules/root.go index 19db6432fb..dca7186411 100644 --- a/cmd/neofs-cli/modules/root.go +++ b/cmd/neofs-cli/modules/root.go @@ -257,13 +257,8 @@ func getSDKClient(key *ecdsa.PrivateKey) (client.Client, error) { return nil, err } - hostAddr, err := netAddr.HostAddrString() - if err != nil { - return nil, errInvalidEndpoint - } - options := []client.Option{ - client.WithAddress(hostAddr), + client.WithAddress(netAddr.HostAddrString()), client.WithDefaultPrivateKey(key), } diff --git a/pkg/network/address.go b/pkg/network/address.go index b39ed02a61..1bba70f693 100644 --- a/pkg/network/address.go +++ b/pkg/network/address.go @@ -64,13 +64,17 @@ func (a Address) IPAddrString() (string, error) { } // 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) 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. diff --git a/pkg/network/address_test.go b/pkg/network/address_test.go index aa6f155eca..6ffeaacb9a 100644 --- a/pkg/network/address_test.go +++ b/pkg/network/address_test.go @@ -62,8 +62,7 @@ func TestAddress_HostAddrString(t *testing.T) { for _, testcase := range testcases { addr := Address{testcase.ma} - got, err := addr.HostAddrString() - require.NoError(t, err) + got := addr.HostAddrString() require.Equal(t, testcase.exp, got) } @@ -76,8 +75,7 @@ func TestAddress_HostAddrString(t *testing.T) { for _, testcase := range testcases { addr := Address{testcase} - _, err := addr.HostAddrString() - require.Error(t, err) + require.Panics(t, func() { addr.HostAddrString() }) } }) } diff --git a/pkg/network/cache/client.go b/pkg/network/cache/client.go index 2e77ada0b4..e62f37334c 100644 --- a/pkg/network/cache/client.go +++ b/pkg/network/cache/client.go @@ -2,7 +2,6 @@ package cache import ( "crypto/tls" - "fmt" "sync" "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 } - hostAddr, err := 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)) + opts := append(c.opts, client.WithAddress(netAddr.HostAddrString())) if netAddr.TLSEnabled() { opts = append(opts, client.WithTLSConfig(&tls.Config{})) diff --git a/pkg/services/object/get/get_test.go b/pkg/services/object/get/get_test.go index b0232d686f..70841a05af 100644 --- a/pkg/services/object/get/get_test.go +++ b/pkg/services/object/get/get_test.go @@ -83,12 +83,7 @@ func (p *testPlacementBuilder) BuildPlacement(addr *objectSDK.Address, _ *netmap } func (c *testClientCache) get(mAddr *network.Address) (getClient, error) { - hostAddr, err := mAddr.HostAddrString() - if err != nil { - return nil, err - } - - v, ok := c.clients[hostAddr] + v, ok := c.clients[mAddr.HostAddrString()] if !ok { 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) require.NoError(t, err) - as[j], err = na.HostAddrString() - require.NoError(t, err) + as[j] = na.HostAddrString() ni := netmap.NewNodeInfo() ni.SetAddress(a) diff --git a/pkg/services/object/search/search_test.go b/pkg/services/object/search/search_test.go index 394b6ef8fe..ab7c8250a1 100644 --- a/pkg/services/object/search/search_test.go +++ b/pkg/services/object/search/search_test.go @@ -85,12 +85,7 @@ func (p *testPlacementBuilder) BuildPlacement(addr *objectSDK.Address, _ *netmap } func (c *testClientCache) get(mAddr *network.Address) (searchClient, error) { - hostAddr, err := mAddr.HostAddrString() - if err != nil { - return nil, err - } - - v, ok := c.clients[hostAddr] + v, ok := c.clients[mAddr.HostAddrString()] if !ok { 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) require.NoError(t, err) - as[j], err = na.HostAddrString() - require.NoError(t, err) + as[j] = na.HostAddrString() ni := netmap.NewNodeInfo() ni.SetAddress(a)