Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move object operations to Object structure #457

Merged
merged 1 commit into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 29 additions & 29 deletions object/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ func CalculatePayloadChecksum(payload []byte) checksum.Checksum {

// CalculateAndSetPayloadChecksum calculates checksum of current
// object payload and writes it to the object.
func CalculateAndSetPayloadChecksum(obj *Object) {
obj.SetPayloadChecksum(
CalculatePayloadChecksum(obj.Payload()),
func (o *Object) CalculateAndSetPayloadChecksum() {
o.SetPayloadChecksum(
CalculatePayloadChecksum(o.Payload()),
)
}

// VerifyPayloadChecksum checks if payload checksum in the object
// corresponds to its payload.
func VerifyPayloadChecksum(obj *Object) error {
actual := CalculatePayloadChecksum(obj.Payload())
func (o *Object) VerifyPayloadChecksum() error {
actual := CalculatePayloadChecksum(o.Payload())

cs, set := obj.PayloadChecksum()
cs, set := o.PayloadChecksum()
if !set {
return errCheckSumNotSet
}
Expand All @@ -53,35 +53,35 @@ func VerifyPayloadChecksum(obj *Object) error {
}

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

return id, nil
}

// CalculateAndSetID calculates identifier for the object
// and writes the result to it.
func CalculateAndSetID(obj *Object) error {
id, err := CalculateID(obj)
func (o *Object) CalculateAndSetID() error {
id, err := o.CalculateID()
if err != nil {
return err
}

obj.SetID(id)
o.SetID(id)

return nil
}

// VerifyID checks if identifier in the object corresponds to
// its structure.
func VerifyID(obj *Object) error {
id, err := CalculateID(obj)
func (o *Object) VerifyID() error {
id, err := o.CalculateID()
if err != nil {
return err
}

oID, set := obj.ID()
oID, set := o.ID()
if !set {
return errOIDNotSet
}
Expand All @@ -95,8 +95,8 @@ func VerifyID(obj *Object) error {

// CalculateAndSetSignature signs id with provided key and sets that signature to
// the object.
func CalculateAndSetSignature(signer neofscrypto.Signer, obj *Object) error {
oID, set := obj.ID()
func (o *Object) CalculateAndSetSignature(signer neofscrypto.Signer) error {
oID, set := o.ID()
if !set {
return errOIDNotSet
}
Expand All @@ -106,7 +106,7 @@ func CalculateAndSetSignature(signer neofscrypto.Signer, obj *Object) error {
return err
}

obj.SetSignature(&sig)
o.SetSignature(&sig)

return nil
}
Expand All @@ -131,32 +131,32 @@ func (o *Object) VerifyIDSignature() bool {
}

// SetIDWithSignature sets object identifier and signature.
func SetIDWithSignature(signer neofscrypto.Signer, obj *Object) error {
if err := CalculateAndSetID(obj); err != nil {
func (o *Object) SetIDWithSignature(signer neofscrypto.Signer) error {
if err := o.CalculateAndSetID(); err != nil {
return fmt.Errorf("could not set identifier: %w", err)
}

if err := CalculateAndSetSignature(signer, obj); err != nil {
if err := o.CalculateAndSetSignature(signer); err != nil {
return fmt.Errorf("could not set signature: %w", err)
}

return nil
}

// SetVerificationFields calculates and sets all verification fields of the object.
func SetVerificationFields(signer neofscrypto.Signer, obj *Object) error {
CalculateAndSetPayloadChecksum(obj)
func (o *Object) SetVerificationFields(signer neofscrypto.Signer) error {
o.CalculateAndSetPayloadChecksum()

return SetIDWithSignature(signer, obj)
return o.SetIDWithSignature(signer)
}

// CheckVerificationFields checks all verification fields of the object.
func CheckVerificationFields(obj *Object) error {
if err := CheckHeaderVerificationFields(obj); err != nil {
func (o *Object) CheckVerificationFields() error {
if err := o.CheckHeaderVerificationFields(); err != nil {
return fmt.Errorf("invalid header structure: %w", err)
}

if err := VerifyPayloadChecksum(obj); err != nil {
if err := o.VerifyPayloadChecksum(); err != nil {
return fmt.Errorf("invalid payload checksum: %w", err)
}

Expand All @@ -166,12 +166,12 @@ func CheckVerificationFields(obj *Object) error {
var errInvalidSignature = errors.New("invalid signature")

// CheckHeaderVerificationFields checks all verification fields except payload.
func CheckHeaderVerificationFields(obj *Object) error {
if !obj.VerifyIDSignature() {
func (o *Object) CheckHeaderVerificationFields() error {
if !o.VerifyIDSignature() {
return errInvalidSignature
}

if err := VerifyID(obj); err != nil {
if err := o.VerifyID(); err != nil {
return fmt.Errorf("invalid identifier: %w", err)
}

Expand Down
8 changes: 4 additions & 4 deletions object/fmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ func TestVerificationFields(t *testing.T) {
obj.SetPayload(payload)
obj.SetPayloadSize(uint64(len(payload)))

require.NoError(t, SetVerificationFields(test.RandomSigner(t), obj))
require.NoError(t, obj.SetVerificationFields(test.RandomSigner(t)))

require.NoError(t, CheckVerificationFields(obj))
require.NoError(t, obj.CheckVerificationFields())

items := []struct {
corrupt func()
Expand Down Expand Up @@ -54,10 +54,10 @@ func TestVerificationFields(t *testing.T) {
for _, item := range items {
item.corrupt()

require.Error(t, CheckVerificationFields(obj))
require.Error(t, obj.CheckVerificationFields())

item.restore()

require.NoError(t, CheckVerificationFields(obj))
require.NoError(t, obj.CheckVerificationFields())
}
}
11 changes: 5 additions & 6 deletions object/lock.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,20 @@ import (
type Lock v2object.Lock

// WriteLock writes Lock to the Object, and sets its type to TypeLock.
// The object must not be nil.
//
// See also ReadLock.
func WriteLock(obj *Object, l Lock) {
obj.SetType(TypeLock)
obj.SetPayload(l.Marshal())
func (o *Object) WriteLock(l Lock) {
o.SetType(TypeLock)
o.SetPayload(l.Marshal())
}

// ReadLock reads Lock from the Object. The lock must not be nil.
// Returns an error describing incorrect format. Makes sense only
// if object has TypeLock type.
//
// See also WriteLock.
func ReadLock(l *Lock, obj Object) error {
return l.Unmarshal(obj.Payload())
func (o *Object) ReadLock(l *Lock) error {
return l.Unmarshal(o.Payload())
}

// NumberOfMembers returns number of members in lock list.
Expand Down
6 changes: 3 additions & 3 deletions object/lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ func TestWriteLock(t *testing.T) {
l := *objecttest.Lock()
var o object.Object

object.WriteLock(&o, l)
o.WriteLock(l)

var l2 object.Lock

require.NoError(t, object.ReadLock(&l2, o))
require.NoError(t, o.ReadLock(&l2))
require.Equal(t, l, l2)

// corrupt payload
o.Payload()[0]++

require.Error(t, object.ReadLock(&l2, o))
require.Error(t, o.ReadLock(&l2))
}
7 changes: 3 additions & 4 deletions object/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ type RequiredFields struct {
}

// InitCreation initializes the object instance with minimum set of required fields.
// 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)
func (o *Object) InitCreation(rf RequiredFields) {
o.SetContainerID(rf.Container)
o.SetOwnerID(&rf.Owner)
}

// NewFromV2 wraps v2 Object message to Object.
Expand Down
2 changes: 1 addition & 1 deletion object/object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestInitCreation(t *testing.T) {
cnr := cidtest.ID()
own := *usertest.ID(t)

object.InitCreation(&o, object.RequiredFields{
o.InitCreation(object.RequiredFields{
Container: cnr,
Owner: own,
})
Expand Down
2 changes: 1 addition & 1 deletion object/slicer/slicer.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ func flushObjectMetadata(signer neofscrypto.Signer, meta dynamicObjectMetadata,

header.SetPayloadSize(meta.length)

id, err := object.CalculateID(header)
id, err := header.CalculateID()
if err != nil {
return id, fmt.Errorf("calculate ID: %w", err)
}
Expand Down
6 changes: 3 additions & 3 deletions object/slicer/slicer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ func checkStaticMetadata(tb testing.TB, header object.Object, in input) {
require.EqualValues(tb, in.currentEpoch, header.CreationEpoch(), "configured current epoch must be set as creation epoch")
require.Equal(tb, in.sessionToken, header.SessionToken(), "configured session token must be written into objects")

require.NoError(tb, object.CheckHeaderVerificationFields(&header), "verification fields must be correctly set in header")
require.NoError(tb, header.CheckHeaderVerificationFields(), "verification fields must be correctly set in header")

_, ok = header.PayloadHomomorphicHash()
require.Equal(tb, in.withHomo, ok)
Expand All @@ -373,7 +373,7 @@ func (x *chainCollector) handleOutgoingObject(header object.Object, payload io.R
id, ok := header.ID()
require.True(x.tb, ok, "all objects must have an ID")

idCalc, err := object.CalculateID(&header)
idCalc, err := header.CalculateID()
require.NoError(x.tb, err)

require.True(x.tb, idCalc.Equals(id))
Expand Down Expand Up @@ -503,5 +503,5 @@ func (x *chainCollector) verify(in input, rootID oid.ID) {
}

require.Equal(x.tb, in.payload, rootObj.Payload())
require.NoError(x.tb, object.VerifyPayloadChecksum(&rootObj), "payload checksum must be correctly set")
require.NoError(x.tb, rootObj.VerifyPayloadChecksum(), "payload checksum must be correctly set")
}