forked from TrueCloudLab/frostfs-node
[#355] morph/event: Add container estimate size events
Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
parent
db94ad5567
commit
7a7a351f1f
2 changed files with 146 additions and 0 deletions
66
pkg/morph/event/container/estimates.go
Normal file
66
pkg/morph/event/container/estimates.go
Normal file
|
@ -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
|
||||
}
|
80
pkg/morph/event/container/estimates_test.go
Normal file
80
pkg/morph/event/container/estimates_test.go
Normal file
|
@ -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)
|
||||
})
|
||||
}
|
Loading…
Reference in a new issue