@@ -24,22 +24,26 @@ func (s *SQLite) GetLAOOrganizerPubKey(electionPath string) (kyber.Point, error)
24
24
if err != nil {
25
25
return nil , poperrors .NewDatabaseTransactionBeginErrorMsg (err .Error ())
26
26
}
27
+
27
28
defer tx .Rollback ()
28
29
29
30
var organizerPubBuf []byte
31
+
30
32
err = tx .QueryRow (selectLaoOrganizerKey , electionPath ).
31
33
Scan (& organizerPubBuf )
32
34
if err != nil {
33
35
return nil , poperrors .NewDatabaseSelectErrorMsg ("lao organizer public key: %v" , err )
34
36
}
35
37
36
38
organizerPubKey := crypto .Suite .Point ()
39
+
37
40
err = organizerPubKey .UnmarshalBinary (organizerPubBuf )
38
41
if err != nil {
39
42
return nil , poperrors .NewInternalServerError ("failed to unmarshal lao organizer public key: %v" , err )
40
43
}
41
44
42
45
err = tx .Commit ()
46
+
43
47
if err != nil {
44
48
return nil , poperrors .NewDatabaseTransactionCommitErrorMsg (err .Error ())
45
49
}
@@ -52,23 +56,26 @@ func (s *SQLite) GetElectionSecretKey(electionPath string) (kyber.Scalar, error)
52
56
defer dbLock .Unlock ()
53
57
54
58
var electionSecretBuf []byte
55
- err := s . database . QueryRow ( selectSecretKey , electionPath ).
56
- Scan (& electionSecretBuf )
59
+
60
+ err := s . database . QueryRow ( selectSecretKey , electionPath ). Scan (& electionSecretBuf )
57
61
if err != nil {
58
62
return nil , poperrors .NewDatabaseSelectErrorMsg ("election secret key: %v" , err )
59
63
}
60
64
61
65
electionSecretKey := crypto .Suite .Scalar ()
62
66
err = electionSecretKey .UnmarshalBinary (electionSecretBuf )
67
+
63
68
if err != nil {
64
69
return nil , poperrors .NewInternalServerError ("failed to unmarshal election secret key: %v" , err )
65
70
}
71
+
66
72
return electionSecretKey , nil
67
73
}
68
74
69
75
func (s * SQLite ) getElectionState (electionPath string ) (string , error ) {
70
76
71
77
var state string
78
+
72
79
err := s .database .QueryRow (selectLastElectionMessage ,
73
80
electionPath ,
74
81
channel .ElectionObject ,
@@ -78,6 +85,7 @@ func (s *SQLite) getElectionState(electionPath string) (string, error) {
78
85
if err != nil && ! errors .Is (err , sql .ErrNoRows ) {
79
86
return "" , poperrors .NewDatabaseSelectErrorMsg ("election state: %v" , err )
80
87
}
88
+
81
89
return state , nil
82
90
}
83
91
@@ -101,6 +109,7 @@ func (s *SQLite) IsElectionStarted(electionPath string) (bool, error) {
101
109
if err != nil {
102
110
return false , err
103
111
}
112
+
104
113
return state == channel .ElectionActionOpen , nil
105
114
}
106
115
@@ -112,6 +121,7 @@ func (s *SQLite) IsElectionEnded(electionPath string) (bool, error) {
112
121
if err != nil {
113
122
return false , err
114
123
}
124
+
115
125
return state == channel .ElectionActionEnd , nil
116
126
}
117
127
@@ -120,11 +130,13 @@ func (s *SQLite) GetElectionCreationTime(electionPath string) (int64, error) {
120
130
defer dbLock .Unlock ()
121
131
122
132
var creationTime int64
133
+
123
134
err := s .database .QueryRow (selectElectionCreationTime , electionPath , channel .ElectionObject , channel .ElectionActionSetup ).
124
135
Scan (& creationTime )
125
136
if err != nil {
126
137
return 0 , poperrors .NewDatabaseSelectErrorMsg ("election creation time: %v" , err )
127
138
}
139
+
128
140
return creationTime , nil
129
141
}
130
142
@@ -133,15 +145,16 @@ func (s *SQLite) GetElectionType(electionPath string) (string, error) {
133
145
defer dbLock .Unlock ()
134
146
135
147
var electionType string
148
+
136
149
err := s .database .QueryRow (selectElectionType ,
137
150
electionPath ,
138
151
channel .ElectionObject ,
139
152
channel .ElectionActionSetup ).
140
153
Scan (& electionType )
141
-
142
154
if err != nil {
143
155
return "" , poperrors .NewDatabaseSelectErrorMsg ("election type: %v" , err )
144
156
}
157
+
145
158
return electionType , nil
146
159
}
147
160
@@ -163,32 +176,39 @@ func (s *SQLite) GetElectionAttendees(electionPath string) (map[string]struct{},
163
176
}
164
177
165
178
var rollCallClose mlao.RollCallClose
179
+
166
180
err = json .Unmarshal (rollCallCloseBytes , & rollCallClose )
167
181
if err != nil {
168
182
return nil , poperrors .NewJsonUnmarshalError ("roll call close message data: %v" , err )
169
183
}
170
184
171
185
attendeesMap := make (map [string ]struct {})
186
+
172
187
for _ , attendee := range rollCallClose .Attendees {
173
188
attendeesMap [attendee ] = struct {}{}
174
189
}
190
+
175
191
return attendeesMap , nil
176
192
}
177
193
178
194
func (s * SQLite ) getElectionSetup (electionPath string , tx * sql.Tx ) (mlao.ElectionSetup , error ) {
179
195
180
196
var electionSetupBytes []byte
197
+
181
198
err := tx .QueryRow (selectElectionSetup , electionPath , channel .ElectionObject , channel .ElectionActionSetup ).
182
199
Scan (& electionSetupBytes )
200
+
183
201
if err != nil {
184
202
return mlao.ElectionSetup {}, poperrors .NewDatabaseSelectErrorMsg ("election setup message data: %v" , err )
185
203
}
186
204
187
205
var electionSetup mlao.ElectionSetup
206
+
188
207
err = json .Unmarshal (electionSetupBytes , & electionSetup )
189
208
if err != nil {
190
209
return mlao.ElectionSetup {}, poperrors .NewJsonUnmarshalError ("election setup message data: %v" , err )
191
210
}
211
+
192
212
return electionSetup , nil
193
213
}
194
214
@@ -201,13 +221,15 @@ func (s *SQLite) GetElectionQuestions(electionPath string) (map[string]telection
201
221
return nil , poperrors .NewDatabaseTransactionBeginErrorMsg (err .Error ())
202
222
203
223
}
224
+
204
225
defer tx .Rollback ()
205
226
206
227
electionSetup , err := s .getElectionSetup (electionPath , tx )
207
228
if err != nil {
208
229
return nil , err
209
230
210
231
}
232
+
211
233
questions , err := getQuestionsFromMessage (electionSetup )
212
234
if err != nil {
213
235
return nil , err
@@ -218,6 +240,7 @@ func (s *SQLite) GetElectionQuestions(electionPath string) (map[string]telection
218
240
return nil , poperrors .NewDatabaseTransactionCommitErrorMsg (err .Error ())
219
241
220
242
}
243
+
221
244
return questions , nil
222
245
}
223
246
@@ -229,6 +252,7 @@ func (s *SQLite) GetElectionQuestionsWithValidVotes(electionPath string) (map[st
229
252
if err != nil {
230
253
return nil , poperrors .NewDatabaseTransactionBeginErrorMsg (err .Error ())
231
254
}
255
+
232
256
defer tx .Rollback ()
233
257
234
258
electionSetup , err := s .getElectionSetup (electionPath , tx )
@@ -244,6 +268,7 @@ func (s *SQLite) GetElectionQuestionsWithValidVotes(electionPath string) (map[st
244
268
if err != nil {
245
269
return nil , poperrors .NewDatabaseSelectErrorMsg ("cast vote messages: %v" , err )
246
270
}
271
+
247
272
defer rows .Close ()
248
273
249
274
for rows .Next () {
@@ -253,53 +278,67 @@ func (s *SQLite) GetElectionQuestionsWithValidVotes(electionPath string) (map[st
253
278
if err = rows .Scan (& voteBytes , & msgID , & sender ); err != nil {
254
279
return nil , poperrors .NewDatabaseScanErrorMsg ("cast vote message: %v" , err )
255
280
}
281
+
256
282
var vote melection.VoteCastVote
257
283
err = json .Unmarshal (voteBytes , & vote )
284
+
258
285
if err != nil {
259
286
return nil , poperrors .NewJsonUnmarshalError ("cast vote message data: %v" , err )
260
287
}
288
+
261
289
err = updateVote (msgID , sender , vote , questions )
262
290
if err != nil {
263
291
return nil , err
264
292
}
265
293
}
294
+
266
295
if err = rows .Err (); err != nil {
267
296
return nil , poperrors .NewDatabaseIteratorErrorMsg ("cast vote messages: %v" , err )
268
297
}
298
+
269
299
err = tx .Commit ()
300
+
270
301
if err != nil {
271
302
return nil , poperrors .NewDatabaseTransactionCommitErrorMsg (err .Error ())
272
303
}
304
+
273
305
return questions , nil
274
306
}
275
307
276
308
func getQuestionsFromMessage (electionSetup mlao.ElectionSetup ) (map [string ]telection.Question , error ) {
277
309
278
310
questions := make (map [string ]telection.Question )
311
+
279
312
for _ , question := range electionSetup .Questions {
280
313
ballotOptions := make ([]string , len (question .BallotOptions ))
281
314
copy (ballotOptions , question .BallotOptions )
282
315
_ , ok := questions [question .ID ]
316
+
283
317
if ok {
284
318
return nil , poperrors .NewInvalidMessageFieldError ("duplicate question ID in election setup message data: %s" , question .ID )
285
319
}
320
+
286
321
questions [question .ID ] = telection.Question {
287
322
ID : []byte (question .ID ),
288
323
BallotOptions : ballotOptions ,
289
324
ValidVotes : make (map [string ]telection.ValidVote ),
290
325
Method : question .VotingMethod ,
291
326
}
292
327
}
328
+
293
329
return questions , nil
294
330
}
295
331
296
332
func updateVote (msgID , sender string , castVote melection.VoteCastVote , questions map [string ]telection.Question ) error {
297
333
for idx , vote := range castVote .Votes {
298
334
question , ok := questions [vote .Question ]
335
+
299
336
if ! ok {
300
337
return poperrors .NewInvalidMessageFieldError ("question not found in election setup for vote number %d sent by %s" , idx , sender )
301
338
}
339
+
302
340
earlierVote , ok := question .ValidVotes [sender ]
341
+
303
342
if ! ok || earlierVote .VoteTime < castVote .CreatedAt {
304
343
question .ValidVotes [sender ] = telection.ValidVote {
305
344
MsgID : msgID ,
@@ -309,6 +348,7 @@ func updateVote(msgID, sender string, castVote melection.VoteCastVote, questions
309
348
}
310
349
}
311
350
}
351
+
312
352
return nil
313
353
}
314
354
@@ -326,37 +366,44 @@ func (s *SQLite) StoreElectionEndWithResult(channelPath string, msg, electionRes
326
366
if err != nil {
327
367
return poperrors .NewJsonMarshalError ("election end message: %v" , err )
328
368
}
369
+
329
370
messageData , err := base64 .URLEncoding .DecodeString (msg .Data )
330
371
if err != nil {
331
372
return poperrors .NewDecodeStringError ("election end message data: %v" , err )
332
373
}
374
+
333
375
electionResult , err := base64 .URLEncoding .DecodeString (electionResultMsg .Data )
334
376
if err != nil {
335
377
return poperrors .NewInternalServerError ("failed to decode election result message data: %v" , err )
336
378
}
379
+
337
380
electionResultMsgBytes , err := json .Marshal (electionResultMsg )
338
381
if err != nil {
339
382
return poperrors .NewJsonMarshalError ("failed to marshal election result message: %v" , err )
340
383
}
384
+
341
385
storedTime := time .Now ().UnixNano ()
342
386
343
387
err = s .insertMessageHelper (tx , msg .MessageID , msgBytes , messageData , storedTime )
344
388
if err != nil {
345
389
return err
346
-
347
390
}
391
+
348
392
_ , err = tx .Exec (insertChannelMessage , channelPath , msg .MessageID , true )
349
393
if err != nil {
350
394
return poperrors .NewDatabaseInsertErrorMsg ("relation election end message and election channel: %v" , err )
351
395
}
396
+
352
397
_ , err = tx .Exec (insertMessage , electionResultMsg .MessageID , electionResultMsgBytes , electionResult , storedTime )
353
398
if err != nil {
354
399
return poperrors .NewDatabaseInsertErrorMsg ("election result message: %v" , err )
355
400
}
401
+
356
402
_ , err = tx .Exec (insertChannelMessage , channelPath , electionResultMsg .MessageID , false )
357
403
if err != nil {
358
404
return poperrors .NewDatabaseInsertErrorMsg ("relation election result message and election channel: %v" , err )
359
405
}
406
+
360
407
err = tx .Commit ()
361
408
362
409
if err != nil {
0 commit comments