Skip to content
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

Fixed governance bug with iterate delegations #1801

Merged
merged 3 commits into from
Jul 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ IMPROVEMENTS

BUG FIXES
* \#1666 Add intra-tx counter to the genesis validators
* \#1787 Fixed bug where Tally fails due to revoked/unbonding validator
16 changes: 8 additions & 8 deletions x/gov/tally.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ func tally(ctx sdk.Context, keeper Keeper, proposal Proposal) (passes bool, nonV
} else {

keeper.ds.IterateDelegations(ctx, vote.Voter, func(index int64, delegation sdk.Delegation) (stop bool) {
val := currValidators[delegation.GetValidator().String()]
val.Minus = val.Minus.Add(delegation.GetBondShares())
currValidators[delegation.GetValidator().String()] = val
if val, ok := currValidators[delegation.GetValidator().String()]; ok {
val.Minus = val.Minus.Add(delegation.GetBondShares())
currValidators[delegation.GetValidator().String()] = val

delegatorShare := delegation.GetBondShares().Quo(val.DelegatorShares)
votingPower := val.Power.Mul(delegatorShare)

results[vote.Option] = results[vote.Option].Add(votingPower)
totalVotingPower = totalVotingPower.Add(votingPower)
delegatorShare := delegation.GetBondShares().Quo(val.DelegatorShares)
votingPower := val.Power.Mul(delegatorShare)

results[vote.Option] = results[vote.Option].Add(votingPower)
totalVotingPower = totalVotingPower.Add(votingPower)
}
return false
})
}
Expand Down
40 changes: 40 additions & 0 deletions x/gov/tally_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,3 +389,43 @@ func TestTallyDelgatorMultipleInherit(t *testing.T) {

require.False(t, passes)
}

func TestTallyRevokedValidator(t *testing.T) {
mapp, keeper, sk, addrs, _, _ := getMockApp(t, 10)
mapp.BeginBlock(abci.RequestBeginBlock{})
ctx := mapp.BaseApp.NewContext(false, abci.Header{})
stakeHandler := stake.NewHandler(sk)

dummyDescription := stake.NewDescription("T", "E", "S", "T")
val1CreateMsg := stake.NewMsgCreateValidator(addrs[0], crypto.GenPrivKeyEd25519().PubKey(), sdk.NewCoin("steak", 25), dummyDescription)
stakeHandler(ctx, val1CreateMsg)
val2CreateMsg := stake.NewMsgCreateValidator(addrs[1], crypto.GenPrivKeyEd25519().PubKey(), sdk.NewCoin("steak", 6), dummyDescription)
stakeHandler(ctx, val2CreateMsg)
val3CreateMsg := stake.NewMsgCreateValidator(addrs[2], crypto.GenPrivKeyEd25519().PubKey(), sdk.NewCoin("steak", 7), dummyDescription)
stakeHandler(ctx, val3CreateMsg)

delegator1Msg := stake.NewMsgDelegate(addrs[3], addrs[2], sdk.NewCoin("steak", 10))
stakeHandler(ctx, delegator1Msg)
delegator1Msg2 := stake.NewMsgDelegate(addrs[3], addrs[1], sdk.NewCoin("steak", 10))
stakeHandler(ctx, delegator1Msg2)

val2, found := sk.GetValidator(ctx, addrs[1])
require.True(t, found)
sk.Revoke(ctx, val2.PubKey)

proposal := keeper.NewTextProposal(ctx, "Test", "description", ProposalTypeText)
proposalID := proposal.GetProposalID()
proposal.SetStatus(StatusVotingPeriod)
keeper.SetProposal(ctx, proposal)

err := keeper.AddVote(ctx, proposalID, addrs[0], OptionYes)
require.Nil(t, err)
err = keeper.AddVote(ctx, proposalID, addrs[1], OptionNo)
require.Nil(t, err)
err = keeper.AddVote(ctx, proposalID, addrs[2], OptionNo)
require.Nil(t, err)

passes, _ := tally(ctx, keeper, keeper.GetProposal(ctx, proposalID))

require.True(t, passes)
}