frostfs-node/pkg/core/container/fmt.go
Leonard Lyubich 54d6f3de55 [] core/container: Use version.IsValid in CheckFormat
Container can have version later than the current lib version, and it should
not be considered as invalid. But it should still have adequate version.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
2021-07-05 11:05:44 +03:00

55 lines
1.4 KiB
Go

package container
import (
"errors"
"fmt"
"github.com/nspcc-dev/neofs-api-go/pkg/container"
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
"github.com/nspcc-dev/neofs-node/pkg/core/version"
)
var (
errNilPolicy = errors.New("placement policy is nil")
errRepeatedAttributes = errors.New("repeated attributes found")
errEmptyAttribute = errors.New("empty attribute found")
)
// CheckFormat conducts an initial check of the v2 container data.
//
// It is expected that if a container fails this test,
// it will not be inner-ring approved.
func CheckFormat(c *container.Container) error {
if c.PlacementPolicy() == nil {
return errNilPolicy
}
if v := c.Version(); v == nil || !version.IsValid(*v) {
return fmt.Errorf("incorrect version %s", v)
}
if ln := len(c.OwnerID().ToV2().GetValue()); ln != owner.NEO3WalletSize {
return fmt.Errorf("incorrect owner identifier: expected length %d != %d", owner.NEO3WalletSize, ln)
}
if _, err := c.NonceUUID(); err != nil {
return fmt.Errorf("incorrect nonce: %w", err)
}
// check if there are repeated attributes
attrs := c.Attributes()
uniqueAttr := make(map[string]struct{}, len(attrs))
for _, attr := range attrs {
if _, exists := uniqueAttr[attr.Key()]; exists {
return errRepeatedAttributes
}
if attr.Value() == "" {
return errEmptyAttribute
}
uniqueAttr[attr.Key()] = struct{}{}
}
return nil
}