diff --git a/pkg/morph/event/balance/lock.go b/pkg/morph/event/balance/lock.go index 9acb5ad51..1e465b4e6 100644 --- a/pkg/morph/event/balance/lock.go +++ b/pkg/morph/event/balance/lock.go @@ -3,8 +3,9 @@ package balance 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/neo-go/pkg/vm/stackitem" "github.com/nspcc-dev/neofs-node/pkg/morph/client" "github.com/nspcc-dev/neofs-node/pkg/morph/event" ) @@ -37,12 +38,17 @@ func (l Lock) Amount() int64 { return l.amount } func (l Lock) Until() int64 { return l.until } // ParseLock from notification into lock structure. -func ParseLock(params []stackitem.Item) (event.Event, error) { +func ParseLock(e *subscriptions.NotificationEvent) (event.Event, error) { var ( ev Lock 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 != 5 { return nil, event.WrongNumberOfParameters(5, ln) } diff --git a/pkg/morph/event/balance/lock_test.go b/pkg/morph/event/balance/lock_test.go index bc0435752..7bc539a11 100644 --- a/pkg/morph/event/balance/lock_test.go +++ b/pkg/morph/event/balance/lock_test.go @@ -4,6 +4,8 @@ import ( "math/big" "testing" + "github.com/nspcc-dev/neo-go/pkg/core/state" + "github.com/nspcc-dev/neo-go/pkg/rpc/response/result/subscriptions" "github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "github.com/nspcc-dev/neofs-node/pkg/morph/event" @@ -26,68 +28,68 @@ func TestParseLock(t *testing.T) { stackitem.NewMap(), } - _, err := ParseLock(prms) + _, err := ParseLock(createNotifyEventFromItems(prms)) require.EqualError(t, err, event.WrongNumberOfParameters(5, len(prms)).Error()) }) t.Run("wrong id parameter", func(t *testing.T) { - _, err := ParseLock([]stackitem.Item{ + _, err := ParseLock(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewMap(), - }) + })) require.Error(t, err) }) t.Run("wrong from parameter", func(t *testing.T) { - _, err := ParseLock([]stackitem.Item{ + _, err := ParseLock(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewByteArray(id), stackitem.NewMap(), - }) + })) require.Error(t, err) }) t.Run("wrong lock parameter", func(t *testing.T) { - _, err := ParseLock([]stackitem.Item{ + _, err := ParseLock(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewByteArray(id), stackitem.NewByteArray(user.BytesBE()), stackitem.NewMap(), - }) + })) require.Error(t, err) }) t.Run("wrong amount parameter", func(t *testing.T) { - _, err := ParseLock([]stackitem.Item{ + _, err := ParseLock(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewByteArray(id), stackitem.NewByteArray(user.BytesBE()), stackitem.NewByteArray(lock.BytesBE()), stackitem.NewMap(), - }) + })) require.Error(t, err) }) t.Run("wrong until parameter", func(t *testing.T) { - _, err := ParseLock([]stackitem.Item{ + _, err := ParseLock(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewByteArray(id), stackitem.NewByteArray(user.BytesBE()), stackitem.NewByteArray(lock.BytesBE()), stackitem.NewBigInteger(new(big.Int).SetInt64(amount)), stackitem.NewMap(), - }) + })) require.Error(t, err) }) t.Run("correct behavior", func(t *testing.T) { - ev, err := ParseLock([]stackitem.Item{ + ev, err := ParseLock(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewByteArray(id), stackitem.NewByteArray(user.BytesBE()), stackitem.NewByteArray(lock.BytesBE()), stackitem.NewBigInteger(new(big.Int).SetInt64(amount)), stackitem.NewBigInteger(new(big.Int).SetInt64(until)), - }) + })) require.NoError(t, err) require.Equal(t, Lock{ @@ -99,3 +101,11 @@ func TestParseLock(t *testing.T) { }, ev) }) } + +func createNotifyEventFromItems(items []stackitem.Item) *subscriptions.NotificationEvent { + return &subscriptions.NotificationEvent{ + NotificationEvent: state.NotificationEvent{ + Item: stackitem.NewArray(items), + }, + } +} diff --git a/pkg/morph/event/container/delete.go b/pkg/morph/event/container/delete.go index a3115cd07..4ce7b0baa 100644 --- a/pkg/morph/event/container/delete.go +++ b/pkg/morph/event/container/delete.go @@ -3,8 +3,9 @@ package container import ( "fmt" + "github.com/nspcc-dev/neo-go/pkg/rpc/response/result/subscriptions" + "github.com/nspcc-dev/neo-go/pkg/network/payload" - "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "github.com/nspcc-dev/neofs-node/pkg/morph/client" "github.com/nspcc-dev/neofs-node/pkg/morph/event" ) @@ -46,12 +47,17 @@ const expectedItemNumDelete = 3 // ParseDelete from notification into container event structure. // // Expects 3 stack items. -func ParseDelete(params []stackitem.Item) (event.Event, error) { +func ParseDelete(e *subscriptions.NotificationEvent) (event.Event, error) { var ( ev Delete 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 != expectedItemNumDelete { return nil, event.WrongNumberOfParameters(expectedItemNumDelete, ln) } diff --git a/pkg/morph/event/container/delete_test.go b/pkg/morph/event/container/delete_test.go index 37f5edef6..ea808b3e7 100644 --- a/pkg/morph/event/container/delete_test.go +++ b/pkg/morph/event/container/delete_test.go @@ -20,43 +20,43 @@ func TestParseDelete(t *testing.T) { stackitem.NewMap(), } - _, err := ParseDelete(prms) + _, err := ParseDelete(createNotifyEventFromItems(prms)) require.EqualError(t, err, event.WrongNumberOfParameters(3, len(prms)).Error()) }) t.Run("wrong container parameter", func(t *testing.T) { - _, err := ParseDelete([]stackitem.Item{ + _, err := ParseDelete(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewMap(), - }) + })) require.Error(t, err) }) t.Run("wrong signature parameter", func(t *testing.T) { - _, err := ParseDelete([]stackitem.Item{ + _, err := ParseDelete(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewByteArray(containerID), stackitem.NewMap(), - }) + })) require.Error(t, err) }) t.Run("wrong session token parameter", func(t *testing.T) { - _, err := ParseDelete([]stackitem.Item{ + _, err := ParseDelete(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewByteArray(containerID), stackitem.NewByteArray(signature), stackitem.NewMap(), - }) + })) require.Error(t, err) }) t.Run("correct behavior", func(t *testing.T) { - ev, err := ParseDelete([]stackitem.Item{ + ev, err := ParseDelete(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewByteArray(containerID), stackitem.NewByteArray(signature), stackitem.NewByteArray(token), - }) + })) require.NoError(t, err) diff --git a/pkg/morph/event/container/eacl.go b/pkg/morph/event/container/eacl.go index f18145982..cdb9f9bd8 100644 --- a/pkg/morph/event/container/eacl.go +++ b/pkg/morph/event/container/eacl.go @@ -4,7 +4,7 @@ import ( "fmt" "github.com/nspcc-dev/neo-go/pkg/network/payload" - "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" + "github.com/nspcc-dev/neo-go/pkg/rpc/response/result/subscriptions" "github.com/nspcc-dev/neofs-node/pkg/morph/client" "github.com/nspcc-dev/neofs-node/pkg/morph/event" ) @@ -58,36 +58,41 @@ const expectedItemNumEACL = 4 // ParseSetEACL parses SetEACL notification event from list of stack items. // // Expects 4 stack items. -func ParseSetEACL(items []stackitem.Item) (event.Event, error) { +func ParseSetEACL(e *subscriptions.NotificationEvent) (event.Event, error) { var ( ev SetEACL err error ) - if ln := len(items); ln != expectedItemNumEACL { + 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 != expectedItemNumEACL { return nil, event.WrongNumberOfParameters(expectedItemNumEACL, ln) } // parse table - ev.table, err = client.BytesFromStackItem(items[0]) + ev.table, err = client.BytesFromStackItem(params[0]) if err != nil { return nil, fmt.Errorf("could not parse binary table: %w", err) } // parse signature - ev.signature, err = client.BytesFromStackItem(items[1]) + ev.signature, err = client.BytesFromStackItem(params[1]) if err != nil { return nil, fmt.Errorf("could not parse table signature: %w", err) } // parse public key - ev.publicKey, err = client.BytesFromStackItem(items[2]) + ev.publicKey, err = client.BytesFromStackItem(params[2]) if err != nil { return nil, fmt.Errorf("could not parse binary public key: %w", err) } // parse session token - ev.token, err = client.BytesFromStackItem(items[3]) + ev.token, err = client.BytesFromStackItem(params[3]) if err != nil { return nil, fmt.Errorf("could not get session token: %w", err) } diff --git a/pkg/morph/event/container/eacl_test.go b/pkg/morph/event/container/eacl_test.go index d1daadbe9..b521d4293 100644 --- a/pkg/morph/event/container/eacl_test.go +++ b/pkg/morph/event/container/eacl_test.go @@ -1,11 +1,12 @@ -package container_test +package container import ( "testing" + "github.com/nspcc-dev/neo-go/pkg/core/state" + "github.com/nspcc-dev/neo-go/pkg/rpc/response/result/subscriptions" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "github.com/nspcc-dev/neofs-node/pkg/morph/event" - "github.com/nspcc-dev/neofs-node/pkg/morph/event/container" "github.com/stretchr/testify/require" ) @@ -23,60 +24,60 @@ func TestParseEACL(t *testing.T) { stackitem.NewMap(), } - _, err := container.ParseSetEACL(items) + _, err := ParseSetEACL(createNotifyEventFromItems(items)) require.EqualError(t, err, event.WrongNumberOfParameters(4, len(items)).Error()) }) t.Run("wrong container parameter", func(t *testing.T) { - _, err := container.ParseSetEACL([]stackitem.Item{ + _, err := ParseSetEACL(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewMap(), stackitem.NewMap(), stackitem.NewMap(), - }) + })) require.Error(t, err) }) t.Run("wrong signature parameter", func(t *testing.T) { - _, err := container.ParseSetEACL([]stackitem.Item{ + _, err := ParseSetEACL(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewByteArray(binaryTable), stackitem.NewMap(), - }) + })) require.Error(t, err) }) t.Run("wrong key parameter", func(t *testing.T) { - _, err := container.ParseSetEACL([]stackitem.Item{ + _, err := ParseSetEACL(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewByteArray(binaryTable), stackitem.NewByteArray(signature), stackitem.NewMap(), - }) + })) require.Error(t, err) }) t.Run("wrong session token parameter", func(t *testing.T) { - _, err := container.ParseSetEACL([]stackitem.Item{ + _, err := ParseSetEACL(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewByteArray(binaryTable), stackitem.NewByteArray(signature), stackitem.NewByteArray(publicKey), stackitem.NewMap(), - }) + })) require.Error(t, err) }) t.Run("correct behavior", func(t *testing.T) { - ev, err := container.ParseSetEACL([]stackitem.Item{ + ev, err := ParseSetEACL(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewByteArray(binaryTable), stackitem.NewByteArray(signature), stackitem.NewByteArray(publicKey), stackitem.NewByteArray(token), - }) + })) require.NoError(t, err) - e := ev.(container.SetEACL) + e := ev.(SetEACL) require.Equal(t, binaryTable, e.Table()) require.Equal(t, signature, e.Signature()) @@ -84,3 +85,11 @@ func TestParseEACL(t *testing.T) { require.Equal(t, token, e.SessionToken()) }) } + +func createNotifyEventFromItems(items []stackitem.Item) *subscriptions.NotificationEvent { + return &subscriptions.NotificationEvent{ + NotificationEvent: state.NotificationEvent{ + Item: stackitem.NewArray(items), + }, + } +} diff --git a/pkg/morph/event/container/estimates.go b/pkg/morph/event/container/estimates.go index b3e7b0746..a5b27a735 100644 --- a/pkg/morph/event/container/estimates.go +++ b/pkg/morph/event/container/estimates.go @@ -3,6 +3,8 @@ package container import ( "fmt" + "github.com/nspcc-dev/neo-go/pkg/rpc/response/result/subscriptions" + "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "github.com/nspcc-dev/neofs-node/pkg/morph/client" "github.com/nspcc-dev/neofs-node/pkg/morph/event" @@ -33,7 +35,12 @@ func (s StartEstimation) Epoch() uint64 { return s.epoch } func (s StopEstimation) Epoch() uint64 { return s.epoch } // ParseStartEstimation from notification into container event structure. -func ParseStartEstimation(params []stackitem.Item) (event.Event, error) { +func ParseStartEstimation(e *subscriptions.NotificationEvent) (event.Event, error) { + params, err := event.ParseStackArray(e) + if err != nil { + return nil, fmt.Errorf("could not parse stack items from notify event: %w", err) + } + epoch, err := parseEstimation(params) if err != nil { return nil, err @@ -43,7 +50,12 @@ func ParseStartEstimation(params []stackitem.Item) (event.Event, error) { } // ParseStopEstimation from notification into container event structure. -func ParseStopEstimation(params []stackitem.Item) (event.Event, error) { +func ParseStopEstimation(e *subscriptions.NotificationEvent) (event.Event, error) { + params, err := event.ParseStackArray(e) + if err != nil { + return nil, fmt.Errorf("could not parse stack items from notify event: %w", err) + } + epoch, err := parseEstimation(params) if err != nil { return nil, err diff --git a/pkg/morph/event/container/estimates_test.go b/pkg/morph/event/container/estimates_test.go index 5a117bcac..1f7a59672 100644 --- a/pkg/morph/event/container/estimates_test.go +++ b/pkg/morph/event/container/estimates_test.go @@ -19,22 +19,22 @@ func TestStartEstimation(t *testing.T) { stackitem.NewMap(), } - _, err := ParseStartEstimation(prms) + _, err := ParseStartEstimation(createNotifyEventFromItems(prms)) require.EqualError(t, err, event.WrongNumberOfParameters(1, len(prms)).Error()) }) t.Run("wrong estimation parameter", func(t *testing.T) { - _, err := ParseStartEstimation([]stackitem.Item{ + _, err := ParseStartEstimation(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewMap(), - }) + })) require.Error(t, err) }) t.Run("correct behavior", func(t *testing.T) { - ev, err := ParseStartEstimation([]stackitem.Item{ + ev, err := ParseStartEstimation(createNotifyEventFromItems([]stackitem.Item{ epochItem, - }) + })) require.NoError(t, err) @@ -54,22 +54,22 @@ func TestStopEstimation(t *testing.T) { stackitem.NewMap(), } - _, err := ParseStopEstimation(prms) + _, err := ParseStopEstimation(createNotifyEventFromItems(prms)) require.EqualError(t, err, event.WrongNumberOfParameters(1, len(prms)).Error()) }) t.Run("wrong estimation parameter", func(t *testing.T) { - _, err := ParseStopEstimation([]stackitem.Item{ + _, err := ParseStopEstimation(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewMap(), - }) + })) require.Error(t, err) }) t.Run("correct behavior", func(t *testing.T) { - ev, err := ParseStopEstimation([]stackitem.Item{ + ev, err := ParseStopEstimation(createNotifyEventFromItems([]stackitem.Item{ epochItem, - }) + })) require.NoError(t, err) diff --git a/pkg/morph/event/container/put.go b/pkg/morph/event/container/put.go index 9cfdf4557..21b3ef56b 100644 --- a/pkg/morph/event/container/put.go +++ b/pkg/morph/event/container/put.go @@ -3,8 +3,9 @@ package container import ( "fmt" + "github.com/nspcc-dev/neo-go/pkg/rpc/response/result/subscriptions" + "github.com/nspcc-dev/neo-go/pkg/network/payload" - "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "github.com/nspcc-dev/neofs-node/pkg/morph/client" "github.com/nspcc-dev/neofs-node/pkg/morph/event" ) @@ -65,12 +66,17 @@ func (x PutNamed) Zone() string { } // ParsePut from notification into container event structure. -func ParsePut(params []stackitem.Item) (event.Event, error) { +func ParsePut(e *subscriptions.NotificationEvent) (event.Event, error) { var ( ev Put 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 != expectedItemNumPut { return nil, event.WrongNumberOfParameters(expectedItemNumPut, ln) } diff --git a/pkg/morph/event/container/put_test.go b/pkg/morph/event/container/put_test.go index c2d9184d7..e53bfd916 100644 --- a/pkg/morph/event/container/put_test.go +++ b/pkg/morph/event/container/put_test.go @@ -22,55 +22,55 @@ func TestParsePut(t *testing.T) { stackitem.NewMap(), } - _, err := ParsePut(prms) + _, err := ParsePut(createNotifyEventFromItems(prms)) require.EqualError(t, err, event.WrongNumberOfParameters(expectedItemNumPut, len(prms)).Error()) }) t.Run("wrong container parameter", func(t *testing.T) { - _, err := ParsePut([]stackitem.Item{ + _, err := ParsePut(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewMap(), - }) + })) require.Error(t, err) }) t.Run("wrong signature parameter", func(t *testing.T) { - _, err := ParsePut([]stackitem.Item{ + _, err := ParsePut(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewByteArray(containerData), stackitem.NewMap(), - }) + })) require.Error(t, err) }) t.Run("wrong key parameter", func(t *testing.T) { - _, err := ParsePut([]stackitem.Item{ + _, err := ParsePut(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewByteArray(containerData), stackitem.NewByteArray(signature), stackitem.NewMap(), - }) + })) require.Error(t, err) }) t.Run("wrong session token parameter", func(t *testing.T) { - _, err := ParsePut([]stackitem.Item{ + _, err := ParsePut(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewByteArray(containerData), stackitem.NewByteArray(signature), stackitem.NewByteArray(publicKey), stackitem.NewMap(), - }) + })) require.Error(t, err) }) t.Run("correct behavior", func(t *testing.T) { - ev, err := ParsePut([]stackitem.Item{ + ev, err := ParsePut(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewByteArray(containerData), stackitem.NewByteArray(signature), stackitem.NewByteArray(publicKey), stackitem.NewByteArray(token), - }) + })) require.NoError(t, err) require.Equal(t, Put{ diff --git a/pkg/morph/event/listener.go b/pkg/morph/event/listener.go index dfa65e2f0..a21642746 100644 --- a/pkg/morph/event/listener.go +++ b/pkg/morph/event/listener.go @@ -285,19 +285,6 @@ func (l listener) parseAndHandleNotification(notifyEvent *subscriptions.Notifica zap.String("script hash LE", notifyEvent.ScriptHash.StringLE()), ) - // stack item must be an array of items - arr, err := client.ArrayFromStackItem(notifyEvent.Item) - if err != nil { - log.Warn("stack item is not an array type", - zap.String("error", err.Error()), - ) - - return - } else if len(arr) == 0 { - log.Warn("stack item array is empty") - return - } - // calculate event type from bytes typEvent := TypeFromString(notifyEvent.Name) @@ -321,7 +308,7 @@ func (l listener) parseAndHandleNotification(notifyEvent *subscriptions.Notifica } // parse the notification event - event, err := parser(arr) + event, err := parser(notifyEvent) if err != nil { log.Warn("could not parse notification event", zap.String("error", err.Error()), diff --git a/pkg/morph/event/neofs/bind.go b/pkg/morph/event/neofs/bind.go index a516e0d9d..c5c4674e7 100644 --- a/pkg/morph/event/neofs/bind.go +++ b/pkg/morph/event/neofs/bind.go @@ -3,6 +3,7 @@ package neofs import ( "fmt" + "github.com/nspcc-dev/neo-go/pkg/rpc/response/result/subscriptions" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "github.com/nspcc-dev/neofs-node/pkg/morph/client" "github.com/nspcc-dev/neofs-node/pkg/morph/event" @@ -24,12 +25,17 @@ func (b bindCommon) Keys() [][]byte { return b.keys } func (b bindCommon) User() []byte { return b.user } -func ParseBind(params []stackitem.Item) (event.Event, error) { +func ParseBind(e *subscriptions.NotificationEvent) (event.Event, error) { var ( ev Bind err error ) + params, err := event.ParseStackArray(e) + if err != nil { + return nil, fmt.Errorf("could not parse stack items from notify event: %w", err) + } + err = parseBind(&ev.bindCommon, params) if err != nil { return nil, err diff --git a/pkg/morph/event/neofs/bind_test.go b/pkg/morph/event/neofs/bind_test.go index 7fd1d9b28..c7a628833 100644 --- a/pkg/morph/event/neofs/bind_test.go +++ b/pkg/morph/event/neofs/bind_test.go @@ -3,6 +3,8 @@ package neofs import ( "testing" + "github.com/nspcc-dev/neo-go/pkg/core/state" + "github.com/nspcc-dev/neo-go/pkg/rpc/response/result/subscriptions" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "github.com/nspcc-dev/neofs-node/pkg/morph/event" "github.com/stretchr/testify/require" @@ -23,36 +25,36 @@ func TestParseBind(t *testing.T) { stackitem.NewMap(), } - _, err := ParseBind(prms) + _, err := ParseBind(createNotifyEventFromItems(prms)) require.EqualError(t, err, event.WrongNumberOfParameters(2, len(prms)).Error()) }) t.Run("wrong first parameter", func(t *testing.T) { - _, err := ParseBind([]stackitem.Item{ + _, err := ParseBind(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewMap(), - }) + })) require.Error(t, err) }) t.Run("wrong second parameter", func(t *testing.T) { - _, err := ParseBind([]stackitem.Item{ + _, err := ParseBind(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewByteArray(user), stackitem.NewMap(), - }) + })) require.Error(t, err) }) t.Run("correct", func(t *testing.T) { - ev, err := ParseBind([]stackitem.Item{ + ev, err := ParseBind(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewByteArray(user), stackitem.NewArray([]stackitem.Item{ stackitem.NewByteArray(publicKeys[0]), stackitem.NewByteArray(publicKeys[1]), stackitem.NewByteArray(publicKeys[2]), }), - }) + })) require.NoError(t, err) e := ev.(Bind) @@ -61,3 +63,11 @@ func TestParseBind(t *testing.T) { require.Equal(t, publicKeys, e.Keys()) }) } + +func createNotifyEventFromItems(items []stackitem.Item) *subscriptions.NotificationEvent { + return &subscriptions.NotificationEvent{ + NotificationEvent: state.NotificationEvent{ + Item: stackitem.NewArray(items), + }, + } +} diff --git a/pkg/morph/event/neofs/cheque.go b/pkg/morph/event/neofs/cheque.go index c289303ae..8c09fba57 100644 --- a/pkg/morph/event/neofs/cheque.go +++ b/pkg/morph/event/neofs/cheque.go @@ -3,8 +3,8 @@ package neofs 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/neo-go/pkg/vm/stackitem" "github.com/nspcc-dev/neofs-node/pkg/morph/client" "github.com/nspcc-dev/neofs-node/pkg/morph/event" ) @@ -33,12 +33,17 @@ func (c Cheque) Amount() int64 { return c.amount } func (c Cheque) LockAccount() util.Uint160 { return c.lock } // ParseCheque from notification into cheque structure. -func ParseCheque(params []stackitem.Item) (event.Event, error) { +func ParseCheque(e *subscriptions.NotificationEvent) (event.Event, error) { var ( ev Cheque 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 != 4 { return nil, event.WrongNumberOfParameters(4, ln) } diff --git a/pkg/morph/event/neofs/cheque_test.go b/pkg/morph/event/neofs/cheque_test.go index 915cc6f8a..744844682 100644 --- a/pkg/morph/event/neofs/cheque_test.go +++ b/pkg/morph/event/neofs/cheque_test.go @@ -25,55 +25,55 @@ func TestParseCheque(t *testing.T) { stackitem.NewMap(), } - _, err := ParseCheque(prms) + _, err := ParseCheque(createNotifyEventFromItems(prms)) require.EqualError(t, err, event.WrongNumberOfParameters(4, len(prms)).Error()) }) t.Run("wrong id parameter", func(t *testing.T) { - _, err := ParseCheque([]stackitem.Item{ + _, err := ParseCheque(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewMap(), - }) + })) require.Error(t, err) }) t.Run("wrong user parameter", func(t *testing.T) { - _, err := ParseCheque([]stackitem.Item{ + _, err := ParseCheque(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewByteArray(id), stackitem.NewMap(), - }) + })) require.Error(t, err) }) t.Run("wrong amount parameter", func(t *testing.T) { - _, err := ParseCheque([]stackitem.Item{ + _, err := ParseCheque(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewByteArray(id), stackitem.NewByteArray(user.BytesBE()), stackitem.NewMap(), - }) + })) require.Error(t, err) }) t.Run("wrong lock parameter", func(t *testing.T) { - _, err := ParseCheque([]stackitem.Item{ + _, err := ParseCheque(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewByteArray(id), stackitem.NewByteArray(user.BytesBE()), stackitem.NewBigInteger(new(big.Int).SetInt64(amount)), stackitem.NewMap(), - }) + })) require.Error(t, err) }) t.Run("correct behavior", func(t *testing.T) { - ev, err := ParseCheque([]stackitem.Item{ + ev, err := ParseCheque(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewByteArray(id), stackitem.NewByteArray(user.BytesBE()), stackitem.NewBigInteger(new(big.Int).SetInt64(amount)), stackitem.NewByteArray(lock.BytesBE()), - }) + })) require.NoError(t, err) require.Equal(t, Cheque{ diff --git a/pkg/morph/event/neofs/config.go b/pkg/morph/event/neofs/config.go index 6c2fe4eb4..01efc955e 100644 --- a/pkg/morph/event/neofs/config.go +++ b/pkg/morph/event/neofs/config.go @@ -3,7 +3,7 @@ package neofs import ( "fmt" - "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" + "github.com/nspcc-dev/neo-go/pkg/rpc/response/result/subscriptions" "github.com/nspcc-dev/neofs-node/pkg/morph/client" "github.com/nspcc-dev/neofs-node/pkg/morph/event" ) @@ -23,12 +23,17 @@ func (u Config) Key() []byte { return u.key } func (u Config) Value() []byte { return u.value } -func ParseConfig(params []stackitem.Item) (event.Event, error) { +func ParseConfig(e *subscriptions.NotificationEvent) (event.Event, error) { var ( ev Config 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 != 3 { return nil, event.WrongNumberOfParameters(3, ln) } diff --git a/pkg/morph/event/neofs/config_test.go b/pkg/morph/event/neofs/config_test.go index fbe060fa7..b6a3222e8 100644 --- a/pkg/morph/event/neofs/config_test.go +++ b/pkg/morph/event/neofs/config_test.go @@ -20,43 +20,43 @@ func TestParseConfig(t *testing.T) { stackitem.NewMap(), } - _, err := ParseConfig(prms) + _, err := ParseConfig(createNotifyEventFromItems(prms)) require.EqualError(t, err, event.WrongNumberOfParameters(3, len(prms)).Error()) }) t.Run("wrong first parameter", func(t *testing.T) { - _, err := ParseConfig([]stackitem.Item{ + _, err := ParseConfig(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewMap(), - }) + })) require.Error(t, err) }) t.Run("wrong second parameter", func(t *testing.T) { - _, err := ParseConfig([]stackitem.Item{ + _, err := ParseConfig(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewByteArray(id), stackitem.NewMap(), - }) + })) require.Error(t, err) }) t.Run("wrong third parameter", func(t *testing.T) { - _, err := ParseConfig([]stackitem.Item{ + _, err := ParseConfig(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewByteArray(id), stackitem.NewByteArray(key), stackitem.NewMap(), - }) + })) require.Error(t, err) }) t.Run("correct", func(t *testing.T) { - ev, err := ParseConfig([]stackitem.Item{ + ev, err := ParseConfig(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewByteArray(id), stackitem.NewByteArray(key), stackitem.NewByteArray(value), - }) + })) require.NoError(t, err) require.Equal(t, Config{ diff --git a/pkg/morph/event/neofs/deposit.go b/pkg/morph/event/neofs/deposit.go index 6019f7f28..2485a01cd 100644 --- a/pkg/morph/event/neofs/deposit.go +++ b/pkg/morph/event/neofs/deposit.go @@ -3,8 +3,8 @@ package neofs 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/neo-go/pkg/vm/stackitem" "github.com/nspcc-dev/neofs-node/pkg/morph/client" "github.com/nspcc-dev/neofs-node/pkg/morph/event" ) @@ -33,9 +33,14 @@ func (d Deposit) To() util.Uint160 { return d.to } func (d Deposit) Amount() int64 { return d.amount } // ParseDeposit notification into deposit structure. -func ParseDeposit(params []stackitem.Item) (event.Event, error) { +func ParseDeposit(e *subscriptions.NotificationEvent) (event.Event, error) { var ev Deposit + 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 != 4 { return nil, event.WrongNumberOfParameters(4, ln) } diff --git a/pkg/morph/event/neofs/deposit_test.go b/pkg/morph/event/neofs/deposit_test.go index 80de2cc41..dfd455828 100644 --- a/pkg/morph/event/neofs/deposit_test.go +++ b/pkg/morph/event/neofs/deposit_test.go @@ -25,55 +25,55 @@ func TestParseDeposit(t *testing.T) { stackitem.NewMap(), } - _, err := ParseDeposit(prms) + _, err := ParseDeposit(createNotifyEventFromItems(prms)) require.EqualError(t, err, event.WrongNumberOfParameters(4, len(prms)).Error()) }) t.Run("wrong from parameter", func(t *testing.T) { - _, err := ParseDeposit([]stackitem.Item{ + _, err := ParseDeposit(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewMap(), - }) + })) require.Error(t, err) }) t.Run("wrong amount parameter", func(t *testing.T) { - _, err := ParseDeposit([]stackitem.Item{ + _, err := ParseDeposit(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewByteArray(from.BytesBE()), stackitem.NewMap(), - }) + })) require.Error(t, err) }) t.Run("wrong to parameter", func(t *testing.T) { - _, err := ParseDeposit([]stackitem.Item{ + _, err := ParseDeposit(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewByteArray(from.BytesBE()), stackitem.NewBigInteger(new(big.Int).SetInt64(amount)), stackitem.NewMap(), - }) + })) require.Error(t, err) }) t.Run("wrong id parameter", func(t *testing.T) { - _, err := ParseDeposit([]stackitem.Item{ + _, err := ParseDeposit(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewByteArray(from.BytesBE()), stackitem.NewBigInteger(new(big.Int).SetInt64(amount)), stackitem.NewByteArray(to.BytesBE()), stackitem.NewMap(), - }) + })) require.Error(t, err) }) t.Run("correct behavior", func(t *testing.T) { - ev, err := ParseDeposit([]stackitem.Item{ + ev, err := ParseDeposit(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewByteArray(from.BytesBE()), stackitem.NewBigInteger(new(big.Int).SetInt64(amount)), stackitem.NewByteArray(to.BytesBE()), stackitem.NewByteArray(id), - }) + })) require.NoError(t, err) require.Equal(t, Deposit{ diff --git a/pkg/morph/event/neofs/unbind.go b/pkg/morph/event/neofs/unbind.go index 2b07465b8..8f7bb6c19 100644 --- a/pkg/morph/event/neofs/unbind.go +++ b/pkg/morph/event/neofs/unbind.go @@ -1,7 +1,9 @@ package neofs import ( - "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" + "fmt" + + "github.com/nspcc-dev/neo-go/pkg/rpc/response/result/subscriptions" "github.com/nspcc-dev/neofs-node/pkg/morph/event" ) @@ -9,12 +11,17 @@ type Unbind struct { bindCommon } -func ParseUnbind(params []stackitem.Item) (event.Event, error) { +func ParseUnbind(e *subscriptions.NotificationEvent) (event.Event, error) { var ( ev Unbind err error ) + params, err := event.ParseStackArray(e) + if err != nil { + return nil, fmt.Errorf("could not parse stack items from notify event: %w", err) + } + err = parseBind(&ev.bindCommon, params) if err != nil { return nil, err diff --git a/pkg/morph/event/neofs/unbind_test.go b/pkg/morph/event/neofs/unbind_test.go index ebd0bec21..b2cd93e06 100644 --- a/pkg/morph/event/neofs/unbind_test.go +++ b/pkg/morph/event/neofs/unbind_test.go @@ -23,36 +23,36 @@ func TestParseUnbind(t *testing.T) { stackitem.NewMap(), } - _, err := ParseUnbind(prms) + _, err := ParseUnbind(createNotifyEventFromItems(prms)) require.EqualError(t, err, event.WrongNumberOfParameters(2, len(prms)).Error()) }) t.Run("wrong first parameter", func(t *testing.T) { - _, err := ParseUnbind([]stackitem.Item{ + _, err := ParseUnbind(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewMap(), - }) + })) require.Error(t, err) }) t.Run("wrong second parameter", func(t *testing.T) { - _, err := ParseUnbind([]stackitem.Item{ + _, err := ParseUnbind(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewByteArray(user), stackitem.NewMap(), - }) + })) require.Error(t, err) }) t.Run("correct", func(t *testing.T) { - ev, err := ParseUnbind([]stackitem.Item{ + ev, err := ParseUnbind(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewByteArray(user), stackitem.NewArray([]stackitem.Item{ stackitem.NewByteArray(publicKeys[0]), stackitem.NewByteArray(publicKeys[1]), stackitem.NewByteArray(publicKeys[2]), }), - }) + })) require.NoError(t, err) e := ev.(Unbind) diff --git a/pkg/morph/event/neofs/withdraw.go b/pkg/morph/event/neofs/withdraw.go index 2c0c45929..781f74e66 100644 --- a/pkg/morph/event/neofs/withdraw.go +++ b/pkg/morph/event/neofs/withdraw.go @@ -3,8 +3,8 @@ package neofs 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/neo-go/pkg/vm/stackitem" "github.com/nspcc-dev/neofs-node/pkg/morph/client" "github.com/nspcc-dev/neofs-node/pkg/morph/event" ) @@ -29,9 +29,14 @@ func (w Withdraw) User() util.Uint160 { return w.user } func (w Withdraw) Amount() int64 { return w.amount } // ParseWithdraw notification into withdraw structure. -func ParseWithdraw(params []stackitem.Item) (event.Event, error) { +func ParseWithdraw(e *subscriptions.NotificationEvent) (event.Event, error) { var ev Withdraw + 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 != 3 { return nil, event.WrongNumberOfParameters(3, ln) } diff --git a/pkg/morph/event/neofs/withdraw_test.go b/pkg/morph/event/neofs/withdraw_test.go index 798ef77d5..d251d9559 100644 --- a/pkg/morph/event/neofs/withdraw_test.go +++ b/pkg/morph/event/neofs/withdraw_test.go @@ -24,43 +24,43 @@ func TestParseWithdraw(t *testing.T) { stackitem.NewMap(), } - _, err := ParseWithdraw(prms) + _, err := ParseWithdraw(createNotifyEventFromItems(prms)) require.EqualError(t, err, event.WrongNumberOfParameters(3, len(prms)).Error()) }) t.Run("wrong user parameter", func(t *testing.T) { - _, err := ParseWithdraw([]stackitem.Item{ + _, err := ParseWithdraw(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewMap(), - }) + })) require.Error(t, err) }) t.Run("wrong amount parameter", func(t *testing.T) { - _, err := ParseWithdraw([]stackitem.Item{ + _, err := ParseWithdraw(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewByteArray(user.BytesBE()), stackitem.NewMap(), - }) + })) require.Error(t, err) }) t.Run("wrong id parameter", func(t *testing.T) { - _, err := ParseWithdraw([]stackitem.Item{ + _, err := ParseWithdraw(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewByteArray(user.BytesBE()), stackitem.NewBigInteger(new(big.Int).SetInt64(amount)), stackitem.NewMap(), - }) + })) require.Error(t, err) }) t.Run("correct behavior", func(t *testing.T) { - ev, err := ParseWithdraw([]stackitem.Item{ + ev, err := ParseWithdraw(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewByteArray(user.BytesBE()), stackitem.NewBigInteger(new(big.Int).SetInt64(amount)), stackitem.NewByteArray(id), - }) + })) require.NoError(t, err) require.Equal(t, Withdraw{ diff --git a/pkg/morph/event/netmap/add_peer.go b/pkg/morph/event/netmap/add_peer.go index 1d013b34f..aa0014e14 100644 --- a/pkg/morph/event/netmap/add_peer.go +++ b/pkg/morph/event/netmap/add_peer.go @@ -4,7 +4,7 @@ import ( "fmt" "github.com/nspcc-dev/neo-go/pkg/network/payload" - "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" + "github.com/nspcc-dev/neo-go/pkg/rpc/response/result/subscriptions" "github.com/nspcc-dev/neofs-node/pkg/morph/client" "github.com/nspcc-dev/neofs-node/pkg/morph/event" ) @@ -32,17 +32,22 @@ func (s AddPeer) NotaryRequest() *payload.P2PNotaryRequest { const expectedItemNumAddPeer = 1 -func ParseAddPeer(prms []stackitem.Item) (event.Event, error) { +func ParseAddPeer(e *subscriptions.NotificationEvent) (event.Event, error) { var ( ev AddPeer err error ) - if ln := len(prms); ln != expectedItemNumAddPeer { + 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 != expectedItemNumAddPeer { return nil, event.WrongNumberOfParameters(expectedItemNumAddPeer, ln) } - ev.node, err = client.BytesFromStackItem(prms[0]) + ev.node, err = client.BytesFromStackItem(params[0]) if err != nil { return nil, fmt.Errorf("could not get raw nodeinfo: %w", err) } diff --git a/pkg/morph/event/netmap/add_peer_test.go b/pkg/morph/event/netmap/add_peer_test.go index ae74e19dd..ae231ecf0 100644 --- a/pkg/morph/event/netmap/add_peer_test.go +++ b/pkg/morph/event/netmap/add_peer_test.go @@ -3,6 +3,8 @@ package netmap import ( "testing" + "github.com/nspcc-dev/neo-go/pkg/core/state" + "github.com/nspcc-dev/neo-go/pkg/rpc/response/result/subscriptions" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "github.com/nspcc-dev/neofs-node/pkg/morph/event" "github.com/stretchr/testify/require" @@ -15,14 +17,14 @@ func TestParseAddPeer(t *testing.T) { stackitem.NewMap(), } - _, err := ParseAddPeer(prms) + _, err := ParseAddPeer(createNotifyEventFromItems(prms)) require.EqualError(t, err, event.WrongNumberOfParameters(1, len(prms)).Error()) }) t.Run("wrong first parameter type", func(t *testing.T) { - _, err := ParseAddPeer([]stackitem.Item{ + _, err := ParseAddPeer(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewMap(), - }) + })) require.Error(t, err) }) @@ -30,9 +32,9 @@ func TestParseAddPeer(t *testing.T) { t.Run("correct behavior", func(t *testing.T) { info := []byte{1, 2, 3} - ev, err := ParseAddPeer([]stackitem.Item{ + ev, err := ParseAddPeer(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewByteArray(info), - }) + })) require.NoError(t, err) require.Equal(t, AddPeer{ @@ -40,3 +42,11 @@ func TestParseAddPeer(t *testing.T) { }, ev) }) } + +func createNotifyEventFromItems(items []stackitem.Item) *subscriptions.NotificationEvent { + return &subscriptions.NotificationEvent{ + NotificationEvent: state.NotificationEvent{ + Item: stackitem.NewArray(items), + }, + } +} diff --git a/pkg/morph/event/netmap/epoch.go b/pkg/morph/event/netmap/epoch.go index 406aba806..371a47ec6 100644 --- a/pkg/morph/event/netmap/epoch.go +++ b/pkg/morph/event/netmap/epoch.go @@ -3,7 +3,7 @@ package netmap import ( "fmt" - "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" + "github.com/nspcc-dev/neo-go/pkg/rpc/response/result/subscriptions" "github.com/nspcc-dev/neofs-node/pkg/morph/client" "github.com/nspcc-dev/neofs-node/pkg/morph/event" ) @@ -24,12 +24,17 @@ func (s NewEpoch) EpochNumber() uint64 { // ParseNewEpoch is a parser of new epoch notification event. // // Result is type of NewEpoch. -func ParseNewEpoch(prms []stackitem.Item) (event.Event, error) { - if ln := len(prms); ln != 1 { +func ParseNewEpoch(e *subscriptions.NotificationEvent) (event.Event, 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 != 1 { return nil, event.WrongNumberOfParameters(1, ln) } - prmEpochNum, err := client.IntFromStackItem(prms[0]) + prmEpochNum, err := client.IntFromStackItem(params[0]) if err != nil { return nil, fmt.Errorf("could not get integer epoch number: %w", err) } diff --git a/pkg/morph/event/netmap/epoch_test.go b/pkg/morph/event/netmap/epoch_test.go index a3d9780ea..bf7ea66d5 100644 --- a/pkg/morph/event/netmap/epoch_test.go +++ b/pkg/morph/event/netmap/epoch_test.go @@ -16,14 +16,14 @@ func TestParseNewEpoch(t *testing.T) { stackitem.NewMap(), } - _, err := ParseNewEpoch(prms) + _, err := ParseNewEpoch(createNotifyEventFromItems(prms)) require.EqualError(t, err, event.WrongNumberOfParameters(1, len(prms)).Error()) }) t.Run("wrong first parameter type", func(t *testing.T) { - _, err := ParseNewEpoch([]stackitem.Item{ + _, err := ParseNewEpoch(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewMap(), - }) + })) require.Error(t, err) }) @@ -31,9 +31,9 @@ func TestParseNewEpoch(t *testing.T) { t.Run("correct behavior", func(t *testing.T) { epochNum := uint64(100) - ev, err := ParseNewEpoch([]stackitem.Item{ + ev, err := ParseNewEpoch(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewBigInteger(new(big.Int).SetUint64(epochNum)), - }) + })) require.NoError(t, err) require.Equal(t, NewEpoch{ diff --git a/pkg/morph/event/netmap/update_peer.go b/pkg/morph/event/netmap/update_peer.go index baf35c592..7d0897d78 100644 --- a/pkg/morph/event/netmap/update_peer.go +++ b/pkg/morph/event/netmap/update_peer.go @@ -4,9 +4,11 @@ import ( "crypto/elliptic" "fmt" + "github.com/nspcc-dev/neo-go/pkg/rpc/response/result/subscriptions" + "github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/network/payload" - "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" + "github.com/nspcc-dev/neofs-api-go/pkg/netmap" v2netmap "github.com/nspcc-dev/neofs-api-go/v2/netmap" "github.com/nspcc-dev/neofs-node/pkg/morph/client" "github.com/nspcc-dev/neofs-node/pkg/morph/event" @@ -41,18 +43,23 @@ func (s UpdatePeer) NotaryRequest() *payload.P2PNotaryRequest { const expectedItemNumUpdatePeer = 2 -func ParseUpdatePeer(prms []stackitem.Item) (event.Event, error) { +func ParseUpdatePeer(e *subscriptions.NotificationEvent) (event.Event, error) { var ( ev UpdatePeer err error ) - if ln := len(prms); ln != expectedItemNumUpdatePeer { + 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 != expectedItemNumUpdatePeer { return nil, event.WrongNumberOfParameters(expectedItemNumUpdatePeer, ln) } // parse public key - key, err := client.BytesFromStackItem(prms[1]) + key, err := client.BytesFromStackItem(params[1]) if err != nil { return nil, fmt.Errorf("could not get public key: %w", err) } @@ -63,7 +70,7 @@ func ParseUpdatePeer(prms []stackitem.Item) (event.Event, error) { } // parse node status - st, err := client.IntFromStackItem(prms[0]) + st, err := client.IntFromStackItem(params[0]) if err != nil { return nil, fmt.Errorf("could not get node status: %w", err) } diff --git a/pkg/morph/event/netmap/update_peer_test.go b/pkg/morph/event/netmap/update_peer_test.go index dd5052a73..0ee4072f9 100644 --- a/pkg/morph/event/netmap/update_peer_test.go +++ b/pkg/morph/event/netmap/update_peer_test.go @@ -25,32 +25,32 @@ func TestParseUpdatePeer(t *testing.T) { stackitem.NewMap(), } - _, err := ParseUpdatePeer(prms) + _, err := ParseUpdatePeer(createNotifyEventFromItems(prms)) require.EqualError(t, err, event.WrongNumberOfParameters(2, len(prms)).Error()) }) t.Run("wrong first parameter type", func(t *testing.T) { - _, err := ParseUpdatePeer([]stackitem.Item{ + _, err := ParseUpdatePeer(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewMap(), - }) + })) require.Error(t, err) }) t.Run("wrong second parameter type", func(t *testing.T) { - _, err := ParseUpdatePeer([]stackitem.Item{ + _, err := ParseUpdatePeer(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewByteArray(publicKey.Bytes()), stackitem.NewMap(), - }) + })) require.Error(t, err) }) t.Run("correct behavior", func(t *testing.T) { - ev, err := ParseUpdatePeer([]stackitem.Item{ + ev, err := ParseUpdatePeer(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewBigInteger(new(big.Int).SetInt64(int64(state.ToV2()))), stackitem.NewByteArray(publicKey.Bytes()), - }) + })) require.NoError(t, err) require.Equal(t, UpdatePeer{ diff --git a/pkg/morph/event/parsers.go b/pkg/morph/event/parsers.go index 4670c5a47..37b57f467 100644 --- a/pkg/morph/event/parsers.go +++ b/pkg/morph/event/parsers.go @@ -4,12 +4,12 @@ import ( "fmt" "github.com/nspcc-dev/neo-go/pkg/network/payload" - "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" + "github.com/nspcc-dev/neo-go/pkg/rpc/response/result/subscriptions" ) // NotificationParser is a function that constructs Event // from the StackItem list. -type NotificationParser func([]stackitem.Item) (Event, error) +type NotificationParser func(*subscriptions.NotificationEvent) (Event, error) // NotificationParserInfo is a structure that groups // the parameters of particular contract diff --git a/pkg/morph/event/reputation/put.go b/pkg/morph/event/reputation/put.go index 9d31c12ea..3e0e21d2a 100644 --- a/pkg/morph/event/reputation/put.go +++ b/pkg/morph/event/reputation/put.go @@ -4,7 +4,7 @@ import ( "fmt" "github.com/nspcc-dev/neo-go/pkg/network/payload" - "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" + "github.com/nspcc-dev/neo-go/pkg/rpc/response/result/subscriptions" "github.com/nspcc-dev/neofs-node/pkg/morph/client" "github.com/nspcc-dev/neofs-node/pkg/morph/event" "github.com/nspcc-dev/neofs-sdk-go/reputation" @@ -49,18 +49,23 @@ func (p Put) NotaryRequest() *payload.P2PNotaryRequest { } // ParsePut from notification into reputation event structure. -func ParsePut(prms []stackitem.Item) (event.Event, error) { +func ParsePut(e *subscriptions.NotificationEvent) (event.Event, error) { var ( ev Put err error ) - if ln := len(prms); ln != 3 { + 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 != 3 { return nil, event.WrongNumberOfParameters(3, ln) } // parse epoch number - epoch, err := client.IntFromStackItem(prms[0]) + epoch, err := client.IntFromStackItem(params[0]) if err != nil { return nil, fmt.Errorf("could not get integer epoch number: %w", err) } @@ -68,7 +73,7 @@ func ParsePut(prms []stackitem.Item) (event.Event, error) { ev.epoch = uint64(epoch) // parse peer ID value - peerID, err := client.BytesFromStackItem(prms[1]) + peerID, err := client.BytesFromStackItem(params[1]) if err != nil { return nil, fmt.Errorf("could not get peer ID value: %w", err) } @@ -82,7 +87,7 @@ func ParsePut(prms []stackitem.Item) (event.Event, error) { ev.peerID.SetPublicKey(publicKey) // parse global trust value - rawValue, err := client.BytesFromStackItem(prms[2]) + rawValue, err := client.BytesFromStackItem(params[2]) if err != nil { return nil, fmt.Errorf("could not get global trust value: %w", err) } diff --git a/pkg/morph/event/reputation/put_test.go b/pkg/morph/event/reputation/put_test.go index e9ed967ee..0d53df22d 100644 --- a/pkg/morph/event/reputation/put_test.go +++ b/pkg/morph/event/reputation/put_test.go @@ -4,6 +4,8 @@ import ( "math/big" "testing" + "github.com/nspcc-dev/neo-go/pkg/core/state" + "github.com/nspcc-dev/neo-go/pkg/rpc/response/result/subscriptions" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "github.com/nspcc-dev/neofs-node/pkg/morph/event" "github.com/nspcc-dev/neofs-sdk-go/reputation" @@ -39,43 +41,43 @@ func TestParsePut(t *testing.T) { stackitem.NewMap(), } - _, err := ParsePut(prms) + _, err := ParsePut(createNotifyEventFromItems(prms)) require.EqualError(t, err, event.WrongNumberOfParameters(3, len(prms)).Error()) }) t.Run("wrong epoch parameter", func(t *testing.T) { - _, err := ParsePut([]stackitem.Item{ + _, err := ParsePut(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewMap(), - }) + })) require.Error(t, err) }) t.Run("wrong peerID parameter", func(t *testing.T) { - _, err := ParsePut([]stackitem.Item{ + _, err := ParsePut(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewBigInteger(new(big.Int).SetUint64(epoch)), stackitem.NewMap(), - }) + })) require.Error(t, err) }) t.Run("wrong value parameter", func(t *testing.T) { - _, err := ParsePut([]stackitem.Item{ + _, err := ParsePut(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewBigInteger(new(big.Int).SetUint64(epoch)), stackitem.NewByteArray(rawPeerID[:]), stackitem.NewMap(), - }) + })) require.Error(t, err) }) t.Run("correct behavior", func(t *testing.T) { - ev, err := ParsePut([]stackitem.Item{ + ev, err := ParsePut(createNotifyEventFromItems([]stackitem.Item{ stackitem.NewBigInteger(new(big.Int).SetUint64(epoch)), stackitem.NewByteArray(rawPeerID[:]), stackitem.NewByteArray(rawValue), - }) + })) require.NoError(t, err) require.Equal(t, Put{ @@ -85,3 +87,11 @@ func TestParsePut(t *testing.T) { }, ev) }) } + +func createNotifyEventFromItems(items []stackitem.Item) *subscriptions.NotificationEvent { + return &subscriptions.NotificationEvent{ + NotificationEvent: state.NotificationEvent{ + Item: stackitem.NewArray(items), + }, + } +} diff --git a/pkg/morph/event/rolemanagement/designate.go b/pkg/morph/event/rolemanagement/designate.go index 700f224fe..1a0a1e304 100644 --- a/pkg/morph/event/rolemanagement/designate.go +++ b/pkg/morph/event/rolemanagement/designate.go @@ -4,7 +4,7 @@ import ( "fmt" "github.com/nspcc-dev/neo-go/pkg/core/native/noderoles" - "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" + "github.com/nspcc-dev/neo-go/pkg/rpc/response/result/subscriptions" "github.com/nspcc-dev/neofs-node/pkg/morph/event" ) @@ -17,7 +17,12 @@ type Designate struct { func (Designate) MorphEvent() {} // ParseDesignate from notification into container event structure. -func ParseDesignate(params []stackitem.Item) (event.Event, error) { +func ParseDesignate(e *subscriptions.NotificationEvent) (event.Event, error) { + params, err := event.ParseStackArray(e) + if err != nil { + return nil, fmt.Errorf("could not parse stack items from notify event: %w", err) + } + if len(params) != 2 { return nil, event.WrongNumberOfParameters(2, len(params)) } diff --git a/pkg/morph/event/rolemanagement/designate_test.go b/pkg/morph/event/rolemanagement/designate_test.go index 8c1dc014e..1c9ca7f5c 100644 --- a/pkg/morph/event/rolemanagement/designate_test.go +++ b/pkg/morph/event/rolemanagement/designate_test.go @@ -4,24 +4,34 @@ import ( "testing" "github.com/nspcc-dev/neo-go/pkg/core/native/noderoles" + "github.com/nspcc-dev/neo-go/pkg/core/state" + "github.com/nspcc-dev/neo-go/pkg/rpc/response/result/subscriptions" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" "github.com/stretchr/testify/require" ) func TestParseRoleUpdate(t *testing.T) { t.Run("wrong number of arguments", func(t *testing.T) { - _, err := ParseDesignate([]stackitem.Item{}) + _, err := ParseDesignate(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 := ParseDesignate(args) + _, err := ParseDesignate(createNotifyEventFromItems(args)) require.Error(t, err) }) t.Run("good", func(t *testing.T) { args := []stackitem.Item{stackitem.Make(int(noderoles.NeoFSAlphabet)), stackitem.Make(123)} - e, err := ParseDesignate(args) + e, err := ParseDesignate(createNotifyEventFromItems(args)) require.NoError(t, err) require.Equal(t, noderoles.NeoFSAlphabet, e.(Designate).Role) }) } + +func createNotifyEventFromItems(items []stackitem.Item) *subscriptions.NotificationEvent { + return &subscriptions.NotificationEvent{ + NotificationEvent: state.NotificationEvent{ + Item: stackitem.NewArray(items), + }, + } +} diff --git a/pkg/morph/event/utils.go b/pkg/morph/event/utils.go index 31415319a..f85d9598f 100644 --- a/pkg/morph/event/utils.go +++ b/pkg/morph/event/utils.go @@ -1,8 +1,14 @@ package event import ( + "errors" + "fmt" + "github.com/nspcc-dev/neo-go/pkg/core/mempoolevent" + "github.com/nspcc-dev/neo-go/pkg/rpc/response/result/subscriptions" "github.com/nspcc-dev/neo-go/pkg/util" + "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" + "github.com/nspcc-dev/neofs-node/pkg/morph/client" util2 "github.com/nspcc-dev/neofs-node/pkg/util" "go.uber.org/zap" ) @@ -88,3 +94,18 @@ func WorkerPoolHandler(w util2.WorkerPool, h Handler, log *zap.Logger) Handler { } } } + +var errEmptyStackArray = errors.New("stack item array is empty") + +// ParseStackArray parses stack array from raw notification +// event received from neo-go RPC node. +func ParseStackArray(event *subscriptions.NotificationEvent) ([]stackitem.Item, error) { + arr, err := client.ArrayFromStackItem(event.Item) + if err != nil { + return nil, fmt.Errorf("stack item is not an array type: %w", err) + } else if len(arr) == 0 { + return nil, errEmptyStackArray + } + + return arr, nil +}