From 7a7a351f1f8a1a787d8581c15821790062a971cc Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Thu, 28 Jan 2021 18:01:54 +0300 Subject: [PATCH] [#355] morph/event: Add container estimate size events Signed-off-by: Alex Vanin --- pkg/morph/event/container/estimates.go | 66 +++++++++++++++++ pkg/morph/event/container/estimates_test.go | 80 +++++++++++++++++++++ 2 files changed, 146 insertions(+) create mode 100644 pkg/morph/event/container/estimates.go create mode 100644 pkg/morph/event/container/estimates_test.go diff --git a/pkg/morph/event/container/estimates.go b/pkg/morph/event/container/estimates.go new file mode 100644 index 000000000..fbe88175f --- /dev/null +++ b/pkg/morph/event/container/estimates.go @@ -0,0 +1,66 @@ +package container + +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/pkg/errors" +) + +// StartEstimation structure of container.StartEstimation notification from +// morph chain. +type StartEstimation struct { + epoch uint64 +} + +// StopEstimation structure of container.StopEstimation notification from +// morph chain. +type StopEstimation struct { + epoch uint64 +} + +// MorphEvent implements Neo:Morph Event interface. +func (StartEstimation) MorphEvent() {} + +// MorphEvent implements Neo:Morph Event interface. +func (StopEstimation) MorphEvent() {} + +// Epoch returns epoch value for which to start container size estimation. +func (s StartEstimation) Epoch() uint64 { return s.epoch } + +// Epoch returns epoch value for which to stop container size estimation. +func (s StopEstimation) Epoch() uint64 { return s.epoch } + +// ParseStartEstimation from notification into container event structure. +func ParseStartEstimation(params []stackitem.Item) (event.Event, error) { + epoch, err := parseEstimation(params) + if err != nil { + return nil, err + } + + return StartEstimation{epoch: epoch}, nil +} + +// ParseStopEstimation from notification into container event structure. +func ParseStopEstimation(params []stackitem.Item) (event.Event, error) { + epoch, err := parseEstimation(params) + if err != nil { + return nil, err + } + + return StopEstimation{epoch: epoch}, nil +} + +func parseEstimation(params []stackitem.Item) (uint64, error) { + if ln := len(params); ln != 1 { + return 0, event.WrongNumberOfParameters(1, ln) + } + + // parse container + epoch, err := client.IntFromStackItem(params[0]) + if err != nil { + return 0, errors.Wrap(err, "could not get estimation epoch") + } + + return uint64(epoch), nil +} diff --git a/pkg/morph/event/container/estimates_test.go b/pkg/morph/event/container/estimates_test.go new file mode 100644 index 000000000..5a117bcac --- /dev/null +++ b/pkg/morph/event/container/estimates_test.go @@ -0,0 +1,80 @@ +package container + +import ( + "math/big" + "testing" + + "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" + "github.com/nspcc-dev/neofs-node/pkg/morph/event" + "github.com/stretchr/testify/require" +) + +func TestStartEstimation(t *testing.T) { + var epochNum uint64 = 100 + epochItem := stackitem.NewBigInteger(new(big.Int).SetUint64(epochNum)) + + t.Run("wrong number of parameters", func(t *testing.T) { + prms := []stackitem.Item{ + stackitem.NewMap(), + stackitem.NewMap(), + } + + _, err := ParseStartEstimation(prms) + require.EqualError(t, err, event.WrongNumberOfParameters(1, len(prms)).Error()) + }) + + t.Run("wrong estimation parameter", func(t *testing.T) { + _, err := ParseStartEstimation([]stackitem.Item{ + stackitem.NewMap(), + }) + + require.Error(t, err) + }) + + t.Run("correct behavior", func(t *testing.T) { + ev, err := ParseStartEstimation([]stackitem.Item{ + epochItem, + }) + + require.NoError(t, err) + + require.Equal(t, StartEstimation{ + epochNum, + }, ev) + }) +} + +func TestStopEstimation(t *testing.T) { + var epochNum uint64 = 100 + epochItem := stackitem.NewBigInteger(new(big.Int).SetUint64(epochNum)) + + t.Run("wrong number of parameters", func(t *testing.T) { + prms := []stackitem.Item{ + stackitem.NewMap(), + stackitem.NewMap(), + } + + _, err := ParseStopEstimation(prms) + require.EqualError(t, err, event.WrongNumberOfParameters(1, len(prms)).Error()) + }) + + t.Run("wrong estimation parameter", func(t *testing.T) { + _, err := ParseStopEstimation([]stackitem.Item{ + stackitem.NewMap(), + }) + + require.Error(t, err) + }) + + t.Run("correct behavior", func(t *testing.T) { + ev, err := ParseStopEstimation([]stackitem.Item{ + epochItem, + }) + + require.NoError(t, err) + + require.Equal(t, StopEstimation{ + epochNum, + }, ev) + }) +}