From 5de074f24c8b46e3115c2492b4491e8a52965271 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Wed, 16 Jun 2021 15:10:38 +0300 Subject: [PATCH] [#607] network: Implement WriteToNodeInfo method on Address Implement `Address.WriteToNodeInfo` method which sets address of `NodeInfo` structure. Use it in storage node application. Signed-off-by: Leonard Lyubich --- cmd/neofs-node/netmap.go | 2 +- pkg/network/address.go | 6 ++++++ pkg/network/address_test.go | 16 ++++++++++++++++ 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/cmd/neofs-node/netmap.go b/cmd/neofs-node/netmap.go index 6d06ecc6d..730530447 100644 --- a/cmd/neofs-node/netmap.go +++ b/cmd/neofs-node/netmap.go @@ -73,7 +73,7 @@ func nodeAddressFromNetmap(c *cfg) string { } func initNetmapService(c *cfg) { - c.cfgNodeInfo.localInfo.SetAddress(c.localAddr.String()) + c.localAddr.WriteToNodeInfo(&c.cfgNodeInfo.localInfo) c.cfgNodeInfo.localInfo.SetPublicKey(c.key.PublicKey().Bytes()) c.cfgNodeInfo.localInfo.SetAttributes(parseAttributes(c.appCfg)...) c.cfgNodeInfo.localInfo.SetState(netmapSDK.NodeStateOffline) diff --git a/pkg/network/address.go b/pkg/network/address.go index d73aa1ed8..8eb8a0ce7 100644 --- a/pkg/network/address.go +++ b/pkg/network/address.go @@ -7,6 +7,7 @@ import ( "github.com/multiformats/go-multiaddr" manet "github.com/multiformats/go-multiaddr/net" + "github.com/nspcc-dev/neofs-api-go/pkg/netmap" ) /* @@ -37,6 +38,11 @@ func (a Address) Equal(addr Address) bool { return a.ma.Equal(addr.ma) } +// WriteToNodeInfo writes Address to netmap.NodeInfo structure. +func (a Address) WriteToNodeInfo(ni *netmap.NodeInfo) { + ni.SetAddress(a.ma.String()) +} + // HostAddr returns host address in string format. // // Panics if host address cannot be fetched from Address. diff --git a/pkg/network/address_test.go b/pkg/network/address_test.go index 84e384c36..ee7167f07 100644 --- a/pkg/network/address_test.go +++ b/pkg/network/address_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/multiformats/go-multiaddr" + "github.com/nspcc-dev/neofs-api-go/pkg/netmap" "github.com/stretchr/testify/require" ) @@ -63,3 +64,18 @@ func buildMultiaddr(s string, t *testing.T) multiaddr.Multiaddr { require.NoError(t, err) return ma } + +func TestAddress_WriteToNodeInfo(t *testing.T) { + a := "127.0.0.1:8080" + + addr, err := AddressFromString(a) + require.NoError(t, err) + + var ni netmap.NodeInfo + + addr.WriteToNodeInfo(&ni) + + restored, err := AddressFromString(ni.Address()) + require.NoError(t, err) + require.True(t, restored.Equal(*addr)) +}