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

71 lines
1.9 KiB
Go
Raw Normal View History

2020-07-24 13:54:03 +00:00
package netmap
import (
"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/pkg/errors"
)
// GetNetMapArgs groups the arguments
// of get network map test invoke call.
type GetNetMapArgs struct {
}
// GetNetMapValues groups the stack parameters
// returned by get network map test invoke.
type GetNetMapValues struct {
peers [][]byte
2020-07-24 13:54:03 +00:00
}
const nodeInfoFixedPrmNumber = 1
2020-07-24 13:54:03 +00:00
// Peers return the list of peers from
// network map in a binary format.
func (g GetNetMapValues) Peers() [][]byte {
2020-07-24 13:54:03 +00:00
return g.peers
}
// NetMap performs the test invoke of get network map
// method of NeoFS Netmap contract.
func (c *Client) NetMap(_ GetNetMapArgs) (*GetNetMapValues, error) {
2020-07-24 13:54:03 +00:00
prms, err := c.client.TestInvoke(
c.netMapMethod,
)
if err != nil {
return nil, errors.Wrapf(err, "could not perform test invocation (%s)", c.netMapMethod)
} else if ln := len(prms); ln != 1 {
return nil, errors.Errorf("unexpected stack item count (%s): %d", c.netMapMethod, ln)
}
prms, err = client.ArrayFromStackItem(prms[0])
2020-07-24 13:54:03 +00:00
if err != nil {
return nil, errors.Wrapf(err, "could not get stack item array from stack item (%s)", c.netMapMethod)
}
res := &GetNetMapValues{
peers: make([][]byte, 0, len(prms)),
2020-07-24 13:54:03 +00:00
}
for i := range prms {
peer, err := peerInfoFromStackItem(prms[i])
if err != nil {
return nil, errors.Wrapf(err, "could not parse stack item (Peer #%d)", i)
}
res.peers = append(res.peers, 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, errors.Wrapf(err, "could not get stack item array (PeerInfo)")
} else if ln := len(prms); ln != nodeInfoFixedPrmNumber {
return nil, errors.Errorf("unexpected stack item count (PeerInfo): expected %d, has %d", 1, ln)
2020-07-24 13:54:03 +00:00
}
return client.BytesFromStackItem(prms[0])
2020-07-24 13:54:03 +00:00
}