diff --git a/database/gov.go b/database/gov.go index 9159576db..b8ee30349 100644 --- a/database/gov.go +++ b/database/gov.go @@ -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)}) diff --git a/database/schema/08-gov.sql b/database/schema/08-gov.sql index f2a98ca1f..819192f51 100644 --- a/database/schema/08-gov.sql +++ b/database/schema/08-gov.sql @@ -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, diff --git a/modules/gov/handle_msg.go b/modules/gov/handle_msg.go index 45fca0ba3..24d82c9ba 100644 --- a/modules/gov/handle_msg.go +++ b/modules/gov/handle_msg.go @@ -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) } @@ -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)