user: Refactor user.ID structure #323

Merged
fyrchik merged 2 commits from elebedeva/frostfs-sdk-go:fix/user-id-scripthash into master 2025-01-29 10:36:03 +00:00
Member

Close #302, #303

Changed user.ID.w field type to util.Uint160. util.Uint160 is a byte array of size 20.

Signed-off-by: Ekaterina Lebedeva ekaterina.lebedeva@yadro.com

Close #302, #303 Changed `user.ID.w` field type to `util.Uint160`. `util.Uint160` is a byte array of size 20. Signed-off-by: Ekaterina Lebedeva <ekaterina.lebedeva@yadro.com>
elebedeva added the
refactoring
internal
labels 2025-01-27 16:34:48 +00:00
elebedeva added 2 commits 2025-01-27 16:34:49 +00:00
Signed-off-by: Ekaterina Lebedeva <ekaterina.lebedeva@yadro.com>
[#303] user: Make user.ID a [20]byte array
All checks were successful
DCO / DCO (pull_request) Successful in 27s
Code generation / Generate proto (pull_request) Successful in 33s
Tests and linters / Tests (pull_request) Successful in 49s
Tests and linters / Lint (pull_request) Successful in 2m7s
873f0896ca
Signed-off-by: Ekaterina Lebedeva <ekaterina.lebedeva@yadro.com>
elebedeva force-pushed fix/user-id-scripthash from 873f0896ca to 81c3d4a86f 2025-01-28 08:05:34 +00:00 Compare
elebedeva changed title from WIP: user: Refactor user.ID structure to user: Refactor user.ID structure 2025-01-28 08:09:17 +00:00
elebedeva requested review from storage-services-committers 2025-01-28 08:09:17 +00:00
elebedeva requested review from storage-services-developers 2025-01-28 08:09:17 +00:00
elebedeva requested review from storage-core-committers 2025-01-28 08:09:18 +00:00
elebedeva requested review from storage-core-developers 2025-01-28 08:09:18 +00:00
a-savchuk approved these changes 2025-01-28 09:54:11 +00:00
Dismissed
achuprov approved these changes 2025-01-28 09:55:56 +00:00
Dismissed
fyrchik reviewed 2025-01-28 10:27:11 +00:00
user/id.go Outdated
@ -76,2 +59,2 @@
func (x *ID) ScriptHash() (util.Uint160, error) {
return util.Uint160DecodeBytesBE(x.w[1:21])
func (x *ID) ScriptHash() util.Uint160 {
res, _ := util.Uint160DecodeBytesBE(x.w[:])
Owner

Doesn't simple util.Uint160(x.w) work?

Doesn't simple `util.Uint160(x.w)` work?
Owner

I would argue we can even make const idSize = util.Uint160Size and struct { w util.Uint160 }

I would argue we can even make `const idSize = util.Uint160Size` and `struct { w util.Uint160 }`
Author
Member

It does, thanks! Fixed.

It does, thanks! Fixed.
fyrchik marked this conversation as resolved
fyrchik reviewed 2025-01-28 10:28:18 +00:00
user/id.go Outdated
@ -127,3 +112,3 @@
// IsEmpty returns True, if ID is empty value.
func (x ID) IsEmpty() bool {
return bytes.Equal(zeroSlice, x.w)
return bytes.Equal(zeroSlice, x.w[:])
Owner

Doesn't x.w == util.Uint160{} work?

Doesn't `x.w == util.Uint160{}` work?
Author
Member

Fixed.

Fixed.
fyrchik marked this conversation as resolved
elebedeva force-pushed fix/user-id-scripthash from 81c3d4a86f to 00cebd297f 2025-01-28 15:02:49 +00:00 Compare
elebedeva dismissed a-savchuk's review 2025-01-28 15:02:52 +00:00
Reason:

New commits pushed, approval review dismissed automatically according to repository settings

elebedeva dismissed achuprov's review 2025-01-28 15:02:52 +00:00
Reason:

New commits pushed, approval review dismissed automatically according to repository settings

fyrchik approved these changes 2025-01-29 10:24:05 +00:00
@ -84,3 +60,3 @@
// See also Neo3 wallet docs.
func (x ID) WalletBytes() []byte {
return x.w
v := make([]byte, idFullSize)
Owner

The comment about MUST NOT be mutated can be removed now.

The comment about `MUST NOT be mutated` can be removed now.
@ -91,3 +71,3 @@
// See also DecodeString.
func (x ID) EncodeToString() string {
return base58.Encode(x.w)
return base58.Encode(x.WalletBytes())
Owner

EncodeToString() makes 3 allocations. One of them (inside WalletBytes()) can be avoided with little cost.

$ go build -gcflags "-m -l" ./user
...
user/id.go:62:11: make([]byte, 25) escapes to heap

One way to achieve this is to move encoding to a function (ID).encode([]byte).
Both WalletBytes() and EncodeToString() methods will create new slice and encode to it.

`EncodeToString()` makes 3 allocations. One of them (inside `WalletBytes()`) can be avoided with little cost. ``` $ go build -gcflags "-m -l" ./user ... user/id.go:62:11: make([]byte, 25) escapes to heap ``` One way to achieve this is to move encoding to a function `(ID).encode([]byte)`. Both `WalletBytes()` and `EncodeToString()` methods will create new slice and `encode` to it.
Owner

We can do this in another PR, though.

We can do this in another PR, though.
fyrchik merged commit 00cebd297f into master 2025-01-29 10:36:03 +00:00
Sign in to join this conversation.
No reviewers
TrueCloudLab/storage-services-committers
TrueCloudLab/storage-services-developers
No milestone
No project
No assignees
4 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: TrueCloudLab/frostfs-sdk-go#323
No description provided.