From f1addc4cc52a8d172cac7f888cebaee551e4240e Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Thu, 20 Aug 2020 11:48:35 +0300 Subject: [PATCH] Use checksum structure in object package Signed-off-by: Alex Vanin --- v2/object/convert.go | 24 ++++++++++++++++++---- v2/object/grpc/service.go | 14 +++++++++++++ v2/object/grpc/types.go | 4 ++-- v2/object/marshal.go | 39 ++++++++++++++++++++++++++++-------- v2/object/marshal_test.go | 14 +++++++++++-- v2/object/types.go | 42 ++++++++++++++++++++++++++++++++++----- 6 files changed, 116 insertions(+), 21 deletions(-) diff --git a/v2/object/convert.go b/v2/object/convert.go index 69df7989..3f7b85ad 100644 --- a/v2/object/convert.go +++ b/v2/object/convert.go @@ -196,9 +196,13 @@ func HeaderToGRPCMessage(h *Header) *object.Header { m.SetPayloadLength(h.GetPayloadLength()) - m.SetPayloadHash(h.GetPayloadHash()) + m.SetPayloadHash( + refs.ChecksumToGRPCMessage(h.GetPayloadHash()), + ) - m.SetHomomorphicHash(h.GetHomomorphicHash()) + m.SetHomomorphicHash( + refs.ChecksumToGRPCMessage(h.GetHomomorphicHash()), + ) m.SetObjectType( TypeToGRPCField(h.GetObjectType()), @@ -247,9 +251,13 @@ func HeaderFromGRPCMessage(m *object.Header) *Header { h.SetPayloadLength(m.GetPayloadLength()) - h.SetPayloadHash(m.GetPayloadHash()) + h.SetPayloadHash( + refs.ChecksumFromGRPCMessage(m.GetPayloadHash()), + ) - h.SetHomomorphicHash(m.GetHomomorphicHash()) + h.SetHomomorphicHash( + refs.ChecksumFromGRPCMessage(m.GetHomomorphicHash()), + ) h.SetObjectType( TypeFromGRPCField(m.GetObjectType()), @@ -1423,6 +1431,8 @@ func GetRangeHashRequestBodyToGRPCMessage(r *GetRangeHashRequestBody) *object.Ge m.SetRanges(rngMsg) + m.SetType(refsGRPC.ChecksumType(r.GetType())) + return m } @@ -1448,6 +1458,8 @@ func GetRangeHashRequestBodyFromGRPCMessage(m *object.GetRangeHashRequest_Body) r.SetRanges(rngs) + r.SetType(refs.ChecksumType(m.GetType())) + return r } @@ -1490,6 +1502,8 @@ func GetRangeHashResponseBodyToGRPCMessage(r *GetRangeHashResponseBody) *object. m := new(object.GetRangeHashResponse_Body) + m.SetType(refsGRPC.ChecksumType(r.GetType())) + m.SetHashList(r.GetHashList()) return m @@ -1502,6 +1516,8 @@ func GetRangeHashResponseBodyFromGRPCMessage(m *object.GetRangeHashResponse_Body r := new(GetRangeHashResponseBody) + r.SetType(refs.ChecksumType(m.GetType())) + r.SetHashList(m.GetHashList()) return r diff --git a/v2/object/grpc/service.go b/v2/object/grpc/service.go index 3ec97fc1..9dda1cb0 100644 --- a/v2/object/grpc/service.go +++ b/v2/object/grpc/service.go @@ -570,6 +570,13 @@ func (m *GetRangeHashRequest_Body) SetSalt(v []byte) { } } +// Set sets salt for the object payload ranges. +func (m *GetRangeHashRequest_Body) SetType(v refs.ChecksumType) { + if m != nil { + m.Type = v + } +} + // SetBody sets body of the request. func (m *GetRangeHashRequest) SetBody(v *GetRangeHashRequest_Body) { if m != nil { @@ -598,6 +605,13 @@ func (m *GetRangeHashResponse_Body) SetHashList(v [][]byte) { } } +// SetHashList returns list of the range hashes. +func (m *GetRangeHashResponse_Body) SetType(v refs.ChecksumType) { + if m != nil { + m.Type = v + } +} + // SetBody sets body of the response. func (m *GetRangeHashResponse) SetBody(v *GetRangeHashResponse_Body) { if m != nil { diff --git a/v2/object/grpc/types.go b/v2/object/grpc/types.go index b5197f60..36007657 100644 --- a/v2/object/grpc/types.go +++ b/v2/object/grpc/types.go @@ -90,7 +90,7 @@ func (m *Header) SetPayloadLength(v uint64) { } // SetPayloadHash sets hash of the object payload. -func (m *Header) SetPayloadHash(v []byte) { +func (m *Header) SetPayloadHash(v *refs.Checksum) { if m != nil { m.PayloadHash = v } @@ -104,7 +104,7 @@ func (m *Header) SetObjectType(v ObjectType) { } // SetHomomorphicHash sets homomorphic hash of the object payload. -func (m *Header) SetHomomorphicHash(v []byte) { +func (m *Header) SetHomomorphicHash(v *refs.Checksum) { if m != nil { m.HomomorphicHash = v } diff --git a/v2/object/marshal.go b/v2/object/marshal.go index 39490666..79bd131b 100644 --- a/v2/object/marshal.go +++ b/v2/object/marshal.go @@ -87,8 +87,10 @@ const ( getRangeHashReqBodyAddressField = 1 getRangeHashReqBodyRangesField = 2 getRangeHashReqBodySaltField = 3 + getRangeHashReqBodyTypeField = 4 - getRangeHashRespBodyHashList = 1 + getRangeHashRespBodyTypeField = 1 + getRangeHashRespBodyHashListField = 2 ) func (h *ShortHeader) StableMarshal(buf []byte) ([]byte, error) { @@ -315,7 +317,7 @@ func (h *Header) StableMarshal(buf []byte) ([]byte, error) { offset += n - n, err = proto.BytesMarshal(hdrPayloadHashField, buf[offset:], h.payloadHash) + n, err = proto.NestedStructureMarshal(hdrPayloadHashField, buf[offset:], h.payloadHash) if err != nil { return nil, err } @@ -329,7 +331,7 @@ func (h *Header) StableMarshal(buf []byte) ([]byte, error) { offset += n - n, err = proto.BytesMarshal(hdrHomomorphicHashField, buf[offset:], h.homoHash) + n, err = proto.NestedStructureMarshal(hdrHomomorphicHashField, buf[offset:], h.homoHash) if err != nil { return nil, err } @@ -370,9 +372,9 @@ func (h *Header) StableSize() (size int) { size += proto.NestedStructureSize(hdrOwnerIDField, h.ownerID) size += proto.UInt64Size(hdrEpochField, h.creatEpoch) size += proto.UInt64Size(hdrPayloadLengthField, h.payloadLen) - size += proto.BytesSize(hdrPayloadHashField, h.payloadHash) + size += proto.NestedStructureSize(hdrPayloadHashField, h.payloadHash) size += proto.EnumSize(hdrObjectTypeField, int32(h.typ)) - size += proto.BytesSize(hdrHomomorphicHashField, h.homoHash) + size += proto.NestedStructureSize(hdrHomomorphicHashField, h.homoHash) size += proto.NestedStructureSize(hdrSessionTokenField, h.sessionToken) for i := range h.attr { size += proto.NestedStructureSize(hdrAttributesField, h.attr[i]) @@ -1130,7 +1132,14 @@ func (r *GetRangeHashRequestBody) StableMarshal(buf []byte) ([]byte, error) { offset += n } - _, err = proto.BytesMarshal(getRangeHashReqBodySaltField, buf[offset:], r.salt) + n, err = proto.BytesMarshal(getRangeHashReqBodySaltField, buf[offset:], r.salt) + if err != nil { + return nil, err + } + + offset += n + + _, err = proto.EnumMarshal(getRangeHashReqBodyTypeField, buf[offset:], int32(r.typ)) if err != nil { return nil, err } @@ -1150,6 +1159,7 @@ func (r *GetRangeHashRequestBody) StableSize() (size int) { } size += proto.BytesSize(getRangeHashReqBodySaltField, r.salt) + size += proto.EnumSize(getRangeHashReqBodyTypeField, int32(r.typ)) return size } @@ -1163,7 +1173,19 @@ func (r *GetRangeHashResponseBody) StableMarshal(buf []byte) ([]byte, error) { buf = make([]byte, r.StableSize()) } - _, err := proto.RepeatedBytesMarshal(getRangeHashRespBodyHashList, buf, r.hashList) + var ( + offset, n int + err error + ) + + n, err = proto.EnumMarshal(getRangeHashRespBodyTypeField, buf, int32(r.typ)) + if err != nil { + return nil, err + } + + offset += n + + _, err = proto.RepeatedBytesMarshal(getRangeHashRespBodyHashListField, buf[offset:], r.hashList) if err != nil { return nil, err } @@ -1176,7 +1198,8 @@ func (r *GetRangeHashResponseBody) StableSize() (size int) { return 0 } - size += proto.RepeatedBytesSize(getRangeHashRespBodyHashList, r.hashList) + size += proto.EnumSize(getRangeHashRespBodyTypeField, int32(r.typ)) + size += proto.RepeatedBytesSize(getRangeHashRespBodyHashListField, r.hashList) return size } diff --git a/v2/object/marshal_test.go b/v2/object/marshal_test.go index 87dc0c78..fd0eefeb 100644 --- a/v2/object/marshal_test.go +++ b/v2/object/marshal_test.go @@ -461,6 +461,14 @@ func generateSplit(sig string) *object.SplitHeader { return split } +func generateChecksum(data string) *refs.Checksum { + checksum := new(refs.Checksum) + checksum.SetType(refs.TillichZemor) + checksum.SetSum([]byte(data)) + + return checksum +} + func generateHeader(ln uint64) *object.Header { hdr := new(object.Header) hdr.SetPayloadLength(ln) @@ -472,9 +480,9 @@ func generateHeader(ln uint64) *object.Header { generateAttribute("One", "Two"), generateAttribute("Three", "Four"), }) - hdr.SetHomomorphicHash([]byte("Homomorphic Hash")) + hdr.SetHomomorphicHash(generateChecksum("Homomorphic Hash")) hdr.SetObjectType(object.TypeRegular) - hdr.SetPayloadHash([]byte("Payload Hash")) + hdr.SetPayloadHash(generateChecksum("Payload Hash")) hdr.SetSessionToken(generateSessionToken(string(ln))) return hdr @@ -662,6 +670,7 @@ func generateRangeHashRequestBody(cid, oid string, n int) *object.GetRangeHashRe req.SetRanges(rngs) req.SetSalt([]byte("xor salt")) + req.SetType(refs.TillichZemor) return req } @@ -674,6 +683,7 @@ func generateRangeHashResponseBody(n int) *object.GetRangeHashResponseBody { list[i] = []byte("Some homomorphic hash data" + string(n)) } + resp.SetType(refs.TillichZemor) resp.SetHashList(list) return resp diff --git a/v2/object/types.go b/v2/object/types.go index a8c61238..7999ceec 100644 --- a/v2/object/types.go +++ b/v2/object/types.go @@ -46,7 +46,7 @@ type Header struct { payloadLen uint64 - payloadHash, homoHash []byte + payloadHash, homoHash *refs.Checksum typ Type @@ -281,6 +281,8 @@ type GetRangeHashRequestBody struct { rngs []*Range salt []byte + + typ refs.ChecksumType } type GetRangeHashRequest struct { @@ -292,6 +294,8 @@ type GetRangeHashRequest struct { } type GetRangeHashResponseBody struct { + typ refs.ChecksumType + hashList [][]byte } @@ -552,7 +556,7 @@ func (h *Header) SetPayloadLength(v uint64) { } } -func (h *Header) GetPayloadHash() []byte { +func (h *Header) GetPayloadHash() *refs.Checksum { if h != nil { return h.payloadHash } @@ -560,7 +564,7 @@ func (h *Header) GetPayloadHash() []byte { return nil } -func (h *Header) SetPayloadHash(v []byte) { +func (h *Header) SetPayloadHash(v *refs.Checksum) { if h != nil { h.payloadHash = v } @@ -580,7 +584,7 @@ func (h *Header) SetObjectType(v Type) { } } -func (h *Header) GetHomomorphicHash() []byte { +func (h *Header) GetHomomorphicHash() *refs.Checksum { if h != nil { return h.homoHash } @@ -588,7 +592,7 @@ func (h *Header) GetHomomorphicHash() []byte { return nil } -func (h *Header) SetHomomorphicHash(v []byte) { +func (h *Header) SetHomomorphicHash(v *refs.Checksum) { if h != nil { h.homoHash = v } @@ -1712,6 +1716,20 @@ func (r *GetRangeHashRequestBody) SetSalt(v []byte) { } } +func (r *GetRangeHashRequestBody) GetType() refs.ChecksumType { + if r != nil { + return r.typ + } + + return refs.UnknownChecksum +} + +func (r *GetRangeHashRequestBody) SetType(v refs.ChecksumType) { + if r != nil { + r.typ = v + } +} + func (r *GetRangeHashRequest) GetBody() *GetRangeHashRequestBody { if r != nil { return r.body @@ -1754,6 +1772,20 @@ func (r *GetRangeHashRequest) SetVerificationHeader(v *service.RequestVerificati } } +func (r *GetRangeHashResponseBody) GetType() refs.ChecksumType { + if r != nil { + return r.typ + } + + return refs.UnknownChecksum +} + +func (r *GetRangeHashResponseBody) SetType(v refs.ChecksumType) { + if r != nil { + r.typ = v + } +} + func (r *GetRangeHashResponseBody) GetHashList() [][]byte { if r != nil { return r.hashList