2020-07-24 13:54:03 +00:00
|
|
|
package netmap
|
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
netmapcontract "git.frostfs.info/TrueCloudLab/frostfs-contract/netmap"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
|
2020-08-27 11:57:13 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
2020-07-24 13:54:03 +00:00
|
|
|
)
|
|
|
|
|
2022-09-28 11:34:28 +00:00
|
|
|
// GetNetMapByEpoch calls "snapshotByEpoch" method with the given epoch and
|
|
|
|
// decodes netmap.NetMap from the response.
|
2022-06-08 23:18:26 +00:00
|
|
|
func (c *Client) GetNetMapByEpoch(epoch uint64) (*netmap.NetMap, error) {
|
2021-11-09 20:52:29 +00:00
|
|
|
invokePrm := client.TestInvokePrm{}
|
2022-01-31 11:58:55 +00:00
|
|
|
invokePrm.SetMethod(epochSnapshotMethod)
|
2022-03-11 09:28:34 +00:00
|
|
|
invokePrm.SetArgs(epoch)
|
2021-11-09 20:52:29 +00:00
|
|
|
|
2022-01-31 11:58:55 +00:00
|
|
|
res, err := c.client.TestInvoke(invokePrm)
|
2020-07-24 13:54:03 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not perform test invocation (%s): %w",
|
2022-01-31 11:58:55 +00:00
|
|
|
epochSnapshotMethod, err)
|
2020-09-22 08:09:45 +00:00
|
|
|
}
|
|
|
|
|
2023-01-31 10:33:33 +00:00
|
|
|
nm, err := DecodeNetMap(res)
|
2022-09-28 11:34:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-09-17 12:37:01 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 11:34:28 +00:00
|
|
|
nm.SetEpoch(epoch)
|
|
|
|
|
2022-09-17 12:37:01 +00:00
|
|
|
return nm, err
|
2021-11-10 10:44:19 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 11:34:28 +00:00
|
|
|
// GetCandidates calls "netmapCandidates" method and decodes []netmap.NodeInfo
|
|
|
|
// from the response.
|
|
|
|
func (c *Client) GetCandidates() ([]netmap.NodeInfo, error) {
|
2021-11-09 20:52:29 +00:00
|
|
|
invokePrm := client.TestInvokePrm{}
|
2022-01-31 11:58:55 +00:00
|
|
|
invokePrm.SetMethod(netMapCandidatesMethod)
|
2021-11-09 20:52:29 +00:00
|
|
|
|
2022-09-28 11:34:28 +00:00
|
|
|
res, err := c.client.TestInvoke(invokePrm)
|
2020-09-22 08:09:45 +00:00
|
|
|
if err != nil {
|
2022-01-31 11:58:55 +00:00
|
|
|
return nil, fmt.Errorf("could not perform test invocation (%s): %w", netMapCandidatesMethod, err)
|
2020-09-22 08:09:45 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 11:34:28 +00:00
|
|
|
if len(res) > 0 {
|
|
|
|
return decodeNodeList(res[0])
|
2022-01-31 11:58:55 +00:00
|
|
|
}
|
2021-11-10 10:44:19 +00:00
|
|
|
|
2022-09-28 11:34:28 +00:00
|
|
|
return nil, nil
|
2021-11-10 10:44:19 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 11:34:28 +00:00
|
|
|
// NetMap calls "netmap" method and decode netmap.NetMap from the response.
|
|
|
|
func (c *Client) NetMap() (*netmap.NetMap, error) {
|
2021-11-09 20:52:29 +00:00
|
|
|
invokePrm := client.TestInvokePrm{}
|
2022-01-31 11:58:55 +00:00
|
|
|
invokePrm.SetMethod(netMapMethod)
|
2021-11-09 20:52:29 +00:00
|
|
|
|
2022-09-28 11:34:28 +00:00
|
|
|
res, err := c.client.TestInvoke(invokePrm)
|
2021-01-12 11:26:58 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not perform test invocation (%s): %w",
|
2022-01-31 11:58:55 +00:00
|
|
|
netMapMethod, err)
|
2021-01-12 11:26:58 +00:00
|
|
|
}
|
|
|
|
|
2023-01-31 10:33:33 +00:00
|
|
|
return DecodeNetMap(res)
|
2021-11-10 10:44:19 +00:00
|
|
|
}
|
|
|
|
|
2023-01-31 10:33:33 +00:00
|
|
|
func DecodeNetMap(resStack []stackitem.Item) (*netmap.NetMap, error) {
|
2022-09-28 11:34:28 +00:00
|
|
|
var nm netmap.NetMap
|
2021-06-25 15:23:29 +00:00
|
|
|
|
2022-09-28 11:34:28 +00:00
|
|
|
if len(resStack) > 0 {
|
|
|
|
nodes, err := decodeNodeList(resStack[0])
|
2021-06-25 15:23:29 +00:00
|
|
|
if err != nil {
|
2022-09-28 11:34:28 +00:00
|
|
|
return nil, err
|
2021-06-25 15:23:29 +00:00
|
|
|
}
|
2022-09-28 11:34:28 +00:00
|
|
|
|
|
|
|
nm.SetNodes(nodes)
|
2021-06-25 15:23:29 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 11:34:28 +00:00
|
|
|
return &nm, nil
|
2021-06-25 15:23:29 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 11:34:28 +00:00
|
|
|
func decodeNodeList(itemNodes stackitem.Item) ([]netmap.NodeInfo, error) {
|
|
|
|
itemArrNodes, err := client.ArrayFromStackItem(itemNodes)
|
2021-06-25 15:23:29 +00:00
|
|
|
if err != nil {
|
2022-09-28 11:34:28 +00:00
|
|
|
return nil, fmt.Errorf("decode item array of nodes from the response item: %w", err)
|
2021-06-25 15:23:29 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 11:34:28 +00:00
|
|
|
var nodes []netmap.NodeInfo
|
2021-06-25 15:23:29 +00:00
|
|
|
|
2022-09-28 11:34:28 +00:00
|
|
|
if len(itemArrNodes) > 0 {
|
|
|
|
nodes = make([]netmap.NodeInfo, len(itemArrNodes))
|
2021-06-25 15:23:29 +00:00
|
|
|
|
2022-09-28 11:34:28 +00:00
|
|
|
for i := range itemArrNodes {
|
|
|
|
err = decodeNodeInfo(&nodes[i], itemArrNodes[i])
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("decode node #%d: %w", i+1, err)
|
|
|
|
}
|
|
|
|
}
|
2021-06-25 15:23:29 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 11:34:28 +00:00
|
|
|
return nodes, nil
|
2021-06-25 15:23:29 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 11:34:28 +00:00
|
|
|
func decodeNodeInfo(dst *netmap.NodeInfo, itemNode stackitem.Item) error {
|
|
|
|
nodeFields, err := client.ArrayFromStackItem(itemNode)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("decode item array of node fields: %w", err)
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 11:34:28 +00:00
|
|
|
var node netmapcontract.Node
|
|
|
|
|
|
|
|
if len(nodeFields) > 0 {
|
|
|
|
node.BLOB, err = client.BytesFromStackItem(nodeFields[0])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("decode node info BLOB: %w", err)
|
|
|
|
}
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 11:34:28 +00:00
|
|
|
node.State = netmapcontract.NodeStateOnline
|
2020-07-24 13:54:03 +00:00
|
|
|
|
2022-09-28 11:34:28 +00:00
|
|
|
if len(nodeFields) > 1 {
|
|
|
|
state, err := client.IntFromStackItem(nodeFields[1])
|
2020-07-24 13:54:03 +00:00
|
|
|
if err != nil {
|
2022-09-28 11:34:28 +00:00
|
|
|
return fmt.Errorf("decode integer from 2nd item: %w", err)
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 11:34:28 +00:00
|
|
|
node.State = netmapcontract.NodeState(state)
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 11:34:28 +00:00
|
|
|
err = dst.Unmarshal(node.BLOB)
|
2020-07-24 13:54:03 +00:00
|
|
|
if err != nil {
|
2022-09-28 11:34:28 +00:00
|
|
|
return fmt.Errorf("decode node info: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch node.State {
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unsupported state %v", node.State)
|
|
|
|
case netmapcontract.NodeStateOnline:
|
2024-09-18 09:23:46 +00:00
|
|
|
dst.SetStatus(netmap.Online)
|
2022-09-28 11:34:28 +00:00
|
|
|
case netmapcontract.NodeStateOffline:
|
2024-09-18 09:23:46 +00:00
|
|
|
dst.SetStatus(netmap.Offline)
|
2022-09-28 11:34:28 +00:00
|
|
|
case netmapcontract.NodeStateMaintenance:
|
2024-09-18 09:23:46 +00:00
|
|
|
dst.SetStatus(netmap.Maintenance)
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 11:34:28 +00:00
|
|
|
return nil
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|