2020-11-03 09:03:41 +00:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2021-07-02 05:54:07 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/version"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/container"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/owner"
|
2020-11-03 09:03:41 +00:00
|
|
|
)
|
|
|
|
|
2021-06-23 12:45:42 +00:00
|
|
|
var (
|
|
|
|
errNilPolicy = errors.New("placement policy is nil")
|
|
|
|
errRepeatedAttributes = errors.New("repeated attributes found")
|
|
|
|
errEmptyAttribute = errors.New("empty attribute found")
|
|
|
|
)
|
2020-11-03 09:03:41 +00:00
|
|
|
|
2020-11-16 09:43:52 +00:00
|
|
|
// CheckFormat conducts an initial check of the v2 container data.
|
2020-11-03 09:03:41 +00:00
|
|
|
//
|
|
|
|
// It is expected that if a container fails this test,
|
|
|
|
// it will not be inner-ring approved.
|
|
|
|
func CheckFormat(c *container.Container) error {
|
2020-11-16 09:43:52 +00:00
|
|
|
if c.PlacementPolicy() == nil {
|
2020-11-03 09:03:41 +00:00
|
|
|
return errNilPolicy
|
|
|
|
}
|
|
|
|
|
2021-07-02 05:54:07 +00:00
|
|
|
if v := c.Version(); v == nil || !version.IsValid(*v) {
|
|
|
|
return fmt.Errorf("incorrect version %s", v)
|
2020-11-03 09:03:41 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 08:51:49 +00:00
|
|
|
if ln := len(c.OwnerID().ToV2().GetValue()); ln != owner.NEO3WalletSize {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("incorrect owner identifier: expected length %d != %d", owner.NEO3WalletSize, ln)
|
2020-11-03 09:03:41 +00:00
|
|
|
}
|
|
|
|
|
2020-12-24 10:20:20 +00:00
|
|
|
if _, err := c.NonceUUID(); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return fmt.Errorf("incorrect nonce: %w", err)
|
2020-11-03 09:03:41 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 12:45:42 +00:00
|
|
|
// 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{}{}
|
|
|
|
}
|
|
|
|
|
2020-11-03 09:03:41 +00:00
|
|
|
return nil
|
|
|
|
}
|