Skip to content
Snippets Groups Projects
Unverified Commit 0b551922 authored by Levente Pap's avatar Levente Pap
Browse files

Message Tests

Unit test for the following:
 - MessageID methods
 - NewMessage
 - Bytes
 - FromBytes
 - MessageFromMarshalUtil
 - ForEach...
parent 7835b47b
No related branches found
No related tags found
No related merge requests found
...@@ -3,6 +3,7 @@ package tangle ...@@ -3,6 +3,7 @@ package tangle
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"math/bits"
"sort" "sort"
"sync" "sync"
"time" "time"
...@@ -21,6 +22,7 @@ import ( ...@@ -21,6 +22,7 @@ import (
) )
const ( const (
// MessageVersion defines the version of the message structure.
MessageVersion uint8 = 1 MessageVersion uint8 = 1
// MaxMessageSize defines the maximum size of a message. // MaxMessageSize defines the maximum size of a message.
...@@ -43,6 +45,7 @@ const ( ...@@ -43,6 +45,7 @@ const (
// MessageID identifies a message via its BLAKE2b-256 hash of its bytes. // MessageID identifies a message via its BLAKE2b-256 hash of its bytes.
type MessageID [MessageIDLength]byte type MessageID [MessageIDLength]byte
// Parent is a parent that can be either strong or weak.
type Parent struct { type Parent struct {
ID MessageID ID MessageID
Type uint8 Type uint8
...@@ -53,21 +56,20 @@ var EmptyMessageID = MessageID{} ...@@ -53,21 +56,20 @@ var EmptyMessageID = MessageID{}
// NewMessageID creates a new message id. // NewMessageID creates a new message id.
func NewMessageID(base58EncodedString string) (result MessageID, err error) { func NewMessageID(base58EncodedString string) (result MessageID, err error) {
// TODO: rename to avoid collision with imported package msgIDBytes, err := base58.Decode(base58EncodedString)
bytes, err := base58.Decode(base58EncodedString)
if err != nil { if err != nil {
err = fmt.Errorf("failed to decode base58 encoded string '%s': %w", base58EncodedString, err) err = fmt.Errorf("failed to decode base58 encoded string '%s': %w", base58EncodedString, err)
return return
} }
if len(bytes) != MessageIDLength { if len(msgIDBytes) != MessageIDLength {
err = fmt.Errorf("length of base58 formatted message id is wrong") err = fmt.Errorf("length of base58 formatted message id is wrong")
return return
} }
copy(result[:], bytes) copy(result[:], msgIDBytes)
return return
} }
...@@ -77,6 +79,7 @@ func MessageIDFromBytes(bytes []byte) (result MessageID, consumedBytes int, err ...@@ -77,6 +79,7 @@ func MessageIDFromBytes(bytes []byte) (result MessageID, consumedBytes int, err
// check arguments // check arguments
if len(bytes) < MessageIDLength { if len(bytes) < MessageIDLength {
err = fmt.Errorf("bytes not long enough to encode a valid message id") err = fmt.Errorf("bytes not long enough to encode a valid message id")
return
} }
// calculate result // calculate result
...@@ -105,6 +108,10 @@ func (id *MessageID) MarshalBinary() (result []byte, err error) { ...@@ -105,6 +108,10 @@ func (id *MessageID) MarshalBinary() (result []byte, err error) {
// UnmarshalBinary unmarshals the bytes into an MessageID. // UnmarshalBinary unmarshals the bytes into an MessageID.
func (id *MessageID) UnmarshalBinary(data []byte) (err error) { func (id *MessageID) UnmarshalBinary(data []byte) (err error) {
if len(data) != MessageIDLength {
err = fmt.Errorf("data must be exactly %d long to encode a valid message id", MessageIDLength)
return
}
copy(id[:], data) copy(id[:], data)
return return
...@@ -194,6 +201,9 @@ func sortParents(parents []MessageID) (sorted []MessageID) { ...@@ -194,6 +201,9 @@ func sortParents(parents []MessageID) (sorted []MessageID) {
func MessageFromBytes(bytes []byte) (result *Message, consumedBytes int, err error) { func MessageFromBytes(bytes []byte) (result *Message, consumedBytes int, err error) {
marshalUtil := marshalutil.New(bytes) marshalUtil := marshalutil.New(bytes)
result, err = MessageFromMarshalUtil(marshalUtil) result, err = MessageFromMarshalUtil(marshalUtil)
if err != nil {
return
}
consumedBytes = marshalUtil.ReadOffset() consumedBytes = marshalUtil.ReadOffset()
if len(bytes) != consumedBytes { if len(bytes) != consumedBytes {
...@@ -229,6 +239,10 @@ func MessageFromMarshalUtil(marshalUtil *marshalutil.MarshalUtil) (result *Messa ...@@ -229,6 +239,10 @@ func MessageFromMarshalUtil(marshalUtil *marshalutil.MarshalUtil) (result *Messa
err = xerrors.Errorf("failed to parse parent types from MarshalUtil: %w", err) err = xerrors.Errorf("failed to parse parent types from MarshalUtil: %w", err)
return return
} }
if bits.OnesCount8(parentTypes) < 1 {
err = xerrors.Errorf("invalid parent types, no strong parent specified: %b", parentTypes)
return
}
bitMask := bitmask.BitMask(parentTypes) bitMask := bitmask.BitMask(parentTypes)
var previousParent MessageID var previousParent MessageID
......
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment