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

Filter parents before counting

parent 11d12d0e
No related branches found
No related tags found
No related merge requests found
......@@ -167,20 +167,24 @@ type Message struct {
// NewMessage creates a new message with the details provided by the issuer.
func NewMessage(strongParents []MessageID, weakParents []MessageID, issuingTime time.Time, issuerPublicKey ed25519.PublicKey, sequenceNumber uint64, payload payload.Payload, nonce uint64, signature ed25519.Signature) (result *Message) {
// remove duplicates, sort in ASC
sortedStrongParents := sortParents(strongParents)
sortedWeakParents := sortParents(weakParents)
// syntactical validation
parentsCount := len(strongParents) + len(weakParents)
parentsCount := len(sortedStrongParents) + len(sortedWeakParents)
if parentsCount < MinParentsCount || parentsCount > MaxParentsCount {
panic(fmt.Sprintf("amount of parents (%d) not in valid range (%d-%d)", parentsCount, MinParentsCount, MaxParentsCount))
}
if len(strongParents) < MinStrongParentsCount {
if len(sortedStrongParents) < MinStrongParentsCount {
panic(fmt.Sprintf("amount of strong parents (%d) failed to reach MinStrongParentsCount (%d)", len(strongParents), MinStrongParentsCount))
}
return &Message{
version: MessageVersion,
strongParents: sortParents(strongParents),
weakParents: sortParents(weakParents),
strongParents: sortedStrongParents,
weakParents: sortedWeakParents,
issuerPublicKey: issuerPublicKey,
issuingTime: issuingTime,
sequenceNumber: sequenceNumber,
......
......@@ -343,6 +343,24 @@ func TestMessage_NewMessage(t *testing.T) {
})
})
t.Run("CASE: Too many parents, but okay without duplicates", func(t *testing.T) {
strongParents := randomParents(MaxParentsCount)
// MaxParentsCount + 1 parents, but there is one duplicate
strongParents = append(strongParents, strongParents[MaxParentsCount-1])
assert.NotPanics(t, func() {
_ = NewMessage(
strongParents,
nil,
time.Now(),
ed25519.PublicKey{},
0,
payload.NewGenericDataPayload([]byte("")),
0,
ed25519.Signature{},
)
})
})
t.Run("CASE: Strong parents are sorted", func(t *testing.T) {
// max number of parents supplied (only strong)
strongParents := randomParents(MaxParentsCount)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment