client: rename CliContext to Context (#6290)

* Refactor CliContext as Context

* Fix lint issues

* Fix goimports

* Fix gov tests

* Resolved ci-lint issues

* Add changelog

* Rename cliCtx to clientCtx

* Fix mocks and routes

* Add changelog

* Update changelog

* Apply suggestions from code review

Co-authored-by: Alessio Treglia <alessio@tendermint.com>

* merge client/rpc/ro{ot,utes}.go

* Update docs

* client/rpc: remove redundant client/rpc.RegisterRPCRoutes

* regenerate mocks

* Update ADRs

Co-authored-by: Alessio Treglia <alessio@tendermint.com>
Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
This commit is contained in:
SaReN
2020-06-01 12:46:03 +00:00
committed by GitHub
co-authored by Alessio Treglia Federico Kunze mergify[bot]
parent 654b2fdd10
commit 39f53ac22f
158 changed files with 1810 additions and 1830 deletions
+20 -21
View File
@@ -8,7 +8,6 @@ import (
"github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
@@ -46,10 +45,10 @@ func GetCmdQueryParams(queryRoute string, cdc *codec.Codec) *cobra.Command {
Args: cobra.NoArgs,
Short: "Query distribution params",
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
clientCtx := client.NewContext().WithCodec(cdc)
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, types.QueryParams)
res, _, err := cliCtx.QueryWithData(route, nil)
res, _, err := clientCtx.QueryWithData(route, nil)
if err != nil {
return err
}
@@ -59,7 +58,7 @@ func GetCmdQueryParams(queryRoute string, cdc *codec.Codec) *cobra.Command {
return fmt.Errorf("failed to unmarshal params: %w", err)
}
return cliCtx.PrintOutput(params)
return clientCtx.PrintOutput(params)
},
}
}
@@ -81,7 +80,7 @@ $ %s query distribution validator-outstanding-rewards cosmosvaloper1lwjmdnks33xw
),
),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
clientCtx := client.NewContext().WithCodec(cdc)
valAddr, err := sdk.ValAddressFromBech32(args[0])
if err != nil {
@@ -94,7 +93,7 @@ $ %s query distribution validator-outstanding-rewards cosmosvaloper1lwjmdnks33xw
return err
}
resp, _, err := cliCtx.QueryWithData(
resp, _, err := clientCtx.QueryWithData(
fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryValidatorOutstandingRewards),
bz,
)
@@ -107,7 +106,7 @@ $ %s query distribution validator-outstanding-rewards cosmosvaloper1lwjmdnks33xw
return err
}
return cliCtx.PrintOutput(outstandingRewards)
return clientCtx.PrintOutput(outstandingRewards)
},
}
}
@@ -128,21 +127,21 @@ $ %s query distribution commission cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fxs9l
),
),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
clientCtx := client.NewContext().WithCodec(cdc)
validatorAddr, err := sdk.ValAddressFromBech32(args[0])
if err != nil {
return err
}
res, err := common.QueryValidatorCommission(cliCtx, queryRoute, validatorAddr)
res, err := common.QueryValidatorCommission(clientCtx, queryRoute, validatorAddr)
if err != nil {
return err
}
var valCom types.ValidatorAccumulatedCommission
cdc.MustUnmarshalJSON(res, &valCom)
return cliCtx.PrintOutput(valCom)
return clientCtx.PrintOutput(valCom)
},
}
}
@@ -163,7 +162,7 @@ $ %s query distribution slashes cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fxs9ldmq
),
),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
clientCtx := client.NewContext().WithCodec(cdc)
validatorAddr, err := sdk.ValAddressFromBech32(args[0])
if err != nil {
@@ -186,14 +185,14 @@ $ %s query distribution slashes cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fxs9ldmq
return err
}
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/validator_slashes", queryRoute), bz)
res, _, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/validator_slashes", queryRoute), bz)
if err != nil {
return err
}
var slashes types.ValidatorSlashEvents
cdc.MustUnmarshalJSON(res, &slashes)
return cliCtx.PrintOutput(slashes)
return clientCtx.PrintOutput(slashes)
},
}
}
@@ -215,11 +214,11 @@ $ %s query distribution rewards cosmos1gghjut3ccd8ay0zduzj64hwre2fxs9ld75ru9p co
),
),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
clientCtx := client.NewContext().WithCodec(cdc)
// query for rewards from a particular delegation
if len(args) == 2 {
resp, _, err := common.QueryDelegationRewards(cliCtx, queryRoute, args[0], args[1])
resp, _, err := common.QueryDelegationRewards(clientCtx, queryRoute, args[0], args[1])
if err != nil {
return err
}
@@ -229,7 +228,7 @@ $ %s query distribution rewards cosmos1gghjut3ccd8ay0zduzj64hwre2fxs9ld75ru9p co
return fmt.Errorf("failed to unmarshal response: %w", err)
}
return cliCtx.PrintOutput(result)
return clientCtx.PrintOutput(result)
}
delegatorAddr, err := sdk.AccAddressFromBech32(args[0])
@@ -245,7 +244,7 @@ $ %s query distribution rewards cosmos1gghjut3ccd8ay0zduzj64hwre2fxs9ld75ru9p co
// query for delegator total rewards
route := fmt.Sprintf("custom/%s/%s", queryRoute, types.QueryDelegatorTotalRewards)
res, _, err := cliCtx.QueryWithData(route, bz)
res, _, err := clientCtx.QueryWithData(route, bz)
if err != nil {
return err
}
@@ -255,7 +254,7 @@ $ %s query distribution rewards cosmos1gghjut3ccd8ay0zduzj64hwre2fxs9ld75ru9p co
return fmt.Errorf("failed to unmarshal response: %w", err)
}
return cliCtx.PrintOutput(result)
return clientCtx.PrintOutput(result)
},
}
}
@@ -276,16 +275,16 @@ $ %s query distribution community-pool
),
),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
clientCtx := client.NewContext().WithCodec(cdc)
res, _, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/community_pool", queryRoute), nil)
res, _, err := clientCtx.QueryWithData(fmt.Sprintf("custom/%s/community_pool", queryRoute), nil)
if err != nil {
return err
}
var result sdk.DecCoins
cdc.MustUnmarshalJSON(res, &result)
return cliCtx.PrintOutput(result)
return clientCtx.PrintOutput(result)
},
}
}
+36 -37
View File
@@ -9,7 +9,6 @@ import (
"github.com/spf13/viper"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
@@ -31,7 +30,7 @@ const (
)
// NewTxCmd returns a root CLI command handler for all x/distribution transaction commands.
func NewTxCmd(ctx context.CLIContext) *cobra.Command {
func NewTxCmd(clientCtx client.Context) *cobra.Command {
distTxCmd := &cobra.Command{
Use: types.ModuleName,
Short: "Distribution transactions subcommands",
@@ -41,25 +40,25 @@ func NewTxCmd(ctx context.CLIContext) *cobra.Command {
}
distTxCmd.AddCommand(flags.PostCommands(
NewWithdrawRewardsCmd(ctx),
NewWithdrawAllRewardsCmd(ctx),
NewSetWithdrawAddrCmd(ctx),
NewFundCommunityPoolCmd(ctx),
NewWithdrawRewardsCmd(clientCtx),
NewWithdrawAllRewardsCmd(clientCtx),
NewSetWithdrawAddrCmd(clientCtx),
NewFundCommunityPoolCmd(clientCtx),
)...)
return distTxCmd
}
type newGenerateOrBroadcastFunc func(ctx context.CLIContext, msgs ...sdk.Msg) error
type newGenerateOrBroadcastFunc func(clientCtx client.Context, msgs ...sdk.Msg) error
func newSplitAndApply(
newGenerateOrBroadcast newGenerateOrBroadcastFunc,
cliCtx context.CLIContext,
clientCtx client.Context,
msgs []sdk.Msg,
chunkSize int,
) error {
if chunkSize == 0 {
return newGenerateOrBroadcast(cliCtx, msgs...)
return newGenerateOrBroadcast(clientCtx, msgs...)
}
// split messages into slices of length chunkSize
@@ -72,7 +71,7 @@ func newSplitAndApply(
}
msgChunk := msgs[i:sliceEnd]
if err := newGenerateOrBroadcast(cliCtx, msgChunk...); err != nil {
if err := newGenerateOrBroadcast(clientCtx, msgChunk...); err != nil {
return err
}
}
@@ -80,7 +79,7 @@ func newSplitAndApply(
return nil
}
func NewWithdrawRewardsCmd(ctx context.CLIContext) *cobra.Command {
func NewWithdrawRewardsCmd(clientCtx client.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "withdraw-rewards [validator-addr]",
Short: "Withdraw rewards from a given delegation address, and optionally withdraw validator commission if the delegation address given is a validator operator",
@@ -97,9 +96,9 @@ $ %s tx distribution withdraw-rewards cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fx
),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := ctx.InitWithInput(cmd.InOrStdin())
clientCtx := clientCtx.InitWithInput(cmd.InOrStdin())
delAddr := cliCtx.GetFromAddress()
delAddr := clientCtx.GetFromAddress()
valAddr, err := sdk.ValAddressFromBech32(args[0])
if err != nil {
return err
@@ -116,14 +115,14 @@ $ %s tx distribution withdraw-rewards cosmosvaloper1gghjut3ccd8ay0zduzj64hwre2fx
}
}
return tx.GenerateOrBroadcastTx(cliCtx, msgs...)
return tx.GenerateOrBroadcastTx(clientCtx, msgs...)
},
}
cmd.Flags().Bool(flagCommission, false, "also withdraw validator's commission")
return cmd
}
func NewWithdrawAllRewardsCmd(ctx context.CLIContext) *cobra.Command {
func NewWithdrawAllRewardsCmd(clientCtx client.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "withdraw-all-rewards",
Short: "withdraw all delegations rewards for a delegator",
@@ -138,29 +137,29 @@ $ %s tx distribution withdraw-all-rewards --from mykey
),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := ctx.InitWithInput(cmd.InOrStdin())
clientCtx := clientCtx.InitWithInput(cmd.InOrStdin())
delAddr := cliCtx.GetFromAddress()
delAddr := clientCtx.GetFromAddress()
// The transaction cannot be generated offline since it requires a query
// to get all the validators.
if cliCtx.Offline {
if clientCtx.Offline {
return fmt.Errorf("cannot generate tx in offline mode")
}
msgs, err := common.WithdrawAllDelegatorRewards(cliCtx, types.QuerierRoute, delAddr)
msgs, err := common.WithdrawAllDelegatorRewards(clientCtx, types.QuerierRoute, delAddr)
if err != nil {
return err
}
chunkSize := viper.GetInt(flagMaxMessagesPerTx)
return newSplitAndApply(tx.GenerateOrBroadcastTx, cliCtx, msgs, chunkSize)
return newSplitAndApply(tx.GenerateOrBroadcastTx, clientCtx, msgs, chunkSize)
},
}
return cmd
}
func NewSetWithdrawAddrCmd(ctx context.CLIContext) *cobra.Command {
func NewSetWithdrawAddrCmd(clientCtx client.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "set-withdraw-addr [withdraw-addr]",
Short: "change the default withdraw address for rewards associated with an address",
@@ -175,9 +174,9 @@ $ %s tx distribution set-withdraw-addr cosmos1gghjut3ccd8ay0zduzj64hwre2fxs9ld75
),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := ctx.InitWithInput(cmd.InOrStdin())
clientCtx := clientCtx.InitWithInput(cmd.InOrStdin())
delAddr := cliCtx.GetFromAddress()
delAddr := clientCtx.GetFromAddress()
withdrawAddr, err := sdk.AccAddressFromBech32(args[0])
if err != nil {
return err
@@ -188,13 +187,13 @@ $ %s tx distribution set-withdraw-addr cosmos1gghjut3ccd8ay0zduzj64hwre2fxs9ld75
return err
}
return tx.GenerateOrBroadcastTx(cliCtx, msg)
return tx.GenerateOrBroadcastTx(clientCtx, msg)
},
}
return cmd
}
func NewFundCommunityPoolCmd(ctx context.CLIContext) *cobra.Command {
func NewFundCommunityPoolCmd(clientCtx client.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "community-pool-spend [proposal-file]",
Args: cobra.ExactArgs(1),
@@ -220,9 +219,9 @@ Where proposal.json contains:
),
),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := ctx.InitWithInput(cmd.InOrStdin())
clientCtx := clientCtx.InitWithInput(cmd.InOrStdin())
depositorAddr := cliCtx.GetFromAddress()
depositorAddr := clientCtx.GetFromAddress()
amount, err := sdk.ParseCoins(args[0])
if err != nil {
return err
@@ -233,14 +232,14 @@ Where proposal.json contains:
return err
}
return tx.GenerateOrBroadcastTx(cliCtx, msg)
return tx.GenerateOrBroadcastTx(clientCtx, msg)
},
}
return cmd
}
// GetCmdSubmitProposal implements the command to submit a community-pool-spend proposal
func GetCmdSubmitProposal(ctx context.CLIContext) *cobra.Command {
func GetCmdSubmitProposal(clientCtx client.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "community-pool-spend [proposal-file]",
Args: cobra.ExactArgs(1),
@@ -266,14 +265,14 @@ Where proposal.json contains:
),
),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := ctx.InitWithInput(cmd.InOrStdin())
clientCtx := clientCtx.InitWithInput(cmd.InOrStdin())
proposal, err := ParseCommunityPoolSpendProposalJSON(ctx.JSONMarshaler, args[0])
proposal, err := ParseCommunityPoolSpendProposalJSON(clientCtx.JSONMarshaler, args[0])
if err != nil {
return err
}
from := cliCtx.GetFromAddress()
from := clientCtx.GetFromAddress()
amount, err := sdk.ParseCoins(proposal.Amount)
if err != nil {
@@ -293,24 +292,24 @@ Where proposal.json contains:
return err
}
return tx.GenerateOrBroadcastTx(cliCtx, msg)
return tx.GenerateOrBroadcastTx(clientCtx, msg)
},
}
return cmd
}
type generateOrBroadcastFunc func(context.CLIContext, []sdk.Msg) error
type generateOrBroadcastFunc func(client.Context, []sdk.Msg) error
func splitAndApply(
generateOrBroadcast generateOrBroadcastFunc,
cliCtx context.CLIContext,
clientCtx client.Context,
msgs []sdk.Msg,
chunkSize int,
) error {
if chunkSize == 0 {
return generateOrBroadcast(cliCtx, msgs)
return generateOrBroadcast(clientCtx, msgs)
}
// split messages into slices of length chunkSize
@@ -323,7 +322,7 @@ func splitAndApply(
}
msgChunk := msgs[i:sliceEnd]
if err := generateOrBroadcast(cliCtx, msgChunk); err != nil {
if err := generateOrBroadcast(clientCtx, msgChunk); err != nil {
return err
}
}
+7 -7
View File
@@ -9,20 +9,20 @@ import (
"github.com/stretchr/testify/assert"
"github.com/tendermint/tendermint/crypto/secp256k1"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func Test_splitAndCall_NoMessages(t *testing.T) {
ctx := context.CLIContext{}
clientCtx := client.Context{}
err := splitAndApply(nil, ctx, nil, 10)
err := splitAndApply(nil, clientCtx, nil, 10)
assert.NoError(t, err, "")
}
func Test_splitAndCall_Splitting(t *testing.T) {
ctx := context.CLIContext{}
clientCtx := client.Context{}
addr := sdk.AccAddress(secp256k1.GenPrivKey().PubKey().Address())
@@ -40,10 +40,10 @@ func Test_splitAndCall_Splitting(t *testing.T) {
callCount := 0
err := splitAndApply(
func(ctx context.CLIContext, msgs []sdk.Msg) error {
func(clientCtx client.Context, msgs []sdk.Msg) error {
callCount++
assert.NotNil(t, ctx)
assert.NotNil(t, clientCtx)
assert.NotNil(t, msgs)
if callCount < 3 {
@@ -54,7 +54,7 @@ func Test_splitAndCall_Splitting(t *testing.T) {
return nil
},
ctx, msgs, chunkSize)
clientCtx, msgs, chunkSize)
assert.NoError(t, err, "")
assert.Equal(t, 3, callCount)