From 70c29ca3e5f687c219e502cb815186f6cf86fd9d Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Thu, 24 Dec 2020 12:26:41 +0300 Subject: [PATCH] [#194] pkg/container: Implement container nonce getter/setter as UUID Implement NonceUUID/SetNonceUUID methods on container structure. Change implementation of Nonce/SetNonce methods and mark them deprecated. Signed-off-by: Leonard Lyubich --- pkg/container/container.go | 41 ++++++++++++++++++++++++++++++--- pkg/container/container_test.go | 11 +++++---- 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/pkg/container/container.go b/pkg/container/container.go index 1638ca5..2c94938 100644 --- a/pkg/container/container.go +++ b/pkg/container/container.go @@ -3,6 +3,7 @@ package container import ( "crypto/sha256" + "github.com/google/uuid" "github.com/nspcc-dev/neofs-api-go/pkg" "github.com/nspcc-dev/neofs-api-go/pkg/netmap" "github.com/nspcc-dev/neofs-api-go/pkg/owner" @@ -21,7 +22,7 @@ func New(opts ...NewOption) *Container { } cnr := new(Container) - cnr.SetNonce(cnrOptions.nonce[:]) + cnr.SetNonceUUID(cnrOptions.nonce) cnr.SetBasicACL(cnrOptions.acl) if cnrOptions.owner != nil { @@ -81,12 +82,46 @@ func (c *Container) SetOwnerID(v *owner.ID) { c.v2.SetOwnerID(v.ToV2()) } +// Nonce returns container nonce in a binary format. +// +// Returns nil if container nonce is not a valid UUID. +// +// Deprecated: use NonceUUID instead. func (c *Container) Nonce() []byte { - return c.v2.GetNonce() // return uuid? + uid, err := c.NonceUUID() + if err == nil { + data, _ := uid.MarshalBinary() + return data + } + + return nil } +// SetNonce sets container nonce in a binary format. +// +// If slice length is less than UUID size, than +// value is padded with a sequence of zeros. +// If slice length is more than UUID size, than +// value is cut. +// +// Deprecated: use SetNonceUUID instead. func (c *Container) SetNonce(v []byte) { - c.v2.SetNonce(v) // set uuid? + u := uuid.UUID{} + copy(u[:], v) + c.v2.SetNonce(u[:]) +} + +// Returns container nonce in UUID format. +// +// Returns error if container nonce is not a valid UUID. +func (c *Container) NonceUUID() (uuid.UUID, error) { + return uuid.FromBytes(c.v2.GetNonce()) +} + +// SetNonceUUID sets container nonce as UUID. +func (c *Container) SetNonceUUID(v uuid.UUID) { + data, _ := v.MarshalBinary() + c.v2.SetNonce(data) } func (c *Container) BasicACL() uint32 { diff --git a/pkg/container/container_test.go b/pkg/container/container_test.go index 391b983..f4739ab 100644 --- a/pkg/container/container_test.go +++ b/pkg/container/container_test.go @@ -17,8 +17,7 @@ import ( func TestNewContainer(t *testing.T) { c := container.New() - nonce, err := uuid.New().MarshalBinary() - require.NoError(t, err) + nonce := uuid.New() wallet, err := owner.NEO3WalletFromPublicKey(&test.DecodeKey(1).PublicKey) require.NoError(t, err) @@ -29,7 +28,7 @@ func TestNewContainer(t *testing.T) { c.SetBasicACL(acl.PublicBasicRule) c.SetAttributes(generateAttributes(5)) c.SetPlacementPolicy(policy) - c.SetNonce(nonce) + c.SetNonceUUID(nonce) c.SetOwnerID(ownerID) c.SetVersion(pkg.SDKVersion()) @@ -39,7 +38,11 @@ func TestNewContainer(t *testing.T) { require.EqualValues(t, newContainer.PlacementPolicy(), policy) require.EqualValues(t, newContainer.Attributes(), generateAttributes(5)) require.EqualValues(t, newContainer.BasicACL(), acl.PublicBasicRule) - require.EqualValues(t, newContainer.Nonce(), nonce) + + newNonce, err := newContainer.NonceUUID() + require.NoError(t, err) + + require.EqualValues(t, newNonce, nonce) require.EqualValues(t, newContainer.OwnerID(), ownerID) require.EqualValues(t, newContainer.Version(), pkg.SDKVersion()) }