From 31df0807c9de53ec4c6c86032341754d71915a1c Mon Sep 17 00:00:00 2001 From: Carine Dengler Date: Thu, 20 Jun 2024 16:55:12 +0200 Subject: [PATCH] fix: count newline character at the end of the lines --- contracts/evoting/types/ballots.go | 36 +++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/contracts/evoting/types/ballots.go b/contracts/evoting/types/ballots.go index 2d7d1093..dcbe6628 100644 --- a/contracts/evoting/types/ballots.go +++ b/contracts/evoting/types/ballots.go @@ -333,30 +333,50 @@ 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.GetID()) size += len(rank.ID) + + // ':' separators ('id:id:choice') + size += 2 + // at most 3 bytes (128) + ',' per choice - size += len(rank.Choices) * 4 + size += (len(rank.Choices) * 4) + + // line ends w/ '\n' + size += 1 } for _, selection := range s.Selects { - size += len(selection.GetID() + "::") + size += len(selection.GetID()) size += len(selection.ID) + + // ':' separators ('id:id:choice') + size += 2 + // 1 bytes (0/1) + ',' per choice - size += len(selection.Choices) * 2 + size += (len(selection.Choices) * 2) + + // line ends w/ '\n' + size += 1 } for _, text := range s.Texts { - size += len(text.GetID() + "::") + size += len(text.GetID()) size += len(text.ID) + // ':' separators ('id:id:choice') + size += 2 + // at most 4 bytes per character + ',' per answer maxTextPerAnswer := 4*int(text.MaxLength) + 1 - size += maxTextPerAnswer*int(text.MaxN) + - int(math.Max(float64(len(text.Choices)-int(text.MaxN)), 0)) + size += (maxTextPerAnswer*int(text.MaxN) + + int(math.Max(float64(len(text.Choices)-int(text.MaxN)), 0))) + + // line ends w/ '\n' + size += 1 } - // Last line has 2 '\n' + // additional '\n' on last line if size != 0 { size++ }