Skip to content

Commit

Permalink
Add test for governance queues import/export fix (#3247)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnya97 authored and jackzampolin committed Jan 7, 2019
1 parent 82779a6 commit 831e3da
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 34 deletions.
8 changes: 4 additions & 4 deletions x/gov/endblocker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func TestTickExpiredDepositPeriod(t *testing.T) {
mapp, keeper, _, addrs, _, _ := getMockApp(t, 10)
mapp, keeper, _, addrs, _, _ := getMockApp(t, 10, GenesisState{}, nil)
mapp.BeginBlock(abci.RequestBeginBlock{})
ctx := mapp.BaseApp.NewContext(false, abci.Header{})
govHandler := NewHandler(keeper)
Expand Down Expand Up @@ -55,7 +55,7 @@ func TestTickExpiredDepositPeriod(t *testing.T) {
}

func TestTickMultipleExpiredDepositPeriod(t *testing.T) {
mapp, keeper, _, addrs, _, _ := getMockApp(t, 10)
mapp, keeper, _, addrs, _, _ := getMockApp(t, 10, GenesisState{}, nil)
mapp.BeginBlock(abci.RequestBeginBlock{})
ctx := mapp.BaseApp.NewContext(false, abci.Header{})
govHandler := NewHandler(keeper)
Expand Down Expand Up @@ -111,7 +111,7 @@ func TestTickMultipleExpiredDepositPeriod(t *testing.T) {
}

func TestTickPassedDepositPeriod(t *testing.T) {
mapp, keeper, _, addrs, _, _ := getMockApp(t, 10)
mapp, keeper, _, addrs, _, _ := getMockApp(t, 10, GenesisState{}, nil)
mapp.BeginBlock(abci.RequestBeginBlock{})
ctx := mapp.BaseApp.NewContext(false, abci.Header{})
govHandler := NewHandler(keeper)
Expand Down Expand Up @@ -152,7 +152,7 @@ func TestTickPassedDepositPeriod(t *testing.T) {
}

func TestTickPassedVotingPeriod(t *testing.T) {
mapp, keeper, _, addrs, _, _ := getMockApp(t, 10)
mapp, keeper, _, addrs, _, _ := getMockApp(t, 10, GenesisState{}, nil)
SortAddresses(addrs)
mapp.BeginBlock(abci.RequestBeginBlock{})
ctx := mapp.BaseApp.NewContext(false, abci.Header{})
Expand Down
52 changes: 52 additions & 0 deletions x/gov/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,58 @@ func DefaultGenesisState() GenesisState {
}
}

// Checks whether 2 GenesisState structs are equivalent.
func (data GenesisState) Equal(data2 GenesisState) bool {
if data.StartingProposalID != data.StartingProposalID ||
!data.DepositParams.Equal(data2.DepositParams) ||
data.VotingParams != data2.VotingParams ||
data.TallyParams != data2.TallyParams {
return false
}

if len(data.Deposits) != len(data2.Deposits) {
return false
}
for i := range data.Deposits {
deposit1 := data.Deposits[i]
deposit2 := data2.Deposits[i]
if deposit1.ProposalID != deposit2.ProposalID ||
!deposit1.Deposit.Equals(deposit2.Deposit) {
return false
}
}

if len(data.Votes) != len(data2.Votes) {
return false
}
for i := range data.Votes {
vote1 := data.Votes[i]
vote2 := data2.Votes[i]
if vote1.ProposalID != vote2.ProposalID ||
!vote1.Vote.Equals(vote2.Vote) {
return false
}
}

if len(data.Proposals) != len(data2.Proposals) {
return false
}
for i := range data.Proposals {
if data.Proposals[i] != data.Proposals[i] {
return false
}
}

return true

}

// Returns if a GenesisState is empty or has data in it
func (data GenesisState) IsEmpty() bool {
emptyGenState := GenesisState{}
return data.Equal(emptyGenState)
}

// ValidateGenesis TODO https://github.com/cosmos/cosmos-sdk/issues/3007
func ValidateGenesis(data GenesisState) error {
threshold := data.TallyParams.Threshold
Expand Down
54 changes: 54 additions & 0 deletions x/gov/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package gov

import (
"testing"

"github.com/cosmos/cosmos-sdk/x/mock"
"github.com/stretchr/testify/require"

abci "github.com/tendermint/tendermint/abci/types"
)

func TestImportExportQueues(t *testing.T) {

// Generate mock app and keepers
mapp, keeper, _, addrs, _, _ := getMockApp(t, 2, GenesisState{}, nil)
SortAddresses(addrs)
mapp.BeginBlock(abci.RequestBeginBlock{})
ctx := mapp.BaseApp.NewContext(false, abci.Header{})

// Create two proposals, put the second into the voting period
proposal1 := keeper.NewTextProposal(ctx, "Test", "description", ProposalTypeText)
proposalID1 := proposal1.GetProposalID()

proposal2 := keeper.NewTextProposal(ctx, "Test", "description", ProposalTypeText)
proposalID2 := proposal2.GetProposalID()

_, votingStarted := keeper.AddDeposit(ctx, proposalID2, addrs[0], keeper.GetDepositParams(ctx).MinDeposit)
require.True(t, votingStarted)

require.True(t, keeper.GetProposal(ctx, proposalID1).GetStatus() == StatusDepositPeriod)
require.True(t, keeper.GetProposal(ctx, proposalID2).GetStatus() == StatusVotingPeriod)

genAccs := mock.GetAllAccounts(mapp.AccountKeeper, ctx)

// Export the state and import it into a new Mock App
genState := ExportGenesis(ctx, keeper)
mapp2, keeper2, _, _, _, _ := getMockApp(t, 2, genState, genAccs)

mapp2.BeginBlock(abci.RequestBeginBlock{})
ctx2 := mapp2.BaseApp.NewContext(false, abci.Header{})

// Jump the time forward past the DepositPeriod and VotingPeriod
ctx2 = ctx2.WithBlockTime(ctx2.BlockHeader().Time.Add(keeper2.GetDepositParams(ctx2).MaxDepositPeriod).Add(keeper2.GetVotingParams(ctx2).VotingPeriod))

// Make sure that they are still in the DepositPeriod and VotingPeriod respectively
require.True(t, keeper2.GetProposal(ctx2, proposalID1).GetStatus() == StatusDepositPeriod)
require.True(t, keeper2.GetProposal(ctx2, proposalID2).GetStatus() == StatusVotingPeriod)

// Run the endblocker. Check to make sure that proposal1 is removed from state, and proposal2 is finished VotingPeriod.
EndBlocker(ctx2, keeper2)

require.Nil(t, keeper2.GetProposal(ctx2, proposalID1))
require.True(t, keeper2.GetProposal(ctx2, proposalID2).GetStatus() == StatusRejected)
}
12 changes: 6 additions & 6 deletions x/gov/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func TestGetSetProposal(t *testing.T) {
mapp, keeper, _, _, _, _ := getMockApp(t, 0)
mapp, keeper, _, _, _, _ := getMockApp(t, 0, GenesisState{}, nil)
mapp.BeginBlock(abci.RequestBeginBlock{})
ctx := mapp.BaseApp.NewContext(false, abci.Header{})

Expand All @@ -26,7 +26,7 @@ func TestGetSetProposal(t *testing.T) {
}

func TestIncrementProposalNumber(t *testing.T) {
mapp, keeper, _, _, _, _ := getMockApp(t, 0)
mapp, keeper, _, _, _, _ := getMockApp(t, 0, GenesisState{}, nil)
mapp.BeginBlock(abci.RequestBeginBlock{})
ctx := mapp.BaseApp.NewContext(false, abci.Header{})

Expand All @@ -41,7 +41,7 @@ func TestIncrementProposalNumber(t *testing.T) {
}

func TestActivateVotingPeriod(t *testing.T) {
mapp, keeper, _, _, _, _ := getMockApp(t, 0)
mapp, keeper, _, _, _, _ := getMockApp(t, 0, GenesisState{}, nil)
mapp.BeginBlock(abci.RequestBeginBlock{})
ctx := mapp.BaseApp.NewContext(false, abci.Header{})

Expand All @@ -62,7 +62,7 @@ func TestActivateVotingPeriod(t *testing.T) {
}

func TestDeposits(t *testing.T) {
mapp, keeper, _, addrs, _, _ := getMockApp(t, 2)
mapp, keeper, _, addrs, _, _ := getMockApp(t, 2, GenesisState{}, nil)
SortAddresses(addrs)
mapp.BeginBlock(abci.RequestBeginBlock{})
ctx := mapp.BaseApp.NewContext(false, abci.Header{})
Expand Down Expand Up @@ -149,7 +149,7 @@ func TestDeposits(t *testing.T) {
}

func TestVotes(t *testing.T) {
mapp, keeper, _, addrs, _, _ := getMockApp(t, 2)
mapp, keeper, _, addrs, _, _ := getMockApp(t, 2, GenesisState{}, nil)
SortAddresses(addrs)
mapp.BeginBlock(abci.RequestBeginBlock{})
ctx := mapp.BaseApp.NewContext(false, abci.Header{})
Expand Down Expand Up @@ -205,7 +205,7 @@ func TestVotes(t *testing.T) {
}

func TestProposalQueues(t *testing.T) {
mapp, keeper, _, _, _, _ := getMockApp(t, 0)
mapp, keeper, _, _, _, _ := getMockApp(t, 0, GenesisState{}, nil)
mapp.BeginBlock(abci.RequestBeginBlock{})
ctx := mapp.BaseApp.NewContext(false, abci.Header{})
mapp.InitChainer(ctx, abci.RequestInitChain{})
Expand Down
5 changes: 5 additions & 0 deletions x/gov/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ type DepositParams struct {
MaxDepositPeriod time.Duration `json:"max_deposit_period"` // Maximum period for Atom holders to deposit on a proposal. Initial value: 2 months
}

// Checks equality of DepositParams
func (dp DepositParams) Equal(dp2 DepositParams) bool {
return dp.MinDeposit.IsEqual(dp2.MinDeposit) && dp.MaxDepositPeriod == dp2.MaxDepositPeriod
}

// Param around Tallying votes in governance
type TallyParams struct {
Quorum sdk.Dec `json:"quorum"` // Minimum percentage of total stake needed to vote for a result to be considered valid
Expand Down
4 changes: 2 additions & 2 deletions x/gov/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func getQueriedTally(t *testing.T, ctx sdk.Context, cdc *codec.Codec, querier sd

func testQueryParams(t *testing.T) {
cdc := codec.New()
mapp, keeper, _, _, _, _ := getMockApp(t, 1000)
mapp, keeper, _, _, _, _ := getMockApp(t, 1000, GenesisState{}, nil)
querier := NewQuerier(keeper)
ctx := mapp.NewContext(false, abci.Header{})

Expand All @@ -179,7 +179,7 @@ func testQueryParams(t *testing.T) {

func testQueries(t *testing.T) {
cdc := codec.New()
mapp, keeper, _, addrs, _, _ := getMockApp(t, 1000)
mapp, keeper, _, addrs, _, _ := getMockApp(t, 1000, GenesisState{}, nil)
querier := NewQuerier(keeper)
handler := NewHandler(keeper)
ctx := mapp.NewContext(false, abci.Header{})
Expand Down
28 changes: 14 additions & 14 deletions x/gov/tally_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func createValidators(t *testing.T, stakeHandler sdk.Handler, ctx sdk.Context, a
}

func TestTallyNoOneVotes(t *testing.T) {
mapp, keeper, sk, addrs, _, _ := getMockApp(t, 10)
mapp, keeper, sk, addrs, _, _ := getMockApp(t, 10, GenesisState{}, nil)
mapp.BeginBlock(abci.RequestBeginBlock{})
ctx := mapp.BaseApp.NewContext(false, abci.Header{})
stakeHandler := stake.NewHandler(sk)
Expand All @@ -61,7 +61,7 @@ func TestTallyNoOneVotes(t *testing.T) {
}

func TestTallyNoQuorum(t *testing.T) {
mapp, keeper, sk, addrs, _, _ := getMockApp(t, 10)
mapp, keeper, sk, addrs, _, _ := getMockApp(t, 10, GenesisState{}, nil)
mapp.BeginBlock(abci.RequestBeginBlock{})
ctx := mapp.BaseApp.NewContext(false, abci.Header{})
stakeHandler := stake.NewHandler(sk)
Expand All @@ -87,7 +87,7 @@ func TestTallyNoQuorum(t *testing.T) {
}

func TestTallyOnlyValidatorsAllYes(t *testing.T) {
mapp, keeper, sk, addrs, _, _ := getMockApp(t, 10)
mapp, keeper, sk, addrs, _, _ := getMockApp(t, 10, GenesisState{}, nil)
mapp.BeginBlock(abci.RequestBeginBlock{})
ctx := mapp.BaseApp.NewContext(false, abci.Header{})
stakeHandler := stake.NewHandler(sk)
Expand Down Expand Up @@ -117,7 +117,7 @@ func TestTallyOnlyValidatorsAllYes(t *testing.T) {
}

func TestTallyOnlyValidators51No(t *testing.T) {
mapp, keeper, sk, addrs, _, _ := getMockApp(t, 10)
mapp, keeper, sk, addrs, _, _ := getMockApp(t, 10, GenesisState{}, nil)
mapp.BeginBlock(abci.RequestBeginBlock{})
ctx := mapp.BaseApp.NewContext(false, abci.Header{})
stakeHandler := stake.NewHandler(sk)
Expand Down Expand Up @@ -146,7 +146,7 @@ func TestTallyOnlyValidators51No(t *testing.T) {
}

func TestTallyOnlyValidators51Yes(t *testing.T) {
mapp, keeper, sk, addrs, _, _ := getMockApp(t, 10)
mapp, keeper, sk, addrs, _, _ := getMockApp(t, 10, GenesisState{}, nil)
mapp.BeginBlock(abci.RequestBeginBlock{})
ctx := mapp.BaseApp.NewContext(false, abci.Header{})
stakeHandler := stake.NewHandler(sk)
Expand Down Expand Up @@ -178,7 +178,7 @@ func TestTallyOnlyValidators51Yes(t *testing.T) {
}

func TestTallyOnlyValidatorsVetoed(t *testing.T) {
mapp, keeper, sk, addrs, _, _ := getMockApp(t, 10)
mapp, keeper, sk, addrs, _, _ := getMockApp(t, 10, GenesisState{}, nil)
mapp.BeginBlock(abci.RequestBeginBlock{})
ctx := mapp.BaseApp.NewContext(false, abci.Header{})
stakeHandler := stake.NewHandler(sk)
Expand Down Expand Up @@ -210,7 +210,7 @@ func TestTallyOnlyValidatorsVetoed(t *testing.T) {
}

func TestTallyOnlyValidatorsAbstainPasses(t *testing.T) {
mapp, keeper, sk, addrs, _, _ := getMockApp(t, 10)
mapp, keeper, sk, addrs, _, _ := getMockApp(t, 10, GenesisState{}, nil)
mapp.BeginBlock(abci.RequestBeginBlock{})
ctx := mapp.BaseApp.NewContext(false, abci.Header{})
stakeHandler := stake.NewHandler(sk)
Expand Down Expand Up @@ -242,7 +242,7 @@ func TestTallyOnlyValidatorsAbstainPasses(t *testing.T) {
}

func TestTallyOnlyValidatorsAbstainFails(t *testing.T) {
mapp, keeper, sk, addrs, _, _ := getMockApp(t, 10)
mapp, keeper, sk, addrs, _, _ := getMockApp(t, 10, GenesisState{}, nil)
mapp.BeginBlock(abci.RequestBeginBlock{})
ctx := mapp.BaseApp.NewContext(false, abci.Header{})
stakeHandler := stake.NewHandler(sk)
Expand Down Expand Up @@ -274,7 +274,7 @@ func TestTallyOnlyValidatorsAbstainFails(t *testing.T) {
}

func TestTallyOnlyValidatorsNonVoter(t *testing.T) {
mapp, keeper, sk, addrs, _, _ := getMockApp(t, 10)
mapp, keeper, sk, addrs, _, _ := getMockApp(t, 10, GenesisState{}, nil)
mapp.BeginBlock(abci.RequestBeginBlock{})
ctx := mapp.BaseApp.NewContext(false, abci.Header{})
stakeHandler := stake.NewHandler(sk)
Expand Down Expand Up @@ -304,7 +304,7 @@ func TestTallyOnlyValidatorsNonVoter(t *testing.T) {
}

func TestTallyDelgatorOverride(t *testing.T) {
mapp, keeper, sk, addrs, _, _ := getMockApp(t, 10)
mapp, keeper, sk, addrs, _, _ := getMockApp(t, 10, GenesisState{}, nil)
mapp.BeginBlock(abci.RequestBeginBlock{})
ctx := mapp.BaseApp.NewContext(false, abci.Header{})
stakeHandler := stake.NewHandler(sk)
Expand Down Expand Up @@ -341,7 +341,7 @@ func TestTallyDelgatorOverride(t *testing.T) {
}

func TestTallyDelgatorInherit(t *testing.T) {
mapp, keeper, sk, addrs, _, _ := getMockApp(t, 10)
mapp, keeper, sk, addrs, _, _ := getMockApp(t, 10, GenesisState{}, nil)
mapp.BeginBlock(abci.RequestBeginBlock{})
ctx := mapp.BaseApp.NewContext(false, abci.Header{})
stakeHandler := stake.NewHandler(sk)
Expand Down Expand Up @@ -376,7 +376,7 @@ func TestTallyDelgatorInherit(t *testing.T) {
}

func TestTallyDelgatorMultipleOverride(t *testing.T) {
mapp, keeper, sk, addrs, _, _ := getMockApp(t, 10)
mapp, keeper, sk, addrs, _, _ := getMockApp(t, 10, GenesisState{}, nil)
mapp.BeginBlock(abci.RequestBeginBlock{})
ctx := mapp.BaseApp.NewContext(false, abci.Header{})
stakeHandler := stake.NewHandler(sk)
Expand Down Expand Up @@ -415,7 +415,7 @@ func TestTallyDelgatorMultipleOverride(t *testing.T) {
}

func TestTallyDelgatorMultipleInherit(t *testing.T) {
mapp, keeper, sk, addrs, _, _ := getMockApp(t, 10)
mapp, keeper, sk, addrs, _, _ := getMockApp(t, 10, GenesisState{}, nil)
mapp.BeginBlock(abci.RequestBeginBlock{})
ctx := mapp.BaseApp.NewContext(false, abci.Header{})
stakeHandler := stake.NewHandler(sk)
Expand Down Expand Up @@ -462,7 +462,7 @@ func TestTallyDelgatorMultipleInherit(t *testing.T) {
}

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

0 comments on commit 831e3da

Please sign in to comment.