2020-03-02 07:04:49 -08:00
|
|
|
package keeper_test
|
2019-08-08 12:51:18 -07:00
|
|
|
|
|
|
|
import (
|
2019-12-27 09:57:54 -08:00
|
|
|
"errors"
|
2020-08-11 07:22:30 -07:00
|
|
|
"fmt"
|
2019-08-08 12:51:18 -07:00
|
|
|
"strings"
|
2022-01-21 03:14:00 -08:00
|
|
|
"testing"
|
2019-08-08 12:51:18 -07:00
|
|
|
"time"
|
|
|
|
|
2022-01-21 03:14:00 -08:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/testutil/testdata"
|
2019-08-08 12:51:18 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/gov/types"
|
2022-03-11 07:58:01 -08:00
|
|
|
v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
|
2021-12-13 10:48:44 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
|
2019-08-08 12:51:18 -07:00
|
|
|
)
|
|
|
|
|
2021-04-30 01:41:25 -07:00
|
|
|
func (suite *KeeperTestSuite) TestGetSetProposal() {
|
2019-08-08 12:51:18 -07:00
|
|
|
tp := TestProposal
|
2022-03-01 00:35:37 -08:00
|
|
|
proposal, err := suite.app.GovKeeper.SubmitProposal(suite.ctx, tp, "")
|
2021-04-30 01:41:25 -07:00
|
|
|
suite.Require().NoError(err)
|
2022-02-14 05:48:17 -08:00
|
|
|
proposalID := proposal.Id
|
2021-04-30 01:41:25 -07:00
|
|
|
suite.app.GovKeeper.SetProposal(suite.ctx, proposal)
|
2019-08-08 12:51:18 -07:00
|
|
|
|
2021-04-30 01:41:25 -07:00
|
|
|
gotProposal, ok := suite.app.GovKeeper.GetProposal(suite.ctx, proposalID)
|
|
|
|
suite.Require().True(ok)
|
2022-01-21 03:14:00 -08:00
|
|
|
suite.Require().Equal(proposal, gotProposal)
|
2019-08-08 12:51:18 -07:00
|
|
|
}
|
|
|
|
|
2021-04-30 01:41:25 -07:00
|
|
|
func (suite *KeeperTestSuite) TestActivateVotingPeriod() {
|
2019-08-08 12:51:18 -07:00
|
|
|
tp := TestProposal
|
2022-03-01 00:35:37 -08:00
|
|
|
proposal, err := suite.app.GovKeeper.SubmitProposal(suite.ctx, tp, "")
|
2021-04-30 01:41:25 -07:00
|
|
|
suite.Require().NoError(err)
|
2019-08-08 12:51:18 -07:00
|
|
|
|
2022-01-21 03:14:00 -08:00
|
|
|
suite.Require().Nil(proposal.VotingStartTime)
|
2019-08-08 12:51:18 -07:00
|
|
|
|
2021-04-30 01:41:25 -07:00
|
|
|
suite.app.GovKeeper.ActivateVotingPeriod(suite.ctx, proposal)
|
2019-08-08 12:51:18 -07:00
|
|
|
|
2022-02-14 05:48:17 -08:00
|
|
|
proposal, ok := suite.app.GovKeeper.GetProposal(suite.ctx, proposal.Id)
|
2021-04-30 01:41:25 -07:00
|
|
|
suite.Require().True(ok)
|
2022-01-21 03:14:00 -08:00
|
|
|
suite.Require().True(proposal.VotingStartTime.Equal(suite.ctx.BlockHeader().Time))
|
2019-08-08 12:51:18 -07:00
|
|
|
|
2022-01-21 03:14:00 -08:00
|
|
|
activeIterator := suite.app.GovKeeper.ActiveProposalQueueIterator(suite.ctx, *proposal.VotingEndTime)
|
2021-04-30 01:41:25 -07:00
|
|
|
suite.Require().True(activeIterator.Valid())
|
2019-08-08 12:51:18 -07:00
|
|
|
|
|
|
|
proposalID := types.GetProposalIDFromBytes(activeIterator.Value())
|
2022-02-14 05:48:17 -08:00
|
|
|
suite.Require().Equal(proposalID, proposal.Id)
|
2019-08-08 12:51:18 -07:00
|
|
|
activeIterator.Close()
|
|
|
|
}
|
|
|
|
|
2021-12-13 10:48:44 -08:00
|
|
|
type invalidProposalRoute struct{ v1beta1.TextProposal }
|
2019-08-08 12:51:18 -07:00
|
|
|
|
|
|
|
func (invalidProposalRoute) ProposalRoute() string { return "nonexistingroute" }
|
|
|
|
|
2021-04-30 01:41:25 -07:00
|
|
|
func (suite *KeeperTestSuite) TestSubmitProposal() {
|
2022-01-21 03:14:00 -08:00
|
|
|
govAcct := suite.app.GovKeeper.GetGovernanceAccount(suite.ctx).GetAddress().String()
|
|
|
|
_, _, randomAddr := testdata.KeyTestPubAddr()
|
2022-01-25 02:52:45 -08:00
|
|
|
tp := v1beta1.TextProposal{Title: "title", Description: "description"}
|
2022-01-21 03:14:00 -08:00
|
|
|
|
2019-08-08 12:51:18 -07:00
|
|
|
testCases := []struct {
|
2021-12-13 10:48:44 -08:00
|
|
|
content v1beta1.Content
|
2022-01-21 03:14:00 -08:00
|
|
|
authority string
|
2022-03-01 00:35:37 -08:00
|
|
|
metadata string
|
2019-12-27 09:57:54 -08:00
|
|
|
expectedErr error
|
2019-08-08 12:51:18 -07:00
|
|
|
}{
|
2022-03-01 00:35:37 -08:00
|
|
|
{&tp, govAcct, "", nil},
|
2019-08-08 12:51:18 -07:00
|
|
|
// Keeper does not check the validity of title and description, no error
|
2022-03-01 00:35:37 -08:00
|
|
|
{&v1beta1.TextProposal{Title: "", Description: "description"}, govAcct, "", nil},
|
|
|
|
{&v1beta1.TextProposal{Title: strings.Repeat("1234567890", 100), Description: "description"}, govAcct, "", nil},
|
|
|
|
{&v1beta1.TextProposal{Title: "title", Description: ""}, govAcct, "", nil},
|
|
|
|
{&v1beta1.TextProposal{Title: "title", Description: strings.Repeat("1234567890", 1000)}, govAcct, "", nil},
|
2022-01-25 02:52:45 -08:00
|
|
|
// error when metadata is too long (>10000)
|
2022-03-01 00:35:37 -08:00
|
|
|
{&tp, govAcct, strings.Repeat("a", 100001), types.ErrMetadataTooLong},
|
2022-01-21 03:14:00 -08:00
|
|
|
// error when signer is not gov acct
|
2022-03-01 00:35:37 -08:00
|
|
|
{&tp, randomAddr.String(), "", types.ErrInvalidSigner},
|
2019-08-08 12:51:18 -07:00
|
|
|
// error only when invalid route
|
2022-03-01 00:35:37 -08:00
|
|
|
{&invalidProposalRoute{}, govAcct, "", types.ErrNoProposalHandlerExists},
|
2019-08-08 12:51:18 -07:00
|
|
|
}
|
|
|
|
|
2019-12-27 09:57:54 -08:00
|
|
|
for i, tc := range testCases {
|
2022-03-11 07:58:01 -08:00
|
|
|
prop, err := v1.NewLegacyContent(tc.content, tc.authority)
|
2022-01-21 03:14:00 -08:00
|
|
|
suite.Require().NoError(err)
|
2022-01-25 02:52:45 -08:00
|
|
|
_, err = suite.app.GovKeeper.SubmitProposal(suite.ctx, []sdk.Msg{prop}, tc.metadata)
|
2021-04-30 01:41:25 -07:00
|
|
|
suite.Require().True(errors.Is(tc.expectedErr, err), "tc #%d; got: %v, expected: %v", i, err, tc.expectedErr)
|
2019-08-08 12:51:18 -07:00
|
|
|
}
|
|
|
|
}
|
2019-10-18 09:46:21 -07:00
|
|
|
|
2021-04-30 01:41:25 -07:00
|
|
|
func (suite *KeeperTestSuite) TestGetProposalsFiltered() {
|
2019-10-18 09:46:21 -07:00
|
|
|
proposalID := uint64(1)
|
2022-03-11 07:58:01 -08:00
|
|
|
status := []v1.ProposalStatus{v1.StatusDepositPeriod, v1.StatusVotingPeriod}
|
2019-10-18 09:46:21 -07:00
|
|
|
|
2020-09-25 03:25:37 -07:00
|
|
|
addr1 := sdk.AccAddress("foo_________________")
|
2019-10-18 09:46:21 -07:00
|
|
|
|
|
|
|
for _, s := range status {
|
|
|
|
for i := 0; i < 50; i++ {
|
2022-03-11 07:58:01 -08:00
|
|
|
p, err := v1.NewProposal(TestProposal, proposalID, "", time.Now(), time.Now())
|
2021-04-30 01:41:25 -07:00
|
|
|
suite.Require().NoError(err)
|
2020-05-19 13:17:29 -07:00
|
|
|
|
2019-10-18 09:46:21 -07:00
|
|
|
p.Status = s
|
|
|
|
|
|
|
|
if i%2 == 0 {
|
2022-03-11 07:58:01 -08:00
|
|
|
d := v1.NewDeposit(proposalID, addr1, nil)
|
|
|
|
v := v1.NewVote(proposalID, addr1, v1.NewNonSplitVoteOption(v1.OptionYes), "")
|
2021-04-30 01:41:25 -07:00
|
|
|
suite.app.GovKeeper.SetDeposit(suite.ctx, d)
|
|
|
|
suite.app.GovKeeper.SetVote(suite.ctx, v)
|
2019-10-18 09:46:21 -07:00
|
|
|
}
|
|
|
|
|
2021-04-30 01:41:25 -07:00
|
|
|
suite.app.GovKeeper.SetProposal(suite.ctx, p)
|
2019-10-18 09:46:21 -07:00
|
|
|
proposalID++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := []struct {
|
2022-03-11 07:58:01 -08:00
|
|
|
params v1.QueryProposalsParams
|
2019-10-18 09:46:21 -07:00
|
|
|
expectedNumResults int
|
|
|
|
}{
|
2022-03-11 07:58:01 -08:00
|
|
|
{v1.NewQueryProposalsParams(1, 50, v1.StatusNil, nil, nil), 50},
|
|
|
|
{v1.NewQueryProposalsParams(1, 50, v1.StatusDepositPeriod, nil, nil), 50},
|
|
|
|
{v1.NewQueryProposalsParams(1, 50, v1.StatusVotingPeriod, nil, nil), 50},
|
|
|
|
{v1.NewQueryProposalsParams(1, 25, v1.StatusNil, nil, nil), 25},
|
|
|
|
{v1.NewQueryProposalsParams(2, 25, v1.StatusNil, nil, nil), 25},
|
|
|
|
{v1.NewQueryProposalsParams(1, 50, v1.StatusRejected, nil, nil), 0},
|
|
|
|
{v1.NewQueryProposalsParams(1, 50, v1.StatusNil, addr1, nil), 50},
|
|
|
|
{v1.NewQueryProposalsParams(1, 50, v1.StatusNil, nil, addr1), 50},
|
|
|
|
{v1.NewQueryProposalsParams(1, 50, v1.StatusNil, addr1, addr1), 50},
|
|
|
|
{v1.NewQueryProposalsParams(1, 50, v1.StatusDepositPeriod, addr1, addr1), 25},
|
|
|
|
{v1.NewQueryProposalsParams(1, 50, v1.StatusDepositPeriod, nil, nil), 50},
|
|
|
|
{v1.NewQueryProposalsParams(1, 50, v1.StatusVotingPeriod, nil, nil), 50},
|
2019-10-18 09:46:21 -07:00
|
|
|
}
|
|
|
|
|
2020-08-11 07:22:30 -07:00
|
|
|
for i, tc := range testCases {
|
2021-04-30 01:41:25 -07:00
|
|
|
suite.Run(fmt.Sprintf("Test Case %d", i), func() {
|
|
|
|
proposals := suite.app.GovKeeper.GetProposalsFiltered(suite.ctx, tc.params)
|
|
|
|
suite.Require().Len(proposals, tc.expectedNumResults)
|
2020-08-11 07:22:30 -07:00
|
|
|
|
|
|
|
for _, p := range proposals {
|
2022-03-11 07:58:01 -08:00
|
|
|
if v1.ValidProposalStatus(tc.params.ProposalStatus) {
|
2021-04-30 01:41:25 -07:00
|
|
|
suite.Require().Equal(tc.params.ProposalStatus, p.Status)
|
2020-08-11 07:22:30 -07:00
|
|
|
}
|
2019-10-18 09:46:21 -07:00
|
|
|
}
|
2020-08-11 07:22:30 -07:00
|
|
|
})
|
2019-10-18 09:46:21 -07:00
|
|
|
}
|
|
|
|
}
|
2022-01-21 03:14:00 -08:00
|
|
|
|
|
|
|
func TestMigrateProposalMessages(t *testing.T) {
|
|
|
|
content := v1beta1.NewTextProposal("Test", "description")
|
2022-03-11 07:58:01 -08:00
|
|
|
contentMsg, err := v1.NewLegacyContent(content, sdk.AccAddress("test1").String())
|
2022-01-21 03:14:00 -08:00
|
|
|
require.NoError(t, err)
|
2022-03-11 07:58:01 -08:00
|
|
|
content, err = v1.LegacyContentFromMessage(contentMsg)
|
2022-01-21 03:14:00 -08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, "Test", content.GetTitle())
|
|
|
|
require.Equal(t, "description", content.GetDescription())
|
|
|
|
}
|