From 29f589b54e3962356fcef524805aafe8b84863d1 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Fri, 26 Nov 2021 14:03:27 +0300 Subject: [PATCH] [#94] subnet: Add methods to check subnet owner and ID Implement `IsOwner` and `IDEquals` functions which check the correspondence of the fields in `Info`. Remove no longer needed `HasOwner` method of `Info`. Signed-off-by: Leonard Lyubich --- subnet/subnet.go | 35 +++++++++++++++++++++++++++-------- subnet/subnet_test.go | 38 ++++++++++++++------------------------ 2 files changed, 41 insertions(+), 32 deletions(-) diff --git a/subnet/subnet.go b/subnet/subnet.go index 996f449..f70f876 100644 --- a/subnet/subnet.go +++ b/subnet/subnet.go @@ -76,17 +76,36 @@ func (x *Info) SetOwner(id owner.ID) { *idv2 = *id.ToV2() } -// HasOwner checks if subnet owner is set. -func (x Info) HasOwner() bool { - return (*subnet.Info)(&x).Owner() != nil -} - // ReadOwner reads the identifier of the subnet that Info describes. // Must be called only if owner is set (see HasOwner). Arg must not be nil. func (x Info) ReadOwner(id *owner.ID) { infov2 := (subnet.Info)(x) - // FIXME: we need to implement and use owner.ID.FromV2 method - id2 := owner.NewIDFromV2(infov2.Owner()) - *id = *id2 + id2 := infov2.Owner() + if id2 == nil { + // TODO: implement owner.ID.Reset + *id = owner.ID{} + return + } + + // TODO: we need to implement and use owner.ID.FromV2 method + *id = *owner.NewIDFromV2(infov2.Owner()) +} + +// IsOwner checks subnet ownership. +func IsOwner(info Info, id owner.ID) bool { + id2 := new(owner.ID) + + info.ReadOwner(id2) + + return id.Equal(id2) +} + +// IDEquals checks if ID refers to subnet that Info describes. +func IDEquals(info Info, id subnetid.ID) bool { + id2 := new(subnetid.ID) + + info.ReadID(id2) + + return id.Equals(id2) } diff --git a/subnet/subnet_test.go b/subnet/subnet_test.go index 763cb71..438bfb4 100644 --- a/subnet/subnet_test.go +++ b/subnet/subnet_test.go @@ -7,61 +7,51 @@ import ( subnettest "github.com/nspcc-dev/neofs-api-go/v2/subnet/test" "github.com/nspcc-dev/neofs-sdk-go/owner" ownertest "github.com/nspcc-dev/neofs-sdk-go/owner/test" - "github.com/nspcc-dev/neofs-sdk-go/subnet" + . "github.com/nspcc-dev/neofs-sdk-go/subnet" subnetid "github.com/nspcc-dev/neofs-sdk-go/subnet/id" "github.com/stretchr/testify/require" ) func TestInfoZero(t *testing.T) { - var info subnet.Info + var info Info var id subnetid.ID info.ReadID(&id) require.True(t, subnetid.IsZero(id)) - - require.False(t, info.HasOwner()) } func TestInfo_SetID(t *testing.T) { var ( - idFrom, idTo subnetid.ID - - info subnet.Info + id subnetid.ID + info Info ) - idFrom.SetNumber(222) + id.SetNumber(222) - info.SetID(idFrom) + info.SetID(id) - info.ReadID(&idTo) - - require.True(t, idTo.Equals(&idFrom)) + require.True(t, IDEquals(info, id)) } func TestInfo_SetOwner(t *testing.T) { var ( - idFrom, idTo owner.ID - - info subnet.Info + id owner.ID + info Info ) - idFrom = *ownertest.GenerateID() + id = *ownertest.GenerateID() - require.False(t, info.HasOwner()) + require.False(t, IsOwner(info, id)) - info.SetOwner(idFrom) + info.SetOwner(id) - require.True(t, info.HasOwner()) - - info.ReadOwner(&idTo) - - require.True(t, idTo.Equal(&idFrom)) + require.True(t, IsOwner(info, id)) } func TestInfo_WriteToV2(t *testing.T) { var ( - infoTo, infoFrom subnet.Info + infoTo, infoFrom Info infoV2From, infoV2To subnetv2.Info )