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

Missing cli tests for distr and mint #6349

Merged
merged 23 commits into from
Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
1cb0065
added missing cli tests for distribution module
atheeshp Jun 5, 2020
a00ea6d
added tests for mint module
atheeshp Jun 5, 2020
ad782fb
changed debugging code
atheeshp Jun 5, 2020
7b50a8a
review changes
atheeshp Jun 8, 2020
d41fe3b
review change
atheeshp Jun 8, 2020
1948053
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/mi…
atheeshp Jun 8, 2020
2e05bed
fixed distribution slashes command issue
atheeshp Jun 8, 2020
427bb9a
review changes
atheeshp Jun 9, 2020
a2edc77
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/mi…
atheeshp Jun 9, 2020
3d5547c
added fund community pool txn and query
atheeshp Jun 9, 2020
73bdf82
updated fund community pool command test
atheeshp Jun 9, 2020
800a5c2
staking file revert
atheeshp Jun 9, 2020
a5f8661
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/mi…
atheeshp Jun 9, 2020
c3e3d72
Merge branch 'master' into atheesh/missing-cli-tests
alexanderbez Jun 10, 2020
d8dc4b2
review changes
atheeshp Jun 10, 2020
e27fb4f
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/mi…
atheeshp Jun 10, 2020
fd1b104
removed consoles
atheeshp Jun 10, 2020
c75c9cd
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/mi…
atheeshp Jun 10, 2020
dd7c927
revew changes
atheeshp Jun 10, 2020
18e0bfc
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/mi…
atheeshp Jun 10, 2020
99a0a6d
Review changes
atheeshp Jun 11, 2020
7f611cc
Merge branch 'master' of github.com:cosmos/cosmos-sdk into atheesh/mi…
atheeshp Jun 11, 2020
9e3d79c
Merge branch 'master' into atheesh/missing-cli-tests
Jun 11, 2020
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
78 changes: 73 additions & 5 deletions x/distribution/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/stretchr/testify/require"
tmtypes "github.com/tendermint/tendermint/types"

"github.com/cosmos/cosmos-sdk/tests"
"github.com/cosmos/cosmos-sdk/tests/cli"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/distribution/client/testutil"
Expand Down Expand Up @@ -40,18 +41,85 @@ func TestCLIWithdrawRewards(t *testing.T) {
proc := f.SDStart()
t.Cleanup(func() { proc.Stop(false) })

params := testutil.QueryParameters(f)
require.NotEmpty(t, params)

fooAddr := f.KeyAddress(cli.KeyFoo)
barAddr := f.KeyAddress(cli.KeyBar)
fooVal := sdk.ValAddress(fooAddr)

outStandingRewards := testutil.QueryValidatorOutstandingRewards(f, fooVal.String())
require.NotEmpty(t, outStandingRewards)
require.False(t, outStandingRewards.Rewards.IsZero())

commission := testutil.QueryCommission(f, fooVal.String())
require.NotEmpty(t, commission)
require.False(t, commission.Commission.IsZero())

rewards := testutil.QueryRewards(f, fooAddr)
require.Equal(t, 1, len(rewards.Rewards))
require.NotNil(t, rewards.Total)
require.Len(t, rewards.Rewards, 1)
require.NotEmpty(t, rewards.Total)

fooVal := sdk.ValAddress(fooAddr)
// withdrawing rewards of a delegation for a single validator
success := testutil.TxWithdrawRewards(f, fooVal, fooAddr.String(), "-y")
require.True(t, success)

rewards = testutil.QueryRewards(f, fooAddr)
require.Equal(t, 1, len(rewards.Rewards))

require.Len(t, rewards.Rewards, 1)
require.Nil(t, rewards.Total)

// Setting up a new withdraw address
success, stdout, stderr := testutil.TxSetWithdrawAddress(f, fooAddr.String(), barAddr.String(), "--generate-only")
require.True(f.T, success)
require.Empty(f.T, stderr)

msg := cli.UnmarshalStdTx(f.T, f.Cdc, stdout)
require.NotZero(t, msg.Fee.Gas)
require.Len(t, msg.Msgs, 1)
require.Len(t, msg.GetSignatures(), 0)

success, _, stderr = testutil.TxSetWithdrawAddress(f, cli.KeyFoo, barAddr.String(), "-y")
require.True(f.T, success)
require.Empty(f.T, stderr)
tests.WaitForNextNBlocksTM(1, f.Port)

// Withdraw all delegation rewards from all validators
success, stdout, stderr = testutil.TxWithdrawAllRewards(f, fooAddr.String(), "--generate-only")
require.True(f.T, success)
require.Empty(f.T, stderr)

msg = cli.UnmarshalStdTx(f.T, f.Cdc, stdout)
require.NotZero(t, msg.Fee.Gas)
require.Len(t, msg.Msgs, 1)
require.Len(t, msg.GetSignatures(), 0)

success, _, stderr = testutil.TxWithdrawAllRewards(f, cli.KeyFoo, "-y")
require.True(f.T, success)
require.Empty(f.T, stderr)
tests.WaitForNextNBlocksTM(1, f.Port)

newTokens := sdk.NewCoin(cli.Denom, sdk.TokensFromConsensusPower(1))

// Withdraw all delegation rewards from all validators
success, stdout, stderr = testutil.TxFundCommunityPool(f, fooAddr.String(), newTokens, "--generate-only")
require.True(f.T, success)
atheeshp marked this conversation as resolved.
Show resolved Hide resolved
require.Empty(f.T, stderr)

msg = cli.UnmarshalStdTx(f.T, f.Cdc, stdout)
require.NotZero(t, msg.Fee.Gas)
require.Len(t, msg.Msgs, 1)
require.Len(t, msg.GetSignatures(), 0)

success, _, stderr = testutil.TxFundCommunityPool(f, cli.KeyFoo, newTokens, "-y")
require.True(f.T, success)
require.Empty(f.T, stderr)
tests.WaitForNextNBlocksTM(1, f.Port)

amount := testutil.QueryCommunityPool(f)
require.False(f.T, amount.IsZero())

slashes := testutil.QuerySlashes(f, fooVal.String())
require.Nil(t, slashes, nil)

f.Cleanup()
}
6 changes: 4 additions & 2 deletions x/distribution/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,10 @@ $ %s query distribution slashes cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fxs9ldmq
return err
}

var slashes types.ValidatorSlashEvents
cdc.MustUnmarshalJSON(res, &slashes)
var slashes []types.ValidatorSlashEvent
if err = cdc.UnmarshalJSON(res, &slashes); err != nil {
return fmt.Errorf("failed to unmarshal response: %w", err)
}
return clientCtx.PrintOutput(slashes)
},
}
Expand Down
85 changes: 85 additions & 0 deletions x/distribution/client/testutil/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,30 @@ import (
"github.com/cosmos/cosmos-sdk/x/distribution"
)

// TxWithdrawRewards raises a txn to withdraw rewards
func TxWithdrawRewards(f *cli.Fixtures, valAddr sdk.ValAddress, from string, flags ...string) bool {
cmd := fmt.Sprintf("%s tx distribution withdraw-rewards %s %v --keyring-backend=test --from=%s", f.SimcliBinary, valAddr, f.Flags(), from)
return cli.ExecuteWrite(f.T, cli.AddFlags(cmd, flags), clientkeys.DefaultKeyPass)
}

// TxSetWithdrawAddress helps to set the withdraw address for rewards associated with a delegator address
func TxSetWithdrawAddress(f *cli.Fixtures, from, withDrawAddr string, flags ...string) (bool, string, string) {
cmd := fmt.Sprintf("%s tx distribution set-withdraw-addr %s --from %s %v --keyring-backend=test", f.SimcliBinary, withDrawAddr, from, f.Flags())
return cli.ExecuteWriteRetStdStreams(f.T, cli.AddFlags(cmd, flags), clientkeys.DefaultKeyPass)
}

// TxWithdrawAllRewards raises a txn to withdraw all rewards of a delegator address
func TxWithdrawAllRewards(f *cli.Fixtures, from string, flags ...string) (bool, string, string) {
cmd := fmt.Sprintf("%s tx distribution withdraw-all-rewards %v --keyring-backend=test --from=%s", f.SimcliBinary, f.Flags(), from)
return cli.ExecuteWriteRetStdStreams(f.T, cli.AddFlags(cmd, flags), clientkeys.DefaultKeyPass)
}

// TxFundCommunityPool Funds the community pool with the specified amount
func TxFundCommunityPool(f *cli.Fixtures, from string, amount sdk.Coin, flags ...string) (bool, string, string) {
cmd := fmt.Sprintf("%s tx distribution fund-community-pool %v %v --keyring-backend=test --from=%s", f.SimcliBinary, amount, f.Flags(), from)
return cli.ExecuteWriteRetStdStreams(f.T, cli.AddFlags(cmd, flags), clientkeys.DefaultKeyPass)
}

// QueryRewards returns the rewards of a delegator
func QueryRewards(f *cli.Fixtures, delAddr sdk.AccAddress, flags ...string) distribution.QueryDelegatorTotalRewardsResponse {
cmd := fmt.Sprintf("%s query distribution rewards %s %s", f.SimcliBinary, delAddr, f.Flags())
Expand All @@ -25,6 +44,72 @@ func QueryRewards(f *cli.Fixtures, delAddr sdk.AccAddress, flags ...string) dist

var rewards distribution.QueryDelegatorTotalRewardsResponse
err := f.Cdc.UnmarshalJSON([]byte(res), &rewards)
atheeshp marked this conversation as resolved.
Show resolved Hide resolved
f.T.Log(fmt.Sprintf("\n out %v\n err %v", res, err))
atheeshp marked this conversation as resolved.
Show resolved Hide resolved
require.NoError(f.T, err)
atheeshp marked this conversation as resolved.
Show resolved Hide resolved
return rewards
}

// QueryValidatorOutstandingRewards distribution outstanding (un-withdrawn) rewards
func QueryValidatorOutstandingRewards(f *cli.Fixtures, valAddr string) distribution.ValidatorOutstandingRewards {
cmd := fmt.Sprintf("%s query distribution validator-outstanding-rewards %s %v", f.SimcliBinary, valAddr, f.Flags())
res, errStr := tests.ExecuteT(f.T, cmd, "")
require.Empty(f.T, errStr)

var outstandingRewards distribution.ValidatorOutstandingRewards
err := f.Cdc.UnmarshalJSON([]byte(res), &outstandingRewards)
atheeshp marked this conversation as resolved.
Show resolved Hide resolved
f.T.Log(fmt.Sprintf("\n out %v\n err %v", res, err))
require.NoError(f.T, err)
atheeshp marked this conversation as resolved.
Show resolved Hide resolved
return outstandingRewards
}

// QueryParameters is simcli query distribution parameters
func QueryParameters(f *cli.Fixtures, flags ...string) distribution.Params {
cmd := fmt.Sprintf("%s query distribution params %v", f.SimcliBinary, f.Flags())
out, errStr := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
require.Empty(f.T, errStr)

var params distribution.Params
err := f.Cdc.UnmarshalJSON([]byte(out), &params)
f.T.Log(fmt.Sprintf("\n out %v\n err %v", out, err))
require.NoError(f.T, err)
return params
}

// QueryCommission returns validator commission rewards from delegators to that validator.
func QueryCommission(f *cli.Fixtures, valAddr string, flags ...string) distribution.ValidatorAccumulatedCommission {
cmd := fmt.Sprintf("%s query distribution commission %s %v", f.SimcliBinary, valAddr, f.Flags())
out, errStr := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
require.Empty(f.T, errStr)

var commission distribution.ValidatorAccumulatedCommission
err := f.Cdc.UnmarshalJSON([]byte(out), &commission)
atheeshp marked this conversation as resolved.
Show resolved Hide resolved
f.T.Log(fmt.Sprintf("\n out %v\n err %v", out, err))
require.NoError(f.T, err)
atheeshp marked this conversation as resolved.
Show resolved Hide resolved
return commission
}

// QuerySlashes returns all slashes of a validator for a given block range.
func QuerySlashes(f *cli.Fixtures, valAddr string, flags ...string) []distribution.ValidatorSlashEvent {
cmd := fmt.Sprintf("%s query distribution slashes %s 0 5 %v ", f.SimcliBinary, valAddr, f.Flags())
out, errStr := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
require.Empty(f.T, errStr)

var slashes []distribution.ValidatorSlashEvent
err := f.Cdc.UnmarshalJSON([]byte(out), &slashes)
atheeshp marked this conversation as resolved.
Show resolved Hide resolved
f.T.Log(fmt.Sprintf("\n out %v\n err %v", out, err))
require.NoError(f.T, err)
atheeshp marked this conversation as resolved.
Show resolved Hide resolved
return slashes
}

// QueryCommunityPool returns the amount of coins in the community pool
func QueryCommunityPool(f *cli.Fixtures, flags ...string) sdk.DecCoins {
cmd := fmt.Sprintf("%s query distribution community-pool %v ", f.SimcliBinary, f.Flags())
out, errStr := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
require.Empty(f.T, errStr)

var amount sdk.DecCoins
err := f.Cdc.UnmarshalJSON([]byte(out), &amount)
atheeshp marked this conversation as resolved.
Show resolved Hide resolved
f.T.Log(fmt.Sprintf("\n out %v\n err %v", out, err))
require.NoError(f.T, err)
atheeshp marked this conversation as resolved.
Show resolved Hide resolved
return amount
}
28 changes: 28 additions & 0 deletions x/mint/client/cli/cli_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// +build cli_test

package cli_test

import (
"testing"

"github.com/cosmos/cosmos-sdk/tests/cli"
"github.com/cosmos/cosmos-sdk/x/mint/client/testutil"
"github.com/stretchr/testify/require"
)

func TestCLIMintQueries(t *testing.T) {
t.Parallel()
f := cli.InitFixtures(t)

proc := f.SDStart()
t.Cleanup(func() { proc.Stop(false) })

params := testutil.QueryMintingParams(f)
require.NotEmpty(t, params)

inflation := testutil.QueryInflation(f)
require.False(t, inflation.IsZero())

annualProvisions := testutil.QueryAnnualProvisions(f)
require.False(t, annualProvisions.IsZero())
}
50 changes: 50 additions & 0 deletions x/mint/client/testutil/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package testutil

import (
"fmt"

"github.com/cosmos/cosmos-sdk/tests"
"github.com/cosmos/cosmos-sdk/tests/cli"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/mint/types"
"github.com/stretchr/testify/require"
)

// QueryMintingParams returns the current minting parameters
func QueryMintingParams(f *cli.Fixtures, flags ...string) types.Params {
cmd := fmt.Sprintf("%s query mint params %v", f.SimcliBinary, f.Flags())
out, errStr := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
require.Empty(f.T, errStr)

var params types.Params
err := f.Cdc.UnmarshalJSON([]byte(out), &params)
f.T.Log(fmt.Sprintf("\n out %v\n err %v", out, err))
require.NoError(f.T, err)
return params
}

// QueryInflation returns the current minting inflation value
func QueryInflation(f *cli.Fixtures, flags ...string) sdk.Dec {
cmd := fmt.Sprintf("%s query mint inflation %v", f.SimcliBinary, f.Flags())
out, errStr := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
require.Empty(f.T, errStr)

var inflation sdk.Dec
err := f.Cdc.UnmarshalJSON([]byte(out), &inflation)
f.T.Log(fmt.Sprintf("\n out %v\n err %v", out, err))
require.NoError(f.T, err)
return inflation
}

// QueryAnnualProvisions returns the current minting annual provisions value
func QueryAnnualProvisions(f *cli.Fixtures, flags ...string) sdk.Dec {
cmd := fmt.Sprintf("%s query mint annual-provisions %v", f.SimcliBinary, f.Flags())
out, errStr := tests.ExecuteT(f.T, cli.AddFlags(cmd, flags), "")
require.Empty(f.T, errStr)

var annualProvisions sdk.Dec
err := f.Cdc.UnmarshalJSON([]byte(out), &annualProvisions)
atheeshp marked this conversation as resolved.
Show resolved Hide resolved
f.T.Log(fmt.Sprintf("\n out %v\n err %v", out, err))
require.NoError(f.T, err)
atheeshp marked this conversation as resolved.
Show resolved Hide resolved
return annualProvisions
}