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 | 6 +- pkg/morph/event/subnet/remove_node.go | 69 ++++++++++++++++++++++ pkg/morph/event/subnet/remove_node_test.go | 57 ++++++++++++++++++ 4 files changed, 132 insertions(+), 4 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 5dd613f6e..014590ee1 100644 --- a/go.sum +++ b/go.sum @@ -362,14 +362,16 @@ github.com/nspcc-dev/hrw v1.0.9/go.mod h1:l/W2vx83vMQo6aStyx2AuZrJ+07lGv2JQGlVkP github.com/nspcc-dev/neo-go v0.73.1-pre.0.20200303142215-f5a1b928ce09/go.mod h1:pPYwPZ2ks+uMnlRLUyXOpLieaDQSEaf4NM3zHVbRjmg= github.com/nspcc-dev/neo-go v0.97.4-pre.0.20211123163659-b25c3775e847 h1:9cRAXmYoMxWhCJnlh0c9twr6/1faDC57pdOv2ABxqLs= github.com/nspcc-dev/neo-go v0.97.4-pre.0.20211123163659-b25c3775e847/go.mod h1:ThDwtZ1KXjpz2n0UyKkS0sEth5weNqMkZ2cWSIKaxaE= -github.com/nspcc-dev/neofs-api-go/v2 v2.11.0-pre.0.20211118144033-580f6c5554ff h1:j3NbdVKYPMh9/cQLD/LhY7gGKyVA6qtOIsJ/cKiXWUQ= github.com/nspcc-dev/neofs-api-go/v2 v2.11.0-pre.0.20211118144033-580f6c5554ff/go.mod h1:oS8dycEh8PPf2Jjp6+8dlwWyEv2Dy77h/XhhcdxYEFs= +github.com/nspcc-dev/neofs-api-go/v2 v2.11.0-pre.0.20211124141318-d93828f46514 h1:ccZ+d8nJa8H2IWCLlr6YSanqwtTP6YiWa8RmCtT8aOs= +github.com/nspcc-dev/neofs-api-go/v2 v2.11.0-pre.0.20211124141318-d93828f46514/go.mod h1:oS8dycEh8PPf2Jjp6+8dlwWyEv2Dy77h/XhhcdxYEFs= github.com/nspcc-dev/neofs-crypto v0.2.0/go.mod h1:F/96fUzPM3wR+UGsPi3faVNmFlA9KAEAUQR7dMxZmNA= github.com/nspcc-dev/neofs-crypto v0.2.3/go.mod h1:8w16GEJbH6791ktVqHN9YRNH3s9BEEKYxGhlFnp0cDw= github.com/nspcc-dev/neofs-crypto v0.3.0 h1:zlr3pgoxuzrmGCxc5W8dGVfA9Rro8diFvVnBg0L4ifM= github.com/nspcc-dev/neofs-crypto v0.3.0/go.mod h1:8w16GEJbH6791ktVqHN9YRNH3s9BEEKYxGhlFnp0cDw= -github.com/nspcc-dev/neofs-sdk-go v0.0.0-20211123100340-d9317cbea191 h1:UgUnroRAeWTpuiws6IQfMfvwwZ3UPctz2ykPU0BbGf0= github.com/nspcc-dev/neofs-sdk-go v0.0.0-20211123100340-d9317cbea191/go.mod h1:zpeoUpflmlq9aDOTC72+E2JyJZDNzoqbZJFtqClBuu4= +github.com/nspcc-dev/neofs-sdk-go v0.0.0-20211126123811-1dde267424aa h1:twlGWZU0cwWnJvLrVVQxnNcJBj16HsNoOuZPJ/pP/vE= +github.com/nspcc-dev/neofs-sdk-go v0.0.0-20211126123811-1dde267424aa/go.mod h1:kyRFPFVQ7z5oeqNxawVk05E4w+fkbkWbgBIRH/wlvRQ= github.com/nspcc-dev/rfc6979 v0.1.0/go.mod h1:exhIh1PdpDC5vQmyEsGvc4YDM/lyQp/452QxGq/UEso= github.com/nspcc-dev/rfc6979 v0.2.0 h1:3e1WNxrN60/6N0DW7+UYisLeZJyfqZTNOjeV/toYvOE= github.com/nspcc-dev/rfc6979 v0.2.0/go.mod h1:exhIh1PdpDC5vQmyEsGvc4YDM/lyQp/452QxGq/UEso= 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), + }, + } +}