From cd7e939e5f781f685aba05056c6c095dbc5eca69 Mon Sep 17 00:00:00 2001 From: Yun Yeo Date: Mon, 8 Mar 2021 14:53:30 +0900 Subject: [PATCH 1/6] add initial deposit query to QueryDepositsByTxQuery query utils --- x/gov/client/utils/query.go | 64 ++++++++++++++++++++++++++++++++++--- 1 file changed, 59 insertions(+), 5 deletions(-) diff --git a/x/gov/client/utils/query.go b/x/gov/client/utils/query.go index 9b103e3468c9..cdc4842c832f 100644 --- a/x/gov/client/utils/query.go +++ b/x/gov/client/utils/query.go @@ -37,6 +37,18 @@ 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) { + var deposits []types.Deposit + + // initial deposit was submitted with proposal, so must be queried seperately + initialDeposit, err := QueryInitialDepositByTxQuery(clientCtx, params.ProposalID) + if err != nil { + return nil, err + } + + if !initialDeposit.Amount.IsZero() { + deposits = append(deposits, initialDeposit) + } + searchResult, err := combineEvents( clientCtx, defaultPage, // Query old Msgs @@ -50,11 +62,6 @@ func QueryDepositsByTxQuery(clientCtx client.Context, params types.QueryProposal fmt.Sprintf("%s.%s='%s'", types.EventTypeProposalDeposit, types.AttributeKeyProposalID, []byte(fmt.Sprintf("%d", params.ProposalID))), }, ) - if err != nil { - return nil, err - } - - var deposits []types.Deposit for _, info := range searchResult.Txs { for _, msg := range info.GetTx().GetMsgs() { @@ -346,6 +353,53 @@ func QueryProposerByTxQuery(clientCtx client.Context, proposalID uint64) (Propos return Proposer{}, fmt.Errorf("failed to find the proposer for proposalID %d", proposalID) } +// 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 old Msgs + []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 service Msgs + []string{ + fmt.Sprintf("%s.%s='%s'", sdk.EventTypeMessage, sdk.AttributeKeyAction, types.TypeSvcMsgSubmitProposal), + 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 msg.Type() == types.TypeSvcMsgSubmitProposal { + subMsg := msg.(sdk.ServiceMsg).Request.(*types.MsgSubmitProposal) + + return types.Deposit{ + ProposalId: proposalID, + Depositor: subMsg.Proposer, + Amount: subMsg.InitialDeposit, + }, nil + } else if protoSubMsg, ok := msg.(*types.MsgSubmitProposal); ok { + subMsg := protoSubMsg + + return types.Deposit{ + ProposalId: proposalID, + Depositor: subMsg.Proposer, + Amount: subMsg.InitialDeposit, + }, nil + } + } + } + + return types.Deposit{}, fmt.Errorf("failed to find the initial deposit for proposalID %d", proposalID) +} + // QueryProposalByID takes a proposalID and returns a proposal func QueryProposalByID(proposalID uint64, clientCtx client.Context, queryRoute string) ([]byte, error) { params := types.NewQueryProposalParams(proposalID) From fe4e92aafdfa6b632660e3387cb5e000d55fabdf Mon Sep 17 00:00:00 2001 From: Yun Yeo Date: Mon, 8 Mar 2021 14:55:48 +0900 Subject: [PATCH 2/6] restore error if statement --- x/gov/client/utils/query.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/x/gov/client/utils/query.go b/x/gov/client/utils/query.go index cdc4842c832f..b964fee7db6e 100644 --- a/x/gov/client/utils/query.go +++ b/x/gov/client/utils/query.go @@ -62,6 +62,9 @@ func QueryDepositsByTxQuery(clientCtx client.Context, params types.QueryProposal fmt.Sprintf("%s.%s='%s'", types.EventTypeProposalDeposit, types.AttributeKeyProposalID, []byte(fmt.Sprintf("%d", params.ProposalID))), }, ) + if err != nil { + return nil, err + } for _, info := range searchResult.Txs { for _, msg := range info.GetTx().GetMsgs() { From 60ca73b222932309e1fb3f3f885305a3be851277 Mon Sep 17 00:00:00 2001 From: Yun Yeo Date: Mon, 8 Mar 2021 14:59:13 +0900 Subject: [PATCH 3/6] remove duplicated code --- x/gov/client/utils/query.go | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/x/gov/client/utils/query.go b/x/gov/client/utils/query.go index b964fee7db6e..68ad0064d7ec 100644 --- a/x/gov/client/utils/query.go +++ b/x/gov/client/utils/query.go @@ -380,23 +380,18 @@ func QueryInitialDepositByTxQuery(clientCtx client.Context, proposalID uint64) ( for _, info := range searchResult.Txs { for _, msg := range info.GetTx().GetMsgs() { // there should only be a single proposal under the given conditions + var subMsg *types.MsgSubmitProposal if msg.Type() == types.TypeSvcMsgSubmitProposal { - subMsg := msg.(sdk.ServiceMsg).Request.(*types.MsgSubmitProposal) - - return types.Deposit{ - ProposalId: proposalID, - Depositor: subMsg.Proposer, - Amount: subMsg.InitialDeposit, - }, nil + subMsg = msg.(sdk.ServiceMsg).Request.(*types.MsgSubmitProposal) } else if protoSubMsg, ok := msg.(*types.MsgSubmitProposal); ok { - subMsg := protoSubMsg - - return types.Deposit{ - ProposalId: proposalID, - Depositor: subMsg.Proposer, - Amount: subMsg.InitialDeposit, - }, nil + subMsg = protoSubMsg } + + return types.Deposit{ + ProposalId: proposalID, + Depositor: subMsg.Proposer, + Amount: subMsg.InitialDeposit, + }, nil } } From 145a720813208321fc227ceb6c5b393a47eed25d Mon Sep 17 00:00:00 2001 From: Yun Yeo Date: Mon, 8 Mar 2021 15:09:22 +0900 Subject: [PATCH 4/6] typofix + changelog --- CHANGELOG.md | 1 + x/gov/client/utils/query.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 972be3a80771..689e055038bf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -85,6 +85,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (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 * (server) [\#8399](https://github.com/cosmos/cosmos-sdk/pull/8399) fix gRPC-web flag default value +* (x/gov) [\#8813](https://github.com/cosmos/cosmos-sdk/pull/8813) fix `GET /cosmos/gov/v1beta1/proposals/{proposal_id}/deposits` to include initial deposit ## [v0.41.4](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.41.3) - 2021-03-02 diff --git a/x/gov/client/utils/query.go b/x/gov/client/utils/query.go index 68ad0064d7ec..d10637fa003d 100644 --- a/x/gov/client/utils/query.go +++ b/x/gov/client/utils/query.go @@ -39,7 +39,7 @@ func (p Proposer) String() string { func QueryDepositsByTxQuery(clientCtx client.Context, params types.QueryProposalParams) ([]byte, error) { var deposits []types.Deposit - // initial deposit was submitted with proposal, so must be queried seperately + // initial deposit was submitted with proposal, so must be queried separately initialDeposit, err := QueryInitialDepositByTxQuery(clientCtx, params.ProposalID) if err != nil { return nil, err From 604c00f4b2f19f25af2159d51a2d5dae8118a4eb Mon Sep 17 00:00:00 2001 From: Yun Yeo Date: Wed, 10 Mar 2021 20:25:28 +0900 Subject: [PATCH 5/6] add for loop escape condition --- x/gov/client/utils/query.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/x/gov/client/utils/query.go b/x/gov/client/utils/query.go index d10637fa003d..fc4ad6d8500b 100644 --- a/x/gov/client/utils/query.go +++ b/x/gov/client/utils/query.go @@ -387,11 +387,13 @@ func QueryInitialDepositByTxQuery(clientCtx client.Context, proposalID uint64) ( subMsg = protoSubMsg } - return types.Deposit{ - ProposalId: proposalID, - Depositor: subMsg.Proposer, - Amount: subMsg.InitialDeposit, - }, nil + if subMsg != nil { + return types.Deposit{ + ProposalId: proposalID, + Depositor: subMsg.Proposer, + Amount: subMsg.InitialDeposit, + }, nil + } } } From 045268cf1a6d9c37e9d752b4aa08d61c442695fe Mon Sep 17 00:00:00 2001 From: Yun Yeo Date: Thu, 11 Mar 2021 12:23:54 +0900 Subject: [PATCH 6/6] change unnecessary public method to private --- x/gov/client/utils/query.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/x/gov/client/utils/query.go b/x/gov/client/utils/query.go index fc4ad6d8500b..9d8ebb34f0a6 100644 --- a/x/gov/client/utils/query.go +++ b/x/gov/client/utils/query.go @@ -40,7 +40,7 @@ func QueryDepositsByTxQuery(clientCtx client.Context, params types.QueryProposal var deposits []types.Deposit // initial deposit was submitted with proposal, so must be queried separately - initialDeposit, err := QueryInitialDepositByTxQuery(clientCtx, params.ProposalID) + initialDeposit, err := queryInitialDepositByTxQuery(clientCtx, params.ProposalID) if err != nil { return nil, err } @@ -356,9 +356,9 @@ func QueryProposerByTxQuery(clientCtx client.Context, proposalID uint64) (Propos return Proposer{}, fmt.Errorf("failed to find the proposer for proposalID %d", proposalID) } -// QueryInitialDepositByTxQuery will query for a initial deposit of a governance proposal by +// queryInitialDepositByTxQuery will query for a initial deposit of a governance proposal by // ID. -func QueryInitialDepositByTxQuery(clientCtx client.Context, proposalID uint64) (types.Deposit, error) { +func queryInitialDepositByTxQuery(clientCtx client.Context, proposalID uint64) (types.Deposit, error) { searchResult, err := combineEvents( clientCtx, defaultPage, // Query old Msgs