Use checksum structure in object package

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2020-08-20 11:48:35 +03:00 committed by Stanislav Bogatyrev
parent 4405492640
commit f1addc4cc5
6 changed files with 116 additions and 21 deletions

View file

@ -196,9 +196,13 @@ func HeaderToGRPCMessage(h *Header) *object.Header {
m.SetPayloadLength(h.GetPayloadLength()) 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( m.SetObjectType(
TypeToGRPCField(h.GetObjectType()), TypeToGRPCField(h.GetObjectType()),
@ -247,9 +251,13 @@ func HeaderFromGRPCMessage(m *object.Header) *Header {
h.SetPayloadLength(m.GetPayloadLength()) 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( h.SetObjectType(
TypeFromGRPCField(m.GetObjectType()), TypeFromGRPCField(m.GetObjectType()),
@ -1423,6 +1431,8 @@ func GetRangeHashRequestBodyToGRPCMessage(r *GetRangeHashRequestBody) *object.Ge
m.SetRanges(rngMsg) m.SetRanges(rngMsg)
m.SetType(refsGRPC.ChecksumType(r.GetType()))
return m return m
} }
@ -1448,6 +1458,8 @@ func GetRangeHashRequestBodyFromGRPCMessage(m *object.GetRangeHashRequest_Body)
r.SetRanges(rngs) r.SetRanges(rngs)
r.SetType(refs.ChecksumType(m.GetType()))
return r return r
} }
@ -1490,6 +1502,8 @@ func GetRangeHashResponseBodyToGRPCMessage(r *GetRangeHashResponseBody) *object.
m := new(object.GetRangeHashResponse_Body) m := new(object.GetRangeHashResponse_Body)
m.SetType(refsGRPC.ChecksumType(r.GetType()))
m.SetHashList(r.GetHashList()) m.SetHashList(r.GetHashList())
return m return m
@ -1502,6 +1516,8 @@ func GetRangeHashResponseBodyFromGRPCMessage(m *object.GetRangeHashResponse_Body
r := new(GetRangeHashResponseBody) r := new(GetRangeHashResponseBody)
r.SetType(refs.ChecksumType(m.GetType()))
r.SetHashList(m.GetHashList()) r.SetHashList(m.GetHashList())
return r return r

View file

@ -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. // SetBody sets body of the request.
func (m *GetRangeHashRequest) SetBody(v *GetRangeHashRequest_Body) { func (m *GetRangeHashRequest) SetBody(v *GetRangeHashRequest_Body) {
if m != nil { 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. // SetBody sets body of the response.
func (m *GetRangeHashResponse) SetBody(v *GetRangeHashResponse_Body) { func (m *GetRangeHashResponse) SetBody(v *GetRangeHashResponse_Body) {
if m != nil { if m != nil {

View file

@ -90,7 +90,7 @@ func (m *Header) SetPayloadLength(v uint64) {
} }
// SetPayloadHash sets hash of the object payload. // SetPayloadHash sets hash of the object payload.
func (m *Header) SetPayloadHash(v []byte) { func (m *Header) SetPayloadHash(v *refs.Checksum) {
if m != nil { if m != nil {
m.PayloadHash = v m.PayloadHash = v
} }
@ -104,7 +104,7 @@ func (m *Header) SetObjectType(v ObjectType) {
} }
// SetHomomorphicHash sets homomorphic hash of the object payload. // SetHomomorphicHash sets homomorphic hash of the object payload.
func (m *Header) SetHomomorphicHash(v []byte) { func (m *Header) SetHomomorphicHash(v *refs.Checksum) {
if m != nil { if m != nil {
m.HomomorphicHash = v m.HomomorphicHash = v
} }

View file

@ -87,8 +87,10 @@ const (
getRangeHashReqBodyAddressField = 1 getRangeHashReqBodyAddressField = 1
getRangeHashReqBodyRangesField = 2 getRangeHashReqBodyRangesField = 2
getRangeHashReqBodySaltField = 3 getRangeHashReqBodySaltField = 3
getRangeHashReqBodyTypeField = 4
getRangeHashRespBodyHashList = 1 getRangeHashRespBodyTypeField = 1
getRangeHashRespBodyHashListField = 2
) )
func (h *ShortHeader) StableMarshal(buf []byte) ([]byte, error) { func (h *ShortHeader) StableMarshal(buf []byte) ([]byte, error) {
@ -315,7 +317,7 @@ func (h *Header) StableMarshal(buf []byte) ([]byte, error) {
offset += n offset += n
n, err = proto.BytesMarshal(hdrPayloadHashField, buf[offset:], h.payloadHash) n, err = proto.NestedStructureMarshal(hdrPayloadHashField, buf[offset:], h.payloadHash)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -329,7 +331,7 @@ func (h *Header) StableMarshal(buf []byte) ([]byte, error) {
offset += n offset += n
n, err = proto.BytesMarshal(hdrHomomorphicHashField, buf[offset:], h.homoHash) n, err = proto.NestedStructureMarshal(hdrHomomorphicHashField, buf[offset:], h.homoHash)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -370,9 +372,9 @@ func (h *Header) StableSize() (size int) {
size += proto.NestedStructureSize(hdrOwnerIDField, h.ownerID) size += proto.NestedStructureSize(hdrOwnerIDField, h.ownerID)
size += proto.UInt64Size(hdrEpochField, h.creatEpoch) size += proto.UInt64Size(hdrEpochField, h.creatEpoch)
size += proto.UInt64Size(hdrPayloadLengthField, h.payloadLen) 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.EnumSize(hdrObjectTypeField, int32(h.typ))
size += proto.BytesSize(hdrHomomorphicHashField, h.homoHash) size += proto.NestedStructureSize(hdrHomomorphicHashField, h.homoHash)
size += proto.NestedStructureSize(hdrSessionTokenField, h.sessionToken) size += proto.NestedStructureSize(hdrSessionTokenField, h.sessionToken)
for i := range h.attr { for i := range h.attr {
size += proto.NestedStructureSize(hdrAttributesField, h.attr[i]) size += proto.NestedStructureSize(hdrAttributesField, h.attr[i])
@ -1130,7 +1132,14 @@ func (r *GetRangeHashRequestBody) StableMarshal(buf []byte) ([]byte, error) {
offset += n 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 { if err != nil {
return nil, err return nil, err
} }
@ -1150,6 +1159,7 @@ func (r *GetRangeHashRequestBody) StableSize() (size int) {
} }
size += proto.BytesSize(getRangeHashReqBodySaltField, r.salt) size += proto.BytesSize(getRangeHashReqBodySaltField, r.salt)
size += proto.EnumSize(getRangeHashReqBodyTypeField, int32(r.typ))
return size return size
} }
@ -1163,7 +1173,19 @@ func (r *GetRangeHashResponseBody) StableMarshal(buf []byte) ([]byte, error) {
buf = make([]byte, r.StableSize()) 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 { if err != nil {
return nil, err return nil, err
} }
@ -1176,7 +1198,8 @@ func (r *GetRangeHashResponseBody) StableSize() (size int) {
return 0 return 0
} }
size += proto.RepeatedBytesSize(getRangeHashRespBodyHashList, r.hashList) size += proto.EnumSize(getRangeHashRespBodyTypeField, int32(r.typ))
size += proto.RepeatedBytesSize(getRangeHashRespBodyHashListField, r.hashList)
return size return size
} }

View file

@ -461,6 +461,14 @@ func generateSplit(sig string) *object.SplitHeader {
return split 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 { func generateHeader(ln uint64) *object.Header {
hdr := new(object.Header) hdr := new(object.Header)
hdr.SetPayloadLength(ln) hdr.SetPayloadLength(ln)
@ -472,9 +480,9 @@ func generateHeader(ln uint64) *object.Header {
generateAttribute("One", "Two"), generateAttribute("One", "Two"),
generateAttribute("Three", "Four"), generateAttribute("Three", "Four"),
}) })
hdr.SetHomomorphicHash([]byte("Homomorphic Hash")) hdr.SetHomomorphicHash(generateChecksum("Homomorphic Hash"))
hdr.SetObjectType(object.TypeRegular) hdr.SetObjectType(object.TypeRegular)
hdr.SetPayloadHash([]byte("Payload Hash")) hdr.SetPayloadHash(generateChecksum("Payload Hash"))
hdr.SetSessionToken(generateSessionToken(string(ln))) hdr.SetSessionToken(generateSessionToken(string(ln)))
return hdr return hdr
@ -662,6 +670,7 @@ func generateRangeHashRequestBody(cid, oid string, n int) *object.GetRangeHashRe
req.SetRanges(rngs) req.SetRanges(rngs)
req.SetSalt([]byte("xor salt")) req.SetSalt([]byte("xor salt"))
req.SetType(refs.TillichZemor)
return req return req
} }
@ -674,6 +683,7 @@ func generateRangeHashResponseBody(n int) *object.GetRangeHashResponseBody {
list[i] = []byte("Some homomorphic hash data" + string(n)) list[i] = []byte("Some homomorphic hash data" + string(n))
} }
resp.SetType(refs.TillichZemor)
resp.SetHashList(list) resp.SetHashList(list)
return resp return resp

View file

@ -46,7 +46,7 @@ type Header struct {
payloadLen uint64 payloadLen uint64
payloadHash, homoHash []byte payloadHash, homoHash *refs.Checksum
typ Type typ Type
@ -281,6 +281,8 @@ type GetRangeHashRequestBody struct {
rngs []*Range rngs []*Range
salt []byte salt []byte
typ refs.ChecksumType
} }
type GetRangeHashRequest struct { type GetRangeHashRequest struct {
@ -292,6 +294,8 @@ type GetRangeHashRequest struct {
} }
type GetRangeHashResponseBody struct { type GetRangeHashResponseBody struct {
typ refs.ChecksumType
hashList [][]byte 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 { if h != nil {
return h.payloadHash return h.payloadHash
} }
@ -560,7 +564,7 @@ func (h *Header) GetPayloadHash() []byte {
return nil return nil
} }
func (h *Header) SetPayloadHash(v []byte) { func (h *Header) SetPayloadHash(v *refs.Checksum) {
if h != nil { if h != nil {
h.payloadHash = v 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 { if h != nil {
return h.homoHash return h.homoHash
} }
@ -588,7 +592,7 @@ func (h *Header) GetHomomorphicHash() []byte {
return nil return nil
} }
func (h *Header) SetHomomorphicHash(v []byte) { func (h *Header) SetHomomorphicHash(v *refs.Checksum) {
if h != nil { if h != nil {
h.homoHash = v 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 { func (r *GetRangeHashRequest) GetBody() *GetRangeHashRequestBody {
if r != nil { if r != nil {
return r.body 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 { func (r *GetRangeHashResponseBody) GetHashList() [][]byte {
if r != nil { if r != nil {
return r.hashList return r.hashList