From 335b31206fd596a86c22431b7c6513cc6c905bba Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Wed, 31 Mar 2021 17:32:47 +0300 Subject: [PATCH] [#452] morph/event: Support `reputationPut` event Signed-off-by: Alex Vanin --- pkg/morph/event/reputation/put.go | 68 ++++++++++++++++++++++++ pkg/morph/event/reputation/put_test.go | 72 ++++++++++++++++++++++++++ 2 files changed, 140 insertions(+) create mode 100644 pkg/morph/event/reputation/put.go create mode 100644 pkg/morph/event/reputation/put_test.go diff --git a/pkg/morph/event/reputation/put.go b/pkg/morph/event/reputation/put.go new file mode 100644 index 000000000..89b761090 --- /dev/null +++ b/pkg/morph/event/reputation/put.go @@ -0,0 +1,68 @@ +package reputation + +import ( + "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" + "github.com/nspcc-dev/neofs-node/pkg/services/reputation" + "github.com/pkg/errors" +) + +// Put structure of reputation.reputationPut notification from +// morph chain. +type Put struct { + epoch uint64 + peerID reputation.PeerID + value []byte +} + +// MorphEvent implements Neo:Morph Event interface. +func (Put) MorphEvent() {} + +// Epoch returns epoch value of reputation data. +func (p Put) Epoch() uint64 { + return p.epoch +} + +// PeerID returns peer id of reputation data. +func (p Put) PeerID() reputation.PeerID { + return p.peerID +} + +// Value returns reputation structure. +func (p Put) Value() []byte { + return p.value // consider returning parsed structure +} + +// ParsePut from notification into reputation event structure. +func ParsePut(prms []stackitem.Item) (event.Event, error) { + var ( + ev Put + err error + ) + + if ln := len(prms); ln != 3 { + return nil, event.WrongNumberOfParameters(3, ln) + } + + epoch, err := client.IntFromStackItem(prms[0]) + if err != nil { + return nil, errors.Wrap(err, "could not get integer epoch number") + } + + ev.epoch = uint64(epoch) + + peerID, err := client.BytesFromStackItem(prms[1]) + if err != nil { + return nil, errors.Wrap(err, "could not get peer ID value") + } + + ev.peerID = reputation.PeerIDFromBytes(peerID) + + ev.value, err = client.BytesFromStackItem(prms[2]) + if err != nil { + return nil, errors.Wrap(err, "could not get reputation value") + } + + return ev, nil +} diff --git a/pkg/morph/event/reputation/put_test.go b/pkg/morph/event/reputation/put_test.go new file mode 100644 index 000000000..9856f0bdb --- /dev/null +++ b/pkg/morph/event/reputation/put_test.go @@ -0,0 +1,72 @@ +package reputation + +import ( + "math/big" + "testing" + + "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/services/reputation" + "github.com/stretchr/testify/require" +) + +func TestParsePut(t *testing.T) { + var ( + epoch uint64 = 42 + + peerID = reputation.PeerIDFromBytes([]byte("peerID")) + value = []byte("There should be marshalled structure") + ) + + t.Run("wrong number of parameters", func(t *testing.T) { + prms := []stackitem.Item{ + stackitem.NewMap(), + stackitem.NewMap(), + } + + _, err := ParsePut(prms) + require.EqualError(t, err, event.WrongNumberOfParameters(3, len(prms)).Error()) + }) + + t.Run("wrong epoch parameter", func(t *testing.T) { + _, err := ParsePut([]stackitem.Item{ + stackitem.NewMap(), + }) + + require.Error(t, err) + }) + + t.Run("wrong peerID parameter", func(t *testing.T) { + _, err := ParsePut([]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{ + stackitem.NewBigInteger(new(big.Int).SetUint64(epoch)), + stackitem.NewByteArray(peerID.Bytes()), + stackitem.NewMap(), + }) + + require.Error(t, err) + }) + + t.Run("correct behavior", func(t *testing.T) { + ev, err := ParsePut([]stackitem.Item{ + stackitem.NewBigInteger(new(big.Int).SetUint64(epoch)), + stackitem.NewByteArray(peerID.Bytes()), + stackitem.NewByteArray(value), + }) + require.NoError(t, err) + + require.Equal(t, Put{ + epoch: epoch, + peerID: peerID, + value: value, + }, ev) + }) +}