diff --git a/go.mod b/go.mod index f1800cef06..23103ca9ca 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/multiformats/go-multiaddr-net v0.1.2 // v0.1.1 => v0.1.2 github.com/multiformats/go-multihash v0.0.13 // indirect github.com/nspcc-dev/neo-go v0.91.1-pre.0.20201030072836-71216865717b - github.com/nspcc-dev/neofs-api-go v1.3.1-0.20201029071528-352e99d9b91a + github.com/nspcc-dev/neofs-api-go v1.3.1-0.20201103083623-89a7a946dcd5 github.com/nspcc-dev/neofs-crypto v0.3.0 github.com/nspcc-dev/tzhash v1.4.0 github.com/panjf2000/ants/v2 v2.3.0 diff --git a/go.sum b/go.sum index f97b2706b3..9334b943ba 100644 Binary files a/go.sum and b/go.sum differ diff --git a/pkg/core/container/fmt.go b/pkg/core/container/fmt.go new file mode 100644 index 0000000000..e5c45f6fe8 --- /dev/null +++ b/pkg/core/container/fmt.go @@ -0,0 +1,35 @@ +package container + +import ( + "github.com/google/uuid" + "github.com/nspcc-dev/neofs-api-go/pkg" + "github.com/nspcc-dev/neofs-api-go/pkg/container" + "github.com/nspcc-dev/neofs-api-go/pkg/owner" + "github.com/pkg/errors" +) + +var errNilPolicy = errors.New("placement policy is nil") + +// CheckFormat conducts an initial check of the 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.GetPlacementPolicy() == nil { + return errNilPolicy + } + + if err := pkg.IsSupportedVersion(pkg.NewVersionFromV2(c.GetVersion())); err != nil { + return errors.Wrap(err, "incorrect version") + } + + if len(c.GetOwnerID().GetValue()) != owner.NEO3WalletSize { + return errors.Wrap(owner.ErrBadID, "incorrect owner identifier") + } + + if _, err := uuid.FromBytes(c.GetNonce()); err != nil { + return errors.Wrap(err, "incorrect nonce") + } + + return nil +} diff --git a/pkg/core/container/fmt_test.go b/pkg/core/container/fmt_test.go new file mode 100644 index 0000000000..f3fdd5f01c --- /dev/null +++ b/pkg/core/container/fmt_test.go @@ -0,0 +1,46 @@ +package container + +import ( + "testing" + + "github.com/google/uuid" + "github.com/nspcc-dev/neofs-api-go/pkg" + "github.com/nspcc-dev/neofs-api-go/pkg/container" + "github.com/nspcc-dev/neofs-api-go/pkg/netmap" + "github.com/nspcc-dev/neofs-api-go/pkg/owner" + "github.com/nspcc-dev/neofs-node/pkg/util/test" + "github.com/stretchr/testify/require" +) + +func TestCheckFormat(t *testing.T) { + c := container.New() + + require.Error(t, CheckFormat(c)) + + policy := new(netmap.PlacementPolicy) + c.SetPlacementPolicy(policy) + + require.Error(t, CheckFormat(c)) + + c.SetVersion(pkg.SDKVersion().ToV2()) + + require.Error(t, CheckFormat(c)) + + wallet, err := owner.NEO3WalletFromPublicKey(&test.DecodeKey(-1).PublicKey) + require.NoError(t, err) + + c.SetOwnerID(owner.NewIDFromNeo3Wallet(wallet).ToV2()) + + c.SetNonce(nil) + + require.Error(t, CheckFormat(c)) + + uid, err := uuid.NewRandom() + require.NoError(t, err) + + nonce, _ := uid.MarshalBinary() + + c.SetNonce(nonce) + + require.NoError(t, CheckFormat(c)) +}