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

R4R: Ensure all CLI queries respect output flags #3320

Merged
merged 8 commits into from
Jan 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
24 changes: 18 additions & 6 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ BREAKING CHANGES
* [\#2222] all endpoints renamed from `/stake` -> `/staking`
* [\#1268] `LooseTokens` -> `NotBondedTokens`
* [\#3289] misc renames:
* `Validator.UnbondingMinTime` -> `Validator.UnbondingCompletionTime`
* `Delegation` -> `Value` in `MsgCreateValidator` and `MsgDelegate`
* `Validator.UnbondingMinTime` -> `Validator.UnbondingCompletionTime`
* `Delegation` -> `Value` in `MsgCreateValidator` and `MsgDelegate`
* `MsgBeginUnbonding` -> `MsgUndelegate`

* Gaia CLI (`gaiacli`)
Expand All @@ -19,6 +19,7 @@ BREAKING CHANGES
* [\#3069](https://github.com/cosmos/cosmos-sdk/pull/3069) `--fee` flag renamed to `--fees` to support multiple coins
* [\#3156](https://github.com/cosmos/cosmos-sdk/pull/3156) Remove unimplemented `gaiacli init` command
* [\#2222] `gaiacli tx stake` -> `gaiacli tx staking`, `gaiacli query stake` -> `gaiacli query staking`
* [\#3320](https://github.com/cosmos/cosmos-sdk/pull/3320) Ensure all `gaiacli query` commands respect the `--output` and `--indent` flags

* Gaia
* https://github.com/cosmos/cosmos-sdk/issues/2838 - Move store keys to constants
Expand All @@ -32,14 +33,21 @@ BREAKING CHANGES
* [\#3064](https://github.com/cosmos/cosmos-sdk/issues/3064) Sanitize `sdk.Coin` denom. Coins denoms are now case insensitive, i.e. 100fooToken equals to 100FOOTOKEN.
* [\#3195](https://github.com/cosmos/cosmos-sdk/issues/3195) Allows custom configuration for syncable strategy
* [\#3242](https://github.com/cosmos/cosmos-sdk/issues/3242) Fix infinite gas
<<<<<<< HEAD
Copy link
Contributor

Choose a reason for hiding this comment

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

@jackzampolin this doesn't look intentional - merge artifact?

Copy link
Member Author

Choose a reason for hiding this comment

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

can we take care of this in the release PR? 😬 sorry about this

meter utilization during aborted ante handler executions.
* [x/distribution] \#3292 Enable or disable withdraw addresses with a parameter in the param store
* [staking] \#2222 `/stake` -> `/staking` module rename
* [staking] \#1268 `LooseTokens` -> `NotBondedTokens`
* [staking] \#1402 Redelegation and unbonding-delegation structs changed to include multiple an array of entries
* [staking] \#1402 Redelegation and unbonding-delegation structs changed to include multiple an array of entries
=======
meter utilization during aborted ante handler executions.
* [\#2222] [x/staking] `/stake` -> `/staking` module rename
* \#3292 [x/distribution] Enable or disable withdraw addresses with a parameter in the param store
* [staking] \#1402 Redelegation and unbonding-delegation structs changed to include multiple an array of entries
>>>>>>> Pending.md
* [staking] \#3289 misc renames:
* `Validator.UnbondingMinTime` -> `Validator.UnbondingCompletionTime`
* `Delegation` -> `Value` in `MsgCreateValidator` and `MsgDelegate`
* `Validator.UnbondingMinTime` -> `Validator.UnbondingCompletionTime`
* `Delegation` -> `Value` in `MsgCreateValidator` and `MsgDelegate`
* `MsgBeginUnbonding` -> `MsgUndelegate`
* [\#3315] Increase decimal precision to 18
* [\#3328](https://github.com/cosmos/cosmos-sdk/issues/3328) [x/gov] Remove redundant action tag
Expand Down Expand Up @@ -114,8 +122,12 @@ IMPROVEMENTS
slashing, and staking modules.
* [\#3093](https://github.com/cosmos/cosmos-sdk/issues/3093) Ante handler does no longer read all accounts in one go when processing signatures as signature
verification may fail before last signature is checked.
* [staking] \#1402 Add for multiple simultaneous redelegations or unbonding-delegations within an unbonding period
<<<<<<< HEAD
* [staking] \#1402 Add for multiple simultaneous redelegations or unbonding-delegations within an unbonding period
* [staking] \#1268 staking spec rewrite
=======
* [x/stake] \#1402 Add for multiple simultaneous redelegations or unbonding-delegations within an unbonding period
>>>>>>> Pending.md

* Tendermint

Expand Down
27 changes: 27 additions & 0 deletions client/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type CLIContext struct {
AccDecoder auth.AccountDecoder
Client rpcclient.Client
Output io.Writer
OutputFormat string
Height int64
NodeURI string
From string
Expand Down Expand Up @@ -76,6 +77,7 @@ func NewCLIContext() CLIContext {
NodeURI: nodeURI,
AccountStore: auth.StoreKey,
From: viper.GetString(client.FlagFrom),
OutputFormat: viper.GetString(cli.OutputFlag),
Height: viper.GetInt64(client.FlagHeight),
TrustNode: viper.GetBool(client.FlagTrustNode),
UseLedger: viper.GetBool(client.FlagUseLedger),
Expand Down Expand Up @@ -254,3 +256,28 @@ func (ctx CLIContext) WithSimulation(simulate bool) CLIContext {
ctx.Simulate = simulate
return ctx
}

// PrintOutput prints output while respecting output and indent flags
// NOTE: pass in marshalled structs that have been unmarshaled
// because this function will panic on marshaling errors
func (ctx CLIContext) PrintOutput(toPrint fmt.Stringer) (err error) {
jackzampolin marked this conversation as resolved.
Show resolved Hide resolved
jackzampolin marked this conversation as resolved.
Show resolved Hide resolved
var out []byte

switch ctx.OutputFormat {
case "text":
out = []byte(toPrint.String())
case "json":
if ctx.Indent {
out, err = ctx.Codec.MarshalJSONIndent(toPrint, "", " ")
} else {
out, err = ctx.Codec.MarshalJSON(toPrint)
}
}

if err != nil {
return
}

fmt.Println(string(out))
return
}
1 change: 1 addition & 0 deletions client/tx/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ $ gaiacli query txs --tags '<tag1>:<value1>&<tag2>:<value2>' --page 1 --limit 30
cmd.Flags().String(flagTags, "", "tag:value list of tags that must match")
cmd.Flags().Int32(flagPage, defaultPage, "Query a specific page of paginated results")
cmd.Flags().Int32(flagLimit, defaultLimit, "Query number of transactions results per page returned")
cmd.MarkFlagRequired(flagTags)
alessio marked this conversation as resolved.
Show resolved Hide resolved
return cmd
}

Expand Down
10 changes: 5 additions & 5 deletions cmd/gaia/cli_test/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ func TestGaiaCLISubmitProposal(t *testing.T) {
require.Equal(t, int64(50), fooAcc.GetCoins().AmountOf(stakingTypes.DefaultBondDenom).Int64())

proposalsQuery := f.QueryGovProposals()
require.Equal(t, "No matching proposals found", proposalsQuery)
require.Empty(t, proposalsQuery)

// Test submit generate only for submit proposal
success, stdout, stderr := f.TxGovSubmitProposal(
Expand Down Expand Up @@ -418,7 +418,7 @@ func TestGaiaCLISubmitProposal(t *testing.T) {

// Ensure query proposals returns properly
proposalsQuery = f.QueryGovProposals()
require.Equal(t, " 1 - Test", proposalsQuery)
require.Equal(t, uint64(1), proposalsQuery[0].GetProposalID())

// Query the deposits on the proposal
deposit := f.QueryGovDeposit(1, fooAddr)
Expand Down Expand Up @@ -489,19 +489,19 @@ func TestGaiaCLISubmitProposal(t *testing.T) {

// Ensure no proposals in deposit period
proposalsQuery = f.QueryGovProposals("--status=DepositPeriod")
require.Equal(t, "No matching proposals found", proposalsQuery)
require.Empty(t, proposalsQuery)

// Ensure the proposal returns as in the voting period
proposalsQuery = f.QueryGovProposals("--status=VotingPeriod")
require.Equal(t, " 1 - Test", proposalsQuery)
require.Equal(t, uint64(1), proposalsQuery[0].GetProposalID())

// submit a second test proposal
f.TxGovSubmitProposal(keyFoo, "Text", "Apples", "test", sdk.NewInt64Coin(denom, 5))
tests.WaitForNextNBlocksTM(1, f.Port)

// Test limit on proposals query
proposalsQuery = f.QueryGovProposals("--limit=1")
require.Equal(t, " 2 - Apples", proposalsQuery)
require.Equal(t, uint64(2), proposalsQuery[0].GetProposalID())

f.Cleanup()
}
Expand Down
13 changes: 10 additions & 3 deletions cmd/gaia/cli_test/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ func (f *Fixtures) QueryStakingPool(flags ...string) staking.Pool {

// QueryStakingParameters is gaiacli query staking parameters
func (f *Fixtures) QueryStakingParameters(flags ...string) staking.Params {
cmd := fmt.Sprintf("gaiacli query staking parameters %v", f.Flags())
cmd := fmt.Sprintf("gaiacli query staking params %v", f.Flags())
out, _ := tests.ExecuteT(f.T, addFlags(cmd, flags), "")
var params staking.Params
cdc := app.MakeCodec()
Expand Down Expand Up @@ -426,11 +426,18 @@ func (f *Fixtures) QueryGovParamTallying() gov.TallyParams {
}

// QueryGovProposals is gaiacli query gov proposals
func (f *Fixtures) QueryGovProposals(flags ...string) string {
func (f *Fixtures) QueryGovProposals(flags ...string) gov.Proposals {
cmd := fmt.Sprintf("gaiacli query gov proposals %v", f.Flags())
stdout, stderr := tests.ExecuteT(f.T, addFlags(cmd, flags), "")
if strings.Contains(stderr, "No matching proposals found") {
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks @jackzampolin -- we should use error variables in the future (e.g. strings.Contains(stderr, gov.ErrNoMatchingProposals)

Copy link
Member Author

Choose a reason for hiding this comment

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

Absolutely. There is a bunch more that needs to be done to standardize errors across the various client software.

return gov.Proposals{}
}
require.Empty(f.T, stderr)
return stdout
var out gov.Proposals
cdc := app.MakeCodec()
err := cdc.UnmarshalJSON([]byte(stdout), &out)
require.NoError(f.T, err)
return out
}

// QueryGovProposal is gaiacli query gov proposal
Expand Down
25 changes: 12 additions & 13 deletions docs/gaia/gaiacli.md
Original file line number Diff line number Diff line change
Expand Up @@ -380,15 +380,13 @@ Don't use more `steak` thank you have! You can always get more by using the [Fau
Once submitted a delegation to a validator, you can see it's information by using the following command:

```bash
gaiacli query staking delegation \
--address-delegator=<account_cosmos> \
--validator=<account_cosmosval>
gaiacli query staking delegation <delegator_addr> <validator_addr>
```

Or if you want to check all your current delegations with disctinct validators:

```bash
gaiacli query staking delegations <account_cosmos>
gaiacli query staking delegations <delegator_addr>
```

You can also get previous delegation(s) status by adding the `--height` flag.
Expand All @@ -412,9 +410,7 @@ The unbonding will be automatically completed when the unbonding period has pass
Once you begin an unbonding-delegation, you can see it's information by using the following command:

```bash
gaiacli query staking unbonding-delegation \
--address-delegator=<account_cosmos> \
--validator=<account_cosmosval> \
gaiacli query staking unbonding-delegation <delegator_addr> <validator_addr>
```

Or if you want to check all your current unbonding-delegations with disctinct validators:
Expand All @@ -426,7 +422,7 @@ gaiacli query staking unbonding-delegations <account_cosmos>
Additionally, as you can get all the unbonding-delegations from a particular validator:

```bash
gaiacli query staking unbonding-delegations-from <account_cosmosval>
gaiacli query staking unbonding-delegations-from <account_cosmosval>
```

To get previous unbonding-delegation(s) status on past blocks, try adding the `--height` flag.
Expand All @@ -453,10 +449,7 @@ The redelegation will be automatically completed when the unbonding period has p
Once you begin an redelegation, you can see it's information by using the following command:

```bash
gaiacli query staking redelegation \
--address-delegator=<account_cosmos> \
--addr-validator-source=<account_cosmosval> \
--addr-validator-dest=<account_cosmosval> \
gaiacli query staking redelegation <delegator_addr> <src_val_addr> <dst_val_addr>
```

Or if you want to check all your current unbonding-delegations with disctinct validators:
Expand All @@ -478,7 +471,7 @@ To get previous redelegation(s) status on past blocks, try adding the `--height`
Parameters define high level settings for staking. You can get the current values by using:

```bash
gaiacli query staking parameters
gaiacli query staking params
```

With the above command you will get the values for:
Expand Down Expand Up @@ -629,6 +622,12 @@ gaiacli query gov tally <proposal_id>

To check the current governance parameters run:

```bash
gaiacli query gov params
jackzampolin marked this conversation as resolved.
Show resolved Hide resolved
```

To query subsets of the governance parameters run:

```bash
gaiacli query gov param voting
gaiacli query gov param tallying
Expand Down
46 changes: 46 additions & 0 deletions x/auth/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package auth

import (
"errors"
"fmt"
"time"

"github.com/tendermint/tendermint/crypto"
Expand Down Expand Up @@ -35,6 +36,9 @@ type Account interface {
// Calculates the amount of coins that can be sent to other accounts given
// the current time.
SpendableCoins(blockTime time.Time) sdk.Coins

// Ensure that account implements stringer
String() string
jackzampolin marked this conversation as resolved.
Show resolved Hide resolved
}

// VestingAccount defines an account type that vests coins via a vesting schedule.
Expand Down Expand Up @@ -74,6 +78,16 @@ type BaseAccount struct {
Sequence uint64 `json:"sequence"`
}

// String implements fmt.Stringer
func (acc BaseAccount) String() string {
jackzampolin marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Sprintf(`Account %s:
Coins: %s
PubKey: %s
AccountNumber: %d
Sequence: %d`, acc.Address, acc.Coins,
acc.PubKey.Address(), acc.AccountNumber, acc.Sequence)
}

// Prototype function for BaseAccount
func ProtoBaseAccount() Account {
return &BaseAccount{}
Expand Down Expand Up @@ -164,6 +178,22 @@ type BaseVestingAccount struct {
EndTime int64 // when the coins become unlocked
}

// String implements fmt.Stringer
func (bva BaseVestingAccount) String() string {
jackzampolin marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Sprintf(`Vesting Account %s:
jackzampolin marked this conversation as resolved.
Show resolved Hide resolved
Coins: %s
PubKey: %s
AccountNumber: %d
Sequence: %d
OriginalVesting: %s
DelegatedFree: %s
DelegatedVesting: %s
EndTime: %d `, bva.Address, bva.Coins,
bva.PubKey.Address(), bva.AccountNumber, bva.Sequence,
bva.OriginalVesting, bva.DelegatedFree,
bva.DelegatedVesting, bva.EndTime)
}

// spendableCoins returns all the spendable coins for a vesting account given a
// set of vesting coins.
//
Expand Down Expand Up @@ -342,6 +372,22 @@ func NewContinuousVestingAccount(
}
}

func (cva ContinuousVestingAccount) String() string {
return fmt.Sprintf(`Vesting Account %s:
Coins: %s
PubKey: %s
AccountNumber: %d
Sequence: %d
OriginalVesting: %s
DelegatedFree: %s
DelegatedVesting: %s
StartTime: %d
EndTime: %d `, cva.Address, cva.Coins,
cva.PubKey.Address(), cva.AccountNumber, cva.Sequence,
cva.OriginalVesting, cva.DelegatedFree,
cva.DelegatedVesting, cva.StartTime, cva.EndTime)
}

// GetVestedCoins returns the total number of vested coins. If no coins are vested,
// nil is returned.
func (cva ContinuousVestingAccount) GetVestedCoins(blockTime time.Time) sdk.Coins {
Expand Down
25 changes: 4 additions & 21 deletions x/auth/client/cli/account.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package cli

import (
"fmt"

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
Expand All @@ -20,18 +18,14 @@ func GetAccountCmd(storeName string, cdc *codec.Codec) *cobra.Command {
Short: "Query account balance",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
// find the key to look up the account
addr := args[0]
cliCtx := context.NewCLIContext().
WithCodec(cdc).WithAccountDecoder(cdc)

key, err := sdk.AccAddressFromBech32(addr)
key, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return err
}

cliCtx := context.NewCLIContext().
WithCodec(cdc).
WithAccountDecoder(cdc)

if err = cliCtx.EnsureAccountExistsFromAddr(key); err != nil {
return err
}
Expand All @@ -41,18 +35,7 @@ func GetAccountCmd(storeName string, cdc *codec.Codec) *cobra.Command {
return err
}

var output []byte
if cliCtx.Indent {
output, err = cdc.MarshalJSONIndent(acc, "", " ")
} else {
output, err = cdc.MarshalJSON(acc)
}
if err != nil {
return err
}

fmt.Println(string(output))
return nil
return cliCtx.PrintOutput(acc)
},
}

Expand Down
Loading