diff --git a/go.mod b/go.mod index 85467626..0806974a 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 5dd613f6..014590ee 100644 Binary files a/go.sum and b/go.sum differ diff --git a/pkg/morph/event/subnet/remove_node.go b/pkg/morph/event/subnet/remove_node.go new file mode 100644 index 00000000..81b2b7ac --- /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 00000000..40c3d76c --- /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), + }, + } +}