From 7d154f8659afd5386166eca51916545beea675e0 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Tue, 8 Sep 2020 11:38:49 +0300 Subject: [PATCH] [#21] Add neofs config event Signed-off-by: Alex Vanin --- pkg/morph/event/neofs/config.go | 45 ++++++++++++++++++ pkg/morph/event/neofs/config_test.go | 68 ++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 pkg/morph/event/neofs/config.go create mode 100644 pkg/morph/event/neofs/config_test.go diff --git a/pkg/morph/event/neofs/config.go b/pkg/morph/event/neofs/config.go new file mode 100644 index 000000000..af9968440 --- /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 000000000..cfecff468 --- /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) + }) +}