Skip to content

Commit

Permalink
fix: use length of encoded ID to determine total length of ballot
Browse files Browse the repository at this point in the history
  • Loading branch information
PascalinDe committed Jun 24, 2024
1 parent fb3090e commit c66decd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
12 changes: 9 additions & 3 deletions contracts/evoting/types/ballots.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 decoded
// we need the size of the Base64-encoded string
size += len(base64.StdEncoding.EncodeToString([]byte(rank.ID)))

// ':' separators ('id:id:choice')
size += 2
Expand All @@ -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 decoded
// we need the size of the Base64-encoded string
size += len(base64.StdEncoding.EncodeToString([]byte(selection.ID)))

// ':' separators ('id:id:choice')
size += 2
Expand All @@ -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 decoded
// we need the size of the Base64-encoded string
size += len(base64.StdEncoding.EncodeToString([]byte(text.ID)))

// ':' separators ('id:id:choice')
size += 2
Expand Down
10 changes: 5 additions & 5 deletions contracts/evoting/types/ballots_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,37 +314,37 @@ 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,
Choices: make([]Choice, 5),
}},

Ranks: []Rank{{
ID: encodedQuestionID(3),
ID: decodedQuestionID(3),
Title: Title{En: "", Fr: "", De: "", URL: ""},
MaxN: 4,
MinN: 0,
Choices: make([]Choice, 4),
}},

Texts: []Text{{
ID: encodedQuestionID(4),
ID: decodedQuestionID(4),
Title: Title{En: "", Fr: "", De: "", URL: ""},
MaxN: 2,
MinN: 0,
MaxLength: 10,
Regex: "",
Choices: make([]Choice, 2),
}, {
ID: encodedQuestionID(5),
ID: decodedQuestionID(5),
Title: Title{En: "", Fr: "", De: "", URL: ""},
MaxN: 1,
MinN: 0,
Expand Down

0 comments on commit c66decd

Please sign in to comment.