From b6db699b6e983df50e8239b852cc80da9b0544ee Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Sun, 28 Nov 2021 15:56:11 +0300 Subject: [PATCH] [#990] morph/client: Add `NodeAllowed` subnet method Signed-off-by: Pavel Karpy --- pkg/morph/client/subnet/node.go | 58 +++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 pkg/morph/client/subnet/node.go diff --git a/pkg/morph/client/subnet/node.go b/pkg/morph/client/subnet/node.go new file mode 100644 index 00000000..c09e4ca8 --- /dev/null +++ b/pkg/morph/client/subnet/node.go @@ -0,0 +1,58 @@ +package morphsubnet + +import ( + "fmt" + + "github.com/nspcc-dev/neofs-node/pkg/morph/client" +) + +// NodeAllowedPrm groups parameters of NodeAllowed method of Subnet contract. +type NodeAllowedPrm struct { + cliPrm client.TestInvokePrm + + args [2]interface{} +} + +// SetID sets identifier of the subnet of the node in a binary NeoFS API protocol format. +func (x *NodeAllowedPrm) SetID(id []byte) { + x.args[0] = id +} + +// SetNode sets public key of the node that is being checked. +func (x *NodeAllowedPrm) SetNode(id []byte) { + x.args[1] = id +} + +// NodeAllowedRes groups resulting values of NodeAllowed method of Subnet contract. +type NodeAllowedRes struct { + result bool +} + +// Allowed returns true iff the node is allowed to enter the subnet. +func (x NodeAllowedRes) Allowed() bool { + return x.result +} + +// NodeAllowed checks if the node is included in the subnetwork. +func (x *Client) NodeAllowed(prm NodeAllowedPrm) (*NodeAllowedRes, error) { + prm.cliPrm.SetMethod("nodeAllowed") + prm.cliPrm.SetArgs(prm.args[:]...) + + res, err := x.client.TestInvoke(prm.cliPrm) + if err != nil { + return nil, fmt.Errorf("could not make test invoke: %w", err) + } + + if len(res) == 0 { + return nil, errEmptyResponse + } + + result, err := client.BoolFromStackItem(res[0]) + if err != nil { + return nil, err + } + + return &NodeAllowedRes{ + result: result, + }, nil +}