From 40c3c71e25cb69058e54fc81a2e22617c7a4c401 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Thu, 18 Jan 2024 21:09:41 +0000 Subject: [PATCH] fix(client/v2): resolve keyring flags properly (backport #19060) (#19105) Co-authored-by: Julien Robert --- client/cmd.go | 12 ++--- client/v2/CHANGELOG.md | 9 ++++ client/v2/autocli/app.go | 5 +- client/v2/autocli/builder.go | 4 -- client/v2/autocli/common.go | 6 +-- client/v2/autocli/common_test.go | 23 ++++----- client/v2/autocli/flag/address.go | 14 ++--- client/v2/autocli/msg.go | 9 ++-- client/v2/autocli/msg_test.go | 37 +++++++------ client/v2/autocli/query.go | 13 ++--- client/v2/autocli/query_test.go | 86 +++++++++++++++++-------------- client/v2/autocli/util.go | 8 ++- simapp/go.mod | 4 +- simapp/go.sum | 4 +- tests/go.mod | 4 +- tests/go.sum | 4 +- 16 files changed, 126 insertions(+), 116 deletions(-) diff --git a/client/cmd.go b/client/cmd.go index b98b5d6e895b..3b0cd4be105e 100644 --- a/client/cmd.go +++ b/client/cmd.go @@ -1,6 +1,7 @@ package client import ( + "context" "crypto/tls" "fmt" "strings" @@ -278,7 +279,7 @@ func readTxCommandFlags(clientCtx Context, flagSet *pflag.FlagSet) (Context, err from, _ := flagSet.GetString(flags.FlagFrom) fromAddr, fromName, keyType, err := GetFromFields(clientCtx, clientCtx.Keyring, from) if err != nil { - return clientCtx, err + return clientCtx, fmt.Errorf("failed to convert address field to address: %w", err) } clientCtx = clientCtx.WithFrom(from).WithFromAddress(fromAddr).WithFromName(fromName) @@ -358,13 +359,6 @@ func GetClientContextFromCmd(cmd *cobra.Command) Context { // SetCmdClientContext sets a command's Context value to the provided argument. // If the context has not been set, set the given context as the default. func SetCmdClientContext(cmd *cobra.Command, clientCtx Context) error { - v := cmd.Context().Value(ClientContextKey) - if v == nil { - v = &clientCtx - } - - clientCtxPtr := v.(*Context) - *clientCtxPtr = clientCtx - + cmd.SetContext(context.WithValue(cmd.Context(), ClientContextKey, &clientCtx)) return nil } diff --git a/client/v2/CHANGELOG.md b/client/v2/CHANGELOG.md index e74c2e521bbb..a470761f8c08 100644 --- a/client/v2/CHANGELOG.md +++ b/client/v2/CHANGELOG.md @@ -36,6 +36,15 @@ Ref: https://keepachangelog.com/en/1.0.0/ ## [Unreleased] +### Improvements + +* [#19060](https://github.com/cosmos/cosmos-sdk/pull/19060) Use client context from root (or enhanced) command in autocli commands. + * Note, the given command must have a `client.Context` in its context. + +### Bug Fixes + +* [#19060](https://github.com/cosmos/cosmos-sdk/pull/19060) Simplify key flag parsing logic in flag handler. + ## [v2.0.0-beta.1] - 2023-11-07 This is the first tagged version of client/v2. diff --git a/client/v2/autocli/app.go b/client/v2/autocli/app.go index 56e0de8406ca..476864225c8b 100644 --- a/client/v2/autocli/app.go +++ b/client/v2/autocli/app.go @@ -78,7 +78,6 @@ func (appOptions AppOptions) EnhanceRootCommand(rootCmd *cobra.Command) error { ConsensusAddressCodec: appOptions.ConsensusAddressCodec, Keyring: appOptions.Keyring, }, - ClientCtx: appOptions.ClientCtx, TxConfigOpts: appOptions.TxConfigOpts, GetClientConn: func(cmd *cobra.Command) (grpc.ClientConnInterface, error) { return client.GetClientQueryContext(cmd) @@ -119,7 +118,7 @@ func (appOptions AppOptions) EnhanceRootCommandWithBuilder(rootCmd *cobra.Comman return err } } else { - queryCmd, err := builder.BuildQueryCommand(appOptions, customQueryCmds) + queryCmd, err := builder.BuildQueryCommand(rootCmd.Context(), appOptions, customQueryCmds) if err != nil { return err } @@ -132,7 +131,7 @@ func (appOptions AppOptions) EnhanceRootCommandWithBuilder(rootCmd *cobra.Comman return err } } else { - subCmd, err := builder.BuildMsgCommand(appOptions, customMsgCmds) + subCmd, err := builder.BuildMsgCommand(rootCmd.Context(), appOptions, customMsgCmds) if err != nil { return err } diff --git a/client/v2/autocli/builder.go b/client/v2/autocli/builder.go index d42330aa8dbb..7518b3a034b5 100644 --- a/client/v2/autocli/builder.go +++ b/client/v2/autocli/builder.go @@ -6,7 +6,6 @@ import ( "cosmossdk.io/client/v2/autocli/flag" - "github.com/cosmos/cosmos-sdk/client" authtx "github.com/cosmos/cosmos-sdk/x/auth/tx" ) @@ -19,9 +18,6 @@ type Builder struct { // from a given context. GetClientConn func(*cobra.Command) (grpc.ClientConnInterface, error) - // ClientCtx contains the necessary information needed to execute the commands. - ClientCtx client.Context - // TxConfigOptions is required to support sign mode textual TxConfigOpts authtx.ConfigOptions diff --git a/client/v2/autocli/common.go b/client/v2/autocli/common.go index 8fdecf69cf8e..4c9a8f07dc2b 100644 --- a/client/v2/autocli/common.go +++ b/client/v2/autocli/common.go @@ -1,7 +1,6 @@ package autocli import ( - "context" "fmt" "github.com/spf13/cobra" @@ -56,7 +55,6 @@ func (b *Builder) buildMethodCommandCommon(descriptor protoreflect.MethodDescrip Version: options.Version, } - cmd.SetContext(context.Background()) binder, err := b.AddMessageFlags(cmd.Context(), cmd.Flags(), inputType, options) if err != nil { return nil, err @@ -180,7 +178,7 @@ func (b *Builder) enhanceCommandCommon( // enhanceQuery enhances the provided query command with the autocli commands for a module. func enhanceQuery(builder *Builder, moduleName string, cmd *cobra.Command, modOpts *autocliv1.ModuleOptions) error { if queryCmdDesc := modOpts.Query; queryCmdDesc != nil { - subCmd := topLevelCmd(moduleName, fmt.Sprintf("Querying commands for the %s module", moduleName)) + subCmd := topLevelCmd(cmd.Context(), moduleName, fmt.Sprintf("Querying commands for the %s module", moduleName)) if err := builder.AddQueryServiceCommands(subCmd, queryCmdDesc); err != nil { return err } @@ -194,7 +192,7 @@ func enhanceQuery(builder *Builder, moduleName string, cmd *cobra.Command, modOp // enhanceMsg enhances the provided msg command with the autocli commands for a module. func enhanceMsg(builder *Builder, moduleName string, cmd *cobra.Command, modOpts *autocliv1.ModuleOptions) error { if txCmdDesc := modOpts.Tx; txCmdDesc != nil { - subCmd := topLevelCmd(moduleName, fmt.Sprintf("Transactions commands for the %s module", moduleName)) + subCmd := topLevelCmd(cmd.Context(), moduleName, fmt.Sprintf("Transactions commands for the %s module", moduleName)) if err := builder.AddMsgServiceCommands(subCmd, txCmdDesc); err != nil { return err } diff --git a/client/v2/autocli/common_test.go b/client/v2/autocli/common_test.go index 2f4f9b804fa6..31dc43d3af62 100644 --- a/client/v2/autocli/common_test.go +++ b/client/v2/autocli/common_test.go @@ -28,8 +28,9 @@ import ( ) type fixture struct { - conn *testClientConn - b *Builder + conn *testClientConn + b *Builder + clientCtx client.Context } func initFixture(t *testing.T) *fixture { @@ -60,8 +61,7 @@ func initFixture(t *testing.T) *fixture { interfaceRegistry := encodingConfig.Codec.InterfaceRegistry() banktypes.RegisterInterfaces(interfaceRegistry) - var initClientCtx client.Context - initClientCtx = initClientCtx. + clientCtx := client.Context{}. WithKeyring(kr). WithKeyringDir(home). WithHomeDir(home). @@ -86,19 +86,19 @@ func initFixture(t *testing.T) *fixture { }, AddQueryConnFlags: flags.AddQueryFlagsToCmd, AddTxConnFlags: flags.AddTxFlagsToCmd, - ClientCtx: initClientCtx, } assert.NilError(t, b.ValidateAndComplete()) return &fixture{ - conn: conn, - b: b, + conn: conn, + b: b, + clientCtx: clientCtx, } } -func runCmd(conn *testClientConn, b *Builder, command func(moduleName string, b *Builder) (*cobra.Command, error), args ...string) (*bytes.Buffer, error) { +func runCmd(fixture *fixture, command func(moduleName string, f *fixture) (*cobra.Command, error), args ...string) (*bytes.Buffer, error) { out := &bytes.Buffer{} - cmd, err := command("test", b) + cmd, err := command("test", fixture) if err != nil { return out, err } @@ -212,14 +212,13 @@ func TestErrorBuildCommand(t *testing.T) { Tx: commandDescriptor, }, }, - ClientCtx: b.ClientCtx, } - _, err := b.BuildMsgCommand(appOptions, nil) + _, err := b.BuildMsgCommand(context.Background(), appOptions, nil) assert.ErrorContains(t, err, "can't find field un-existent-proto-field") appOptions.ModuleOptions["test"].Tx = &autocliv1.ServiceCommandDescriptor{Service: "un-existent-service"} appOptions.ModuleOptions["test"].Query = &autocliv1.ServiceCommandDescriptor{Service: "un-existent-service"} - _, err = b.BuildMsgCommand(appOptions, nil) + _, err = b.BuildMsgCommand(context.Background(), appOptions, nil) assert.ErrorContains(t, err, "can't find service un-existent-service") } diff --git a/client/v2/autocli/flag/address.go b/client/v2/autocli/flag/address.go index 507a7267a9fc..99d1a9c3284b 100644 --- a/client/v2/autocli/flag/address.go +++ b/client/v2/autocli/flag/address.go @@ -62,11 +62,9 @@ func (a *addressValue) Set(s string) error { return nil } - _, err = a.addressCodec.StringToBytes(s) - if err != nil { - return fmt.Errorf("invalid account address or key name: %w", err) - } - + // failed all validation, just accept the input. + // TODO(@julienrbrt), for final client/v2 2.0.0 revert the logic and + // do a better keyring instantiation. a.value = s return nil @@ -129,7 +127,11 @@ func (a *consensusAddressValue) Set(s string) error { var pk cryptotypes.PubKey err2 := cdc.UnmarshalInterfaceJSON([]byte(s), &pk) if err2 != nil { - return fmt.Errorf("input isn't a pubkey %w or is an invalid account address: %w", err, err2) + // failed all validation, just accept the input. + // TODO(@julienrbrt), for final client/v2 2.0.0 revert the logic and + // do a better keyring instantiation. + a.value = s + return nil } a.value, err = a.addressCodec.BytesToString(pk.Address()) diff --git a/client/v2/autocli/msg.go b/client/v2/autocli/msg.go index 6e8b237c3584..f900cd914e6a 100644 --- a/client/v2/autocli/msg.go +++ b/client/v2/autocli/msg.go @@ -25,8 +25,9 @@ import ( // BuildMsgCommand builds the msg commands for all the provided modules. If a custom command is provided for a // module, this is used instead of any automatically generated CLI commands. This allows apps to a fully dynamic client // with a more customized experience if a binary with custom commands is downloaded. -func (b *Builder) BuildMsgCommand(appOptions AppOptions, customCmds map[string]*cobra.Command) (*cobra.Command, error) { - msgCmd := topLevelCmd("tx", "Transaction subcommands") +func (b *Builder) BuildMsgCommand(ctx context.Context, appOptions AppOptions, customCmds map[string]*cobra.Command) (*cobra.Command, error) { + msgCmd := topLevelCmd(ctx, "tx", "Transaction subcommands") + if err := b.enhanceCommandCommon(msgCmd, msgCmdType, appOptions, customCmds); err != nil { return nil, err } @@ -41,7 +42,7 @@ func (b *Builder) AddMsgServiceCommands(cmd *cobra.Command, cmdDescriptor *autoc for cmdName, subCmdDescriptor := range cmdDescriptor.SubCommands { subCmd := findSubCommand(cmd, cmdName) if subCmd == nil { - subCmd = topLevelCmd(cmdName, fmt.Sprintf("Tx commands for the %s service", subCmdDescriptor.Service)) + subCmd = topLevelCmd(cmd.Context(), cmdName, fmt.Sprintf("Tx commands for the %s service", subCmdDescriptor.Service)) } // Add recursive sub-commands if there are any. This is used for nested services. @@ -112,8 +113,6 @@ func (b *Builder) AddMsgServiceCommands(cmd *cobra.Command, cmdDescriptor *autoc // BuildMsgMethodCommand returns a command that outputs the JSON representation of the message. func (b *Builder) BuildMsgMethodCommand(descriptor protoreflect.MethodDescriptor, options *autocliv1.RpcCommandOptions) (*cobra.Command, error) { cmd, err := b.buildMethodCommandCommon(descriptor, options, func(cmd *cobra.Command, input protoreflect.Message) error { - cmd.SetContext(context.WithValue(context.Background(), client.ClientContextKey, &b.ClientCtx)) - clientCtx, err := client.GetClientTxContext(cmd) if err != nil { return err diff --git a/client/v2/autocli/msg_test.go b/client/v2/autocli/msg_test.go index 96b930c33c52..5937ba24dbc7 100644 --- a/client/v2/autocli/msg_test.go +++ b/client/v2/autocli/msg_test.go @@ -1,6 +1,7 @@ package autocli import ( + "context" "fmt" "testing" @@ -11,18 +12,22 @@ import ( autocliv1 "cosmossdk.io/api/cosmos/autocli/v1" bankv1beta1 "cosmossdk.io/api/cosmos/bank/v1beta1" "cosmossdk.io/client/v2/internal/testpb" + + "github.com/cosmos/cosmos-sdk/client" ) -var buildModuleMsgCommand = func(moduleName string, b *Builder) (*cobra.Command, error) { - cmd := topLevelCmd(moduleName, fmt.Sprintf("Transactions commands for the %s module", moduleName)) - err := b.AddMsgServiceCommands(cmd, bankAutoCLI) +var buildModuleMsgCommand = func(moduleName string, f *fixture) (*cobra.Command, error) { + ctx := context.WithValue(context.Background(), client.ClientContextKey, &f.clientCtx) + cmd := topLevelCmd(ctx, moduleName, fmt.Sprintf("Transactions commands for the %s module", moduleName)) + err := f.b.AddMsgServiceCommands(cmd, bankAutoCLI) return cmd, err } -func buildCustomModuleMsgCommand(cmdDescriptor *autocliv1.ServiceCommandDescriptor) func(moduleName string, b *Builder) (*cobra.Command, error) { - return func(moduleName string, b *Builder) (*cobra.Command, error) { - cmd := topLevelCmd(moduleName, fmt.Sprintf("Transactions commands for the %s module", moduleName)) - err := b.AddMsgServiceCommands(cmd, cmdDescriptor) +func buildCustomModuleMsgCommand(cmdDescriptor *autocliv1.ServiceCommandDescriptor) func(moduleName string, f *fixture) (*cobra.Command, error) { + return func(moduleName string, f *fixture) (*cobra.Command, error) { + ctx := context.WithValue(context.Background(), client.ClientContextKey, &f.clientCtx) + cmd := topLevelCmd(ctx, moduleName, fmt.Sprintf("Transactions commands for the %s module", moduleName)) + err := f.b.AddMsgServiceCommands(cmd, cmdDescriptor) return cmd, err } } @@ -42,7 +47,7 @@ var bankAutoCLI = &autocliv1.ServiceCommandDescriptor{ func TestMsg(t *testing.T) { fixture := initFixture(t) - out, err := runCmd(fixture.conn, fixture.b, buildModuleMsgCommand, "send", + out, err := runCmd(fixture, buildModuleMsgCommand, "send", "cosmos1y74p8wyy4enfhfn342njve6cjmj5c8dtl6emdk", "cosmos1y74p8wyy4enfhfn342njve6cjmj5c8dtl6emdk", "1foo", "--generate-only", "--output", "json", @@ -50,7 +55,7 @@ func TestMsg(t *testing.T) { assert.NilError(t, err) golden.Assert(t, out.String(), "msg-output.golden") - out, err = runCmd(fixture.conn, fixture.b, buildCustomModuleMsgCommand(&autocliv1.ServiceCommandDescriptor{ + out, err = runCmd(fixture, buildCustomModuleMsgCommand(&autocliv1.ServiceCommandDescriptor{ Service: bankv1beta1.Msg_ServiceDesc.ServiceName, RpcCommandOptions: []*autocliv1.RpcCommandOptions{ { @@ -71,7 +76,7 @@ func TestMsg(t *testing.T) { assert.NilError(t, err) golden.Assert(t, out.String(), "msg-output.golden") - out, err = runCmd(fixture.conn, fixture.b, buildCustomModuleMsgCommand(&autocliv1.ServiceCommandDescriptor{ + out, err = runCmd(fixture, buildCustomModuleMsgCommand(&autocliv1.ServiceCommandDescriptor{ Service: bankv1beta1.Msg_ServiceDesc.ServiceName, RpcCommandOptions: []*autocliv1.RpcCommandOptions{ { @@ -98,12 +103,12 @@ func TestMsg(t *testing.T) { func TestMsgOptionsError(t *testing.T) { fixture := initFixture(t) - _, err := runCmd(fixture.conn, fixture.b, buildModuleMsgCommand, + _, err := runCmd(fixture, buildModuleMsgCommand, "send", "5", ) assert.ErrorContains(t, err, "accepts 3 arg(s)") - _, err = runCmd(fixture.conn, fixture.b, buildModuleMsgCommand, + _, err = runCmd(fixture, buildModuleMsgCommand, "send", "foo", "bar", "invalid", ) assert.ErrorContains(t, err, "invalid argument") @@ -112,11 +117,11 @@ func TestMsgOptionsError(t *testing.T) { func TestHelpMsg(t *testing.T) { fixture := initFixture(t) - out, err := runCmd(fixture.conn, fixture.b, buildModuleMsgCommand, "-h") + out, err := runCmd(fixture, buildModuleMsgCommand, "-h") assert.NilError(t, err) golden.Assert(t, out.String(), "help-toplevel-msg.golden") - out, err = runCmd(fixture.conn, fixture.b, buildModuleMsgCommand, "send", "-h") + out, err = runCmd(fixture, buildModuleMsgCommand, "send", "-h") assert.NilError(t, err) golden.Assert(t, out.String(), "help-echo-msg.golden") } @@ -135,7 +140,7 @@ func TestBuildCustomMsgCommand(t *testing.T) { }, } - cmd, err := b.BuildMsgCommand(appOptions, map[string]*cobra.Command{ + cmd, err := b.BuildMsgCommand(context.Background(), appOptions, map[string]*cobra.Command{ "test": {Use: "test", Run: func(cmd *cobra.Command, args []string) { customCommandCalled = true }}, @@ -153,7 +158,7 @@ func TestNotFoundErrorsMsg(t *testing.T) { b.AddTxConnFlags = nil buildModuleMsgCommand := func(moduleName string, cmdDescriptor *autocliv1.ServiceCommandDescriptor) (*cobra.Command, error) { - cmd := topLevelCmd(moduleName, fmt.Sprintf("Transactions commands for the %s module", moduleName)) + cmd := topLevelCmd(context.Background(), moduleName, fmt.Sprintf("Transactions commands for the %s module", moduleName)) err := b.AddMsgServiceCommands(cmd, cmdDescriptor) return cmd, err diff --git a/client/v2/autocli/query.go b/client/v2/autocli/query.go index 05665e048fac..41fec4a23f5f 100644 --- a/client/v2/autocli/query.go +++ b/client/v2/autocli/query.go @@ -9,7 +9,6 @@ import ( autocliv1 "cosmossdk.io/api/cosmos/autocli/v1" "cosmossdk.io/x/tx/signing/aminojson" "github.com/cockroachdb/errors" - "github.com/cosmos/cosmos-sdk/client" "github.com/spf13/cobra" "google.golang.org/protobuf/reflect/protoreflect" @@ -20,8 +19,8 @@ import ( // BuildQueryCommand builds the query commands for all the provided modules. If a custom command is provided for a // module, this is used instead of any automatically generated CLI commands. This allows apps to a fully dynamic client // with a more customized experience if a binary with custom commands is downloaded. -func (b *Builder) BuildQueryCommand(appOptions AppOptions, customCmds map[string]*cobra.Command) (*cobra.Command, error) { - queryCmd := topLevelCmd("query", "Querying subcommands") +func (b *Builder) BuildQueryCommand(ctx context.Context, appOptions AppOptions, customCmds map[string]*cobra.Command) (*cobra.Command, error) { + queryCmd := topLevelCmd(ctx, "query", "Querying subcommands") queryCmd.Aliases = []string{"q"} if err := b.enhanceCommandCommon(queryCmd, queryCmdType, appOptions, customCmds); err != nil { @@ -38,7 +37,7 @@ func (b *Builder) AddQueryServiceCommands(cmd *cobra.Command, cmdDescriptor *aut for cmdName, subCmdDesc := range cmdDescriptor.SubCommands { subCmd := findSubCommand(cmd, cmdName) if subCmd == nil { - subCmd = topLevelCmd(cmdName, fmt.Sprintf("Querying commands for the %s service", subCmdDesc.Service)) + subCmd = topLevelCmd(cmd.Context(), cmdName, fmt.Sprintf("Querying commands for the %s service", subCmdDesc.Service)) } if err := b.AddQueryServiceCommands(subCmd, subCmdDesc); err != nil { @@ -86,7 +85,7 @@ func (b *Builder) AddQueryServiceCommands(cmd *cobra.Command, cmdDescriptor *aut continue } - methodCmd, err := b.BuildQueryMethodCommand(methodDescriptor, methodOpts) + methodCmd, err := b.BuildQueryMethodCommand(cmd.Context(), methodDescriptor, methodOpts) if err != nil { return err } @@ -105,7 +104,7 @@ func (b *Builder) AddQueryServiceCommands(cmd *cobra.Command, cmdDescriptor *aut // BuildQueryMethodCommand creates a gRPC query command for the given service method. This can be used to auto-generate // just a single command for a single service rpc method. -func (b *Builder) BuildQueryMethodCommand(descriptor protoreflect.MethodDescriptor, options *autocliv1.RpcCommandOptions) (*cobra.Command, error) { +func (b *Builder) BuildQueryMethodCommand(ctx context.Context, descriptor protoreflect.MethodDescriptor, options *autocliv1.RpcCommandOptions) (*cobra.Command, error) { getClientConn := b.GetClientConn serviceDescriptor := descriptor.Parent().(protoreflect.ServiceDescriptor) methodName := fmt.Sprintf("/%s/%s", serviceDescriptor.FullName(), descriptor.Name()) @@ -118,8 +117,6 @@ func (b *Builder) BuildQueryMethodCommand(descriptor protoreflect.MethodDescript } cmd, err := b.buildMethodCommandCommon(descriptor, options, func(cmd *cobra.Command, input protoreflect.Message) error { - cmd.SetContext(context.WithValue(context.Background(), client.ClientContextKey, &b.ClientCtx)) - clientConn, err := getClientConn(cmd) if err != nil { return err diff --git a/client/v2/autocli/query_test.go b/client/v2/autocli/query_test.go index 6ed940bb6faf..07d6354928e0 100644 --- a/client/v2/autocli/query_test.go +++ b/client/v2/autocli/query_test.go @@ -1,6 +1,7 @@ package autocli import ( + "context" "fmt" "os" "strings" @@ -18,26 +19,31 @@ import ( queryv1beta1 "cosmossdk.io/api/cosmos/base/query/v1beta1" basev1beta1 "cosmossdk.io/api/cosmos/base/v1beta1" "cosmossdk.io/client/v2/internal/testpb" + + "github.com/cosmos/cosmos-sdk/client" ) -var buildModuleQueryCommand = func(moduleName string, b *Builder) (*cobra.Command, error) { - cmd := topLevelCmd(moduleName, fmt.Sprintf("Querying commands for the %s module", moduleName)) +var buildModuleQueryCommand = func(moduleName string, f *fixture) (*cobra.Command, error) { + ctx := context.WithValue(context.Background(), client.ClientContextKey, &f.clientCtx) + cmd := topLevelCmd(ctx, moduleName, fmt.Sprintf("Querying commands for the %s module", moduleName)) - err := b.AddQueryServiceCommands(cmd, testCmdDesc) + err := f.b.AddQueryServiceCommands(cmd, testCmdDesc) return cmd, err } -var buildModuleQueryCommandOptional = func(moduleName string, b *Builder) (*cobra.Command, error) { - cmd := topLevelCmd(moduleName, fmt.Sprintf("Querying commands for the %s module", moduleName)) +var buildModuleQueryCommandOptional = func(moduleName string, f *fixture) (*cobra.Command, error) { + ctx := context.WithValue(context.Background(), client.ClientContextKey, &f.clientCtx) + cmd := topLevelCmd(ctx, moduleName, fmt.Sprintf("Querying commands for the %s module", moduleName)) - err := b.AddQueryServiceCommands(cmd, testCmdDescOptional) + err := f.b.AddQueryServiceCommands(cmd, testCmdDescOptional) return cmd, err } -var buildModuleVargasOptional = func(moduleName string, b *Builder) (*cobra.Command, error) { - cmd := topLevelCmd(moduleName, fmt.Sprintf("Querying commands for the %s module", moduleName)) +var buildModuleVargasOptional = func(moduleName string, f *fixture) (*cobra.Command, error) { + ctx := context.WithValue(context.Background(), client.ClientContextKey, &f.clientCtx) + cmd := topLevelCmd(ctx, moduleName, fmt.Sprintf("Querying commands for the %s module", moduleName)) - err := b.AddQueryServiceCommands(cmd, testCmdDescInvalidOptAndVargas) + err := f.b.AddQueryServiceCommands(cmd, testCmdDescInvalidOptAndVargas) return cmd, err } @@ -190,7 +196,7 @@ var testCmdDescInvalidOptAndVargas = &autocliv1.ServiceCommandDescriptor{ func TestCoin(t *testing.T) { fixture := initFixture(t) - _, err := runCmd(fixture.conn, fixture.b, buildModuleQueryCommand, + _, err := runCmd(fixture, buildModuleQueryCommand, "echo", "1", "abc", @@ -200,7 +206,7 @@ func TestCoin(t *testing.T) { ) assert.ErrorContains(t, err, "coin flag must be a single coin, specific multiple coins with multiple flags or spaces") - _, err = runCmd(fixture.conn, fixture.b, buildModuleQueryCommand, + _, err = runCmd(fixture, buildModuleQueryCommand, "echo", "1", "abc", @@ -241,7 +247,7 @@ func TestCoin(t *testing.T) { func TestOptional(t *testing.T) { fixture := initFixture(t) - _, err := runCmd(fixture.conn, fixture.b, buildModuleQueryCommandOptional, + _, err := runCmd(fixture, buildModuleQueryCommandOptional, "echo", "1", "abc", @@ -251,7 +257,7 @@ func TestOptional(t *testing.T) { assert.Equal(t, request.Positional2, "abc") assert.DeepEqual(t, fixture.conn.lastRequest, fixture.conn.lastResponse.(*testpb.EchoResponse).Request, protocmp.Transform()) - _, err = runCmd(fixture.conn, fixture.b, buildModuleQueryCommandOptional, + _, err = runCmd(fixture, buildModuleQueryCommandOptional, "echo", "1", ) @@ -261,7 +267,7 @@ func TestOptional(t *testing.T) { assert.Equal(t, request.Positional2, "") assert.DeepEqual(t, fixture.conn.lastRequest, fixture.conn.lastResponse.(*testpb.EchoResponse).Request, protocmp.Transform()) - _, err = runCmd(fixture.conn, fixture.b, buildModuleQueryCommandOptional, + _, err = runCmd(fixture, buildModuleQueryCommandOptional, "echo", "1", "abc", @@ -269,7 +275,7 @@ func TestOptional(t *testing.T) { ) assert.ErrorContains(t, err, "accepts between 1 and 2 arg(s), received 3") - _, err = runCmd(fixture.conn, fixture.b, buildModuleVargasOptional, + _, err = runCmd(fixture, buildModuleVargasOptional, "echo", "1", "abc", @@ -281,7 +287,7 @@ func TestOptional(t *testing.T) { func TestMap(t *testing.T) { fixture := initFixture(t) - _, err := runCmd(fixture.conn, fixture.b, buildModuleQueryCommand, + _, err := runCmd(fixture, buildModuleQueryCommand, "echo", "1", "abc", @@ -296,7 +302,7 @@ func TestMap(t *testing.T) { assert.NilError(t, err) assert.DeepEqual(t, fixture.conn.lastRequest, fixture.conn.lastResponse.(*testpb.EchoResponse).Request, protocmp.Transform()) - _, err = runCmd(fixture.conn, fixture.b, buildModuleQueryCommand, + _, err = runCmd(fixture, buildModuleQueryCommand, "echo", "1", "abc", @@ -308,7 +314,7 @@ func TestMap(t *testing.T) { ) assert.ErrorContains(t, err, "invalid argument \"baz,100000foo\" for \"--map-string-coin\" flag: invalid format, expected key=value") - _, err = runCmd(fixture.conn, fixture.b, buildModuleQueryCommand, + _, err = runCmd(fixture, buildModuleQueryCommand, "echo", "1", "abc", @@ -320,7 +326,7 @@ func TestMap(t *testing.T) { ) assert.ErrorContains(t, err, "invalid argument \"bar=not-unint32\" for \"--map-string-uint32\" flag: strconv.ParseUint: parsing \"not-unint32\": invalid syntax") - _, err = runCmd(fixture.conn, fixture.b, buildModuleQueryCommand, + _, err = runCmd(fixture, buildModuleQueryCommand, "echo", "1", "abc", @@ -338,7 +344,7 @@ func TestMap(t *testing.T) { func TestEverything(t *testing.T) { fixture := initFixture(t) - _, err := runCmd(fixture.conn, fixture.b, buildModuleQueryCommand, + _, err := runCmd(fixture, buildModuleQueryCommand, "echo", "1", "abc", @@ -450,7 +456,7 @@ func TestEverything(t *testing.T) { func TestPubKeyParsingConsensusAddress(t *testing.T) { fixture := initFixture(t) - _, err := runCmd(fixture.conn, fixture.b, buildModuleQueryCommand, + _, err := runCmd(fixture, buildModuleQueryCommand, "echo", "1", "abc", "1foo", "--a-consensus-address", "{\"@type\":\"/cosmos.crypto.ed25519.PubKey\",\"key\":\"j8qdbR+AlH/V6aBTCSWXRvX3JUESF2bV+SEzndBhF0o=\"}", @@ -463,7 +469,7 @@ func TestPubKeyParsingConsensusAddress(t *testing.T) { func TestJSONParsing(t *testing.T) { fixture := initFixture(t) - _, err := runCmd(fixture.conn, fixture.b, buildModuleQueryCommand, + _, err := runCmd(fixture, buildModuleQueryCommand, "echo", "1", "abc", "1foo", "--some-messages", `{"bar":"baz"}`, @@ -472,7 +478,7 @@ func TestJSONParsing(t *testing.T) { assert.NilError(t, err) assert.DeepEqual(t, fixture.conn.lastRequest, fixture.conn.lastResponse.(*testpb.EchoResponse).Request, protocmp.Transform()) - _, err = runCmd(fixture.conn, fixture.b, buildModuleQueryCommand, + _, err = runCmd(fixture, buildModuleQueryCommand, "echo", "1", "abc", "1foo", "--some-messages", "testdata/some_message.json", @@ -485,7 +491,7 @@ func TestJSONParsing(t *testing.T) { func TestOptions(t *testing.T) { fixture := initFixture(t) - _, err := runCmd(fixture.conn, fixture.b, buildModuleQueryCommand, + _, err := runCmd(fixture, buildModuleQueryCommand, "echo", "1", "abc", "123foo", "-u", "27", // shorthand @@ -552,7 +558,7 @@ func TestBinaryFlag(t *testing.T) { fixture := initFixture(t) for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { - _, err := runCmd(fixture.conn, fixture.b, buildModuleQueryCommand, + _, err := runCmd(fixture, buildModuleQueryCommand, "echo", "1", "abc", `100foo`, "--bz", tc.input, @@ -569,23 +575,25 @@ func TestBinaryFlag(t *testing.T) { } func TestAddressValidation(t *testing.T) { + t.Skip() // TODO(@julienrbrt) re-able with better keyring instiantiation + fixture := initFixture(t) - _, err := runCmd(fixture.conn, fixture.b, buildModuleQueryCommand, + _, err := runCmd(fixture, buildModuleQueryCommand, "echo", "1", "abc", "1foo", "--an-address", "cosmos1y74p8wyy4enfhfn342njve6cjmj5c8dtl6emdk", ) assert.NilError(t, err) - _, err = runCmd(fixture.conn, fixture.b, buildModuleQueryCommand, + _, err = runCmd(fixture, buildModuleQueryCommand, "echo", "1", "abc", "1foo", "--an-address", "regen1y74p8wyy4enfhfn342njve6cjmj5c8dtlqj7ule2", ) assert.ErrorContains(t, err, "invalid account address") - _, err = runCmd(fixture.conn, fixture.b, buildModuleQueryCommand, + _, err = runCmd(fixture, buildModuleQueryCommand, "echo", "1", "abc", "1foo", "--an-address", "cosmps1BAD_ENCODING", @@ -596,7 +604,7 @@ func TestAddressValidation(t *testing.T) { func TestOutputFormat(t *testing.T) { fixture := initFixture(t) - out, err := runCmd(fixture.conn, fixture.b, buildModuleQueryCommand, + out, err := runCmd(fixture, buildModuleQueryCommand, "echo", "1", "abc", "1foo", "--output", "json", @@ -604,7 +612,7 @@ func TestOutputFormat(t *testing.T) { assert.NilError(t, err) assert.Assert(t, strings.Contains(out.String(), "{")) - out, err = runCmd(fixture.conn, fixture.b, buildModuleQueryCommand, + out, err = runCmd(fixture, buildModuleQueryCommand, "echo", "1", "abc", "1foo", "--output", "text", @@ -616,19 +624,19 @@ func TestOutputFormat(t *testing.T) { func TestHelpQuery(t *testing.T) { fixture := initFixture(t) - out, err := runCmd(fixture.conn, fixture.b, buildModuleQueryCommand, "-h") + out, err := runCmd(fixture, buildModuleQueryCommand, "-h") assert.NilError(t, err) golden.Assert(t, out.String(), "help-toplevel.golden") - out, err = runCmd(fixture.conn, fixture.b, buildModuleQueryCommand, "echo", "-h") + out, err = runCmd(fixture, buildModuleQueryCommand, "echo", "-h") assert.NilError(t, err) golden.Assert(t, out.String(), "help-echo.golden") - out, err = runCmd(fixture.conn, fixture.b, buildModuleQueryCommand, "deprecatedecho", "echo", "-h") + out, err = runCmd(fixture, buildModuleQueryCommand, "deprecatedecho", "echo", "-h") assert.NilError(t, err) golden.Assert(t, out.String(), "help-deprecated.golden") - out, err = runCmd(fixture.conn, fixture.b, buildModuleQueryCommand, "skipecho", "-h") + out, err = runCmd(fixture, buildModuleQueryCommand, "skipecho", "-h") assert.NilError(t, err) golden.Assert(t, out.String(), "help-skip.golden") } @@ -636,12 +644,12 @@ func TestHelpQuery(t *testing.T) { func TestDeprecatedQuery(t *testing.T) { fixture := initFixture(t) - out, err := runCmd(fixture.conn, fixture.b, buildModuleQueryCommand, "echo", + out, err := runCmd(fixture, buildModuleQueryCommand, "echo", "1", "abc", "--deprecated-field", "foo") assert.NilError(t, err) assert.Assert(t, strings.Contains(out.String(), "--deprecated-field has been deprecated")) - out, err = runCmd(fixture.conn, fixture.b, buildModuleQueryCommand, "echo", + out, err = runCmd(fixture, buildModuleQueryCommand, "echo", "1", "abc", "-s", "foo") assert.NilError(t, err) assert.Assert(t, strings.Contains(out.String(), "--shorthand-deprecated-field has been deprecated")) @@ -659,7 +667,7 @@ func TestBuildCustomQueryCommand(t *testing.T) { }, } - cmd, err := b.BuildQueryCommand(appOptions, map[string]*cobra.Command{ + cmd, err := b.BuildQueryCommand(context.Background(), appOptions, map[string]*cobra.Command{ "test": {Use: "test", Run: func(cmd *cobra.Command, args []string) { customCommandCalled = true }}, @@ -677,7 +685,7 @@ func TestNotFoundErrorsQuery(t *testing.T) { b.AddTxConnFlags = nil buildModuleQueryCommand := func(moduleName string, cmdDescriptor *autocliv1.ServiceCommandDescriptor) (*cobra.Command, error) { - cmd := topLevelCmd("query", "Querying subcommands") + cmd := topLevelCmd(context.Background(), "query", "Querying subcommands") err := b.AddMsgServiceCommands(cmd, cmdDescriptor) return cmd, err } @@ -727,7 +735,7 @@ func TestNotFoundErrorsQuery(t *testing.T) { func TestDurationMarshal(t *testing.T) { fixture := initFixture(t) - out, err := runCmd(fixture.conn, fixture.b, buildModuleQueryCommand, "echo", "1", "abc", "--duration", "1s") + out, err := runCmd(fixture, buildModuleQueryCommand, "echo", "1", "abc", "--duration", "1s") assert.NilError(t, err) assert.Assert(t, strings.Contains(out.String(), "duration: 1s")) } diff --git a/client/v2/autocli/util.go b/client/v2/autocli/util.go index ca9a1674f853..96276543e7db 100644 --- a/client/v2/autocli/util.go +++ b/client/v2/autocli/util.go @@ -1,6 +1,7 @@ package autocli import ( + "context" "strings" "github.com/spf13/cobra" @@ -31,14 +32,17 @@ func findSubCommand(cmd *cobra.Command, subCmdName string) *cobra.Command { // topLevelCmd creates a new top-level command with the provided name and // description. The command will have DisableFlagParsing set to false and // SuggestionsMinimumDistance set to 2. -func topLevelCmd(use, short string) *cobra.Command { - return &cobra.Command{ +func topLevelCmd(ctx context.Context, use, short string) *cobra.Command { + cmd := &cobra.Command{ Use: use, Short: short, DisableFlagParsing: false, SuggestionsMinimumDistance: 2, RunE: validateCmd, } + cmd.SetContext(ctx) + + return cmd } func protoNameToCliName(name protoreflect.Name) string { diff --git a/simapp/go.mod b/simapp/go.mod index 11caa1e452f5..f08f393956f9 100644 --- a/simapp/go.mod +++ b/simapp/go.mod @@ -4,7 +4,7 @@ go 1.21 require ( cosmossdk.io/api v0.7.2 - cosmossdk.io/client/v2 v2.0.0-beta.1 + cosmossdk.io/client/v2 v2.0.0-beta.1.0.20240118205803-02b5997c5192 cosmossdk.io/collections v0.4.0 // indirect cosmossdk.io/core v0.11.0 cosmossdk.io/depinject v1.0.0-alpha.4 @@ -21,7 +21,7 @@ require ( github.com/cometbft/cometbft v0.38.2 github.com/cosmos/cosmos-db v1.0.0 // this version is not used as it is always replaced by the latest Cosmos SDK version - github.com/cosmos/cosmos-sdk v0.50.2 + github.com/cosmos/cosmos-sdk v0.50.3 github.com/cosmos/gogoproto v1.4.11 github.com/golang/mock v1.6.0 github.com/spf13/cast v1.5.1 diff --git a/simapp/go.sum b/simapp/go.sum index e6acc3a9513a..976004c8a1b6 100644 --- a/simapp/go.sum +++ b/simapp/go.sum @@ -189,8 +189,8 @@ cloud.google.com/go/workflows v1.6.0/go.mod h1:6t9F5h/unJz41YqfBmqSASJSXccBLtD1V cloud.google.com/go/workflows v1.7.0/go.mod h1:JhSrZuVZWuiDfKEFxU0/F1PQjmpnpcoISEXH2bcHC3M= cosmossdk.io/api v0.7.2 h1:BO3i5fvKMKvfaUiMkCznxViuBEfyWA/k6w2eAF6q1C4= cosmossdk.io/api v0.7.2/go.mod h1:IcxpYS5fMemZGqyYtErK7OqvdM0C8kdW3dq8Q/XIG38= -cosmossdk.io/client/v2 v2.0.0-beta.1 h1:XkHh1lhrLYIT9zKl7cIOXUXg2hdhtjTPBUfqERNA1/Q= -cosmossdk.io/client/v2 v2.0.0-beta.1/go.mod h1:JEUSu9moNZQ4kU3ir1DKD5eU4bllmAexrGWjmb9k8qU= +cosmossdk.io/client/v2 v2.0.0-beta.1.0.20240118205803-02b5997c5192 h1:4lCp0JJlrRX0fN5T2ob87vUFplbFfDfDgvazQGIf95s= +cosmossdk.io/client/v2 v2.0.0-beta.1.0.20240118205803-02b5997c5192/go.mod h1:Fi+Bqmvo+7wImB5+31CsBheyjBkvQxx8QRQY1acPVDU= cosmossdk.io/collections v0.4.0 h1:PFmwj2W8szgpD5nOd8GWH6AbYNi1f2J6akWXJ7P5t9s= cosmossdk.io/collections v0.4.0/go.mod h1:oa5lUING2dP+gdDquow+QjlF45eL1t4TJDypgGd+tv0= cosmossdk.io/core v0.11.0 h1:vtIafqUi+1ZNAE/oxLOQQ7Oek2n4S48SWLG8h/+wdbo= diff --git a/tests/go.mod b/tests/go.mod index 00e27b59f5fb..2569120d47e1 100644 --- a/tests/go.mod +++ b/tests/go.mod @@ -20,7 +20,7 @@ require ( github.com/cosmos/cosmos-db v1.0.0 github.com/cosmos/cosmos-proto v1.0.0-beta.3 // this version is not used as it is always replaced by the latest Cosmos SDK version - github.com/cosmos/cosmos-sdk v0.50.2 + github.com/cosmos/cosmos-sdk v0.50.3 github.com/cosmos/gogoproto v1.4.11 github.com/golang/mock v1.6.0 github.com/spf13/cobra v1.7.0 @@ -37,7 +37,7 @@ require ( cloud.google.com/go/compute/metadata v0.2.3 // indirect cloud.google.com/go/iam v1.1.5 // indirect cloud.google.com/go/storage v1.30.1 // indirect - cosmossdk.io/client/v2 v2.0.0-beta.1 // indirect + cosmossdk.io/client/v2 v2.0.0-beta.1.0.20240118205803-02b5997c5192 // indirect cosmossdk.io/collections v0.4.0 // indirect cosmossdk.io/x/circuit v0.1.0 // indirect filippo.io/edwards25519 v1.0.0 // indirect diff --git a/tests/go.sum b/tests/go.sum index 0cab0d2af9d7..32a228ad5d48 100644 --- a/tests/go.sum +++ b/tests/go.sum @@ -189,8 +189,8 @@ cloud.google.com/go/workflows v1.6.0/go.mod h1:6t9F5h/unJz41YqfBmqSASJSXccBLtD1V cloud.google.com/go/workflows v1.7.0/go.mod h1:JhSrZuVZWuiDfKEFxU0/F1PQjmpnpcoISEXH2bcHC3M= cosmossdk.io/api v0.7.2 h1:BO3i5fvKMKvfaUiMkCznxViuBEfyWA/k6w2eAF6q1C4= cosmossdk.io/api v0.7.2/go.mod h1:IcxpYS5fMemZGqyYtErK7OqvdM0C8kdW3dq8Q/XIG38= -cosmossdk.io/client/v2 v2.0.0-beta.1 h1:XkHh1lhrLYIT9zKl7cIOXUXg2hdhtjTPBUfqERNA1/Q= -cosmossdk.io/client/v2 v2.0.0-beta.1/go.mod h1:JEUSu9moNZQ4kU3ir1DKD5eU4bllmAexrGWjmb9k8qU= +cosmossdk.io/client/v2 v2.0.0-beta.1.0.20240118205803-02b5997c5192 h1:4lCp0JJlrRX0fN5T2ob87vUFplbFfDfDgvazQGIf95s= +cosmossdk.io/client/v2 v2.0.0-beta.1.0.20240118205803-02b5997c5192/go.mod h1:Fi+Bqmvo+7wImB5+31CsBheyjBkvQxx8QRQY1acPVDU= cosmossdk.io/collections v0.4.0 h1:PFmwj2W8szgpD5nOd8GWH6AbYNi1f2J6akWXJ7P5t9s= cosmossdk.io/collections v0.4.0/go.mod h1:oa5lUING2dP+gdDquow+QjlF45eL1t4TJDypgGd+tv0= cosmossdk.io/core v0.11.0 h1:vtIafqUi+1ZNAE/oxLOQQ7Oek2n4S48SWLG8h/+wdbo=