diff --git a/pkg/netmap/node_info.go b/pkg/netmap/node_info.go index 2c14de2..327e77a 100644 --- a/pkg/netmap/node_info.go +++ b/pkg/netmap/node_info.go @@ -29,6 +29,9 @@ type NodeState uint32 // NodeAttribute represents v2 compatible attribute of the NeoFS Storage Node. type NodeAttribute netmap.Attribute +// NodeInfo represents v2 compatible descriptor of the NeoFS node. +type NodeInfo netmap.NodeInfo + const ( _ NodeState = iota @@ -200,3 +203,82 @@ func (a *NodeAttribute) SetParentKeys(keys ...string) { (*netmap.Attribute)(a). SetParents(keys) } + +// NewNodeInfo creates and returns new NodeInfo instance. +func NewNodeInfo() *NodeInfo { + return NewNodeInfoFromV2(new(netmap.NodeInfo)) +} + +// NewNodeInfoFromV2 converts v2 NodeInfo to NodeInfo. +func NewNodeInfoFromV2(i *netmap.NodeInfo) *NodeInfo { + return (*NodeInfo)(i) +} + +// ToV2 converts NodeInfo to v2 NodeInfo. +func (i *NodeInfo) ToV2() *netmap.NodeInfo { + return (*netmap.NodeInfo)(i) +} + +// PublicKey returns public key of the node in a binary format. +func (i *NodeInfo) PublicKey() []byte { + return (*netmap.NodeInfo)(i). + GetPublicKey() +} + +// SetPublicKey sets public key of the node in a binary format. +func (i *NodeInfo) SetPublicKey(key []byte) { + (*netmap.NodeInfo)(i). + SetPublicKey(key) +} + +// Address returns network endpoint address of the node. +func (i *NodeInfo) Address() string { + return (*netmap.NodeInfo)(i). + GetAddress() +} + +// SetAddress sets network endpoint address of the node. +func (i *NodeInfo) SetAddress(addr string) { + (*netmap.NodeInfo)(i). + SetAddress(addr) +} + +// Attributes returns list of the node attributes. +func (i *NodeInfo) Attributes() []*NodeAttribute { + as := (*netmap.NodeInfo)(i). + GetAttributes() + + res := make([]*NodeAttribute, 0, len(as)) + + for i := range as { + res = append(res, NewNodeAttributeFromV2(as[i])) + } + + return res +} + +// SetAttributes sets list of the node attributes. +func (i *NodeInfo) SetAttributes(as ...*NodeAttribute) { + asV2 := make([]*netmap.Attribute, 0, len(as)) + + for i := range as { + asV2 = append(asV2, as[i].ToV2()) + } + + (*netmap.NodeInfo)(i). + SetAttributes(asV2) +} + +// State returns node state. +func (i *NodeInfo) State() NodeState { + return NodeStateFromV2( + (*netmap.NodeInfo)(i). + GetState(), + ) +} + +// SetState sets node state. +func (i *NodeInfo) SetState(s NodeState) { + (*netmap.NodeInfo)(i). + SetState(s.ToV2()) +} diff --git a/pkg/netmap/node_info_test.go b/pkg/netmap/node_info_test.go index abbc96f..75de15d 100644 --- a/pkg/netmap/node_info_test.go +++ b/pkg/netmap/node_info_test.go @@ -80,3 +80,63 @@ func TestNodeAttribute_ParentKeys(t *testing.T) { require.Equal(t, keys, a.ParentKeys()) } + +func testNodeAttribute() *NodeAttribute { + a := new(NodeAttribute) + a.SetKey("key") + a.SetValue("value") + a.SetParentKeys("par1", "par2") + + return a +} + +func TestNodeInfoFromV2(t *testing.T) { + iV2 := new(netmap.NodeInfo) + iV2.SetPublicKey([]byte{1, 2, 3}) + iV2.SetAddress("456") + iV2.SetState(netmap.Online) + iV2.SetAttributes([]*netmap.Attribute{ + testNodeAttribute().ToV2(), + testNodeAttribute().ToV2(), + }) + + i := NewNodeInfoFromV2(iV2) + + require.Equal(t, iV2, i.ToV2()) +} + +func TestNodeInfo_PublicKey(t *testing.T) { + i := new(NodeInfo) + key := []byte{1, 2, 3} + + i.SetPublicKey(key) + + require.Equal(t, key, i.PublicKey()) +} + +func TestNodeInfo_Address(t *testing.T) { + i := new(NodeInfo) + a := "127.0.0.1:8080" + + i.SetAddress(a) + + require.Equal(t, a, i.Address()) +} + +func TestNodeInfo_State(t *testing.T) { + i := new(NodeInfo) + s := NodeStateOnline + + i.SetState(s) + + require.Equal(t, s, i.State()) +} + +func TestNodeInfo_Attributes(t *testing.T) { + i := new(NodeInfo) + as := []*NodeAttribute{testNodeAttribute(), testNodeAttribute()} + + i.SetAttributes(as...) + + require.Equal(t, as, i.Attributes()) +}