From 93e15806840572236637da8bf46ce55e19009143 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Thu, 28 Jan 2021 16:01:07 +0300 Subject: [PATCH] [#253] container: Implement marshalers on UsedSpaceAnnouncement structure Implement Unmarshal method on UsedSpaceAnnouncement v2 message. Implement Marshal/Unmarshal methods on cross-version UsedSpaceAnnouncement type. Signed-off-by: Leonard Lyubich --- pkg/container/announcement.go | 20 ++++++++++++++ pkg/container/announcement_test.go | 22 ++++++++++++++++ v2/container/marshal.go | 11 ++++++++ v2/container/marshal_test.go | 42 +++++++++++++++++++++++------- 4 files changed, 85 insertions(+), 10 deletions(-) diff --git a/pkg/container/announcement.go b/pkg/container/announcement.go index bc8b6e4..8af1950 100644 --- a/pkg/container/announcement.go +++ b/pkg/container/announcement.go @@ -55,3 +55,23 @@ func (a *UsedSpaceAnnouncement) SetUsedSpace(value uint64) { func (a *UsedSpaceAnnouncement) ToV2() *container.UsedSpaceAnnouncement { return (*container.UsedSpaceAnnouncement)(a) } + +// Marshal marshals UsedSpaceAnnouncement into a protobuf binary form. +// +// Buffer is allocated when the argument is empty. +// Otherwise, the first buffer is used. +func (a *UsedSpaceAnnouncement) Marshal(b ...[]byte) ([]byte, error) { + var buf []byte + if len(b) > 0 { + buf = b[0] + } + + return a.ToV2(). + StableMarshal(buf) +} + +// Unmarshal unmarshals protobuf binary representation of UsedSpaceAnnouncement. +func (a *UsedSpaceAnnouncement) Unmarshal(data []byte) error { + return a.ToV2(). + Unmarshal(data) +} diff --git a/pkg/container/announcement_test.go b/pkg/container/announcement_test.go index ad3b16f..011b297 100644 --- a/pkg/container/announcement_test.go +++ b/pkg/container/announcement_test.go @@ -1,6 +1,7 @@ package container_test import ( + "crypto/sha256" "testing" "github.com/nspcc-dev/neofs-api-go/pkg/container" @@ -47,3 +48,24 @@ func TestAnnouncement(t *testing.T) { require.Equal(t, container.NewIDFromV2(newCID), newA.ContainerID()) }) } + +func TestUsedSpaceEncoding(t *testing.T) { + a := container.NewAnnouncement() + a.SetUsedSpace(13) + a.SetEpoch(666) + + id := container.NewID() + id.SetSHA256([sha256.Size]byte{1, 2, 3}) + + a.SetContainerID(id) + + t.Run("binary", func(t *testing.T) { + data, err := a.Marshal() + require.NoError(t, err) + + a2 := container.NewAnnouncement() + require.NoError(t, a2.Unmarshal(data)) + + require.Equal(t, a, a2) + }) +} diff --git a/v2/container/marshal.go b/v2/container/marshal.go index ae190bd..cf16e8a 100644 --- a/v2/container/marshal.go +++ b/v2/container/marshal.go @@ -589,6 +589,17 @@ func (a *UsedSpaceAnnouncement) StableSize() (size int) { return size } +func (a *UsedSpaceAnnouncement) Unmarshal(data []byte) error { + m := new(container.AnnounceUsedSpaceRequest_Body_Announcement) + if err := proto.Unmarshal(data, m); err != nil { + return err + } + + *a = *UsedSpaceAnnouncementFromGRPCMessage(m) + + return nil +} + func (r *AnnounceUsedSpaceRequestBody) StableMarshal(buf []byte) ([]byte, error) { if r == nil { return []byte{}, nil diff --git a/v2/container/marshal_test.go b/v2/container/marshal_test.go index dd6599f..f1fc189 100644 --- a/v2/container/marshal_test.go +++ b/v2/container/marshal_test.go @@ -234,6 +234,22 @@ func TestGetEACLResponseBody_StableMarshal(t *testing.T) { }) } +func TestUsedSpaceAnnouncement_StableMarshal(t *testing.T) { + from := generateAnnouncement() + transport := new(grpc.AnnounceUsedSpaceRequest_Body_Announcement) + + t.Run("non empty", func(t *testing.T) { + wire, err := from.StableMarshal(nil) + require.NoError(t, err) + + err = goproto.Unmarshal(wire, transport) + require.NoError(t, err) + + to := container.UsedSpaceAnnouncementFromGRPCMessage(transport) + require.Equal(t, from, to) + }) +} + func TestAnnounceUsedSpaceRequestBody_StableMarshal(t *testing.T) { requestFrom := generateAnnounceRequestBody(10) transport := new(grpc.AnnounceUsedSpaceRequest_Body) @@ -443,21 +459,27 @@ func generateGetEACLResponseBody(n int, k, v string) *container.GetExtendedACLRe return resp } +func generateAnnouncement() *container.UsedSpaceAnnouncement { + buf := make([]byte, sha256.Size) + rand.Read(buf) + + cid := new(refs.ContainerID) + cid.SetValue(buf) + + a := new(container.UsedSpaceAnnouncement) + a.SetEpoch(rand.Uint64()) + a.SetContainerID(cid) + a.SetUsedSpace(rand.Uint64()) + + return a +} + func generateAnnounceRequestBody(n int) *container.AnnounceUsedSpaceRequestBody { resp := new(container.AnnounceUsedSpaceRequestBody) - buf := make([]byte, sha256.Size) announcements := make([]*container.UsedSpaceAnnouncement, 0, n) for i := 0; i < n; i++ { - rand.Read(buf) - - cid := new(refs.ContainerID) - cid.SetValue(buf) - - a := new(container.UsedSpaceAnnouncement) - a.SetEpoch(rand.Uint64()) - a.SetContainerID(cid) - a.SetUsedSpace(rand.Uint64()) + announcements = append(announcements, generateAnnouncement()) } resp.SetAnnouncements(announcements)