From 4405492640dcd1be61293fdf0e015f6dd7e0f552 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Thu, 20 Aug 2020 11:04:16 +0300 Subject: [PATCH] Use checksum structure in storagegroup package Signed-off-by: Alex Vanin --- v2/storagegroup/convert.go | 8 ++++++-- v2/storagegroup/grpc/types.go | 2 +- v2/storagegroup/marshal.go | 4 ++-- v2/storagegroup/marshal_test.go | 10 +++++++++- v2/storagegroup/types.go | 6 +++--- 5 files changed, 21 insertions(+), 9 deletions(-) diff --git a/v2/storagegroup/convert.go b/v2/storagegroup/convert.go index df545f9..6712619 100644 --- a/v2/storagegroup/convert.go +++ b/v2/storagegroup/convert.go @@ -15,7 +15,9 @@ func StorageGroupToGRPCMessage(s *StorageGroup) *sg.StorageGroup { m := new(sg.StorageGroup) m.SetValidationDataSize(s.GetValidationDataSize()) - m.SetValidationHash(s.GetValidationHash()) + m.SetValidationHash( + refs.ChecksumToGRPCMessage(s.GetValidationHash()), + ) m.SetExpirationEpoch(s.GetExpirationEpoch()) members := s.GetMembers() @@ -39,7 +41,9 @@ func StorageGroupFromGRPCMessage(m *sg.StorageGroup) *StorageGroup { s := new(StorageGroup) s.SetValidationDataSize(m.GetValidationDataSize()) - s.SetValidationHash(m.GetValidationHash()) + s.SetValidationHash( + refs.ChecksumFromGRPCMessage(m.GetValidationHash()), + ) s.SetExpirationEpoch(m.GetExpirationEpoch()) memberMsg := m.GetMembers() diff --git a/v2/storagegroup/grpc/types.go b/v2/storagegroup/grpc/types.go index b1159c4..5bba1b1 100644 --- a/v2/storagegroup/grpc/types.go +++ b/v2/storagegroup/grpc/types.go @@ -12,7 +12,7 @@ func (m *StorageGroup) SetValidationDataSize(v uint64) { } // SetValidationHash sets total homomorphic hash of the storage group payloads. -func (m *StorageGroup) SetValidationHash(v []byte) { +func (m *StorageGroup) SetValidationHash(v *refs.Checksum) { if m != nil { m.ValidationHash = v } diff --git a/v2/storagegroup/marshal.go b/v2/storagegroup/marshal.go index cc1160f..33b0c15 100644 --- a/v2/storagegroup/marshal.go +++ b/v2/storagegroup/marshal.go @@ -34,7 +34,7 @@ func (s *StorageGroup) StableMarshal(buf []byte) ([]byte, error) { offset += n - n, err = proto.BytesMarshal(hashField, buf[offset:], s.hash) + n, err = proto.NestedStructureMarshal(hashField, buf[offset:], s.hash) if err != nil { return nil, err } @@ -67,7 +67,7 @@ func (s *StorageGroup) StableSize() (size int) { } size += proto.UInt64Size(sizeField, s.size) - size += proto.BytesSize(hashField, s.hash) + size += proto.NestedStructureSize(hashField, s.hash) size += proto.UInt64Size(expirationField, s.exp) for i := range s.members { diff --git a/v2/storagegroup/marshal_test.go b/v2/storagegroup/marshal_test.go index b79ed80..0b6a191 100644 --- a/v2/storagegroup/marshal_test.go +++ b/v2/storagegroup/marshal_test.go @@ -21,7 +21,7 @@ func TestStorageGroup_StableMarshal(t *testing.T) { t.Run("non empty", func(t *testing.T) { storageGroupFrom.SetValidationDataSize(300) - storageGroupFrom.SetValidationHash([]byte("Homomorphic hash value")) + storageGroupFrom.SetValidationHash(generateChecksum("Homomorphic hash")) storageGroupFrom.SetExpirationEpoch(100) storageGroupFrom.SetMembers([]*refs.ObjectID{ownerID1, ownerID2}) @@ -35,3 +35,11 @@ func TestStorageGroup_StableMarshal(t *testing.T) { require.Equal(t, storageGroupFrom, storageGroupTo) }) } + +func generateChecksum(data string) *refs.Checksum { + checksum := new(refs.Checksum) + checksum.SetType(refs.TillichZemor) + checksum.SetSum([]byte(data)) + + return checksum +} diff --git a/v2/storagegroup/types.go b/v2/storagegroup/types.go index 720bbe1..6c493ed 100644 --- a/v2/storagegroup/types.go +++ b/v2/storagegroup/types.go @@ -9,7 +9,7 @@ import ( type StorageGroup struct { size uint64 - hash []byte + hash *refs.Checksum exp uint64 @@ -33,7 +33,7 @@ func (s *StorageGroup) SetValidationDataSize(v uint64) { } // GetValidationHash of unified storage group structure. -func (s *StorageGroup) GetValidationHash() []byte { +func (s *StorageGroup) GetValidationHash() *refs.Checksum { if s != nil { return s.hash } @@ -42,7 +42,7 @@ func (s *StorageGroup) GetValidationHash() []byte { } // SetValidationHash into unified storage group structure. -func (s *StorageGroup) SetValidationHash(v []byte) { +func (s *StorageGroup) SetValidationHash(v *refs.Checksum) { if s != nil { s.hash = v }