[#170] checksum: Do not use pointers

Do not return pointers from getters. Do not pass pointers to the methods
that does not modify the checksum. Add `Empty` method.

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2021-03-04 14:38:23 +03:00 committed by LeL
parent fd13e61266
commit caa055236b
7 changed files with 53 additions and 35 deletions

View file

@ -8,7 +8,7 @@ import (
)
// Checksum returns random checksum.Checksum.
func Checksum() *checksum.Checksum {
func Checksum() checksum.Checksum {
var cs [sha256.Size]byte
rand.Read(cs[:])
@ -17,5 +17,5 @@ func Checksum() *checksum.Checksum {
x.SetSHA256(cs)
return &x
return x
}

View file

@ -137,7 +137,7 @@ func (r *Record) AddObjectPayloadLengthFilter(m Match, size uint64) {
}
// AddObjectPayloadHashFilter adds filter by object payload hash value.
func (r *Record) AddObjectPayloadHashFilter(m Match, h *checksum.Checksum) {
func (r *Record) AddObjectPayloadHashFilter(m Match, h checksum.Checksum) {
r.addObjectReservedFilter(m, fKeyObjPayloadHash, h)
}
@ -147,7 +147,7 @@ func (r *Record) AddObjectTypeFilter(m Match, t object.Type) {
}
// AddObjectHomomorphicHashFilter adds filter by object payload homomorphic hash value.
func (r *Record) AddObjectHomomorphicHashFilter(m Match, h *checksum.Checksum) {
func (r *Record) AddObjectHomomorphicHashFilter(m Match, h checksum.Checksum) {
r.addObjectReservedFilter(m, fKeyObjHomomorphicHash, h)
}

View file

@ -20,11 +20,11 @@ var errIncorrectID = errors.New("incorrect object identifier")
// CalculatePayloadChecksum calculates and returns checksum of
// object payload bytes.
func CalculatePayloadChecksum(payload []byte) *checksum.Checksum {
func CalculatePayloadChecksum(payload []byte) checksum.Checksum {
var res checksum.Checksum
checksum.Calculate(&res, checksum.SHA256, payload)
return &res
return res
}
// CalculateAndSetPayloadChecksum calculates checksum of current

View file

@ -200,19 +200,26 @@ func (o *Object) SetCreationEpoch(v uint64) {
}
// PayloadChecksum returns checksum of the object payload.
func (o *Object) PayloadChecksum() *checksum.Checksum {
//
// Zero Object has zero checksum.
//
// See also SetPayloadChecksum.
func (o *Object) PayloadChecksum() checksum.Checksum {
var v checksum.Checksum
v.ReadFromV2(
*(*object.Object)(o).
GetHeader().
GetPayloadHash(),
)
v2 := (*object.Object)(o)
return &v
if hash := v2.GetHeader().GetPayloadHash(); hash != nil {
v.ReadFromV2(*hash)
}
return v
}
// SetPayloadChecksum sets checksum of the object payload.
func (o *Object) SetPayloadChecksum(v *checksum.Checksum) {
// Checksum must not be nil.
//
// See also PayloadChecksum.
func (o *Object) SetPayloadChecksum(v checksum.Checksum) {
var v2 refs.Checksum
v.WriteToV2(&v2)
@ -222,19 +229,26 @@ func (o *Object) SetPayloadChecksum(v *checksum.Checksum) {
}
// PayloadHomomorphicHash returns homomorphic hash of the object payload.
func (o *Object) PayloadHomomorphicHash() *checksum.Checksum {
//
// Zero Object has zero checksum.
//
// See also SetPayloadHomomorphicHash.
func (o *Object) PayloadHomomorphicHash() checksum.Checksum {
var v checksum.Checksum
v.ReadFromV2(
*(*object.Object)(o).
GetHeader().
GetHomomorphicHash(),
)
v2 := (*object.Object)(o)
return &v
if hash := v2.GetHeader().GetHomomorphicHash(); hash != nil {
v.ReadFromV2(*hash)
}
return v
}
// SetPayloadHomomorphicHash sets homomorphic hash of the object payload.
func (o *Object) SetPayloadHomomorphicHash(v *checksum.Checksum) {
// Checksum must not be nil.
//
// See also PayloadHomomorphicHash.
func (o *Object) SetPayloadHomomorphicHash(v checksum.Checksum) {
var v2 refs.Checksum
v.WriteToV2(&v2)

View file

@ -125,9 +125,9 @@ func TestObject_SetPayloadChecksum(t *testing.T) {
var cs checksum.Checksum
cs.SetSHA256(randSHA256Checksum(t))
obj.SetPayloadChecksum(&cs)
obj.SetPayloadChecksum(cs)
require.Equal(t, &cs, obj.PayloadChecksum())
require.Equal(t, cs, obj.PayloadChecksum())
}
func TestObject_SetPayloadHomomorphicHash(t *testing.T) {
@ -136,9 +136,9 @@ func TestObject_SetPayloadHomomorphicHash(t *testing.T) {
var cs checksum.Checksum
cs.SetTillichZemor(randTZChecksum(t))
obj.SetPayloadHomomorphicHash(&cs)
obj.SetPayloadHomomorphicHash(cs)
require.Equal(t, &cs, obj.PayloadHomomorphicHash())
require.Equal(t, cs, obj.PayloadHomomorphicHash())
}
func TestObject_SetAttributes(t *testing.T) {

View file

@ -42,20 +42,24 @@ func (sg *StorageGroup) SetValidationDataSize(epoch uint64) {
// ValidationDataHash returns homomorphic hash from the
// concatenation of the payloads of the storage group members.
func (sg *StorageGroup) ValidationDataHash() *checksum.Checksum {
if v2 := (*storagegroup.StorageGroup)(sg).GetValidationHash(); v2 != nil {
var v checksum.Checksum
v.ReadFromV2(*v2)
return &v
//
// Zero StorageGroup has zero checksum.
//
// See also SetValidationDataHash.
func (sg StorageGroup) ValidationDataHash() (v checksum.Checksum) {
v2 := (storagegroup.StorageGroup)(sg)
if checksumV2 := v2.GetValidationHash(); checksumV2 != nil {
v.ReadFromV2(*checksumV2)
}
return nil
return
}
// SetValidationDataHash sets homomorphic hash from the
// concatenation of the payloads of the storage group members.
func (sg *StorageGroup) SetValidationDataHash(hash *checksum.Checksum) {
//
// See also ValidationDataHash.
func (sg *StorageGroup) SetValidationDataHash(hash checksum.Checksum) {
var v2 refs.Checksum
hash.WriteToV2(&v2)

View file

@ -78,7 +78,7 @@ func TestNew(t *testing.T) {
// check initial values
require.Nil(t, sg.Members())
require.Nil(t, sg.ValidationDataHash())
require.True(t, sg.ValidationDataHash().Empty())
require.Zero(t, sg.ExpirationEpoch())
require.Zero(t, sg.ValidationDataSize())