cosmos-sdk/x/gov/keeper/querier_test.go

398 lines
14 KiB
Go
Raw Normal View History

2020-03-02 07:22:36 -08:00
package keeper_test
import (
"math/rand"
"strings"
"testing"
"time"
"github.com/stretchr/testify/require"
2020-03-02 12:50:20 -08:00
abci "github.com/tendermint/tendermint/abci/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/codec"
2020-03-02 07:22:36 -08:00
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
2020-03-02 07:22:36 -08:00
"github.com/cosmos/cosmos-sdk/x/gov/keeper"
"github.com/cosmos/cosmos-sdk/x/gov/types"
)
const custom = "custom"
func getQueriedParams(t *testing.T, ctx sdk.Context, cdc *codec.LegacyAmino, querier sdk.Querier) (types.DepositParams, types.VotingParams, types.TallyParams) {
query := abci.RequestQuery{
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryParams, types.ParamDeposit}, "/"),
Data: []byte{},
}
bz, err := querier(ctx, []string{types.QueryParams, types.ParamDeposit}, query)
require.NoError(t, err)
require.NotNil(t, bz)
var depositParams types.DepositParams
require.NoError(t, cdc.UnmarshalJSON(bz, &depositParams))
query = abci.RequestQuery{
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryParams, types.ParamVoting}, "/"),
Data: []byte{},
}
bz, err = querier(ctx, []string{types.QueryParams, types.ParamVoting}, query)
require.NoError(t, err)
require.NotNil(t, bz)
var votingParams types.VotingParams
require.NoError(t, cdc.UnmarshalJSON(bz, &votingParams))
query = abci.RequestQuery{
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryParams, types.ParamTallying}, "/"),
Data: []byte{},
}
bz, err = querier(ctx, []string{types.QueryParams, types.ParamTallying}, query)
require.NoError(t, err)
require.NotNil(t, bz)
var tallyParams types.TallyParams
require.NoError(t, cdc.UnmarshalJSON(bz, &tallyParams))
return depositParams, votingParams, tallyParams
}
func getQueriedProposals(
t *testing.T, ctx sdk.Context, cdc *codec.LegacyAmino, querier sdk.Querier,
depositor, voter sdk.AccAddress, status types.ProposalStatus, page, limit int,
) []types.Proposal {
query := abci.RequestQuery{
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryProposals}, "/"),
Data: cdc.MustMarshalJSON(types.NewQueryProposalsParams(page, limit, status, voter, depositor)),
}
bz, err := querier(ctx, []string{types.QueryProposals}, query)
require.NoError(t, err)
require.NotNil(t, bz)
var proposals types.Proposals
require.NoError(t, cdc.UnmarshalJSON(bz, &proposals))
return proposals
}
func getQueriedDeposit(t *testing.T, ctx sdk.Context, cdc *codec.LegacyAmino, querier sdk.Querier, proposalID uint64, depositor sdk.AccAddress) types.Deposit {
query := abci.RequestQuery{
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryDeposit}, "/"),
Data: cdc.MustMarshalJSON(types.NewQueryDepositParams(proposalID, depositor)),
}
bz, err := querier(ctx, []string{types.QueryDeposit}, query)
require.NoError(t, err)
require.NotNil(t, bz)
var deposit types.Deposit
require.NoError(t, cdc.UnmarshalJSON(bz, &deposit))
return deposit
}
func getQueriedDeposits(t *testing.T, ctx sdk.Context, cdc *codec.LegacyAmino, querier sdk.Querier, proposalID uint64) []types.Deposit {
query := abci.RequestQuery{
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryDeposits}, "/"),
Data: cdc.MustMarshalJSON(types.NewQueryProposalParams(proposalID)),
}
bz, err := querier(ctx, []string{types.QueryDeposits}, query)
require.NoError(t, err)
require.NotNil(t, bz)
var deposits []types.Deposit
require.NoError(t, cdc.UnmarshalJSON(bz, &deposits))
return deposits
}
func getQueriedVote(t *testing.T, ctx sdk.Context, cdc *codec.LegacyAmino, querier sdk.Querier, proposalID uint64, voter sdk.AccAddress) types.Vote {
query := abci.RequestQuery{
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryVote}, "/"),
Data: cdc.MustMarshalJSON(types.NewQueryVoteParams(proposalID, voter)),
}
bz, err := querier(ctx, []string{types.QueryVote}, query)
require.NoError(t, err)
require.NotNil(t, bz)
var vote types.Vote
require.NoError(t, cdc.UnmarshalJSON(bz, &vote))
return vote
}
func getQueriedVotes(t *testing.T, ctx sdk.Context, cdc *codec.LegacyAmino, querier sdk.Querier,
proposalID uint64, page, limit int) []types.Vote {
query := abci.RequestQuery{
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryVote}, "/"),
Data: cdc.MustMarshalJSON(types.NewQueryProposalVotesParams(proposalID, page, limit)),
}
bz, err := querier(ctx, []string{types.QueryVotes}, query)
require.NoError(t, err)
require.NotNil(t, bz)
var votes []types.Vote
require.NoError(t, cdc.UnmarshalJSON(bz, &votes))
return votes
}
func TestQueries(t *testing.T) {
2020-03-02 07:22:36 -08:00
app := simapp.Setup(false)
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
legacyQuerierCdc := app.LegacyAmino()
querier := keeper.NewQuerier(app.GovKeeper, legacyQuerierCdc)
2020-03-02 07:22:36 -08:00
TestAddrs := simapp.AddTestAddrsIncremental(app, ctx, 2, sdk.NewInt(20000001))
oneCoins := sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, 1))
consCoins := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, app.StakingKeeper.TokensFromConsensusPower(ctx, 10)))
tp := TestProposal
depositParams, _, _ := getQueriedParams(t, ctx, legacyQuerierCdc, querier)
// TestAddrs[0] proposes (and deposits) proposals #1 and #2
2020-03-02 07:22:36 -08:00
proposal1, err := app.GovKeeper.SubmitProposal(ctx, tp)
require.NoError(t, err)
deposit1 := types.NewDeposit(proposal1.ProposalId, TestAddrs[0], oneCoins)
Change `address` from bytes to bech32 strings (#7242) * init * Fix bank proto messages * missing conversions * remove casttype for addresses * Fix tests * Fix consaddress * more test fixes * Fix tests * fixed tests * migrate missing proto declarations * format * Fix format * Fix alignment * Fix more tests * Fix ibc merge issue * Fix fmt * Fix more tests * Fix missing address declarations * Fix staking tests * Fix more tests * Fix config * fixed tests * Fix more tests * Update staking grpc tests * Fix merge issue * fixed failing tests in x/distr * fixed sim tests * fixed failing tests * Fix bugs * Add logs * fixed slashing issue * Fix staking grpc tests * Fix all bank tests :) * Fix tests in distribution * Fix more tests in distr * Fix slashing tests * Fix statking tests * Fix evidence tests * Fix gov tests * Fix bug in create vesting account * Fix test * remove fmt * fixed gov tests * fixed x/ibc tests * fixed x/ibc-transfer tests * fixed staking tests * fixed staking tests * fixed test * fixed distribution issue * fix pagination test * fmt * lint * fix build * fix format * revert tally tests * revert tally tests * lint * Fix sim test * revert * revert * fixed tally issue * fix tests * revert * fmt * refactor * remove `GetAddress()` * remove fmt * revert fmt.Striger usage * Fix tests * Fix rest test * disable interfacer lint check * make proto-format * add nolint rule * remove stray println Co-authored-by: aleem1314 <aleem.md789@gmail.com> Co-authored-by: atheesh <atheesh@vitwit.com>
2020-09-25 03:25:37 -07:00
depositer1, err := sdk.AccAddressFromBech32(deposit1.Depositor)
require.NoError(t, err)
_, err = app.GovKeeper.AddDeposit(ctx, deposit1.ProposalId, depositer1, deposit1.Amount)
require.NoError(t, err)
proposal1.TotalDeposit = proposal1.TotalDeposit.Add(deposit1.Amount...)
2020-03-02 07:22:36 -08:00
proposal2, err := app.GovKeeper.SubmitProposal(ctx, tp)
require.NoError(t, err)
deposit2 := types.NewDeposit(proposal2.ProposalId, TestAddrs[0], consCoins)
Change `address` from bytes to bech32 strings (#7242) * init * Fix bank proto messages * missing conversions * remove casttype for addresses * Fix tests * Fix consaddress * more test fixes * Fix tests * fixed tests * migrate missing proto declarations * format * Fix format * Fix alignment * Fix more tests * Fix ibc merge issue * Fix fmt * Fix more tests * Fix missing address declarations * Fix staking tests * Fix more tests * Fix config * fixed tests * Fix more tests * Update staking grpc tests * Fix merge issue * fixed failing tests in x/distr * fixed sim tests * fixed failing tests * Fix bugs * Add logs * fixed slashing issue * Fix staking grpc tests * Fix all bank tests :) * Fix tests in distribution * Fix more tests in distr * Fix slashing tests * Fix statking tests * Fix evidence tests * Fix gov tests * Fix bug in create vesting account * Fix test * remove fmt * fixed gov tests * fixed x/ibc tests * fixed x/ibc-transfer tests * fixed staking tests * fixed staking tests * fixed test * fixed distribution issue * fix pagination test * fmt * lint * fix build * fix format * revert tally tests * revert tally tests * lint * Fix sim test * revert * revert * fixed tally issue * fix tests * revert * fmt * refactor * remove `GetAddress()` * remove fmt * revert fmt.Striger usage * Fix tests * Fix rest test * disable interfacer lint check * make proto-format * add nolint rule * remove stray println Co-authored-by: aleem1314 <aleem.md789@gmail.com> Co-authored-by: atheesh <atheesh@vitwit.com>
2020-09-25 03:25:37 -07:00
depositer2, err := sdk.AccAddressFromBech32(deposit2.Depositor)
require.NoError(t, err)
_, err = app.GovKeeper.AddDeposit(ctx, deposit2.ProposalId, depositer2, deposit2.Amount)
require.NoError(t, err)
proposal2.TotalDeposit = proposal2.TotalDeposit.Add(deposit2.Amount...)
// TestAddrs[1] proposes (and deposits) on proposal #3
2020-03-02 07:22:36 -08:00
proposal3, err := app.GovKeeper.SubmitProposal(ctx, tp)
require.NoError(t, err)
deposit3 := types.NewDeposit(proposal3.ProposalId, TestAddrs[1], oneCoins)
Change `address` from bytes to bech32 strings (#7242) * init * Fix bank proto messages * missing conversions * remove casttype for addresses * Fix tests * Fix consaddress * more test fixes * Fix tests * fixed tests * migrate missing proto declarations * format * Fix format * Fix alignment * Fix more tests * Fix ibc merge issue * Fix fmt * Fix more tests * Fix missing address declarations * Fix staking tests * Fix more tests * Fix config * fixed tests * Fix more tests * Update staking grpc tests * Fix merge issue * fixed failing tests in x/distr * fixed sim tests * fixed failing tests * Fix bugs * Add logs * fixed slashing issue * Fix staking grpc tests * Fix all bank tests :) * Fix tests in distribution * Fix more tests in distr * Fix slashing tests * Fix statking tests * Fix evidence tests * Fix gov tests * Fix bug in create vesting account * Fix test * remove fmt * fixed gov tests * fixed x/ibc tests * fixed x/ibc-transfer tests * fixed staking tests * fixed staking tests * fixed test * fixed distribution issue * fix pagination test * fmt * lint * fix build * fix format * revert tally tests * revert tally tests * lint * Fix sim test * revert * revert * fixed tally issue * fix tests * revert * fmt * refactor * remove `GetAddress()` * remove fmt * revert fmt.Striger usage * Fix tests * Fix rest test * disable interfacer lint check * make proto-format * add nolint rule * remove stray println Co-authored-by: aleem1314 <aleem.md789@gmail.com> Co-authored-by: atheesh <atheesh@vitwit.com>
2020-09-25 03:25:37 -07:00
depositer3, err := sdk.AccAddressFromBech32(deposit3.Depositor)
require.NoError(t, err)
_, err = app.GovKeeper.AddDeposit(ctx, deposit3.ProposalId, depositer3, deposit3.Amount)
require.NoError(t, err)
proposal3.TotalDeposit = proposal3.TotalDeposit.Add(deposit3.Amount...)
// TestAddrs[1] deposits on proposals #2 & #3
deposit4 := types.NewDeposit(proposal2.ProposalId, TestAddrs[1], depositParams.MinDeposit)
Change `address` from bytes to bech32 strings (#7242) * init * Fix bank proto messages * missing conversions * remove casttype for addresses * Fix tests * Fix consaddress * more test fixes * Fix tests * fixed tests * migrate missing proto declarations * format * Fix format * Fix alignment * Fix more tests * Fix ibc merge issue * Fix fmt * Fix more tests * Fix missing address declarations * Fix staking tests * Fix more tests * Fix config * fixed tests * Fix more tests * Update staking grpc tests * Fix merge issue * fixed failing tests in x/distr * fixed sim tests * fixed failing tests * Fix bugs * Add logs * fixed slashing issue * Fix staking grpc tests * Fix all bank tests :) * Fix tests in distribution * Fix more tests in distr * Fix slashing tests * Fix statking tests * Fix evidence tests * Fix gov tests * Fix bug in create vesting account * Fix test * remove fmt * fixed gov tests * fixed x/ibc tests * fixed x/ibc-transfer tests * fixed staking tests * fixed staking tests * fixed test * fixed distribution issue * fix pagination test * fmt * lint * fix build * fix format * revert tally tests * revert tally tests * lint * Fix sim test * revert * revert * fixed tally issue * fix tests * revert * fmt * refactor * remove `GetAddress()` * remove fmt * revert fmt.Striger usage * Fix tests * Fix rest test * disable interfacer lint check * make proto-format * add nolint rule * remove stray println Co-authored-by: aleem1314 <aleem.md789@gmail.com> Co-authored-by: atheesh <atheesh@vitwit.com>
2020-09-25 03:25:37 -07:00
depositer4, err := sdk.AccAddressFromBech32(deposit4.Depositor)
require.NoError(t, err)
_, err = app.GovKeeper.AddDeposit(ctx, deposit4.ProposalId, depositer4, deposit4.Amount)
require.NoError(t, err)
proposal2.TotalDeposit = proposal2.TotalDeposit.Add(deposit4.Amount...)
proposal2.Status = types.StatusVotingPeriod
proposal2.VotingEndTime = proposal2.VotingEndTime.Add(types.DefaultPeriod)
deposit5 := types.NewDeposit(proposal3.ProposalId, TestAddrs[1], depositParams.MinDeposit)
Change `address` from bytes to bech32 strings (#7242) * init * Fix bank proto messages * missing conversions * remove casttype for addresses * Fix tests * Fix consaddress * more test fixes * Fix tests * fixed tests * migrate missing proto declarations * format * Fix format * Fix alignment * Fix more tests * Fix ibc merge issue * Fix fmt * Fix more tests * Fix missing address declarations * Fix staking tests * Fix more tests * Fix config * fixed tests * Fix more tests * Update staking grpc tests * Fix merge issue * fixed failing tests in x/distr * fixed sim tests * fixed failing tests * Fix bugs * Add logs * fixed slashing issue * Fix staking grpc tests * Fix all bank tests :) * Fix tests in distribution * Fix more tests in distr * Fix slashing tests * Fix statking tests * Fix evidence tests * Fix gov tests * Fix bug in create vesting account * Fix test * remove fmt * fixed gov tests * fixed x/ibc tests * fixed x/ibc-transfer tests * fixed staking tests * fixed staking tests * fixed test * fixed distribution issue * fix pagination test * fmt * lint * fix build * fix format * revert tally tests * revert tally tests * lint * Fix sim test * revert * revert * fixed tally issue * fix tests * revert * fmt * refactor * remove `GetAddress()` * remove fmt * revert fmt.Striger usage * Fix tests * Fix rest test * disable interfacer lint check * make proto-format * add nolint rule * remove stray println Co-authored-by: aleem1314 <aleem.md789@gmail.com> Co-authored-by: atheesh <atheesh@vitwit.com>
2020-09-25 03:25:37 -07:00
depositer5, err := sdk.AccAddressFromBech32(deposit5.Depositor)
require.NoError(t, err)
_, err = app.GovKeeper.AddDeposit(ctx, deposit5.ProposalId, depositer5, deposit5.Amount)
require.NoError(t, err)
proposal3.TotalDeposit = proposal3.TotalDeposit.Add(deposit5.Amount...)
proposal3.Status = types.StatusVotingPeriod
proposal3.VotingEndTime = proposal3.VotingEndTime.Add(types.DefaultPeriod)
// total deposit of TestAddrs[1] on proposal #3 is worth the proposal deposit + individual deposit
deposit5.Amount = deposit5.Amount.Add(deposit3.Amount...)
// check deposits on proposal1 match individual deposits
deposits := getQueriedDeposits(t, ctx, legacyQuerierCdc, querier, proposal1.ProposalId)
require.Len(t, deposits, 1)
require.Equal(t, deposit1, deposits[0])
deposit := getQueriedDeposit(t, ctx, legacyQuerierCdc, querier, proposal1.ProposalId, TestAddrs[0])
require.Equal(t, deposit1, deposit)
// check deposits on proposal2 match individual deposits
deposits = getQueriedDeposits(t, ctx, legacyQuerierCdc, querier, proposal2.ProposalId)
require.Len(t, deposits, 2)
// NOTE order of deposits is determined by the addresses
require.Equal(t, deposit2, deposits[0])
require.Equal(t, deposit4, deposits[1])
// check deposits on proposal3 match individual deposits
deposits = getQueriedDeposits(t, ctx, legacyQuerierCdc, querier, proposal3.ProposalId)
require.Len(t, deposits, 1)
require.Equal(t, deposit5, deposits[0])
deposit = getQueriedDeposit(t, ctx, legacyQuerierCdc, querier, proposal3.ProposalId, TestAddrs[1])
require.Equal(t, deposit5, deposit)
// Only proposal #1 should be in types.Deposit Period
proposals := getQueriedProposals(t, ctx, legacyQuerierCdc, querier, nil, nil, types.StatusDepositPeriod, 1, 0)
require.Len(t, proposals, 1)
require.Equal(t, proposal1, proposals[0])
// Only proposals #2 and #3 should be in Voting Period
proposals = getQueriedProposals(t, ctx, legacyQuerierCdc, querier, nil, nil, types.StatusVotingPeriod, 1, 0)
require.Len(t, proposals, 2)
require.Equal(t, proposal2, proposals[0])
require.Equal(t, proposal3, proposals[1])
// Addrs[0] votes on proposals #2 & #3
vote1 := types.NewVote(proposal2.ProposalId, TestAddrs[0], types.NewNonSplitVoteOption(types.OptionYes))
vote2 := types.NewVote(proposal3.ProposalId, TestAddrs[0], types.NewNonSplitVoteOption(types.OptionYes))
2020-03-02 07:22:36 -08:00
app.GovKeeper.SetVote(ctx, vote1)
app.GovKeeper.SetVote(ctx, vote2)
// Addrs[1] votes on proposal #3
vote3 := types.NewVote(proposal3.ProposalId, TestAddrs[1], types.NewNonSplitVoteOption(types.OptionYes))
2020-03-02 07:22:36 -08:00
app.GovKeeper.SetVote(ctx, vote3)
// Test query voted by TestAddrs[0]
proposals = getQueriedProposals(t, ctx, legacyQuerierCdc, querier, nil, TestAddrs[0], types.StatusNil, 1, 0)
require.Equal(t, proposal2, proposals[0])
require.Equal(t, proposal3, proposals[1])
// Test query votes on types.Proposal 2
votes := getQueriedVotes(t, ctx, legacyQuerierCdc, querier, proposal2.ProposalId, 1, 0)
require.Len(t, votes, 1)
refactor: Bring back deprecated proto fields to `v1beta1` (#9534) <!-- The default pull request template is for types feat, fix, or refactor. For other templates, add one of the following parameters to the url: - template=docs.md - template=other.md --> ## Description Closes: #9446 <!-- Add a description of the changes that this PR introduces and the files that are the most critical to review. --> --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [x] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [x] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
2021-06-23 06:03:33 -07:00
checkEqualVotes(t, vote1, votes[0])
vote := getQueriedVote(t, ctx, legacyQuerierCdc, querier, proposal2.ProposalId, TestAddrs[0])
refactor: Bring back deprecated proto fields to `v1beta1` (#9534) <!-- The default pull request template is for types feat, fix, or refactor. For other templates, add one of the following parameters to the url: - template=docs.md - template=other.md --> ## Description Closes: #9446 <!-- Add a description of the changes that this PR introduces and the files that are the most critical to review. --> --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [x] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [x] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
2021-06-23 06:03:33 -07:00
checkEqualVotes(t, vote1, vote)
// Test query votes on types.Proposal 3
votes = getQueriedVotes(t, ctx, legacyQuerierCdc, querier, proposal3.ProposalId, 1, 0)
require.Len(t, votes, 2)
refactor: Bring back deprecated proto fields to `v1beta1` (#9534) <!-- The default pull request template is for types feat, fix, or refactor. For other templates, add one of the following parameters to the url: - template=docs.md - template=other.md --> ## Description Closes: #9446 <!-- Add a description of the changes that this PR introduces and the files that are the most critical to review. --> --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [x] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [x] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
2021-06-23 06:03:33 -07:00
checkEqualVotes(t, vote2, votes[0])
checkEqualVotes(t, vote3, votes[1])
// Test query all proposals
proposals = getQueriedProposals(t, ctx, legacyQuerierCdc, querier, nil, nil, types.StatusNil, 1, 0)
require.Equal(t, proposal1, proposals[0])
require.Equal(t, proposal2, proposals[1])
require.Equal(t, proposal3, proposals[2])
// Test query voted by TestAddrs[1]
proposals = getQueriedProposals(t, ctx, legacyQuerierCdc, querier, nil, TestAddrs[1], types.StatusNil, 1, 0)
require.Equal(t, proposal3.ProposalId, proposals[0].ProposalId)
// Test query deposited by TestAddrs[0]
proposals = getQueriedProposals(t, ctx, legacyQuerierCdc, querier, TestAddrs[0], nil, types.StatusNil, 1, 0)
require.Equal(t, proposal1.ProposalId, proposals[0].ProposalId)
// Test query deposited by addr2
proposals = getQueriedProposals(t, ctx, legacyQuerierCdc, querier, TestAddrs[1], nil, types.StatusNil, 1, 0)
require.Equal(t, proposal2.ProposalId, proposals[0].ProposalId)
require.Equal(t, proposal3.ProposalId, proposals[1].ProposalId)
// Test query voted AND deposited by addr1
proposals = getQueriedProposals(t, ctx, legacyQuerierCdc, querier, TestAddrs[0], TestAddrs[0], types.StatusNil, 1, 0)
require.Equal(t, proposal2.ProposalId, proposals[0].ProposalId)
}
func TestPaginatedVotesQuery(t *testing.T) {
2020-03-02 07:22:36 -08:00
app := simapp.Setup(false)
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
legacyQuerierCdc := app.LegacyAmino()
proposal := types.Proposal{
ProposalId: 100,
Status: types.StatusVotingPeriod,
}
2020-03-02 12:50:20 -08:00
2020-03-02 07:22:36 -08:00
app.GovKeeper.SetProposal(ctx, proposal)
votes := make([]types.Vote, 20)
random := rand.New(rand.NewSource(time.Now().UnixNano()))
addrMap := make(map[string]struct{})
genAddr := func() string {
addr := make(sdk.AccAddress, 20)
for {
random.Read(addr)
addrStr := addr.String()
if _, ok := addrMap[addrStr]; !ok {
addrMap[addrStr] = struct{}{}
return addrStr
}
}
}
for i := range votes {
vote := types.Vote{
ProposalId: proposal.ProposalId,
Voter: genAddr(),
Options: types.NewNonSplitVoteOption(types.OptionYes),
}
votes[i] = vote
2020-03-02 07:22:36 -08:00
app.GovKeeper.SetVote(ctx, vote)
}
querier := keeper.NewQuerier(app.GovKeeper, legacyQuerierCdc)
// keeper preserves consistent order for each query, but this is not the insertion order
all := getQueriedVotes(t, ctx, legacyQuerierCdc, querier, proposal.ProposalId, 1, 0)
require.Equal(t, len(all), len(votes))
type testCase struct {
description string
page int
limit int
votes []types.Vote
}
for _, tc := range []testCase{
{
description: "SkipAll",
page: 2,
limit: len(all),
},
{
description: "GetFirstChunk",
page: 1,
limit: 10,
votes: all[:10],
},
{
description: "GetSecondsChunk",
page: 2,
limit: 10,
votes: all[10:],
},
{
description: "InvalidPage",
page: -1,
},
} {
tc := tc
t.Run(tc.description, func(t *testing.T) {
votes := getQueriedVotes(t, ctx, legacyQuerierCdc, querier, proposal.ProposalId, tc.page, tc.limit)
require.Equal(t, len(tc.votes), len(votes))
for i := range votes {
require.Equal(t, tc.votes[i], votes[i])
}
})
}
}
refactor: Bring back deprecated proto fields to `v1beta1` (#9534) <!-- The default pull request template is for types feat, fix, or refactor. For other templates, add one of the following parameters to the url: - template=docs.md - template=other.md --> ## Description Closes: #9446 <!-- Add a description of the changes that this PR introduces and the files that are the most critical to review. --> --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [x] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [x] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
2021-06-23 06:03:33 -07:00
// checkEqualVotes checks that two votes are equal, without taking into account
// graceful fallback for `Option`.
// When querying, the keeper populates the `vote.Option` field when there's
// only 1 vote, this function checks equality of structs while skipping that
// field.
func checkEqualVotes(t *testing.T, vote1, vote2 types.Vote) {
require.Equal(t, vote1.Options, vote2.Options)
require.Equal(t, vote1.Voter, vote2.Voter)
require.Equal(t, vote1.ProposalId, vote2.ProposalId)
}