Skip to content

Commit

Permalink
migreate TestTallyDelgatorOverride to simapp
Browse files Browse the repository at this point in the history
  • Loading branch information
jgimeno committed Mar 2, 2020
1 parent 46a70d5 commit 9f25188
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 40 deletions.
4 changes: 2 additions & 2 deletions x/gov/keeper/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ func ProposalEqual(proposalA types.Proposal, proposalB types.Proposal) bool {
}

func createValidators(ctx sdk.Context, app *simapp.SimApp, powers []int64) ([]sdk.AccAddress, []sdk.ValAddress) {
addrs := simapp.AddTestAddrsIncremental(app, ctx, 3, sdk.NewInt(10000000))
addrs := simapp.AddTestAddrsIncremental(app, ctx, 5, sdk.NewInt(30000000))
valAddrs := simapp.ConvertAddrsToValAddrs(addrs)
pks := simapp.CreateTestPubKeys(3)
pks := simapp.CreateTestPubKeys(5)

appCodec := codec.NewAppCodec(app.Codec())
app.StakingKeeper = staking.NewKeeper(
Expand Down
34 changes: 0 additions & 34 deletions x/gov/keeper/old_tally_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,6 @@ import (
"github.com/cosmos/cosmos-sdk/x/staking"
)

func TestTallyDelgatorOverride(t *testing.T) {
ctx, _, _, keeper, sk, _ := createTestInput(t, false, 100)
createValidators(ctx, sk, []int64{5, 6, 7})

delTokens := sdk.TokensFromConsensusPower(30)
val1, found := sk.GetValidator(ctx, valOpAddr1)
require.True(t, found)

_, err := sk.Delegate(ctx, TestAddrs[0], delTokens, sdk.Unbonded, val1, true)
require.NoError(t, err)

_ = staking.EndBlocker(ctx, sk)

tp := TestProposal
proposal, err := keeper.SubmitProposal(ctx, tp)
require.NoError(t, err)
proposalID := proposal.ProposalID
proposal.Status = types.StatusVotingPeriod
keeper.SetProposal(ctx, proposal)

require.NoError(t, keeper.AddVote(ctx, proposalID, valAccAddr1, types.OptionYes))
require.NoError(t, keeper.AddVote(ctx, proposalID, valAccAddr2, types.OptionYes))
require.NoError(t, keeper.AddVote(ctx, proposalID, valAccAddr3, types.OptionYes))
require.NoError(t, keeper.AddVote(ctx, proposalID, TestAddrs[0], types.OptionNo))

proposal, ok := keeper.GetProposal(ctx, proposalID)
require.True(t, ok)
passes, burnDeposits, tallyResults := keeper.Tally(ctx, proposal)

require.False(t, passes)
require.False(t, burnDeposits)
require.False(t, tallyResults.Equals(types.EmptyTallyResult()))
}

func TestTallyDelgatorInherit(t *testing.T) {
ctx, _, _, keeper, sk, _ := createTestInput(t, false, 100)
createValidators(ctx, sk, []int64{5, 6, 7})
Expand Down
47 changes: 43 additions & 4 deletions x/gov/keeper/tally_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package keeper_test
import (
"testing"

"github.com/cosmos/cosmos-sdk/x/staking"

"github.com/stretchr/testify/require"

abci "github.com/tendermint/tendermint/abci/types"
Expand Down Expand Up @@ -191,7 +193,6 @@ func TestTallyOnlyValidatorsAbstainFails(t *testing.T) {
ctx := app.BaseApp.NewContext(false, abci.Header{})

valAccAddrs, _ := createValidators(ctx, app, []int64{6, 6, 7})
valAccAddr1, valAccAddr2, valAccAddr3 := valAccAddrs[0], valAccAddrs[1], valAccAddrs[2]

tp := TestProposal
proposal, err := app.GovKeeper.SubmitProposal(ctx, tp)
Expand All @@ -200,9 +201,9 @@ func TestTallyOnlyValidatorsAbstainFails(t *testing.T) {
proposal.Status = types.StatusVotingPeriod
app.GovKeeper.SetProposal(ctx, proposal)

require.NoError(t, app.GovKeeper.AddVote(ctx, proposalID, valAccAddr1, types.OptionAbstain))
require.NoError(t, app.GovKeeper.AddVote(ctx, proposalID, valAccAddr2, types.OptionYes))
require.NoError(t, app.GovKeeper.AddVote(ctx, proposalID, valAccAddr3, types.OptionNo))
require.NoError(t, app.GovKeeper.AddVote(ctx, proposalID, valAccAddrs[0], types.OptionAbstain))
require.NoError(t, app.GovKeeper.AddVote(ctx, proposalID, valAccAddrs[1], types.OptionYes))
require.NoError(t, app.GovKeeper.AddVote(ctx, proposalID, valAccAddrs[2], types.OptionNo))

proposal, ok := app.GovKeeper.GetProposal(ctx, proposalID)
require.True(t, ok)
Expand Down Expand Up @@ -238,3 +239,41 @@ func TestTallyOnlyValidatorsNonVoter(t *testing.T) {
require.False(t, burnDeposits)
require.False(t, tallyResults.Equals(types.EmptyTallyResult()))
}

func TestTallyDelgatorOverride(t *testing.T) {
app := simapp.Setup(false)
ctx := app.BaseApp.NewContext(false, abci.Header{})

addrs, valAddrs := createValidators(ctx, app, []int64{5, 6, 7})
valOpAddr1 := valAddrs[0]
valAccAddr1, valAccAddr2, valAccAddr3, valAccAddr4 := addrs[1], addrs[2], addrs[3], addrs[4]

delTokens := sdk.TokensFromConsensusPower(30)
val1, found := app.StakingKeeper.GetValidator(ctx, valOpAddr1)
require.True(t, found)

_, err := app.StakingKeeper.Delegate(ctx, valAccAddr4, delTokens, sdk.Unbonded, val1, true)
require.NoError(t, err)

_ = staking.EndBlocker(ctx, app.StakingKeeper)

tp := TestProposal
proposal, err := app.GovKeeper.SubmitProposal(ctx, tp)
require.NoError(t, err)
proposalID := proposal.ProposalID
proposal.Status = types.StatusVotingPeriod
app.GovKeeper.SetProposal(ctx, proposal)

require.NoError(t, app.GovKeeper.AddVote(ctx, proposalID, valAccAddr1, types.OptionYes))
require.NoError(t, app.GovKeeper.AddVote(ctx, proposalID, valAccAddr2, types.OptionYes))
require.NoError(t, app.GovKeeper.AddVote(ctx, proposalID, valAccAddr3, types.OptionYes))
require.NoError(t, app.GovKeeper.AddVote(ctx, proposalID, valAccAddr4, types.OptionNo))

proposal, ok := app.GovKeeper.GetProposal(ctx, proposalID)
require.True(t, ok)
passes, burnDeposits, tallyResults := app.GovKeeper.Tally(ctx, proposal)

require.False(t, passes)
require.False(t, burnDeposits)
require.False(t, tallyResults.Equals(types.EmptyTallyResult()))
}

0 comments on commit 9f25188

Please sign in to comment.