2021-10-27 10:00:35 +00:00
|
|
|
package netmap
|
|
|
|
|
|
|
|
import (
|
2022-06-07 08:25:34 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/hrw"
|
2021-10-27 10:00:35 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/netmap"
|
2022-06-07 08:25:34 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
|
|
|
subnetid "github.com/nspcc-dev/neofs-sdk-go/subnet/id"
|
2021-10-27 10:00:35 +00:00
|
|
|
)
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// NodeInfo groups information about NeoFS storage node which is reflected
|
|
|
|
// in the NeoFS network map. Storage nodes advertise this information when
|
|
|
|
// registering with the NeoFS network. After successful registration, information
|
|
|
|
// about the nodes is available to all network participants to work with the network
|
|
|
|
// map (mainly to comply with container storage policies).
|
|
|
|
//
|
|
|
|
// NodeInfo is mutually compatible with github.com/nspcc-dev/neofs-api-go/v2/netmap.NodeInfo
|
|
|
|
// message. See ReadFromV2 / WriteToV2 methods.
|
|
|
|
//
|
|
|
|
// Instances can be created using built-in var declaration.
|
2022-06-07 02:12:39 +00:00
|
|
|
type NodeInfo struct {
|
2022-06-07 08:25:34 +00:00
|
|
|
m netmap.NodeInfo
|
2022-06-07 02:12:39 +00:00
|
|
|
}
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// reads NodeInfo from netmap.NodeInfo message. If checkFieldPresence is set,
|
|
|
|
// returns an error on absence of any protocol-required field. Verifies format of any
|
|
|
|
// presented field according to NeoFS API V2 protocol.
|
|
|
|
func (x *NodeInfo) readFromV2(m netmap.NodeInfo, checkFieldPresence bool) error {
|
|
|
|
var err error
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
binPublicKey := m.GetPublicKey()
|
|
|
|
if checkFieldPresence && len(binPublicKey) == 0 {
|
|
|
|
return errors.New("missing public key")
|
|
|
|
}
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
if checkFieldPresence && m.NumberOfAddresses() <= 0 {
|
|
|
|
return errors.New("missing network endpoints")
|
|
|
|
}
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
attributes := m.GetAttributes()
|
|
|
|
mAttr := make(map[string]struct{}, len(attributes))
|
|
|
|
for i := range attributes {
|
|
|
|
key := attributes[i].GetKey()
|
|
|
|
if key == "" {
|
|
|
|
return fmt.Errorf("empty key of the attribute #%d", i)
|
|
|
|
} else if _, ok := mAttr[key]; ok {
|
|
|
|
return fmt.Errorf("duplicated attbiuted %s", key)
|
|
|
|
}
|
|
|
|
|
|
|
|
const subnetPrefix = "__NEOFS__SUBNET_"
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case key == attrCapacity:
|
|
|
|
_, err = strconv.ParseUint(attributes[i].GetValue(), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid %s attribute: %w", attrCapacity, err)
|
|
|
|
}
|
|
|
|
case key == attrPrice:
|
|
|
|
var err error
|
|
|
|
_, err = strconv.ParseUint(attributes[i].GetValue(), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid %s attribute: %w", attrPrice, err)
|
|
|
|
}
|
|
|
|
case strings.HasPrefix(key, subnetPrefix):
|
|
|
|
var id subnetid.ID
|
|
|
|
|
|
|
|
err = id.DecodeString(strings.TrimPrefix(key, subnetPrefix))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid key to the subnet attribute %s: %w", key, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if val := attributes[i].GetValue(); val != "True" && val != "False" {
|
|
|
|
return fmt.Errorf("invalid value of the subnet attribute %s: %w", val, err)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
if attributes[i].GetValue() == "" {
|
2022-06-14 14:27:51 +00:00
|
|
|
return fmt.Errorf("empty value of the attribute %s", key)
|
2022-06-07 08:25:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
x.m = m
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// ReadFromV2 reads NodeInfo from the netmap.NodeInfo message. Checks if the
|
|
|
|
// message conforms to NeoFS API V2 protocol.
|
|
|
|
//
|
|
|
|
// See also WriteToV2.
|
|
|
|
func (x *NodeInfo) ReadFromV2(m netmap.NodeInfo) error {
|
|
|
|
return x.readFromV2(m, true)
|
|
|
|
}
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// WriteToV2 writes NodeInfo to the netmap.NodeInfo message. The message MUST NOT
|
|
|
|
// be nil.
|
|
|
|
//
|
|
|
|
// See also ReadFromV2.
|
|
|
|
func (x NodeInfo) WriteToV2(m *netmap.NodeInfo) {
|
|
|
|
*m = x.m
|
|
|
|
}
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// Marshal encodes NodeInfo into a binary format of the NeoFS API protocol
|
|
|
|
// (Protocol Buffers with direct field order).
|
|
|
|
//
|
|
|
|
// See also Unmarshal.
|
|
|
|
func (x NodeInfo) Marshal() []byte {
|
|
|
|
var m netmap.NodeInfo
|
|
|
|
x.WriteToV2(&m)
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
return m.StableMarshal(nil)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// Unmarshal decodes NeoFS API protocol binary format into the NodeInfo
|
|
|
|
// (Protocol Buffers with direct field order). Returns an error describing
|
|
|
|
// a format violation.
|
|
|
|
//
|
|
|
|
// See also Marshal.
|
|
|
|
func (x *NodeInfo) Unmarshal(data []byte) error {
|
|
|
|
var m netmap.NodeInfo
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
err := m.Unmarshal(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
2022-06-07 08:25:34 +00:00
|
|
|
|
|
|
|
return x.readFromV2(m, false)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// MarshalJSON encodes NodeInfo into a JSON format of the NeoFS API protocol
|
|
|
|
// (Protocol Buffers JSON).
|
2021-10-27 10:00:35 +00:00
|
|
|
//
|
2022-06-07 08:25:34 +00:00
|
|
|
// See also UnmarshalJSON.
|
|
|
|
func (x NodeInfo) MarshalJSON() ([]byte, error) {
|
|
|
|
var m netmap.NodeInfo
|
|
|
|
x.WriteToV2(&m)
|
|
|
|
|
|
|
|
return m.MarshalJSON()
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// UnmarshalJSON decodes NeoFS API protocol JSON format into the NodeInfo
|
|
|
|
// (Protocol Buffers JSON). Returns an error describing a format violation.
|
2021-10-27 10:00:35 +00:00
|
|
|
//
|
2022-06-07 08:25:34 +00:00
|
|
|
// See also MarshalJSON.
|
|
|
|
func (x *NodeInfo) UnmarshalJSON(data []byte) error {
|
|
|
|
var m netmap.NodeInfo
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
err := m.UnmarshalJSON(data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
return x.readFromV2(m, false)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// SetPublicKey sets binary-encoded public key bound to the node. The key
|
|
|
|
// authenticates the storage node, so it MUST be unique within the network.
|
|
|
|
//
|
|
|
|
// Argument MUST NOT be mutated, make a copy first.
|
2021-10-27 10:00:35 +00:00
|
|
|
//
|
2022-06-07 08:25:34 +00:00
|
|
|
// See also PublicKey.
|
|
|
|
func (x *NodeInfo) SetPublicKey(key []byte) {
|
|
|
|
x.m.SetPublicKey(key)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// PublicKey returns value set using SetPublicKey.
|
2021-10-27 10:00:35 +00:00
|
|
|
//
|
2022-06-07 08:25:34 +00:00
|
|
|
// Zero NodeInfo has no public key, which is incorrect according to
|
|
|
|
// NeoFS system requirements.
|
|
|
|
//
|
|
|
|
// Return value MUST not be mutated, make a copy first.
|
|
|
|
func (x NodeInfo) PublicKey() []byte {
|
|
|
|
return x.m.GetPublicKey()
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// SetNetworkEndpoints sets list to the announced node's network endpoints.
|
|
|
|
// Node MUSt have at least one announced endpoint. List MUST be unique.
|
|
|
|
// Endpoints are used for communication with the storage node within NeoFS
|
|
|
|
// network. It is expected that node serves storage node services on these
|
|
|
|
// endpoints (it also adds a wait on their network availability).
|
|
|
|
//
|
|
|
|
// Argument MUST NOT be mutated, make a copy first.
|
2021-10-27 10:00:35 +00:00
|
|
|
//
|
2022-06-07 08:25:34 +00:00
|
|
|
// See also IterateNetworkEndpoints.
|
|
|
|
func (x *NodeInfo) SetNetworkEndpoints(v ...string) {
|
|
|
|
x.m.SetAddresses(v...)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// NumberOfNetworkEndpoints returns number of network endpoints announced by the node.
|
|
|
|
//
|
|
|
|
// See also SetNetworkEndpoints.
|
|
|
|
func (x NodeInfo) NumberOfNetworkEndpoints() int {
|
|
|
|
return x.m.NumberOfAddresses()
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// IterateNetworkEndpoints iterates over network endpoints announced by the
|
|
|
|
// node and pass them into f. Breaks iteration on f's true return. Handler
|
|
|
|
// MUST NOT be nil.
|
|
|
|
//
|
|
|
|
// Zero NodeInfo contains no endpoints which is incorrect according to
|
|
|
|
// NeoFS system requirements.
|
|
|
|
//
|
|
|
|
// See also SetNetworkEndpoints.
|
|
|
|
func (x NodeInfo) IterateNetworkEndpoints(f func(string) bool) {
|
|
|
|
x.m.IterateAddresses(f)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// IterateNetworkEndpoints is an extra-sugared function over IterateNetworkEndpoints
|
|
|
|
// method which allows to unconditionally iterate over all node's network endpoints.
|
|
|
|
func IterateNetworkEndpoints(node NodeInfo, f func(string)) {
|
|
|
|
node.IterateNetworkEndpoints(func(addr string) bool {
|
|
|
|
f(addr)
|
|
|
|
return false
|
|
|
|
})
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// assert NodeInfo type provides hrw.Hasher required for HRW sorting.
|
|
|
|
var _ hrw.Hasher = NodeInfo{}
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// Hash implements hrw.Hasher interface.
|
|
|
|
//
|
|
|
|
// Hash is needed to support weighted HRW therefore sort function sorts nodes
|
|
|
|
// based on their public key. Hash isn't expected to be used directly.
|
|
|
|
func (x NodeInfo) Hash() uint64 {
|
|
|
|
return hrw.Hash(x.m.GetPublicKey())
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// less declares "less than" comparison between two NodeInfo instances:
|
|
|
|
// x1 is less than x2 if it has less Hash().
|
|
|
|
//
|
|
|
|
// Method is needed for internal placement needs.
|
|
|
|
func less(x1, x2 NodeInfo) bool {
|
|
|
|
return x1.Hash() < x2.Hash()
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
func (x *NodeInfo) setNumericAttribute(key string, num uint64) {
|
|
|
|
x.SetAttribute(key, strconv.FormatUint(num, 10))
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// SetPrice sets the storage cost declared by the node. By default, zero
|
|
|
|
// price is announced.
|
|
|
|
func (x *NodeInfo) SetPrice(price uint64) {
|
|
|
|
x.setNumericAttribute(attrPrice, price)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// Price returns price set using SetPrice.
|
|
|
|
//
|
|
|
|
// Zero NodeInfo has zero price.
|
|
|
|
func (x NodeInfo) Price() uint64 {
|
|
|
|
val := x.Attribute(attrPrice)
|
|
|
|
if val == "" {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
price, err := strconv.ParseUint(val, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("unexpected price parsing error %s: %v", val, err))
|
|
|
|
}
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
return price
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// SetCapacity sets the storage capacity declared by the node. By default, zero
|
|
|
|
// capacity is announced.
|
|
|
|
func (x *NodeInfo) SetCapacity(capacity uint64) {
|
|
|
|
x.setNumericAttribute(attrCapacity, capacity)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// capacity returns capacity set using SetCapacity.
|
2021-10-27 10:00:35 +00:00
|
|
|
//
|
2022-06-07 08:25:34 +00:00
|
|
|
// Zero NodeInfo has zero capacity.
|
|
|
|
func (x NodeInfo) capacity() uint64 {
|
|
|
|
val := x.Attribute(attrCapacity)
|
|
|
|
if val == "" {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
capacity, err := strconv.ParseUint(val, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("unexpected capacity parsing error %s: %v", val, err))
|
|
|
|
}
|
2022-06-07 02:12:39 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
return capacity
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
const attrUNLOCODE = "UN-LOCODE"
|
|
|
|
|
|
|
|
// SetLOCODE specifies node's geographic location in UN/LOCODE format. Each
|
|
|
|
// storage node MUST declare it for entrance to the NeoFS network. Node MAY
|
|
|
|
// declare the code of the nearest location as needed, for example, when it is
|
|
|
|
// impossible to unambiguously attribute the node to any location from UN/LOCODE
|
|
|
|
// database.
|
2021-10-27 10:00:35 +00:00
|
|
|
//
|
2022-06-07 08:25:34 +00:00
|
|
|
// See also LOCODE.
|
|
|
|
func (x *NodeInfo) SetLOCODE(locode string) {
|
|
|
|
x.SetAttribute(attrUNLOCODE, locode)
|
|
|
|
}
|
2022-06-07 02:12:39 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// LOCODE returns node's location code set using SetLOCODE.
|
|
|
|
//
|
|
|
|
// Zero NodeInfo has empty location code which is invalid according to
|
|
|
|
// NeoFS API system requirement.
|
|
|
|
func (x NodeInfo) LOCODE() string {
|
|
|
|
return x.Attribute(attrUNLOCODE)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// SetCountryCode sets code of the country in ISO 3166-1_alpha-2 to which
|
|
|
|
// storage node belongs (or the closest one).
|
|
|
|
//
|
|
|
|
// SetCountryCode is intended only for processing the network registration
|
|
|
|
// request by the Inner Ring. Other parties SHOULD NOT use it.
|
|
|
|
func (x *NodeInfo) SetCountryCode(countryCode string) {
|
|
|
|
x.SetAttribute("CountryCode", countryCode)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// SetCountryName sets short name of the country in ISO-3166 format to which
|
|
|
|
// storage node belongs (or the closest one).
|
|
|
|
//
|
|
|
|
// SetCountryName is intended only for processing the network registration
|
|
|
|
// request by the Inner Ring. Other parties SHOULD NOT use it.
|
|
|
|
func (x *NodeInfo) SetCountryName(country string) {
|
|
|
|
x.SetAttribute("Country", country)
|
|
|
|
}
|
2022-06-07 02:12:39 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// SetLocationName sets storage node's location name from "NameWoDiacritics"
|
|
|
|
// column in the UN/LOCODE record corresponding to the specified LOCODE.
|
|
|
|
//
|
|
|
|
// SetLocationName is intended only for processing the network registration
|
|
|
|
// request by the Inner Ring. Other parties SHOULD NOT use it.
|
|
|
|
func (x *NodeInfo) SetLocationName(location string) {
|
|
|
|
x.SetAttribute("Location", location)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// SetSubdivisionCode sets storage node's subdivision code from "SubDiv" column in
|
|
|
|
// the UN/LOCODE record corresponding to the specified LOCODE.
|
|
|
|
//
|
|
|
|
// SetSubdivisionCode is intended only for processing the network registration
|
|
|
|
// request by the Inner Ring. Other parties SHOULD NOT use it.
|
|
|
|
func (x *NodeInfo) SetSubdivisionCode(subDiv string) {
|
|
|
|
x.SetAttribute("SubDivCode", subDiv)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// SetSubdivisionName sets storage node's subdivision name in ISO 3166-2 format.
|
2021-10-27 10:00:35 +00:00
|
|
|
//
|
2022-06-07 08:25:34 +00:00
|
|
|
// SetSubdivisionName is intended only for processing the network registration
|
|
|
|
// request by the Inner Ring. Other parties SHOULD NOT use it.
|
|
|
|
func (x *NodeInfo) SetSubdivisionName(subDiv string) {
|
|
|
|
x.SetAttribute("SubDiv", subDiv)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// SetContinentName sets name of the storage node's continent from
|
|
|
|
// Seven-Continent model.
|
|
|
|
//
|
|
|
|
// SetContinentName is intended only for processing the network registration
|
|
|
|
// request by the Inner Ring. Other parties SHOULD NOT use it.
|
|
|
|
func (x *NodeInfo) SetContinentName(continent string) {
|
|
|
|
x.SetAttribute("Continent", continent)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// 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"
|
|
|
|
)
|
2022-06-07 02:12:39 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// NumberOfAttributes returns number of attributes announced by the node.
|
|
|
|
//
|
|
|
|
// See also SetAttribute.
|
|
|
|
func (x NodeInfo) NumberOfAttributes() int {
|
|
|
|
return len(x.m.GetAttributes())
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// IterateAttributes iterates over all node attributes and passes the into f.
|
|
|
|
// Handler MUST NOT be nil.
|
|
|
|
func (x NodeInfo) IterateAttributes(f func(key, value string)) {
|
|
|
|
a := x.m.GetAttributes()
|
|
|
|
for i := range a {
|
|
|
|
f(a[i].GetKey(), a[i].GetValue())
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
2022-06-07 08:25:34 +00:00
|
|
|
}
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// SetAttribute sets value of the node attribute value by the given key.
|
|
|
|
// Both key and value MUST NOT be empty.
|
|
|
|
func (x *NodeInfo) SetAttribute(key, value string) {
|
|
|
|
if key == "" {
|
|
|
|
panic("empty key in SetAttribute")
|
|
|
|
} else if value == "" {
|
|
|
|
panic("empty value in SetAttribute")
|
|
|
|
}
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
a := x.m.GetAttributes()
|
|
|
|
for i := range a {
|
|
|
|
if a[i].GetKey() == key {
|
|
|
|
a[i].SetValue(value)
|
|
|
|
return
|
|
|
|
}
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
a = append(a, netmap.Attribute{})
|
|
|
|
a[len(a)-1].SetKey(key)
|
|
|
|
a[len(a)-1].SetValue(value)
|
|
|
|
|
|
|
|
x.m.SetAttributes(a)
|
|
|
|
}
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// Attribute returns value of the node attribute set using SetAttribute by the
|
|
|
|
// given key. Returns empty string if attribute is missing.
|
|
|
|
func (x NodeInfo) Attribute(key string) string {
|
|
|
|
a := x.m.GetAttributes()
|
|
|
|
for i := range a {
|
|
|
|
if a[i].GetKey() == key {
|
|
|
|
return a[i].GetValue()
|
|
|
|
}
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
return ""
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// SortAttributes sorts node attributes set using SetAttribute lexicographically.
|
|
|
|
// The method is only needed to make NodeInfo consistent, e.g. for signing.
|
|
|
|
func (x *NodeInfo) SortAttributes() {
|
|
|
|
as := x.m.GetAttributes()
|
|
|
|
if len(as) == 0 {
|
|
|
|
return
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
sort.Slice(as, func(i, j int) bool {
|
|
|
|
switch strings.Compare(as[i].GetKey(), as[j].GetKey()) {
|
|
|
|
case -1:
|
|
|
|
return true
|
|
|
|
case 1:
|
|
|
|
return false
|
|
|
|
default:
|
|
|
|
return as[i].GetValue() < as[j].GetValue()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
x.m.SetAttributes(as)
|
|
|
|
}
|
2022-06-07 02:12:39 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// EnterSubnet writes storage node's intention to enter the given subnet.
|
|
|
|
//
|
|
|
|
// Zero NodeInfo belongs to zero subnet.
|
|
|
|
func (x *NodeInfo) EnterSubnet(id subnetid.ID) {
|
|
|
|
x.changeSubnet(id, true)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// ExitSubnet writes storage node's intention to exit the given subnet.
|
|
|
|
func (x *NodeInfo) ExitSubnet(id subnetid.ID) {
|
|
|
|
x.changeSubnet(id, false)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
func (x *NodeInfo) changeSubnet(id subnetid.ID, isMember bool) {
|
|
|
|
var (
|
|
|
|
idv2 refs.SubnetID
|
|
|
|
info netmap.NodeSubnetInfo
|
|
|
|
)
|
2022-06-07 02:12:39 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
id.WriteToV2(&idv2)
|
|
|
|
|
|
|
|
info.SetID(&idv2)
|
|
|
|
info.SetEntryFlag(isMember)
|
2021-10-27 10:00:35 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
netmap.WriteSubnetInfo(&x.m, info)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// ErrRemoveSubnet is returned when a node needs to leave the subnet.
|
|
|
|
var ErrRemoveSubnet = netmap.ErrRemoveSubnet
|
2022-06-07 02:12:39 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// IterateSubnets iterates over all subnets the node belongs to and passes the IDs to f.
|
|
|
|
// Handler MUST NOT be nil.
|
|
|
|
//
|
|
|
|
// If f returns ErrRemoveSubnet, then removes subnet entry. Note that this leads to an
|
|
|
|
// instant mutation of NodeInfo. Breaks on any other non-nil error and returns it.
|
|
|
|
//
|
|
|
|
// Returns an error if subnet incorrectly enabled/disabled.
|
|
|
|
// Returns an error if the node is not included to any subnet by the end of the loop.
|
|
|
|
//
|
|
|
|
// See also EnterSubnet, ExitSubnet.
|
|
|
|
func (x NodeInfo) IterateSubnets(f func(subnetid.ID) error) error {
|
|
|
|
var id subnetid.ID
|
|
|
|
|
|
|
|
return netmap.IterateSubnets(&x.m, func(idv2 refs.SubnetID) error {
|
|
|
|
err := id.ReadFromV2(idv2)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid subnet: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = f(id)
|
|
|
|
if errors.Is(err, ErrRemoveSubnet) {
|
|
|
|
return netmap.ErrRemoveSubnet
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
})
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
var errAbortSubnetIter = errors.New("abort subnet iterator")
|
|
|
|
|
|
|
|
// BelongsToSubnet is a helper function over the IterateSubnets method which
|
|
|
|
// checks whether a node belongs to a subnet.
|
|
|
|
//
|
|
|
|
// Zero NodeInfo belongs to zero subnet only.
|
|
|
|
func BelongsToSubnet(node NodeInfo, id subnetid.ID) bool {
|
|
|
|
err := node.IterateSubnets(func(id_ subnetid.ID) error {
|
|
|
|
if id.Equals(id_) {
|
|
|
|
return errAbortSubnetIter
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return errors.Is(err, errAbortSubnetIter)
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// SetOffline sets the state of the node to "offline". When a node updates
|
|
|
|
// information about itself in the network map, this action is interpreted as
|
|
|
|
// an intention to leave the network.
|
|
|
|
func (x *NodeInfo) SetOffline() {
|
|
|
|
x.m.SetState(netmap.Offline)
|
|
|
|
}
|
2022-06-07 02:12:39 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// IsOffline checks if the node is in the "offline" state.
|
|
|
|
//
|
|
|
|
// Zero NodeInfo has undefined state which is not offline (note that it does not
|
|
|
|
// mean online).
|
|
|
|
//
|
|
|
|
// See also SetOffline.
|
|
|
|
func (x NodeInfo) IsOffline() bool {
|
|
|
|
return x.m.GetState() == netmap.Offline
|
|
|
|
}
|
2022-06-07 02:12:39 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// SetOnline sets the state of the node to "online". When a node updates
|
|
|
|
// information about itself in the network map, this
|
|
|
|
// action is interpreted as an intention to enter the network.
|
|
|
|
//
|
|
|
|
// See also IsOnline.
|
|
|
|
func (x *NodeInfo) SetOnline() {
|
|
|
|
x.m.SetState(netmap.Online)
|
|
|
|
}
|
2022-06-07 02:12:39 +00:00
|
|
|
|
2022-06-07 08:25:34 +00:00
|
|
|
// IsOnline checks if the node is in the "online" state.
|
|
|
|
//
|
|
|
|
// Zero NodeInfo has undefined state which is not online (note that it does not
|
|
|
|
// mean offline).
|
|
|
|
//
|
|
|
|
// See also SetOnline.
|
|
|
|
func (x NodeInfo) IsOnline() bool {
|
|
|
|
return x.m.GetState() == netmap.Online
|
2021-10-27 10:00:35 +00:00
|
|
|
}
|