Skip to content

Commit

Permalink
Merge PR #1801: Fixed governance bug with iterate delegations
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnya97 authored and cwgoes committed Jul 24, 2018
1 parent 75eeaad commit 89eba49
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 15 deletions.
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 @@ -45,3 +45,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)
}

0 comments on commit 89eba49

Please sign in to comment.