-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
R4R: Fix tally result #2861
R4R: Fix tally result #2861
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -239,6 +239,37 @@ func (keeper Keeper) activateVotingPeriod(ctx sdk.Context, proposal Proposal) { | |
keeper.InsertActiveProposalQueue(ctx, proposal.GetVotingEndTime(), proposal.GetProposalID()) | ||
} | ||
|
||
// checks if two proposals are equal | ||
func (keeper Keeper) decreaseProposalTally(ctx sdk.Context, proposal Proposal, option VoteOption) { | ||
tally := proposal.GetTallyResult() | ||
switch { | ||
case option == OptionYes: | ||
tally.Yes = tally.Yes.Sub(sdk.NewDec(1)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use |
||
case option == OptionNo: | ||
tally.No = tally.No.Sub(sdk.NewDec(1)) | ||
case option == OptionNoWithVeto: | ||
tally.NoWithVeto = tally.NoWithVeto.Sub(sdk.NewDec(1)) | ||
case option == OptionAbstain: | ||
tally.Abstain = tally.Abstain.Sub(sdk.NewDec(1)) | ||
} | ||
proposal.SetTallyResult(tally) | ||
} | ||
|
||
func (keeper Keeper) increaseProposalTally(ctx sdk.Context, proposal Proposal, option VoteOption) { | ||
tally := proposal.GetTallyResult() | ||
switch { | ||
case option == OptionYes: | ||
tally.Yes = tally.Yes.Add(sdk.NewDec(1)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto. |
||
case option == OptionNo: | ||
tally.No = tally.No.Add(sdk.NewDec(1)) | ||
case option == OptionNoWithVeto: | ||
tally.NoWithVeto = tally.NoWithVeto.Add(sdk.NewDec(1)) | ||
case option == OptionAbstain: | ||
tally.Abstain = tally.Abstain.Add(sdk.NewDec(1)) | ||
} | ||
proposal.SetTallyResult(tally) | ||
} | ||
|
||
// ===================================================== | ||
// Params | ||
|
||
|
@@ -298,12 +329,35 @@ func (keeper Keeper) AddVote(ctx sdk.Context, proposalID uint64, voterAddr sdk.A | |
return ErrInvalidVote(keeper.codespace, option) | ||
} | ||
|
||
vote := Vote{ | ||
ProposalID: proposalID, | ||
Voter: voterAddr, | ||
Option: option, | ||
oldVote, found := keeper.GetVote(ctx, proposalID, voterAddr) | ||
if found { | ||
// Case 1: address already has a vote | ||
if oldVote.Option != option { | ||
// Case 1.1: new option is different than the previous one | ||
keeper.decreaseProposalTally(ctx, proposal, oldVote.Option) | ||
keeper.SetProposal(ctx, proposal) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we setting the proposal twice? |
||
vote := Vote{ | ||
ProposalID: proposalID, | ||
Voter: voterAddr, | ||
Option: option, | ||
} | ||
keeper.increaseProposalTally(ctx, proposal, option) | ||
keeper.setVote(ctx, proposalID, voterAddr, vote) | ||
keeper.SetProposal(ctx, proposal) | ||
} | ||
// Case 1.2: new option is the same as the one before ==> no updates | ||
} else { | ||
// Case 2: address doesn't have a vote | ||
vote := Vote{ | ||
ProposalID: proposalID, | ||
Voter: voterAddr, | ||
Option: option, | ||
} | ||
|
||
keeper.increaseProposalTally(ctx, proposal, option) | ||
keeper.setVote(ctx, proposalID, voterAddr, vote) | ||
keeper.SetProposal(ctx, proposal) | ||
} | ||
keeper.setVote(ctx, proposalID, voterAddr, vote) | ||
|
||
return nil | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not use
option
here since we already have a variable for it.