Skip to content

Commit

Permalink
Merge pull request #1976 from dedis/work-be1-etienne-comments-previou…
Browse files Browse the repository at this point in the history
…s-pr

[BE1] Address issues mentioned in the comments for pull request #1815
  • Loading branch information
sgueissa authored Jun 30, 2024
2 parents 144e13c + 02d74fc commit 7049cfb
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 21 deletions.
7 changes: 7 additions & 0 deletions be1-go/internal/database/sqlite/chirp.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,39 @@ func (s *SQLite) StoreChirpMessages(channelPath, generalChannel string, msg, gen
if err != nil {
return errors.NewJsonMarshalError("chirp message: %v", err)
}

messageData, err := base64.URLEncoding.DecodeString(msg.Data)
if err != nil {
return errors.NewDecodeStringError("chirp message data: %v", err)
}

generalMsgBytes, err := json.Marshal(generalMsg)
if err != nil {
return errors.NewInternalServerError("failed to marshal general chirp message: %v", err)
}

generalMessageData, err := base64.URLEncoding.DecodeString(generalMsg.Data)
if err != nil {
return errors.NewInternalServerError("failed to decode general chirp message data: %v", err)
}

storedTime := time.Now().UnixNano()

err = s.insertMessageHelper(tx, msg.MessageID, msgBytes, messageData, storedTime)
if err != nil {
return err
}

_, err = tx.Exec(insertChannelMessage, channelPath, msg.MessageID, true)
if err != nil {
return errors.NewDatabaseInsertErrorMsg("relation chirp message and chirp channel: %v", err)
}

_, err = tx.Exec(insertMessage, generalMsg.MessageID, generalMsgBytes, generalMessageData, storedTime)
if err != nil {
return errors.NewDatabaseInsertErrorMsg("general chirp message: %v", err)
}

_, err = tx.Exec(insertChannelMessage, generalChannel, generalMsg.MessageID, false)
if err != nil {
return errors.NewDatabaseInsertErrorMsg("relation general chirp message and general chirp channel: %v", err)
Expand Down
55 changes: 51 additions & 4 deletions be1-go/internal/database/sqlite/election.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,26 @@ func (s *SQLite) GetLAOOrganizerPubKey(electionPath string) (kyber.Point, error)
if err != nil {
return nil, poperrors.NewDatabaseTransactionBeginErrorMsg(err.Error())
}

defer tx.Rollback()

var organizerPubBuf []byte

err = tx.QueryRow(selectLaoOrganizerKey, electionPath).
Scan(&organizerPubBuf)
if err != nil {
return nil, poperrors.NewDatabaseSelectErrorMsg("lao organizer public key: %v", err)
}

organizerPubKey := crypto.Suite.Point()

err = organizerPubKey.UnmarshalBinary(organizerPubBuf)
if err != nil {
return nil, poperrors.NewInternalServerError("failed to unmarshal lao organizer public key: %v", err)
}

err = tx.Commit()

if err != nil {
return nil, poperrors.NewDatabaseTransactionCommitErrorMsg(err.Error())
}
Expand All @@ -52,23 +56,26 @@ func (s *SQLite) GetElectionSecretKey(electionPath string) (kyber.Scalar, error)
defer dbLock.Unlock()

var electionSecretBuf []byte
err := s.database.QueryRow(selectSecretKey, electionPath).
Scan(&electionSecretBuf)

err := s.database.QueryRow(selectSecretKey, electionPath).Scan(&electionSecretBuf)
if err != nil {
return nil, poperrors.NewDatabaseSelectErrorMsg("election secret key: %v", err)
}

electionSecretKey := crypto.Suite.Scalar()
err = electionSecretKey.UnmarshalBinary(electionSecretBuf)

if err != nil {
return nil, poperrors.NewInternalServerError("failed to unmarshal election secret key: %v", err)
}

return electionSecretKey, nil
}

func (s *SQLite) getElectionState(electionPath string) (string, error) {

var state string

err := s.database.QueryRow(selectLastElectionMessage,
electionPath,
channel.ElectionObject,
Expand All @@ -78,6 +85,7 @@ func (s *SQLite) getElectionState(electionPath string) (string, error) {
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return "", poperrors.NewDatabaseSelectErrorMsg("election state: %v", err)
}

return state, nil
}

Expand All @@ -101,6 +109,7 @@ func (s *SQLite) IsElectionStarted(electionPath string) (bool, error) {
if err != nil {
return false, err
}

return state == channel.ElectionActionOpen, nil
}

Expand All @@ -112,6 +121,7 @@ func (s *SQLite) IsElectionEnded(electionPath string) (bool, error) {
if err != nil {
return false, err
}

return state == channel.ElectionActionEnd, nil
}

Expand All @@ -120,11 +130,13 @@ func (s *SQLite) GetElectionCreationTime(electionPath string) (int64, error) {
defer dbLock.Unlock()

var creationTime int64

err := s.database.QueryRow(selectElectionCreationTime, electionPath, channel.ElectionObject, channel.ElectionActionSetup).
Scan(&creationTime)
if err != nil {
return 0, poperrors.NewDatabaseSelectErrorMsg("election creation time: %v", err)
}

return creationTime, nil
}

Expand All @@ -133,15 +145,16 @@ func (s *SQLite) GetElectionType(electionPath string) (string, error) {
defer dbLock.Unlock()

var electionType string

err := s.database.QueryRow(selectElectionType,
electionPath,
channel.ElectionObject,
channel.ElectionActionSetup).
Scan(&electionType)

if err != nil {
return "", poperrors.NewDatabaseSelectErrorMsg("election type: %v", err)
}

return electionType, nil
}

Expand All @@ -163,32 +176,39 @@ func (s *SQLite) GetElectionAttendees(electionPath string) (map[string]struct{},
}

var rollCallClose mlao.RollCallClose

err = json.Unmarshal(rollCallCloseBytes, &rollCallClose)
if err != nil {
return nil, poperrors.NewJsonUnmarshalError("roll call close message data: %v", err)
}

attendeesMap := make(map[string]struct{})

for _, attendee := range rollCallClose.Attendees {
attendeesMap[attendee] = struct{}{}
}

return attendeesMap, nil
}

func (s *SQLite) getElectionSetup(electionPath string, tx *sql.Tx) (mlao.ElectionSetup, error) {

var electionSetupBytes []byte

err := tx.QueryRow(selectElectionSetup, electionPath, channel.ElectionObject, channel.ElectionActionSetup).
Scan(&electionSetupBytes)

if err != nil {
return mlao.ElectionSetup{}, poperrors.NewDatabaseSelectErrorMsg("election setup message data: %v", err)
}

var electionSetup mlao.ElectionSetup

err = json.Unmarshal(electionSetupBytes, &electionSetup)
if err != nil {
return mlao.ElectionSetup{}, poperrors.NewJsonUnmarshalError("election setup message data: %v", err)
}

return electionSetup, nil
}

Expand All @@ -201,13 +221,15 @@ func (s *SQLite) GetElectionQuestions(electionPath string) (map[string]telection
return nil, poperrors.NewDatabaseTransactionBeginErrorMsg(err.Error())

}

defer tx.Rollback()

electionSetup, err := s.getElectionSetup(electionPath, tx)
if err != nil {
return nil, err

}

questions, err := getQuestionsFromMessage(electionSetup)
if err != nil {
return nil, err
Expand All @@ -218,6 +240,7 @@ func (s *SQLite) GetElectionQuestions(electionPath string) (map[string]telection
return nil, poperrors.NewDatabaseTransactionCommitErrorMsg(err.Error())

}

return questions, nil
}

Expand All @@ -229,6 +252,7 @@ func (s *SQLite) GetElectionQuestionsWithValidVotes(electionPath string) (map[st
if err != nil {
return nil, poperrors.NewDatabaseTransactionBeginErrorMsg(err.Error())
}

defer tx.Rollback()

electionSetup, err := s.getElectionSetup(electionPath, tx)
Expand All @@ -244,6 +268,7 @@ func (s *SQLite) GetElectionQuestionsWithValidVotes(electionPath string) (map[st
if err != nil {
return nil, poperrors.NewDatabaseSelectErrorMsg("cast vote messages: %v", err)
}

defer rows.Close()

for rows.Next() {
Expand All @@ -253,53 +278,67 @@ func (s *SQLite) GetElectionQuestionsWithValidVotes(electionPath string) (map[st
if err = rows.Scan(&voteBytes, &msgID, &sender); err != nil {
return nil, poperrors.NewDatabaseScanErrorMsg("cast vote message: %v", err)
}

var vote melection.VoteCastVote
err = json.Unmarshal(voteBytes, &vote)

if err != nil {
return nil, poperrors.NewJsonUnmarshalError("cast vote message data: %v", err)
}

err = updateVote(msgID, sender, vote, questions)
if err != nil {
return nil, err
}
}

if err = rows.Err(); err != nil {
return nil, poperrors.NewDatabaseIteratorErrorMsg("cast vote messages: %v", err)
}

err = tx.Commit()

if err != nil {
return nil, poperrors.NewDatabaseTransactionCommitErrorMsg(err.Error())
}

return questions, nil
}

func getQuestionsFromMessage(electionSetup mlao.ElectionSetup) (map[string]telection.Question, error) {

questions := make(map[string]telection.Question)

for _, question := range electionSetup.Questions {
ballotOptions := make([]string, len(question.BallotOptions))
copy(ballotOptions, question.BallotOptions)
_, ok := questions[question.ID]

if ok {
return nil, poperrors.NewInvalidMessageFieldError("duplicate question ID in election setup message data: %s", question.ID)
}

questions[question.ID] = telection.Question{
ID: []byte(question.ID),
BallotOptions: ballotOptions,
ValidVotes: make(map[string]telection.ValidVote),
Method: question.VotingMethod,
}
}

return questions, nil
}

func updateVote(msgID, sender string, castVote melection.VoteCastVote, questions map[string]telection.Question) error {
for idx, vote := range castVote.Votes {
question, ok := questions[vote.Question]

if !ok {
return poperrors.NewInvalidMessageFieldError("question not found in election setup for vote number %d sent by %s", idx, sender)
}

earlierVote, ok := question.ValidVotes[sender]

if !ok || earlierVote.VoteTime < castVote.CreatedAt {
question.ValidVotes[sender] = telection.ValidVote{
MsgID: msgID,
Expand All @@ -309,6 +348,7 @@ func updateVote(msgID, sender string, castVote melection.VoteCastVote, questions
}
}
}

return nil
}

Expand All @@ -326,37 +366,44 @@ func (s *SQLite) StoreElectionEndWithResult(channelPath string, msg, electionRes
if err != nil {
return poperrors.NewJsonMarshalError("election end message: %v", err)
}

messageData, err := base64.URLEncoding.DecodeString(msg.Data)
if err != nil {
return poperrors.NewDecodeStringError("election end message data: %v", err)
}

electionResult, err := base64.URLEncoding.DecodeString(electionResultMsg.Data)
if err != nil {
return poperrors.NewInternalServerError("failed to decode election result message data: %v", err)
}

electionResultMsgBytes, err := json.Marshal(electionResultMsg)
if err != nil {
return poperrors.NewJsonMarshalError("failed to marshal election result message: %v", err)
}

storedTime := time.Now().UnixNano()

err = s.insertMessageHelper(tx, msg.MessageID, msgBytes, messageData, storedTime)
if err != nil {
return err

}

_, err = tx.Exec(insertChannelMessage, channelPath, msg.MessageID, true)
if err != nil {
return poperrors.NewDatabaseInsertErrorMsg("relation election end message and election channel: %v", err)
}

_, err = tx.Exec(insertMessage, electionResultMsg.MessageID, electionResultMsgBytes, electionResult, storedTime)
if err != nil {
return poperrors.NewDatabaseInsertErrorMsg("election result message: %v", err)
}

_, err = tx.Exec(insertChannelMessage, channelPath, electionResultMsg.MessageID, false)
if err != nil {
return poperrors.NewDatabaseInsertErrorMsg("relation election result message and election channel: %v", err)
}

err = tx.Commit()

if err != nil {
Expand Down
Loading

0 comments on commit 7049cfb

Please sign in to comment.