Skip to content

Commit

Permalink
feat(gov): autocli query support (backport #16987) (#16999)
Browse files Browse the repository at this point in the history
Co-authored-by: Julien Robert <julien@rbrt.fr>
  • Loading branch information
mergify[bot] and julienrbrt authored Jul 14, 2023
1 parent 1018f53 commit 93a995c
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 1,603 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ Ref: https://keepachangelog.com/en/1.0.0/

### CLI Breaking Changes

* (all) Query pagination flags have been renamed with the migration to AutoCLI:
* `--limit` -> `--page-limit`
* `--offset` -> `--page-offset`
* `--count-total` -> `--page-count-total`
* `--reverse` -> `--page-reverse`
* (x/gov) [#16987](https://github.com/cosmos/cosmos-sdk/pull/16987) In `<appd> query gov proposals` the proposal status flag have renamed from `--status` to `--proposal-status`. Additonally, that flags now uses the ENUM values: `PROPOSAL_STATUS_DEPOSIT_PERIOD`, `PROPOSAL_STATUS_VOTING_PERIOD`, `PROPOSAL_STATUS_PASSED`, `PROPOSAL_STATUS_REJECTED`, `PROPOSAL_STATUS_FAILED`.
* (x/bank) [#16899](https://github.com/cosmos/cosmos-sdk/pull/16899) With the migration to AutoCLI some bank commands have been split in two:
* Use `denoms-metadata` for querying all denom metadata and `denom-metadata` for querying a specific denom metadata.
* Use `total-supply` (or `total`) for querying the total supply and `total-supply-of` for querying the supply of a specific denom.
Expand Down
1 change: 0 additions & 1 deletion tests/e2e/auth/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,6 @@ func (s *E2ETestSuite) TestCLIMultisign() {
var balRes banktypes.QueryAllBalancesResponse
err = s.network.RetryForBlocks(func() error {
resp, err := testutil.GetRequest(fmt.Sprintf("%s/cosmos/bank/v1beta1/balances/%s", val1.APIAddress, addr))
s.Require().NoError(err)
if err != nil {
return err
}
Expand Down
96 changes: 38 additions & 58 deletions tests/e2e/gov/deposits.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ import (

"cosmossdk.io/math"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec/address"
"github.com/cosmos/cosmos-sdk/testutil"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
"github.com/cosmos/cosmos-sdk/testutil/network"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/gov/client/cli"
Expand Down Expand Up @@ -59,14 +56,12 @@ func (s *DepositTestSuite) submitProposal(val *network.Validator, initialDeposit
s.Require().NoError(s.network.WaitForNextBlock())

// query proposals, return the last's id
cmd := cli.GetCmdQueryProposals(address.NewBech32Codec("cosmos"))
args := []string{fmt.Sprintf("--%s=json", flags.FlagOutput)}
res, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, args)
res, err := testutil.GetRequest(fmt.Sprintf("%s/cosmos/gov/v1/proposals", val.APIAddress))
s.Require().NoError(err)

var proposals v1.QueryProposalsResponse
err = s.cfg.Codec.UnmarshalJSON(res.Bytes(), &proposals)
err = s.cfg.Codec.UnmarshalJSON(res, &proposals)
s.Require().NoError(err)
s.Require().GreaterOrEqual(len(proposals.Proposals), 1)

return proposals.Proposals[len(proposals.Proposals)-1].Id
}
Expand All @@ -93,14 +88,14 @@ func (s *DepositTestSuite) TestQueryDepositsWithoutInitialDeposit() {
// query deposit
deposit := s.queryDeposit(val, proposalID, false, "")
s.Require().NotNil(deposit)
s.Require().Equal(sdk.Coins(deposit.Amount).String(), depositAmount)
s.Require().Equal(depositAmount, sdk.Coins(deposit.Deposit.Amount).String())

// query deposits
deposits := s.queryDeposits(val, proposalID, false, "")
s.Require().NotNil(deposits)
s.Require().Len(deposits.Deposits, 1)
// verify initial deposit
s.Require().Equal(sdk.Coins(deposits.Deposits[0].Amount).String(), depositAmount)
s.Require().Equal(depositAmount, sdk.Coins(deposits.Deposits[0].Amount).String())
}

func (s *DepositTestSuite) TestQueryDepositsWithInitialDeposit() {
Expand All @@ -114,14 +109,14 @@ func (s *DepositTestSuite) TestQueryDepositsWithInitialDeposit() {
// query deposit
deposit := s.queryDeposit(val, proposalID, false, "")
s.Require().NotNil(deposit)
s.Require().Equal(sdk.Coins(deposit.Amount).String(), depositAmount.String())
s.Require().Equal(depositAmount.String(), sdk.Coins(deposit.Deposit.Amount).String())

// query deposits
deposits := s.queryDeposits(val, proposalID, false, "")
s.Require().NotNil(deposits)
s.Require().Len(deposits.Deposits, 1)
// verify initial deposit
s.Require().Equal(sdk.Coins(deposits.Deposits[0].Amount).String(), depositAmount.String())
s.Require().Equal(depositAmount.String(), sdk.Coins(deposits.Deposits[0].Amount).String())
}

func (s *DepositTestSuite) TestQueryProposalAfterVotingPeriod() {
Expand All @@ -132,80 +127,65 @@ func (s *DepositTestSuite) TestQueryProposalAfterVotingPeriod() {
id := s.submitProposal(val, depositAmount, "TestQueryProposalAfterVotingPeriod")
proposalID := strconv.FormatUint(id, 10)

args := []string{fmt.Sprintf("--%s=json", flags.FlagOutput)}
cmd := cli.GetCmdQueryProposals(address.NewBech32Codec("cosmos"))
_, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, args)
resp, err := testutil.GetRequest(fmt.Sprintf("%s/cosmos/gov/v1/proposals", val.APIAddress))
s.Require().NoError(err)
var proposals v1.QueryProposalsResponse
err = s.cfg.Codec.UnmarshalJSON(resp, &proposals)
s.Require().NoError(err)
s.Require().GreaterOrEqual(len(proposals.Proposals), 1)

// query proposal
args = []string{proposalID, fmt.Sprintf("--%s=json", flags.FlagOutput)}
cmd = cli.GetCmdQueryProposal()
_, err = clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, args)
resp, err = testutil.GetRequest(fmt.Sprintf("%s/cosmos/gov/v1/proposals/%s", val.APIAddress, proposalID))
s.Require().NoError(err)
var proposal v1.QueryProposalResponse
err = s.cfg.Codec.UnmarshalJSON(resp, &proposal)
s.Require().NoError(err)

// waiting for deposit and voting period to end
time.Sleep(25 * time.Second)

// query proposal
_, err = clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, args)
s.Require().Error(err)
s.Require().Contains(err.Error(), fmt.Sprintf("proposal %s doesn't exist", proposalID))
resp, err = testutil.GetRequest(fmt.Sprintf("%s/cosmos/gov/v1/proposals/%s", val.APIAddress, proposalID))
s.Require().NoError(err)
s.Require().Contains(string(resp), fmt.Sprintf("proposal %s doesn't exist", proposalID))

// query deposits
deposits := s.queryDeposits(val, proposalID, true, "proposal 3 doesn't exist")
s.Require().Nil(deposits)
deposits := s.queryDeposits(val, proposalID, false, "")
s.Require().Len(deposits.Deposits, 0)
}

func (s *DepositTestSuite) queryDeposits(val *network.Validator, proposalID string, exceptErr bool, message string) *v1.QueryDepositsResponse {
args := []string{proposalID, fmt.Sprintf("--%s=json", flags.FlagOutput)}
var depositsRes *v1.QueryDepositsResponse
cmd := cli.GetCmdQueryDeposits()

var (
out testutil.BufferWriter
err error
)
s.Require().NoError(s.network.WaitForNextBlock())

err = s.network.RetryForBlocks(func() error {
out, err = clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, args)
if err == nil {
err = val.ClientCtx.LegacyAmino.UnmarshalJSON(out.Bytes(), &depositsRes)
return err
}
return err
}, 3)
resp, err := testutil.GetRequest(fmt.Sprintf("%s/cosmos/gov/v1/proposals/%s/deposits", val.APIAddress, proposalID))
s.Require().NoError(err)

if exceptErr {
s.Require().Error(err)
s.Require().Contains(err.Error(), message)
s.Require().Contains(string(resp), message)
return nil
}

var depositsRes v1.QueryDepositsResponse
err = val.ClientCtx.Codec.UnmarshalJSON(resp, &depositsRes)
s.Require().NoError(err)
return depositsRes

return &depositsRes
}

func (s *DepositTestSuite) queryDeposit(val *network.Validator, proposalID string, exceptErr bool, message string) *v1.Deposit {
args := []string{proposalID, val.Address.String(), fmt.Sprintf("--%s=json", flags.FlagOutput)}
var depositRes *v1.Deposit
cmd := cli.GetCmdQueryDeposit()
var (
out testutil.BufferWriter
err error
)
func (s *DepositTestSuite) queryDeposit(val *network.Validator, proposalID string, exceptErr bool, message string) *v1.QueryDepositResponse {
s.Require().NoError(s.network.WaitForNextBlock())

err = s.network.RetryForBlocks(func() error {
out, err = clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, args)
return err
}, 3)
resp, err := testutil.GetRequest(fmt.Sprintf("%s/cosmos/gov/v1/proposals/%s/deposits/%s", val.APIAddress, proposalID, val.Address.String()))
s.Require().NoError(err)

if exceptErr {
s.Require().Error(err)
s.Require().Contains(err.Error(), message)
s.Require().Contains(string(resp), message)
return nil
}

var depositRes v1.QueryDepositResponse
err = val.ClientCtx.Codec.UnmarshalJSON(resp, &depositRes)
s.Require().NoError(err)
s.Require().NoError(val.ClientCtx.LegacyAmino.UnmarshalJSON(out.Bytes(), &depositRes))

return depositRes
return &depositRes
}
Loading

0 comments on commit 93a995c

Please sign in to comment.