[#170] checksum: Drop Empty method

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2022-04-12 16:08:09 +03:00 committed by LeL
parent 168b3ee7a4
commit 96892d7bc4
6 changed files with 42 additions and 26 deletions

View file

@ -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

View file

@ -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
}

View file

@ -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.

View file

@ -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) {

View file

@ -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

View file

@ -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())