frostfs-node/pkg/morph/event/container/estimates.go
Pavel Karpy c167ae26f9 [#971] morph/event: Change notification parser's signature
Parsers should have original notification
structure to be able to construct internal
event structure that contains necessary
for unique nonce calculation information.
So notification parsers take raw notification
structure instead of slice of stack items.

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
2021-11-19 09:58:03 +03:00

79 lines
2.1 KiB
Go

package container
import (
"fmt"
"github.com/nspcc-dev/neo-go/pkg/rpc/response/result/subscriptions"
"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"
)
// 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(e *subscriptions.NotificationEvent) (event.Event, error) {
params, err := event.ParseStackArray(e)
if err != nil {
return nil, fmt.Errorf("could not parse stack items from notify event: %w", err)
}
epoch, err := parseEstimation(params)
if err != nil {
return nil, err
}
return StartEstimation{epoch: epoch}, nil
}
// ParseStopEstimation from notification into container event structure.
func ParseStopEstimation(e *subscriptions.NotificationEvent) (event.Event, error) {
params, err := event.ParseStackArray(e)
if err != nil {
return nil, fmt.Errorf("could not parse stack items from notify event: %w", err)
}
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, fmt.Errorf("could not get estimation epoch: %w", err)
}
return uint64(epoch), nil
}