From e5504c713003a3640605700214b2dd3a2e6ece11 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Wed, 16 Jun 2021 15:03:11 +0300 Subject: [PATCH] [#607] network: Do not use Address.String for address comparison Signed-off-by: Leonard Lyubich --- cmd/neofs-node/container.go | 10 +++++----- cmd/neofs-node/reputation/common/remote.go | 10 +++++----- pkg/innerring/rpc.go | 2 +- pkg/network/address.go | 2 +- pkg/network/tls_test.go | 5 ++++- .../object_manager/placement/traverser_test.go | 10 ++++++++-- 6 files changed, 24 insertions(+), 15 deletions(-) diff --git a/cmd/neofs-node/container.go b/cmd/neofs-node/container.go index adabdc769..8dd79c58d 100644 --- a/cmd/neofs-node/container.go +++ b/cmd/neofs-node/container.go @@ -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) diff --git a/cmd/neofs-node/reputation/common/remote.go b/cmd/neofs-node/reputation/common/remote.go index bb4d88e63..7c11da308 100644 --- a/cmd/neofs-node/reputation/common/remote.go +++ b/cmd/neofs-node/reputation/common/remote.go @@ -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) diff --git a/pkg/innerring/rpc.go b/pkg/innerring/rpc.go index 3d812d2de..c1dbe82f6 100644 --- a/pkg/innerring/rpc.go +++ b/pkg/innerring/rpc.go @@ -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 diff --git a/pkg/network/address.go b/pkg/network/address.go index a40fef43b..d73aa1ed8 100644 --- a/pkg/network/address.go +++ b/pkg/network/address.go @@ -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() } diff --git a/pkg/network/tls_test.go b/pkg/network/tls_test.go index 1d7f106dd..2f00dad63 100644 --- a/pkg/network/tls_test.go +++ b/pkg/network/tls_test.go @@ -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) } } diff --git a/pkg/services/object_manager/placement/traverser_test.go b/pkg/services/object_manager/placement/traverser_test.go index 0cba8c3fc..a3bb48a80 100644 --- a/pkg/services/object_manager/placement/traverser_test.go +++ b/pkg/services/object_manager/placement/traverser_test.go @@ -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]) } }