Skip to content

Commit

Permalink
[#607] network: Implement WriteToNodeInfo method on Address
Browse files Browse the repository at this point in the history
Implement `Address.WriteToNodeInfo` method which sets address of `NodeInfo`
structure. Use it in storage node application.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
  • Loading branch information
Leonard Lyubich authored and cthulhu-rider committed Jun 18, 2021
1 parent e5504c7 commit 5de074f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmd/neofs-node/netmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions pkg/network/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

/*
Expand Down Expand Up @@ -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.
Expand Down
16 changes: 16 additions & 0 deletions pkg/network/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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))
}

0 comments on commit 5de074f

Please sign in to comment.