diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ece4ef04cab..a40d5bfa97bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Bug Fixes +<<<<<<< HEAD * [\#9235](https://github.com/cosmos/cosmos-sdk/pull/9235) CreateMembershipProof/CreateNonMembershipProof now returns an error if input key is empty, or input data contains empty key. * [\#9108](https://github.com/cosmos/cosmos-sdk/pull/9108) Fixed the bug with querying multisig account, which is not showing threshold and public_keys. @@ -53,6 +54,14 @@ if input key is empty, or input data contains empty key. * [\#9040](https://github.com/cosmos/cosmos-sdk/pull/9040) Fix ENV variables binding to CLI flags for client config. ### Features +======= +* (x/gov) [\#8813](https://github.com/cosmos/cosmos-sdk/pull/8813) fix `GET /cosmos/gov/v1beta1/proposals/{proposal_id}/deposits` to include initial deposit +* (gRPC) [\#8945](https://github.com/cosmos/cosmos-sdk/pull/8945) gRPC reflection now works correctly. +* (keyring) [#\8635](https://github.com/cosmos/cosmos-sdk/issues/8635) Remove hardcoded default passphrase value on `NewMnemonic` +* (x/bank) [\#8434](https://github.com/cosmos/cosmos-sdk/pull/8434) Fix legacy REST API `GET /bank/total` and `GET /bank/total/{denom}` in swagger +* (x/slashing) [\#8427](https://github.com/cosmos/cosmos-sdk/pull/8427) Fix query signing infos command +* (x/bank) [\#9229](https://github.com/cosmos/cosmos-sdk/pull/9229) Now zero coin balances cannot be added to balances & supply stores. If any denom becomes zero corresponding key gets deleted from store. +>>>>>>> 66ee994ce (fix: x/gov deposits querier (Initial Deposit) (#9288)) * [\#8953](https://github.com/cosmos/cosmos-sdk/pull/8953) Add the `config` CLI subcommand back to the SDK, which saves client-side configuration in a `client.toml` file. diff --git a/x/gov/client/cli/query.go b/x/gov/client/cli/query.go index 8c67cd96e5e1..1a20b5745965 100644 --- a/x/gov/client/cli/query.go +++ b/x/gov/client/cli/query.go @@ -364,8 +364,14 @@ $ %s query gov deposit 1 cosmos1skjwj5whet0lpe65qaq4rpq03hjxlwd9nf39lk } // check to see if the proposal is in the store +<<<<<<< HEAD _, err = queryClient.Proposal( context.Background(), +======= + ctx := cmd.Context() + proposalRes, err := queryClient.Proposal( + ctx, +>>>>>>> 66ee994ce (fix: x/gov deposits querier (Initial Deposit) (#9288)) &types.QueryProposalRequest{ProposalId: proposalID}, ) if err != nil { @@ -377,6 +383,7 @@ $ %s query gov deposit 1 cosmos1skjwj5whet0lpe65qaq4rpq03hjxlwd9nf39lk return err } +<<<<<<< HEAD res, err := queryClient.Deposit( context.Background(), &types.QueryDepositRequest{ProposalId: proposalID, Depositor: args[1]}, @@ -387,15 +394,33 @@ $ %s query gov deposit 1 cosmos1skjwj5whet0lpe65qaq4rpq03hjxlwd9nf39lk deposit := res.GetDeposit() if deposit.Empty() { +======= + var deposit types.Deposit + propStatus := proposalRes.Proposal.Status + if !(propStatus == types.StatusVotingPeriod || propStatus == types.StatusDepositPeriod) { +>>>>>>> 66ee994ce (fix: x/gov deposits querier (Initial Deposit) (#9288)) params := types.NewQueryDepositParams(proposalID, depositorAddr) resByTxQuery, err := gcutils.QueryDepositByTxQuery(clientCtx, params) if err != nil { return err } +<<<<<<< HEAD clientCtx.JSONMarshaler.MustUnmarshalJSON(resByTxQuery, &deposit) +======= + clientCtx.JSONCodec.MustUnmarshalJSON(resByTxQuery, &deposit) + return clientCtx.PrintProto(&deposit) + } + + res, err := queryClient.Deposit( + ctx, + &types.QueryDepositRequest{ProposalId: proposalID, Depositor: args[1]}, + ) + if err != nil { + return err +>>>>>>> 66ee994ce (fix: x/gov deposits querier (Initial Deposit) (#9288)) } - return clientCtx.PrintProto(&deposit) + return clientCtx.PrintProto(&res.Deposit) }, } diff --git a/x/gov/client/testutil/cli_test.go b/x/gov/client/testutil/cli_test.go new file mode 100644 index 000000000000..713c33f12182 --- /dev/null +++ b/x/gov/client/testutil/cli_test.go @@ -0,0 +1,29 @@ +// +build norace + +package testutil + +import ( + "testing" + "time" + + "github.com/cosmos/cosmos-sdk/testutil/network" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/gov/types" + + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" +) + +func TestIntegrationTestSuite(t *testing.T) { + cfg := network.DefaultConfig() + cfg.NumValidators = 1 + suite.Run(t, NewIntegrationTestSuite(cfg)) + + genesisState := types.DefaultGenesisState() + genesisState.DepositParams = types.NewDepositParams(sdk.NewCoins(sdk.NewCoin(cfg.BondDenom, types.DefaultMinDepositTokens)), time.Duration(15)*time.Second) + genesisState.VotingParams = types.NewVotingParams(time.Duration(5) * time.Second) + bz, err := cfg.Codec.MarshalJSON(genesisState) + require.NoError(t, err) + cfg.GenesisState["gov"] = bz + suite.Run(t, NewDepositTestSuite(cfg)) +} diff --git a/x/gov/client/testutil/deposits.go b/x/gov/client/testutil/deposits.go new file mode 100644 index 000000000000..57c3c9363330 --- /dev/null +++ b/x/gov/client/testutil/deposits.go @@ -0,0 +1,193 @@ +package testutil + +import ( + "fmt" + "time" + + 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" + "github.com/cosmos/cosmos-sdk/x/gov/types" + "github.com/stretchr/testify/suite" + tmcli "github.com/tendermint/tendermint/libs/cli" +) + +type DepositTestSuite struct { + suite.Suite + + cfg network.Config + network *network.Network + fees string +} + +func NewDepositTestSuite(cfg network.Config) *DepositTestSuite { + return &DepositTestSuite{cfg: cfg} +} + +func (s *DepositTestSuite) SetupSuite() { + s.T().Log("setting up test suite") + + s.network = network.New(s.T(), s.cfg) + + _, err := s.network.WaitForHeight(1) + s.Require().NoError(err) + s.fees = sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(20))).String() + +} + +func (s *DepositTestSuite) TearDownSuite() { + s.T().Log("tearing down test suite") + s.network.Cleanup() +} + +func (s *DepositTestSuite) TestQueryDepositsInitialDeposit() { + val := s.network.Validators[0] + clientCtx := val.ClientCtx + initialDeposit := sdk.NewCoin(s.cfg.BondDenom, types.DefaultMinDepositTokens.Sub(sdk.NewInt(20))).String() + + // create a proposal with deposit + _, err := MsgSubmitProposal(val.ClientCtx, val.Address.String(), + "Text Proposal 1", "Where is the title!?", types.ProposalTypeText, + fmt.Sprintf("--%s=%s", cli.FlagDeposit, initialDeposit)) + s.Require().NoError(err) + + // deposit more amount + _, err = MsgDeposit(clientCtx, val.Address.String(), "1", sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(50)).String()) + s.Require().NoError(err) + + // waiting for voting period to end + time.Sleep(20 * time.Second) + + // query deposit & verify initial deposit + deposit := s.queryDeposit(val, "1", false) + s.Require().Equal(deposit.Amount.String(), initialDeposit) + + // query deposits + deposits := s.queryDeposits(val, "1", false) + s.Require().Equal(len(deposits), 2) + // verify initial deposit + s.Require().Equal(deposits[0].Amount.String(), initialDeposit) +} + +func (s *DepositTestSuite) TestQueryDepositsWithoutInitialDeposit() { + val := s.network.Validators[0] + clientCtx := val.ClientCtx + + // create a proposal without deposit + _, err := MsgSubmitProposal(val.ClientCtx, val.Address.String(), + "Text Proposal 2", "Where is the title!?", types.ProposalTypeText) + s.Require().NoError(err) + + // deposit amount + _, err = MsgDeposit(clientCtx, val.Address.String(), "2", sdk.NewCoin(s.cfg.BondDenom, types.DefaultMinDepositTokens.Add(sdk.NewInt(50))).String()) + s.Require().NoError(err) + + // waiting for voting period to end + time.Sleep(20 * time.Second) + + // query deposit + deposit := s.queryDeposit(val, "2", false) + s.Require().Equal(deposit.Amount.String(), sdk.NewCoin(s.cfg.BondDenom, types.DefaultMinDepositTokens.Add(sdk.NewInt(50))).String()) + + // query deposits + deposits := s.queryDeposits(val, "2", false) + s.Require().Equal(len(deposits), 1) + // verify initial deposit + s.Require().Equal(deposits[0].Amount.String(), sdk.NewCoin(s.cfg.BondDenom, types.DefaultMinDepositTokens.Add(sdk.NewInt(50))).String()) +} + +func (s *DepositTestSuite) TestQueryProposalNotEnoughDeposits() { + val := s.network.Validators[0] + clientCtx := val.ClientCtx + initialDeposit := sdk.NewCoin(s.cfg.BondDenom, types.DefaultMinDepositTokens.Sub(sdk.NewInt(2000))).String() + + // create a proposal with deposit + _, err := MsgSubmitProposal(val.ClientCtx, val.Address.String(), + "Text Proposal 3", "Where is the title!?", types.ProposalTypeText, + fmt.Sprintf("--%s=%s", cli.FlagDeposit, initialDeposit)) + s.Require().NoError(err) + + // query proposal + args := []string{"3", fmt.Sprintf("--%s=json", tmcli.OutputFlag)} + cmd := cli.GetCmdQueryProposal() + _, err = clitestutil.ExecTestCLICmd(clientCtx, cmd, args) + s.Require().NoError(err) + + // waiting for deposit period to end + time.Sleep(20 * time.Second) + + // query proposal + _, err = clitestutil.ExecTestCLICmd(clientCtx, cmd, args) + s.Require().Error(err) + s.Require().Contains(err.Error(), "proposal 3 doesn't exist") +} + +func (s *DepositTestSuite) TestRejectedProposalDeposits() { + val := s.network.Validators[0] + clientCtx := val.ClientCtx + initialDeposit := sdk.NewCoin(s.cfg.BondDenom, types.DefaultMinDepositTokens) + + // create a proposal with deposit + _, err := MsgSubmitProposal(clientCtx, val.Address.String(), + "Text Proposal 4", "Where is the title!?", types.ProposalTypeText, + fmt.Sprintf("--%s=%s", cli.FlagDeposit, initialDeposit)) + s.Require().NoError(err) + + // query deposits + var deposits types.QueryDepositsResponse + args := []string{"4", fmt.Sprintf("--%s=json", tmcli.OutputFlag)} + cmd := cli.GetCmdQueryDeposits() + out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, args) + s.Require().NoError(err) + s.Require().NoError(val.ClientCtx.LegacyAmino.UnmarshalJSON(out.Bytes(), &deposits)) + s.Require().Equal(len(deposits.Deposits), 1) + // verify initial deposit + s.Require().Equal(deposits.Deposits[0].Amount.String(), sdk.NewCoin(s.cfg.BondDenom, types.DefaultMinDepositTokens).String()) + + // vote + _, err = MsgVote(clientCtx, val.Address.String(), "4", "no") + s.Require().NoError(err) + + time.Sleep(20 * time.Second) + + args = []string{"4", fmt.Sprintf("--%s=json", tmcli.OutputFlag)} + cmd = cli.GetCmdQueryProposal() + _, err = clitestutil.ExecTestCLICmd(clientCtx, cmd, args) + s.Require().NoError(err) + + // query deposits + depositsRes := s.queryDeposits(val, "4", false) + s.Require().Equal(len(depositsRes), 1) + // verify initial deposit + s.Require().Equal(depositsRes[0].Amount.String(), initialDeposit.String()) + +} + +func (s *DepositTestSuite) queryDeposits(val *network.Validator, proposalID string, exceptErr bool) types.Deposits { + args := []string{proposalID, fmt.Sprintf("--%s=json", tmcli.OutputFlag)} + var depositsRes types.Deposits + cmd := cli.GetCmdQueryDeposits() + out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, args) + if exceptErr { + s.Require().Error(err) + return nil + } + s.Require().NoError(err) + s.Require().NoError(val.ClientCtx.LegacyAmino.UnmarshalJSON(out.Bytes(), &depositsRes)) + return depositsRes +} + +func (s *DepositTestSuite) queryDeposit(val *network.Validator, proposalID string, exceptErr bool) *types.Deposit { + args := []string{proposalID, val.Address.String(), fmt.Sprintf("--%s=json", tmcli.OutputFlag)} + var depositRes types.Deposit + cmd := cli.GetCmdQueryDeposit() + out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, args) + if exceptErr { + s.Require().Error(err) + return nil + } + s.Require().NoError(err) + s.Require().NoError(val.ClientCtx.LegacyAmino.UnmarshalJSON(out.Bytes(), &depositRes)) + return &depositRes +} diff --git a/x/gov/client/testutil/helpers.go b/x/gov/client/testutil/helpers.go index 535cb8d09ca6..36fdabbbcd2f 100644 --- a/x/gov/client/testutil/helpers.go +++ b/x/gov/client/testutil/helpers.go @@ -43,3 +43,15 @@ func MsgVote(clientCtx client.Context, from, id, vote string, extraArgs ...strin return clitestutil.ExecTestCLICmd(clientCtx, govcli.NewCmdVote(), args) } + +func MsgDeposit(clientCtx client.Context, from, id, deposit string, extraArgs ...string) (testutil.BufferWriter, error) { + args := append([]string{ + id, + deposit, + fmt.Sprintf("--%s=%s", flags.FlagFrom, from), + }, commonArgs...) + + args = append(args, extraArgs...) + + return clitestutil.ExecTestCLICmd(clientCtx, govcli.NewCmdDeposit(), args) +} diff --git a/x/gov/client/utils/query.go b/x/gov/client/utils/query.go index e543f526b3f0..4f43ec55dfd8 100644 --- a/x/gov/client/utils/query.go +++ b/x/gov/client/utils/query.go @@ -5,7 +5,12 @@ import ( "github.com/cosmos/cosmos-sdk/client" sdk "github.com/cosmos/cosmos-sdk/types" +<<<<<<< HEAD authclient "github.com/cosmos/cosmos-sdk/x/auth/client" +======= + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + authtx "github.com/cosmos/cosmos-sdk/x/auth/tx" +>>>>>>> 66ee994ce (fix: x/gov deposits querier (Initial Deposit) (#9288)) "github.com/cosmos/cosmos-sdk/x/gov/types" ) @@ -37,6 +42,7 @@ func (p Proposer) String() string { // NOTE: SearchTxs is used to facilitate the txs query which does not currently // support configurable pagination. func QueryDepositsByTxQuery(clientCtx client.Context, params types.QueryProposalParams) ([]byte, error) { +<<<<<<< HEAD events := []string{ fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, types.TypeMsgDeposit), fmt.Sprintf("%s.%s='%s'", types.EventTypeProposalDeposit, types.AttributeKeyProposalID, []byte(fmt.Sprintf("%d", params.ProposalID))), @@ -45,11 +51,36 @@ func QueryDepositsByTxQuery(clientCtx client.Context, params types.QueryProposal // NOTE: SearchTxs is used to facilitate the txs query which does not currently // support configurable pagination. searchResult, err := authclient.QueryTxsByEvents(clientCtx, events, defaultPage, defaultLimit, "") +======= + var deposits []types.Deposit + + // initial deposit was submitted with proposal, so must be queried separately + initialDeposit, err := queryInitialDepositByTxQuery(clientCtx, params.ProposalID) if err != nil { return nil, err } - var deposits []types.Deposit + if !initialDeposit.Amount.IsZero() { + deposits = append(deposits, initialDeposit) + } + + searchResult, err := combineEvents( + clientCtx, defaultPage, + // Query legacy Msgs event action + []string{ + fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, types.TypeMsgDeposit), + fmt.Sprintf("%s.%s='%s'", types.EventTypeProposalDeposit, types.AttributeKeyProposalID, []byte(fmt.Sprintf("%d", params.ProposalID))), + }, + // Query proto Msgs event action + []string{ + fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, sdk.MsgTypeURL(&types.MsgDeposit{})), + fmt.Sprintf("%s.%s='%s'", types.EventTypeProposalDeposit, types.AttributeKeyProposalID, []byte(fmt.Sprintf("%d", params.ProposalID))), + }, + ) +>>>>>>> 66ee994ce (fix: x/gov deposits querier (Initial Deposit) (#9288)) + if err != nil { + return nil, err + } for _, info := range searchResult.Txs { for _, msg := range info.GetTx().GetMsgs() { @@ -167,6 +198,7 @@ func QueryVoteByTxQuery(clientCtx client.Context, params types.QueryVoteParams) // QueryDepositByTxQuery will query for a single deposit via a direct txs tags // query. func QueryDepositByTxQuery(clientCtx client.Context, params types.QueryDepositParams) ([]byte, error) { +<<<<<<< HEAD events := []string{ fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, types.TypeMsgDeposit), fmt.Sprintf("%s.%s='%s'", types.EventTypeProposalDeposit, types.AttributeKeyProposalID, []byte(fmt.Sprintf("%d", params.ProposalID))), @@ -176,6 +208,39 @@ func QueryDepositByTxQuery(clientCtx client.Context, params types.QueryDepositPa // NOTE: SearchTxs is used to facilitate the txs query which does not currently // support configurable pagination. searchResult, err := authclient.QueryTxsByEvents(clientCtx, events, defaultPage, defaultLimit, "") +======= + + // initial deposit was submitted with proposal, so must be queried separately + initialDeposit, err := queryInitialDepositByTxQuery(clientCtx, params.ProposalID) + if err != nil { + return nil, err + } + + if !initialDeposit.Amount.IsZero() { + bz, err := clientCtx.JSONCodec.MarshalJSON(&initialDeposit) + if err != nil { + return nil, err + } + + return bz, nil + } + + searchResult, err := combineEvents( + clientCtx, defaultPage, + // Query legacy Msgs event action + []string{ + fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, types.TypeMsgDeposit), + fmt.Sprintf("%s.%s='%s'", types.EventTypeProposalDeposit, types.AttributeKeyProposalID, []byte(fmt.Sprintf("%d", params.ProposalID))), + fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeySender, []byte(params.Depositor.String())), + }, + // Query proto Msgs event action + []string{ + fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, sdk.MsgTypeURL(&types.MsgDeposit{})), + fmt.Sprintf("%s.%s='%s'", types.EventTypeProposalDeposit, types.AttributeKeyProposalID, []byte(fmt.Sprintf("%d", params.ProposalID))), + fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeySender, []byte(params.Depositor.String())), + }, + ) +>>>>>>> 66ee994ce (fix: x/gov deposits querier (Initial Deposit) (#9288)) if err != nil { return nil, err } @@ -248,3 +313,65 @@ func QueryProposalByID(proposalID uint64, clientCtx client.Context, queryRoute s return res, err } +<<<<<<< HEAD +======= + +// combineEvents queries txs by events with all events from each event group, +// and combines all those events together. +// +// Tx are indexed in tendermint via their Msgs `Type()`, which can be: +// - via legacy Msgs (amino or proto), their `Type()` is a custom string, +// - via ADR-031 proto msgs, their `Type()` is the protobuf FQ method name. +// In searching for events, we search for both `Type()`s, and we use the +// `combineEvents` function here to merge events. +func combineEvents(clientCtx client.Context, page int, eventGroups ...[]string) (*sdk.SearchTxsResult, error) { + // Only the Txs field will be populated in the final SearchTxsResult. + allTxs := []*sdk.TxResponse{} + for _, events := range eventGroups { + res, err := authtx.QueryTxsByEvents(clientCtx, events, page, defaultLimit, "") + if err != nil { + return nil, err + } + allTxs = append(allTxs, res.Txs...) + } + + return &sdk.SearchTxsResult{Txs: allTxs}, nil +} + +// queryInitialDepositByTxQuery will query for a initial deposit of a governance proposal by +// ID. +func queryInitialDepositByTxQuery(clientCtx client.Context, proposalID uint64) (types.Deposit, error) { + searchResult, err := combineEvents( + clientCtx, defaultPage, + // Query legacy Msgs event action + []string{ + fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, types.TypeMsgSubmitProposal), + fmt.Sprintf("%s.%s='%s'", types.EventTypeSubmitProposal, types.AttributeKeyProposalID, []byte(fmt.Sprintf("%d", proposalID))), + }, + // Query proto Msgs event action + []string{ + fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, sdk.MsgTypeURL(&types.MsgSubmitProposal{})), + fmt.Sprintf("%s.%s='%s'", types.EventTypeSubmitProposal, types.AttributeKeyProposalID, []byte(fmt.Sprintf("%d", proposalID))), + }, + ) + + if err != nil { + return types.Deposit{}, err + } + + for _, info := range searchResult.Txs { + for _, msg := range info.GetTx().GetMsgs() { + // there should only be a single proposal under the given conditions + if subMsg, ok := msg.(*types.MsgSubmitProposal); ok { + return types.Deposit{ + ProposalId: proposalID, + Depositor: subMsg.Proposer, + Amount: subMsg.InitialDeposit, + }, nil + } + } + } + + return types.Deposit{}, sdkerrors.ErrNotFound.Wrapf("failed to find the initial deposit for proposalID %d", proposalID) +} +>>>>>>> 66ee994ce (fix: x/gov deposits querier (Initial Deposit) (#9288))