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

152 lines
3.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 {
}
// GetSnapshotArgs groups the arguments
// of get netmap snapshot test invoke call.
type GetSnapshotArgs struct {
diff uint64
}
2020-07-24 13:54:03 +00:00
// 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
}
// EpochSnapshotArgs groups the arguments
// of snapshot by epoch test invoke call.
type EpochSnapshotArgs struct {
epoch uint64
}
// EpochSnapshotValues groups the stack parameters
// returned by snapshot by epoch test invoke.
type EpochSnapshotValues struct {
*GetNetMapValues
}
const nodeInfoFixedPrmNumber = 1
2020-07-24 13:54:03 +00:00
// SetDiff sets argument for snapshot method of
// netmap contract.
func (g *GetSnapshotArgs) SetDiff(d uint64) {
g.diff = d
}
// SetEpoch sets epoch number to get snapshot.
func (a *EpochSnapshotArgs) SetEpoch(d uint64) {
a.epoch = d
}
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)
}
return peersFromStackItems(prms, c.netMapMethod)
}
// NetMap performs the test invoke of get snapshot of network map
// from NeoFS Netmap contract. Contract saves only one previous epoch,
// so all invokes with diff > 1 return error.
func (c *Client) Snapshot(a GetSnapshotArgs) (*GetNetMapValues, error) {
prms, err := c.client.TestInvoke(
c.snapshotMethod,
int64(a.diff),
)
if err != nil {
return nil, errors.Wrapf(err,
"could not perform test invocation (%s)",
c.netMapMethod)
}
return peersFromStackItems(prms, c.snapshotMethod)
}
// EpochSnapshot performs the test invoke of get snapshot of network map by epoch
// from NeoFS Netmap contract.
func (c *Client) EpochSnapshot(args EpochSnapshotArgs) (*EpochSnapshotValues, error) {
prms, err := c.client.TestInvoke(
c.epochSnapshotMethod,
int64(args.epoch),
)
if err != nil {
return nil, errors.Wrapf(err,
"could not perform test invocation (%s)",
c.epochSnapshotMethod)
}
nmVals, err := peersFromStackItems(prms, c.epochSnapshotMethod)
if err != nil {
return nil, err
}
return &EpochSnapshotValues{
GetNetMapValues: nmVals,
}, nil
}
func peersFromStackItems(stack []stackitem.Item, method string) (*GetNetMapValues, error) {
if ln := len(stack); ln != 1 {
return nil, errors.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, errors.Wrapf(err,
"could not get stack item array from stack item (%s)",
method)
2020-07-24 13:54:03 +00:00
}
res := &GetNetMapValues{
peers: 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, errors.Wrapf(err,
"could not parse stack item (Peer #%d)", i)
2020-07-24 13:54:03 +00:00
}
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
}