From c6576c8112eea7b98cb7f0c84be046d47ea28795 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Sat, 24 Sep 2022 13:37:08 +0300 Subject: [PATCH] [#344] netmap: Support `ExternalAddr` well-known attribute Signed-off-by: Evgenii Stratonikov --- netmap/node_info.go | 24 ++++++++++++++++++++++++ netmap/node_info_test.go | 14 ++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/netmap/node_info.go b/netmap/node_info.go index 242807a..58af58e 100644 --- a/netmap/node_info.go +++ b/netmap/node_info.go @@ -371,8 +371,32 @@ const ( // attrCapacity is a key to the node attribute that indicates the // total available disk space in Gigabytes. attrCapacity = "Capacity" + + // attrExternalAddr is a key for the attribute storing node external addresses. + attrExternalAddr = "ExternalAddr" + // sepExternalAddr is a separator for multi-value ExternalAddr attribute. + sepExternalAddr = "," ) +// SetExternalAddresses sets multi-addresses to use +// to connect to this node from outside. +// +// Panics if addr is an empty list. +func (x *NodeInfo) SetExternalAddresses(addr ...string) { + x.SetAttribute(attrExternalAddr, strings.Join(addr, sepExternalAddr)) +} + +// ExternalAddresses returns list of multi-addresses to use +// to connect to this node from outside. +func (x NodeInfo) ExternalAddresses() []string { + a := x.Attribute(attrExternalAddr) + if len(a) == 0 { + return nil + } + + return strings.Split(a, sepExternalAddr) +} + // NumberOfAttributes returns number of attributes announced by the node. // // See also SetAttribute. diff --git a/netmap/node_info_test.go b/netmap/node_info_test.go index f475132..1d1c018 100644 --- a/netmap/node_info_test.go +++ b/netmap/node_info_test.go @@ -45,3 +45,17 @@ func TestNodeInfo_Status(t *testing.T) { require.False(t, n.IsOnline()) require.False(t, n.IsOffline()) } + +func TestNodeInfo_ExternalAddr(t *testing.T) { + var n NodeInfo + + require.Empty(t, n.ExternalAddresses()) + require.Panics(t, func() { n.SetExternalAddresses() }) + + addr := []string{"1", "2", "3"} + n.SetExternalAddresses(addr[0]) + require.Equal(t, addr[:1], n.ExternalAddresses()) + + n.SetExternalAddresses(addr[1:]...) + require.Equal(t, addr[1:], n.ExternalAddresses()) +}