Skip to content

Commit

Permalink
feat: Add past vote invalidation
Browse files Browse the repository at this point in the history
  • Loading branch information
clockworkgr committed Mar 22, 2024
1 parent 2268d2e commit f02167d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
12 changes: 10 additions & 2 deletions database/gov.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,12 +284,20 @@ WHERE proposal_deposit.height <= excluded.height`
}

// --------------------------------------------------------------------------------------------------------------------
func (db *Db) CleanVote(proposal_id uint64, voter string) error {
flagQuery := `UPDATE proposal_vote SET is_valid=FALSE WHERE proposal_id=$1 AND voter_address=$2`
_, err := db.SQL.Exec(flagQuery, proposal_id, voter)
if err != nil {
return fmt.Errorf("error while updating past vote: %s", err)
}
return nil
}

// SaveVote allows to save for the given height and the message vote
func (db *Db) SaveVote(vote types.Vote) error {
query := `
INSERT INTO proposal_vote (proposal_id, voter_address, option, weight, timestamp, height)
VALUES ($1, $2, $3, $4, $5, $6)`
INSERT INTO proposal_vote (proposal_id, voter_address, is_valid, option, weight, timestamp, height)
VALUES ($1, $2, TRUE, $3, $4, $5, $6)`

// Store the voter account
err := db.SaveAccounts([]types.Account{types.NewAccount(vote.Voter)})
Expand Down
1 change: 1 addition & 0 deletions database/schema/08-gov.sql
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ CREATE TABLE proposal_vote
(
proposal_id INTEGER NOT NULL REFERENCES proposal (id),
voter_address TEXT NOT NULL REFERENCES account (address),
is_valid BOOLEAN NOT NULL,
option TEXT NOT NULL,
weight TEXT NOT NULL,
timestamp TIMESTAMP,
Expand Down
10 changes: 8 additions & 2 deletions modules/gov/handle_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ func (m *Module) handleMsgVote(tx *juno.Tx, msg *govtypes.MsgVote) error {
}

vote := types.NewVote(msg.ProposalId, msg.Voter, msg.Option, "1.0", txTimestamp, tx.Height)

err = m.db.CleanVote(msg.ProposalId, msg.Voter)
if err != nil {
return err
}
return m.db.SaveVote(vote)
}

Expand All @@ -125,7 +128,10 @@ func (m *Module) handleMsgVoteWeighted(tx *juno.Tx, msg *govtypes.MsgVoteWeighte
if err != nil {
return fmt.Errorf("error while parsing time: %s", err)
}

err = m.db.CleanVote(msg.ProposalId, msg.Voter)
if err != nil {
return err
}
for _, option := range msg.Options {
vote := types.NewVote(msg.ProposalId, msg.Voter, option.Option, option.Weight.String(), txTimestamp, tx.Height)
err = m.db.SaveVote(vote)
Expand Down

0 comments on commit f02167d

Please sign in to comment.