From eef20c53df6730d517dd13f02a687b8c6f7d24eb Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Thu, 3 Sep 2020 17:04:04 +0300 Subject: [PATCH] [#18] Add AddPeer and UpdatePeer morph events Signed-off-by: Alex Vanin --- pkg/morph/event/netmap/add_peer.go | 37 +++++++++++ pkg/morph/event/netmap/add_peer_test.go | 47 ++++++++++++++ pkg/morph/event/netmap/update_peer.go | 59 +++++++++++++++++ pkg/morph/event/netmap/update_peer_test.go | 75 ++++++++++++++++++++++ 4 files changed, 218 insertions(+) create mode 100644 pkg/morph/event/netmap/add_peer.go create mode 100644 pkg/morph/event/netmap/add_peer_test.go create mode 100644 pkg/morph/event/netmap/update_peer.go create mode 100644 pkg/morph/event/netmap/update_peer_test.go diff --git a/pkg/morph/event/netmap/add_peer.go b/pkg/morph/event/netmap/add_peer.go new file mode 100644 index 000000000..5a7bb0e84 --- /dev/null +++ b/pkg/morph/event/netmap/add_peer.go @@ -0,0 +1,37 @@ +package netmap + +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 AddPeer struct { + node []byte +} + +// MorphEvent implements Neo:Morph Event interface. +func (AddPeer) MorphEvent() {} + +func (s AddPeer) Node() []byte { + return s.node +} + +func ParseAddPeer(prms []smartcontract.Parameter) (event.Event, error) { + var ( + ev AddPeer + err error + ) + + if ln := len(prms); ln != 1 { + return nil, event.WrongNumberOfParameters(1, ln) + } + + ev.node, err = client.BytesFromStackParameter(prms[0]) + if err != nil { + return nil, errors.Wrap(err, "could not get integer epoch number") + } + + return ev, nil +} diff --git a/pkg/morph/event/netmap/add_peer_test.go b/pkg/morph/event/netmap/add_peer_test.go new file mode 100644 index 000000000..0842d4eed --- /dev/null +++ b/pkg/morph/event/netmap/add_peer_test.go @@ -0,0 +1,47 @@ +package netmap + +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 TestParseAddPeer(t *testing.T) { + t.Run("wrong number of parameters", func(t *testing.T) { + prms := []smartcontract.Parameter{ + {}, + {}, + } + + _, err := ParseAddPeer(prms) + require.EqualError(t, err, event.WrongNumberOfParameters(1, len(prms)).Error()) + }) + + t.Run("wrong first parameter type", func(t *testing.T) { + _, err := ParseAddPeer([]smartcontract.Parameter{ + { + Type: smartcontract.ByteArrayType, + }, + }) + + require.Error(t, err) + }) + + t.Run("correct behavior", func(t *testing.T) { + info := []byte{1, 2, 3} + + ev, err := ParseAddPeer([]smartcontract.Parameter{ + { + Type: smartcontract.ByteArrayType, + Value: info, + }, + }) + + require.NoError(t, err) + require.Equal(t, AddPeer{ + node: info, + }, ev) + }) +} diff --git a/pkg/morph/event/netmap/update_peer.go b/pkg/morph/event/netmap/update_peer.go new file mode 100644 index 000000000..259cd3645 --- /dev/null +++ b/pkg/morph/event/netmap/update_peer.go @@ -0,0 +1,59 @@ +package netmap + +import ( + "crypto/elliptic" + + "github.com/nspcc-dev/neo-go/pkg/crypto/keys" + "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 UpdatePeer struct { + publicKey *keys.PublicKey + status uint32 +} + +// MorphEvent implements Neo:Morph Event interface. +func (UpdatePeer) MorphEvent() {} + +func (s UpdatePeer) Status() uint32 { + return s.status +} + +func (s UpdatePeer) PublicKey() *keys.PublicKey { + return s.publicKey +} + +func ParseUpdatePeer(prms []smartcontract.Parameter) (event.Event, error) { + var ( + ev UpdatePeer + err error + ) + + if ln := len(prms); ln != 2 { + return nil, event.WrongNumberOfParameters(2, ln) + } + + // parse public key + key, err := client.BytesFromStackParameter(prms[0]) + if err != nil { + return nil, errors.Wrap(err, "could not get public key") + } + + ev.publicKey, err = keys.NewPublicKeyFromBytes(key, elliptic.P256()) + if err != nil { + return nil, errors.Wrap(err, "could not parse public key") + } + + // parse node status + st, err := client.IntFromStackParameter(prms[1]) + if err != nil { + return nil, errors.Wrap(err, "could not get node status") + } + + ev.status = uint32(st) + + return ev, nil +} diff --git a/pkg/morph/event/netmap/update_peer_test.go b/pkg/morph/event/netmap/update_peer_test.go new file mode 100644 index 000000000..3c81cbe3b --- /dev/null +++ b/pkg/morph/event/netmap/update_peer_test.go @@ -0,0 +1,75 @@ +package netmap + +import ( + "crypto/elliptic" + "testing" + + "github.com/nspcc-dev/neo-go/pkg/crypto/keys" + "github.com/nspcc-dev/neo-go/pkg/smartcontract" + crypto "github.com/nspcc-dev/neofs-crypto" + "github.com/nspcc-dev/neofs-crypto/test" + "github.com/nspcc-dev/neofs-node/pkg/morph/event" + "github.com/stretchr/testify/require" +) + +func TestParseUpdatePeer(t *testing.T) { + var ( + publicKey = &test.DecodeKey(-1).PublicKey + state int64 = 1 + ) + + t.Run("wrong number of parameters", func(t *testing.T) { + prms := []smartcontract.Parameter{ + {}, + } + + _, err := ParseUpdatePeer(prms) + require.EqualError(t, err, event.WrongNumberOfParameters(2, len(prms)).Error()) + }) + + t.Run("wrong first parameter type", func(t *testing.T) { + _, err := ParseUpdatePeer([]smartcontract.Parameter{ + { + Type: smartcontract.ByteArrayType, + }, + }) + + require.Error(t, err) + }) + + t.Run("wrong second parameter type", func(t *testing.T) { + _, err := ParseUpdatePeer([]smartcontract.Parameter{ + { + Type: smartcontract.ByteArrayType, + Value: crypto.MarshalPublicKey(publicKey), + }, + { + Type: smartcontract.ArrayType, + }, + }) + + require.Error(t, err) + }) + + t.Run("correct behavior", func(t *testing.T) { + ev, err := ParseUpdatePeer([]smartcontract.Parameter{ + { + Type: smartcontract.ByteArrayType, + Value: crypto.MarshalPublicKey(publicKey), + }, + { + Type: smartcontract.IntegerType, + Value: state, + }, + }) + require.NoError(t, err) + + expectedKey, err := keys.NewPublicKeyFromBytes(crypto.MarshalPublicKey(publicKey), elliptic.P256()) + require.NoError(t, err) + + require.Equal(t, UpdatePeer{ + publicKey: expectedKey, + status: uint32(state), + }, ev) + }) +}