Skip to content

Commit

Permalink
*: Affirm IDs as arrays
Browse files Browse the repository at this point in the history
This continues cde122e and finally
commits to supporting identifiers as fixed-length byte arrays. Since
their format has not changed so far, this is unlikely to happen in the
near foreseeable future. The benefit is the user's code simplification
code and the abandonment of some no longer needed methods (they are
marked deprecated).

Signed-off-by: Leonard Lyubich <leonard@morphbits.io>
  • Loading branch information
cthulhu-rider committed Jul 16, 2024
1 parent b2939ba commit 8b416e5
Show file tree
Hide file tree
Showing 13 changed files with 28 additions and 67 deletions.
2 changes: 1 addition & 1 deletion container/container_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestContainer_CopyTo(t *testing.T) {
require.True(t, container.Owner() == dst.Owner())

newOwner := usertest.ID()
dst.v2.GetOwnerID().SetValue(newOwner.WalletBytes())
dst.v2.GetOwnerID().SetValue(newOwner[:])

require.False(t, container.Owner() == dst.Owner())
})
Expand Down
10 changes: 3 additions & 7 deletions container/id/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ const Size = sha256.Size
// message. See ReadFromV2 / WriteToV2 methods.
//
// Instances can be created using built-in var declaration.
//
// Note that direct typecast is not safe and may result in loss of compatibility:
//
// _ = ID([32]byte) // not recommended
type ID [Size]byte

// ReadFromV2 reads ID from the refs.ContainerID message.
Expand All @@ -48,6 +44,7 @@ func (id ID) WriteToV2(m *refs.ContainerID) {
// Zero ID is all zeros.
//
// See also Decode.
// Deprecated: use id[:] instead.
func (id ID) Encode(dst []byte) {
if l := len(dst); l < Size {
panic(fmt.Sprintf("destination length is less than %d bytes: %d", Size, l))
Expand All @@ -63,8 +60,6 @@ func (id ID) Encode(dst []byte) {
// remains unchanged.
//
// Decode doesn't mutate src.
//
// See also Encode.
func (id *ID) Decode(src []byte) error {
if len(src) != Size {
return fmt.Errorf("invalid length %d", len(src))
Expand All @@ -76,6 +71,7 @@ func (id *ID) Decode(src []byte) error {
}

// SetSHA256 sets container identifier value to SHA256 checksum of container structure.
// Deprecated: use direct assignment instead.
func (id *ID) SetSHA256(v [sha256.Size]byte) {
*id = v
}
Expand Down Expand Up @@ -125,5 +121,5 @@ func (id ID) String() string {
//
// See also [container.Container.CalculateID], [container.Container.AssertID].
func (id *ID) FromBinary(cnr []byte) {
id.SetSHA256(sha256.Sum256(cnr))
*id = sha256.Sum256(cnr)
}
5 changes: 1 addition & 4 deletions container/id/test/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,5 @@ loop:
// with specified checksum.
// Deprecated: use [ID], [OtherID] or manual creation instead.
func IDWithChecksum(cs [sha256.Size]byte) cid.ID {
var id cid.ID
id.SetSHA256(cs)

return id
return cs
}
5 changes: 1 addition & 4 deletions eacl/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,7 @@ func NewTableFromV2(table *v2acl.Table) *Table {
t.cid = new(cid.ID)
}

var h [cid.Size]byte

copy(h[:], id.GetValue())
t.cid.SetSHA256(h)
copy(t.cid[:], id.GetValue())
}

// set eacl records
Expand Down
3 changes: 2 additions & 1 deletion netmap/netmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ func flattenNodes(ns []nodes) nodes {
// the fixed NetMap and parameters.
func (m NetMap) PlacementVectors(vectors [][]NodeInfo, objectID oid.ID) ([][]NodeInfo, error) {
pivot := make([]byte, oid.Size)
copy(pivot, objectID[:])
objectID.Encode(pivot)

h := hrw.WrapBytes(pivot)
Expand Down Expand Up @@ -180,7 +181,7 @@ func (m NetMap) ContainerNodes(p PlacementPolicy, containerID cid.ID) ([][]NodeI
c.setCBF(p.backupFactor)

pivot := make([]byte, cid.Size)
containerID.Encode(pivot)
copy(pivot, containerID[:])
c.setPivot(pivot)

if err := c.processFilters(p); err != nil {
Expand Down
5 changes: 1 addition & 4 deletions object/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,7 @@ func (o *Object) VerifyPayloadChecksum() error {

// CalculateID calculates identifier for the object.
func (o *Object) CalculateID() (oid.ID, error) {
var id oid.ID
id.SetSHA256(sha256.Sum256(o.ToV2().GetHeader().StableMarshal(nil)))

return id, nil
return sha256.Sum256(o.ToV2().GetHeader().StableMarshal(nil)), nil
}

// CalculateAndSetID calculates identifier for the object
Expand Down
10 changes: 3 additions & 7 deletions object/id/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ const Size = sha256.Size
// message. See ReadFromV2 / WriteToV2 methods.
//
// Instances can be created using built-in var declaration.
//
// Note that direct typecast is not safe and may result in loss of compatibility:
//
// _ = ID([32]byte{}) // not recommended
type ID [Size]byte

// ReadFromV2 reads ID from the refs.ObjectID message. Returns an error if
Expand All @@ -48,6 +44,7 @@ func (id ID) WriteToV2(m *refs.ObjectID) {
// Zero ID is all zeros.
//
// See also Decode.
// Deprecated: use id[:] instead.
func (id ID) Encode(dst []byte) {
if l := len(dst); l < Size {
panic(fmt.Sprintf("destination length is less than %d bytes: %d", Size, l))
Expand All @@ -58,13 +55,11 @@ func (id ID) Encode(dst []byte) {

// Decode decodes src bytes into ID.
//
// Decode expects that src has [Size] bytes length. If the input is malformed,
// Decode expects that src has [IDSize] bytes length. If the input is malformed,
// Decode returns an error describing format violation. In this case ID
// remains unchanged.
//
// Decode doesn't mutate src.
//
// See also Encode.
func (id *ID) Decode(src []byte) error {
if len(src) != Size {
return fmt.Errorf("invalid length %d", len(src))
Expand All @@ -76,6 +71,7 @@ func (id *ID) Decode(src []byte) error {
}

// SetSHA256 sets object identifier value to SHA256 checksum.
// Deprecated: use direct assignment instead.
func (id *ID) SetSHA256(v [sha256.Size]byte) {
*id = v
}
Expand Down
21 changes: 5 additions & 16 deletions object/id/id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,19 @@ func randSHA256Checksum(t *testing.T) (cs [sha256.Size]byte) {
func TestIDV2(t *testing.T) {
var id oid.ID

checksum := [sha256.Size]byte{}

_, err := rand.Read(checksum[:])
_, err := rand.Read(id[:])
require.NoError(t, err)

id.SetSHA256(checksum)

var idV2 refs.ObjectID
id.WriteToV2(&idV2)

require.Equal(t, checksum[:], idV2.GetValue())
require.Equal(t, id[:], idV2.GetValue())
}

func TestID_Equal(t *testing.T) {
cs := randSHA256Checksum(t)

var id1 oid.ID
id1.SetSHA256(cs)

var id2 oid.ID
id2.SetSHA256(cs)

var id3 oid.ID
id3.SetSHA256(randSHA256Checksum(t))
id1 := oidtest.ID()
id2 := id1
id3 := oidtest.OtherID(id1)

require.True(t, id1.Equals(id2))
require.False(t, id1.Equals(id3))
Expand Down
6 changes: 4 additions & 2 deletions session/container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,10 @@ func TestContainerProtocolV2(t *testing.T) {
require.Equal(t, usrID, val.Issuer())
},
breakSign: func(m *v2session.Token) {
id := m.GetBody().GetOwnerID().GetValue()
copy(id, usertest.ID().WalletBytes())
otherUsr := usertest.OtherID(usrID)
var mID refs.OwnerID
otherUsr.WriteToV2(&mID)
m.GetBody().SetOwnerID(&mID)
},
},
{
Expand Down
6 changes: 4 additions & 2 deletions session/object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,10 @@ func TestObjectProtocolV2(t *testing.T) {
require.Equal(t, usrID, val.Issuer())
},
breakSign: func(m *v2session.Token) {
id := m.GetBody().GetOwnerID().GetValue()
copy(id, usertest.ID().WalletBytes())
otherUsr := usertest.OtherID(usrID)
var mID refs.OwnerID
otherUsr.WriteToV2(&mID)
m.GetBody().SetOwnerID(&mID)
},
},
{
Expand Down
11 changes: 0 additions & 11 deletions user/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,6 @@ func ExampleID_SetScriptHash() {
id.SetScriptHash(scriptHash)
}

// ID is compatible with the NeoFS Smart Contract API.
func ExampleID_WalletBytes() {
var id user.ID
// ...

wallet := id.WalletBytes()
_ = wallet

// use wallet in call
}

// Encoding mechanisms are used to transfer identifiers on client.
func ExampleID_EncodeToString() {
var id user.ID
Expand Down
3 changes: 2 additions & 1 deletion user/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
// IDSize is the size of an [ID] in bytes.
const IDSize = 25

// ID identifies users of the NeoFS system.
// ID identifies users of the NeoFS system and represents Neo3 account address.
//
// ID implements built-in comparable interface.
//
Expand Down Expand Up @@ -68,6 +68,7 @@ func (x *ID) SetScriptHash(scriptHash util.Uint160) {
// Make a copy if you need to change it.
//
// See also Neo3 wallet docs.
// Deprecated: use x[:] instead.
func (x ID) WalletBytes() []byte {
return x[:]
}
Expand Down
8 changes: 1 addition & 7 deletions user/id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,7 @@ func TestV2_ID(t *testing.T) {
func TestID_EncodeToString(t *testing.T) {
const s = "NXWcEedga62wcBmfb9dPwar3vbbZJrMtT1"
b := [user.IDSize]byte{53, 127, 54, 116, 58, 70, 206, 247, 185, 103, 214, 89, 184, 42, 40, 234, 173, 68, 209, 25, 168, 14, 134, 47, 224}
var id user.ID

var m refs.OwnerID
m.SetValue(b[:])
require.NoError(t, id.ReadFromV2(m))

require.Equal(t, s, id.EncodeToString())
require.Equal(t, s, user.ID(b).EncodeToString())
}

func TestID_DecodeString(t *testing.T) {
Expand Down

0 comments on commit 8b416e5

Please sign in to comment.