2020-03-13 15:11:31 +00:00
|
|
|
package keeper_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2022-01-08 00:39:27 +00:00
|
|
|
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
|
2020-04-30 14:23:41 +00:00
|
|
|
|
2020-03-13 15:11:31 +00:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
2022-01-08 00:39:27 +00:00
|
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
2020-03-13 15:11:31 +00:00
|
|
|
|
|
|
|
"github.com/kava-labs/kava/app"
|
|
|
|
"github.com/kava-labs/kava/x/committee/keeper"
|
2022-01-08 00:39:27 +00:00
|
|
|
"github.com/kava-labs/kava/x/committee/testutil"
|
2020-03-13 15:11:31 +00:00
|
|
|
"github.com/kava-labs/kava/x/committee/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
custom = "custom"
|
|
|
|
)
|
|
|
|
|
|
|
|
type QuerierTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
keeper keeper.Keeper
|
|
|
|
app app.TestApp
|
|
|
|
ctx sdk.Context
|
|
|
|
cdc codec.Codec
|
|
|
|
legacyAmino *codec.LegacyAmino
|
2020-03-13 15:11:31 +00:00
|
|
|
|
|
|
|
querier sdk.Querier
|
|
|
|
|
2020-03-27 18:34:03 +00:00
|
|
|
addresses []sdk.AccAddress
|
2022-01-08 00:39:27 +00:00
|
|
|
testGenesis *types.GenesisState
|
2020-03-27 18:34:03 +00:00
|
|
|
votes map[uint64]([]types.Vote)
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *QuerierTestSuite) SetupTest() {
|
|
|
|
suite.app = app.NewTestApp()
|
|
|
|
suite.keeper = suite.app.GetCommitteeKeeper()
|
2022-01-08 00:39:27 +00:00
|
|
|
suite.ctx = suite.app.NewContext(true, tmproto.Header{})
|
|
|
|
suite.cdc = suite.app.AppCodec()
|
|
|
|
suite.legacyAmino = suite.app.LegacyAmino()
|
|
|
|
suite.querier = keeper.NewQuerier(suite.keeper, suite.legacyAmino)
|
2020-03-13 15:11:31 +00:00
|
|
|
|
|
|
|
_, suite.addresses = app.GeneratePrivKeyAddressPairs(5)
|
2022-01-08 00:39:27 +00:00
|
|
|
memberCommittee := mustNewTestMemberCommittee(suite.addresses[:3])
|
|
|
|
memberCommittee.ID = 1
|
|
|
|
noPermCommittee := mustNewTestMemberCommittee(suite.addresses[2:])
|
|
|
|
noPermCommittee.ID = 2
|
|
|
|
noPermCommittee.SetPermissions([]types.Permission{})
|
|
|
|
proposalOne := mustNewTestProposal()
|
|
|
|
proposalTwo := mustNewTestProposal()
|
|
|
|
proposalTwo.ID = 2
|
2020-03-27 18:34:03 +00:00
|
|
|
suite.testGenesis = types.NewGenesisState(
|
|
|
|
3,
|
2022-01-08 00:39:27 +00:00
|
|
|
[]types.Committee{memberCommittee, noPermCommittee},
|
|
|
|
types.Proposals{proposalOne, proposalTwo},
|
2020-03-27 18:34:03 +00:00
|
|
|
[]types.Vote{
|
2022-01-08 00:39:27 +00:00
|
|
|
{ProposalID: 1, Voter: suite.addresses[0], VoteType: types.VOTE_TYPE_YES},
|
|
|
|
{ProposalID: 1, Voter: suite.addresses[1], VoteType: types.VOTE_TYPE_YES},
|
|
|
|
{ProposalID: 2, Voter: suite.addresses[2], VoteType: types.VOTE_TYPE_YES},
|
2020-03-27 18:34:03 +00:00
|
|
|
},
|
|
|
|
)
|
2022-01-08 00:39:27 +00:00
|
|
|
genState := NewCommitteeGenesisState(suite.cdc, suite.testGenesis)
|
|
|
|
suite.app.InitializeFromGenesisStates(genState)
|
2020-03-27 18:34:03 +00:00
|
|
|
|
2020-04-24 22:15:51 +00:00
|
|
|
suite.votes = getProposalVoteMap(suite.keeper, suite.ctx)
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func (suite *QuerierTestSuite) assertQuerierResponse(expected interface{}, actual []byte) {
|
|
|
|
expectedJson, err := suite.legacyAmino.MarshalJSONIndent(expected, "", " ")
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().Equal(string(expectedJson), string(actual))
|
|
|
|
}
|
|
|
|
|
2020-03-13 15:11:31 +00:00
|
|
|
func (suite *QuerierTestSuite) TestQueryCommittees() {
|
|
|
|
ctx := suite.ctx.WithIsCheckTx(false)
|
|
|
|
// Set up request query
|
|
|
|
query := abci.RequestQuery{
|
|
|
|
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryCommittees}, "/"),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute query and check the []byte result
|
|
|
|
bz, err := suite.querier(ctx, []string{types.QueryCommittees}, query)
|
2022-01-08 00:39:27 +00:00
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().NotNil(bz)
|
|
|
|
suite.assertQuerierResponse(suite.testGenesis.GetCommittees(), bz)
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *QuerierTestSuite) TestQueryCommittee() {
|
2021-06-07 16:08:03 +00:00
|
|
|
ctx := suite.ctx.WithIsCheckTx(false)
|
2020-03-13 15:11:31 +00:00
|
|
|
// Set up request query
|
2022-01-08 00:39:27 +00:00
|
|
|
targetCommittee := suite.testGenesis.GetCommittees()[0]
|
2020-03-13 15:11:31 +00:00
|
|
|
query := abci.RequestQuery{
|
|
|
|
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryCommittee}, "/"),
|
2022-01-08 00:39:27 +00:00
|
|
|
Data: suite.legacyAmino.MustMarshalJSON(types.NewQueryCommitteeParams(targetCommittee.GetID())),
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Execute query and check the []byte result
|
|
|
|
bz, err := suite.querier(ctx, []string{types.QueryCommittee}, query)
|
|
|
|
suite.NoError(err)
|
|
|
|
suite.NotNil(bz)
|
2022-01-08 00:39:27 +00:00
|
|
|
suite.assertQuerierResponse(targetCommittee, bz)
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *QuerierTestSuite) TestQueryProposals() {
|
|
|
|
ctx := suite.ctx.WithIsCheckTx(false)
|
|
|
|
// Set up request query
|
2020-03-27 18:34:03 +00:00
|
|
|
comID := suite.testGenesis.Proposals[0].CommitteeID
|
2020-03-13 15:11:31 +00:00
|
|
|
query := abci.RequestQuery{
|
|
|
|
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryProposals}, "/"),
|
2022-01-08 00:39:27 +00:00
|
|
|
Data: suite.legacyAmino.MustMarshalJSON(types.NewQueryCommitteeParams(comID)),
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Execute query and check the []byte result
|
|
|
|
bz, err := suite.querier(ctx, []string{types.QueryProposals}, query)
|
|
|
|
suite.NoError(err)
|
|
|
|
suite.NotNil(bz)
|
|
|
|
|
|
|
|
// Check
|
2022-01-08 00:39:27 +00:00
|
|
|
expectedProposals := types.Proposals{}
|
2020-03-27 18:34:03 +00:00
|
|
|
for _, p := range suite.testGenesis.Proposals {
|
2020-03-13 15:11:31 +00:00
|
|
|
if p.CommitteeID == comID {
|
|
|
|
expectedProposals = append(expectedProposals, p)
|
|
|
|
}
|
|
|
|
}
|
2022-01-08 00:39:27 +00:00
|
|
|
suite.assertQuerierResponse(expectedProposals, bz)
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *QuerierTestSuite) TestQueryProposal() {
|
2021-06-07 16:08:03 +00:00
|
|
|
ctx := suite.ctx.WithIsCheckTx(false)
|
2020-03-13 15:11:31 +00:00
|
|
|
// Set up request query
|
|
|
|
query := abci.RequestQuery{
|
|
|
|
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryProposal}, "/"),
|
2022-01-08 00:39:27 +00:00
|
|
|
Data: suite.legacyAmino.MustMarshalJSON(types.NewQueryProposalParams(suite.testGenesis.Proposals[0].ID)),
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Execute query and check the []byte result
|
|
|
|
bz, err := suite.querier(ctx, []string{types.QueryProposal}, query)
|
|
|
|
suite.NoError(err)
|
|
|
|
suite.NotNil(bz)
|
2022-01-08 00:39:27 +00:00
|
|
|
suite.assertQuerierResponse(suite.testGenesis.Proposals[0], bz)
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
|
|
|
|
2020-06-29 23:39:35 +00:00
|
|
|
func (suite *QuerierTestSuite) TestQueryNextProposalID() {
|
|
|
|
bz, err := suite.querier(suite.ctx, []string{types.QueryNextProposalID}, abci.RequestQuery{})
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().NotNil(bz)
|
|
|
|
|
|
|
|
var nextProposalID uint64
|
2022-01-08 00:39:27 +00:00
|
|
|
suite.Require().NoError(suite.legacyAmino.UnmarshalJSON(bz, &nextProposalID))
|
2020-06-29 23:39:35 +00:00
|
|
|
|
|
|
|
expectedID, _ := suite.keeper.GetNextProposalID(suite.ctx)
|
|
|
|
suite.Require().Equal(expectedID, nextProposalID)
|
|
|
|
}
|
|
|
|
|
2020-03-13 15:11:31 +00:00
|
|
|
func (suite *QuerierTestSuite) TestQueryVotes() {
|
|
|
|
ctx := suite.ctx.WithIsCheckTx(false)
|
|
|
|
// Set up request query
|
2020-03-27 18:34:03 +00:00
|
|
|
propID := suite.testGenesis.Proposals[0].ID
|
2020-03-13 15:11:31 +00:00
|
|
|
query := abci.RequestQuery{
|
|
|
|
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryVotes}, "/"),
|
2022-01-08 00:39:27 +00:00
|
|
|
Data: suite.legacyAmino.MustMarshalJSON(types.NewQueryProposalParams(propID)),
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Execute query and check the []byte result
|
|
|
|
bz, err := suite.querier(ctx, []string{types.QueryVotes}, query)
|
|
|
|
suite.NoError(err)
|
|
|
|
suite.NotNil(bz)
|
2022-01-08 00:39:27 +00:00
|
|
|
suite.assertQuerierResponse(suite.votes[propID], bz)
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *QuerierTestSuite) TestQueryVote() {
|
2021-06-07 16:08:03 +00:00
|
|
|
ctx := suite.ctx.WithIsCheckTx(false)
|
2020-03-13 15:11:31 +00:00
|
|
|
// Set up request query
|
2020-03-27 18:34:03 +00:00
|
|
|
propID := suite.testGenesis.Proposals[0].ID
|
2020-03-13 15:11:31 +00:00
|
|
|
query := abci.RequestQuery{
|
|
|
|
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryVote}, "/"),
|
2022-01-08 00:39:27 +00:00
|
|
|
Data: suite.legacyAmino.MustMarshalJSON(types.NewQueryVoteParams(propID, suite.votes[propID][0].Voter)),
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Execute query and check the []byte result
|
|
|
|
bz, err := suite.querier(ctx, []string{types.QueryVote}, query)
|
|
|
|
suite.NoError(err)
|
|
|
|
suite.NotNil(bz)
|
2022-01-08 00:39:27 +00:00
|
|
|
suite.assertQuerierResponse(suite.votes[propID][0], bz)
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *QuerierTestSuite) TestQueryTally() {
|
2021-06-07 16:08:03 +00:00
|
|
|
ctx := suite.ctx.WithIsCheckTx(false)
|
|
|
|
|
|
|
|
// Expected result
|
2020-03-27 18:34:03 +00:00
|
|
|
propID := suite.testGenesis.Proposals[0].ID
|
2022-01-08 00:39:27 +00:00
|
|
|
expectedPollingStatus := types.QueryTallyResponse{
|
2021-06-07 16:08:03 +00:00
|
|
|
ProposalID: 1,
|
|
|
|
YesVotes: sdk.NewDec(int64(len(suite.votes[propID]))),
|
2021-10-12 15:51:01 +00:00
|
|
|
NoVotes: sdk.ZeroDec(),
|
2021-06-07 16:08:03 +00:00
|
|
|
CurrentVotes: sdk.NewDec(int64(len(suite.votes[propID]))),
|
2022-01-08 00:39:27 +00:00
|
|
|
PossibleVotes: testutil.D("3.0"),
|
|
|
|
VoteThreshold: testutil.D("0.667"),
|
|
|
|
Quorum: testutil.D("0"),
|
2021-06-07 16:08:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set up request query
|
2020-03-13 15:11:31 +00:00
|
|
|
query := abci.RequestQuery{
|
|
|
|
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryTally}, "/"),
|
2022-01-08 00:39:27 +00:00
|
|
|
Data: suite.legacyAmino.MustMarshalJSON(types.NewQueryProposalParams(propID)),
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Execute query and check the []byte result
|
|
|
|
bz, err := suite.querier(ctx, []string{types.QueryTally}, query)
|
|
|
|
suite.NoError(err)
|
|
|
|
suite.NotNil(bz)
|
2022-01-08 00:39:27 +00:00
|
|
|
suite.assertQuerierResponse(expectedPollingStatus, bz)
|
2020-03-13 15:11:31 +00:00
|
|
|
}
|
2020-06-23 13:17:08 +00:00
|
|
|
|
|
|
|
type TestSubParam struct {
|
|
|
|
Some string
|
|
|
|
Test sdk.Dec
|
|
|
|
Params []types.Vote
|
|
|
|
}
|
|
|
|
type TestParams struct {
|
|
|
|
TestKey TestSubParam
|
|
|
|
}
|
|
|
|
|
|
|
|
const paramKey = "TestKey"
|
|
|
|
|
2022-01-08 00:39:27 +00:00
|
|
|
func (p *TestParams) ParamSetPairs() paramstypes.ParamSetPairs {
|
|
|
|
return paramstypes.ParamSetPairs{
|
|
|
|
paramstypes.NewParamSetPair([]byte(paramKey), &p.TestKey, func(interface{}) error { return nil }),
|
2020-06-23 13:17:08 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-09 18:37:36 +00:00
|
|
|
|
2020-06-23 13:17:08 +00:00
|
|
|
func (suite *QuerierTestSuite) TestQueryRawParams() {
|
2021-06-07 16:08:03 +00:00
|
|
|
ctx := suite.ctx.WithIsCheckTx(false)
|
2020-06-23 13:17:08 +00:00
|
|
|
|
|
|
|
// Create a new param subspace to avoid adding dependency to another module. Set a test param value.
|
|
|
|
subspaceName := "test"
|
|
|
|
subspace := suite.app.GetParamsKeeper().Subspace(subspaceName)
|
2022-01-08 00:39:27 +00:00
|
|
|
subspace = subspace.WithKeyTable(paramstypes.NewKeyTable().RegisterParamSet(&TestParams{}))
|
2020-06-23 13:17:08 +00:00
|
|
|
|
|
|
|
paramValue := TestSubParam{
|
2021-06-07 16:08:03 +00:00
|
|
|
Some: "test",
|
2022-01-08 00:39:27 +00:00
|
|
|
Test: testutil.D("1000000000000.000000000000000001"),
|
2021-06-07 16:08:03 +00:00
|
|
|
Params: []types.Vote{
|
2022-01-08 00:39:27 +00:00
|
|
|
types.NewVote(1, suite.addresses[0], types.VOTE_TYPE_YES),
|
|
|
|
types.NewVote(12, suite.addresses[1], types.VOTE_TYPE_YES),
|
2021-06-07 16:08:03 +00:00
|
|
|
},
|
2020-06-23 13:17:08 +00:00
|
|
|
}
|
|
|
|
subspace.Set(ctx, []byte(paramKey), paramValue)
|
|
|
|
|
|
|
|
// Set up request query
|
|
|
|
query := abci.RequestQuery{
|
|
|
|
Path: strings.Join([]string{custom, types.QuerierRoute, types.QueryRawParams}, "/"),
|
2022-01-08 00:39:27 +00:00
|
|
|
Data: suite.legacyAmino.MustMarshalJSON(types.NewQueryRawParamsParams(subspaceName, paramKey)),
|
2020-06-23 13:17:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Execute query
|
|
|
|
bz, err := suite.querier(ctx, []string{types.QueryRawParams}, query)
|
|
|
|
suite.NoError(err)
|
|
|
|
suite.NotNil(bz)
|
|
|
|
|
|
|
|
// Unmarshal the bytes
|
|
|
|
var returnedParamValue []byte
|
2022-01-08 00:39:27 +00:00
|
|
|
suite.NoError(suite.legacyAmino.UnmarshalJSON(bz, &returnedParamValue))
|
2020-06-23 13:17:08 +00:00
|
|
|
|
|
|
|
// Check
|
2022-01-08 00:39:27 +00:00
|
|
|
suite.Equal(suite.legacyAmino.MustMarshalJSON(paramValue), returnedParamValue)
|
2020-06-23 13:17:08 +00:00
|
|
|
}
|
|
|
|
|
2020-03-13 15:11:31 +00:00
|
|
|
func TestQuerierTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(QuerierTestSuite))
|
|
|
|
}
|