From 157a9930e8120edd0f7e20d19cee224c2297b583 Mon Sep 17 00:00:00 2001 From: Dmitrii Stepanov Date: Tue, 21 Nov 2023 11:35:10 +0300 Subject: [PATCH] [#190] sdk-go: Pass user.ID by value Signed-off-by: Dmitrii Stepanov --- bearer/bearer_test.go | 8 ++++---- bearer/test/generate.go | 2 +- client/accounting.go | 6 +++--- client/container_list.go | 6 +++--- container/container_test.go | 2 +- container/test/generate.go | 2 +- eacl/record.go | 2 +- object/object.go | 16 +++++++++------- object/object_test.go | 4 ++-- object/transformer/size_hint_test.go | 2 +- object/transformer/transformer_test.go | 4 ++-- pool/pool.go | 4 ++-- session/container_test.go | 2 +- session/object_test.go | 2 +- user/id.go | 21 +++++++++++++++------ user/id_test.go | 8 ++++---- user/test/id.go | 4 ++-- 17 files changed, 53 insertions(+), 42 deletions(-) diff --git a/bearer/bearer_test.go b/bearer/bearer_test.go index 5948bad..3c0e946 100644 --- a/bearer/bearer_test.go +++ b/bearer/bearer_test.go @@ -107,7 +107,7 @@ func TestToken_ForUser(t *testing.T) { require.Zero(t, m.GetBody()) // set value - usr := *usertest.ID() + usr := usertest.ID() var usrV2 refs.OwnerID usr.WriteToV2(&usrV2) @@ -243,11 +243,11 @@ func TestToken_AssertContainer(t *testing.T) { func TestToken_AssertUser(t *testing.T) { var val bearer.Token - usr := *usertest.ID() + usr := usertest.ID() require.True(t, val.AssertUser(usr)) - val.ForUser(*usertest.ID()) + val.ForUser(usertest.ID()) require.False(t, val.AssertUser(usr)) val.ForUser(usr) @@ -332,7 +332,7 @@ func TestToken_ReadFromV2(t *testing.T) { val.WriteToV2(&m2) require.Equal(t, m, m2) - usr, usr2 := *usertest.ID(), *usertest.ID() + usr, usr2 := usertest.ID(), usertest.ID() require.True(t, val.AssertUser(usr)) require.True(t, val.AssertUser(usr2)) diff --git a/bearer/test/generate.go b/bearer/test/generate.go index 4f5dd3d..4fce6a4 100644 --- a/bearer/test/generate.go +++ b/bearer/test/generate.go @@ -13,7 +13,7 @@ func Token() (t bearer.Token) { t.SetExp(3) t.SetNbf(2) t.SetIat(1) - t.ForUser(*usertest.ID()) + t.ForUser(usertest.ID()) t.SetEACLTable(*eacltest.Table()) return t diff --git a/client/accounting.go b/client/accounting.go index d0aa653..faf7ddc 100644 --- a/client/accounting.go +++ b/client/accounting.go @@ -19,7 +19,7 @@ import ( type PrmBalanceGet struct { XHeaders []string - Account *user.ID + Account user.ID } // SetAccount sets identifier of the FrostFS account for which the balance is requested. @@ -27,11 +27,11 @@ type PrmBalanceGet struct { // // Deprecated: Use PrmBalanceGet.Account instead. func (x *PrmBalanceGet) SetAccount(id user.ID) { - x.Account = &id + x.Account = id } func (x *PrmBalanceGet) buildRequest(c *Client) (*v2accounting.BalanceRequest, error) { - if x.Account == nil { + if x.Account.IsEmpty() { return nil, errorAccountNotSet } diff --git a/client/container_list.go b/client/container_list.go index 0fb91c2..4e88135 100644 --- a/client/container_list.go +++ b/client/container_list.go @@ -19,7 +19,7 @@ import ( type PrmContainerList struct { XHeaders []string - Account *user.ID + Account user.ID } // SetAccount sets identifier of the FrostFS account to list the containers. @@ -27,11 +27,11 @@ type PrmContainerList struct { // // Deprecated: Use PrmContainerList.Account instead. func (x *PrmContainerList) SetAccount(id user.ID) { - x.Account = &id + x.Account = id } func (x *PrmContainerList) buildRequest(c *Client) (*v2container.ListRequest, error) { - if x.Account == nil { + if x.Account.IsEmpty() { return nil, errorAccountNotSet } diff --git a/container/container_test.go b/container/container_test.go index f0a2244..d2ecf1d 100644 --- a/container/container_test.go +++ b/container/container_test.go @@ -78,7 +78,7 @@ func TestContainer_Owner(t *testing.T) { val = containertest.Container() - owner := *usertest.ID() + owner := usertest.ID() val.SetOwner(owner) diff --git a/container/test/generate.go b/container/test/generate.go index 65b03a8..7ac0c2a 100644 --- a/container/test/generate.go +++ b/container/test/generate.go @@ -16,7 +16,7 @@ func Container() (x container.Container) { x.Init() x.SetAttribute("some attribute", "value") - x.SetOwner(*owner) + x.SetOwner(owner) x.SetBasicACL(BasicACL()) x.SetPlacementPolicy(netmaptest.PlacementPolicy()) diff --git a/eacl/record.go b/eacl/record.go index 0762ef3..eeb9646 100644 --- a/eacl/record.go +++ b/eacl/record.go @@ -125,7 +125,7 @@ func (r *Record) AddObjectContainerIDFilter(m Match, id cid.ID) { } // AddObjectOwnerIDFilter adds filter by object owner ID. -func (r *Record) AddObjectOwnerIDFilter(m Match, id *user.ID) { +func (r *Record) AddObjectOwnerIDFilter(m Match, id user.ID) { r.addObjectReservedFilter(m, fKeyObjOwnerID, id) } diff --git a/object/object.go b/object/object.go index b21cd69..2685a24 100644 --- a/object/object.go +++ b/object/object.go @@ -39,7 +39,7 @@ type RequiredFields struct { // Object is expected (but not required) to be blank. Object must not be nil. func InitCreation(dst *Object, rf RequiredFields) { dst.SetContainerID(rf.Container) - dst.SetOwnerID(&rf.Owner) + dst.SetOwnerID(rf.Owner) } // NewFromV2 wraps v2 Object message to Object. @@ -201,8 +201,8 @@ func (o *Object) SetContainerID(v cid.ID) { }) } -// OwnerID returns identifier of the object owner. -func (o *Object) OwnerID() *user.ID { +// OwnerID returns identifier of the object owner and True. +func (o *Object) OwnerID() user.ID { var id user.ID m := (*object.Object)(o).GetHeader().GetOwnerID() @@ -210,11 +210,11 @@ func (o *Object) OwnerID() *user.ID { _ = id.ReadFromV2(*m) } - return &id + return id } // SetOwnerID sets identifier of the object owner. -func (o *Object) SetOwnerID(v *user.ID) { +func (o *Object) SetOwnerID(v user.ID) { o.setHeaderField(func(h *object.Header) { var m refs.OwnerID v.WriteToV2(&m) @@ -621,8 +621,10 @@ func (o *Object) UnmarshalJSON(data []byte) error { return formatCheck((*object.Object)(o)) } -var errOIDNotSet = errors.New("object ID is not set") -var errCIDNotSet = errors.New("container ID is not set") +var ( + errOIDNotSet = errors.New("object ID is not set") + errCIDNotSet = errors.New("container ID is not set") +) func formatCheck(v2 *object.Object) error { var ( diff --git a/object/object_test.go b/object/object_test.go index 61b5521..5e42352 100644 --- a/object/object_test.go +++ b/object/object_test.go @@ -12,7 +12,7 @@ import ( func TestInitCreation(t *testing.T) { var o object.Object cnr := cidtest.ID() - own := *usertest.ID() + own := usertest.ID() object.InitCreation(&o, object.RequiredFields{ Container: cnr, @@ -22,5 +22,5 @@ func TestInitCreation(t *testing.T) { cID, set := o.ContainerID() require.True(t, set) require.Equal(t, cnr, cID) - require.Equal(t, &own, o.OwnerID()) + require.Equal(t, own, o.OwnerID()) } diff --git a/object/transformer/size_hint_test.go b/object/transformer/size_hint_test.go index b0965b1..69a3908 100644 --- a/object/transformer/size_hint_test.go +++ b/object/transformer/size_hint_test.go @@ -34,7 +34,7 @@ func TestTransformerSizeHintCorrectness(t *testing.T) { var owner user.ID user.IDFromKey(&owner, pk.PrivateKey.PublicKey) - hdr.SetOwnerID(&owner) + hdr.SetOwnerID(owner) expected := make([]byte, payloadSize) _, _ = rand.Read(expected) diff --git a/object/transformer/transformer_test.go b/object/transformer/transformer_test.go index f1a3280..97b2b01 100644 --- a/object/transformer/transformer_test.go +++ b/object/transformer/transformer_test.go @@ -27,7 +27,7 @@ func TestTransformer(t *testing.T) { var owner user.ID user.IDFromKey(&owner, pk.PrivateKey.PublicKey) - hdr.SetOwnerID(&owner) + hdr.SetOwnerID(owner) expectedPayload := make([]byte, maxSize*2+maxSize/2) _, _ = rand.Read(expectedPayload) @@ -41,7 +41,7 @@ func TestTransformer(t *testing.T) { require.True(t, ok) require.Equal(t, cnr, childCnr) require.Equal(t, objectSDK.TypeRegular, tt.objects[i].Type()) - require.Equal(t, &owner, tt.objects[i].OwnerID()) + require.Equal(t, owner, tt.objects[i].OwnerID()) payload := tt.objects[i].Payload() require.EqualValues(t, tt.objects[i].PayloadSize(), len(payload)) diff --git a/pool/pool.go b/pool/pool.go index a656332..48f2ee1 100644 --- a/pool/pool.go +++ b/pool/pool.go @@ -422,7 +422,7 @@ func (c *clientWrapper) balanceGet(ctx context.Context, prm PrmBalanceGet) (acco } cliPrm := sdkClient.PrmBalanceGet{ - Account: &prm.account, + Account: prm.account, } start := time.Now() @@ -508,7 +508,7 @@ func (c *clientWrapper) containerList(ctx context.Context, prm PrmContainerList) } cliPrm := sdkClient.PrmContainerList{ - Account: &prm.ownerID, + Account: prm.ownerID, } start := time.Now() diff --git a/session/container_test.go b/session/container_test.go index d3d4662..d906482 100644 --- a/session/container_test.go +++ b/session/container_test.go @@ -37,7 +37,7 @@ func TestContainerProtocolV2(t *testing.T) { restoreID() // Owner - usr := *usertest.ID() + usr := usertest.ID() var usrV2 refs.OwnerID usr.WriteToV2(&usrV2) restoreUser := func() { diff --git a/session/object_test.go b/session/object_test.go index 7d1d77b..1df0f76 100644 --- a/session/object_test.go +++ b/session/object_test.go @@ -53,7 +53,7 @@ func TestObjectProtocolV2(t *testing.T) { restoreID() // Owner - usr := *usertest.ID() + usr := usertest.ID() var usrV2 refs.OwnerID usr.WriteToV2(&usrV2) restoreUser := func() { diff --git a/user/id.go b/user/id.go index b77492f..8535b01 100644 --- a/user/id.go +++ b/user/id.go @@ -12,6 +12,10 @@ import ( "github.com/nspcc-dev/neo-go/pkg/util" ) +const idSize = 25 + +var zeroSlice = bytes.Repeat([]byte{0}, idSize) + // ID identifies users of the FrostFS system. // // ID is mutually compatible with git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs.OwnerID @@ -30,8 +34,8 @@ type ID struct { // See also WriteToV2. func (x *ID) ReadFromV2(m refs.OwnerID) error { w := m.GetValue() - if len(w) != 25 { - return fmt.Errorf("invalid length %d, expected 25", len(w)) + if len(w) != idSize { + return fmt.Errorf("invalid length %d, expected %d", len(w), idSize) } if w[0] != address.NEO3Prefix { @@ -57,10 +61,10 @@ func (x ID) WriteToV2(m *refs.OwnerID) { // SetScriptHash forms user ID from wallet address scripthash. func (x *ID) SetScriptHash(scriptHash util.Uint160) { - if cap(x.w) < 25 { - x.w = make([]byte, 25) - } else if len(x.w) < 25 { - x.w = x.w[:25] + if cap(x.w) < idSize { + x.w = make([]byte, idSize) + } else if len(x.w) < idSize { + x.w = x.w[:idSize] } x.w[0] = address.Prefix @@ -114,3 +118,8 @@ func (x ID) String() string { func (x ID) Equals(x2 ID) bool { return bytes.Equal(x.w, x2.w) } + +// IsEmpty returns True, if ID is empty value. +func (x ID) IsEmpty() bool { + return bytes.Equal(zeroSlice, x.w) +} diff --git a/user/id_test.go b/user/id_test.go index 30e19db..6b1d0f8 100644 --- a/user/id_test.go +++ b/user/id_test.go @@ -48,7 +48,7 @@ func TestID_SetScriptHash(t *testing.T) { } func TestV2_ID(t *testing.T) { - id := *usertest.ID() + id := usertest.ID() var m refs.OwnerID var id2 ID @@ -91,7 +91,7 @@ func TestV2_ID(t *testing.T) { } func TestID_EncodeToString(t *testing.T) { - id := *usertest.ID() + id := usertest.ID() s := id.EncodeToString() @@ -110,8 +110,8 @@ func TestID_EncodeToString(t *testing.T) { } func TestID_Equal(t *testing.T) { - id1 := *usertest.ID() - id2 := *usertest.ID() + id1 := usertest.ID() + id2 := usertest.ID() id3 := id1 require.True(t, id1.Equals(id1)) // self-equality diff --git a/user/test/id.go b/user/test/id.go index dd21405..b4d8f9c 100644 --- a/user/test/id.go +++ b/user/test/id.go @@ -6,7 +6,7 @@ import ( ) // ID returns random user.ID. -func ID() *user.ID { +func ID() user.ID { key, err := keys.NewPrivateKey() if err != nil { panic(err) @@ -15,5 +15,5 @@ func ID() *user.ID { var x user.ID user.IDFromKey(&x, key.PrivateKey.PublicKey) - return &x + return x }