Skip to content

Commit

Permalink
Fix build errors and tests after struct modification
Browse files Browse the repository at this point in the history
  • Loading branch information
antstalepresh committed Oct 28, 2020
1 parent 09a282d commit c504784
Show file tree
Hide file tree
Showing 21 changed files with 137 additions and 101 deletions.
4 changes: 2 additions & 2 deletions x/gov/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ func TestProposalPassedEndblocker(t *testing.T) {
deposits := initialModuleAccCoins.Add(proposal.TotalDeposit...).Add(proposalCoins...)
require.True(t, moduleAccCoins.IsEqual(deposits))

err = app.GovKeeper.AddVote(ctx, proposal.ProposalId, addrs[0], types.OptionYes)
err = app.GovKeeper.AddVote(ctx, proposal.ProposalId, addrs[0], types.SubVotes{types.NewSubVote(types.OptionYes, 1)})
require.NoError(t, err)

newHeader := ctx.BlockHeader()
Expand Down Expand Up @@ -348,7 +348,7 @@ func TestEndBlockerProposalHandlerFailed(t *testing.T) {

handleAndCheck(t, gov.NewHandler(app.GovKeeper), ctx, newDepositMsg)

err = app.GovKeeper.AddVote(ctx, proposal.ProposalId, addrs[0], types.OptionYes)
err = app.GovKeeper.AddVote(ctx, proposal.ProposalId, addrs[0], types.SubVotes{types.NewSubVote(types.OptionYes, 1)})
require.NoError(t, err)

newHeader := ctx.BlockHeader()
Expand Down
3 changes: 2 additions & 1 deletion x/gov/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,8 @@ func (s *IntegrationTestSuite) TestCmdQueryVote() {

var vote types.Vote
s.Require().NoError(clientCtx.JSONMarshaler.UnmarshalJSON(out.Bytes(), &vote), out.String())
s.Require().Equal(types.OptionYes, vote.Option)
s.Require().True(len(vote.SubVotes) == 1)
s.Require().Equal(types.OptionYes, vote.SubVotes[0].Option)
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion x/gov/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ $ %s tx gov vote 1 yes --from mykey
}

// Build vote message and run basic validation
msg := types.NewMsgVote(from, proposalID, byteVoteOption)
msg := types.NewMsgVote(from, proposalID, types.SubVotes{types.NewSubVote(byteVoteOption, 1)})
err = msg.ValidateBasic()
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion x/gov/client/rest/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ func (s *IntegrationTestSuite) TestGetProposalVoteGRPC() {
} else {
s.Require().NoError(err)
s.Require().NotEmpty(vote.Vote)
s.Require().Equal(types.OptionYes, vote.Vote.Option)
s.Require().True(len(vote.Vote.SubVotes) == 1)
s.Require().Equal(types.OptionYes, vote.Vote.SubVotes[0].Option)
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion x/gov/client/rest/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func newVoteHandlerFn(clientCtx client.Context) http.HandlerFunc {
}

// create the message
msg := types.NewMsgVote(req.Voter, proposalID, []types.SubVote{types.NewSubVote(voteOption, 1)})
msg := types.NewMsgVote(req.Voter, proposalID, types.SubVotes{types.NewSubVote(voteOption, 1)})
if rest.CheckBadRequestError(w, msg.ValidateBasic()) {
return
}
Expand Down
22 changes: 11 additions & 11 deletions x/gov/client/utils/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ func TestGetPaginatedVotes(t *testing.T) {
acc2 := make(sdk.AccAddress, 20)
acc2[0] = 2
acc1Msgs := []sdk.Msg{
types.NewMsgVote(acc1, 0, types.OptionYes),
types.NewMsgVote(acc1, 0, types.OptionYes),
types.NewMsgVote(acc1, 0, types.SubVotes{types.NewSubVote(types.OptionYes, 1)}),
types.NewMsgVote(acc1, 0, types.SubVotes{types.NewSubVote(types.OptionYes, 1)}),
}
acc2Msgs := []sdk.Msg{
types.NewMsgVote(acc2, 0, types.OptionYes),
types.NewMsgVote(acc2, 0, types.OptionYes),
types.NewMsgVote(acc2, 0, types.SubVotes{types.NewSubVote(types.OptionYes, 1)}),
types.NewMsgVote(acc2, 0, types.SubVotes{types.NewSubVote(types.OptionYes, 1)}),
}
for _, tc := range []testCase{
{
Expand All @@ -87,8 +87,8 @@ func TestGetPaginatedVotes(t *testing.T) {
acc2Msgs[:1],
},
votes: []types.Vote{
types.NewVote(0, acc1, types.OptionYes),
types.NewVote(0, acc2, types.OptionYes)},
types.NewVote(0, acc1, types.SubVotes{types.NewSubVote(types.OptionYes, 1)}),
types.NewVote(0, acc2, types.SubVotes{types.NewSubVote(types.OptionYes, 1)})},
},
{
description: "2MsgPerTx1Chunk",
Expand All @@ -99,8 +99,8 @@ func TestGetPaginatedVotes(t *testing.T) {
acc2Msgs,
},
votes: []types.Vote{
types.NewVote(0, acc1, types.OptionYes),
types.NewVote(0, acc1, types.OptionYes)},
types.NewVote(0, acc1, types.SubVotes{types.NewSubVote(types.OptionYes, 1)}),
types.NewVote(0, acc1, types.SubVotes{types.NewSubVote(types.OptionYes, 1)})},
},
{
description: "2MsgPerTx2Chunk",
Expand All @@ -111,8 +111,8 @@ func TestGetPaginatedVotes(t *testing.T) {
acc2Msgs,
},
votes: []types.Vote{
types.NewVote(0, acc2, types.OptionYes),
types.NewVote(0, acc2, types.OptionYes)},
types.NewVote(0, acc2, types.SubVotes{types.NewSubVote(types.OptionYes, 1)}),
types.NewVote(0, acc2, types.SubVotes{types.NewSubVote(types.OptionYes, 1)})},
},
{
description: "IncompleteSearchTx",
Expand All @@ -121,7 +121,7 @@ func TestGetPaginatedVotes(t *testing.T) {
msgs: [][]sdk.Msg{
acc1Msgs[:1],
},
votes: []types.Vote{types.NewVote(0, acc1, types.OptionYes)},
votes: []types.Vote{types.NewVote(0, acc1, types.SubVotes{types.NewSubVote(types.OptionYes, 1)})},
},
{
description: "InvalidPage",
Expand Down
20 changes: 10 additions & 10 deletions x/gov/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func (suite *KeeperTestSuite) TestGRPCQueryProposals() {
func() {
testProposals[1].Status = types.StatusVotingPeriod
app.GovKeeper.SetProposal(ctx, testProposals[1])
suite.Require().NoError(app.GovKeeper.AddVote(ctx, testProposals[1].ProposalId, addrs[0], types.OptionAbstain))
suite.Require().NoError(app.GovKeeper.AddVote(ctx, testProposals[1].ProposalId, addrs[0], types.SubVotes{types.NewSubVote(types.OptionAbstain, 1)}))

req = &types.QueryProposalsRequest{
Voter: addrs[0].String(),
Expand Down Expand Up @@ -291,14 +291,14 @@ func (suite *KeeperTestSuite) TestGRPCQueryVote() {
func() {
proposal.Status = types.StatusVotingPeriod
app.GovKeeper.SetProposal(ctx, proposal)
suite.Require().NoError(app.GovKeeper.AddVote(ctx, proposal.ProposalId, addrs[0], types.OptionAbstain))
suite.Require().NoError(app.GovKeeper.AddVote(ctx, proposal.ProposalId, addrs[0], types.SubVotes{types.NewSubVote(types.OptionAbstain, 1)}))

req = &types.QueryVoteRequest{
ProposalId: proposal.ProposalId,
Voter: addrs[0].String(),
}

expRes = &types.QueryVoteResponse{Vote: types.NewVote(proposal.ProposalId, addrs[0], types.OptionAbstain)}
expRes = &types.QueryVoteResponse{Vote: types.NewVote(proposal.ProposalId, addrs[0], types.SubVotes{types.NewSubVote(types.OptionAbstain, 1)})}
},
true,
},
Expand Down Expand Up @@ -395,15 +395,15 @@ func (suite *KeeperTestSuite) TestGRPCQueryVotes() {
app.GovKeeper.SetProposal(ctx, proposal)

votes = []types.Vote{
{proposal.ProposalId, addrs[0].String(), types.OptionAbstain},
{proposal.ProposalId, addrs[1].String(), types.OptionYes},
{proposal.ProposalId, addrs[0].String(), types.SubVotes{types.NewSubVote(types.OptionAbstain, 1)}},
{proposal.ProposalId, addrs[1].String(), types.SubVotes{types.NewSubVote(types.OptionYes, 1)}},
}
accAddr1, err1 := sdk.AccAddressFromBech32(votes[0].Voter)
accAddr2, err2 := sdk.AccAddressFromBech32(votes[1].Voter)
suite.Require().NoError(err1)
suite.Require().NoError(err2)
suite.Require().NoError(app.GovKeeper.AddVote(ctx, proposal.ProposalId, accAddr1, votes[0].Option))
suite.Require().NoError(app.GovKeeper.AddVote(ctx, proposal.ProposalId, accAddr2, votes[1].Option))
suite.Require().NoError(app.GovKeeper.AddVote(ctx, proposal.ProposalId, accAddr1, votes[0].SubVotes))
suite.Require().NoError(app.GovKeeper.AddVote(ctx, proposal.ProposalId, accAddr2, votes[1].SubVotes))

req = &types.QueryVotesRequest{
ProposalId: proposal.ProposalId,
Expand Down Expand Up @@ -769,9 +769,9 @@ func (suite *KeeperTestSuite) TestGRPCQueryTally() {
proposal.Status = types.StatusVotingPeriod
app.GovKeeper.SetProposal(ctx, proposal)

suite.Require().NoError(app.GovKeeper.AddVote(ctx, proposal.ProposalId, addrs[0], types.OptionYes))
suite.Require().NoError(app.GovKeeper.AddVote(ctx, proposal.ProposalId, addrs[1], types.OptionYes))
suite.Require().NoError(app.GovKeeper.AddVote(ctx, proposal.ProposalId, addrs[2], types.OptionYes))
suite.Require().NoError(app.GovKeeper.AddVote(ctx, proposal.ProposalId, addrs[0], types.SubVotes{types.NewSubVote(types.OptionYes, 1)}))
suite.Require().NoError(app.GovKeeper.AddVote(ctx, proposal.ProposalId, addrs[1], types.SubVotes{types.NewSubVote(types.OptionYes, 1)}))
suite.Require().NoError(app.GovKeeper.AddVote(ctx, proposal.ProposalId, addrs[2], types.SubVotes{types.NewSubVote(types.OptionYes, 1)}))

req = &types.QueryTallyResultRequest{ProposalId: proposal.ProposalId}

Expand Down
2 changes: 1 addition & 1 deletion x/gov/keeper/proposal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func TestGetProposalsFiltered(t *testing.T) {

if i%2 == 0 {
d := types.NewDeposit(proposalID, addr1, nil)
v := types.NewVote(proposalID, addr1, types.OptionYes)
v := types.NewVote(proposalID, addr1, types.SubVotes{types.NewSubVote(types.OptionYes, 1)})
app.GovKeeper.SetDeposit(ctx, d)
app.GovKeeper.SetVote(ctx, v)
}
Expand Down
8 changes: 4 additions & 4 deletions x/gov/keeper/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,13 +251,13 @@ func TestQueries(t *testing.T) {
require.Equal(t, proposal3, proposals[1])

// Addrs[0] votes on proposals #2 & #3
vote1 := types.NewVote(proposal2.ProposalId, TestAddrs[0], types.OptionYes)
vote2 := types.NewVote(proposal3.ProposalId, TestAddrs[0], types.OptionYes)
vote1 := types.NewVote(proposal2.ProposalId, TestAddrs[0], types.SubVotes{types.NewSubVote(types.OptionYes, 1)})
vote2 := types.NewVote(proposal3.ProposalId, TestAddrs[0], types.SubVotes{types.NewSubVote(types.OptionYes, 1)})
app.GovKeeper.SetVote(ctx, vote1)
app.GovKeeper.SetVote(ctx, vote2)

// Addrs[1] votes on proposal #3
vote3 := types.NewVote(proposal3.ProposalId, TestAddrs[1], types.OptionYes)
vote3 := types.NewVote(proposal3.ProposalId, TestAddrs[1], types.SubVotes{types.NewSubVote(types.OptionYes, 1)})
app.GovKeeper.SetVote(ctx, vote3)

// Test query voted by TestAddrs[0]
Expand Down Expand Up @@ -323,7 +323,7 @@ func TestPaginatedVotesQuery(t *testing.T) {
vote := types.Vote{
ProposalId: proposal.ProposalId,
Voter: addr.String(),
Option: types.OptionYes,
SubVotes: types.SubVotes{types.NewSubVote(types.OptionYes, 1)},
}
votes[i] = vote
app.GovKeeper.SetVote(ctx, vote)
Expand Down
32 changes: 25 additions & 7 deletions x/gov/keeper/tally.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (keeper Keeper) Tally(ctx sdk.Context, proposal types.Proposal) (passes boo
validator.GetBondedTokens(),
validator.GetDelegatorShares(),
sdk.ZeroDec(),
types.OptionEmpty,
types.SubVotes{},
)

return false
Expand All @@ -43,7 +43,7 @@ func (keeper Keeper) Tally(ctx sdk.Context, proposal types.Proposal) (passes boo

valAddrStr := sdk.ValAddress(voter.Bytes()).String()
if val, ok := currValidators[valAddrStr]; ok {
val.Vote = vote.Option
val.Vote = vote.SubVotes
currValidators[valAddrStr] = val
}

Expand All @@ -60,8 +60,17 @@ func (keeper Keeper) Tally(ctx sdk.Context, proposal types.Proposal) (passes boo
delegatorShare := delegation.GetShares().Quo(val.DelegatorShares)
votingPower := delegatorShare.MulInt(val.BondedTokens)

results[vote.Option] = results[vote.Option].Add(votingPower)
totalVotingPower = totalVotingPower.Add(votingPower)
totalRates := sdk.NewDec(0)
for _, subvote := range vote.SubVotes {
totalRates = totalRates.Add(subvote.Rate)
}
for _, subvote := range vote.SubVotes {
subPower := sdk.NewDecFromBigInt(votingPower.BigInt().Div(votingPower.Mul(subvote.Rate).BigInt(), totalRates.BigInt()))
results[subvote.Option] = results[subvote.Option].Add(subPower)
totalVotingPower = totalVotingPower.Add(subPower)
}
// TODO how should handle remainder?
// TODO how should handle when totalRates == 0
}

return false
Expand All @@ -73,16 +82,25 @@ func (keeper Keeper) Tally(ctx sdk.Context, proposal types.Proposal) (passes boo

// iterate over the validators again to tally their voting power
for _, val := range currValidators {
if val.Vote == types.OptionEmpty {
if len(val.Vote) == 0 {
continue
}

sharesAfterDeductions := val.DelegatorShares.Sub(val.DelegatorDeductions)
fractionAfterDeductions := sharesAfterDeductions.Quo(val.DelegatorShares)
votingPower := fractionAfterDeductions.MulInt(val.BondedTokens)

results[val.Vote] = results[val.Vote].Add(votingPower)
totalVotingPower = totalVotingPower.Add(votingPower)
totalRates := sdk.NewDec(0)
for _, subvote := range val.Vote {
totalRates = totalRates.Add(subvote.Rate)
}
for _, subvote := range val.Vote {
subPower := sdk.NewDecFromBigInt(votingPower.BigInt().Div(votingPower.Mul(subvote.Rate).BigInt(), totalRates.BigInt()))
results[subvote.Option] = results[subvote.Option].Add(subPower)
totalVotingPower = totalVotingPower.Add(subPower)
// TODO how should handle remainder?
// TODO how should handle when totalRates == 0
}
}

tallyParams := keeper.GetTallyParams(ctx)
Expand Down
Loading

0 comments on commit c504784

Please sign in to comment.