diff --git a/pkg/morph/event/balance/lock.go b/pkg/morph/event/balance/lock.go index 9acb5ad516..1e465b4e62 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 bc04357522..7bc539a115 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 a3115cd076..4ce7b0baae 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 37f5edef6b..ea808b3e77 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 f181459822..cdb9f9bd84 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 d1daadbe90..b521d42932 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 b3e7b0746e..a5b27a7355 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 5a117bcac4..1f7a596721 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 9cfdf4557d..21b3ef56b0 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 c2d9184d70..e53bfd9166 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 dfa65e2f00..a216427461 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 a516e0d9dd..c5c4674e7c 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 7fd1d9b28d..c7a6288335 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 c289303ae2..8c09fba57c 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 915cc6f8a7..744844682f 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 6c2fe4eb44..01efc955ef 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 fbe060fa7a..b6a3222e88 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 6019f7f28c..2485a01cd7 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 80de2cc413..dfd4558285 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 2b07465b80..8f7bb6c197 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 ebd0bec21c..b2cd93e06c 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 2c0c459292..781f74e66a 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 798ef77d5b..d251d95598 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 1d013b34f5..aa0014e145 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 ae74e19dd2..ae231ecf01 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 406aba8064..371a47ec6d 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 a3d9780ea2..bf7ea66d5b 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 baf35c592b..7d0897d78f 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 dd5052a736..0ee4072f98 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 4670c5a472..37b57f4679 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 9d31c12eab..3e0e21d2af 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 e9ed967ee9..0d53df22d1 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 700f224fea..1a0a1e304c 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 8c1dc014eb..1c9ca7f5c6 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 31415319ac..f85d9598fe 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 +}