-
Notifications
You must be signed in to change notification settings - Fork 4.1k
refactor(client/v2)!: remove client.Context #22493
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
Changes from all commits
962ed4b
528af7c
73d8d85
3ad45d6
004d071
a404604
30f57d3
9509d1d
06ffcbe
7e0583f
76556fc
e2af16d
3b2c8d5
f67063c
7482bef
f3320d6
5198686
1a8f7b5
68b5fed
fe28cec
4d47bc4
879db32
ed9d006
045e717
c6a699c
488c781
4401ad8
f81c59c
4727ae4
958c106
54254d4
79adc32
8c25235
ae69fec
dc85d30
2f4029a
6722779
f9b7320
6e4a04b
2be9f51
d0938d3
e0abf39
dab815d
2ff6fab
7421b85
2949a28
11ebebf
9c7eb26
e63aa2b
ff44a71
e289671
6cafaa8
6e1f112
a509f03
d8909ed
ae00cea
d4b36f9
d63f468
84891ce
f67151d
e6e1759
e2e2a03
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,29 @@ | ||
| package autocli | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/tls" | ||
| "fmt" | ||
| "strings" | ||
| "strconv" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "google.golang.org/grpc" | ||
| "google.golang.org/grpc/credentials" | ||
| grpcinsecure "google.golang.org/grpc/credentials/insecure" | ||
| "google.golang.org/protobuf/reflect/protoreflect" | ||
| "sigs.k8s.io/yaml" | ||
|
|
||
| autocliv1 "cosmossdk.io/api/cosmos/autocli/v1" | ||
| apitxsigning "cosmossdk.io/api/cosmos/tx/signing/v1beta1" | ||
| "cosmossdk.io/client/v2/autocli/config" | ||
| "cosmossdk.io/client/v2/autocli/keyring" | ||
| "cosmossdk.io/client/v2/broadcast/comet" | ||
| clientcontext "cosmossdk.io/client/v2/context" | ||
| "cosmossdk.io/client/v2/internal/flags" | ||
| "cosmossdk.io/client/v2/internal/print" | ||
| "cosmossdk.io/client/v2/internal/util" | ||
|
|
||
| "github.com/cosmos/cosmos-sdk/client" | ||
| "github.com/cosmos/cosmos-sdk/codec" | ||
| "github.com/cosmos/cosmos-sdk/types/tx/signing" | ||
| ) | ||
|
|
||
| type cmdType int | ||
|
|
@@ -62,8 +73,13 @@ | |
| } | ||
| cmd.Args = binder.CobraArgs | ||
|
|
||
| cmd.PreRunE = b.preRunE() | ||
|
|
||
| cmd.RunE = func(cmd *cobra.Command, args []string) error { | ||
| ctx = cmd.Context() | ||
| ctx, err = b.getContext(cmd) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| input, err := binder.BuildMessage(args) | ||
| if err != nil { | ||
|
|
@@ -237,27 +253,132 @@ | |
|
|
||
| // outOrStdoutFormat formats the output based on the output flag and writes it to the command's output stream. | ||
| func (b *Builder) outOrStdoutFormat(cmd *cobra.Command, out []byte) error { | ||
| clientCtx := client.Context{} | ||
| if v := cmd.Context().Value(client.ClientContextKey); v != nil { | ||
| clientCtx = *(v.(*client.Context)) | ||
| p, err := print.NewPrinter(cmd) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return p.PrintBytes(out) | ||
| } | ||
|
|
||
| // getContext creates and returns a new context.Context with an autocli.Context value. | ||
| // It initializes a printer and, if necessary, a keyring based on command flags. | ||
| func (b *Builder) getContext(cmd *cobra.Command) (context.Context, error) { | ||
| // if the command uses the keyring this must be set | ||
| var ( | ||
| k keyring.Keyring | ||
| err error | ||
| ) | ||
| if cmd.Flags().Lookup(flags.FlagKeyringDir) != nil && cmd.Flags().Lookup(flags.FlagKeyringBackend) != nil { | ||
| k, err = keyring.NewKeyringFromFlags(cmd.Flags(), b.AddressCodec, cmd.InOrStdin(), b.Cdc) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| } else { | ||
| k = keyring.NoKeyring{} | ||
| } | ||
| flagSet := cmd.Flags() | ||
| if clientCtx.OutputFormat == "" || flagSet.Changed(flags.FlagOutput) { | ||
| output, _ := flagSet.GetString(flags.FlagOutput) | ||
| clientCtx = clientCtx.WithOutputFormat(output) | ||
|
|
||
| clientCtx := clientcontext.Context{ | ||
| Flags: cmd.Flags(), | ||
| AddressCodec: b.AddressCodec, | ||
| ValidatorAddressCodec: b.ValidatorAddressCodec, | ||
| ConsensusAddressCodec: b.ConsensusAddressCodec, | ||
| Cdc: b.Cdc, | ||
| Keyring: k, | ||
| EnabledSignModes: signModesToApiSignModes(b.EnabledSignModes), | ||
| } | ||
|
|
||
| var err error | ||
| outputType := clientCtx.OutputFormat | ||
| // if the output type is text, convert the json to yaml | ||
| // if output type is json or nil, default to json | ||
| if outputType == flags.OutputFormatText { | ||
| out, err = yaml.JSONToYAML(out) | ||
| return clientcontext.SetInContext(cmd.Context(), clientCtx), nil | ||
| } | ||
|
|
||
| // preRunE returns a function that sets flags from the configuration before running a command. | ||
| // It is used as a PreRunE hook for cobra commands to ensure flags are properly initialized | ||
| // from the configuration before command execution. | ||
| func (b *Builder) preRunE() func(cmd *cobra.Command, args []string) error { | ||
| return func(cmd *cobra.Command, args []string) error { | ||
| err := b.setFlagsFromConfig(cmd) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
| } | ||
|
|
||
| // setFlagsFromConfig sets command flags from the provided configuration. | ||
| // It only sets flags that haven't been explicitly changed by the user. | ||
| func (b *Builder) setFlagsFromConfig(cmd *cobra.Command) error { | ||
| conf, err := config.CreateClientConfigFromFlags(cmd.Flags()) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| flagsToSet := map[string]string{ | ||
| flags.FlagChainID: conf.ChainID, | ||
| flags.FlagKeyringBackend: conf.KeyringBackend, | ||
| flags.FlagFrom: conf.KeyringDefaultKeyName, | ||
| flags.FlagOutput: conf.Output, | ||
| flags.FlagNode: conf.Node, | ||
| flags.FlagBroadcastMode: conf.BroadcastMode, | ||
| flags.FlagGrpcAddress: conf.GRPC.Address, | ||
| flags.FlagGrpcInsecure: strconv.FormatBool(conf.GRPC.Insecure), | ||
| } | ||
|
|
||
| for flagName, value := range flagsToSet { | ||
| if flag := cmd.Flags().Lookup(flagName); flag != nil && !cmd.Flags().Changed(flagName) { | ||
| if err := cmd.Flags().Set(flagName, value); err != nil { | ||
| return err | ||
| } | ||
| } | ||
| } | ||
Check warningCode scanning / CodeQL Iteration over map Warning
Iteration over map may be a possible source of non-determinism
|
||
|
|
||
| cmd.Println(strings.TrimSpace(string(out))) | ||
| return nil | ||
| } | ||
|
|
||
| // getQueryClientConn returns a function that creates a gRPC client connection based on command flags. | ||
| // It handles the creation of secure or insecure connections and falls back to a CometBFT broadcaster | ||
| // if no gRPC address is specified. | ||
| func getQueryClientConn(cdc codec.Codec) func(cmd *cobra.Command) (grpc.ClientConnInterface, error) { | ||
| return func(cmd *cobra.Command) (grpc.ClientConnInterface, error) { | ||
| var err error | ||
| creds := grpcinsecure.NewCredentials() | ||
|
|
||
| insecure := true | ||
| if cmd.Flags().Lookup(flags.FlagGrpcInsecure) != nil { | ||
| insecure, err = cmd.Flags().GetBool(flags.FlagGrpcInsecure) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
| if !insecure { | ||
| creds = credentials.NewTLS(&tls.Config{MinVersion: tls.VersionTLS12}) | ||
| } | ||
|
Comment on lines
+353
to
+354
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Set explicit server name in TLS configuration When creating TLS credentials with Modify the TLS configuration to include the server name derived from the address: if !insecure {
- creds = credentials.NewTLS(&tls.Config{MinVersion: tls.VersionTLS12})
+ tlsConfig := &tls.Config{
+ MinVersion: tls.VersionTLS12,
+ ServerName: extractHostname(addr),
+ }
+ creds = credentials.NewTLS(tlsConfig)
}Implement a helper function
|
||
|
|
||
| var addr string | ||
| if cmd.Flags().Lookup(flags.FlagGrpcAddress) != nil { | ||
| addr, err = cmd.Flags().GetString(flags.FlagGrpcAddress) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
| if addr == "" { | ||
| // if grpc-addr has not been set, use the default clientConn | ||
| // TODO: default is comet | ||
| node, err := cmd.Flags().GetString(flags.FlagNode) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return comet.NewCometBFTBroadcaster(node, comet.BroadcastSync, cdc) | ||
| } | ||
|
|
||
| return grpc.NewClient(addr, []grpc.DialOption{grpc.WithTransportCredentials(creds)}...) | ||
JulianToledano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
facundomedica marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
JulianToledano marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // signModesToApiSignModes converts a slice of signing.SignMode to a slice of apitxsigning.SignMode. | ||
| func signModesToApiSignModes(modes []signing.SignMode) []apitxsigning.SignMode { | ||
| r := make([]apitxsigning.SignMode, len(modes)) | ||
| for i, m := range modes { | ||
| r[i] = apitxsigning.SignMode(m) | ||
| } | ||
| return r | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.