frostfs-node/pkg/morph/client/netmap/netmap.go

168 lines
4.5 KiB
Go
Raw Permalink Normal View History

2020-07-24 13:54:03 +00:00
package netmap
import (
"fmt"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
2020-07-24 13:54:03 +00:00
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
"github.com/nspcc-dev/neofs-sdk-go/netmap"
2020-07-24 13:54:03 +00:00
)
const (
nodeInfoFixedPrmNumber = 1
peerWithStateFixedPrmNumber = 2
)
2020-07-24 13:54:03 +00:00
// GetNetMapByEpoch receives information list about storage nodes
// through the Netmap contract call, composes network map
// from them and returns it. Returns snapshot of the specified epoch number.
func (c *Client) GetNetMapByEpoch(epoch uint64) (*netmap.NetMap, error) {
invokePrm := client.TestInvokePrm{}
invokePrm.SetMethod(epochSnapshotMethod)
invokePrm.SetArgs(epoch)
res, err := c.client.TestInvoke(invokePrm)
2020-07-24 13:54:03 +00:00
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w",
epochSnapshotMethod, err)
}
return unmarshalNetmap(res, epochSnapshotMethod)
}
// GetCandidates receives information list about candidates
// for the next epoch network map through the Netmap contract
// call, composes network map from them and returns it.
func (c *Client) GetCandidates() (*netmap.NetMap, error) {
invokePrm := client.TestInvokePrm{}
invokePrm.SetMethod(netMapCandidatesMethod)
prms, err := c.client.TestInvoke(invokePrm)
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w", netMapCandidatesMethod, err)
}
candVals, err := nodeInfosFromStackItems(prms, netMapCandidatesMethod)
if err != nil {
return nil, fmt.Errorf("could not parse contract response: %w", err)
}
var nm netmap.NetMap
nm.SetNodes(candVals)
return &nm, nil
}
// NetMap performs the test invoke of get network map
// method of NeoFS Netmap contract.
func (c *Client) NetMap() ([][]byte, error) {
invokePrm := client.TestInvokePrm{}
invokePrm.SetMethod(netMapMethod)
prms, err := c.client.TestInvoke(invokePrm)
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w",
netMapMethod, err)
}
return peersFromStackItems(prms, netMapMethod)
}
func nodeInfosFromStackItems(stack []stackitem.Item, method string) ([]netmap.NodeInfo, error) {
if ln := len(stack); ln != 1 {
return nil, fmt.Errorf("unexpected stack item count (%s): %d", method, ln)
}
netmapNodes, err := client.ArrayFromStackItem(stack[0])
if err != nil {
return nil, fmt.Errorf("could not get stack item array from stack item (%s): %w", method, err)
}
res := make([]netmap.NodeInfo, len(netmapNodes))
for i := range netmapNodes {
err := stackItemToNodeInfo(netmapNodes[i], &res[i])
if err != nil {
return nil, fmt.Errorf("could not parse stack item (Peer #%d): %w", i, err)
}
}
return res, nil
}
func stackItemToNodeInfo(prm stackitem.Item, res *netmap.NodeInfo) error {
prms, err := client.ArrayFromStackItem(prm)
if err != nil {
return fmt.Errorf("could not get stack item array (PeerWithState): %w", err)
} else if ln := len(prms); ln != peerWithStateFixedPrmNumber {
return fmt.Errorf(
"unexpected stack item count (PeerWithState): expected %d, has %d",
peerWithStateFixedPrmNumber,
ln,
)
}
peer, err := peerInfoFromStackItem(prms[0])
if err != nil {
return fmt.Errorf("could not get bytes from 'node' field of PeerWithState: %w", err)
} else if err = res.Unmarshal(peer); err != nil {
return fmt.Errorf("can't unmarshal peer info: %w", err)
}
// state
state, err := client.IntFromStackItem(prms[1])
if err != nil {
return fmt.Errorf("could not get int from 'state' field of PeerWithState: %w", err)
}
switch state {
case 1:
res.SetOnline()
case 2:
res.SetOffline()
}
return nil
}
func peersFromStackItems(stack []stackitem.Item, method string) ([][]byte, error) {
if ln := len(stack); ln != 1 {
return nil, fmt.Errorf("unexpected stack item count (%s): %d",
method, ln)
2020-07-24 13:54:03 +00:00
}
peers, err := client.ArrayFromStackItem(stack[0])
2020-07-24 13:54:03 +00:00
if err != nil {
return nil, fmt.Errorf("could not get stack item array from stack item (%s): %w",
method, err)
2020-07-24 13:54:03 +00:00
}
res := make([][]byte, 0, len(peers))
2020-07-24 13:54:03 +00:00
for i := range peers {
peer, err := peerInfoFromStackItem(peers[i])
2020-07-24 13:54:03 +00:00
if err != nil {
return nil, fmt.Errorf("could not parse stack item (Peer #%d): %w", i, err)
2020-07-24 13:54:03 +00:00
}
res = append(res, peer)
2020-07-24 13:54:03 +00:00
}
return res, nil
}
func peerInfoFromStackItem(prm stackitem.Item) ([]byte, error) {
prms, err := client.ArrayFromStackItem(prm)
2020-07-24 13:54:03 +00:00
if err != nil {
return nil, fmt.Errorf("could not get stack item array (PeerInfo): %w", err)
2020-07-24 13:54:03 +00:00
} else if ln := len(prms); ln != nodeInfoFixedPrmNumber {
return nil, fmt.Errorf(
"unexpected stack item count (PeerInfo): expected %d, has %d",
nodeInfoFixedPrmNumber,
ln,
)
2020-07-24 13:54:03 +00:00
}
return client.BytesFromStackItem(prms[0])
2020-07-24 13:54:03 +00:00
}