2021-10-27 10:00:35 +00:00
|
|
|
package netmap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/netmap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NodeState is an enumeration of various states of the NeoFS node.
|
|
|
|
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.
|
2022-06-07 02:12:39 +00:00
|
|
|
type NodeInfo struct {
|
|
|
|
priceAttr uint64
|
|
|
|
|
|
|
|
capAttr uint64
|
|
|
|
|
|
|
|
m *netmap.NodeInfo
|
|
|
|
}
|
2021-10-27 10:00:35 +00:00
|
|
|
|
|
|
|
const (
|
|
|
|
_ NodeState = iota
|
|
|
|
|
|
|
|
// NodeStateOffline is network unavailable state.
|
|
|
|
NodeStateOffline
|
|
|
|
|
|
|
|
// NodeStateOnline is an active state in the network.
|
|
|
|
NodeStateOnline
|
|
|
|
)
|
|
|
|
|
|
|
|
// Enumeration of well-known attributes.
|
|
|
|
const (
|
|
|
|
// AttrPrice is a key to the node attribute that indicates the
|
|
|
|
// price in GAS tokens for storing one GB of data during one Epoch.
|
|
|
|
AttrPrice = "Price"
|
|
|
|
|
|
|
|
// AttrCapacity is a key to the node attribute that indicates the
|
|
|
|
// total available disk space in Gigabytes.
|
|
|
|
AttrCapacity = "Capacity"
|
|
|
|
|
|
|
|
// AttrSubnet is a key to the node attribute that indicates the
|
|
|
|
// string ID of node's storage subnet.
|
|
|
|
AttrSubnet = "Subnet"
|
|
|
|
|
|
|
|
// AttrUNLOCODE is a key to the node attribute that indicates the
|
|
|
|
// node's geographic location in UN/LOCODE format.
|
|
|
|
AttrUNLOCODE = "UN-LOCODE"
|
|
|
|
|
|
|
|
// AttrCountryCode is a key to the node attribute that indicates the
|
|
|
|
// Country code in ISO 3166-1_alpha-2 format.
|
|
|
|
AttrCountryCode = "CountryCode"
|
|
|
|
|
|
|
|
// AttrCountry is a key to the node attribute that indicates the
|
|
|
|
// country short name in English, as defined in ISO-3166.
|
|
|
|
AttrCountry = "Country"
|
|
|
|
|
|
|
|
// AttrLocation is a key to the node attribute that indicates the
|
|
|
|
// place name of the node location.
|
|
|
|
AttrLocation = "Location"
|
|
|
|
|
|
|
|
// AttrSubDivCode is a key to the node attribute that indicates the
|
|
|
|
// country's administrative subdivision where node is located
|
|
|
|
// in ISO 3166-2 format.
|
|
|
|
AttrSubDivCode = "SubDivCode"
|
|
|
|
|
|
|
|
// AttrSubDiv is a key to the node attribute that indicates the
|
|
|
|
// country's administrative subdivision name, as defined in
|
|
|
|
// ISO 3166-2.
|
|
|
|
AttrSubDiv = "SubDiv"
|
|
|
|
|
|
|
|
// AttrContinent is a key to the node attribute that indicates the
|
|
|
|
// node's continent name according to the Seven-Continent model.
|
|
|
|
AttrContinent = "Continent"
|
|
|
|
)
|
|
|
|
|
2022-06-07 06:03:50 +00:00
|
|
|
// calcBucketWeight computes weight for a Bucket.
|
|
|
|
func calcBucketWeight(ns nodes, a aggregator, wf weightFunc) float64 {
|
2021-10-27 10:00:35 +00:00
|
|
|
for i := range ns {
|
2022-06-07 02:12:39 +00:00
|
|
|
a.Add(wf(ns[i]))
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return a.Compute()
|
|
|
|
}
|
|
|
|
|
|
|
|
// NodeStateFromV2 converts v2 NodeState to NodeState.
|
|
|
|
func NodeStateFromV2(s netmap.NodeState) NodeState {
|
|
|
|
switch s {
|
|
|
|
default:
|
|
|
|
return 0
|
|
|
|
case netmap.Online:
|
|
|
|
return NodeStateOnline
|
|
|
|
case netmap.Offline:
|
|
|
|
return NodeStateOffline
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToV2 converts NodeState to v2 NodeState.
|
|
|
|
func (s NodeState) ToV2() netmap.NodeState {
|
|
|
|
switch s {
|
|
|
|
default:
|
|
|
|
return netmap.UnspecifiedState
|
|
|
|
case NodeStateOffline:
|
|
|
|
return netmap.Offline
|
|
|
|
case NodeStateOnline:
|
|
|
|
return netmap.Online
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns string representation of NodeState.
|
|
|
|
//
|
|
|
|
// String mapping:
|
|
|
|
// * NodeStateOnline: ONLINE;
|
|
|
|
// * NodeStateOffline: OFFLINE;
|
|
|
|
// * default: UNSPECIFIED.
|
|
|
|
func (s NodeState) String() string {
|
|
|
|
return s.ToV2().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// FromString parses NodeState from a string representation.
|
|
|
|
// It is a reverse action to String().
|
|
|
|
//
|
|
|
|
// Returns true if s was parsed successfully.
|
|
|
|
func (s *NodeState) FromString(str string) bool {
|
|
|
|
var g netmap.NodeState
|
|
|
|
|
|
|
|
ok := g.FromString(str)
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
*s = NodeStateFromV2(g)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewNodeAttribute creates and returns new NodeAttribute instance.
|
|
|
|
//
|
|
|
|
// Defaults:
|
|
|
|
// - key: "";
|
|
|
|
// - value: "";
|
|
|
|
// - parents: nil.
|
|
|
|
func NewNodeAttribute() *NodeAttribute {
|
|
|
|
return NewNodeAttributeFromV2(new(netmap.Attribute))
|
|
|
|
}
|
|
|
|
|
|
|
|
// NodeAttributeFromV2 converts v2 node Attribute to NodeAttribute.
|
|
|
|
//
|
|
|
|
// Nil netmap.Attribute converts to nil.
|
|
|
|
func NewNodeAttributeFromV2(a *netmap.Attribute) *NodeAttribute {
|
|
|
|
return (*NodeAttribute)(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToV2 converts NodeAttribute to v2 node Attribute.
|
|
|
|
//
|
|
|
|
// Nil NodeAttribute converts to nil.
|
|
|
|
func (a *NodeAttribute) ToV2() *netmap.Attribute {
|
|
|
|
return (*netmap.Attribute)(a)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Key returns key to the node attribute.
|
|
|
|
func (a *NodeAttribute) Key() string {
|
|
|
|
return (*netmap.Attribute)(a).
|
|
|
|
GetKey()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetKey sets key to the node attribute.
|
|
|
|
func (a *NodeAttribute) SetKey(key string) {
|
|
|
|
(*netmap.Attribute)(a).
|
|
|
|
SetKey(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value returns value of the node attribute.
|
|
|
|
func (a *NodeAttribute) Value() string {
|
|
|
|
return (*netmap.Attribute)(a).
|
|
|
|
GetValue()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetValue sets value of the node attribute.
|
|
|
|
func (a *NodeAttribute) SetValue(val string) {
|
|
|
|
(*netmap.Attribute)(a).
|
|
|
|
SetValue(val)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParentKeys returns list of parent keys.
|
|
|
|
func (a *NodeAttribute) ParentKeys() []string {
|
|
|
|
return (*netmap.Attribute)(a).
|
|
|
|
GetParents()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetParentKeys sets list of parent keys.
|
|
|
|
func (a *NodeAttribute) SetParentKeys(keys ...string) {
|
|
|
|
(*netmap.Attribute)(a).
|
|
|
|
SetParents(keys)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Marshal marshals NodeAttribute into a protobuf binary form.
|
|
|
|
func (a *NodeAttribute) Marshal() ([]byte, error) {
|
2022-05-30 19:05:35 +00:00
|
|
|
return (*netmap.Attribute)(a).StableMarshal(nil), nil
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal unmarshals protobuf binary representation of NodeAttribute.
|
|
|
|
func (a *NodeAttribute) Unmarshal(data []byte) error {
|
|
|
|
return (*netmap.Attribute)(a).Unmarshal(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON encodes NodeAttribute to protobuf JSON format.
|
|
|
|
func (a *NodeAttribute) MarshalJSON() ([]byte, error) {
|
|
|
|
return (*netmap.Attribute)(a).MarshalJSON()
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON decodes NodeAttribute from protobuf JSON format.
|
|
|
|
func (a *NodeAttribute) UnmarshalJSON(data []byte) error {
|
|
|
|
return (*netmap.Attribute)(a).UnmarshalJSON(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewNodeInfo creates and returns new NodeInfo instance.
|
|
|
|
//
|
|
|
|
// Defaults:
|
|
|
|
// - publicKey: nil;
|
|
|
|
// - address: "";
|
|
|
|
// - attributes nil;
|
|
|
|
// - state: 0.
|
|
|
|
func NewNodeInfo() *NodeInfo {
|
|
|
|
return NewNodeInfoFromV2(new(netmap.NodeInfo))
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewNodeInfoFromV2 converts v2 NodeInfo to NodeInfo.
|
|
|
|
//
|
|
|
|
// Nil netmap.NodeInfo converts to nil.
|
|
|
|
func NewNodeInfoFromV2(i *netmap.NodeInfo) *NodeInfo {
|
2022-06-07 02:12:39 +00:00
|
|
|
var res NodeInfo
|
|
|
|
res.m = i
|
|
|
|
res.syncAttributes()
|
|
|
|
|
|
|
|
return &res
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ToV2 converts NodeInfo to v2 NodeInfo.
|
|
|
|
//
|
|
|
|
// Nil NodeInfo converts to nil.
|
|
|
|
func (i *NodeInfo) ToV2() *netmap.NodeInfo {
|
2022-06-07 02:12:39 +00:00
|
|
|
if i == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return i.m
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PublicKey returns public key of the node in a binary format.
|
|
|
|
func (i *NodeInfo) PublicKey() []byte {
|
2022-06-07 02:12:39 +00:00
|
|
|
return i.m.GetPublicKey()
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetPublicKey sets public key of the node in a binary format.
|
|
|
|
func (i *NodeInfo) SetPublicKey(key []byte) {
|
2022-06-07 02:12:39 +00:00
|
|
|
if i.m == nil {
|
|
|
|
i.m = new(netmap.NodeInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
i.m.SetPublicKey(key)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NumberOfAddresses returns number of network addresses of the node.
|
|
|
|
func (i *NodeInfo) NumberOfAddresses() int {
|
2022-06-07 02:12:39 +00:00
|
|
|
return i.m.NumberOfAddresses()
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IterateAddresses iterates over network addresses of the node.
|
|
|
|
// Breaks iteration on f's true return.
|
|
|
|
//
|
|
|
|
// Handler should not be nil.
|
|
|
|
func (i *NodeInfo) IterateAddresses(f func(string) bool) {
|
2022-06-07 02:12:39 +00:00
|
|
|
i.m.IterateAddresses(f)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IterateAllAddresses is a helper function to unconditionally
|
|
|
|
// iterate over all node addresses.
|
|
|
|
func IterateAllAddresses(i *NodeInfo, f func(string)) {
|
|
|
|
i.IterateAddresses(func(addr string) bool {
|
|
|
|
f(addr)
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetAddresses sets list of network addresses of the node.
|
|
|
|
func (i *NodeInfo) SetAddresses(v ...string) {
|
2022-06-07 02:12:39 +00:00
|
|
|
if i.m == nil {
|
|
|
|
i.m = new(netmap.NodeInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
i.m.SetAddresses(v...)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Attributes returns list of the node attributes.
|
2022-03-11 09:13:08 +00:00
|
|
|
func (i *NodeInfo) Attributes() []NodeAttribute {
|
2021-10-27 10:00:35 +00:00
|
|
|
if i == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-07 02:12:39 +00:00
|
|
|
as := i.m.GetAttributes()
|
2021-10-27 10:00:35 +00:00
|
|
|
|
|
|
|
if as == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-11 09:13:08 +00:00
|
|
|
res := make([]NodeAttribute, len(as))
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-03-11 09:13:08 +00:00
|
|
|
for ind := range as {
|
|
|
|
res[ind] = *NewNodeAttributeFromV2(&as[ind])
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetAttributes sets list of the node attributes.
|
2022-03-11 09:13:08 +00:00
|
|
|
func (i *NodeInfo) SetAttributes(as ...NodeAttribute) {
|
|
|
|
asV2 := make([]netmap.Attribute, len(as))
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-03-11 09:13:08 +00:00
|
|
|
for ind := range as {
|
|
|
|
asV2[ind] = *as[ind].ToV2()
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 02:12:39 +00:00
|
|
|
if i.m == nil {
|
|
|
|
i.m = new(netmap.NodeInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
i.m.SetAttributes(asV2)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// State returns node state.
|
|
|
|
func (i *NodeInfo) State() NodeState {
|
2022-06-07 02:12:39 +00:00
|
|
|
return NodeStateFromV2(i.m.GetState())
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetState sets node state.
|
|
|
|
func (i *NodeInfo) SetState(s NodeState) {
|
2022-06-07 02:12:39 +00:00
|
|
|
if i.m == nil {
|
|
|
|
i.m = new(netmap.NodeInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
i.m.SetState(s.ToV2())
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Marshal marshals NodeInfo into a protobuf binary form.
|
|
|
|
func (i *NodeInfo) Marshal() ([]byte, error) {
|
2022-06-07 02:12:39 +00:00
|
|
|
return i.m.StableMarshal(nil), nil
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal unmarshals protobuf binary representation of NodeInfo.
|
|
|
|
func (i *NodeInfo) Unmarshal(data []byte) error {
|
2022-06-07 02:12:39 +00:00
|
|
|
if i.m == nil {
|
|
|
|
i.m = new(netmap.NodeInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
return i.m.Unmarshal(data)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON encodes NodeInfo to protobuf JSON format.
|
|
|
|
func (i *NodeInfo) MarshalJSON() ([]byte, error) {
|
2022-06-07 02:12:39 +00:00
|
|
|
return i.m.MarshalJSON()
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON decodes NodeInfo from protobuf JSON format.
|
|
|
|
func (i *NodeInfo) UnmarshalJSON(data []byte) error {
|
2022-06-07 02:12:39 +00:00
|
|
|
if i.m == nil {
|
|
|
|
i.m = new(netmap.NodeInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := i.m.UnmarshalJSON(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
i.syncAttributes()
|
|
|
|
|
|
|
|
return nil
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|