[#85] core/container: Implement CheckFormat function

Implement function that conducts sanity checks of the container data.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-11-03 12:03:41 +03:00 committed by Leonard Lyubich
parent 83119c00ec
commit a3e0a9f74c
4 changed files with 84 additions and 1 deletions

2
go.mod
View file

@ -13,7 +13,7 @@ require (
github.com/multiformats/go-multiaddr-net v0.1.2 // v0.1.1 => v0.1.2 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/multiformats/go-multihash v0.0.13 // indirect
github.com/nspcc-dev/neo-go v0.91.1-pre.0.20201030072836-71216865717b 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/neofs-crypto v0.3.0
github.com/nspcc-dev/tzhash v1.4.0 github.com/nspcc-dev/tzhash v1.4.0
github.com/panjf2000/ants/v2 v2.3.0 github.com/panjf2000/ants/v2 v2.3.0

2
go.sum
View file

@ -277,6 +277,8 @@ github.com/nspcc-dev/neo-go v0.91.1-pre.0.20201030072836-71216865717b h1:gk5bZgp
github.com/nspcc-dev/neo-go v0.91.1-pre.0.20201030072836-71216865717b/go.mod h1:9s7LNp2lMgf0caH2t0sam4+WT2SIauXozwP0AdBqnEo= github.com/nspcc-dev/neo-go v0.91.1-pre.0.20201030072836-71216865717b/go.mod h1:9s7LNp2lMgf0caH2t0sam4+WT2SIauXozwP0AdBqnEo=
github.com/nspcc-dev/neofs-api-go v1.3.1-0.20201029071528-352e99d9b91a h1:+fYK6dnV+ty2p/PoVLTgN2OgOeyFgl/fFYfIPXdvJYI= github.com/nspcc-dev/neofs-api-go v1.3.1-0.20201029071528-352e99d9b91a h1:+fYK6dnV+ty2p/PoVLTgN2OgOeyFgl/fFYfIPXdvJYI=
github.com/nspcc-dev/neofs-api-go v1.3.1-0.20201029071528-352e99d9b91a/go.mod h1:G7dqincfdjBrAbL5nxVp82emF05fSVEqe59ICsoRDI8= github.com/nspcc-dev/neofs-api-go v1.3.1-0.20201029071528-352e99d9b91a/go.mod h1:G7dqincfdjBrAbL5nxVp82emF05fSVEqe59ICsoRDI8=
github.com/nspcc-dev/neofs-api-go v1.3.1-0.20201103083623-89a7a946dcd5 h1:V5Yo8EA2S5iAG0hlKwfS7TcZqudPOHp/3MLG5/UpQ74=
github.com/nspcc-dev/neofs-api-go v1.3.1-0.20201103083623-89a7a946dcd5/go.mod h1:G7dqincfdjBrAbL5nxVp82emF05fSVEqe59ICsoRDI8=
github.com/nspcc-dev/neofs-crypto v0.2.0/go.mod h1:F/96fUzPM3wR+UGsPi3faVNmFlA9KAEAUQR7dMxZmNA= github.com/nspcc-dev/neofs-crypto v0.2.0/go.mod h1:F/96fUzPM3wR+UGsPi3faVNmFlA9KAEAUQR7dMxZmNA=
github.com/nspcc-dev/neofs-crypto v0.2.3/go.mod h1:8w16GEJbH6791ktVqHN9YRNH3s9BEEKYxGhlFnp0cDw= github.com/nspcc-dev/neofs-crypto v0.2.3/go.mod h1:8w16GEJbH6791ktVqHN9YRNH3s9BEEKYxGhlFnp0cDw=
github.com/nspcc-dev/neofs-crypto v0.3.0 h1:zlr3pgoxuzrmGCxc5W8dGVfA9Rro8diFvVnBg0L4ifM= github.com/nspcc-dev/neofs-crypto v0.3.0 h1:zlr3pgoxuzrmGCxc5W8dGVfA9Rro8diFvVnBg0L4ifM=

35
pkg/core/container/fmt.go Normal file
View file

@ -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
}

View file

@ -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))
}