[#132] Implement netmap snapshot getter for inner ring

Later it should be merged with morph/client getters.

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2020-10-29 17:27:13 +03:00 committed by Alex Vanin
parent b12d26f974
commit c80bce9438
2 changed files with 62 additions and 6 deletions

BIN
go.sum

Binary file not shown.

View file

@ -3,7 +3,11 @@ package invoke
import (
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
netmap "github.com/nspcc-dev/neofs-api-go/v2/netmap/grpc"
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
"github.com/pkg/errors"
"google.golang.org/protobuf/proto"
)
type (
@ -25,6 +29,7 @@ const (
updatePeerStateMethod = "updateState"
setConfigMethod = "setConfigMethod"
updateInnerRingMethod = "updateInnerRingMethod"
getNetmapSnapshotMethod = "netmap"
)
// Epoch return epoch value from contract.
@ -101,3 +106,54 @@ func UpdateInnerRing(cli *client.Client, con util.Uint160, list []*keys.PublicKe
return cli.Invoke(con, extraFee, updateInnerRingMethod, rawKeys)
}
// NetmapSnapshot returns current netmap node infos.
func NetmapSnapshot(cli *client.Client, con util.Uint160) ([]netmap.NodeInfo, error) {
if cli == nil {
return nil, client.ErrNilClient
}
rawNetmapStack, err := cli.TestInvoke(con, getNetmapSnapshotMethod)
if err != nil {
return nil, err
}
if ln := len(rawNetmapStack); ln != 1 {
return nil, errors.New("invalid RPC response")
}
rawNodeInfos, err := client.ArrayFromStackItem(rawNetmapStack[0])
if err != nil {
return nil, err
}
result := make([]netmap.NodeInfo, 0, len(rawNodeInfos))
for i := range rawNodeInfos {
nodeInfo, err := peerInfoFromStackItem(rawNodeInfos[i])
if err != nil {
return nil, errors.Wrap(err, "invalid RPC response")
}
result = append(result, *nodeInfo)
}
return result, nil
}
func peerInfoFromStackItem(prm stackitem.Item) (*netmap.NodeInfo, error) {
node := new(netmap.NodeInfo)
subItems, err := client.ArrayFromStackItem(prm)
if err != nil {
return nil, err
} else if ln := len(subItems); ln != 1 {
return nil, errors.New("invalid RPC response")
} else if rawNodeInfo, err := client.BytesFromStackItem(subItems[0]); err != nil {
return nil, err
} else if err = proto.Unmarshal(rawNodeInfo, node); err != nil {
return nil, err
}
return node, nil
}