[#21] Add neofs config event

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2020-09-08 11:38:49 +03:00
parent f412d20523
commit 7d154f8659
2 changed files with 113 additions and 0 deletions

View file

@ -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
}

View file

@ -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)
})
}