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

100 lines
2.7 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 []PeerInfo // peer list in a binary format
}
const nodeInfoFixedPrmNumber = 3
// Peers return the list of peers from
// network map in a binary format.
func (g GetNetMapValues) Peers() []PeerInfo {
return g.peers
}
// NetMap performs the test invoke of get network map
// method of NeoFS Netmap contract.
func (c *Client) NetMap(args GetNetMapArgs) (*GetNetMapValues, error) {
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([]PeerInfo, 0, len(prms)),
}
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)
}
return res, nil
}
func peerInfoFromStackItem(prm stackitem.Item) (*PeerInfo, 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", 3, ln)
}
res := new(PeerInfo)
// Address
res.address, err = client.BytesFromStackItem(prms[0])
2020-07-24 13:54:03 +00:00
if err != nil {
return nil, errors.Wrap(err, "could not get byte array from stack item (Address)")
}
// Public key
if res.key, err = client.BytesFromStackItem(prms[1]); err != nil {
2020-07-24 13:54:03 +00:00
return nil, errors.Wrap(err, "could not get byte array from stack item (Public key)")
}
// Options
if prms, err = client.ArrayFromStackItem(prms[2]); err != nil {
2020-07-24 13:54:03 +00:00
return nil, errors.Wrapf(err, "could not get stack item array (Options)")
}
res.opts = make([][]byte, 0, len(prms))
for i := range prms {
opt, err := client.BytesFromStackItem(prms[i])
2020-07-24 13:54:03 +00:00
if err != nil {
return nil, errors.Wrapf(err, "could not get byte array from stack item (Option #%d)", i)
}
res.opts = append(res.opts, opt)
}
return res, nil
}