2021-01-28 15:01:54 +00:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event"
|
2022-07-28 16:22:32 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
2021-01-28 15:01:54 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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.
|
2022-07-28 16:22:32 +00:00
|
|
|
func ParseStartEstimation(e *state.ContainedNotificationEvent) (event.Event, error) {
|
2021-10-22 10:06:08 +00:00
|
|
|
params, err := event.ParseStackArray(e)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not parse stack items from notify event: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-01-28 15:01:54 +00:00
|
|
|
epoch, err := parseEstimation(params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return StartEstimation{epoch: epoch}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseStopEstimation from notification into container event structure.
|
2022-07-28 16:22:32 +00:00
|
|
|
func ParseStopEstimation(e *state.ContainedNotificationEvent) (event.Event, error) {
|
2021-10-22 10:06:08 +00:00
|
|
|
params, err := event.ParseStackArray(e)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not parse stack items from notify event: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-01-28 15:01:54 +00:00
|
|
|
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 {
|
2021-05-18 08:12:51 +00:00
|
|
|
return 0, fmt.Errorf("could not get estimation epoch: %w", err)
|
2021-01-28 15:01:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return uint64(epoch), nil
|
|
|
|
}
|