Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: x/gov deposits querier (Initial Deposit) #9288

Merged
merged 26 commits into from
Jun 3, 2021
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
6ea333c
copied from old PR
aleem1314 May 7, 2021
0de858b
fix errors
aleem1314 May 7, 2021
64232e8
add test
aleem1314 May 10, 2021
e185e33
Merge branch 'master' into aleem/8758-gov-deposits
aleem1314 May 10, 2021
0d7b764
Update x/gov/client/utils/query.go
aleem1314 May 10, 2021
5d8cf40
fix tests
aleem1314 May 10, 2021
6e81021
Merge branch 'aleem/8758-gov-deposits' of https://github.com/cosmos/c…
aleem1314 May 10, 2021
8b4d075
Merge branch 'master' into aleem/8758-gov-deposits
aleem1314 May 10, 2021
0048d2a
fix failing test
aleem1314 May 10, 2021
2a69de4
add test
aleem1314 May 11, 2021
12457b9
update test
aleem1314 May 11, 2021
22fe967
fix tests
aleem1314 May 11, 2021
a1daaa4
fix deposit query
aleem1314 May 12, 2021
88e4868
fix test
aleem1314 May 13, 2021
ef11cae
update tests
aleem1314 May 27, 2021
ff024ce
Merge branch 'master' into aleem/8758-gov-deposits
aleem1314 May 27, 2021
1424885
Merge branch 'master' into aleem/8758-gov-deposits
aleem1314 May 27, 2021
c74c3b8
Merge branch 'master' into aleem/8758-gov-deposits
aleem1314 May 28, 2021
fdbc334
add more tests
aleem1314 May 31, 2021
5ad7a69
address lint error
aleem1314 May 31, 2021
1725f6e
address lint error
aleem1314 May 31, 2021
f4fab49
Merge branch 'master' into aleem/8758-gov-deposits
aleem1314 May 31, 2021
7dc95c1
review changes
aleem1314 Jun 1, 2021
8175663
Merge branch 'aleem/8758-gov-deposits' of https://github.com/cosmos/c…
aleem1314 Jun 1, 2021
fe6487d
Merge branch 'master' into aleem/8758-gov-deposits
aleem1314 Jun 2, 2021
4123e05
Merge branch 'master' into aleem/8758-gov-deposits
aleem1314 Jun 3, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ if input key is empty, or input data contains empty key.

### Bug Fixes

* (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
Expand Down
26 changes: 14 additions & 12 deletions x/gov/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ $ %s query gov deposit 1 cosmos1skjwj5whet0lpe65qaq4rpq03hjxlwd9nf39lk

// check to see if the proposal is in the store
ctx := cmd.Context()
_, err = queryClient.Proposal(
proposalRes, err := queryClient.Proposal(
ctx,
&types.QueryProposalRequest{ProposalId: proposalID},
)
Expand All @@ -379,25 +379,27 @@ $ %s query gov deposit 1 cosmos1skjwj5whet0lpe65qaq4rpq03hjxlwd9nf39lk
return err
}

res, err := queryClient.Deposit(
ctx,
&types.QueryDepositRequest{ProposalId: proposalID, Depositor: args[1]},
)
if err != nil {
return err
}

deposit := res.GetDeposit()
if deposit.Empty() {
var deposit types.Deposit
propStatus := proposalRes.Proposal.Status
if !(propStatus == types.StatusVotingPeriod || propStatus == types.StatusDepositPeriod) {
params := types.NewQueryDepositParams(proposalID, depositorAddr)
resByTxQuery, err := gcutils.QueryDepositByTxQuery(clientCtx, params)
if err != nil {
return err
}
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
}

return clientCtx.PrintProto(&deposit)
return clientCtx.PrintProto(&res.Deposit)
},
}

Expand Down
12 changes: 12 additions & 0 deletions x/gov/client/testutil/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,26 @@ 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))
}
142 changes: 142 additions & 0 deletions x/gov/client/testutil/deposits.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package testutil

import (
"fmt"
"time"

"github.com/cosmos/cosmos-sdk/client/flags"
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
args := []string{
"1",
sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(50)).String(),
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
fmt.Sprintf("--%s=%s", flags.FlagFees, s.fees),
}
cmd := cli.NewCmdDeposit()
_, err = clitestutil.ExecTestCLICmd(clientCtx, cmd, args)
s.Require().NoError(err)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's query the deposit before the voting as well.

// waiting for voting period to end
time.Sleep(20 * time.Second)

// query deposit
args = []string{"1", val.Address.String(), fmt.Sprintf("--%s=json", tmcli.OutputFlag)}
var depositRes types.Deposit
cmd = cli.GetCmdQueryDeposit()
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, args)
s.Require().NoError(err)
s.Require().NoError(val.ClientCtx.LegacyAmino.UnmarshalJSON(out.Bytes(), &depositRes))

// verify initial deposit
s.Require().Equal(depositRes.Amount.String(), initialDeposit)

// query deposits
args = []string{"1", fmt.Sprintf("--%s=json", tmcli.OutputFlag)}
var depositsRes types.Deposits
cmd = cli.GetCmdQueryDeposits()
out, err = clitestutil.ExecTestCLICmd(clientCtx, cmd, args)
s.Require().NoError(err)
s.Require().NoError(val.ClientCtx.LegacyAmino.UnmarshalJSON(out.Bytes(), &depositsRes))
s.Require().Equal(len(depositsRes), 2)
// verify initial deposit
s.Require().Equal(depositsRes[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)

_, err = s.network.WaitForHeight(1)
s.Require().NoError(err)

// deposit amount
args := []string{
"2",
sdk.NewCoin(s.cfg.BondDenom, types.DefaultMinDepositTokens.Add(sdk.NewInt(50))).String(),
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()),
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
fmt.Sprintf("--%s=%s", flags.FlagFees, s.fees),
}
cmd := cli.NewCmdDeposit()
_, err = clitestutil.ExecTestCLICmd(clientCtx, cmd, args)
s.Require().NoError(err)

// waiting for voting period to end
time.Sleep(20 * time.Second)

// query deposit
var depositRes types.Deposit
args = []string{"2", val.Address.String(), fmt.Sprintf("--%s=json", tmcli.OutputFlag)}
cmd = cli.GetCmdQueryDeposit()
out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, args)
s.Require().NoError(err)
s.Require().NoError(val.ClientCtx.LegacyAmino.UnmarshalJSON(out.Bytes(), &depositRes))
s.Require().Equal(depositRes.Amount.String(), sdk.NewCoin(s.cfg.BondDenom, types.DefaultMinDepositTokens.Add(sdk.NewInt(50))).String())

// query deposits
args = []string{"2", fmt.Sprintf("--%s=json", tmcli.OutputFlag)}
var depositsRes types.Deposits
cmd = cli.GetCmdQueryDeposits()
out, err = clitestutil.ExecTestCLICmd(clientCtx, cmd, args)
s.Require().NoError(err)
s.Require().NoError(val.ClientCtx.LegacyAmino.UnmarshalJSON(out.Bytes(), &depositsRes))
s.Require().Equal(len(depositsRes), 1)
// verify initial deposit
s.Require().Equal(depositsRes[0].Amount.String(), sdk.NewCoin(s.cfg.BondDenom, types.DefaultMinDepositTokens.Add(sdk.NewInt(50))).String())
}
68 changes: 66 additions & 2 deletions x/gov/client/utils/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/cosmos/cosmos-sdk/client"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
"github.com/cosmos/cosmos-sdk/x/gov/types"
)
Expand Down Expand Up @@ -37,6 +38,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 separately
aleem1314 marked this conversation as resolved.
Show resolved Hide resolved
initialDeposit, err := queryInitialDepositByTxQuery(clientCtx, params.ProposalID)
if err != nil {
return nil, err
}

if !initialDeposit.Amount.IsZero() {
deposits = append(deposits, initialDeposit)
aleem1314 marked this conversation as resolved.
Show resolved Hide resolved
}

searchResult, err := combineEvents(
clientCtx, defaultPage,
// Query legacy Msgs event action
Expand All @@ -54,8 +67,6 @@ func QueryDepositsByTxQuery(clientCtx client.Context, params types.QueryProposal
return nil, err
}

var deposits []types.Deposit

for _, info := range searchResult.Txs {
for _, msg := range info.GetTx().GetMsgs() {
if depMsg, ok := msg.(*types.MsgDeposit); ok {
Expand Down Expand Up @@ -226,6 +237,22 @@ 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) {

// 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
Expand Down Expand Up @@ -338,3 +365,40 @@ func combineEvents(clientCtx client.Context, page int, eventGroups ...[]string)

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))),
},
)
aleem1314 marked this conversation as resolved.
Show resolved Hide resolved

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)
}