Skip to content

Commit

Permalink
Add serialization of custom types
Browse files Browse the repository at this point in the history
  • Loading branch information
ranchalp authored and matejpavlovic committed Nov 3, 2022
1 parent 7f8f9a4 commit 994bd74
Showing 1 changed file with 27 additions and 3 deletions.
30 changes: 27 additions & 3 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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))
}

// ================================================================================
Expand All @@ -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)
Expand All @@ -330,6 +350,10 @@ func (td TimeDuration) Pb() uint64 {
return uint64(td)
}

func (td TimeDuration) Bytes() []byte {
return uint64ToBytes(uint64(td))
}

// ================================================================================
// Auxiliary functions
// ================================================================================
Expand Down

0 comments on commit 994bd74

Please sign in to comment.