From 37cc2d3f2a8452c72031c5b8c4efb3beb2ec4b7b Mon Sep 17 00:00:00 2001 From: Carine Dengler Date: Mon, 24 Jun 2024 13:54:02 +0200 Subject: [PATCH] fix: use length of encoded ID to determine total length of ballot --- contracts/evoting/types/ballots.go | 12 +++++++++--- contracts/evoting/types/ballots_test.go | 10 +++++----- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/contracts/evoting/types/ballots.go b/contracts/evoting/types/ballots.go index e8f8eb14..14058910 100644 --- a/contracts/evoting/types/ballots.go +++ b/contracts/evoting/types/ballots.go @@ -334,7 +334,9 @@ func (s *Subject) MaxEncodedSize() int { //TODO : optimise by computing max size according to number of choices and maxN for _, rank := range s.Ranks { size += len(rank.GetID()) - size += len(rank.ID) + // the ID arrives Base64-encoded, but rank.ID is already decoded + // we need the size of the original Base64-encoded string + size += len(base64.StdEncoding.EncodeToString([]byte(rank.ID))) // ':' separators ('id:id:choice') size += 2 @@ -345,7 +347,9 @@ func (s *Subject) MaxEncodedSize() int { for _, selection := range s.Selects { size += len(selection.GetID()) - size += len(selection.ID) + // the ID arrives Base64-encoded, but selection.ID is already decoded + // we need the size of the original Base64-encoded string + size += len(base64.StdEncoding.EncodeToString([]byte(selection.ID))) // ':' separators ('id:id:choice') size += 2 @@ -356,7 +360,9 @@ func (s *Subject) MaxEncodedSize() int { for _, text := range s.Texts { size += len(text.GetID()) - size += len(text.ID) + // the ID arrives Base64-encoded, but text.ID is already decoded + // we need the size of the original Base64-encoded string + size += len(base64.StdEncoding.EncodeToString([]byte(text.ID))) // ':' separators ('id:id:choice') size += 2 diff --git a/contracts/evoting/types/ballots_test.go b/contracts/evoting/types/ballots_test.go index 9b292f73..f47bddfd 100644 --- a/contracts/evoting/types/ballots_test.go +++ b/contracts/evoting/types/ballots_test.go @@ -314,13 +314,13 @@ func TestSubject_MaxEncodedSize(t *testing.T) { }}, Selects: []Select{{ - ID: encodedQuestionID(1), + ID: decodedQuestionID(1), Title: Title{En: "", Fr: "", De: "", URL: ""}, MaxN: 3, MinN: 0, Choices: make([]Choice, 3), }, { - ID: encodedQuestionID(2), + ID: decodedQuestionID(2), Title: Title{En: "", Fr: "", De: "", URL: ""}, MaxN: 5, MinN: 0, @@ -328,7 +328,7 @@ func TestSubject_MaxEncodedSize(t *testing.T) { }}, Ranks: []Rank{{ - ID: encodedQuestionID(3), + ID: decodedQuestionID(3), Title: Title{En: "", Fr: "", De: "", URL: ""}, MaxN: 4, MinN: 0, @@ -336,7 +336,7 @@ func TestSubject_MaxEncodedSize(t *testing.T) { }}, Texts: []Text{{ - ID: encodedQuestionID(4), + ID: decodedQuestionID(4), Title: Title{En: "", Fr: "", De: "", URL: ""}, MaxN: 2, MinN: 0, @@ -344,7 +344,7 @@ func TestSubject_MaxEncodedSize(t *testing.T) { Regex: "", Choices: make([]Choice, 2), }, { - ID: encodedQuestionID(5), + ID: decodedQuestionID(5), Title: Title{En: "", Fr: "", De: "", URL: ""}, MaxN: 1, MinN: 0,