From a3e0a9f74c147a35b05d484181fd2c66fd06e197 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Tue, 3 Nov 2020 12:03:41 +0300 Subject: [PATCH] [#85] core/container: Implement CheckFormat function Implement function that conducts sanity checks of the container data. Signed-off-by: Leonard Lyubich --- go.mod | 2 +- go.sum | Bin 59670 -> 59915 bytes pkg/core/container/fmt.go | 35 +++++++++++++++++++++++++ pkg/core/container/fmt_test.go | 46 +++++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 pkg/core/container/fmt.go create mode 100644 pkg/core/container/fmt_test.go 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 f97b2706b344c77653327a98694ee76c68f1e3f5..9334b943ba5b767ad8ea9f5f75da78815b96a9a7 100644 GIT binary patch delta 119 zcmbPsiMjg<^M<8alQ(3Fx*8f78(0{d85!$ZSSFe$TAG-pB&V1vWEfh7nMUSYxH=jI zn`Sz?8)W2om!}1rha^W8mZk*wdlcv!`}(+>>W3Btnwv~sm@Nx3O$@tfn}f2B)Bykj Cb0ysX delta 14 WcmeCa!aVH~^M<8an`dPg)&T%ESO+u! 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)) +}