From 0c6cdd0afdd2bc70dc517651f003b4ec3d398dd4 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Thu, 25 Nov 2021 22:12:31 +0300 Subject: [PATCH] [#987] morph/event: Add `RemoveNode` event and its parser Signed-off-by: Pavel Karpy --- go.mod | 4 +- go.sum | Bin 93004 -> 93262 bytes pkg/morph/event/subnet/remove_node.go | 69 +++++++++++++++++++++ pkg/morph/event/subnet/remove_node_test.go | 57 +++++++++++++++++ 4 files changed, 128 insertions(+), 2 deletions(-) create mode 100644 pkg/morph/event/subnet/remove_node.go create mode 100644 pkg/morph/event/subnet/remove_node_test.go diff --git a/go.mod b/go.mod index 854676263..0806974ab 100644 --- a/go.mod +++ b/go.mod @@ -12,8 +12,8 @@ require ( github.com/multiformats/go-multiaddr v0.4.0 github.com/nspcc-dev/hrw v1.0.9 github.com/nspcc-dev/neo-go v0.97.4-pre.0.20211123163659-b25c3775e847 - github.com/nspcc-dev/neofs-api-go/v2 v2.11.0-pre.0.20211118144033-580f6c5554ff - github.com/nspcc-dev/neofs-sdk-go v0.0.0-20211123100340-d9317cbea191 + github.com/nspcc-dev/neofs-api-go/v2 v2.11.0-pre.0.20211124141318-d93828f46514 + github.com/nspcc-dev/neofs-sdk-go v0.0.0-20211126123811-1dde267424aa github.com/nspcc-dev/tzhash v1.4.0 github.com/panjf2000/ants/v2 v2.4.0 github.com/paulmach/orb v0.2.2 diff --git a/go.sum b/go.sum index 5dd613f6e6604a86f87626d15f948ac7e1a1c493..014590ee12489de16856177ce86b6f9f96292fa0 100644 GIT binary patch delta 293 zcmX?ejrH6O)(u~~CYN*vPd?No=VD}HXkut=XrY^8X>4I+k!E6MYG|U6VQ7_{9HpIN zk>{0Y;bG(%?(CCOWEL5mm{(X{5)xn*nHipF5tQp(5@M0)Up%?6UwrcVUN(?{qF4>w zT+;oOaq_)hU!XE+XEQ@1V+%t=UBi@=R3kHU6C;zvM39vw7f=Wj;k= zVSyESe#u@=S%zjF#eVtzrBMN1`UL^{Wv);wrMHq)$ delta 139 zcmX?igZ0cc)(u~~^c6A;t+I^$l2XFFBLjRhEcKHEeO&Z?G9t~>-MuTr9L)+#{5^}k z^pm|aBf>)iCl~gMPfqA%o9xjoH~B$V#%AB1MT}rWLeoR@it>XTQ^P|FN;Aug%{&9s meACLx%cG1#1CmRsj4HDOLJgdf+|vxeh8*av*{s{IcozUo!Zh0e diff --git a/pkg/morph/event/subnet/remove_node.go b/pkg/morph/event/subnet/remove_node.go new file mode 100644 index 000000000..81b2b7acc --- /dev/null +++ b/pkg/morph/event/subnet/remove_node.go @@ -0,0 +1,69 @@ +package subnet + +import ( + "fmt" + + "github.com/nspcc-dev/neo-go/pkg/rpc/response/result/subscriptions" + "github.com/nspcc-dev/neo-go/pkg/util" + "github.com/nspcc-dev/neofs-node/pkg/morph/client" + "github.com/nspcc-dev/neofs-node/pkg/morph/event" +) + +// RemoveNode structure of subnet.RemoveNode notification from morph chain. +type RemoveNode struct { + subnetID []byte + nodeKey []byte + + // txHash is used in notary environmental + // for calculating unique but same for + // all notification receivers values. + txHash util.Uint256 +} + +// MorphEvent implements Neo:Morph Event interface. +func (RemoveNode) MorphEvent() {} + +// SubnetworkID returns a marshalled subnetID structure, defined in API. +func (rn RemoveNode) SubnetworkID() []byte { return rn.subnetID } + +// Node is public key of the nodeKey that is being deleted. +func (rn RemoveNode) Node() []byte { return rn.nodeKey } + +// TxHash returns hash of the TX with RemoveNode +// notification. +func (rn RemoveNode) TxHash() util.Uint256 { return rn.txHash } + +const expectedItemNumRemoveNode = 2 + +// ParseRemoveNode parses notification into subnet event structure. +// +// Expects 2 stack items. +func ParseRemoveNode(e *subscriptions.NotificationEvent) (event.Event, error) { + var ( + ev RemoveNode + err error + ) + + params, err := event.ParseStackArray(e) + if err != nil { + return nil, fmt.Errorf("could not parse stack items from notify event: %w", err) + } + + if ln := len(params); ln != expectedItemNumRemoveNode { + return nil, event.WrongNumberOfParameters(expectedItemNumRemoveNode, ln) + } + + ev.subnetID, err = client.BytesFromStackItem(params[0]) + if err != nil { + return nil, fmt.Errorf("could not get raw subnetID: %w", err) + } + + ev.nodeKey, err = client.BytesFromStackItem(params[1]) + if err != nil { + return nil, fmt.Errorf("could not get raw public key of the node: %w", err) + } + + ev.txHash = e.Container + + return ev, nil +} diff --git a/pkg/morph/event/subnet/remove_node_test.go b/pkg/morph/event/subnet/remove_node_test.go new file mode 100644 index 000000000..40c3d76cb --- /dev/null +++ b/pkg/morph/event/subnet/remove_node_test.go @@ -0,0 +1,57 @@ +package subnet + +import ( + "testing" + + "github.com/nspcc-dev/neo-go/pkg/core/state" + "github.com/nspcc-dev/neo-go/pkg/crypto/keys" + "github.com/nspcc-dev/neo-go/pkg/rpc/response/result/subscriptions" + "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" + subnetid "github.com/nspcc-dev/neofs-sdk-go/subnet/id" + "github.com/stretchr/testify/require" +) + +func TestParseRemoveNode(t *testing.T) { + t.Run("wrong number of arguments", func(t *testing.T) { + _, err := ParseRemoveNode(createNotifyEventFromItems([]stackitem.Item{})) + require.Error(t, err) + }) + + t.Run("invalid item type", func(t *testing.T) { + args := []stackitem.Item{stackitem.NewMap(), stackitem.Make(123)} + _, err := ParseRemoveNode(createNotifyEventFromItems(args)) + require.Error(t, err) + }) + + subnetID := subnetid.ID{} + subnetID.SetNumber(123) + + rawSubnetID, err := subnetID.Marshal() + require.NoError(t, err) + + priv, err := keys.NewPrivateKey() + require.NoError(t, err) + + pub := priv.PublicKey() + + t.Run("good", func(t *testing.T) { + args := []stackitem.Item{stackitem.NewByteArray(rawSubnetID), stackitem.Make(pub.Bytes())} + + e, err := ParseRemoveNode(createNotifyEventFromItems(args)) + require.NoError(t, err) + + gotRaw := e.(RemoveNode).SubnetworkID() + require.NoError(t, err) + + require.Equal(t, rawSubnetID, gotRaw) + require.Equal(t, pub.Bytes(), e.(RemoveNode).Node()) + }) +} + +func createNotifyEventFromItems(items []stackitem.Item) *subscriptions.NotificationEvent { + return &subscriptions.NotificationEvent{ + NotificationEvent: state.NotificationEvent{ + Item: stackitem.NewArray(items), + }, + } +}