|
| 1 | +package testutil |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "time" |
| 6 | + |
| 7 | + clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" |
| 8 | + "github.com/cosmos/cosmos-sdk/testutil/network" |
| 9 | + sdk "github.com/cosmos/cosmos-sdk/types" |
| 10 | + "github.com/cosmos/cosmos-sdk/x/gov/client/cli" |
| 11 | + "github.com/cosmos/cosmos-sdk/x/gov/types" |
| 12 | + "github.com/stretchr/testify/suite" |
| 13 | + tmcli "github.com/tendermint/tendermint/libs/cli" |
| 14 | +) |
| 15 | + |
| 16 | +type DepositTestSuite struct { |
| 17 | + suite.Suite |
| 18 | + |
| 19 | + cfg network.Config |
| 20 | + network *network.Network |
| 21 | + fees string |
| 22 | +} |
| 23 | + |
| 24 | +func NewDepositTestSuite(cfg network.Config) *DepositTestSuite { |
| 25 | + return &DepositTestSuite{cfg: cfg} |
| 26 | +} |
| 27 | + |
| 28 | +func (s *DepositTestSuite) SetupSuite() { |
| 29 | + s.T().Log("setting up test suite") |
| 30 | + |
| 31 | + s.network = network.New(s.T(), s.cfg) |
| 32 | + |
| 33 | + _, err := s.network.WaitForHeight(1) |
| 34 | + s.Require().NoError(err) |
| 35 | + s.fees = sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(20))).String() |
| 36 | + |
| 37 | +} |
| 38 | + |
| 39 | +func (s *DepositTestSuite) TearDownSuite() { |
| 40 | + s.T().Log("tearing down test suite") |
| 41 | + s.network.Cleanup() |
| 42 | +} |
| 43 | + |
| 44 | +func (s *DepositTestSuite) TestQueryDepositsInitialDeposit() { |
| 45 | + val := s.network.Validators[0] |
| 46 | + clientCtx := val.ClientCtx |
| 47 | + initialDeposit := sdk.NewCoin(s.cfg.BondDenom, types.DefaultMinDepositTokens.Sub(sdk.NewInt(20))).String() |
| 48 | + |
| 49 | + // create a proposal with deposit |
| 50 | + _, err := MsgSubmitProposal(val.ClientCtx, val.Address.String(), |
| 51 | + "Text Proposal 1", "Where is the title!?", types.ProposalTypeText, |
| 52 | + fmt.Sprintf("--%s=%s", cli.FlagDeposit, initialDeposit)) |
| 53 | + s.Require().NoError(err) |
| 54 | + |
| 55 | + // deposit more amount |
| 56 | + _, err = MsgDeposit(clientCtx, val.Address.String(), "1", sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(50)).String()) |
| 57 | + s.Require().NoError(err) |
| 58 | + |
| 59 | + // waiting for voting period to end |
| 60 | + time.Sleep(20 * time.Second) |
| 61 | + |
| 62 | + // query deposit & verify initial deposit |
| 63 | + deposit := s.queryDeposit(val, "1", false) |
| 64 | + s.Require().Equal(deposit.Amount.String(), initialDeposit) |
| 65 | + |
| 66 | + // query deposits |
| 67 | + deposits := s.queryDeposits(val, "1", false) |
| 68 | + s.Require().Equal(len(deposits), 2) |
| 69 | + // verify initial deposit |
| 70 | + s.Require().Equal(deposits[0].Amount.String(), initialDeposit) |
| 71 | +} |
| 72 | + |
| 73 | +func (s *DepositTestSuite) TestQueryDepositsWithoutInitialDeposit() { |
| 74 | + val := s.network.Validators[0] |
| 75 | + clientCtx := val.ClientCtx |
| 76 | + |
| 77 | + // create a proposal without deposit |
| 78 | + _, err := MsgSubmitProposal(val.ClientCtx, val.Address.String(), |
| 79 | + "Text Proposal 2", "Where is the title!?", types.ProposalTypeText) |
| 80 | + s.Require().NoError(err) |
| 81 | + |
| 82 | + // deposit amount |
| 83 | + _, err = MsgDeposit(clientCtx, val.Address.String(), "2", sdk.NewCoin(s.cfg.BondDenom, types.DefaultMinDepositTokens.Add(sdk.NewInt(50))).String()) |
| 84 | + s.Require().NoError(err) |
| 85 | + |
| 86 | + // waiting for voting period to end |
| 87 | + time.Sleep(20 * time.Second) |
| 88 | + |
| 89 | + // query deposit |
| 90 | + deposit := s.queryDeposit(val, "2", false) |
| 91 | + s.Require().Equal(deposit.Amount.String(), sdk.NewCoin(s.cfg.BondDenom, types.DefaultMinDepositTokens.Add(sdk.NewInt(50))).String()) |
| 92 | + |
| 93 | + // query deposits |
| 94 | + deposits := s.queryDeposits(val, "2", false) |
| 95 | + s.Require().Equal(len(deposits), 1) |
| 96 | + // verify initial deposit |
| 97 | + s.Require().Equal(deposits[0].Amount.String(), sdk.NewCoin(s.cfg.BondDenom, types.DefaultMinDepositTokens.Add(sdk.NewInt(50))).String()) |
| 98 | +} |
| 99 | + |
| 100 | +func (s *DepositTestSuite) TestQueryProposalNotEnoughDeposits() { |
| 101 | + val := s.network.Validators[0] |
| 102 | + clientCtx := val.ClientCtx |
| 103 | + initialDeposit := sdk.NewCoin(s.cfg.BondDenom, types.DefaultMinDepositTokens.Sub(sdk.NewInt(2000))).String() |
| 104 | + |
| 105 | + // create a proposal with deposit |
| 106 | + _, err := MsgSubmitProposal(val.ClientCtx, val.Address.String(), |
| 107 | + "Text Proposal 3", "Where is the title!?", types.ProposalTypeText, |
| 108 | + fmt.Sprintf("--%s=%s", cli.FlagDeposit, initialDeposit)) |
| 109 | + s.Require().NoError(err) |
| 110 | + |
| 111 | + // query proposal |
| 112 | + args := []string{"3", fmt.Sprintf("--%s=json", tmcli.OutputFlag)} |
| 113 | + cmd := cli.GetCmdQueryProposal() |
| 114 | + _, err = clitestutil.ExecTestCLICmd(clientCtx, cmd, args) |
| 115 | + s.Require().NoError(err) |
| 116 | + |
| 117 | + // waiting for deposit period to end |
| 118 | + time.Sleep(20 * time.Second) |
| 119 | + |
| 120 | + // query proposal |
| 121 | + _, err = clitestutil.ExecTestCLICmd(clientCtx, cmd, args) |
| 122 | + s.Require().Error(err) |
| 123 | + s.Require().Contains(err.Error(), "proposal 3 doesn't exist") |
| 124 | +} |
| 125 | + |
| 126 | +func (s *DepositTestSuite) TestRejectedProposalDeposits() { |
| 127 | + val := s.network.Validators[0] |
| 128 | + clientCtx := val.ClientCtx |
| 129 | + initialDeposit := sdk.NewCoin(s.cfg.BondDenom, types.DefaultMinDepositTokens) |
| 130 | + |
| 131 | + // create a proposal with deposit |
| 132 | + _, err := MsgSubmitProposal(clientCtx, val.Address.String(), |
| 133 | + "Text Proposal 4", "Where is the title!?", types.ProposalTypeText, |
| 134 | + fmt.Sprintf("--%s=%s", cli.FlagDeposit, initialDeposit)) |
| 135 | + s.Require().NoError(err) |
| 136 | + |
| 137 | + // query deposits |
| 138 | + var deposits types.QueryDepositsResponse |
| 139 | + args := []string{"4", fmt.Sprintf("--%s=json", tmcli.OutputFlag)} |
| 140 | + cmd := cli.GetCmdQueryDeposits() |
| 141 | + out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, args) |
| 142 | + s.Require().NoError(err) |
| 143 | + s.Require().NoError(val.ClientCtx.LegacyAmino.UnmarshalJSON(out.Bytes(), &deposits)) |
| 144 | + s.Require().Equal(len(deposits.Deposits), 1) |
| 145 | + // verify initial deposit |
| 146 | + s.Require().Equal(deposits.Deposits[0].Amount.String(), sdk.NewCoin(s.cfg.BondDenom, types.DefaultMinDepositTokens).String()) |
| 147 | + |
| 148 | + // vote |
| 149 | + _, err = MsgVote(clientCtx, val.Address.String(), "4", "no") |
| 150 | + s.Require().NoError(err) |
| 151 | + |
| 152 | + time.Sleep(20 * time.Second) |
| 153 | + |
| 154 | + args = []string{"4", fmt.Sprintf("--%s=json", tmcli.OutputFlag)} |
| 155 | + cmd = cli.GetCmdQueryProposal() |
| 156 | + _, err = clitestutil.ExecTestCLICmd(clientCtx, cmd, args) |
| 157 | + s.Require().NoError(err) |
| 158 | + |
| 159 | + // query deposits |
| 160 | + depositsRes := s.queryDeposits(val, "4", false) |
| 161 | + s.Require().Equal(len(depositsRes), 1) |
| 162 | + // verify initial deposit |
| 163 | + s.Require().Equal(depositsRes[0].Amount.String(), initialDeposit.String()) |
| 164 | + |
| 165 | +} |
| 166 | + |
| 167 | +func (s *DepositTestSuite) queryDeposits(val *network.Validator, proposalID string, exceptErr bool) types.Deposits { |
| 168 | + args := []string{proposalID, fmt.Sprintf("--%s=json", tmcli.OutputFlag)} |
| 169 | + var depositsRes types.Deposits |
| 170 | + cmd := cli.GetCmdQueryDeposits() |
| 171 | + out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, args) |
| 172 | + if exceptErr { |
| 173 | + s.Require().Error(err) |
| 174 | + return nil |
| 175 | + } |
| 176 | + s.Require().NoError(err) |
| 177 | + s.Require().NoError(val.ClientCtx.LegacyAmino.UnmarshalJSON(out.Bytes(), &depositsRes)) |
| 178 | + return depositsRes |
| 179 | +} |
| 180 | + |
| 181 | +func (s *DepositTestSuite) queryDeposit(val *network.Validator, proposalID string, exceptErr bool) *types.Deposit { |
| 182 | + args := []string{proposalID, val.Address.String(), fmt.Sprintf("--%s=json", tmcli.OutputFlag)} |
| 183 | + var depositRes types.Deposit |
| 184 | + cmd := cli.GetCmdQueryDeposit() |
| 185 | + out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, args) |
| 186 | + if exceptErr { |
| 187 | + s.Require().Error(err) |
| 188 | + return nil |
| 189 | + } |
| 190 | + s.Require().NoError(err) |
| 191 | + s.Require().NoError(val.ClientCtx.LegacyAmino.UnmarshalJSON(out.Bytes(), &depositRes)) |
| 192 | + return &depositRes |
| 193 | +} |
0 commit comments