From 994bd74c0af09ba975b0a6cf0ea0ff57755221d9 Mon Sep 17 00:00:00 2001 From: Alejanro Ranchal Pedrosa Date: Wed, 2 Nov 2022 14:27:53 +0100 Subject: [PATCH] Add serialization of custom types --- pkg/types/types.go | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/pkg/types/types.go b/pkg/types/types.go index 20939a235..41fe33be5 100644 --- a/pkg/types/types.go +++ b/pkg/types/types.go @@ -192,6 +192,10 @@ func (cid ClientID) Pb() string { return string(cid) } +func (cid ClientID) Bytes() []byte { + return []byte(cid) +} + // ================================================================================ // ClientWM represents the client request watermark. @@ -255,6 +259,11 @@ func (rn ReqNo) Pb() uint64 { return uint64(rn) } +// Bytes converts a ReqNo to a slice of bytes (useful for serialization). +func (rn ReqNo) Bytes() []byte { + return uint64ToBytes(uint64(rn)) +} + // ================================================================================ // RetentionIndex represents an abstract notion of system progress used in garbage collection. @@ -269,6 +278,11 @@ func (ri RetentionIndex) Pb() uint64 { return uint64(ri) } +// Bytes converts a RetentionIndex to a slice of bytes (useful for serialization). +func (ri RetentionIndex) Bytes() []byte { + return uint64ToBytes(uint64(ri)) +} + // ================================================================================ // SBInstanceNr identifies the instance of Sequenced Broadcast (SB) within an epoch. @@ -279,6 +293,10 @@ func (i SBInstanceNr) Pb() uint64 { return uint64(i) } +func (i SBInstanceNr) Bytes() []byte { + return uint64ToBytes(uint64(i)) +} + // ================================================================================ // EpochNr represents the number of an epoch. @@ -290,9 +308,7 @@ func (e EpochNr) Pb() uint64 { } func (e EpochNr) Bytes() []byte { - bytes := make([]byte, 8) - binary.LittleEndian.PutUint64(bytes, uint64(e)) - return bytes + return uint64ToBytes(uint64(e)) } // ================================================================================ @@ -305,6 +321,10 @@ func (nr NumRequests) Pb() uint64 { return uint64(nr) } +func (nr NumRequests) Bytes() []byte { + return uint64ToBytes(uint64(nr)) +} + // ================================================================================ // PBFTViewNr represents the view number in the PBFT protocol (used as a sub-protocol of ISS) @@ -330,6 +350,10 @@ func (td TimeDuration) Pb() uint64 { return uint64(td) } +func (td TimeDuration) Bytes() []byte { + return uint64ToBytes(uint64(td)) +} + // ================================================================================ // Auxiliary functions // ================================================================================