From 94f068e462fe249d1633f5913a3f84680007b721 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Tue, 5 Apr 2022 11:24:34 +0300 Subject: [PATCH] *: remove error from `StableMarshal` return values We marshal only in-memory structures, no error is expected. Signed-off-by: Evgenii Stratonikov --- accounting/marshal.go | 28 +- acl/marshal.go | 132 +++------- audit/marshal.go | 45 +--- container/marshal.go | 288 ++++++-------------- netmap/marshal.go | 175 ++++--------- object/lock.go | 22 +- object/marshal.go | 564 ++++++++++------------------------------ refs/bench_test.go | 4 +- refs/marshal.go | 78 +++--- reputation/marshal.go | 127 +++------ session/marshal.go | 280 +++++--------------- signature/sign.go | 4 +- status/marshal.go | 33 +-- storagegroup/marshal.go | 26 +- subnet/info.go | 24 +- tombstone/marshal.go | 18 +- util/proto/marshal.go | 14 +- 17 files changed, 504 insertions(+), 1358 deletions(-) diff --git a/accounting/marshal.go b/accounting/marshal.go index 0d908c0..f5265b1 100644 --- a/accounting/marshal.go +++ b/accounting/marshal.go @@ -15,9 +15,9 @@ const ( balanceRespBodyDecimalField = 1 ) -func (d *Decimal) StableMarshal(buf []byte) ([]byte, error) { +func (d *Decimal) StableMarshal(buf []byte) []byte { if d == nil { - return []byte{}, nil + return []byte{} } if buf == nil { @@ -29,7 +29,7 @@ func (d *Decimal) StableMarshal(buf []byte) ([]byte, error) { offset += protoutil.Int64Marshal(decimalValueField, buf[offset:], d.val) protoutil.UInt32Marshal(decimalPrecisionField, buf[offset:], d.prec) - return buf, nil + return buf } func (d *Decimal) StableSize() (size int) { @@ -47,21 +47,18 @@ func (d *Decimal) Unmarshal(data []byte) error { return message.Unmarshal(d, data, new(accounting.Decimal)) } -func (b *BalanceRequestBody) StableMarshal(buf []byte) ([]byte, error) { +func (b *BalanceRequestBody) StableMarshal(buf []byte) []byte { if b == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, b.StableSize()) } - _, err := protoutil.NestedStructureMarshal(balanceReqBodyOwnerField, buf, b.ownerID) - if err != nil { - return nil, err - } + protoutil.NestedStructureMarshal(balanceReqBodyOwnerField, buf, b.ownerID) - return buf, nil + return buf } func (b *BalanceRequestBody) StableSize() (size int) { @@ -78,21 +75,18 @@ func (b *BalanceRequestBody) Unmarshal(data []byte) error { return message.Unmarshal(b, data, new(accounting.BalanceRequest_Body)) } -func (br *BalanceResponseBody) StableMarshal(buf []byte) ([]byte, error) { +func (br *BalanceResponseBody) StableMarshal(buf []byte) []byte { if br == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, br.StableSize()) } - _, err := protoutil.NestedStructureMarshal(balanceRespBodyDecimalField, buf, br.bal) - if err != nil { - return nil, err - } + protoutil.NestedStructureMarshal(balanceRespBodyDecimalField, buf, br.bal) - return buf, nil + return buf } func (br *BalanceResponseBody) StableSize() (size int) { diff --git a/acl/marshal.go b/acl/marshal.go index 11c6174..87d476d 100644 --- a/acl/marshal.go +++ b/acl/marshal.go @@ -38,44 +38,25 @@ const ( // StableMarshal marshals unified acl table structure in a protobuf // compatible way without field order shuffle. -func (t *Table) StableMarshal(buf []byte) ([]byte, error) { +func (t *Table) StableMarshal(buf []byte) []byte { if t == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, t.StableSize()) } - var ( - offset, n int - err error - ) + var offset int - n, err = protoutil.NestedStructureMarshal(tableVersionField, buf[offset:], t.version) - if err != nil { - return nil, err - } - - offset += n - - n, err = protoutil.NestedStructureMarshal(tableContainerIDField, buf[offset:], t.cid) - if err != nil { - return nil, err - } - - offset += n + offset += protoutil.NestedStructureMarshal(tableVersionField, buf[offset:], t.version) + offset += protoutil.NestedStructureMarshal(tableContainerIDField, buf[offset:], t.cid) for i := range t.records { - n, err = protoutil.NestedStructureMarshal(tableRecordsField, buf[offset:], &t.records[i]) - if err != nil { - return nil, err - } - - offset += n + offset += protoutil.NestedStructureMarshal(tableRecordsField, buf[offset:], &t.records[i]) } - return buf, nil + return buf } // StableSize of acl table structure marshalled by StableMarshal function. @@ -100,42 +81,29 @@ func (t *Table) Unmarshal(data []byte) error { // StableMarshal marshals unified acl record structure in a protobuf // compatible way without field order shuffle. -func (r *Record) StableMarshal(buf []byte) ([]byte, error) { +func (r *Record) StableMarshal(buf []byte) []byte { if r == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, r.StableSize()) } - var ( - offset, n int - err error - ) + var offset int offset += protoutil.EnumMarshal(recordOperationField, buf[offset:], int32(r.op)) offset += protoutil.EnumMarshal(recordActionField, buf[offset:], int32(r.action)) for i := range r.filters { - n, err = protoutil.NestedStructureMarshal(recordFiltersField, buf[offset:], &r.filters[i]) - if err != nil { - return nil, err - } - - offset += n + offset += protoutil.NestedStructureMarshal(recordFiltersField, buf[offset:], &r.filters[i]) } for i := range r.targets { - n, err = protoutil.NestedStructureMarshal(recordTargetsField, buf[offset:], &r.targets[i]) - if err != nil { - return nil, err - } - - offset += n + offset += protoutil.NestedStructureMarshal(recordTargetsField, buf[offset:], &r.targets[i]) } - return buf, nil + return buf } // StableSize of acl record structure marshalled by StableMarshal function. @@ -164,9 +132,9 @@ func (r *Record) Unmarshal(data []byte) error { // StableMarshal marshals unified header filter structure in a protobuf // compatible way without field order shuffle. -func (f *HeaderFilter) StableMarshal(buf []byte) ([]byte, error) { +func (f *HeaderFilter) StableMarshal(buf []byte) []byte { if f == nil { - return []byte{}, nil + return []byte{} } if buf == nil { @@ -180,7 +148,7 @@ func (f *HeaderFilter) StableMarshal(buf []byte) ([]byte, error) { offset += protoutil.StringMarshal(filterNameField, buf[offset:], f.key) protoutil.StringMarshal(filterValueField, buf[offset:], f.value) - return buf, nil + return buf } // StableSize of header filter structure marshalled by StableMarshal function. @@ -203,9 +171,9 @@ func (f *HeaderFilter) Unmarshal(data []byte) error { // StableMarshal marshals unified role info structure in a protobuf // compatible way without field order shuffle. -func (t *Target) StableMarshal(buf []byte) ([]byte, error) { +func (t *Target) StableMarshal(buf []byte) []byte { if t == nil { - return []byte{}, nil + return []byte{} } if buf == nil { @@ -217,7 +185,7 @@ func (t *Target) StableMarshal(buf []byte) ([]byte, error) { offset += protoutil.EnumMarshal(targetTypeField, buf[offset:], int32(t.role)) protoutil.RepeatedBytesMarshal(targetKeysField, buf[offset:], t.keys) - return buf, nil + return buf } // StableSize of role info structure marshalled by StableMarshal function. @@ -236,9 +204,9 @@ func (t *Target) Unmarshal(data []byte) error { return message.Unmarshal(t, data, new(acl.EACLRecord_Target)) } -func (l *TokenLifetime) StableMarshal(buf []byte) ([]byte, error) { +func (l *TokenLifetime) StableMarshal(buf []byte) []byte { if l == nil { - return []byte{}, nil + return []byte{} } if buf == nil { @@ -251,7 +219,7 @@ func (l *TokenLifetime) StableMarshal(buf []byte) ([]byte, error) { offset += protoutil.UInt64Marshal(lifetimeNotValidBeforeField, buf[offset:], l.nbf) protoutil.UInt64Marshal(lifetimeIssuedAtField, buf[offset:], l.iat) - return buf, nil + return buf } func (l *TokenLifetime) StableSize() (size int) { @@ -270,40 +238,22 @@ func (l *TokenLifetime) Unmarshal(data []byte) error { return message.Unmarshal(l, data, new(acl.BearerToken_Body_TokenLifetime)) } -func (bt *BearerTokenBody) StableMarshal(buf []byte) ([]byte, error) { +func (bt *BearerTokenBody) StableMarshal(buf []byte) []byte { if bt == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, bt.StableSize()) } - var ( - offset, n int - err error - ) + var offset int - n, err = protoutil.NestedStructureMarshal(bearerTokenBodyACLField, buf[offset:], bt.eacl) - if err != nil { - return nil, err - } + offset += protoutil.NestedStructureMarshal(bearerTokenBodyACLField, buf[offset:], bt.eacl) + offset += protoutil.NestedStructureMarshal(bearerTokenBodyOwnerField, buf[offset:], bt.ownerID) + protoutil.NestedStructureMarshal(bearerTokenBodyLifetimeField, buf[offset:], bt.lifetime) - offset += n - - n, err = protoutil.NestedStructureMarshal(bearerTokenBodyOwnerField, buf[offset:], bt.ownerID) - if err != nil { - return nil, err - } - - offset += n - - _, err = protoutil.NestedStructureMarshal(bearerTokenBodyLifetimeField, buf[offset:], bt.lifetime) - if err != nil { - return nil, err - } - - return buf, nil + return buf } func (bt *BearerTokenBody) StableSize() (size int) { @@ -322,33 +272,21 @@ func (bt *BearerTokenBody) Unmarshal(data []byte) error { return message.Unmarshal(bt, data, new(acl.BearerToken_Body)) } -func (bt *BearerToken) StableMarshal(buf []byte) ([]byte, error) { +func (bt *BearerToken) StableMarshal(buf []byte) []byte { if bt == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, bt.StableSize()) } - var ( - offset, n int - err error - ) + var offset int - n, err = protoutil.NestedStructureMarshal(bearerTokenBodyField, buf[offset:], bt.body) - if err != nil { - return nil, err - } + offset += protoutil.NestedStructureMarshal(bearerTokenBodyField, buf[offset:], bt.body) + protoutil.NestedStructureMarshal(bearerTokenSignatureField, buf[offset:], bt.sig) - offset += n - - _, err = protoutil.NestedStructureMarshal(bearerTokenSignatureField, buf[offset:], bt.sig) - if err != nil { - return nil, err - } - - return buf, nil + return buf } func (bt *BearerToken) StableSize() (size int) { diff --git a/audit/marshal.go b/audit/marshal.go index 02633a4..c97fb4e 100644 --- a/audit/marshal.go +++ b/audit/marshal.go @@ -27,62 +27,33 @@ const ( // StableMarshal marshals unified DataAuditResult structure into a protobuf // binary format without field order shuffle. -func (a *DataAuditResult) StableMarshal(buf []byte) ([]byte, error) { +func (a *DataAuditResult) StableMarshal(buf []byte) []byte { if a == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, a.StableSize()) } - var ( - offset, n int - err error - ) - - n, err = proto.NestedStructureMarshal(versionFNum, buf[offset:], a.version) - if err != nil { - return nil, err - } - - offset += n + var offset int + offset += proto.NestedStructureMarshal(versionFNum, buf[offset:], a.version) offset += proto.Fixed64Marshal(auditEpochFNum, buf[offset:], a.auditEpoch) - - n, err = proto.NestedStructureMarshal(cidFNum, buf[offset:], a.cid) - if err != nil { - return nil, err - } - - offset += n - + offset += proto.NestedStructureMarshal(cidFNum, buf[offset:], a.cid) offset += proto.BytesMarshal(pubKeyFNum, buf[offset:], a.pubKey) offset += proto.BoolMarshal(completeFNum, buf[offset:], a.complete) offset += proto.UInt32Marshal(requestsFNum, buf[offset:], a.requests) offset += proto.UInt32Marshal(retriesFNum, buf[offset:], a.retries) - - n, err = refs.ObjectIDNestedListMarshal(passSGFNum, buf[offset:], a.passSG) - if err != nil { - return nil, err - } - - offset += n - - n, err = refs.ObjectIDNestedListMarshal(failSGFNum, buf[offset:], a.failSG) - if err != nil { - return nil, err - } - - offset += n - + offset += refs.ObjectIDNestedListMarshal(passSGFNum, buf[offset:], a.passSG) + offset += refs.ObjectIDNestedListMarshal(failSGFNum, buf[offset:], a.failSG) offset += proto.UInt32Marshal(hitFNum, buf[offset:], a.hit) offset += proto.UInt32Marshal(missFNum, buf[offset:], a.miss) offset += proto.UInt32Marshal(failFNum, buf[offset:], a.fail) offset += proto.RepeatedBytesMarshal(passNodesFNum, buf[offset:], a.passNodes) proto.RepeatedBytesMarshal(failNodesFNum, buf[offset:], a.failNodes) - return buf, nil + return buf } // StableSize returns byte length of DataAuditResult structure diff --git a/container/marshal.go b/container/marshal.go index f8a940d..ae1ac78 100644 --- a/container/marshal.go +++ b/container/marshal.go @@ -51,9 +51,9 @@ const ( usedSpaceReqBodyAnnouncementsField = 1 ) -func (a *Attribute) StableMarshal(buf []byte) ([]byte, error) { +func (a *Attribute) StableMarshal(buf []byte) []byte { if a == nil { - return []byte{}, nil + return []byte{} } if buf == nil { @@ -65,7 +65,7 @@ func (a *Attribute) StableMarshal(buf []byte) ([]byte, error) { offset += protoutil.StringMarshal(attributeKeyField, buf[offset:], a.key) protoutil.StringMarshal(attributeValueField, buf[offset:], a.val) - return buf, nil + return buf } func (a *Attribute) StableSize() (size int) { @@ -83,52 +83,29 @@ func (a *Attribute) Unmarshal(data []byte) error { return message.Unmarshal(a, data, new(container.Container_Attribute)) } -func (c *Container) StableMarshal(buf []byte) ([]byte, error) { +func (c *Container) StableMarshal(buf []byte) []byte { if c == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, c.StableSize()) } - var ( - offset, n int - err error - ) - - n, err = protoutil.NestedStructureMarshal(containerVersionField, buf[offset:], c.version) - if err != nil { - return nil, err - } - - offset += n - - n, err = protoutil.NestedStructureMarshal(containerOwnerField, buf[offset:], c.ownerID) - if err != nil { - return nil, err - } - - offset += n + var offset int + offset += protoutil.NestedStructureMarshal(containerVersionField, buf[offset:], c.version) + offset += protoutil.NestedStructureMarshal(containerOwnerField, buf[offset:], c.ownerID) offset += protoutil.BytesMarshal(containerNonceField, buf[offset:], c.nonce) offset += protoutil.UInt32Marshal(containerBasicACLField, buf[offset:], c.basicACL) for i := range c.attr { - n, err = protoutil.NestedStructureMarshal(containerAttributesField, buf[offset:], &c.attr[i]) - if err != nil { - return nil, err - } - - offset += n + offset += protoutil.NestedStructureMarshal(containerAttributesField, buf[offset:], &c.attr[i]) } - _, err = protoutil.NestedStructureMarshal(containerPlacementField, buf[offset:], c.policy) - if err != nil { - return nil, err - } + protoutil.NestedStructureMarshal(containerPlacementField, buf[offset:], c.policy) - return buf, nil + return buf } func (c *Container) StableSize() (size int) { @@ -154,33 +131,21 @@ func (c *Container) Unmarshal(data []byte) error { return message.Unmarshal(c, data, new(container.Container)) } -func (r *PutRequestBody) StableMarshal(buf []byte) ([]byte, error) { +func (r *PutRequestBody) StableMarshal(buf []byte) []byte { if r == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, r.StableSize()) } - var ( - offset, n int - err error - ) + var offset int - n, err = protoutil.NestedStructureMarshal(putReqBodyContainerField, buf[offset:], r.cnr) - if err != nil { - return nil, err - } + offset += protoutil.NestedStructureMarshal(putReqBodyContainerField, buf[offset:], r.cnr) + protoutil.NestedStructureMarshal(putReqBodySignatureField, buf[offset:], r.sig) - offset += n - - _, err = protoutil.NestedStructureMarshal(putReqBodySignatureField, buf[offset:], r.sig) - if err != nil { - return nil, err - } - - return buf, nil + return buf } func (r *PutRequestBody) StableSize() (size int) { @@ -198,25 +163,18 @@ func (r *PutRequestBody) Unmarshal(data []byte) error { return message.Unmarshal(r, data, new(container.PutRequest_Body)) } -func (r *PutResponseBody) StableMarshal(buf []byte) ([]byte, error) { +func (r *PutResponseBody) StableMarshal(buf []byte) []byte { if r == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, r.StableSize()) } - var ( - err error - ) + protoutil.NestedStructureMarshal(putRespBodyIDField, buf, r.cid) - _, err = protoutil.NestedStructureMarshal(putRespBodyIDField, buf, r.cid) - if err != nil { - return nil, err - } - - return buf, nil + return buf } func (r *PutResponseBody) StableSize() (size int) { @@ -233,33 +191,21 @@ func (r *PutResponseBody) Unmarshal(data []byte) error { return message.Unmarshal(r, data, new(container.PutResponse_Body)) } -func (r *DeleteRequestBody) StableMarshal(buf []byte) ([]byte, error) { +func (r *DeleteRequestBody) StableMarshal(buf []byte) []byte { if r == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, r.StableSize()) } - var ( - offset, n int - err error - ) + var offset int - n, err = protoutil.NestedStructureMarshal(deleteReqBodyIDField, buf[offset:], r.cid) - if err != nil { - return nil, err - } + offset += protoutil.NestedStructureMarshal(deleteReqBodyIDField, buf[offset:], r.cid) + protoutil.NestedStructureMarshal(deleteReqBodySignatureField, buf[offset:], r.sig) - offset += n - - _, err = protoutil.NestedStructureMarshal(deleteReqBodySignatureField, buf[offset:], r.sig) - if err != nil { - return nil, err - } - - return buf, nil + return buf } func (r *DeleteRequestBody) StableSize() (size int) { @@ -277,8 +223,8 @@ func (r *DeleteRequestBody) Unmarshal(data []byte) error { return message.Unmarshal(r, data, new(container.DeleteRequest_Body)) } -func (r *DeleteResponseBody) StableMarshal(buf []byte) ([]byte, error) { - return nil, nil +func (r *DeleteResponseBody) StableMarshal(buf []byte) []byte { + return nil } func (r *DeleteResponseBody) StableSize() (size int) { @@ -289,21 +235,18 @@ func (r *DeleteResponseBody) Unmarshal([]byte) error { return nil } -func (r *GetRequestBody) StableMarshal(buf []byte) ([]byte, error) { +func (r *GetRequestBody) StableMarshal(buf []byte) []byte { if r == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, r.StableSize()) } - _, err := protoutil.NestedStructureMarshal(getReqBodyIDField, buf, r.cid) - if err != nil { - return nil, err - } + protoutil.NestedStructureMarshal(getReqBodyIDField, buf, r.cid) - return buf, nil + return buf } func (r *GetRequestBody) StableSize() (size int) { @@ -320,31 +263,22 @@ func (r *GetRequestBody) Unmarshal(data []byte) error { return message.Unmarshal(r, data, new(container.GetRequest_Body)) } -func (r *GetResponseBody) StableMarshal(buf []byte) ([]byte, error) { +func (r *GetResponseBody) StableMarshal(buf []byte) []byte { if r == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, r.StableSize()) } - offset, err := protoutil.NestedStructureMarshal(getRespBodyContainerField, buf, r.cnr) - if err != nil { - return nil, err - } + var offset int - n, err := protoutil.NestedStructureMarshal(getRespBodySignatureField, buf[offset:], r.sig) - if err != nil { - return nil, err - } + offset += protoutil.NestedStructureMarshal(getRespBodyContainerField, buf, r.cnr) + offset += protoutil.NestedStructureMarshal(getRespBodySignatureField, buf[offset:], r.sig) + protoutil.NestedStructureMarshal(getRespBodyTokenField, buf[offset:], r.token) - _, err = protoutil.NestedStructureMarshal(getRespBodyTokenField, buf[offset+n:], r.token) - if err != nil { - return nil, err - } - - return buf, nil + return buf } func (r *GetResponseBody) StableSize() (size int) { @@ -363,21 +297,18 @@ func (r *GetResponseBody) Unmarshal(data []byte) error { return message.Unmarshal(r, data, new(container.GetResponse_Body)) } -func (r *ListRequestBody) StableMarshal(buf []byte) ([]byte, error) { +func (r *ListRequestBody) StableMarshal(buf []byte) []byte { if r == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, r.StableSize()) } - _, err := protoutil.NestedStructureMarshal(listReqBodyOwnerField, buf, r.ownerID) - if err != nil { - return nil, err - } + protoutil.NestedStructureMarshal(listReqBodyOwnerField, buf, r.ownerID) - return buf, nil + return buf } func (r *ListRequestBody) StableSize() (size int) { @@ -394,30 +325,22 @@ func (r *ListRequestBody) Unmarshal(data []byte) error { return message.Unmarshal(r, data, new(container.ListRequest_Body)) } -func (r *ListResponseBody) StableMarshal(buf []byte) ([]byte, error) { +func (r *ListResponseBody) StableMarshal(buf []byte) []byte { if r == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, r.StableSize()) } - var ( - n, offset int - err error - ) + var offset int for i := range r.cidList { - n, err = protoutil.NestedStructureMarshal(listRespBodyIDsField, buf[offset:], &r.cidList[i]) - if err != nil { - return nil, err - } - - offset += n + offset += protoutil.NestedStructureMarshal(listRespBodyIDsField, buf[offset:], &r.cidList[i]) } - return buf, nil + return buf } func (r *ListResponseBody) StableSize() (size int) { @@ -436,33 +359,21 @@ func (r *ListResponseBody) Unmarshal(data []byte) error { return message.Unmarshal(r, data, new(container.ListResponse_Body)) } -func (r *SetExtendedACLRequestBody) StableMarshal(buf []byte) ([]byte, error) { +func (r *SetExtendedACLRequestBody) StableMarshal(buf []byte) []byte { if r == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, r.StableSize()) } - var ( - offset, n int - err error - ) + var offset int - n, err = protoutil.NestedStructureMarshal(setEACLReqBodyTableField, buf[offset:], r.eacl) - if err != nil { - return nil, err - } + offset += protoutil.NestedStructureMarshal(setEACLReqBodyTableField, buf[offset:], r.eacl) + protoutil.NestedStructureMarshal(setEACLReqBodySignatureField, buf[offset:], r.sig) - offset += n - - _, err = protoutil.NestedStructureMarshal(setEACLReqBodySignatureField, buf[offset:], r.sig) - if err != nil { - return nil, err - } - - return buf, nil + return buf } func (r *SetExtendedACLRequestBody) StableSize() (size int) { @@ -480,8 +391,8 @@ func (r *SetExtendedACLRequestBody) Unmarshal(data []byte) error { return message.Unmarshal(r, data, new(container.SetExtendedACLRequest_Body)) } -func (r *SetExtendedACLResponseBody) StableMarshal(buf []byte) ([]byte, error) { - return nil, nil +func (r *SetExtendedACLResponseBody) StableMarshal(buf []byte) []byte { + return nil } func (r *SetExtendedACLResponseBody) StableSize() (size int) { @@ -492,21 +403,18 @@ func (r *SetExtendedACLResponseBody) Unmarshal([]byte) error { return nil } -func (r *GetExtendedACLRequestBody) StableMarshal(buf []byte) ([]byte, error) { +func (r *GetExtendedACLRequestBody) StableMarshal(buf []byte) []byte { if r == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, r.StableSize()) } - _, err := protoutil.NestedStructureMarshal(getEACLReqBodyIDField, buf, r.cid) - if err != nil { - return nil, err - } + protoutil.NestedStructureMarshal(getEACLReqBodyIDField, buf, r.cid) - return buf, nil + return buf } func (r *GetExtendedACLRequestBody) StableSize() (size int) { @@ -523,40 +431,22 @@ func (r *GetExtendedACLRequestBody) Unmarshal(data []byte) error { return message.Unmarshal(r, data, new(container.GetExtendedACLRequest_Body)) } -func (r *GetExtendedACLResponseBody) StableMarshal(buf []byte) ([]byte, error) { +func (r *GetExtendedACLResponseBody) StableMarshal(buf []byte) []byte { if r == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, r.StableSize()) } - var ( - offset, n int - err error - ) + var offset int - n, err = protoutil.NestedStructureMarshal(getEACLRespBodyTableField, buf[offset:], r.eacl) - if err != nil { - return nil, err - } + offset += protoutil.NestedStructureMarshal(getEACLRespBodyTableField, buf[offset:], r.eacl) + offset += protoutil.NestedStructureMarshal(getEACLRespBodySignatureField, buf[offset:], r.sig) + protoutil.NestedStructureMarshal(getEACLRespBodyTokenField, buf[offset:], r.token) - offset += n - - n, err = protoutil.NestedStructureMarshal(getEACLRespBodySignatureField, buf[offset:], r.sig) - if err != nil { - return nil, err - } - - offset += n - - _, err = protoutil.NestedStructureMarshal(getEACLRespBodyTokenField, buf[offset:], r.token) - if err != nil { - return nil, err - } - - return buf, nil + return buf } func (r *GetExtendedACLResponseBody) StableSize() (size int) { @@ -575,32 +465,22 @@ func (r *GetExtendedACLResponseBody) Unmarshal(data []byte) error { return message.Unmarshal(r, data, new(container.GetExtendedACLResponse_Body)) } -func (a *UsedSpaceAnnouncement) StableMarshal(buf []byte) ([]byte, error) { +func (a *UsedSpaceAnnouncement) StableMarshal(buf []byte) []byte { if a == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, a.StableSize()) } - var ( - offset, n int - err error - ) + var offset int offset += protoutil.UInt64Marshal(usedSpaceAnnounceEpochField, buf[offset:], a.epoch) - - n, err = protoutil.NestedStructureMarshal(usedSpaceAnnounceCIDField, buf[offset:], a.cid) - if err != nil { - return nil, err - } - - offset += n - + offset += protoutil.NestedStructureMarshal(usedSpaceAnnounceCIDField, buf[offset:], a.cid) protoutil.UInt64Marshal(usedSpaceAnnounceUsedSpaceField, buf[offset:], a.usedSpace) - return buf, nil + return buf } func (a *UsedSpaceAnnouncement) StableSize() (size int) { @@ -619,30 +499,22 @@ func (a *UsedSpaceAnnouncement) Unmarshal(data []byte) error { return message.Unmarshal(a, data, new(container.AnnounceUsedSpaceRequest_Body_Announcement)) } -func (r *AnnounceUsedSpaceRequestBody) StableMarshal(buf []byte) ([]byte, error) { +func (r *AnnounceUsedSpaceRequestBody) StableMarshal(buf []byte) []byte { if r == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, r.StableSize()) } - var ( - offset, n int - err error - ) + var offset int for i := range r.announcements { - n, err = protoutil.NestedStructureMarshal(usedSpaceReqBodyAnnouncementsField, buf[offset:], &r.announcements[i]) - if err != nil { - return nil, err - } - - offset += n + offset += protoutil.NestedStructureMarshal(usedSpaceReqBodyAnnouncementsField, buf[offset:], &r.announcements[i]) } - return buf, nil + return buf } func (r *AnnounceUsedSpaceRequestBody) StableSize() (size int) { @@ -661,8 +533,8 @@ func (r *AnnounceUsedSpaceRequestBody) Unmarshal(data []byte) error { return message.Unmarshal(r, data, new(container.AnnounceUsedSpaceRequest_Body)) } -func (r *AnnounceUsedSpaceResponseBody) StableMarshal(buf []byte) ([]byte, error) { - return nil, nil +func (r *AnnounceUsedSpaceResponseBody) StableMarshal(buf []byte) []byte { + return nil } func (r *AnnounceUsedSpaceResponseBody) StableSize() (size int) { diff --git a/netmap/marshal.go b/netmap/marshal.go index 72a974e..b7f6e08 100644 --- a/netmap/marshal.go +++ b/netmap/marshal.go @@ -41,19 +41,16 @@ const ( nodeInfoResponseBodyField = 2 ) -func (f *Filter) StableMarshal(buf []byte) ([]byte, error) { +func (f *Filter) StableMarshal(buf []byte) []byte { if f == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, f.StableSize()) } - var ( - offset, n int - err error - ) + var offset int offset += protoutil.StringMarshal(nameFilterField, buf[offset:], f.name) offset += protoutil.StringMarshal(keyFilterField, buf[offset:], f.key) @@ -61,15 +58,10 @@ func (f *Filter) StableMarshal(buf []byte) ([]byte, error) { offset += protoutil.StringMarshal(valueFilterField, buf[offset:], f.value) for i := range f.filters { - n, err = protoutil.NestedStructureMarshal(filtersFilterField, buf[offset:], &f.filters[i]) - if err != nil { - return nil, err - } - - offset += n + offset += protoutil.NestedStructureMarshal(filtersFilterField, buf[offset:], &f.filters[i]) } - return buf, nil + return buf } func (f *Filter) StableSize() (size int) { @@ -88,9 +80,9 @@ func (f *Filter) Unmarshal(data []byte) error { return message.Unmarshal(f, data, new(netmap.Filter)) } -func (s *Selector) StableMarshal(buf []byte) ([]byte, error) { +func (s *Selector) StableMarshal(buf []byte) []byte { if s == nil { - return []byte{}, nil + return []byte{} } if buf == nil { @@ -105,7 +97,7 @@ func (s *Selector) StableMarshal(buf []byte) ([]byte, error) { offset += protoutil.StringMarshal(attributeSelectorField, buf[offset:], s.attribute) protoutil.StringMarshal(filterSelectorField, buf[offset:], s.filter) - return buf, nil + return buf } func (s *Selector) StableSize() (size int) { @@ -122,9 +114,9 @@ func (s *Selector) Unmarshal(data []byte) error { return message.Unmarshal(s, data, new(netmap.Selector)) } -func (r *Replica) StableMarshal(buf []byte) ([]byte, error) { +func (r *Replica) StableMarshal(buf []byte) []byte { if r == nil { - return []byte{}, nil + return []byte{} } if buf == nil { @@ -136,7 +128,7 @@ func (r *Replica) StableMarshal(buf []byte) ([]byte, error) { offset += protoutil.UInt32Marshal(countReplicaField, buf[offset:], r.count) protoutil.StringMarshal(selectorReplicaField, buf[offset:], r.selector) - return buf, nil + return buf } func (r *Replica) StableSize() (size int) { @@ -150,55 +142,34 @@ func (r *Replica) Unmarshal(data []byte) error { return message.Unmarshal(r, data, new(netmap.Replica)) } -func (p *PlacementPolicy) StableMarshal(buf []byte) ([]byte, error) { +func (p *PlacementPolicy) StableMarshal(buf []byte) []byte { if p == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, p.StableSize()) } - var ( - offset, n int - err error - ) + var offset int for i := range p.replicas { - n, err = protoutil.NestedStructureMarshal(replicasPolicyField, buf[offset:], &p.replicas[i]) - if err != nil { - return nil, err - } - - offset += n + offset += protoutil.NestedStructureMarshal(replicasPolicyField, buf[offset:], &p.replicas[i]) } offset += protoutil.UInt32Marshal(backupPolicyField, buf[offset:], p.backupFactor) for i := range p.selectors { - n, err = protoutil.NestedStructureMarshal(selectorsPolicyField, buf[offset:], &p.selectors[i]) - if err != nil { - return nil, err - } - - offset += n + offset += protoutil.NestedStructureMarshal(selectorsPolicyField, buf[offset:], &p.selectors[i]) } for i := range p.filters { - n, err = protoutil.NestedStructureMarshal(filtersPolicyField, buf[offset:], &p.filters[i]) - if err != nil { - return nil, err - } - - offset += n + offset += protoutil.NestedStructureMarshal(filtersPolicyField, buf[offset:], &p.filters[i]) } - _, err = protoutil.NestedStructureMarshal(subnetIDPolicyField, buf[offset:], p.subnetID) - if err != nil { - return nil, err - } + protoutil.NestedStructureMarshal(subnetIDPolicyField, buf[offset:], p.subnetID) - return buf, nil + return buf } func (p *PlacementPolicy) StableSize() (size int) { @@ -225,9 +196,9 @@ func (p *PlacementPolicy) Unmarshal(data []byte) error { return message.Unmarshal(p, data, new(netmap.PlacementPolicy)) } -func (a *Attribute) StableMarshal(buf []byte) ([]byte, error) { +func (a *Attribute) StableMarshal(buf []byte) []byte { if a == nil { - return []byte{}, nil + return []byte{} } if buf == nil { @@ -243,7 +214,7 @@ func (a *Attribute) StableMarshal(buf []byte) ([]byte, error) { offset += protoutil.StringMarshal(parentsAttributeField, buf[offset:], a.parents[i]) } - return buf, nil + return buf } func (a *Attribute) StableSize() (size int) { @@ -265,35 +236,27 @@ func (a *Attribute) Unmarshal(data []byte) error { return message.Unmarshal(a, data, new(netmap.NodeInfo_Attribute)) } -func (ni *NodeInfo) StableMarshal(buf []byte) ([]byte, error) { +func (ni *NodeInfo) StableMarshal(buf []byte) []byte { if ni == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, ni.StableSize()) } - var ( - offset, n int - err error - ) + var offset int offset += protoutil.BytesMarshal(keyNodeInfoField, buf[offset:], ni.publicKey) offset += protoutil.RepeatedStringMarshal(addressNodeInfoField, buf[offset:], ni.addresses) for i := range ni.attributes { - n, err = protoutil.NestedStructureMarshal(attributesNodeInfoField, buf[offset:], &ni.attributes[i]) - if err != nil { - return nil, err - } - - offset += n + offset += protoutil.NestedStructureMarshal(attributesNodeInfoField, buf[offset:], &ni.attributes[i]) } protoutil.EnumMarshal(stateNodeInfoField, buf[offset:], int32(ni.state)) - return buf, nil + return buf } func (ni *NodeInfo) StableSize() (size int) { @@ -317,8 +280,8 @@ func (ni *NodeInfo) Unmarshal(data []byte) error { return message.Unmarshal(ni, data, new(netmap.NodeInfo)) } -func (l *LocalNodeInfoRequestBody) StableMarshal(buf []byte) ([]byte, error) { - return nil, nil +func (l *LocalNodeInfoRequestBody) StableMarshal(buf []byte) []byte { + return nil } func (l *LocalNodeInfoRequestBody) StableSize() (size int) { @@ -329,33 +292,21 @@ func (l *LocalNodeInfoRequestBody) Unmarshal([]byte) error { return nil } -func (l *LocalNodeInfoResponseBody) StableMarshal(buf []byte) ([]byte, error) { +func (l *LocalNodeInfoResponseBody) StableMarshal(buf []byte) []byte { if l == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, l.StableSize()) } - var ( - offset, n int - err error - ) + var offset int - n, err = protoutil.NestedStructureMarshal(versionInfoResponseBodyField, buf[offset:], l.version) - if err != nil { - return nil, err - } + offset += protoutil.NestedStructureMarshal(versionInfoResponseBodyField, buf[offset:], l.version) + protoutil.NestedStructureMarshal(nodeInfoResponseBodyField, buf[offset:], l.nodeInfo) - offset += n - - _, err = protoutil.NestedStructureMarshal(nodeInfoResponseBodyField, buf[offset:], l.nodeInfo) - if err != nil { - return nil, err - } - - return buf, nil + return buf } func (l *LocalNodeInfoResponseBody) StableSize() (size int) { @@ -379,9 +330,9 @@ const ( netPrmValFNum ) -func (x *NetworkParameter) StableMarshal(buf []byte) ([]byte, error) { +func (x *NetworkParameter) StableMarshal(buf []byte) []byte { if x == nil { - return []byte{}, nil + return []byte{} } if buf == nil { @@ -393,7 +344,7 @@ func (x *NetworkParameter) StableMarshal(buf []byte) ([]byte, error) { offset += protoutil.BytesMarshal(netPrmKeyFNum, buf[offset:], x.k) protoutil.BytesMarshal(netPrmValFNum, buf[offset:], x.v) - return buf, nil + return buf } func (x *NetworkParameter) StableSize() (size int) { @@ -412,30 +363,22 @@ const ( netCfgPrmsFNum ) -func (x *NetworkConfig) StableMarshal(buf []byte) ([]byte, error) { +func (x *NetworkConfig) StableMarshal(buf []byte) []byte { if x == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, x.StableSize()) } - var ( - offset, n int - err error - ) + var offset int for i := range x.ps { - n, err = protoutil.NestedStructureMarshal(netCfgPrmsFNum, buf[offset:], &x.ps[i]) - if err != nil { - return nil, err - } - - offset += n + offset += protoutil.NestedStructureMarshal(netCfgPrmsFNum, buf[offset:], &x.ps[i]) } - return buf, nil + return buf } func (x *NetworkConfig) StableSize() (size int) { @@ -458,30 +401,23 @@ const ( netInfoCfgFNum ) -func (i *NetworkInfo) StableMarshal(buf []byte) ([]byte, error) { +func (i *NetworkInfo) StableMarshal(buf []byte) []byte { if i == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, i.StableSize()) } - var ( - offset int - err error - ) + var offset int offset += protoutil.UInt64Marshal(netInfoCurEpochFNum, buf[offset:], i.curEpoch) offset += protoutil.UInt64Marshal(netInfoMagicNumFNum, buf[offset:], i.magicNum) offset += protoutil.Int64Marshal(netInfoMSPerBlockFNum, buf[offset:], i.msPerBlock) + protoutil.NestedStructureMarshal(netInfoCfgFNum, buf[offset:], i.netCfg) - _, err = protoutil.NestedStructureMarshal(netInfoCfgFNum, buf[offset:], i.netCfg) - if err != nil { - return nil, err - } - - return buf, nil + return buf } func (i *NetworkInfo) StableSize() (size int) { @@ -501,8 +437,8 @@ func (i *NetworkInfo) Unmarshal(data []byte) error { return message.Unmarshal(i, data, new(netmap.NetworkInfo)) } -func (l *NetworkInfoRequestBody) StableMarshal(buf []byte) ([]byte, error) { - return nil, nil +func (l *NetworkInfoRequestBody) StableMarshal(buf []byte) []byte { + return nil } func (l *NetworkInfoRequestBody) StableSize() (size int) { @@ -518,21 +454,18 @@ const ( netInfoRespBodyNetInfoFNum ) -func (i *NetworkInfoResponseBody) StableMarshal(buf []byte) ([]byte, error) { +func (i *NetworkInfoResponseBody) StableMarshal(buf []byte) []byte { if i == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, i.StableSize()) } - _, err := protoutil.NestedStructureMarshal(netInfoRespBodyNetInfoFNum, buf, i.netInfo) - if err != nil { - return nil, err - } + protoutil.NestedStructureMarshal(netInfoRespBodyNetInfoFNum, buf, i.netInfo) - return buf, nil + return buf } func (i *NetworkInfoResponseBody) StableSize() (size int) { diff --git a/object/lock.go b/object/lock.go index 660c002..44597eb 100644 --- a/object/lock.go +++ b/object/lock.go @@ -48,28 +48,22 @@ const ( // StableMarshal encodes the Lock into Protocol Buffers binary format // with direct field order. -func (x *Lock) StableMarshal(buf []byte) ([]byte, error) { +func (x *Lock) StableMarshal(buf []byte) []byte { if x == nil || len(x.members) == 0 { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, x.StableSize()) } - var offset, n int - var err error + var offset int for i := range x.members { - n, err = proto.NestedStructureMarshal(fNumLockMembers, buf[offset:], &x.members[i]) - if err != nil { - return nil, err - } - - offset += n + offset += proto.NestedStructureMarshal(fNumLockMembers, buf[offset:], &x.members[i]) } - return buf, nil + return buf } // StableSize size of the buffer required to write the Lock in Protocol Buffers @@ -146,11 +140,7 @@ func WriteLock(obj *Object, lock Lock) { hdr.SetObjectType(TypeLock) - payload, err := lock.StableMarshal(nil) - if err != nil { - panic(fmt.Sprintf("encode lock content: %v", err)) - } - + payload := lock.StableMarshal(nil) obj.SetPayload(payload) } diff --git a/object/marshal.go b/object/marshal.go index 79b767d..03ba265 100644 --- a/object/marshal.go +++ b/object/marshal.go @@ -112,52 +112,26 @@ const ( getRangeHashRespBodyHashListField = 2 ) -func (h *ShortHeader) StableMarshal(buf []byte) ([]byte, error) { +func (h *ShortHeader) StableMarshal(buf []byte) []byte { if h == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, h.StableSize()) } - var ( - offset, n int - err error - ) - - n, err = proto.NestedStructureMarshal(shortHdrVersionField, buf[offset:], h.version) - if err != nil { - return nil, err - } - - offset += n + var offset int + offset += proto.NestedStructureMarshal(shortHdrVersionField, buf[offset:], h.version) offset += proto.UInt64Marshal(shortHdrEpochField, buf[offset:], h.creatEpoch) - - n, err = proto.NestedStructureMarshal(shortHdrOwnerField, buf[offset:], h.ownerID) - if err != nil { - return nil, err - } - - offset += n - + offset += proto.NestedStructureMarshal(shortHdrOwnerField, buf[offset:], h.ownerID) offset += proto.EnumMarshal(shortHdrObjectTypeField, buf[offset:], int32(h.typ)) offset += proto.UInt64Marshal(shortHdrPayloadLength, buf[offset:], h.payloadLen) + offset += proto.NestedStructureMarshal(shortHdrHashField, buf[offset:], h.payloadHash) + proto.NestedStructureMarshal(shortHdrHomoHashField, buf[offset:], h.homoHash) - n, err = proto.NestedStructureMarshal(shortHdrHashField, buf[offset:], h.payloadHash) - if err != nil { - return nil, err - } - - offset += n - - _, err = proto.NestedStructureMarshal(shortHdrHomoHashField, buf[offset:], h.homoHash) - if err != nil { - return nil, err - } - - return buf, nil + return buf } func (h *ShortHeader) StableSize() (size int) { @@ -180,9 +154,9 @@ func (h *ShortHeader) Unmarshal(data []byte) error { return message.Unmarshal(h, data, new(object.ShortHeader)) } -func (a *Attribute) StableMarshal(buf []byte) ([]byte, error) { +func (a *Attribute) StableMarshal(buf []byte) []byte { if a == nil { - return []byte{}, nil + return []byte{} } if buf == nil { @@ -194,7 +168,7 @@ func (a *Attribute) StableMarshal(buf []byte) ([]byte, error) { offset += proto.StringMarshal(attributeKeyField, buf[offset:], a.key) proto.StringMarshal(attributeValueField, buf[offset:], a.val) - return buf, nil + return buf } func (a *Attribute) StableSize() (size int) { @@ -212,58 +186,25 @@ func (a *Attribute) Unmarshal(data []byte) error { return message.Unmarshal(a, data, new(object.Header_Attribute)) } -func (h *SplitHeader) StableMarshal(buf []byte) ([]byte, error) { +func (h *SplitHeader) StableMarshal(buf []byte) []byte { if h == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, h.StableSize()) } - var ( - offset, n int - err error - ) - - n, err = proto.NestedStructureMarshal(splitHdrParentField, buf[offset:], h.par) - if err != nil { - return nil, err - } - - offset += n - - n, err = proto.NestedStructureMarshal(splitHdrPreviousField, buf[offset:], h.prev) - if err != nil { - return nil, err - } - - offset += n - - n, err = proto.NestedStructureMarshal(splitHdrParentSignatureField, buf[offset:], h.parSig) - if err != nil { - return nil, err - } - - offset += n - - n, err = proto.NestedStructureMarshal(splitHdrParentHeaderField, buf[offset:], h.parHdr) - if err != nil { - return nil, err - } - - offset += n - - n, err = refs.ObjectIDNestedListMarshal(splitHdrChildrenField, buf[offset:], h.children) - if err != nil { - return nil, err - } - - offset += n + var offset int + offset += proto.NestedStructureMarshal(splitHdrParentField, buf[offset:], h.par) + offset += proto.NestedStructureMarshal(splitHdrPreviousField, buf[offset:], h.prev) + offset += proto.NestedStructureMarshal(splitHdrParentSignatureField, buf[offset:], h.parSig) + offset += proto.NestedStructureMarshal(splitHdrParentHeaderField, buf[offset:], h.parHdr) + offset += refs.ObjectIDNestedListMarshal(splitHdrChildrenField, buf[offset:], h.children) proto.BytesMarshal(splitHdrSplitIDField, buf[offset:], h.splitID) - return buf, nil + return buf } func (h *SplitHeader) StableSize() (size int) { @@ -285,82 +226,34 @@ func (h *SplitHeader) Unmarshal(data []byte) error { return message.Unmarshal(h, data, new(object.Header_Split)) } -func (h *Header) StableMarshal(buf []byte) ([]byte, error) { +func (h *Header) StableMarshal(buf []byte) []byte { if h == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, h.StableSize()) } - var ( - offset, n int - err error - ) - - n, err = proto.NestedStructureMarshal(hdrVersionField, buf[offset:], h.version) - if err != nil { - return nil, err - } - - offset += n - - n, err = proto.NestedStructureMarshal(hdrContainerIDField, buf[offset:], h.cid) - if err != nil { - return nil, err - } - - offset += n - - n, err = proto.NestedStructureMarshal(hdrOwnerIDField, buf[offset:], h.ownerID) - if err != nil { - return nil, err - } - - offset += n + var offset int + offset += proto.NestedStructureMarshal(hdrVersionField, buf[offset:], h.version) + offset += proto.NestedStructureMarshal(hdrContainerIDField, buf[offset:], h.cid) + offset += proto.NestedStructureMarshal(hdrOwnerIDField, buf[offset:], h.ownerID) offset += proto.UInt64Marshal(hdrEpochField, buf[offset:], h.creatEpoch) offset += proto.UInt64Marshal(hdrPayloadLengthField, buf[offset:], h.payloadLen) - - n, err = proto.NestedStructureMarshal(hdrPayloadHashField, buf[offset:], h.payloadHash) - if err != nil { - return nil, err - } - - offset += n - + offset += proto.NestedStructureMarshal(hdrPayloadHashField, buf[offset:], h.payloadHash) offset += proto.EnumMarshal(hdrObjectTypeField, buf[offset:], int32(h.typ)) - - n, err = proto.NestedStructureMarshal(hdrHomomorphicHashField, buf[offset:], h.homoHash) - if err != nil { - return nil, err - } - - offset += n - - n, err = proto.NestedStructureMarshal(hdrSessionTokenField, buf[offset:], h.sessionToken) - if err != nil { - return nil, err - } - - offset += n + offset += proto.NestedStructureMarshal(hdrHomomorphicHashField, buf[offset:], h.homoHash) + offset += proto.NestedStructureMarshal(hdrSessionTokenField, buf[offset:], h.sessionToken) for i := range h.attr { - n, err = proto.NestedStructureMarshal(hdrAttributesField, buf[offset:], &h.attr[i]) - if err != nil { - return nil, err - } - - offset += n + offset += proto.NestedStructureMarshal(hdrAttributesField, buf[offset:], &h.attr[i]) } - _, err = proto.NestedStructureMarshal(hdrSplitField, buf[offset:], h.split) - if err != nil { - return nil, err - } + proto.NestedStructureMarshal(hdrSplitField, buf[offset:], h.split) - return buf, nil + return buf } func (h *Header) StableSize() (size int) { @@ -389,33 +282,21 @@ func (h *Header) Unmarshal(data []byte) error { return message.Unmarshal(h, data, new(object.Header)) } -func (h *HeaderWithSignature) StableMarshal(buf []byte) ([]byte, error) { +func (h *HeaderWithSignature) StableMarshal(buf []byte) []byte { if h == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, h.StableSize()) } - var ( - offset, n int - err error - ) + var offset int - n, err = proto.NestedStructureMarshal(hdrWithSigHeaderField, buf[offset:], h.header) - if err != nil { - return nil, err - } + offset += proto.NestedStructureMarshal(hdrWithSigHeaderField, buf[offset:], h.header) + proto.NestedStructureMarshal(hdrWithSigSignatureField, buf[offset:], h.signature) - offset += n - - _, err = proto.NestedStructureMarshal(hdrWithSigSignatureField, buf[offset:], h.signature) - if err != nil { - return nil, err - } - - return buf, nil + return buf } func (h *HeaderWithSignature) StableSize() (size int) { @@ -433,44 +314,23 @@ func (h *HeaderWithSignature) Unmarshal(data []byte) error { return message.Unmarshal(h, data, new(object.HeaderWithSignature)) } -func (o *Object) StableMarshal(buf []byte) ([]byte, error) { +func (o *Object) StableMarshal(buf []byte) []byte { if o == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, o.StableSize()) } - var ( - offset, n int - err error - ) - - n, err = proto.NestedStructureMarshal(objIDField, buf[offset:], o.objectID) - if err != nil { - return nil, err - } - - offset += n - - n, err = proto.NestedStructureMarshal(objSignatureField, buf[offset:], o.idSig) - if err != nil { - return nil, err - } - - offset += n - - n, err = proto.NestedStructureMarshal(objHeaderField, buf[offset:], o.header) - if err != nil { - return nil, err - } - - offset += n + var offset int + offset += proto.NestedStructureMarshal(objIDField, buf[offset:], o.objectID) + offset += proto.NestedStructureMarshal(objSignatureField, buf[offset:], o.idSig) + offset += proto.NestedStructureMarshal(objHeaderField, buf[offset:], o.header) proto.BytesMarshal(objPayloadField, buf[offset:], o.payload) - return buf, nil + return buf } func (o *Object) StableSize() (size int) { @@ -490,35 +350,22 @@ func (o *Object) Unmarshal(data []byte) error { return message.Unmarshal(o, data, new(object.Object)) } -func (s *SplitInfo) StableMarshal(buf []byte) ([]byte, error) { +func (s *SplitInfo) StableMarshal(buf []byte) []byte { if s == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, s.StableSize()) } - var ( - offset, n int - err error - ) + var offset int offset += proto.BytesMarshal(splitInfoSplitIDField, buf[offset:], s.splitID) + offset += proto.NestedStructureMarshal(splitInfoLastPartField, buf[offset:], s.lastPart) + proto.NestedStructureMarshal(splitInfoLinkField, buf[offset:], s.link) - n, err = proto.NestedStructureMarshal(splitInfoLastPartField, buf[offset:], s.lastPart) - if err != nil { - return nil, err - } - - offset += n - - _, err = proto.NestedStructureMarshal(splitInfoLinkField, buf[offset:], s.link) - if err != nil { - return nil, err - } - - return buf, nil + return buf } func (s *SplitInfo) StableSize() (size int) { @@ -537,30 +384,21 @@ func (s *SplitInfo) Unmarshal(data []byte) error { return message.Unmarshal(s, data, new(object.SplitInfo)) } -func (r *GetRequestBody) StableMarshal(buf []byte) ([]byte, error) { +func (r *GetRequestBody) StableMarshal(buf []byte) []byte { if r == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, r.StableSize()) } - var ( - offset, n int - err error - ) - - n, err = proto.NestedStructureMarshal(getReqBodyAddressField, buf[offset:], r.addr) - if err != nil { - return nil, err - } - - offset += n + var offset int + offset += proto.NestedStructureMarshal(getReqBodyAddressField, buf[offset:], r.addr) proto.BoolMarshal(getReqBodyRawFlagField, buf[offset:], r.raw) - return buf, nil + return buf } func (r *GetRequestBody) StableSize() (size int) { @@ -578,40 +416,22 @@ func (r *GetRequestBody) Unmarshal(data []byte) error { return message.Unmarshal(r, data, new(object.GetRequest_Body)) } -func (r *GetObjectPartInit) StableMarshal(buf []byte) ([]byte, error) { +func (r *GetObjectPartInit) StableMarshal(buf []byte) []byte { if r == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, r.StableSize()) } - var ( - offset, n int - err error - ) + var offset int - n, err = proto.NestedStructureMarshal(getRespInitObjectIDField, buf[offset:], r.id) - if err != nil { - return nil, err - } + offset += proto.NestedStructureMarshal(getRespInitObjectIDField, buf[offset:], r.id) + offset += proto.NestedStructureMarshal(getRespInitSignatureField, buf[offset:], r.sig) + proto.NestedStructureMarshal(getRespInitHeaderField, buf[offset:], r.hdr) - offset += n - - n, err = proto.NestedStructureMarshal(getRespInitSignatureField, buf[offset:], r.sig) - if err != nil { - return nil, err - } - - offset += n - - _, err = proto.NestedStructureMarshal(getRespInitHeaderField, buf[offset:], r.hdr) - if err != nil { - return nil, err - } - - return buf, nil + return buf } func (r *GetObjectPartInit) StableSize() (size int) { @@ -630,9 +450,9 @@ func (r *GetObjectPartInit) Unmarshal(data []byte) error { return message.Unmarshal(r, data, new(object.GetResponse_Body_Init)) } -func (r *GetResponseBody) StableMarshal(buf []byte) ([]byte, error) { +func (r *GetResponseBody) StableMarshal(buf []byte) []byte { if r == nil { - return []byte{}, nil + return []byte{} } if buf == nil { @@ -642,24 +462,18 @@ func (r *GetResponseBody) StableMarshal(buf []byte) ([]byte, error) { switch v := r.objPart.(type) { case nil: case *GetObjectPartInit: - _, err := proto.NestedStructureMarshal(getRespBodyInitField, buf, v) - if err != nil { - return nil, err - } + proto.NestedStructureMarshal(getRespBodyInitField, buf, v) case *GetObjectPartChunk: if v != nil { proto.BytesMarshal(getRespBodyChunkField, buf, v.chunk) } case *SplitInfo: - _, err := proto.NestedStructureMarshal(getRespBodySplitInfoField, buf, v) - if err != nil { - return nil, err - } + proto.NestedStructureMarshal(getRespBodySplitInfoField, buf, v) default: panic("unknown one of object get response body type") } - return buf, nil + return buf } func (r *GetResponseBody) StableSize() (size int) { @@ -688,44 +502,23 @@ func (r *GetResponseBody) Unmarshal(data []byte) error { return message.Unmarshal(r, data, new(object.GetResponse_Body)) } -func (r *PutObjectPartInit) StableMarshal(buf []byte) ([]byte, error) { +func (r *PutObjectPartInit) StableMarshal(buf []byte) []byte { if r == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, r.StableSize()) } - var ( - offset, n int - err error - ) - - n, err = proto.NestedStructureMarshal(putReqInitObjectIDField, buf[offset:], r.id) - if err != nil { - return nil, err - } - - offset += n - - n, err = proto.NestedStructureMarshal(putReqInitSignatureField, buf[offset:], r.sig) - if err != nil { - return nil, err - } - - offset += n - - n, err = proto.NestedStructureMarshal(putReqInitHeaderField, buf[offset:], r.hdr) - if err != nil { - return nil, err - } - - offset += n + var offset int + offset += proto.NestedStructureMarshal(putReqInitObjectIDField, buf[offset:], r.id) + offset += proto.NestedStructureMarshal(putReqInitSignatureField, buf[offset:], r.sig) + offset += proto.NestedStructureMarshal(putReqInitHeaderField, buf[offset:], r.hdr) proto.UInt32Marshal(putReqInitCopiesNumField, buf[offset:], r.copyNum) - return buf, nil + return buf } func (r *PutObjectPartInit) StableSize() (size int) { @@ -745,9 +538,9 @@ func (r *PutObjectPartInit) Unmarshal(data []byte) error { return message.Unmarshal(r, data, new(object.PutRequest_Body_Init)) } -func (r *PutRequestBody) StableMarshal(buf []byte) ([]byte, error) { +func (r *PutRequestBody) StableMarshal(buf []byte) []byte { if r == nil { - return []byte{}, nil + return []byte{} } if buf == nil { @@ -757,10 +550,7 @@ func (r *PutRequestBody) StableMarshal(buf []byte) ([]byte, error) { switch v := r.objPart.(type) { case nil: case *PutObjectPartInit: - _, err := proto.NestedStructureMarshal(putReqBodyInitField, buf, v) - if err != nil { - return nil, err - } + proto.NestedStructureMarshal(putReqBodyInitField, buf, v) case *PutObjectPartChunk: if v != nil { proto.BytesMarshal(putReqBodyChunkField, buf, v.chunk) @@ -769,7 +559,7 @@ func (r *PutRequestBody) StableMarshal(buf []byte) ([]byte, error) { panic("unknown one of object put request body type") } - return buf, nil + return buf } func (r *PutRequestBody) StableSize() (size int) { @@ -796,21 +586,18 @@ func (r *PutRequestBody) Unmarshal(data []byte) error { return message.Unmarshal(r, data, new(object.PutRequest_Body)) } -func (r *PutResponseBody) StableMarshal(buf []byte) ([]byte, error) { +func (r *PutResponseBody) StableMarshal(buf []byte) []byte { if r == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, r.StableSize()) } - _, err := proto.NestedStructureMarshal(putRespBodyObjectIDField, buf, r.id) - if err != nil { - return nil, err - } + proto.NestedStructureMarshal(putRespBodyObjectIDField, buf, r.id) - return buf, nil + return buf } func (r *PutResponseBody) StableSize() (size int) { @@ -827,21 +614,18 @@ func (r *PutResponseBody) Unmarshal(data []byte) error { return message.Unmarshal(r, data, new(object.PutResponse_Body)) } -func (r *DeleteRequestBody) StableMarshal(buf []byte) ([]byte, error) { +func (r *DeleteRequestBody) StableMarshal(buf []byte) []byte { if r == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, r.StableSize()) } - _, err := proto.NestedStructureMarshal(deleteReqBodyAddressField, buf, r.addr) - if err != nil { - return nil, err - } + proto.NestedStructureMarshal(deleteReqBodyAddressField, buf, r.addr) - return buf, nil + return buf } func (r *DeleteRequestBody) StableSize() (size int) { @@ -858,21 +642,18 @@ func (r *DeleteRequestBody) Unmarshal(data []byte) error { return message.Unmarshal(r, data, new(object.DeleteRequest_Body)) } -func (r *DeleteResponseBody) StableMarshal(buf []byte) ([]byte, error) { +func (r *DeleteResponseBody) StableMarshal(buf []byte) []byte { if r == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, r.StableSize()) } - _, err := proto.NestedStructureMarshal(deleteRespBodyTombstoneFNum, buf, r.tombstone) - if err != nil { - return nil, err - } + proto.NestedStructureMarshal(deleteRespBodyTombstoneFNum, buf, r.tombstone) - return buf, nil + return buf } func (r *DeleteResponseBody) StableSize() (size int) { @@ -889,31 +670,22 @@ func (r *DeleteResponseBody) Unmarshal(data []byte) error { return message.Unmarshal(r, data, new(object.DeleteResponse_Body)) } -func (r *HeadRequestBody) StableMarshal(buf []byte) ([]byte, error) { +func (r *HeadRequestBody) StableMarshal(buf []byte) []byte { if r == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, r.StableSize()) } - var ( - offset, n int - err error - ) - - n, err = proto.NestedStructureMarshal(headReqBodyAddressField, buf[offset:], r.addr) - if err != nil { - return nil, err - } - - offset += n + var offset int + offset += proto.NestedStructureMarshal(headReqBodyAddressField, buf[offset:], r.addr) offset += proto.BoolMarshal(headReqBodyMainFlagField, buf[offset:], r.mainOnly) proto.BoolMarshal(headReqBodyRawFlagField, buf[offset:], r.raw) - return buf, nil + return buf } func (r *HeadRequestBody) StableSize() (size int) { @@ -932,9 +704,9 @@ func (r *HeadRequestBody) Unmarshal(data []byte) error { return message.Unmarshal(r, data, new(object.HeadRequest_Body)) } -func (r *HeadResponseBody) StableMarshal(buf []byte) ([]byte, error) { +func (r *HeadResponseBody) StableMarshal(buf []byte) []byte { if r == nil { - return []byte{}, nil + return []byte{} } if buf == nil { @@ -945,30 +717,21 @@ func (r *HeadResponseBody) StableMarshal(buf []byte) ([]byte, error) { case nil: case *HeaderWithSignature: if v != nil { - _, err := proto.NestedStructureMarshal(headRespBodyHeaderField, buf, v) - if err != nil { - return nil, err - } + proto.NestedStructureMarshal(headRespBodyHeaderField, buf, v) } case *ShortHeader: if v != nil { - _, err := proto.NestedStructureMarshal(headRespBodyShortHeaderField, buf, v) - if err != nil { - return nil, err - } + proto.NestedStructureMarshal(headRespBodyShortHeaderField, buf, v) } case *SplitInfo: if v != nil { - _, err := proto.NestedStructureMarshal(headRespBodySplitInfoField, buf, v) - if err != nil { - return nil, err - } + proto.NestedStructureMarshal(headRespBodySplitInfoField, buf, v) } default: panic("unknown one of object put request body type") } - return buf, nil + return buf } func (r *HeadResponseBody) StableSize() (size int) { @@ -1001,9 +764,9 @@ func (r *HeadResponseBody) Unmarshal(data []byte) error { return message.Unmarshal(r, data, new(object.HeadResponse_Body)) } -func (f *SearchFilter) StableMarshal(buf []byte) ([]byte, error) { +func (f *SearchFilter) StableMarshal(buf []byte) []byte { if f == nil { - return []byte{}, nil + return []byte{} } if buf == nil { @@ -1016,7 +779,7 @@ func (f *SearchFilter) StableMarshal(buf []byte) ([]byte, error) { offset += proto.StringMarshal(searchFilterNameField, buf[offset:], f.key) proto.StringMarshal(searchFilterValueField, buf[offset:], f.val) - return buf, nil + return buf } func (f *SearchFilter) StableSize() (size int) { @@ -1035,39 +798,25 @@ func (f *SearchFilter) Unmarshal(data []byte) error { return message.Unmarshal(f, data, new(object.SearchRequest_Body_Filter)) } -func (r *SearchRequestBody) StableMarshal(buf []byte) ([]byte, error) { +func (r *SearchRequestBody) StableMarshal(buf []byte) []byte { if r == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, r.StableSize()) } - var ( - offset, n int - err error - ) - - n, err = proto.NestedStructureMarshal(searchReqBodyContainerIDField, buf[offset:], r.cid) - if err != nil { - return nil, err - } - - offset += n + var offset int + offset += proto.NestedStructureMarshal(searchReqBodyContainerIDField, buf[offset:], r.cid) offset += proto.UInt32Marshal(searchReqBodyVersionField, buf[offset:], r.version) for i := range r.filters { - n, err = proto.NestedStructureMarshal(searchReqBodyFiltersField, buf[offset:], &r.filters[i]) - if err != nil { - return nil, err - } - - offset += n + offset += proto.NestedStructureMarshal(searchReqBodyFiltersField, buf[offset:], &r.filters[i]) } - return buf, nil + return buf } func (r *SearchRequestBody) StableSize() (size int) { @@ -1089,26 +838,20 @@ func (r *SearchRequestBody) Unmarshal(data []byte) error { return message.Unmarshal(r, data, new(object.SearchRequest_Body)) } -func (r *SearchResponseBody) StableMarshal(buf []byte) ([]byte, error) { +func (r *SearchResponseBody) StableMarshal(buf []byte) []byte { if r == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, r.StableSize()) } - var ( - offset int - err error - ) + var offset int - _, err = refs.ObjectIDNestedListMarshal(searchRespBodyObjectIDsField, buf[offset:], r.idList) - if err != nil { - return nil, err - } + refs.ObjectIDNestedListMarshal(searchRespBodyObjectIDsField, buf[offset:], r.idList) - return buf, nil + return buf } func (r *SearchResponseBody) StableSize() (size int) { @@ -1125,9 +868,9 @@ func (r *SearchResponseBody) Unmarshal(data []byte) error { return message.Unmarshal(r, data, new(object.SearchResponse_Body)) } -func (r *Range) StableMarshal(buf []byte) ([]byte, error) { +func (r *Range) StableMarshal(buf []byte) []byte { if r == nil { - return []byte{}, nil + return []byte{} } if buf == nil { @@ -1139,7 +882,7 @@ func (r *Range) StableMarshal(buf []byte) ([]byte, error) { offset += proto.UInt64Marshal(rangeOffsetField, buf[offset:], r.off) proto.UInt64Marshal(rangeLengthField, buf[offset:], r.len) - return buf, nil + return buf } func (r *Range) StableSize() (size int) { @@ -1157,37 +900,22 @@ func (r *Range) Unmarshal(data []byte) error { return message.Unmarshal(r, data, new(object.Range)) } -func (r *GetRangeRequestBody) StableMarshal(buf []byte) ([]byte, error) { +func (r *GetRangeRequestBody) StableMarshal(buf []byte) []byte { if r == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, r.StableSize()) } - var ( - offset, n int - err error - ) - - n, err = proto.NestedStructureMarshal(getRangeReqBodyAddressField, buf[offset:], r.addr) - if err != nil { - return nil, err - } - - offset += n - - n, err = proto.NestedStructureMarshal(getRangeReqBodyRangeField, buf[offset:], r.rng) - if err != nil { - return nil, err - } - - offset += n + var offset int + offset += proto.NestedStructureMarshal(getRangeReqBodyAddressField, buf[offset:], r.addr) + offset += proto.NestedStructureMarshal(getRangeReqBodyRangeField, buf[offset:], r.rng) proto.BoolMarshal(getRangeReqBodyRawField, buf[offset:], r.raw) - return buf, nil + return buf } func (r *GetRangeRequestBody) StableSize() (size int) { @@ -1206,9 +934,9 @@ func (r *GetRangeRequestBody) Unmarshal(data []byte) error { return message.Unmarshal(r, data, new(object.GetRangeRequest_Body)) } -func (r *GetRangeResponseBody) StableMarshal(buf []byte) ([]byte, error) { +func (r *GetRangeResponseBody) StableMarshal(buf []byte) []byte { if r == nil { - return []byte{}, nil + return []byte{} } if buf == nil { @@ -1223,16 +951,13 @@ func (r *GetRangeResponseBody) StableMarshal(buf []byte) ([]byte, error) { } case *SplitInfo: if v != nil { - _, err := proto.NestedStructureMarshal(getRangeRespSplitInfoField, buf, v) - if err != nil { - return nil, err - } + proto.NestedStructureMarshal(getRangeRespSplitInfoField, buf, v) } default: panic("unknown one of object get range request body type") } - return buf, nil + return buf } func (r *GetRangeResponseBody) StableSize() (size int) { @@ -1261,40 +986,27 @@ func (r *GetRangeResponseBody) Unmarshal(data []byte) error { return message.Unmarshal(r, data, new(object.GetRangeResponse_Body)) } -func (r *GetRangeHashRequestBody) StableMarshal(buf []byte) ([]byte, error) { +func (r *GetRangeHashRequestBody) StableMarshal(buf []byte) []byte { if r == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, r.StableSize()) } - var ( - offset, n int - err error - ) + var offset int - n, err = proto.NestedStructureMarshal(getRangeHashReqBodyAddressField, buf[offset:], r.addr) - if err != nil { - return nil, err - } - - offset += n + offset += proto.NestedStructureMarshal(getRangeHashReqBodyAddressField, buf[offset:], r.addr) for i := range r.rngs { - n, err = proto.NestedStructureMarshal(getRangeHashReqBodyRangesField, buf[offset:], &r.rngs[i]) - if err != nil { - return nil, err - } - - offset += n + offset += proto.NestedStructureMarshal(getRangeHashReqBodyRangesField, buf[offset:], &r.rngs[i]) } offset += proto.BytesMarshal(getRangeHashReqBodySaltField, buf[offset:], r.salt) proto.EnumMarshal(getRangeHashReqBodyTypeField, buf[offset:], int32(r.typ)) - return buf, nil + return buf } func (r *GetRangeHashRequestBody) StableSize() (size int) { @@ -1318,9 +1030,9 @@ func (r *GetRangeHashRequestBody) Unmarshal(data []byte) error { return message.Unmarshal(r, data, new(object.GetRangeHashRequest_Body)) } -func (r *GetRangeHashResponseBody) StableMarshal(buf []byte) ([]byte, error) { +func (r *GetRangeHashResponseBody) StableMarshal(buf []byte) []byte { if r == nil { - return []byte{}, nil + return []byte{} } if buf == nil { @@ -1332,7 +1044,7 @@ func (r *GetRangeHashResponseBody) StableMarshal(buf []byte) ([]byte, error) { offset += proto.EnumMarshal(getRangeHashRespBodyTypeField, buf, int32(r.typ)) proto.RepeatedBytesMarshal(getRangeHashRespBodyHashListField, buf[offset:], r.hashList) - return buf, nil + return buf } func (r *GetRangeHashResponseBody) StableSize() (size int) { diff --git a/refs/bench_test.go b/refs/bench_test.go index aed5f79..b24e674 100644 --- a/refs/bench_test.go +++ b/refs/bench_test.go @@ -44,8 +44,8 @@ func benchmarkObjectIDSlice(b *testing.B, size int) { b.ReportAllocs() for i := 0; i < b.N; i++ { buf := make([]byte, ObjectIDNestedListSize(1, ids)) - _, err := ObjectIDNestedListMarshal(1, buf, ids) - if err != nil { + n := ObjectIDNestedListMarshal(1, buf, ids) + if n != len(buf) { b.FailNow() } } diff --git a/refs/marshal.go b/refs/marshal.go index ffb1ba5..0a17a51 100644 --- a/refs/marshal.go +++ b/refs/marshal.go @@ -27,9 +27,9 @@ const ( versionMinorField = 2 ) -func (o *OwnerID) StableMarshal(buf []byte) ([]byte, error) { +func (o *OwnerID) StableMarshal(buf []byte) []byte { if o == nil { - return []byte{}, nil + return []byte{} } if buf == nil { @@ -38,7 +38,7 @@ func (o *OwnerID) StableMarshal(buf []byte) ([]byte, error) { proto.BytesMarshal(ownerIDValField, buf, o.val) - return buf, nil + return buf } func (o *OwnerID) StableSize() int { @@ -53,9 +53,9 @@ func (o *OwnerID) Unmarshal(data []byte) error { return message.Unmarshal(o, data, new(refs.OwnerID)) } -func (c *ContainerID) StableMarshal(buf []byte) ([]byte, error) { +func (c *ContainerID) StableMarshal(buf []byte) []byte { if c == nil { - return []byte{}, nil + return []byte{} } if buf == nil { @@ -64,7 +64,7 @@ func (c *ContainerID) StableMarshal(buf []byte) ([]byte, error) { proto.BytesMarshal(containerIDValField, buf, c.val) - return buf, nil + return buf } func (c *ContainerID) StableSize() int { @@ -79,9 +79,9 @@ func (c *ContainerID) Unmarshal(data []byte) error { return message.Unmarshal(c, data, new(refs.ContainerID)) } -func (o *ObjectID) StableMarshal(buf []byte) ([]byte, error) { +func (o *ObjectID) StableMarshal(buf []byte) []byte { if o == nil { - return []byte{}, nil + return []byte{} } if buf == nil { @@ -90,7 +90,7 @@ func (o *ObjectID) StableMarshal(buf []byte) ([]byte, error) { proto.BytesMarshal(objectIDValField, buf, o.val) - return buf, nil + return buf } // ObjectIDNestedListSize returns byte length of nested @@ -113,16 +113,9 @@ func (o *ObjectID) StableSize() int { // ObjectIDNestedListMarshal writes protobuf repeated ObjectID field // with fNum number to buf. -func ObjectIDNestedListMarshal(fNum int64, buf []byte, ids []ObjectID) (off int, err error) { +func ObjectIDNestedListMarshal(fNum int64, buf []byte, ids []ObjectID) (off int) { for i := range ids { - var n int - - n, err = proto.NestedStructureMarshal(fNum, buf[off:], &ids[i]) - if err != nil { - return - } - - off += n + off += proto.NestedStructureMarshal(fNum, buf[off:], &ids[i]) } return @@ -132,33 +125,21 @@ func (o *ObjectID) Unmarshal(data []byte) error { return message.Unmarshal(o, data, new(refs.ObjectID)) } -func (a *Address) StableMarshal(buf []byte) ([]byte, error) { +func (a *Address) StableMarshal(buf []byte) []byte { if a == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, a.StableSize()) } - var ( - offset, n int - err error - ) + var offset int - n, err = proto.NestedStructureMarshal(addressContainerField, buf[offset:], a.cid) - if err != nil { - return nil, err - } + offset += proto.NestedStructureMarshal(addressContainerField, buf[offset:], a.cid) + proto.NestedStructureMarshal(addressObjectField, buf[offset:], a.oid) - offset += n - - _, err = proto.NestedStructureMarshal(addressObjectField, buf[offset:], a.oid) - if err != nil { - return nil, err - } - - return buf, nil + return buf } func (a *Address) StableSize() (size int) { @@ -167,7 +148,6 @@ func (a *Address) StableSize() (size int) { } size += proto.NestedStructureSize(addressContainerField, a.cid) - size += proto.NestedStructureSize(addressObjectField, a.oid) return size @@ -177,9 +157,9 @@ func (a *Address) Unmarshal(data []byte) error { return message.Unmarshal(a, data, new(refs.Address)) } -func (c *Checksum) StableMarshal(buf []byte) ([]byte, error) { +func (c *Checksum) StableMarshal(buf []byte) []byte { if c == nil { - return []byte{}, nil + return []byte{} } if buf == nil { @@ -191,7 +171,7 @@ func (c *Checksum) StableMarshal(buf []byte) ([]byte, error) { offset += proto.EnumMarshal(checksumTypeField, buf[offset:], int32(c.typ)) proto.BytesMarshal(checksumValueField, buf[offset:], c.sum) - return buf, nil + return buf } func (c *Checksum) StableSize() (size int) { @@ -209,9 +189,9 @@ func (c *Checksum) Unmarshal(data []byte) error { return message.Unmarshal(c, data, new(refs.Checksum)) } -func (s *Signature) StableMarshal(buf []byte) ([]byte, error) { +func (s *Signature) StableMarshal(buf []byte) []byte { if s == nil { - return []byte{}, nil + return []byte{} } if buf == nil { @@ -224,7 +204,7 @@ func (s *Signature) StableMarshal(buf []byte) ([]byte, error) { offset += proto.BytesMarshal(signatureValueField, buf[offset:], s.sign) proto.EnumMarshal(signatureSchemeField, buf[offset:], int32(s.scheme)) - return buf, nil + return buf } func (s *Signature) StableSize() (size int) { @@ -243,9 +223,9 @@ func (s *Signature) Unmarshal(data []byte) error { return message.Unmarshal(s, data, new(refs.Signature)) } -func (v *Version) StableMarshal(buf []byte) ([]byte, error) { +func (v *Version) StableMarshal(buf []byte) []byte { if v == nil { - return []byte{}, nil + return []byte{} } if buf == nil { @@ -257,7 +237,7 @@ func (v *Version) StableMarshal(buf []byte) ([]byte, error) { offset += proto.UInt32Marshal(versionMajorField, buf[offset:], v.major) proto.UInt32Marshal(versionMinorField, buf[offset:], v.minor) - return buf, nil + return buf } func (v *Version) StableSize() (size int) { @@ -284,9 +264,9 @@ const ( // StableMarshal marshals SubnetID to NeoFS API V2 binary format (Protocol Buffers with direct field order). // // Returns a slice of recorded data. Data is written to the provided buffer if there is enough space. -func (s *SubnetID) StableMarshal(buf []byte) ([]byte, error) { +func (s *SubnetID) StableMarshal(buf []byte) []byte { if s == nil { - return []byte{}, nil + return []byte{} } if buf == nil { @@ -295,7 +275,7 @@ func (s *SubnetID) StableMarshal(buf []byte) ([]byte, error) { proto.Fixed32Marshal(subnetIDValFNum, buf, s.value) - return buf, nil + return buf } // StableSize returns the number of bytes required to write SubnetID in NeoFS API V2 binary format (see StableMarshal). diff --git a/reputation/marshal.go b/reputation/marshal.go index 789cb14..83b9a5a 100644 --- a/reputation/marshal.go +++ b/reputation/marshal.go @@ -11,9 +11,9 @@ const ( peerIDPubKeyFNum ) -func (x *PeerID) StableMarshal(buf []byte) ([]byte, error) { +func (x *PeerID) StableMarshal(buf []byte) []byte { if x == nil { - return []byte{}, nil + return []byte{} } if buf == nil { @@ -22,7 +22,7 @@ func (x *PeerID) StableMarshal(buf []byte) ([]byte, error) { protoutil.BytesMarshal(peerIDPubKeyFNum, buf, x.publicKey) - return buf, nil + return buf } func (x *PeerID) StableSize() (size int) { @@ -41,30 +41,21 @@ const ( trustValueFNum ) -func (x *Trust) StableMarshal(buf []byte) ([]byte, error) { +func (x *Trust) StableMarshal(buf []byte) []byte { if x == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, x.StableSize()) } - var ( - offset, n int - err error - ) - - n, err = protoutil.NestedStructureMarshal(trustPeerFNum, buf[offset:], x.peer) - if err != nil { - return nil, err - } - - offset += n + var offset int + offset += protoutil.NestedStructureMarshal(trustPeerFNum, buf[offset:], x.peer) protoutil.Float64Marshal(trustValueFNum, buf[offset:], x.val) - return buf, nil + return buf } func (x *Trust) StableSize() (size int) { @@ -84,26 +75,21 @@ const ( p2pTrustValueFNum ) -func (x *PeerToPeerTrust) StableMarshal(buf []byte) ([]byte, error) { +func (x *PeerToPeerTrust) StableMarshal(buf []byte) []byte { if x == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, x.StableSize()) } - offset, err := protoutil.NestedStructureMarshal(p2pTrustTrustingFNum, buf, x.trusting) - if err != nil { - return nil, err - } + var offset int - _, err = protoutil.NestedStructureMarshal(p2pTrustValueFNum, buf[offset:], x.trust) - if err != nil { - return nil, err - } + offset += protoutil.NestedStructureMarshal(p2pTrustTrustingFNum, buf, x.trusting) + protoutil.NestedStructureMarshal(p2pTrustValueFNum, buf[offset:], x.trust) - return buf, nil + return buf } func (x *PeerToPeerTrust) StableSize() (size int) { @@ -123,26 +109,21 @@ const ( globalTrustBodyValueFNum ) -func (x *GlobalTrustBody) StableMarshal(buf []byte) ([]byte, error) { +func (x *GlobalTrustBody) StableMarshal(buf []byte) []byte { if x == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, x.StableSize()) } - offset, err := protoutil.NestedStructureMarshal(globalTrustBodyManagerFNum, buf, x.manager) - if err != nil { - return nil, err - } + var offset int - _, err = protoutil.NestedStructureMarshal(globalTrustBodyValueFNum, buf[offset:], x.trust) - if err != nil { - return nil, err - } + offset += protoutil.NestedStructureMarshal(globalTrustBodyManagerFNum, buf, x.manager) + protoutil.NestedStructureMarshal(globalTrustBodyValueFNum, buf[offset:], x.trust) - return buf, nil + return buf } func (x *GlobalTrustBody) StableSize() (size int) { @@ -163,33 +144,22 @@ const ( globalTrustSigFNum ) -func (x *GlobalTrust) StableMarshal(buf []byte) ([]byte, error) { +func (x *GlobalTrust) StableMarshal(buf []byte) []byte { if x == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, x.StableSize()) } - offset, err := protoutil.NestedStructureMarshal(globalTrustVersionFNum, buf, x.version) - if err != nil { - return nil, err - } + var offset int - n, err := protoutil.NestedStructureMarshal(globalTrustBodyFNum, buf[offset:], x.body) - if err != nil { - return nil, err - } + offset += protoutil.NestedStructureMarshal(globalTrustVersionFNum, buf, x.version) + offset += protoutil.NestedStructureMarshal(globalTrustBodyFNum, buf[offset:], x.body) + protoutil.NestedStructureMarshal(globalTrustSigFNum, buf[offset:], x.sig) - offset += n - - _, err = protoutil.NestedStructureMarshal(globalTrustSigFNum, buf[offset:], x.sig) - if err != nil { - return nil, err - } - - return buf, nil + return buf } func (x *GlobalTrust) StableSize() (size int) { @@ -210,32 +180,24 @@ const ( announceLocalTrustBodyTrustsFNum ) -func (x *AnnounceLocalTrustRequestBody) StableMarshal(buf []byte) ([]byte, error) { +func (x *AnnounceLocalTrustRequestBody) StableMarshal(buf []byte) []byte { if x == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, x.StableSize()) } - var ( - offset, n int - err error - ) + var offset int offset += protoutil.UInt64Marshal(announceLocalTrustBodyEpochFNum, buf[offset:], x.epoch) for i := range x.trusts { - n, err = protoutil.NestedStructureMarshal(announceLocalTrustBodyTrustsFNum, buf[offset:], &x.trusts[i]) - if err != nil { - return nil, err - } - - offset += n + offset += protoutil.NestedStructureMarshal(announceLocalTrustBodyTrustsFNum, buf[offset:], &x.trusts[i]) } - return buf, nil + return buf } func (x *AnnounceLocalTrustRequestBody) StableSize() (size int) { @@ -252,8 +214,8 @@ func (x *AnnounceLocalTrustRequestBody) Unmarshal(data []byte) error { return message.Unmarshal(x, data, new(reputation.AnnounceLocalTrustRequest_Body)) } -func (x *AnnounceLocalTrustResponseBody) StableMarshal(buf []byte) ([]byte, error) { - return buf, nil +func (x *AnnounceLocalTrustResponseBody) StableMarshal(buf []byte) []byte { + return buf } func (x *AnnounceLocalTrustResponseBody) StableSize() int { @@ -271,29 +233,22 @@ const ( announceInterResBodyTrustFNum ) -func (x *AnnounceIntermediateResultRequestBody) StableMarshal(buf []byte) ([]byte, error) { +func (x *AnnounceIntermediateResultRequestBody) StableMarshal(buf []byte) []byte { if x == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, x.StableSize()) } - var ( - offset int - err error - ) + var offset int offset += protoutil.UInt64Marshal(announceInterResBodyEpochFNum, buf, x.epoch) offset += protoutil.UInt32Marshal(announceInterResBodyIterFNum, buf[offset:], x.iter) + protoutil.NestedStructureMarshal(announceInterResBodyTrustFNum, buf[offset:], x.trust) - _, err = protoutil.NestedStructureMarshal(announceInterResBodyTrustFNum, buf[offset:], x.trust) - if err != nil { - return nil, err - } - - return buf, nil + return buf } func (x *AnnounceIntermediateResultRequestBody) StableSize() (size int) { @@ -308,8 +263,8 @@ func (x *AnnounceIntermediateResultRequestBody) Unmarshal(data []byte) error { return message.Unmarshal(x, data, new(reputation.AnnounceIntermediateResultRequest_Body)) } -func (x *AnnounceIntermediateResultResponseBody) StableMarshal(buf []byte) ([]byte, error) { - return buf, nil +func (x *AnnounceIntermediateResultResponseBody) StableMarshal(buf []byte) []byte { + return buf } func (x *AnnounceIntermediateResultResponseBody) StableSize() int { diff --git a/session/marshal.go b/session/marshal.go index 062076d..d7db4d2 100644 --- a/session/marshal.go +++ b/session/marshal.go @@ -61,30 +61,21 @@ const ( respVerifHeaderOriginField = 4 ) -func (c *CreateRequestBody) StableMarshal(buf []byte) ([]byte, error) { +func (c *CreateRequestBody) StableMarshal(buf []byte) []byte { if c == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, c.StableSize()) } - var ( - offset, n int - err error - ) - - n, err = proto.NestedStructureMarshal(createReqBodyOwnerField, buf[offset:], c.ownerID) - if err != nil { - return nil, err - } - - offset += n + var offset int + offset += proto.NestedStructureMarshal(createReqBodyOwnerField, buf[offset:], c.ownerID) proto.UInt64Marshal(createReqBodyExpirationField, buf[offset:], c.expiration) - return buf, nil + return buf } func (c *CreateRequestBody) StableSize() (size int) { @@ -102,9 +93,9 @@ func (c *CreateRequestBody) Unmarshal(data []byte) error { return message.Unmarshal(c, data, new(session.CreateRequest_Body)) } -func (c *CreateResponseBody) StableMarshal(buf []byte) ([]byte, error) { +func (c *CreateResponseBody) StableMarshal(buf []byte) []byte { if c == nil { - return []byte{}, nil + return []byte{} } if buf == nil { @@ -116,7 +107,7 @@ func (c *CreateResponseBody) StableMarshal(buf []byte) ([]byte, error) { offset += proto.BytesMarshal(createRespBodyIDField, buf[offset:], c.id) proto.BytesMarshal(createRespBodyKeyField, buf[offset:], c.sessionKey) - return buf, nil + return buf } func (c *CreateResponseBody) StableSize() (size int) { @@ -134,9 +125,9 @@ func (c *CreateResponseBody) Unmarshal(data []byte) error { return message.Unmarshal(c, data, new(session.CreateResponse_Body)) } -func (x *XHeader) StableMarshal(buf []byte) ([]byte, error) { +func (x *XHeader) StableMarshal(buf []byte) []byte { if x == nil { - return []byte{}, nil + return []byte{} } if buf == nil { @@ -148,7 +139,7 @@ func (x *XHeader) StableMarshal(buf []byte) ([]byte, error) { offset += proto.StringMarshal(xheaderKeyField, buf[offset:], x.key) proto.StringMarshal(xheaderValueField, buf[offset:], x.val) - return buf, nil + return buf } func (x *XHeader) StableSize() (size int) { @@ -171,9 +162,9 @@ func (x *XHeader) Unmarshal(data []byte) error { return x.FromGRPCMessage(m) } -func (l *TokenLifetime) StableMarshal(buf []byte) ([]byte, error) { +func (l *TokenLifetime) StableMarshal(buf []byte) []byte { if l == nil { - return []byte{}, nil + return []byte{} } if buf == nil { @@ -186,7 +177,7 @@ func (l *TokenLifetime) StableMarshal(buf []byte) ([]byte, error) { offset += proto.UInt64Marshal(lifetimeNotValidBeforeField, buf[offset:], l.nbf) proto.UInt64Marshal(lifetimeIssuedAtField, buf[offset:], l.iat) - return buf, nil + return buf } func (l *TokenLifetime) StableSize() (size int) { @@ -210,9 +201,9 @@ func (l *TokenLifetime) Unmarshal(data []byte) error { return l.FromGRPCMessage(m) } -func (c *ObjectSessionContext) StableMarshal(buf []byte) ([]byte, error) { +func (c *ObjectSessionContext) StableMarshal(buf []byte) []byte { if c == nil { - return []byte{}, nil + return []byte{} } if buf == nil { @@ -224,7 +215,7 @@ func (c *ObjectSessionContext) StableMarshal(buf []byte) ([]byte, error) { offset += proto.EnumMarshal(objectCtxVerbField, buf[offset:], int32(c.verb)) proto.NestedStructureMarshal(objectCtxAddressField, buf[offset:], c.addr) - return buf, nil + return buf } func (c *ObjectSessionContext) StableSize() (size int) { @@ -254,9 +245,9 @@ const ( cnrCtxCidFNum ) -func (x *ContainerSessionContext) StableMarshal(buf []byte) ([]byte, error) { +func (x *ContainerSessionContext) StableMarshal(buf []byte) []byte { if x == nil { - return []byte{}, nil + return []byte{} } if buf == nil { @@ -269,7 +260,7 @@ func (x *ContainerSessionContext) StableMarshal(buf []byte) ([]byte, error) { offset += proto.BoolMarshal(cnrCtxWildcardFNum, buf[offset:], x.wildcard) proto.NestedStructureMarshal(cnrCtxCidFNum, buf[offset:], x.cid) - return buf, nil + return buf } func (x *ContainerSessionContext) StableSize() (size int) { @@ -288,56 +279,34 @@ func (x *ContainerSessionContext) Unmarshal(data []byte) error { return message.Unmarshal(x, data, new(session.ContainerSessionContext)) } -func (t *TokenBody) StableMarshal(buf []byte) ([]byte, error) { +func (t *TokenBody) StableMarshal(buf []byte) []byte { if t == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, t.StableSize()) } - var ( - offset, n int - err error - ) + var offset int offset += proto.BytesMarshal(sessionTokenBodyIDField, buf[offset:], t.id) - - n, err = proto.NestedStructureMarshal(sessionTokenBodyOwnerField, buf[offset:], t.ownerID) - if err != nil { - return nil, err - } - - offset += n - - n, err = proto.NestedStructureMarshal(sessionTokenBodyLifetimeField, buf[offset:], t.lifetime) - if err != nil { - return nil, err - } - - offset += n - + offset += proto.NestedStructureMarshal(sessionTokenBodyOwnerField, buf[offset:], t.ownerID) + offset += proto.NestedStructureMarshal(sessionTokenBodyLifetimeField, buf[offset:], t.lifetime) offset += proto.BytesMarshal(sessionTokenBodyKeyField, buf[offset:], t.sessionKey) if t.ctx != nil { switch v := t.ctx.(type) { case *ObjectSessionContext: - _, err = proto.NestedStructureMarshal(sessionTokenBodyObjectCtxField, buf[offset:], v) - if err != nil { - return nil, err - } + proto.NestedStructureMarshal(sessionTokenBodyObjectCtxField, buf[offset:], v) case *ContainerSessionContext: - _, err = proto.NestedStructureMarshal(sessionTokenBodyCnrCtxField, buf[offset:], v) - if err != nil { - return nil, err - } + proto.NestedStructureMarshal(sessionTokenBodyCnrCtxField, buf[offset:], v) default: panic("cannot marshal unknown session token context") } } - return buf, nil + return buf } func (t *TokenBody) StableSize() (size int) { @@ -373,33 +342,21 @@ func (t *TokenBody) Unmarshal(data []byte) error { return t.FromGRPCMessage(m) } -func (t *Token) StableMarshal(buf []byte) ([]byte, error) { +func (t *Token) StableMarshal(buf []byte) []byte { if t == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, t.StableSize()) } - var ( - offset, n int - err error - ) + var offset int - n, err = proto.NestedStructureMarshal(sessionTokenBodyField, buf[offset:], t.body) - if err != nil { - return nil, err - } + offset += proto.NestedStructureMarshal(sessionTokenBodyField, buf[offset:], t.body) + proto.NestedStructureMarshal(sessionTokenSignatureField, buf[offset:], t.sig) - offset += n - - _, err = proto.NestedStructureMarshal(sessionTokenSignatureField, buf[offset:], t.sig) - if err != nil { - return nil, err - } - - return buf, nil + return buf } func (t *Token) StableSize() (size int) { @@ -422,63 +379,31 @@ func (t *Token) Unmarshal(data []byte) error { return t.FromGRPCMessage(m) } -func (r *RequestMetaHeader) StableMarshal(buf []byte) ([]byte, error) { +func (r *RequestMetaHeader) StableMarshal(buf []byte) []byte { if r == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, r.StableSize()) } - var ( - offset, n int - err error - ) - - n, err = proto.NestedStructureMarshal(reqMetaHeaderVersionField, buf[offset:], r.version) - if err != nil { - return nil, err - } - - offset += n + var offset int + offset += proto.NestedStructureMarshal(reqMetaHeaderVersionField, buf[offset:], r.version) offset += proto.UInt64Marshal(reqMetaHeaderEpochField, buf[offset:], r.epoch) offset += proto.UInt32Marshal(reqMetaHeaderTTLField, buf[offset:], r.ttl) for i := range r.xHeaders { - n, err = proto.NestedStructureMarshal(reqMetaHeaderXHeadersField, buf[offset:], &r.xHeaders[i]) - if err != nil { - return nil, err - } - - offset += n + offset += proto.NestedStructureMarshal(reqMetaHeaderXHeadersField, buf[offset:], &r.xHeaders[i]) } - n, err = proto.NestedStructureMarshal(reqMetaHeaderSessionTokenField, buf[offset:], r.sessionToken) - if err != nil { - return nil, err - } - - offset += n - - n, err = proto.NestedStructureMarshal(reqMetaHeaderBearerTokenField, buf[offset:], r.bearerToken) - if err != nil { - return nil, err - } - - offset += n - - n, err = proto.NestedStructureMarshal(reqMetaHeaderOriginField, buf[offset:], r.origin) - if err != nil { - return nil, err - } - - offset += n - + offset += proto.NestedStructureMarshal(reqMetaHeaderSessionTokenField, buf[offset:], r.sessionToken) + offset += proto.NestedStructureMarshal(reqMetaHeaderBearerTokenField, buf[offset:], r.bearerToken) + offset += proto.NestedStructureMarshal(reqMetaHeaderOriginField, buf[offset:], r.origin) proto.UInt64Marshal(reqMetaHeaderNetMagicField, buf[offset:], r.netMagic) - return buf, nil + return buf } func (r *RequestMetaHeader) StableSize() (size int) { @@ -514,47 +439,23 @@ func (r *RequestMetaHeader) Unmarshal(data []byte) error { return r.FromGRPCMessage(m) } -func (r *RequestVerificationHeader) StableMarshal(buf []byte) ([]byte, error) { +func (r *RequestVerificationHeader) StableMarshal(buf []byte) []byte { if r == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, r.StableSize()) } - var ( - offset, n int - err error - ) + var offset int - n, err = proto.NestedStructureMarshal(reqVerifHeaderBodySignatureField, buf[offset:], r.bodySig) - if err != nil { - return nil, err - } + offset += proto.NestedStructureMarshal(reqVerifHeaderBodySignatureField, buf[offset:], r.bodySig) + offset += proto.NestedStructureMarshal(reqVerifHeaderMetaSignatureField, buf[offset:], r.metaSig) + offset += proto.NestedStructureMarshal(reqVerifHeaderOriginSignatureField, buf[offset:], r.originSig) + proto.NestedStructureMarshal(reqVerifHeaderOriginField, buf[offset:], r.origin) - offset += n - - n, err = proto.NestedStructureMarshal(reqVerifHeaderMetaSignatureField, buf[offset:], r.metaSig) - if err != nil { - return nil, err - } - - offset += n - - n, err = proto.NestedStructureMarshal(reqVerifHeaderOriginSignatureField, buf[offset:], r.originSig) - if err != nil { - return nil, err - } - - offset += n - - _, err = proto.NestedStructureMarshal(reqVerifHeaderOriginField, buf[offset:], r.origin) - if err != nil { - return nil, err - } - - return buf, nil + return buf } func (r *RequestVerificationHeader) StableSize() (size int) { @@ -579,52 +480,29 @@ func (r *RequestVerificationHeader) Unmarshal(data []byte) error { return r.FromGRPCMessage(m) } -func (r *ResponseMetaHeader) StableMarshal(buf []byte) ([]byte, error) { +func (r *ResponseMetaHeader) StableMarshal(buf []byte) []byte { if r == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, r.StableSize()) } - var ( - offset, n int - err error - ) - - n, err = proto.NestedStructureMarshal(respMetaHeaderVersionField, buf[offset:], r.version) - if err != nil { - return nil, err - } - - offset += n + var offset int + offset += proto.NestedStructureMarshal(respMetaHeaderVersionField, buf[offset:], r.version) offset += proto.UInt64Marshal(respMetaHeaderEpochField, buf[offset:], r.epoch) offset += proto.UInt32Marshal(respMetaHeaderTTLField, buf[offset:], r.ttl) for i := range r.xHeaders { - n, err = proto.NestedStructureMarshal(respMetaHeaderXHeadersField, buf[offset:], &r.xHeaders[i]) - if err != nil { - return nil, err - } - - offset += n + offset += proto.NestedStructureMarshal(respMetaHeaderXHeadersField, buf[offset:], &r.xHeaders[i]) } - n, err = proto.NestedStructureMarshal(respMetaHeaderOriginField, buf[offset:], r.origin) - if err != nil { - return nil, err - } + offset += proto.NestedStructureMarshal(respMetaHeaderOriginField, buf[offset:], r.origin) + proto.NestedStructureMarshal(respMetaHeaderStatusField, buf[offset:], r.status) - offset += n - - _, err = proto.NestedStructureMarshal(respMetaHeaderStatusField, buf[offset:], r.status) - if err != nil { - return nil, err - } - - return buf, nil + return buf } func (r *ResponseMetaHeader) StableSize() (size int) { @@ -658,47 +536,23 @@ func (r *ResponseMetaHeader) Unmarshal(data []byte) error { return r.FromGRPCMessage(m) } -func (r *ResponseVerificationHeader) StableMarshal(buf []byte) ([]byte, error) { +func (r *ResponseVerificationHeader) StableMarshal(buf []byte) []byte { if r == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, r.StableSize()) } - var ( - offset, n int - err error - ) + var offset int - n, err = proto.NestedStructureMarshal(respVerifHeaderBodySignatureField, buf[offset:], r.bodySig) - if err != nil { - return nil, err - } + offset += proto.NestedStructureMarshal(respVerifHeaderBodySignatureField, buf[offset:], r.bodySig) + offset += proto.NestedStructureMarshal(respVerifHeaderMetaSignatureField, buf[offset:], r.metaSig) + offset += proto.NestedStructureMarshal(respVerifHeaderOriginSignatureField, buf[offset:], r.originSig) + proto.NestedStructureMarshal(respVerifHeaderOriginField, buf[offset:], r.origin) - offset += n - - n, err = proto.NestedStructureMarshal(respVerifHeaderMetaSignatureField, buf[offset:], r.metaSig) - if err != nil { - return nil, err - } - - offset += n - - n, err = proto.NestedStructureMarshal(respVerifHeaderOriginSignatureField, buf[offset:], r.originSig) - if err != nil { - return nil, err - } - - offset += n - - _, err = proto.NestedStructureMarshal(respVerifHeaderOriginField, buf[offset:], r.origin) - if err != nil { - return nil, err - } - - return buf, nil + return buf } func (r *ResponseVerificationHeader) StableSize() (size int) { diff --git a/signature/sign.go b/signature/sign.go index c954891..e6bb194 100644 --- a/signature/sign.go +++ b/signature/sign.go @@ -28,7 +28,7 @@ type serviceResponse interface { } type stableMarshaler interface { - StableMarshal([]byte) ([]byte, error) + StableMarshal([]byte) []byte StableSize() int } @@ -117,7 +117,7 @@ func (r *responseVerificationHeader) setOrigin(m stableMarshaler) { func (s StableMarshalerWrapper) ReadSignedData(buf []byte) ([]byte, error) { if s.SM != nil { - return s.SM.StableMarshal(buf) + return s.SM.StableMarshal(buf), nil } return nil, nil diff --git a/status/marshal.go b/status/marshal.go index 1500198..968a6fc 100644 --- a/status/marshal.go +++ b/status/marshal.go @@ -12,9 +12,9 @@ const ( detailValueFNum ) -func (x *Detail) StableMarshal(buf []byte) ([]byte, error) { +func (x *Detail) StableMarshal(buf []byte) []byte { if x == nil { - return []byte{}, nil + return []byte{} } if buf == nil { @@ -24,9 +24,9 @@ func (x *Detail) StableMarshal(buf []byte) ([]byte, error) { var offset int offset += protoutil.UInt32Marshal(detailIDFNum, buf[offset:], x.id) - offset += protoutil.BytesMarshal(detailValueFNum, buf[offset:], x.val) + protoutil.BytesMarshal(detailValueFNum, buf[offset:], x.val) - return buf, nil + return buf } func (x *Detail) StableSize() (size int) { @@ -47,40 +47,25 @@ const ( statusDetailsFNum ) -func (x *Status) StableMarshal(buf []byte) ([]byte, error) { +func (x *Status) StableMarshal(buf []byte) []byte { if x == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, x.StableSize()) } - var ( - offset, n int - err error - ) + var offset int offset += protoutil.UInt32Marshal(statusCodeFNum, buf[offset:], CodeToGRPC(x.code)) - if err != nil { - return nil, err - } - offset += protoutil.StringMarshal(statusMsgFNum, buf[offset:], x.msg) - if err != nil { - return nil, err - } for i := range x.details { - n, err = protoutil.NestedStructureMarshal(statusDetailsFNum, buf[offset:], &x.details[i]) - if err != nil { - return nil, err - } - - offset += n + offset += protoutil.NestedStructureMarshal(statusDetailsFNum, buf[offset:], &x.details[i]) } - return buf, nil + return buf } func (x *Status) StableSize() (size int) { diff --git a/storagegroup/marshal.go b/storagegroup/marshal.go index d5e60c1..22e0ca9 100644 --- a/storagegroup/marshal.go +++ b/storagegroup/marshal.go @@ -16,37 +16,23 @@ const ( // StableMarshal marshals unified storage group structure in a protobuf // compatible way without field order shuffle. -func (s *StorageGroup) StableMarshal(buf []byte) ([]byte, error) { +func (s *StorageGroup) StableMarshal(buf []byte) []byte { if s == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, s.StableSize()) } - var ( - offset, n int - err error - ) + var offset int offset += proto.UInt64Marshal(sizeField, buf[offset:], s.size) - - n, err = proto.NestedStructureMarshal(hashField, buf[offset:], s.hash) - if err != nil { - return nil, err - } - - offset += n - + offset += proto.NestedStructureMarshal(hashField, buf[offset:], s.hash) offset += proto.UInt64Marshal(expirationField, buf[offset:], s.exp) + refs.ObjectIDNestedListMarshal(objectIDsField, buf[offset:], s.members) - _, err = refs.ObjectIDNestedListMarshal(objectIDsField, buf[offset:], s.members) - if err != nil { - return nil, err - } - - return buf, nil + return buf } // StableSize of storage group structure marshalled by StableMarshal function. diff --git a/subnet/info.go b/subnet/info.go index 0b4cd16..16efb98 100644 --- a/subnet/info.go +++ b/subnet/info.go @@ -105,33 +105,21 @@ const ( // StableMarshal marshals Info to NeoFS API V2 binary format (Protocol Buffers with direct field order). // // Returns a slice of recorded data. Data is written to the provided buffer if there is enough space. -func (x *Info) StableMarshal(buf []byte) ([]byte, error) { +func (x *Info) StableMarshal(buf []byte) []byte { if x == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, x.StableSize()) } - var ( - offset, n int - err error - ) + var offset int - n, err = protoutil.NestedStructureMarshal(subnetInfoIDFNum, buf[offset:], x.id) - if err != nil { - return nil, err - } + offset += protoutil.NestedStructureMarshal(subnetInfoIDFNum, buf[offset:], x.id) + protoutil.NestedStructureMarshal(subnetInfoOwnerFNum, buf[offset:], x.owner) - offset += n - - _, err = protoutil.NestedStructureMarshal(subnetInfoOwnerFNum, buf[offset:], x.owner) - if err != nil { - return nil, err - } - - return buf, nil + return buf } // StableSize returns the number of bytes required to write Info in NeoFS API V2 binary format (see StableMarshal). diff --git a/tombstone/marshal.go b/tombstone/marshal.go index f2f896a..62497db 100644 --- a/tombstone/marshal.go +++ b/tombstone/marshal.go @@ -14,33 +14,25 @@ const ( // StableMarshal marshals unified tombstone message in a protobuf // compatible way without field order shuffle. -func (s *Tombstone) StableMarshal(buf []byte) ([]byte, error) { +func (s *Tombstone) StableMarshal(buf []byte) []byte { if s == nil { - return []byte{}, nil + return []byte{} } if buf == nil { buf = make([]byte, s.StableSize()) } - var ( - offset, n int - err error - ) + var offset int offset += proto.UInt64Marshal(expFNum, buf[offset:], s.exp) offset += proto.BytesMarshal(splitIDFNum, buf[offset:], s.splitID) for i := range s.members { - n, err = proto.NestedStructureMarshal(membersFNum, buf[offset:], &s.members[i]) - if err != nil { - return nil, err - } - - offset += n + offset += proto.NestedStructureMarshal(membersFNum, buf[offset:], &s.members[i]) } - return buf, nil + return buf } // StableSize returns size of tombstone message marshalled by StableMarshal function. diff --git a/util/proto/marshal.go b/util/proto/marshal.go index a1bb1e9..15c67a2 100644 --- a/util/proto/marshal.go +++ b/util/proto/marshal.go @@ -15,7 +15,7 @@ import ( type ( stableMarshaller interface { - StableMarshal([]byte) ([]byte, error) + StableMarshal([]byte) []byte StableSize() int } ) @@ -296,9 +296,9 @@ func NestedStructurePrefix(field int64) (prefix uint64, ln int) { return prefix, VarUIntSize(prefix) } -func NestedStructureMarshal(field int64, buf []byte, v stableMarshaller) (int, error) { +func NestedStructureMarshal(field int64, buf []byte, v stableMarshaller) int { if v == nil || reflect.ValueOf(v).IsNil() { - return 0, nil + return 0 } prefix, _ := NestedStructurePrefix(field) @@ -306,13 +306,9 @@ func NestedStructureMarshal(field int64, buf []byte, v stableMarshaller) (int, e n := v.StableSize() offset += binary.PutUvarint(buf[offset:], uint64(n)) + v.StableMarshal(buf[offset:]) - _, err := v.StableMarshal(buf[offset:]) - if err != nil { - return 0, err - } - - return offset + n, nil + return offset + n } func NestedStructureSize(field int64, v stableMarshaller) (size int) {