[#607] network: Do not use Address.String for address comparison

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-06-16 15:03:11 +03:00 committed by Leonard Lyubich
parent dd67e2b690
commit e5504c7130
6 changed files with 24 additions and 15 deletions

View file

@ -218,16 +218,16 @@ func (r *remoteLoadAnnounceProvider) InitRemote(srv loadroute.ServerInfo) (loadc
addr := srv.Address()
if r.loadAddrSrc.LocalAddress().String() == srv.Address() {
// if local => return no-op writer
return loadcontroller.SimpleWriterProvider(new(nopLoadWriter)), nil
}
netAddr, err := network.AddressFromString(addr)
if err != nil {
return nil, fmt.Errorf("could not convert address to IP format: %w", err)
}
if network.IsLocalAddress(r.loadAddrSrc, *netAddr) {
// if local => return no-op writer
return loadcontroller.SimpleWriterProvider(new(nopLoadWriter)), nil
}
c, err := r.clientCache.Get(netAddr)
if err != nil {
return nil, fmt.Errorf("could not initialize API client: %w", err)

View file

@ -72,16 +72,16 @@ func (rtp *remoteTrustProvider) InitRemote(srv reputationcommon.ServerInfo) (rep
addr := srv.Address()
if rtp.localAddrSrc.LocalAddress().String() == srv.Address() {
// if local => return no-op writer
return trustcontroller.SimpleWriterProvider(new(NopReputationWriter)), nil
}
netAddr, err := network.AddressFromString(addr)
if err != nil {
return nil, fmt.Errorf("could not convert address to IP format: %w", err)
}
if network.IsLocalAddress(rtp.localAddrSrc, *netAddr) {
// if local => return no-op writer
return trustcontroller.SimpleWriterProvider(new(NopReputationWriter)), nil
}
c, err := rtp.clientCache.Get(netAddr)
if err != nil {
return nil, fmt.Errorf("could not initialize API client: %w", err)

View file

@ -87,7 +87,7 @@ func (c *ClientCache) getSG(ctx context.Context, addr *object.Address, nm *netma
cli, err := c.Get(netAddr)
if err != nil {
c.log.Warn("can't setup remote connection",
zap.String("address", netAddr.String()),
zap.Stringer("address", netAddr),
zap.String("error", err.Error()))
continue

View file

@ -27,7 +27,7 @@ type LocalAddressSource interface {
LocalAddress() *Address
}
// String returns multiaddr string
// String returns multiaddr string.
func (a Address) String() string {
return a.ma.String()
}

View file

@ -43,6 +43,9 @@ func TestAddress_AddTLS(t *testing.T) {
addr.AddTLS()
require.Equal(t, test.want, addr.String(), test.input)
netAddr, err := AddressFromString(test.want)
require.NoError(t, err)
require.True(t, netAddr.Equal(addr), test.input)
}
}

View file

@ -65,6 +65,12 @@ func testPlacement(t *testing.T, ss, rs []int) ([]netmap.Nodes, *container.Conta
return nodes, container.New(container.WithPolicy(policy))
}
func assertSameAddress(t *testing.T, ni *netmap.NodeInfo, addr *network.Address) {
netAddr, err := network.AddressFromString(ni.Address())
require.NoError(t, err)
require.True(t, netAddr.Equal(*addr))
}
func TestTraverserObjectScenarios(t *testing.T) {
t.Run("search scenario", func(t *testing.T) {
selectors := []int{2, 3}
@ -87,7 +93,7 @@ func TestTraverserObjectScenarios(t *testing.T) {
require.Len(t, addrs, len(nodes[i]))
for j, n := range nodes[i] {
require.Equal(t, n.Address(), addrs[j].String())
assertSameAddress(t, n.NodeInfo, addrs[j])
}
}
@ -142,7 +148,7 @@ func TestTraverserObjectScenarios(t *testing.T) {
require.Len(t, addrs, replicas[curVector])
for j := range addrs {
require.Equal(t, nodes[curVector][i+j].Address(), addrs[j].String())
assertSameAddress(t, nodes[curVector][i+j].NodeInfo, addrs[j])
}
}