[#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 <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-11-26 14:03:27 +03:00
parent 8175462050
commit 29f589b54e
2 changed files with 41 additions and 32 deletions

View file

@ -76,17 +76,36 @@ func (x *Info) SetOwner(id owner.ID) {
*idv2 = *id.ToV2() *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. // 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. // Must be called only if owner is set (see HasOwner). Arg must not be nil.
func (x Info) ReadOwner(id *owner.ID) { func (x Info) ReadOwner(id *owner.ID) {
infov2 := (subnet.Info)(x) infov2 := (subnet.Info)(x)
// FIXME: we need to implement and use owner.ID.FromV2 method id2 := infov2.Owner()
id2 := owner.NewIDFromV2(infov2.Owner()) if id2 == nil {
*id = *id2 // 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)
} }

View file

@ -7,61 +7,51 @@ import (
subnettest "github.com/nspcc-dev/neofs-api-go/v2/subnet/test" subnettest "github.com/nspcc-dev/neofs-api-go/v2/subnet/test"
"github.com/nspcc-dev/neofs-sdk-go/owner" "github.com/nspcc-dev/neofs-sdk-go/owner"
ownertest "github.com/nspcc-dev/neofs-sdk-go/owner/test" 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" subnetid "github.com/nspcc-dev/neofs-sdk-go/subnet/id"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func TestInfoZero(t *testing.T) { func TestInfoZero(t *testing.T) {
var info subnet.Info var info Info
var id subnetid.ID var id subnetid.ID
info.ReadID(&id) info.ReadID(&id)
require.True(t, subnetid.IsZero(id)) require.True(t, subnetid.IsZero(id))
require.False(t, info.HasOwner())
} }
func TestInfo_SetID(t *testing.T) { func TestInfo_SetID(t *testing.T) {
var ( var (
idFrom, idTo subnetid.ID id subnetid.ID
info Info
info subnet.Info
) )
idFrom.SetNumber(222) id.SetNumber(222)
info.SetID(idFrom) info.SetID(id)
info.ReadID(&idTo) require.True(t, IDEquals(info, id))
require.True(t, idTo.Equals(&idFrom))
} }
func TestInfo_SetOwner(t *testing.T) { func TestInfo_SetOwner(t *testing.T) {
var ( var (
idFrom, idTo owner.ID id owner.ID
info Info
info subnet.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()) require.True(t, IsOwner(info, id))
info.ReadOwner(&idTo)
require.True(t, idTo.Equal(&idFrom))
} }
func TestInfo_WriteToV2(t *testing.T) { func TestInfo_WriteToV2(t *testing.T) {
var ( var (
infoTo, infoFrom subnet.Info infoTo, infoFrom Info
infoV2From, infoV2To subnetv2.Info infoV2From, infoV2To subnetv2.Info
) )