diff --git a/pkg/morph/event/neofs/config.go b/pkg/morph/event/neofs/config.go new file mode 100644 index 00000000..af996844 --- /dev/null +++ b/pkg/morph/event/neofs/config.go @@ -0,0 +1,45 @@ +package neofs + +import ( + "github.com/nspcc-dev/neo-go/pkg/smartcontract" + "github.com/nspcc-dev/neofs-node/pkg/morph/client" + "github.com/nspcc-dev/neofs-node/pkg/morph/event" + "github.com/pkg/errors" +) + +type Config struct { + key []byte + value []byte +} + +// MorphEvent implements Neo:Morph Event interface. +func (Config) MorphEvent() {} + +func (u Config) Key() []byte { return u.key } + +func (u Config) Value() []byte { return u.value } + +func ParseConfig(params []smartcontract.Parameter) (event.Event, error) { + var ( + ev Config + err error + ) + + if ln := len(params); ln != 2 { + return nil, event.WrongNumberOfParameters(2, ln) + } + + // parse key + ev.key, err = client.BytesFromStackParameter(params[0]) + if err != nil { + return nil, errors.Wrap(err, "could not get config key") + } + + // parse value + ev.value, err = client.BytesFromStackParameter(params[1]) + if err != nil { + return nil, errors.Wrap(err, "could not get config value") + } + + return ev, nil +} diff --git a/pkg/morph/event/neofs/config_test.go b/pkg/morph/event/neofs/config_test.go new file mode 100644 index 00000000..cfecff46 --- /dev/null +++ b/pkg/morph/event/neofs/config_test.go @@ -0,0 +1,68 @@ +package neofs + +import ( + "testing" + + "github.com/nspcc-dev/neo-go/pkg/smartcontract" + "github.com/nspcc-dev/neofs-node/pkg/morph/event" + "github.com/stretchr/testify/require" +) + +func TestParseConfig(t *testing.T) { + var ( + key = []byte("key") + value = []byte("value") + ) + + t.Run("wrong number of parameters", func(t *testing.T) { + prms := []smartcontract.Parameter{ + {}, + } + + _, err := ParseConfig(prms) + require.EqualError(t, err, event.WrongNumberOfParameters(2, len(prms)).Error()) + }) + + t.Run("wrong first parameter", func(t *testing.T) { + _, err := ParseConfig([]smartcontract.Parameter{ + { + Type: smartcontract.ArrayType, + }, + }) + + require.Error(t, err) + }) + + t.Run("wrong second parameter", func(t *testing.T) { + _, err := ParseConfig([]smartcontract.Parameter{ + { + Type: smartcontract.ByteArrayType, + Value: key, + }, + { + Type: smartcontract.ArrayType, + }, + }) + + require.Error(t, err) + }) + + t.Run("correct", func(t *testing.T) { + ev, err := ParseConfig([]smartcontract.Parameter{ + { + Type: smartcontract.ByteArrayType, + Value: key, + }, + { + Type: smartcontract.ByteArrayType, + Value: value, + }, + }) + require.NoError(t, err) + + require.Equal(t, Config{ + key: key, + value: value, + }, ev) + }) +}