From e5299bd8ee38a74cd43c7bee64cad0eba8391dbf Mon Sep 17 00:00:00 2001 From: Raneet Debnath <35629432+Raneet10@users.noreply.github.com> Date: Fri, 10 Jan 2025 12:47:09 +0530 Subject: [PATCH] rm: useless tests (#1223) Co-authored-by: marcello33 --- Makefile | 2 +- gov/genesis_test.go | 130 --------- gov/keeper_test.go | 356 ------------------------ gov/querier_test.go | 302 --------------------- gov/tally_test.go | 604 ----------------------------------------- gov/types/keys_test.go | 16 +- gov/types/msgs_test.go | 121 --------- 7 files changed, 10 insertions(+), 1521 deletions(-) delete mode 100644 gov/genesis_test.go delete mode 100644 gov/keeper_test.go delete mode 100644 gov/querier_test.go delete mode 100644 gov/tally_test.go delete mode 100644 gov/types/msgs_test.go diff --git a/Makefile b/Makefile index 150069c0e..db8d50c51 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,7 @@ clean: tests: # go test -v ./... - go test -v ./app/ ./auth/ ./clerk/ ./sidechannel/ ./bank/ ./chainmanager/ ./topup/ ./checkpoint/ ./staking/ -cover -coverprofile=cover.out -parallel 1 + go test -v ./app/ ./auth/ ./clerk/ ./sidechannel/ ./bank/ ./chainmanager/ ./topup/ ./checkpoint/ ./staking/ ./gov/ -cover -coverprofile=cover.out -parallel 1 # make build build: clean diff --git a/gov/genesis_test.go b/gov/genesis_test.go deleted file mode 100644 index 62fa3a9e4..000000000 --- a/gov/genesis_test.go +++ /dev/null @@ -1,130 +0,0 @@ -package gov - -import ( - "testing" - - "github.com/stretchr/testify/require" - abci "github.com/tendermint/tendermint/abci/types" -) - -func TestEqualProposalID(t *testing.T) { - state1 := GenesisState{} - state2 := GenesisState{} - require.Equal(t, state1, state2) - - // Proposals - state1.StartingProposalID = 1 - require.NotEqual(t, state1, state2) - require.False(t, state1.Equal(state2)) - - state2.StartingProposalID = 1 - require.Equal(t, state1, state2) - require.True(t, state1.Equal(state2)) -} - -func TestEqualProposals(t *testing.T) { - // Generate mock app and keepers - input := getMockApp(t, 2, GenesisState{}, nil) - SortAddresses(input.addrs) - - header := abci.Header{Height: input.mApp.LastBlockHeight() + 1} - input.mApp.BeginBlock(abci.RequestBeginBlock{Header: header}) - - ctx := input.mApp.BaseApp.NewContext(false, abci.Header{}) - - // Submit two proposals - proposal := testProposal() - proposal1, err := input.keeper.SubmitProposal(ctx, proposal) - require.NoError(t, err) - proposal2, err := input.keeper.SubmitProposal(ctx, proposal) - require.NoError(t, err) - - // They are similar but their IDs should be different - require.NotEqual(t, proposal1, proposal2) - require.False(t, ProposalEqual(proposal1, proposal2)) - - // Now create two genesis blocks - state1 := GenesisState{Proposals: []Proposal{proposal1}} - state2 := GenesisState{Proposals: []Proposal{proposal2}} - require.NotEqual(t, state1, state2) - require.False(t, state1.Equal(state2)) - - // Now make proposals identical by setting both IDs to 55 - proposal1.ProposalID = 55 - proposal2.ProposalID = 55 - require.Equal(t, proposal1, proposal1) - require.True(t, ProposalEqual(proposal1, proposal2)) - - // Reassign proposals into state - state1.Proposals[0] = proposal1 - state2.Proposals[0] = proposal2 - - // State should be identical now.. - require.Equal(t, state1, state2) - require.True(t, state1.Equal(state2)) -} - -func TestImportExportQueues(t *testing.T) { - // Generate mock app and keepers - input := getMockApp(t, 2, GenesisState{}, nil) - SortAddresses(input.addrs) - - header := abci.Header{Height: input.mApp.LastBlockHeight() + 1} - input.mApp.BeginBlock(abci.RequestBeginBlock{Header: header}) - - ctx := input.mApp.BaseApp.NewContext(false, abci.Header{}) - - // Create two proposals, put the second into the voting period - proposal := testProposal() - proposal1, err := input.keeper.SubmitProposal(ctx, proposal) - require.NoError(t, err) - proposalID1 := proposal1.ProposalID - - proposal2, err := input.keeper.SubmitProposal(ctx, proposal) - require.NoError(t, err) - proposalID2 := proposal2.ProposalID - - err, votingStarted := input.keeper.AddDeposit(ctx, proposalID2, input.addrs[0], input.keeper.GetDepositParams(ctx).MinDeposit) - require.NoError(t, err) - require.True(t, votingStarted) - - proposal1, ok := input.keeper.GetProposal(ctx, proposalID1) - require.True(t, ok) - proposal2, ok = input.keeper.GetProposal(ctx, proposalID2) - require.True(t, ok) - require.True(t, proposal1.Status == StatusDepositPeriod) - require.True(t, proposal2.Status == StatusVotingPeriod) - - genAccs := input.mApp.AccountKeeper.GetAllAccounts(ctx) - - // Export the state and import it into a new Mock App - genState := ExportGenesis(ctx, input.keeper) - input2 := getMockApp(t, 2, genState, genAccs) - - header = abci.Header{Height: input.mApp.LastBlockHeight() + 1} - input2.mApp.BeginBlock(abci.RequestBeginBlock{Header: header}) - - ctx2 := input2.mApp.BaseApp.NewContext(false, abci.Header{}) - - // Jump the time forward past the DepositPeriod and VotingPeriod - ctx2 = ctx2.WithBlockTime(ctx2.BlockHeader().Time.Add(input2.keeper.GetDepositParams(ctx2).MaxDepositPeriod).Add(input2.keeper.GetVotingParams(ctx2).VotingPeriod)) - - // Make sure that they are still in the DepositPeriod and VotingPeriod respectively - proposal1, ok = input2.keeper.GetProposal(ctx2, proposalID1) - require.True(t, ok) - proposal2, ok = input2.keeper.GetProposal(ctx2, proposalID2) - require.True(t, ok) - require.True(t, proposal1.Status == StatusDepositPeriod) - require.True(t, proposal2.Status == StatusVotingPeriod) - - require.Equal(t, input2.keeper.GetDepositParams(ctx2).MinDeposit, input2.keeper.GetGovernanceAccount(ctx2).GetCoins()) - - // Run the endblocker. Check to make sure that proposal1 is removed from state, and proposal2 is finished VotingPeriod. - EndBlocker(ctx2, input2.keeper) - - proposal1, ok = input2.keeper.GetProposal(ctx2, proposalID1) - require.False(t, ok) - proposal2, ok = input2.keeper.GetProposal(ctx2, proposalID2) - require.True(t, ok) - require.True(t, proposal2.Status == StatusRejected) -} diff --git a/gov/keeper_test.go b/gov/keeper_test.go deleted file mode 100644 index 8689bf55d..000000000 --- a/gov/keeper_test.go +++ /dev/null @@ -1,356 +0,0 @@ -package gov - -import ( - "strings" - "testing" - "time" - - "github.com/stretchr/testify/require" - - abci "github.com/tendermint/tendermint/abci/types" - - "github.com/cosmos/cosmos-sdk/codec" - sdk "github.com/cosmos/cosmos-sdk/types" -) - -func TestGetSetProposal(t *testing.T) { - input := getMockApp(t, 0, GenesisState{}, nil) - - header := abci.Header{Height: input.mApp.LastBlockHeight() + 1} - input.mApp.BeginBlock(abci.RequestBeginBlock{Header: header}) - - ctx := input.mApp.BaseApp.NewContext(false, abci.Header{}) - - tp := testProposal() - proposal, err := input.keeper.SubmitProposal(ctx, tp) - require.NoError(t, err) - proposalID := proposal.ProposalID - input.keeper.SetProposal(ctx, proposal) - - gotProposal, ok := input.keeper.GetProposal(ctx, proposalID) - require.True(t, ok) - require.True(t, ProposalEqual(proposal, gotProposal)) -} - -func TestIncrementProposalNumber(t *testing.T) { - input := getMockApp(t, 0, GenesisState{}, nil) - - header := abci.Header{Height: input.mApp.LastBlockHeight() + 1} - input.mApp.BeginBlock(abci.RequestBeginBlock{Header: header}) - - ctx := input.mApp.BaseApp.NewContext(false, abci.Header{}) - - tp := testProposal() - input.keeper.SubmitProposal(ctx, tp) - input.keeper.SubmitProposal(ctx, tp) - input.keeper.SubmitProposal(ctx, tp) - input.keeper.SubmitProposal(ctx, tp) - input.keeper.SubmitProposal(ctx, tp) - proposal6, err := input.keeper.SubmitProposal(ctx, tp) - require.NoError(t, err) - - require.Equal(t, uint64(6), proposal6.ProposalID) -} - -func TestActivateVotingPeriod(t *testing.T) { - input := getMockApp(t, 0, GenesisState{}, nil) - - header := abci.Header{Height: input.mApp.LastBlockHeight() + 1} - input.mApp.BeginBlock(abci.RequestBeginBlock{Header: header}) - - ctx := input.mApp.BaseApp.NewContext(false, abci.Header{}) - - tp := testProposal() - proposal, err := input.keeper.SubmitProposal(ctx, tp) - require.NoError(t, err) - - require.True(t, proposal.VotingStartTime.Equal(time.Time{})) - - input.keeper.activateVotingPeriod(ctx, proposal) - - require.True(t, proposal.VotingStartTime.Equal(ctx.BlockHeader().Time)) - - proposal, ok := input.keeper.GetProposal(ctx, proposal.ProposalID) - require.True(t, ok) - - activeIterator := input.keeper.ActiveProposalQueueIterator(ctx, proposal.VotingEndTime) - require.True(t, activeIterator.Valid()) - var proposalID uint64 - input.keeper.cdc.UnmarshalBinaryLengthPrefixed(activeIterator.Value(), &proposalID) - require.Equal(t, proposalID, proposal.ProposalID) - activeIterator.Close() -} - -func TestDeposits(t *testing.T) { - input := getMockApp(t, 2, GenesisState{}, nil) - - SortAddresses(input.addrs) - - header := abci.Header{Height: input.mApp.LastBlockHeight() + 1} - input.mApp.BeginBlock(abci.RequestBeginBlock{Header: header}) - - ctx := input.mApp.BaseApp.NewContext(false, abci.Header{}) - - tp := testProposal() - proposal, err := input.keeper.SubmitProposal(ctx, tp) - require.NoError(t, err) - proposalID := proposal.ProposalID - - fourStake := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.TokensFromConsensusPower(4))) - fiveStake := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.TokensFromConsensusPower(5))) - - addr0Initial := input.mApp.AccountKeeper.GetAccount(ctx, input.addrs[0]).GetCoins() - addr1Initial := input.mApp.AccountKeeper.GetAccount(ctx, input.addrs[1]).GetCoins() - - expTokens := sdk.TokensFromConsensusPower(42) - require.Equal(t, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, expTokens)), addr0Initial) - require.True(t, proposal.TotalDeposit.IsEqual(sdk.NewCoins())) - - // Check no deposits at beginning - deposit, found := input.keeper.GetDeposit(ctx, proposalID, input.addrs[1]) - require.False(t, found) - proposal, ok := input.keeper.GetProposal(ctx, proposalID) - require.True(t, ok) - require.True(t, proposal.VotingStartTime.Equal(time.Time{})) - - // Check first deposit - err, votingStarted := input.keeper.AddDeposit(ctx, proposalID, input.addrs[0], fourStake) - require.Nil(t, err) - require.False(t, votingStarted) - deposit, found = input.keeper.GetDeposit(ctx, proposalID, input.addrs[0]) - require.True(t, found) - require.Equal(t, fourStake, deposit.Amount) - require.Equal(t, input.addrs[0], deposit.Depositor) - proposal, ok = input.keeper.GetProposal(ctx, proposalID) - require.True(t, ok) - require.Equal(t, fourStake, proposal.TotalDeposit) - require.Equal(t, addr0Initial.Sub(fourStake), input.mApp.AccountKeeper.GetAccount(ctx, input.addrs[0]).GetCoins()) - - // Check a second deposit from same address - err, votingStarted = input.keeper.AddDeposit(ctx, proposalID, input.addrs[0], fiveStake) - require.Nil(t, err) - require.False(t, votingStarted) - deposit, found = input.keeper.GetDeposit(ctx, proposalID, input.addrs[0]) - require.True(t, found) - require.Equal(t, fourStake.Add(fiveStake), deposit.Amount) - require.Equal(t, input.addrs[0], deposit.Depositor) - proposal, ok = input.keeper.GetProposal(ctx, proposalID) - require.True(t, ok) - require.Equal(t, fourStake.Add(fiveStake), proposal.TotalDeposit) - require.Equal(t, addr0Initial.Sub(fourStake).Sub(fiveStake), input.mApp.AccountKeeper.GetAccount(ctx, input.addrs[0]).GetCoins()) - - // Check third deposit from a new address - err, votingStarted = input.keeper.AddDeposit(ctx, proposalID, input.addrs[1], fourStake) - require.Nil(t, err) - require.True(t, votingStarted) - deposit, found = input.keeper.GetDeposit(ctx, proposalID, input.addrs[1]) - require.True(t, found) - require.Equal(t, input.addrs[1], deposit.Depositor) - require.Equal(t, fourStake, deposit.Amount) - proposal, ok = input.keeper.GetProposal(ctx, proposalID) - require.True(t, ok) - require.Equal(t, fourStake.Add(fiveStake).Add(fourStake), proposal.TotalDeposit) - require.Equal(t, addr1Initial.Sub(fourStake), input.mApp.AccountKeeper.GetAccount(ctx, input.addrs[1]).GetCoins()) - - // Check that proposal moved to voting period - proposal, ok = input.keeper.GetProposal(ctx, proposalID) - require.True(t, ok) - require.True(t, proposal.VotingStartTime.Equal(ctx.BlockHeader().Time)) - - // Test deposit iterator - depositsIterator := input.keeper.GetDepositsIterator(ctx, proposalID) - require.True(t, depositsIterator.Valid()) - input.keeper.cdc.MustUnmarshalBinaryLengthPrefixed(depositsIterator.Value(), &deposit) - require.Equal(t, input.addrs[0], deposit.Depositor) - require.Equal(t, fourStake.Add(fiveStake), deposit.Amount) - depositsIterator.Next() - input.keeper.cdc.MustUnmarshalBinaryLengthPrefixed(depositsIterator.Value(), &deposit) - require.Equal(t, input.addrs[1], deposit.Depositor) - require.Equal(t, fourStake, deposit.Amount) - depositsIterator.Next() - require.False(t, depositsIterator.Valid()) - depositsIterator.Close() - - // Test Refund Deposits - deposit, found = input.keeper.GetDeposit(ctx, proposalID, input.addrs[1]) - require.True(t, found) - require.Equal(t, fourStake, deposit.Amount) - input.keeper.RefundDeposits(ctx, proposalID) - deposit, found = input.keeper.GetDeposit(ctx, proposalID, input.addrs[1]) - require.False(t, found) - require.Equal(t, addr0Initial, input.mApp.AccountKeeper.GetAccount(ctx, input.addrs[0]).GetCoins()) - require.Equal(t, addr1Initial, input.mApp.AccountKeeper.GetAccount(ctx, input.addrs[1]).GetCoins()) - -} - -func TestVotes(t *testing.T) { - input := getMockApp(t, 2, GenesisState{}, nil) - SortAddresses(input.addrs) - - header := abci.Header{Height: input.mApp.LastBlockHeight() + 1} - input.mApp.BeginBlock(abci.RequestBeginBlock{Header: header}) - - ctx := input.mApp.BaseApp.NewContext(false, abci.Header{}) - - tp := testProposal() - proposal, err := input.keeper.SubmitProposal(ctx, tp) - require.NoError(t, err) - proposalID := proposal.ProposalID - - proposal.Status = StatusVotingPeriod - input.keeper.SetProposal(ctx, proposal) - - // Test first vote - input.keeper.AddVote(ctx, proposalID, input.addrs[0], OptionAbstain) - vote, found := input.keeper.GetVote(ctx, proposalID, input.addrs[0]) - require.True(t, found) - require.Equal(t, input.addrs[0], vote.Voter) - require.Equal(t, proposalID, vote.ProposalID) - require.Equal(t, OptionAbstain, vote.Option) - - // Test change of vote - input.keeper.AddVote(ctx, proposalID, input.addrs[0], OptionYes) - vote, found = input.keeper.GetVote(ctx, proposalID, input.addrs[0]) - require.True(t, found) - require.Equal(t, input.addrs[0], vote.Voter) - require.Equal(t, proposalID, vote.ProposalID) - require.Equal(t, OptionYes, vote.Option) - - // Test second vote - input.keeper.AddVote(ctx, proposalID, input.addrs[1], OptionNoWithVeto) - vote, found = input.keeper.GetVote(ctx, proposalID, input.addrs[1]) - require.True(t, found) - require.Equal(t, input.addrs[1], vote.Voter) - require.Equal(t, proposalID, vote.ProposalID) - require.Equal(t, OptionNoWithVeto, vote.Option) - - // Test vote iterator - votesIterator := input.keeper.GetVotesIterator(ctx, proposalID) - require.True(t, votesIterator.Valid()) - input.keeper.cdc.MustUnmarshalBinaryLengthPrefixed(votesIterator.Value(), &vote) - require.True(t, votesIterator.Valid()) - require.Equal(t, input.addrs[0], vote.Voter) - require.Equal(t, proposalID, vote.ProposalID) - require.Equal(t, OptionYes, vote.Option) - votesIterator.Next() - require.True(t, votesIterator.Valid()) - input.keeper.cdc.MustUnmarshalBinaryLengthPrefixed(votesIterator.Value(), &vote) - require.True(t, votesIterator.Valid()) - require.Equal(t, input.addrs[1], vote.Voter) - require.Equal(t, proposalID, vote.ProposalID) - require.Equal(t, OptionNoWithVeto, vote.Option) - votesIterator.Next() - require.False(t, votesIterator.Valid()) - votesIterator.Close() -} - -func TestProposalQueues(t *testing.T) { - input := getMockApp(t, 0, GenesisState{}, nil) - - header := abci.Header{Height: input.mApp.LastBlockHeight() + 1} - input.mApp.BeginBlock(abci.RequestBeginBlock{Header: header}) - - ctx := input.mApp.BaseApp.NewContext(false, abci.Header{}) - input.mApp.InitChainer(ctx, abci.RequestInitChain{}) - - // create test proposals - tp := testProposal() - proposal, err := input.keeper.SubmitProposal(ctx, tp) - require.NoError(t, err) - - inactiveIterator := input.keeper.InactiveProposalQueueIterator(ctx, proposal.DepositEndTime) - require.True(t, inactiveIterator.Valid()) - var proposalID uint64 - input.keeper.cdc.UnmarshalBinaryLengthPrefixed(inactiveIterator.Value(), &proposalID) - require.Equal(t, proposalID, proposal.ProposalID) - inactiveIterator.Close() - - input.keeper.activateVotingPeriod(ctx, proposal) - - proposal, ok := input.keeper.GetProposal(ctx, proposal.ProposalID) - require.True(t, ok) - - activeIterator := input.keeper.ActiveProposalQueueIterator(ctx, proposal.VotingEndTime) - require.True(t, activeIterator.Valid()) - input.keeper.cdc.UnmarshalBinaryLengthPrefixed(activeIterator.Value(), &proposalID) - require.Equal(t, proposalID, proposal.ProposalID) - activeIterator.Close() -} - -type validProposal struct{} - -func (validProposal) GetTitle() string { return "title" } -func (validProposal) GetDescription() string { return "description" } -func (validProposal) ProposalRoute() string { return RouterKey } -func (validProposal) ProposalType() string { return ProposalTypeText } -func (validProposal) String() string { return "" } -func (validProposal) ValidateBasic() sdk.Error { return nil } - -type invalidProposalTitle1 struct{ validProposal } - -func (invalidProposalTitle1) GetTitle() string { return "" } - -type invalidProposalTitle2 struct{ validProposal } - -func (invalidProposalTitle2) GetTitle() string { return strings.Repeat("1234567890", 100) } - -type invalidProposalDesc1 struct{ validProposal } - -func (invalidProposalDesc1) GetDescription() string { return "" } - -type invalidProposalDesc2 struct{ validProposal } - -func (invalidProposalDesc2) GetDescription() string { return strings.Repeat("1234567890", 1000) } - -type invalidProposalRoute struct{ validProposal } - -func (invalidProposalRoute) ProposalRoute() string { return "nonexistingroute" } - -type invalidProposalValidation struct{ validProposal } - -func (invalidProposalValidation) ValidateBasic() sdk.Error { - return sdk.NewError(sdk.CodespaceUndefined, sdk.CodeInternal, "") -} - -func registerTestCodec(cdc *codec.Codec) { - cdc.RegisterConcrete(validProposal{}, "test/validproposal", nil) - cdc.RegisterConcrete(invalidProposalTitle1{}, "test/invalidproposalt1", nil) - cdc.RegisterConcrete(invalidProposalTitle2{}, "test/invalidproposalt2", nil) - cdc.RegisterConcrete(invalidProposalDesc1{}, "test/invalidproposald1", nil) - cdc.RegisterConcrete(invalidProposalDesc2{}, "test/invalidproposald2", nil) - cdc.RegisterConcrete(invalidProposalRoute{}, "test/invalidproposalr", nil) - cdc.RegisterConcrete(invalidProposalValidation{}, "test/invalidproposalv", nil) -} - -func TestSubmitProposal(t *testing.T) { - input := getMockApp(t, 0, GenesisState{}, nil) - - registerTestCodec(input.keeper.cdc) - - header := abci.Header{Height: input.mApp.LastBlockHeight() + 1} - input.mApp.BeginBlock(abci.RequestBeginBlock{Header: header}) - - ctx := input.mApp.BaseApp.NewContext(false, abci.Header{}) - input.mApp.InitChainer(ctx, abci.RequestInitChain{}) - - testCases := []struct { - content Content - expectedErr sdk.Error - }{ - {validProposal{}, nil}, - // Keeper does not check the validity of title and description, no error - {invalidProposalTitle1{}, nil}, - {invalidProposalTitle2{}, nil}, - {invalidProposalDesc1{}, nil}, - {invalidProposalDesc2{}, nil}, - // error only when invalid route - {invalidProposalRoute{}, ErrNoProposalHandlerExists(DefaultCodespace, invalidProposalRoute{})}, - // Keeper does not call ValidateBasic, msg.ValidateBasic does - {invalidProposalValidation{}, nil}, - } - - for _, tc := range testCases { - _, err := input.keeper.SubmitProposal(ctx, tc.content) - require.Equal(t, tc.expectedErr, err, "unexpected type of error: %s", err) - } -} diff --git a/gov/querier_test.go b/gov/querier_test.go deleted file mode 100644 index 33772e781..000000000 --- a/gov/querier_test.go +++ /dev/null @@ -1,302 +0,0 @@ -package gov - -import ( - "strings" - "testing" - - "github.com/stretchr/testify/require" - abci "github.com/tendermint/tendermint/abci/types" - - "github.com/cosmos/cosmos-sdk/codec" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/maticnetwork/heimdall/gov/types" -) - -const custom = "custom" - -func getQueriedParams(t *testing.T, ctx sdk.Context, cdc *codec.Codec, querier sdk.Querier) (DepositParams, VotingParams, TallyParams) { - query := abci.RequestQuery{ - Path: strings.Join([]string{custom, QuerierRoute, QueryParams, ParamDeposit}, "/"), - Data: []byte{}, - } - - bz, err := querier(ctx, []string{QueryParams, ParamDeposit}, query) - require.Nil(t, err) - require.NotNil(t, bz) - - var depositParams DepositParams - err2 := cdc.UnmarshalJSON(bz, &depositParams) - require.Nil(t, err2) - - query = abci.RequestQuery{ - Path: strings.Join([]string{custom, QuerierRoute, QueryParams, ParamVoting}, "/"), - Data: []byte{}, - } - - bz, err = querier(ctx, []string{QueryParams, ParamVoting}, query) - require.Nil(t, err) - require.NotNil(t, bz) - - var votingParams VotingParams - err2 = cdc.UnmarshalJSON(bz, &votingParams) - require.Nil(t, err2) - - query = abci.RequestQuery{ - Path: strings.Join([]string{custom, QuerierRoute, QueryParams, ParamTallying}, "/"), - Data: []byte{}, - } - - bz, err = querier(ctx, []string{QueryParams, ParamTallying}, query) - require.Nil(t, err) - require.NotNil(t, bz) - - var tallyParams TallyParams - err2 = cdc.UnmarshalJSON(bz, &tallyParams) - require.Nil(t, err2) - - return depositParams, votingParams, tallyParams -} - -func getQueriedProposal(t *testing.T, ctx sdk.Context, cdc *codec.Codec, querier sdk.Querier, proposalID uint64) Proposal { - query := abci.RequestQuery{ - Path: strings.Join([]string{custom, QuerierRoute, QueryProposal}, "/"), - Data: cdc.MustMarshalJSON(NewQueryProposalParams(proposalID)), - } - - bz, err := querier(ctx, []string{QueryProposal}, query) - require.Nil(t, err) - require.NotNil(t, bz) - - var proposal Proposal - err2 := cdc.UnmarshalJSON(bz, proposal) - require.Nil(t, err2) - return proposal -} - -func getQueriedProposals(t *testing.T, ctx sdk.Context, cdc *codec.Codec, querier sdk.Querier, depositor, voter sdk.AccAddress, status ProposalStatus, limit uint64) []Proposal { - query := abci.RequestQuery{ - Path: strings.Join([]string{custom, QuerierRoute, QueryProposals}, "/"), - Data: cdc.MustMarshalJSON(NewQueryProposalsParams(status, limit, voter, depositor)), - } - - bz, err := querier(ctx, []string{QueryProposals}, query) - require.Nil(t, err) - require.NotNil(t, bz) - - var proposals Proposals - err2 := cdc.UnmarshalJSON(bz, &proposals) - require.Nil(t, err2) - return proposals -} - -func getQueriedDeposit(t *testing.T, ctx sdk.Context, cdc *codec.Codec, querier sdk.Querier, proposalID uint64, depositor sdk.AccAddress) Deposit { - query := abci.RequestQuery{ - Path: strings.Join([]string{custom, QuerierRoute, QueryDeposit}, "/"), - Data: cdc.MustMarshalJSON(NewQueryDepositParams(proposalID, depositor)), - } - - bz, err := querier(ctx, []string{QueryDeposit}, query) - require.Nil(t, err) - require.NotNil(t, bz) - - var deposit Deposit - err2 := cdc.UnmarshalJSON(bz, &deposit) - require.Nil(t, err2) - return deposit -} - -func getQueriedDeposits(t *testing.T, ctx sdk.Context, cdc *codec.Codec, querier sdk.Querier, proposalID uint64) []Deposit { - query := abci.RequestQuery{ - Path: strings.Join([]string{custom, QuerierRoute, QueryDeposits}, "/"), - Data: cdc.MustMarshalJSON(NewQueryProposalParams(proposalID)), - } - - bz, err := querier(ctx, []string{QueryDeposits}, query) - require.Nil(t, err) - require.NotNil(t, bz) - - var deposits []Deposit - err2 := cdc.UnmarshalJSON(bz, &deposits) - require.Nil(t, err2) - return deposits -} - -func getQueriedVote(t *testing.T, ctx sdk.Context, cdc *codec.Codec, querier sdk.Querier, proposalID uint64, voter sdk.AccAddress) Vote { - query := abci.RequestQuery{ - Path: strings.Join([]string{custom, QuerierRoute, QueryVote}, "/"), - Data: cdc.MustMarshalJSON(NewQueryVoteParams(proposalID, voter)), - } - - bz, err := querier(ctx, []string{QueryVote}, query) - require.Nil(t, err) - require.NotNil(t, bz) - - var vote Vote - err2 := cdc.UnmarshalJSON(bz, &vote) - require.Nil(t, err2) - return vote -} - -func getQueriedVotes(t *testing.T, ctx sdk.Context, cdc *codec.Codec, querier sdk.Querier, proposalID uint64) []Vote { - query := abci.RequestQuery{ - Path: strings.Join([]string{custom, QuerierRoute, QueryVote}, "/"), - Data: cdc.MustMarshalJSON(NewQueryProposalParams(proposalID)), - } - - bz, err := querier(ctx, []string{QueryVotes}, query) - require.Nil(t, err) - require.NotNil(t, bz) - - var votes []Vote - err2 := cdc.UnmarshalJSON(bz, &votes) - require.Nil(t, err2) - return votes -} - -func getQueriedTally(t *testing.T, ctx sdk.Context, cdc *codec.Codec, querier sdk.Querier, proposalID uint64) TallyResult { - query := abci.RequestQuery{ - Path: strings.Join([]string{custom, QuerierRoute, QueryTally}, "/"), - Data: cdc.MustMarshalJSON(NewQueryProposalParams(proposalID)), - } - - bz, err := querier(ctx, []string{QueryTally}, query) - require.Nil(t, err) - require.NotNil(t, bz) - - var tally TallyResult - err2 := cdc.UnmarshalJSON(bz, &tally) - require.Nil(t, err2) - return tally -} - -func TestQueryParams(t *testing.T) { - cdc := codec.New() - input := getMockApp(t, 1000, GenesisState{}, nil) - querier := NewQuerier(input.keeper) - - header := abci.Header{Height: input.mApp.LastBlockHeight() + 1} - input.mApp.BeginBlock(abci.RequestBeginBlock{Header: header}) - - ctx := input.mApp.NewContext(false, abci.Header{}) - - getQueriedParams(t, ctx, cdc, querier) -} - -func TestQueries(t *testing.T) { - cdc := codec.New() - input := getMockApp(t, 1000, GenesisState{}, nil) - querier := NewQuerier(input.keeper) - handler := NewHandler(input.keeper) - - types.RegisterCodec(cdc) - - header := abci.Header{Height: input.mApp.LastBlockHeight() + 1} - input.mApp.BeginBlock(abci.RequestBeginBlock{Header: header}) - - ctx := input.mApp.NewContext(false, abci.Header{}) - - depositParams, _, _ := getQueriedParams(t, ctx, cdc, querier) - - // input.addrs[0] proposes (and deposits) proposals #1 and #2 - res := handler(ctx, NewMsgSubmitProposal(testProposal(), sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 1)}, input.addrs[0])) - var proposalID1 uint64 - require.True(t, res.IsOK()) - cdc.MustUnmarshalBinaryLengthPrefixed(res.Data, &proposalID1) - - res = handler(ctx, NewMsgSubmitProposal(testProposal(), sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 10000000)}, input.addrs[0])) - var proposalID2 uint64 - require.True(t, res.IsOK()) - cdc.MustUnmarshalBinaryLengthPrefixed(res.Data, &proposalID2) - - // input.addrs[1] proposes (and deposits) proposals #3 - res = handler(ctx, NewMsgSubmitProposal(testProposal(), sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 1)}, input.addrs[1])) - var proposalID3 uint64 - require.True(t, res.IsOK()) - cdc.MustUnmarshalBinaryLengthPrefixed(res.Data, &proposalID3) - - // input.addrs[1] deposits on proposals #2 & #3 - res = handler(ctx, NewMsgDeposit(input.addrs[1], proposalID2, depositParams.MinDeposit)) - res = handler(ctx, NewMsgDeposit(input.addrs[1], proposalID3, depositParams.MinDeposit)) - - // check deposits on proposal1 match individual deposits - deposits := getQueriedDeposits(t, ctx, cdc, querier, proposalID1) - require.Len(t, deposits, 1) - deposit := getQueriedDeposit(t, ctx, cdc, querier, proposalID1, input.addrs[0]) - require.Equal(t, deposit, deposits[0]) - - // check deposits on proposal2 match individual deposits - deposits = getQueriedDeposits(t, ctx, cdc, querier, proposalID2) - require.Len(t, deposits, 2) - deposit = getQueriedDeposit(t, ctx, cdc, querier, proposalID2, input.addrs[0]) - require.True(t, deposit.Equals(deposits[0])) - deposit = getQueriedDeposit(t, ctx, cdc, querier, proposalID2, input.addrs[1]) - require.True(t, deposit.Equals(deposits[1])) - - // check deposits on proposal3 match individual deposits - deposits = getQueriedDeposits(t, ctx, cdc, querier, proposalID3) - require.Len(t, deposits, 1) - deposit = getQueriedDeposit(t, ctx, cdc, querier, proposalID3, input.addrs[1]) - require.Equal(t, deposit, deposits[0]) - - // Only proposal #1 should be in Deposit Period - proposals := getQueriedProposals(t, ctx, cdc, querier, nil, nil, StatusDepositPeriod, 0) - require.Len(t, proposals, 1) - require.Equal(t, proposalID1, proposals[0].ProposalID) - - // Only proposals #2 and #3 should be in Voting Period - proposals = getQueriedProposals(t, ctx, cdc, querier, nil, nil, StatusVotingPeriod, 0) - require.Len(t, proposals, 2) - require.Equal(t, proposalID2, proposals[0].ProposalID) - require.Equal(t, proposalID3, proposals[1].ProposalID) - - // Addrs[0] votes on proposals #2 & #3 - require.True(t, handler(ctx, NewMsgVote(input.addrs[0], proposalID2, OptionYes)).IsOK()) - require.True(t, handler(ctx, NewMsgVote(input.addrs[0], proposalID3, OptionYes)).IsOK()) - - // Addrs[1] votes on proposal #3 - handler(ctx, NewMsgVote(input.addrs[1], proposalID3, OptionYes)) - - // Test query voted by input.addrs[0] - proposals = getQueriedProposals(t, ctx, cdc, querier, nil, input.addrs[0], StatusNil, 0) - require.Equal(t, proposalID2, (proposals[0]).ProposalID) - require.Equal(t, proposalID3, (proposals[1]).ProposalID) - - // Test query votes on Proposal 2 - votes := getQueriedVotes(t, ctx, cdc, querier, proposalID2) - require.Len(t, votes, 1) - require.Equal(t, input.addrs[0], votes[0].Voter) - - vote := getQueriedVote(t, ctx, cdc, querier, proposalID2, input.addrs[0]) - require.Equal(t, vote, votes[0]) - - // Test query votes on Proposal 3 - votes = getQueriedVotes(t, ctx, cdc, querier, proposalID3) - require.Len(t, votes, 2) - require.True(t, input.addrs[0].String() == votes[0].Voter.String()) - require.True(t, input.addrs[1].String() == votes[1].Voter.String()) - - // Test proposals queries with filters - - // Test query all proposals - proposals = getQueriedProposals(t, ctx, cdc, querier, nil, nil, StatusNil, 0) - require.Equal(t, proposalID1, (proposals[0]).ProposalID) - require.Equal(t, proposalID2, (proposals[1]).ProposalID) - require.Equal(t, proposalID3, (proposals[2]).ProposalID) - - // Test query voted by input.addrs[1] - proposals = getQueriedProposals(t, ctx, cdc, querier, nil, input.addrs[1], StatusNil, 0) - require.Equal(t, proposalID3, (proposals[0]).ProposalID) - - // Test query deposited by input.addrs[0] - proposals = getQueriedProposals(t, ctx, cdc, querier, input.addrs[0], nil, StatusNil, 0) - require.Equal(t, proposalID1, (proposals[0]).ProposalID) - - // Test query deposited by addr2 - proposals = getQueriedProposals(t, ctx, cdc, querier, input.addrs[1], nil, StatusNil, 0) - require.Equal(t, proposalID2, (proposals[0]).ProposalID) - require.Equal(t, proposalID3, (proposals[1]).ProposalID) - - // Test query voted AND deposited by addr1 - proposals = getQueriedProposals(t, ctx, cdc, querier, input.addrs[0], input.addrs[0], StatusNil, 0) - require.Equal(t, proposalID2, (proposals[0]).ProposalID) -} diff --git a/gov/tally_test.go b/gov/tally_test.go deleted file mode 100644 index 929f353ba..000000000 --- a/gov/tally_test.go +++ /dev/null @@ -1,604 +0,0 @@ -package gov - -import ( - "testing" - - "github.com/stretchr/testify/require" - - abci "github.com/tendermint/tendermint/abci/types" - "github.com/tendermint/tendermint/crypto/ed25519" - - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/staking" -) - -func TestTallyNoOneVotes(t *testing.T) { - input := getMockApp(t, 10, GenesisState{}, nil) - - header := abci.Header{Height: input.mApp.LastBlockHeight() + 1} - input.mApp.BeginBlock(abci.RequestBeginBlock{Header: header}) - - ctx := input.mApp.BaseApp.NewContext(false, abci.Header{}) - stakingHandler := staking.NewHandler(input.sk) - - valAddrs := make([]sdk.ValAddress, len(input.addrs[:2])) - for i, addr := range input.addrs[:2] { - valAddrs[i] = sdk.ValAddress(addr) - } - - createValidators(t, stakingHandler, ctx, valAddrs, []int64{5, 5}) - staking.EndBlocker(ctx, input.sk) - - tp := testProposal() - proposal, err := input.keeper.SubmitProposal(ctx, tp) - require.NoError(t, err) - proposalID := proposal.ProposalID - proposal.Status = StatusVotingPeriod - input.keeper.SetProposal(ctx, proposal) - - proposal, ok := input.keeper.GetProposal(ctx, proposalID) - require.True(t, ok) - passes, burnDeposits, tallyResults := tally(ctx, input.keeper, proposal) - - require.False(t, passes) - require.True(t, burnDeposits) - require.True(t, tallyResults.Equals(EmptyTallyResult())) -} - -func TestTallyNoQuorum(t *testing.T) { - input := getMockApp(t, 10, GenesisState{}, nil) - - header := abci.Header{Height: input.mApp.LastBlockHeight() + 1} - input.mApp.BeginBlock(abci.RequestBeginBlock{Header: header}) - - ctx := input.mApp.BaseApp.NewContext(false, abci.Header{}) - stakingHandler := staking.NewHandler(input.sk) - - valAddrs := make([]sdk.ValAddress, len(input.addrs[:2])) - for i, addr := range input.addrs[:2] { - valAddrs[i] = sdk.ValAddress(addr) - } - - createValidators(t, stakingHandler, ctx, valAddrs, []int64{2, 5}) - staking.EndBlocker(ctx, input.sk) - - tp := testProposal() - proposal, err := input.keeper.SubmitProposal(ctx, tp) - require.NoError(t, err) - proposalID := proposal.ProposalID - proposal.Status = StatusVotingPeriod - input.keeper.SetProposal(ctx, proposal) - - err = input.keeper.AddVote(ctx, proposalID, input.addrs[0], OptionYes) - require.Nil(t, err) - - proposal, ok := input.keeper.GetProposal(ctx, proposalID) - require.True(t, ok) - passes, burnDeposits, _ := tally(ctx, input.keeper, proposal) - require.False(t, passes) - require.True(t, burnDeposits) -} - -func TestTallyOnlyValidatorsAllYes(t *testing.T) { - input := getMockApp(t, 10, GenesisState{}, nil) - - header := abci.Header{Height: input.mApp.LastBlockHeight() + 1} - input.mApp.BeginBlock(abci.RequestBeginBlock{Header: header}) - - ctx := input.mApp.BaseApp.NewContext(false, abci.Header{}) - stakingHandler := staking.NewHandler(input.sk) - - valAddrs := make([]sdk.ValAddress, len(input.addrs[:2])) - for i, addr := range input.addrs[:2] { - valAddrs[i] = sdk.ValAddress(addr) - } - - createValidators(t, stakingHandler, ctx, valAddrs, []int64{5, 5}) - staking.EndBlocker(ctx, input.sk) - - tp := testProposal() - proposal, err := input.keeper.SubmitProposal(ctx, tp) - require.NoError(t, err) - proposalID := proposal.ProposalID - proposal.Status = StatusVotingPeriod - input.keeper.SetProposal(ctx, proposal) - - err = input.keeper.AddVote(ctx, proposalID, input.addrs[0], OptionYes) - require.Nil(t, err) - err = input.keeper.AddVote(ctx, proposalID, input.addrs[1], OptionYes) - require.Nil(t, err) - - proposal, ok := input.keeper.GetProposal(ctx, proposalID) - require.True(t, ok) - passes, burnDeposits, tallyResults := tally(ctx, input.keeper, proposal) - - require.True(t, passes) - require.False(t, burnDeposits) - require.False(t, tallyResults.Equals(EmptyTallyResult())) -} - -func TestTallyOnlyValidators51No(t *testing.T) { - input := getMockApp(t, 10, GenesisState{}, nil) - - header := abci.Header{Height: input.mApp.LastBlockHeight() + 1} - input.mApp.BeginBlock(abci.RequestBeginBlock{Header: header}) - - ctx := input.mApp.BaseApp.NewContext(false, abci.Header{}) - stakingHandler := staking.NewHandler(input.sk) - - valAddrs := make([]sdk.ValAddress, len(input.addrs[:2])) - for i, addr := range input.addrs[:2] { - valAddrs[i] = sdk.ValAddress(addr) - } - - createValidators(t, stakingHandler, ctx, valAddrs, []int64{5, 6}) - staking.EndBlocker(ctx, input.sk) - - tp := testProposal() - proposal, err := input.keeper.SubmitProposal(ctx, tp) - require.NoError(t, err) - proposalID := proposal.ProposalID - proposal.Status = StatusVotingPeriod - input.keeper.SetProposal(ctx, proposal) - - err = input.keeper.AddVote(ctx, proposalID, input.addrs[0], OptionYes) - require.Nil(t, err) - err = input.keeper.AddVote(ctx, proposalID, input.addrs[1], OptionNo) - require.Nil(t, err) - - proposal, ok := input.keeper.GetProposal(ctx, proposalID) - require.True(t, ok) - passes, burnDeposits, _ := tally(ctx, input.keeper, proposal) - - require.False(t, passes) - require.False(t, burnDeposits) -} - -func TestTallyOnlyValidators51Yes(t *testing.T) { - input := getMockApp(t, 10, GenesisState{}, nil) - - header := abci.Header{Height: input.mApp.LastBlockHeight() + 1} - input.mApp.BeginBlock(abci.RequestBeginBlock{Header: header}) - - ctx := input.mApp.BaseApp.NewContext(false, abci.Header{}) - stakingHandler := staking.NewHandler(input.sk) - - valAddrs := make([]sdk.ValAddress, len(input.addrs[:3])) - for i, addr := range input.addrs[:3] { - valAddrs[i] = sdk.ValAddress(addr) - } - - createValidators(t, stakingHandler, ctx, valAddrs, []int64{6, 6, 7}) - staking.EndBlocker(ctx, input.sk) - - tp := testProposal() - proposal, err := input.keeper.SubmitProposal(ctx, tp) - require.NoError(t, err) - proposalID := proposal.ProposalID - proposal.Status = StatusVotingPeriod - input.keeper.SetProposal(ctx, proposal) - - err = input.keeper.AddVote(ctx, proposalID, input.addrs[0], OptionYes) - require.Nil(t, err) - err = input.keeper.AddVote(ctx, proposalID, input.addrs[1], OptionYes) - require.Nil(t, err) - err = input.keeper.AddVote(ctx, proposalID, input.addrs[2], OptionNo) - require.Nil(t, err) - - proposal, ok := input.keeper.GetProposal(ctx, proposalID) - require.True(t, ok) - passes, burnDeposits, tallyResults := tally(ctx, input.keeper, proposal) - - require.True(t, passes) - require.False(t, burnDeposits) - require.False(t, tallyResults.Equals(EmptyTallyResult())) -} - -func TestTallyOnlyValidatorsVetoed(t *testing.T) { - input := getMockApp(t, 10, GenesisState{}, nil) - - header := abci.Header{Height: input.mApp.LastBlockHeight() + 1} - input.mApp.BeginBlock(abci.RequestBeginBlock{Header: header}) - - ctx := input.mApp.BaseApp.NewContext(false, abci.Header{}) - stakingHandler := staking.NewHandler(input.sk) - - valAddrs := make([]sdk.ValAddress, len(input.addrs[:3])) - for i, addr := range input.addrs[:3] { - valAddrs[i] = sdk.ValAddress(addr) - } - - createValidators(t, stakingHandler, ctx, valAddrs, []int64{6, 6, 7}) - staking.EndBlocker(ctx, input.sk) - - tp := testProposal() - proposal, err := input.keeper.SubmitProposal(ctx, tp) - require.NoError(t, err) - proposalID := proposal.ProposalID - proposal.Status = StatusVotingPeriod - input.keeper.SetProposal(ctx, proposal) - - err = input.keeper.AddVote(ctx, proposalID, input.addrs[0], OptionYes) - require.Nil(t, err) - err = input.keeper.AddVote(ctx, proposalID, input.addrs[1], OptionYes) - require.Nil(t, err) - err = input.keeper.AddVote(ctx, proposalID, input.addrs[2], OptionNoWithVeto) - require.Nil(t, err) - - proposal, ok := input.keeper.GetProposal(ctx, proposalID) - require.True(t, ok) - passes, burnDeposits, tallyResults := tally(ctx, input.keeper, proposal) - - require.False(t, passes) - require.True(t, burnDeposits) - require.False(t, tallyResults.Equals(EmptyTallyResult())) - -} - -func TestTallyOnlyValidatorsAbstainPasses(t *testing.T) { - input := getMockApp(t, 10, GenesisState{}, nil) - - header := abci.Header{Height: input.mApp.LastBlockHeight() + 1} - input.mApp.BeginBlock(abci.RequestBeginBlock{Header: header}) - - ctx := input.mApp.BaseApp.NewContext(false, abci.Header{}) - stakingHandler := staking.NewHandler(input.sk) - - valAddrs := make([]sdk.ValAddress, len(input.addrs[:3])) - for i, addr := range input.addrs[:3] { - valAddrs[i] = sdk.ValAddress(addr) - } - - createValidators(t, stakingHandler, ctx, valAddrs, []int64{6, 6, 7}) - staking.EndBlocker(ctx, input.sk) - - tp := testProposal() - proposal, err := input.keeper.SubmitProposal(ctx, tp) - require.NoError(t, err) - proposalID := proposal.ProposalID - proposal.Status = StatusVotingPeriod - input.keeper.SetProposal(ctx, proposal) - - err = input.keeper.AddVote(ctx, proposalID, input.addrs[0], OptionAbstain) - require.Nil(t, err) - err = input.keeper.AddVote(ctx, proposalID, input.addrs[1], OptionNo) - require.Nil(t, err) - err = input.keeper.AddVote(ctx, proposalID, input.addrs[2], OptionYes) - require.Nil(t, err) - - proposal, ok := input.keeper.GetProposal(ctx, proposalID) - require.True(t, ok) - passes, burnDeposits, tallyResults := tally(ctx, input.keeper, proposal) - - require.True(t, passes) - require.False(t, burnDeposits) - require.False(t, tallyResults.Equals(EmptyTallyResult())) -} - -func TestTallyOnlyValidatorsAbstainFails(t *testing.T) { - input := getMockApp(t, 10, GenesisState{}, nil) - - header := abci.Header{Height: input.mApp.LastBlockHeight() + 1} - input.mApp.BeginBlock(abci.RequestBeginBlock{Header: header}) - - ctx := input.mApp.BaseApp.NewContext(false, abci.Header{}) - stakingHandler := staking.NewHandler(input.sk) - - valAddrs := make([]sdk.ValAddress, len(input.addrs[:3])) - for i, addr := range input.addrs[:3] { - valAddrs[i] = sdk.ValAddress(addr) - } - - createValidators(t, stakingHandler, ctx, valAddrs, []int64{6, 6, 7}) - staking.EndBlocker(ctx, input.sk) - - tp := testProposal() - proposal, err := input.keeper.SubmitProposal(ctx, tp) - require.NoError(t, err) - proposalID := proposal.ProposalID - proposal.Status = StatusVotingPeriod - input.keeper.SetProposal(ctx, proposal) - - err = input.keeper.AddVote(ctx, proposalID, input.addrs[0], OptionAbstain) - require.Nil(t, err) - err = input.keeper.AddVote(ctx, proposalID, input.addrs[1], OptionYes) - require.Nil(t, err) - err = input.keeper.AddVote(ctx, proposalID, input.addrs[2], OptionNo) - require.Nil(t, err) - - proposal, ok := input.keeper.GetProposal(ctx, proposalID) - require.True(t, ok) - passes, burnDeposits, tallyResults := tally(ctx, input.keeper, proposal) - - require.False(t, passes) - require.False(t, burnDeposits) - require.False(t, tallyResults.Equals(EmptyTallyResult())) -} - -func TestTallyOnlyValidatorsNonVoter(t *testing.T) { - input := getMockApp(t, 10, GenesisState{}, nil) - - header := abci.Header{Height: input.mApp.LastBlockHeight() + 1} - input.mApp.BeginBlock(abci.RequestBeginBlock{Header: header}) - - ctx := input.mApp.BaseApp.NewContext(false, abci.Header{}) - stakingHandler := staking.NewHandler(input.sk) - - valAddrs := make([]sdk.ValAddress, len(input.addrs[:3])) - for i, addr := range input.addrs[:3] { - valAddrs[i] = sdk.ValAddress(addr) - } - - createValidators(t, stakingHandler, ctx, valAddrs, []int64{6, 6, 7}) - staking.EndBlocker(ctx, input.sk) - - tp := testProposal() - proposal, err := input.keeper.SubmitProposal(ctx, tp) - require.NoError(t, err) - proposalID := proposal.ProposalID - proposal.Status = StatusVotingPeriod - input.keeper.SetProposal(ctx, proposal) - - err = input.keeper.AddVote(ctx, proposalID, input.addrs[1], OptionYes) - require.Nil(t, err) - err = input.keeper.AddVote(ctx, proposalID, input.addrs[2], OptionNo) - require.Nil(t, err) - - proposal, ok := input.keeper.GetProposal(ctx, proposalID) - require.True(t, ok) - passes, burnDeposits, tallyResults := tally(ctx, input.keeper, proposal) - - require.False(t, passes) - require.False(t, burnDeposits) - require.False(t, tallyResults.Equals(EmptyTallyResult())) -} - -func TestTallyDelegatorOverride(t *testing.T) { - input := getMockApp(t, 10, GenesisState{}, nil) - - header := abci.Header{Height: input.mApp.LastBlockHeight() + 1} - input.mApp.BeginBlock(abci.RequestBeginBlock{Header: header}) - - ctx := input.mApp.BaseApp.NewContext(false, abci.Header{}) - stakingHandler := staking.NewHandler(input.sk) - - valAddrs := make([]sdk.ValAddress, len(input.addrs[:3])) - for i, addr := range input.addrs[:3] { - valAddrs[i] = sdk.ValAddress(addr) - } - - createValidators(t, stakingHandler, ctx, valAddrs, []int64{5, 6, 7}) - staking.EndBlocker(ctx, input.sk) - - delTokens := sdk.TokensFromConsensusPower(30) - delegator1Msg := staking.NewMsgDelegate(input.addrs[3], sdk.ValAddress(input.addrs[2]), sdk.NewCoin(sdk.DefaultBondDenom, delTokens)) - stakingHandler(ctx, delegator1Msg) - - tp := testProposal() - proposal, err := input.keeper.SubmitProposal(ctx, tp) - require.NoError(t, err) - proposalID := proposal.ProposalID - proposal.Status = StatusVotingPeriod - input.keeper.SetProposal(ctx, proposal) - - err = input.keeper.AddVote(ctx, proposalID, input.addrs[0], OptionYes) - require.Nil(t, err) - err = input.keeper.AddVote(ctx, proposalID, input.addrs[1], OptionYes) - require.Nil(t, err) - err = input.keeper.AddVote(ctx, proposalID, input.addrs[2], OptionYes) - require.Nil(t, err) - err = input.keeper.AddVote(ctx, proposalID, input.addrs[3], OptionNo) - require.Nil(t, err) - - proposal, ok := input.keeper.GetProposal(ctx, proposalID) - require.True(t, ok) - passes, burnDeposits, tallyResults := tally(ctx, input.keeper, proposal) - - require.False(t, passes) - require.False(t, burnDeposits) - require.False(t, tallyResults.Equals(EmptyTallyResult())) -} - -func TestTallyDelegatorInherit(t *testing.T) { - input := getMockApp(t, 10, GenesisState{}, nil) - - header := abci.Header{Height: input.mApp.LastBlockHeight() + 1} - input.mApp.BeginBlock(abci.RequestBeginBlock{Header: header}) - - ctx := input.mApp.BaseApp.NewContext(false, abci.Header{}) - stakingHandler := staking.NewHandler(input.sk) - - valAddrs := make([]sdk.ValAddress, len(input.addrs[:3])) - for i, addr := range input.addrs[:3] { - valAddrs[i] = sdk.ValAddress(addr) - } - - createValidators(t, stakingHandler, ctx, valAddrs, []int64{5, 6, 7}) - staking.EndBlocker(ctx, input.sk) - - delTokens := sdk.TokensFromConsensusPower(30) - delegator1Msg := staking.NewMsgDelegate(input.addrs[3], sdk.ValAddress(input.addrs[2]), sdk.NewCoin(sdk.DefaultBondDenom, delTokens)) - stakingHandler(ctx, delegator1Msg) - - tp := testProposal() - proposal, err := input.keeper.SubmitProposal(ctx, tp) - require.NoError(t, err) - proposalID := proposal.ProposalID - proposal.Status = StatusVotingPeriod - input.keeper.SetProposal(ctx, proposal) - - err = input.keeper.AddVote(ctx, proposalID, input.addrs[0], OptionNo) - require.Nil(t, err) - err = input.keeper.AddVote(ctx, proposalID, input.addrs[1], OptionNo) - require.Nil(t, err) - err = input.keeper.AddVote(ctx, proposalID, input.addrs[2], OptionYes) - require.Nil(t, err) - - proposal, ok := input.keeper.GetProposal(ctx, proposalID) - require.True(t, ok) - passes, burnDeposits, tallyResults := tally(ctx, input.keeper, proposal) - - require.True(t, passes) - require.False(t, burnDeposits) - require.False(t, tallyResults.Equals(EmptyTallyResult())) -} - -func TestTallyDelegatorMultipleOverride(t *testing.T) { - input := getMockApp(t, 10, GenesisState{}, nil) - - header := abci.Header{Height: input.mApp.LastBlockHeight() + 1} - input.mApp.BeginBlock(abci.RequestBeginBlock{Header: header}) - - ctx := input.mApp.BaseApp.NewContext(false, abci.Header{}) - stakingHandler := staking.NewHandler(input.sk) - - valAddrs := make([]sdk.ValAddress, len(input.addrs[:3])) - for i, addr := range input.addrs[:3] { - valAddrs[i] = sdk.ValAddress(addr) - } - - createValidators(t, stakingHandler, ctx, valAddrs, []int64{5, 6, 7}) - staking.EndBlocker(ctx, input.sk) - - delTokens := sdk.TokensFromConsensusPower(10) - delegator1Msg := staking.NewMsgDelegate(input.addrs[3], sdk.ValAddress(input.addrs[2]), sdk.NewCoin(sdk.DefaultBondDenom, delTokens)) - stakingHandler(ctx, delegator1Msg) - delegator1Msg2 := staking.NewMsgDelegate(input.addrs[3], sdk.ValAddress(input.addrs[1]), sdk.NewCoin(sdk.DefaultBondDenom, delTokens)) - stakingHandler(ctx, delegator1Msg2) - - tp := testProposal() - proposal, err := input.keeper.SubmitProposal(ctx, tp) - require.NoError(t, err) - proposalID := proposal.ProposalID - proposal.Status = StatusVotingPeriod - input.keeper.SetProposal(ctx, proposal) - - err = input.keeper.AddVote(ctx, proposalID, input.addrs[0], OptionYes) - require.Nil(t, err) - err = input.keeper.AddVote(ctx, proposalID, input.addrs[1], OptionYes) - require.Nil(t, err) - err = input.keeper.AddVote(ctx, proposalID, input.addrs[2], OptionYes) - require.Nil(t, err) - err = input.keeper.AddVote(ctx, proposalID, input.addrs[3], OptionNo) - require.Nil(t, err) - - proposal, ok := input.keeper.GetProposal(ctx, proposalID) - require.True(t, ok) - passes, burnDeposits, tallyResults := tally(ctx, input.keeper, proposal) - - require.False(t, passes) - require.False(t, burnDeposits) - require.False(t, tallyResults.Equals(EmptyTallyResult())) -} - -func TestTallyDelegatorMultipleInherit(t *testing.T) { - input := getMockApp(t, 10, GenesisState{}, nil) - - header := abci.Header{Height: input.mApp.LastBlockHeight() + 1} - input.mApp.BeginBlock(abci.RequestBeginBlock{Header: header}) - - ctx := input.mApp.BaseApp.NewContext(false, abci.Header{}) - stakingHandler := staking.NewHandler(input.sk) - - valTokens1 := sdk.TokensFromConsensusPower(25) - val1CreateMsg := staking.NewMsgCreateValidator( - sdk.ValAddress(input.addrs[0]), ed25519.GenPrivKey().PubKey(), sdk.NewCoin(sdk.DefaultBondDenom, valTokens1), testDescription, testCommissionRates, sdk.OneInt(), - ) - stakingHandler(ctx, val1CreateMsg) - - valTokens2 := sdk.TokensFromConsensusPower(6) - val2CreateMsg := staking.NewMsgCreateValidator( - sdk.ValAddress(input.addrs[1]), ed25519.GenPrivKey().PubKey(), sdk.NewCoin(sdk.DefaultBondDenom, valTokens2), testDescription, testCommissionRates, sdk.OneInt(), - ) - stakingHandler(ctx, val2CreateMsg) - - valTokens3 := sdk.TokensFromConsensusPower(7) - val3CreateMsg := staking.NewMsgCreateValidator( - sdk.ValAddress(input.addrs[2]), ed25519.GenPrivKey().PubKey(), sdk.NewCoin(sdk.DefaultBondDenom, valTokens3), testDescription, testCommissionRates, sdk.OneInt(), - ) - stakingHandler(ctx, val3CreateMsg) - - delTokens := sdk.TokensFromConsensusPower(10) - delegator1Msg := staking.NewMsgDelegate(input.addrs[3], sdk.ValAddress(input.addrs[2]), sdk.NewCoin(sdk.DefaultBondDenom, delTokens)) - stakingHandler(ctx, delegator1Msg) - - delegator1Msg2 := staking.NewMsgDelegate(input.addrs[3], sdk.ValAddress(input.addrs[1]), sdk.NewCoin(sdk.DefaultBondDenom, delTokens)) - stakingHandler(ctx, delegator1Msg2) - - staking.EndBlocker(ctx, input.sk) - - tp := testProposal() - proposal, err := input.keeper.SubmitProposal(ctx, tp) - require.NoError(t, err) - proposalID := proposal.ProposalID - proposal.Status = StatusVotingPeriod - input.keeper.SetProposal(ctx, proposal) - - err = input.keeper.AddVote(ctx, proposalID, input.addrs[0], OptionYes) - require.Nil(t, err) - err = input.keeper.AddVote(ctx, proposalID, input.addrs[1], OptionNo) - require.Nil(t, err) - err = input.keeper.AddVote(ctx, proposalID, input.addrs[2], OptionNo) - require.Nil(t, err) - - proposal, ok := input.keeper.GetProposal(ctx, proposalID) - require.True(t, ok) - passes, burnDeposits, tallyResults := tally(ctx, input.keeper, proposal) - - require.False(t, passes) - require.False(t, burnDeposits) - require.False(t, tallyResults.Equals(EmptyTallyResult())) -} - -func TestTallyJailedValidator(t *testing.T) { - input := getMockApp(t, 10, GenesisState{}, nil) - - header := abci.Header{Height: input.mApp.LastBlockHeight() + 1} - input.mApp.BeginBlock(abci.RequestBeginBlock{Header: header}) - - ctx := input.mApp.BaseApp.NewContext(false, abci.Header{}) - stakingHandler := staking.NewHandler(input.sk) - - valAddrs := make([]sdk.ValAddress, len(input.addrs[:3])) - for i, addr := range input.addrs[:3] { - valAddrs[i] = sdk.ValAddress(addr) - } - - createValidators(t, stakingHandler, ctx, valAddrs, []int64{25, 6, 7}) - staking.EndBlocker(ctx, input.sk) - - delTokens := sdk.TokensFromConsensusPower(10) - delegator1Msg := staking.NewMsgDelegate(input.addrs[3], sdk.ValAddress(input.addrs[2]), sdk.NewCoin(sdk.DefaultBondDenom, delTokens)) - stakingHandler(ctx, delegator1Msg) - - delegator1Msg2 := staking.NewMsgDelegate(input.addrs[3], sdk.ValAddress(input.addrs[1]), sdk.NewCoin(sdk.DefaultBondDenom, delTokens)) - stakingHandler(ctx, delegator1Msg2) - - val2, found := input.sk.GetValidator(ctx, sdk.ValAddress(input.addrs[1])) - require.True(t, found) - input.sk.Jail(ctx, sdk.ConsAddress(val2.ConsPubKey.Address())) - - staking.EndBlocker(ctx, input.sk) - - tp := testProposal() - proposal, err := input.keeper.SubmitProposal(ctx, tp) - require.NoError(t, err) - proposalID := proposal.ProposalID - proposal.Status = StatusVotingPeriod - input.keeper.SetProposal(ctx, proposal) - - err = input.keeper.AddVote(ctx, proposalID, input.addrs[0], OptionYes) - require.Nil(t, err) - err = input.keeper.AddVote(ctx, proposalID, input.addrs[1], OptionNo) - require.Nil(t, err) - err = input.keeper.AddVote(ctx, proposalID, input.addrs[2], OptionNo) - require.Nil(t, err) - - proposal, ok := input.keeper.GetProposal(ctx, proposalID) - require.True(t, ok) - passes, burnDeposits, tallyResults := tally(ctx, input.keeper, proposal) - - require.True(t, passes) - require.False(t, burnDeposits) - require.False(t, tallyResults.Equals(EmptyTallyResult())) -} diff --git a/gov/types/keys_test.go b/gov/types/keys_test.go index fd343f5de..871484bee 100644 --- a/gov/types/keys_test.go +++ b/gov/types/keys_test.go @@ -5,9 +5,9 @@ import ( "time" "github.com/stretchr/testify/require" + "github.com/tendermint/tendermint/crypto/secp256k1" hmTypes "github.com/maticnetwork/heimdall/types" - "github.com/tendermint/tendermint/crypto/secp256k1" ) var addr = hmTypes.BytesToHeimdallAddress(secp256k1.GenPrivKey().PubKey().Address()) @@ -38,34 +38,36 @@ func TestProposalKeys(t *testing.T) { func TestDepositKeys(t *testing.T) { + id := 1 + key := DepositsKey(2) proposalID := SplitProposalKey(key) require.Equal(t, int(proposalID), 2) - key = DepositKey(2, addr) + key = DepositKey(2, hmTypes.ValidatorID(id)) proposalID, depositorAddr := SplitKeyDeposit(key) require.Equal(t, int(proposalID), 2) require.Equal(t, addr, depositorAddr) // invalid key - addr2 := hmTypes.SampleHeimdallAddress("test1") - key = DepositKey(5, addr2) + key = DepositKey(5, hmTypes.ValidatorID(id)) require.Panics(t, func() { SplitKeyDeposit(key) }) } func TestVoteKeys(t *testing.T) { + id := 1 + key := VotesKey(2) proposalID := SplitProposalKey(key) require.Equal(t, int(proposalID), 2) - key = VoteKey(2, addr) + key = VoteKey(2, hmTypes.ValidatorID(id)) proposalID, voterAddr := SplitKeyDeposit(key) require.Equal(t, int(proposalID), 2) require.Equal(t, addr, voterAddr) // invalid key - addr2 := hmTypes.SampleHeimdallAddress("test1") - key = VoteKey(5, addr2) + key = VoteKey(5, hmTypes.ValidatorID(id)) require.Panics(t, func() { SplitKeyVote(key) }) } diff --git a/gov/types/msgs_test.go b/gov/types/msgs_test.go deleted file mode 100644 index bb2fc79db..000000000 --- a/gov/types/msgs_test.go +++ /dev/null @@ -1,121 +0,0 @@ -package types - -import ( - "strings" - "testing" - - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/stretchr/testify/require" - - authTypes "github.com/maticnetwork/heimdall/auth/types" - hmTypes "github.com/maticnetwork/heimdall/types" -) - -var ( - coinsPos = sdk.NewCoins(hmTypes.NewInt64Coin(authTypes.FeeToken, 1000)) - coinsZero = sdk.NewCoins() - coinsPosNotMatic = sdk.NewCoins(hmTypes.NewInt64Coin("foo", 10000)) - coinsMulti = sdk.NewCoins(hmTypes.NewInt64Coin(authTypes.FeeToken, 1000), hmTypes.NewInt64Coin("foo", 10000)) - addrs = []hmTypes.HeimdallAddress{ - hmTypes.SampleHeimdallAddress("test1"), - hmTypes.SampleHeimdallAddress("test2"), - } -) - -func init() { - coinsMulti.Sort() -} - -// test ValidateBasic for MsgCreateValidator -func TestMsgSubmitProposal(t *testing.T) { - tests := []struct { - title, description string - proposalType string - proposerAddr hmTypes.HeimdallAddress - initialDeposit sdk.Coins - expectPass bool - }{ - {"Test Proposal", "the purpose of this proposal is to test", ProposalTypeText, addrs[0], coinsPos, true}, - {"", "the purpose of this proposal is to test", ProposalTypeText, addrs[0], coinsPos, false}, - {"Test Proposal", "", ProposalTypeText, addrs[0], coinsPos, false}, - {"Test Proposal", "the purpose of this proposal is to test", ProposalTypeSoftwareUpgrade, addrs[0], coinsPos, false}, - {"Test Proposal", "the purpose of this proposal is to test", ProposalTypeText, hmTypes.HeimdallAddress{}, coinsPos, false}, - {"Test Proposal", "the purpose of this proposal is to test", ProposalTypeText, addrs[0], coinsZero, true}, - {"Test Proposal", "the purpose of this proposal is to test", ProposalTypeText, addrs[0], coinsMulti, true}, - {strings.Repeat("#", MaxTitleLength*2), "the purpose of this proposal is to test", ProposalTypeText, addrs[0], coinsMulti, false}, - {"Test Proposal", strings.Repeat("#", MaxDescriptionLength*2), ProposalTypeText, addrs[0], coinsMulti, false}, - } - - for i, tc := range tests { - msg := NewMsgSubmitProposal( - ContentFromProposalType(tc.title, tc.description, tc.proposalType), - tc.initialDeposit, - tc.proposerAddr, - ) - - if tc.expectPass { - require.NoError(t, msg.ValidateBasic(), "test: %v", i) - } else { - require.Error(t, msg.ValidateBasic(), "test: %v", i) - } - } -} - -func TestMsgDepositGetSignBytes(t *testing.T) { - addr := hmTypes.SampleHeimdallAddress("addr1") - msg := NewMsgDeposit(addr, 0, coinsPos) - res := msg.GetSignBytes() - - expected := `{"type":"heimdall/MsgDeposit","value":{"amount":[{"amount":"1000","denom":"matic"}],"depositor":"0x0000000000000000000000000000006164647231","proposal_id":"0"}}` - require.Equal(t, expected, string(res)) -} - -// test ValidateBasic for MsgDeposit -func TestMsgDeposit(t *testing.T) { - tests := []struct { - proposalID uint64 - depositorAddr hmTypes.HeimdallAddress - depositAmount sdk.Coins - expectPass bool - }{ - {0, addrs[0], coinsPos, true}, - {1, hmTypes.HeimdallAddress{}, coinsPos, false}, - {1, addrs[0], coinsZero, true}, - {1, addrs[0], coinsMulti, true}, - } - - for i, tc := range tests { - msg := NewMsgDeposit(tc.depositorAddr, tc.proposalID, tc.depositAmount) - if tc.expectPass { - require.NoError(t, msg.ValidateBasic(), "test: %v", i) - } else { - require.Error(t, msg.ValidateBasic(), "test: %v", i) - } - } -} - -// test ValidateBasic for MsgDeposit -func TestMsgVote(t *testing.T) { - tests := []struct { - proposalID uint64 - voterAddr hmTypes.HeimdallAddress - option VoteOption - expectPass bool - }{ - {0, addrs[0], OptionYes, true}, - {0, hmTypes.HeimdallAddress{}, OptionYes, false}, - {0, addrs[0], OptionNo, true}, - {0, addrs[0], OptionNoWithVeto, true}, - {0, addrs[0], OptionAbstain, true}, - {0, addrs[0], VoteOption(0x13), false}, - } - - for i, tc := range tests { - msg := NewMsgVote(tc.voterAddr, tc.proposalID, tc.option) - if tc.expectPass { - require.Nil(t, msg.ValidateBasic(), "test: %v", i) - } else { - require.NotNil(t, msg.ValidateBasic(), "test: %v", i) - } - } -}