From c80bce9438c4ecfd7fb2bce1c76ff2362bf63098 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Thu, 29 Oct 2020 17:27:13 +0300 Subject: [PATCH] [#132] Implement netmap snapshot getter for inner ring Later it should be merged with morph/client getters. Signed-off-by: Alex Vanin --- go.sum | Bin 59509 -> 59592 bytes pkg/innerring/invoke/netmap.go | 68 ++++++++++++++++++++++++++++++--- 2 files changed, 62 insertions(+), 6 deletions(-) diff --git a/go.sum b/go.sum index d0aa1269742e40b87eec082d3f328449ffa80245..20bbac32ef40d59d2b5e862fc2f05ab896381f79 100644 GIT binary patch delta 76 zcmV-S0JHz~&;!WP1F*5(lSR`Ilg---H8CzSH7+n9XfZlPYIAFGD^qz;Z)i(bD=~Fx iY%oenO>}KYLuX1*N-uF&RCsbYad0bAG-I=^-N*{4pB;<< delta 14 WcmX?ck@@Qd<_#O~Zf3c6gbM&a!3YKb diff --git a/pkg/innerring/invoke/netmap.go b/pkg/innerring/invoke/netmap.go index 7f71321f..73bbdb74 100644 --- a/pkg/innerring/invoke/netmap.go +++ b/pkg/innerring/invoke/netmap.go @@ -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 ( @@ -19,12 +23,13 @@ type ( ) const ( - getEpochMethod = "epoch" - setNewEpochMethod = "newEpoch" - approvePeerMethod = "addPeer" - updatePeerStateMethod = "updateState" - setConfigMethod = "setConfigMethod" - updateInnerRingMethod = "updateInnerRingMethod" + getEpochMethod = "epoch" + setNewEpochMethod = "newEpoch" + approvePeerMethod = "addPeer" + 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 +}