Require proto.Message in client.Context.PrintOutput (#6999)

* Enable proto JSON json for cli tx & query

* WIP on tests

* Test fixes, cleanup

* Cleanup

* Address review comments

* Update client/context.go

Co-authored-by: Anil Kumar Kammari <anil@vitwit.com>

* Fixes

Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
Co-authored-by: Anil Kumar Kammari <anil@vitwit.com>
This commit is contained in:
Aaron Craelius 2020-08-11 03:19:49 -04:00 committed by GitHub
parent 20c80cfd44
commit 7de8ef75b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 95 additions and 99 deletions

View File

@ -5,6 +5,8 @@ import (
"io"
"os"
"github.com/gogo/protobuf/proto"
"github.com/pkg/errors"
rpcclient "github.com/tendermint/tendermint/rpc/client"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"
@ -209,7 +211,17 @@ func (ctx Context) PrintString(str string) error {
// PrintOutput outputs toPrint to the ctx.Output based on ctx.OutputFormat which is
// either text or json. If text, toPrint will be YAML encoded. Otherwise, toPrint
// will be JSON encoded using ctx.JSONMarshaler. An error is returned upon failure.
func (ctx Context) PrintOutput(toPrint interface{}) error {
func (ctx Context) PrintOutput(toPrint proto.Message) error {
return ctx.printOutput(toPrint)
}
// PrintOutputLegacy is a variant of PrintOutput that doesn't require a proto type
// and uses amino JSON encoding. It will be removed in the near future!
func (ctx Context) PrintOutputLegacy(toPrint interface{}) error {
return ctx.WithJSONMarshaler(ctx.LegacyAmino).printOutput(toPrint)
}
func (ctx Context) printOutput(toPrint interface{}) error {
// always serialize JSON initially because proto json can't be directly YAML encoded
out, err := ctx.JSONMarshaler.MarshalJSON(toPrint)
if err != nil {

View File

@ -50,7 +50,7 @@ func ValidatorCommand() *cobra.Command {
return err
}
return clientCtx.PrintOutput(result)
return clientCtx.PrintOutputLegacy(result)
},
}

View File

@ -64,7 +64,7 @@ $ <appcli> query auth params
return err
}
return clientCtx.PrintOutput(res.Params)
return clientCtx.PrintOutput(&res.Params)
},
}
@ -98,13 +98,7 @@ func GetAccountCmd() *cobra.Command {
return err
}
var account types.AccountI
err = clientCtx.InterfaceRegistry.UnpackAny(res.Account, &account)
if err != nil {
return err
}
return clientCtx.PrintOutput(account)
return clientCtx.PrintOutput(res.Account)
},
}

View File

@ -5,6 +5,8 @@ import (
"fmt"
"testing"
"github.com/gogo/protobuf/proto"
"github.com/stretchr/testify/suite"
tmcli "github.com/tendermint/tendermint/libs/cli"
@ -128,43 +130,41 @@ func (s *IntegrationTestSuite) TestGetCmdQueryTotalSupply() {
name string
args []string
expectErr bool
respType fmt.Stringer
expected fmt.Stringer
respType proto.Message
expected proto.Message
}{
{
"total supply",
[]string{
name: "total supply",
args: []string{
fmt.Sprintf("--%s=1", flags.FlagHeight),
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
},
false,
&sdk.Coins{},
sdk.NewCoins(
sdk.NewCoin(fmt.Sprintf("%stoken", val.Moniker), s.cfg.AccountTokens),
sdk.NewCoin(s.cfg.BondDenom, s.cfg.StakingTokens.Add(sdk.NewInt(10))),
),
respType: &types.QueryTotalSupplyResponse{},
expected: &types.QueryTotalSupplyResponse{
Supply: sdk.NewCoins(
sdk.NewCoin(fmt.Sprintf("%stoken", val.Moniker), s.cfg.AccountTokens),
sdk.NewCoin(s.cfg.BondDenom, s.cfg.StakingTokens.Add(sdk.NewInt(10))),
)},
},
{
"total supply of a specific denomination",
[]string{
name: "total supply of a specific denomination",
args: []string{
fmt.Sprintf("--%s=1", flags.FlagHeight),
fmt.Sprintf("--%s=%s", cli.FlagDenom, s.cfg.BondDenom),
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
},
false,
&sdk.Coin{},
sdk.NewCoin(s.cfg.BondDenom, s.cfg.StakingTokens.Add(sdk.NewInt(10))),
respType: &sdk.Coin{},
expected: &sdk.Coin{s.cfg.BondDenom, s.cfg.StakingTokens.Add(sdk.NewInt(10))},
},
{
"total supply of a bogus denom",
[]string{
name: "total supply of a bogus denom",
args: []string{
fmt.Sprintf("--%s=1", flags.FlagHeight),
fmt.Sprintf("--%s=foobar", cli.FlagDenom),
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
},
false,
&sdk.Coin{},
sdk.NewCoin("foobar", sdk.ZeroInt()),
respType: &sdk.Coin{},
expected: &sdk.Coin{"foobar", sdk.ZeroInt()},
},
}
@ -188,8 +188,8 @@ func (s *IntegrationTestSuite) TestGetCmdQueryTotalSupply() {
s.Require().Error(err)
} else {
s.Require().NoError(err)
s.Require().NoError(clientCtx.JSONMarshaler.UnmarshalJSON(out.Bytes(), tc.respType), out.String())
s.Require().Equal(tc.expected.String(), tc.respType.String())
s.Require().NoError(clientCtx.JSONMarshaler.UnmarshalJSON(out.Bytes(), tc.respType))
s.Require().Equal(tc.expected, tc.respType)
}
})
}

View File

@ -140,7 +140,7 @@ To query for the total supply of a specific coin denomination use:
return err
}
return clientCtx.PrintOutput(res.Supply)
return clientCtx.PrintOutput(res)
}
res, err := queryClient.SupplyOf(context.Background(), &types.QuerySupplyOfRequest{Denom: denom})
@ -148,7 +148,7 @@ To query for the total supply of a specific coin denomination use:
return err
}
return clientCtx.PrintOutput(res.Amount)
return clientCtx.PrintOutput(&res.Amount)
},
}

View File

@ -48,7 +48,7 @@ func (s *IntegrationTestSuite) SetupTest() {
mintData.Params.InflationMin = inflation
mintData.Params.InflationMax = inflation
mintDataBz, err := cfg.Codec.MarshalJSON(mintData)
mintDataBz, err := cfg.Codec.MarshalJSON(&mintData)
s.Require().NoError(err)
genesisState[minttypes.ModuleName] = mintDataBz
cfg.GenesisState = genesisState
@ -389,7 +389,7 @@ func (s *IntegrationTestSuite) TestGetCmdQueryDelegatorRewards() {
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
},
false,
`[{"denom":"stake","amount":"387.100000000000000000"}]`,
`{"rewards":[{"denom":"stake","amount":"387.100000000000000000"}]}`,
},
{
"text output",
@ -416,7 +416,8 @@ total:
addr.String(), valAddr.String(),
},
false,
`- amount: "387.100000000000000000"
`rewards:
- amount: "387.100000000000000000"
denom: stake`,
},
}
@ -461,12 +462,13 @@ func (s *IntegrationTestSuite) TestGetCmdQueryCommunityPool() {
{
"json output",
[]string{fmt.Sprintf("--%s=3", flags.FlagHeight), fmt.Sprintf("--%s=json", tmcli.OutputFlag)},
`[{"denom":"stake","amount":"4.740000000000000000"}]`,
`{"pool":[{"denom":"stake","amount":"4.740000000000000000"}]}`,
},
{
"text output",
[]string{fmt.Sprintf("--%s=text", tmcli.OutputFlag), fmt.Sprintf("--%s=3", flags.FlagHeight)},
`- amount: "4.740000000000000000"
`pool:
- amount: "4.740000000000000000"
denom: stake`,
},
}

View File

@ -56,7 +56,7 @@ func GetCmdQueryParams() *cobra.Command {
return err
}
return clientCtx.PrintOutput(res.GetParams())
return clientCtx.PrintOutput(&res.Params)
},
}
@ -101,7 +101,7 @@ $ %s query distribution validator-outstanding-rewards cosmosvaloper1lwjmdnks33xw
return err
}
return clientCtx.PrintOutput(res.GetRewards())
return clientCtx.PrintOutput(&res.Rewards)
},
}
@ -145,7 +145,7 @@ $ %s query distribution commission cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fxs9l
return err
}
return clientCtx.PrintOutput(res.GetCommission())
return clientCtx.PrintOutput(&res.Commission)
},
}
@ -262,7 +262,7 @@ $ %s query distribution rewards cosmos1gghjut3ccd8ay0zduzj64hwre2fxs9ld75ru9p co
return err
}
return clientCtx.PrintOutput(res.GetRewards())
return clientCtx.PrintOutput(res)
}
res, err := queryClient.DelegationTotalRewards(
@ -309,7 +309,7 @@ $ %s query distribution community-pool
return err
}
return clientCtx.PrintOutput(res.GetPool())
return clientCtx.PrintOutput(res)
},
}

View File

@ -287,7 +287,7 @@ Where proposal.json contains:
return err
}
proposal, err := ParseCommunityPoolSpendProposalJSON(clientCtx.JSONMarshaler, args[0])
proposal, err := ParseCommunityPoolSpendProposalJSON(clientCtx.LegacyAmino, args[0])
if err != nil {
return err
}

View File

@ -19,7 +19,8 @@ type (
)
// ParseCommunityPoolSpendProposalJSON reads and parses a CommunityPoolSpendProposalJSON from a file.
func ParseCommunityPoolSpendProposalJSON(cdc codec.JSONMarshaler, proposalFile string) (CommunityPoolSpendProposalJSON, error) {
// TODO: migrate this to protobuf
func ParseCommunityPoolSpendProposalJSON(cdc *codec.LegacyAmino, proposalFile string) (CommunityPoolSpendProposalJSON, error) {
proposal := CommunityPoolSpendProposalJSON{}
contents, err := ioutil.ReadFile(proposalFile)

View File

@ -12,7 +12,6 @@ import (
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/cosmos-sdk/x/evidence/exported"
"github.com/cosmos/cosmos-sdk/x/evidence/types"
)
@ -86,13 +85,7 @@ func queryEvidence(clientCtx client.Context, hash string) error {
return err
}
var evidence exported.Evidence
err = clientCtx.InterfaceRegistry.UnpackAny(res.Evidence, &evidence)
if err != nil {
return err
}
return clientCtx.PrintOutput(evidence)
return clientCtx.PrintOutput(res.Evidence)
}
func queryAllEvidence(clientCtx client.Context, pageReq *query.PageRequest) error {
@ -103,21 +96,9 @@ func queryAllEvidence(clientCtx client.Context, pageReq *query.PageRequest) erro
}
res, err := queryClient.AllEvidence(context.Background(), params)
if err != nil {
return err
}
evidence := make([]exported.Evidence, 0, len(res.Evidence))
for _, eviAny := range res.Evidence {
var evi exported.Evidence
err = clientCtx.InterfaceRegistry.UnpackAny(eviAny, &evi)
if err != nil {
return err
}
evidence = append(evidence, evi)
}
return clientCtx.PrintOutput(evidence)
return clientCtx.PrintOutput(res)
}

View File

@ -82,7 +82,7 @@ $ %s query gov proposal 1
return err
}
return clientCtx.PrintOutput(res.GetProposal())
return clientCtx.PrintOutput(&res.Proposal)
},
}
@ -237,7 +237,7 @@ $ %s query gov vote 1 cosmos1skjwj5whet0lpe65qaq4rpq03hjxlwd9nf39lk
}
}
return clientCtx.PrintOutput(res.GetVote())
return clientCtx.PrintOutput(&res.Vote)
},
}
@ -298,7 +298,7 @@ $ %[1]s query gov votes 1 --page=2 --limit=100
var votes types.Votes
clientCtx.JSONMarshaler.MustUnmarshalJSON(resByTxQuery, &votes)
return clientCtx.PrintOutput(votes)
return clientCtx.PrintOutputLegacy(votes)
}
@ -389,7 +389,7 @@ $ %s query gov deposit 1 cosmos1skjwj5whet0lpe65qaq4rpq03hjxlwd9nf39lk
clientCtx.JSONMarshaler.MustUnmarshalJSON(resByTxQuery, &deposit)
}
return clientCtx.PrintOutput(deposit)
return clientCtx.PrintOutput(&deposit)
},
}
@ -447,7 +447,8 @@ $ %s query gov deposits 1
var dep types.Deposits
clientCtx.JSONMarshaler.MustUnmarshalJSON(resByTxQuery, &dep)
return clientCtx.PrintOutput(dep)
return clientCtx.PrintOutputLegacy(dep)
}
pageReq, err := client.ReadPageRequest(cmd.Flags())
@ -522,7 +523,7 @@ $ %s query gov tally 1
return err
}
return clientCtx.PrintOutput(res.GetTally())
return clientCtx.PrintOutput(&res.Tally)
},
}
@ -579,11 +580,13 @@ $ %s query gov params
return err
}
return clientCtx.PrintOutput(types.NewParams(
params := types.NewParams(
votingRes.GetVotingParams(),
tallyRes.GetTallyParams(),
depositRes.GetDepositParams(),
))
)
return clientCtx.PrintOutputLegacy(params)
},
}
@ -638,7 +641,7 @@ $ %s query gov param deposit
return fmt.Errorf("argument must be one of (voting|tallying|deposit), was %s", args[0])
}
return clientCtx.PrintOutput(out)
return clientCtx.PrintOutputLegacy(out)
},
}
@ -680,7 +683,7 @@ $ %s query gov proposer 1
return err
}
return clientCtx.PrintOutput(prop)
return clientCtx.PrintOutputLegacy(prop)
},
}

View File

@ -41,7 +41,8 @@ func GetCmdQueryClientStates() *cobra.Command {
}
clientCtx = clientCtx.WithHeight(height)
return clientCtx.PrintOutput(clientStates)
return clientCtx.PrintOutputLegacy(clientStates)
},
}
@ -81,7 +82,7 @@ func GetCmdQueryClientState() *cobra.Command {
}
clientCtx = clientCtx.WithHeight(int64(clientStateRes.ProofHeight))
return clientCtx.PrintOutput(clientStateRes)
return clientCtx.PrintOutputLegacy(clientStateRes)
},
}
@ -125,7 +126,7 @@ func GetCmdQueryConsensusState() *cobra.Command {
}
clientCtx = clientCtx.WithHeight(int64(csRes.ProofHeight))
return clientCtx.PrintOutput(csRes)
return clientCtx.PrintOutputLegacy(csRes)
},
}
@ -155,7 +156,7 @@ func GetCmdQueryHeader() *cobra.Command {
}
clientCtx = clientCtx.WithHeight(height)
return clientCtx.PrintOutput(header)
return clientCtx.PrintOutputLegacy(header)
},
}

View File

@ -165,7 +165,8 @@ func GetCmdQueryChannelClientState() *cobra.Command {
}
clientCtx = clientCtx.WithHeight(height)
return clientCtx.PrintOutput(clientStateRes)
return clientCtx.PrintOutputLegacy(clientStateRes)
},
}

View File

@ -114,12 +114,12 @@ func (s *IntegrationTestSuite) TestGetCmdQueryInflation() {
{
"json output",
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=json", tmcli.OutputFlag)},
`"1.000000000000000000"`,
`1.000000000000000000`,
},
{
"text output",
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=text", tmcli.OutputFlag)},
`"1.000000000000000000"`,
`1.000000000000000000`,
},
}
@ -155,12 +155,12 @@ func (s *IntegrationTestSuite) TestGetCmdQueryAnnualProvisions() {
{
"json output",
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=json", tmcli.OutputFlag)},
`"500000000.000000000000000000"`,
`500000000.000000000000000000`,
},
{
"text output",
[]string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=text", tmcli.OutputFlag)},
`"500000000.000000000000000000"`,
`500000000.000000000000000000`,
},
}

View File

@ -2,6 +2,7 @@ package cli
import (
"context"
"fmt"
"github.com/spf13/cobra"
@ -52,7 +53,7 @@ func GetCmdQueryParams() *cobra.Command {
return err
}
return clientCtx.PrintOutput(res.GetParams())
return clientCtx.PrintOutput(&res.Params)
},
}
@ -84,7 +85,7 @@ func GetCmdQueryInflation() *cobra.Command {
return err
}
return clientCtx.PrintOutput(res.Inflation)
return clientCtx.PrintString(fmt.Sprintf("%s\n", res.Inflation))
},
}
@ -116,7 +117,7 @@ func GetCmdQueryAnnualProvisions() *cobra.Command {
return err
}
return clientCtx.PrintOutput(res.AnnualProvisions)
return clientCtx.PrintString(fmt.Sprintf("%s\n", res.AnnualProvisions))
},
}

View File

@ -47,7 +47,7 @@ func NewQuerySubspaceParamsCmd() *cobra.Command {
return err
}
return clientCtx.PrintOutput(res.GetParam())
return clientCtx.PrintOutput(&res.Param)
},
}

View File

@ -65,7 +65,7 @@ $ <appcli> query slashing signing-info cosmosvalconspub1zcjduepqfhvwcmt7p06fvdge
return err
}
return clientCtx.PrintOutput(res.ValSigningInfo)
return clientCtx.PrintOutput(&res.ValSigningInfo)
},
}
@ -139,7 +139,7 @@ $ <appcli> query slashing params
return err
}
return clientCtx.PrintOutput(res.Params)
return clientCtx.PrintOutput(&res.Params)
},
}

View File

@ -80,7 +80,7 @@ $ %s query staking validator cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fxs9ldmqhff
return err
}
return clientCtx.PrintOutput(res.Validator)
return clientCtx.PrintOutput(&res.Validator)
},
}
@ -126,7 +126,7 @@ $ %s query staking validators
validators = append(validators, validator)
}
return clientCtx.PrintOutput(validators)
return clientCtx.PrintOutputLegacy(validators)
},
}
@ -452,7 +452,7 @@ $ %s query staking unbonding-delegation cosmos1gghjut3ccd8ay0zduzj64hwre2fxs9ld7
return err
}
return clientCtx.PrintOutput(res.Unbond)
return clientCtx.PrintOutput(&res.Unbond)
},
}
@ -567,7 +567,7 @@ $ %s query staking redelegation cosmos1gghjut3ccd8ay0zduzj64hwre2fxs9ld75ru9p co
return err
}
return clientCtx.PrintOutput(res.RedelegationResponses)
return clientCtx.PrintOutput(res)
},
}
@ -705,7 +705,7 @@ $ %s query staking pool
return err
}
return clientCtx.PrintOutput(res.Pool)
return clientCtx.PrintOutput(&res.Pool)
},
}
@ -743,7 +743,7 @@ $ %s query staking params
return err
}
return clientCtx.PrintOutput(res.Params)
return clientCtx.PrintOutput(&res.Params)
},
}

View File

@ -105,7 +105,7 @@ func GetAppliedPlanCmd() *cobra.Command {
if err != nil {
return err
}
return clientCtx.PrintOutput(string(bz))
return clientCtx.PrintString(fmt.Sprintf("%s\n", string(bz)))
},
}