[#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:
parent
4c2d8d5ac9
commit
35f81729e4
6 changed files with 15 additions and 36 deletions
|
@ -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),
|
||||
}
|
||||
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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() })
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
8
pkg/network/cache/client.go
vendored
8
pkg/network/cache/client.go
vendored
|
@ -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{}))
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue