Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
goshimmer
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
iota-imt
goshimmer
Commits
7835b47b
Unverified
Commit
7835b47b
authored
4 years ago
by
Levente Pap
Browse files
Options
Downloads
Patches
Plain Diff
Parent test for message
parent
e477422c
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
packages/tangle/message.go
+3
-3
3 additions, 3 deletions
packages/tangle/message.go
packages/tangle/message_test.go
+231
-0
231 additions, 0 deletions
packages/tangle/message_test.go
with
234 additions
and
3 deletions
packages/tangle/message.go
+
3
−
3
View file @
7835b47b
...
...
@@ -183,8 +183,8 @@ func sortParents(parents []MessageID) (sorted []MessageID) {
}
// sort parents
sort
.
Slice
(
parents
,
func
(
i
,
j
int
)
bool
{
return
bytes
.
Compare
(
parents
[
i
]
.
Bytes
(),
parents
[
j
]
.
Bytes
())
<
0
sort
.
Slice
(
sorted
,
func
(
i
,
j
int
)
bool
{
return
bytes
.
Compare
(
sorted
[
i
]
.
Bytes
(),
sorted
[
j
]
.
Bytes
())
<
0
})
return
...
...
@@ -364,7 +364,7 @@ func (m *Message) StrongParents() []MessageID {
return
m
.
strongParents
}
//
Strong
Parents returns a slice of all
strong
parents of the message.
//
Weak
Parents returns a slice of all
weak
parents of the message.
func
(
m
*
Message
)
WeakParents
()
[]
MessageID
{
return
m
.
weakParents
}
...
...
This diff is collapsed.
Click to expand it.
packages/tangle/message_test.go
+
231
−
0
View file @
7835b47b
package
tangle
import
(
"bytes"
"crypto/rand"
"sort"
"testing"
"time"
...
...
@@ -50,6 +53,234 @@ func TestMessage_MarshalUnmarshal(t *testing.T) {
}
}
func
randomParents
(
count
int
)
[]
MessageID
{
parents
:=
make
([]
MessageID
,
0
,
count
)
for
i
:=
0
;
i
<
count
;
i
++
{
b
:=
make
([]
byte
,
MessageIDLength
)
_
,
_
=
rand
.
Read
(
b
)
randID
,
_
,
_
:=
MessageIDFromBytes
(
b
)
parents
=
append
(
parents
,
randID
)
}
return
parents
}
func
TestMessage_NewMessageTooManyParents
(
t
*
testing
.
T
)
{
// too many strong parents
strongParents
:=
randomParents
(
MaxParentsCount
+
1
)
assert
.
Panics
(
t
,
func
()
{
NewMessage
(
strongParents
,
nil
,
time
.
Now
(),
ed25519
.
PublicKey
{},
0
,
payload
.
NewGenericDataPayload
([]
byte
(
""
)),
0
,
ed25519
.
Signature
{},
)
})
// strong and weak parents are individually okay, but, their sum is bigger
strongParents
=
randomParents
(
MaxParentsCount
-
1
)
weakParents
:=
randomParents
(
2
)
assert
.
Panics
(
t
,
func
()
{
NewMessage
(
strongParents
,
weakParents
,
time
.
Now
(),
ed25519
.
PublicKey
{},
0
,
payload
.
NewGenericDataPayload
([]
byte
(
""
)),
0
,
ed25519
.
Signature
{},
)
})
}
func
TestMessage_NewMessageNotEnoughParents
(
t
*
testing
.
T
)
{
// no parents at all
assert
.
Panics
(
t
,
func
()
{
NewMessage
(
nil
,
nil
,
time
.
Now
(),
ed25519
.
PublicKey
{},
0
,
payload
.
NewGenericDataPayload
([]
byte
(
""
)),
0
,
ed25519
.
Signature
{},
)
})
// only one weak parent
assert
.
Panics
(
t
,
func
()
{
NewMessage
(
nil
,
[]
MessageID
{
EmptyMessageID
},
time
.
Now
(),
ed25519
.
PublicKey
{},
0
,
payload
.
NewGenericDataPayload
([]
byte
(
""
)),
0
,
ed25519
.
Signature
{},
)
})
}
func
TestMessage_NewMessage
(
t
*
testing
.
T
)
{
// minimum number of parents supplied
assert
.
NotPanics
(
t
,
func
()
{
NewMessage
(
[]
MessageID
{
EmptyMessageID
},
nil
,
time
.
Now
(),
ed25519
.
PublicKey
{},
0
,
payload
.
NewGenericDataPayload
([]
byte
(
""
)),
0
,
ed25519
.
Signature
{},
)
})
// max number of parents supplied (only strong)
strongParents
:=
randomParents
(
MaxParentsCount
)
assert
.
NotPanics
(
t
,
func
()
{
NewMessage
(
strongParents
,
nil
,
time
.
Now
(),
ed25519
.
PublicKey
{},
0
,
payload
.
NewGenericDataPayload
([]
byte
(
""
)),
0
,
ed25519
.
Signature
{},
)
})
// max number of parents supplied (one strong)
weakParents
:=
randomParents
(
MaxParentsCount
-
1
)
assert
.
NotPanics
(
t
,
func
()
{
NewMessage
(
[]
MessageID
{
EmptyMessageID
},
weakParents
,
time
.
Now
(),
ed25519
.
PublicKey
{},
0
,
payload
.
NewGenericDataPayload
([]
byte
(
""
)),
0
,
ed25519
.
Signature
{},
)
})
}
func
testAreParentsSorted
(
parents
[]
MessageID
)
bool
{
return
sort
.
SliceIsSorted
(
parents
,
func
(
i
,
j
int
)
bool
{
return
bytes
.
Compare
(
parents
[
i
]
.
Bytes
(),
parents
[
j
]
.
Bytes
())
<
0
})
}
func
testSortParents
(
parents
[]
MessageID
)
{
sort
.
Slice
(
parents
,
func
(
i
,
j
int
)
bool
{
return
bytes
.
Compare
(
parents
[
i
]
.
Bytes
(),
parents
[
j
]
.
Bytes
())
<
0
})
}
func
TestMessage_NewMessageStrongParentsSorted
(
t
*
testing
.
T
)
{
// max number of parents supplied (only strong)
strongParents
:=
randomParents
(
MaxParentsCount
)
if
!
testAreParentsSorted
(
strongParents
)
{
testSortParents
(
strongParents
)
assert
.
True
(
t
,
testAreParentsSorted
(
strongParents
))
}
msg
:=
NewMessage
(
strongParents
,
nil
,
time
.
Now
(),
ed25519
.
PublicKey
{},
0
,
payload
.
NewGenericDataPayload
([]
byte
(
""
)),
0
,
ed25519
.
Signature
{},
)
msgStrongParents
:=
msg
.
StrongParents
()
assert
.
True
(
t
,
testAreParentsSorted
(
msgStrongParents
))
}
func
TestMessage_NewMessageWeakParentsSorted
(
t
*
testing
.
T
)
{
// max number of weak parents supplied (MaxParentsCount-1)
weakParents
:=
randomParents
(
MaxParentsCount
-
1
)
if
!
testAreParentsSorted
(
weakParents
)
{
testSortParents
(
weakParents
)
assert
.
True
(
t
,
testAreParentsSorted
(
weakParents
))
}
msg
:=
NewMessage
(
[]
MessageID
{
EmptyMessageID
},
weakParents
,
time
.
Now
(),
ed25519
.
PublicKey
{},
0
,
payload
.
NewGenericDataPayload
([]
byte
(
""
)),
0
,
ed25519
.
Signature
{},
)
msgWeakParents
:=
msg
.
WeakParents
()
assert
.
True
(
t
,
testAreParentsSorted
(
msgWeakParents
))
}
func
TestMessage_NewMessageDuplicateStrongParents
(
t
*
testing
.
T
)
{
// max number of parents supplied (only strong)
strongParents
:=
randomParents
(
MaxParentsCount
/
2
)
strongParents
=
append
(
strongParents
,
strongParents
...
)
msg
:=
NewMessage
(
strongParents
,
nil
,
time
.
Now
(),
ed25519
.
PublicKey
{},
0
,
payload
.
NewGenericDataPayload
([]
byte
(
""
)),
0
,
ed25519
.
Signature
{},
)
msgStrongParents
:=
msg
.
StrongParents
()
assert
.
True
(
t
,
testAreParentsSorted
(
msgStrongParents
))
assert
.
Equal
(
t
,
MaxParentsCount
/
2
,
len
(
msgStrongParents
))
}
func
TestMessage_NewMessageDuplicateWeakParents
(
t
*
testing
.
T
)
{
weakParents
:=
randomParents
(
3
)
weakParents
=
append
(
weakParents
,
weakParents
...
)
msg
:=
NewMessage
(
[]
MessageID
{
EmptyMessageID
},
weakParents
,
time
.
Now
(),
ed25519
.
PublicKey
{},
0
,
payload
.
NewGenericDataPayload
([]
byte
(
""
)),
0
,
ed25519
.
Signature
{},
)
msgWeakParents
:=
msg
.
WeakParents
()
assert
.
True
(
t
,
testAreParentsSorted
(
msgWeakParents
))
assert
.
Equal
(
t
,
3
,
len
(
msgWeakParents
))
}
// TODO: check if parents are sorted in Bytes()
func
TestMessage_BytesParentsSorted
(
t
*
testing
.
T
)
{
}
// TODO: write unit tests
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment