[#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 82 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-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

BIN
go.sum

Binary file not shown.

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