From caa055236bc96d44c0efdf19ccbf3e4d41d985cb Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Thu, 4 Mar 2021 14:38:23 +0300 Subject: [PATCH] [#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 --- checksum/test/generate.go | 4 +-- eacl/record.go | 4 +-- object/fmt.go | 4 +-- object/object.go | 46 ++++++++++++++++++++----------- object/raw_test.go | 8 +++--- storagegroup/storagegroup.go | 20 ++++++++------ storagegroup/storagegroup_test.go | 2 +- 7 files changed, 53 insertions(+), 35 deletions(-) diff --git a/checksum/test/generate.go b/checksum/test/generate.go index c40bd7a4..72eeadbf 100644 --- a/checksum/test/generate.go +++ b/checksum/test/generate.go @@ -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 } diff --git a/eacl/record.go b/eacl/record.go index 006198a2..430ed22f 100644 --- a/eacl/record.go +++ b/eacl/record.go @@ -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) } diff --git a/object/fmt.go b/object/fmt.go index 58224848..6be3336b 100644 --- a/object/fmt.go +++ b/object/fmt.go @@ -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 diff --git a/object/object.go b/object/object.go index b47c87cc..8bf385e6 100644 --- a/object/object.go +++ b/object/object.go @@ -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) diff --git a/object/raw_test.go b/object/raw_test.go index 8a23aa3d..f9a65d95 100644 --- a/object/raw_test.go +++ b/object/raw_test.go @@ -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) { diff --git a/storagegroup/storagegroup.go b/storagegroup/storagegroup.go index ac626b1b..54895859 100644 --- a/storagegroup/storagegroup.go +++ b/storagegroup/storagegroup.go @@ -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) diff --git a/storagegroup/storagegroup_test.go b/storagegroup/storagegroup_test.go index 07bafa7a..b54117a3 100644 --- a/storagegroup/storagegroup_test.go +++ b/storagegroup/storagegroup_test.go @@ -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())