From 96892d7bc4a8541050b71d05d17e2d342edf0578 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Tue, 12 Apr 2022 16:08:09 +0300 Subject: [PATCH] [#170] checksum: Drop `Empty` method Signed-off-by: Pavel Karpy --- checksum/checksum.go | 7 ------- object/fmt.go | 16 ++++++++++++---- object/object.go | 20 ++++++++++++-------- object/raw_test.go | 8 ++++++-- storagegroup/storagegroup.go | 9 ++++++--- storagegroup/storagegroup_test.go | 8 ++++++-- 6 files changed, 42 insertions(+), 26 deletions(-) diff --git a/checksum/checksum.go b/checksum/checksum.go index 79324d4..a381c57 100644 --- a/checksum/checksum.go +++ b/checksum/checksum.go @@ -127,13 +127,6 @@ func (c Checksum) String() string { return fmt.Sprintf("%s:%s", c.Type(), hex.EncodeToString(v2.GetSum())) } -// Empty returns true if it is called on -// zero checksum. -func (c Checksum) Empty() bool { - v2 := (refs.Checksum)(c) - return v2.GetSum() == nil -} - // String implements fmt.Stringer. // // String is designed to be human-readable, and its format MAY differ between diff --git a/object/fmt.go b/object/fmt.go index 6be3336..05f3ca9 100644 --- a/object/fmt.go +++ b/object/fmt.go @@ -14,9 +14,11 @@ import ( sigutil "github.com/nspcc-dev/neofs-sdk-go/util/signature" ) -var errCheckSumMismatch = errors.New("payload checksum mismatch") - -var errIncorrectID = errors.New("incorrect object identifier") +var ( + errCheckSumMismatch = errors.New("payload checksum mismatch") + errCheckSumNotSet = errors.New("payload checksum is not set") + errIncorrectID = errors.New("incorrect object identifier") +) // CalculatePayloadChecksum calculates and returns checksum of // object payload bytes. @@ -39,7 +41,13 @@ func CalculateAndSetPayloadChecksum(obj *Object) { // corresponds to its payload. func VerifyPayloadChecksum(obj *Object) error { actual := CalculatePayloadChecksum(obj.Payload()) - if !bytes.Equal(obj.PayloadChecksum().Value(), actual.Value()) { + + cs, set := obj.PayloadChecksum() + if !set { + return errCheckSumNotSet + } + + if !bytes.Equal(cs.Value(), actual.Value()) { return errCheckSumMismatch } diff --git a/object/object.go b/object/object.go index 8bf385e..9c509d4 100644 --- a/object/object.go +++ b/object/object.go @@ -199,20 +199,22 @@ func (o *Object) SetCreationEpoch(v uint64) { }) } -// PayloadChecksum returns checksum of the object payload. +// PayloadChecksum returns checksum of the object payload and +// bool that indicates checksum presence in the object. // -// Zero Object has zero checksum. +// Zero Object does not have payload checksum. // // See also SetPayloadChecksum. -func (o *Object) PayloadChecksum() checksum.Checksum { +func (o *Object) PayloadChecksum() (checksum.Checksum, bool) { var v checksum.Checksum v2 := (*object.Object)(o) if hash := v2.GetHeader().GetPayloadHash(); hash != nil { v.ReadFromV2(*hash) + return v, true } - return v + return v, false } // SetPayloadChecksum sets checksum of the object payload. @@ -228,20 +230,22 @@ func (o *Object) SetPayloadChecksum(v checksum.Checksum) { }) } -// PayloadHomomorphicHash returns homomorphic hash of the object payload. +// PayloadHomomorphicHash returns homomorphic hash of the object +// payload and bool that indicates checksum presence in the object. // -// Zero Object has zero checksum. +// Zero Object does not have payload homomorphic checksum. // // See also SetPayloadHomomorphicHash. -func (o *Object) PayloadHomomorphicHash() checksum.Checksum { +func (o *Object) PayloadHomomorphicHash() (checksum.Checksum, bool) { var v checksum.Checksum v2 := (*object.Object)(o) if hash := v2.GetHeader().GetHomomorphicHash(); hash != nil { v.ReadFromV2(*hash) + return v, true } - return v + return v, false } // SetPayloadHomomorphicHash sets homomorphic hash of the object payload. diff --git a/object/raw_test.go b/object/raw_test.go index f9a65d9..cd7197d 100644 --- a/object/raw_test.go +++ b/object/raw_test.go @@ -126,8 +126,10 @@ func TestObject_SetPayloadChecksum(t *testing.T) { cs.SetSHA256(randSHA256Checksum(t)) obj.SetPayloadChecksum(cs) + cs2, set := obj.PayloadChecksum() - require.Equal(t, cs, obj.PayloadChecksum()) + require.True(t, set) + require.Equal(t, cs, cs2) } func TestObject_SetPayloadHomomorphicHash(t *testing.T) { @@ -137,8 +139,10 @@ func TestObject_SetPayloadHomomorphicHash(t *testing.T) { cs.SetTillichZemor(randTZChecksum(t)) obj.SetPayloadHomomorphicHash(cs) + cs2, set := obj.PayloadHomomorphicHash() - require.Equal(t, cs, obj.PayloadHomomorphicHash()) + require.True(t, set) + require.Equal(t, cs, cs2) } func TestObject_SetAttributes(t *testing.T) { diff --git a/storagegroup/storagegroup.go b/storagegroup/storagegroup.go index 5489585..2db0bd2 100644 --- a/storagegroup/storagegroup.go +++ b/storagegroup/storagegroup.go @@ -41,15 +41,18 @@ func (sg *StorageGroup) SetValidationDataSize(epoch uint64) { } // ValidationDataHash returns homomorphic hash from the -// concatenation of the payloads of the storage group members. +// concatenation of the payloads of the storage group members +// and bool that indicates checksum presence in the storage +// group. // -// Zero StorageGroup has zero checksum. +// Zero StorageGroup does not have validation data checksum. // // See also SetValidationDataHash. -func (sg StorageGroup) ValidationDataHash() (v checksum.Checksum) { +func (sg StorageGroup) ValidationDataHash() (v checksum.Checksum, isSet bool) { v2 := (storagegroup.StorageGroup)(sg) if checksumV2 := v2.GetValidationHash(); checksumV2 != nil { v.ReadFromV2(*checksumV2) + isSet = true } return diff --git a/storagegroup/storagegroup_test.go b/storagegroup/storagegroup_test.go index b54117a..9afe8b8 100644 --- a/storagegroup/storagegroup_test.go +++ b/storagegroup/storagegroup_test.go @@ -21,7 +21,10 @@ func TestStorageGroup(t *testing.T) { cs := checksumtest.Checksum() sg.SetValidationDataHash(cs) - require.Equal(t, cs, sg.ValidationDataHash()) + cs2, set := sg.ValidationDataHash() + + require.True(t, set) + require.Equal(t, cs, cs2) exp := uint64(33) sg.SetExpirationEpoch(exp) @@ -78,7 +81,8 @@ func TestNew(t *testing.T) { // check initial values require.Nil(t, sg.Members()) - require.True(t, sg.ValidationDataHash().Empty()) + _, set := sg.ValidationDataHash() + require.False(t, set) require.Zero(t, sg.ExpirationEpoch()) require.Zero(t, sg.ValidationDataSize())