From bab2b1a7ca27ce8a33c7c35b6a937288fe5cf3b7 Mon Sep 17 00:00:00 2001 From: Amaury <1293565+amaurym@users.noreply.github.com> Date: Mon, 20 Mar 2023 09:22:13 +0100 Subject: [PATCH 1/2] feat(client): Add cobra's context to clientCtx --- client/context.go | 11 +++++++++++ client/tx/tx.go | 3 +-- simapp/simd/cmd/root.go | 1 + simapp/simd/cmd/testnet.go | 4 +--- testutil/network/network.go | 3 +-- testutil/sims/tx_helpers.go | 6 +++--- x/auth/client/cli/tx_multisign.go | 7 ++----- x/auth/client/cli/validate_sigs.go | 5 ++--- x/auth/client/tx.go | 7 ++----- 9 files changed, 24 insertions(+), 23 deletions(-) diff --git a/client/context.go b/client/context.go index 2bcbecd83727..d73d72e21543 100644 --- a/client/context.go +++ b/client/context.go @@ -2,6 +2,7 @@ package client import ( "bufio" + "context" "encoding/json" "fmt" "io" @@ -61,6 +62,16 @@ type Context struct { // TODO: Deprecated (remove). LegacyAmino *codec.LegacyAmino + + // CmdContext is the context.Context from the Cobra command. + CmdContext context.Context +} + +// WithCmdContext returns a copy of the context with an updated context.Context, +// usually set to the cobra cmd context. +func (ctx Context) WithCmdContext(c context.Context) Context { + ctx.CmdContext = c + return ctx } // WithKeyring returns a copy of the context with an updated keyring. diff --git a/client/tx/tx.go b/client/tx/tx.go index 0e4e9ac96609..31c94eceb76b 100644 --- a/client/tx/tx.go +++ b/client/tx/tx.go @@ -116,8 +116,7 @@ func BroadcastTx(clientCtx client.Context, txf Factory, msgs ...sdk.Msg) error { } } - // When Textual is wired up, the context argument should be retrieved from the client context. - err = Sign(context.TODO(), txf, clientCtx.GetFromName(), tx, true) + err = Sign(clientCtx.CmdContext, txf, clientCtx.GetFromName(), tx, true) if err != nil { return err } diff --git a/simapp/simd/cmd/root.go b/simapp/simd/cmd/root.go index f572b81545f7..2780fe4005b0 100644 --- a/simapp/simd/cmd/root.go +++ b/simapp/simd/cmd/root.go @@ -69,6 +69,7 @@ func NewRootCmd() *cobra.Command { cmd.SetOut(cmd.OutOrStdout()) cmd.SetErr(cmd.ErrOrStderr()) + initClientCtx = initClientCtx.WithCmdContext(cmd.Context()) initClientCtx, err := client.ReadPersistentCommandFlags(initClientCtx, cmd.Flags()) if err != nil { return err diff --git a/simapp/simd/cmd/testnet.go b/simapp/simd/cmd/testnet.go index 08445a550d77..1e6fecbb6733 100644 --- a/simapp/simd/cmd/testnet.go +++ b/simapp/simd/cmd/testnet.go @@ -2,7 +2,6 @@ package cmd import ( "bufio" - "context" "encoding/json" "fmt" "net" @@ -324,8 +323,7 @@ func initTestnetFiles( WithKeybase(kb). WithTxConfig(clientCtx.TxConfig) - // When Textual is wired up, the context argument should be retrieved from the client context. - if err := tx.Sign(context.TODO(), txFactory, nodeDirName, txBuilder, true); err != nil { + if err := tx.Sign(cmd.Context(), txFactory, nodeDirName, txBuilder, true); err != nil { return err } diff --git a/testutil/network/network.go b/testutil/network/network.go index 14655c4ad31a..8ed0bd5354c2 100644 --- a/testutil/network/network.go +++ b/testutil/network/network.go @@ -546,8 +546,7 @@ func New(l Logger, baseDir string, cfg Config) (*Network, error) { WithKeybase(kb). WithTxConfig(cfg.TxConfig) - // When Textual is wired up, the context argument should be retrieved from the client context. - err = tx.Sign(context.TODO(), txFactory, nodeDirName, txBuilder, true) + err = tx.Sign(context.Background(), txFactory, nodeDirName, txBuilder, true) if err != nil { return nil, err } diff --git a/testutil/sims/tx_helpers.go b/testutil/sims/tx_helpers.go index df546a7f3696..e6daeb06cc85 100644 --- a/testutil/sims/tx_helpers.go +++ b/testutil/sims/tx_helpers.go @@ -1,6 +1,7 @@ package sims import ( + "context" "math/rand" "testing" "time" @@ -61,9 +62,8 @@ func GenSignedMockTx(r *rand.Rand, txConfig client.TxConfig, msgs []sdk.Msg, fee Sequence: accSeqs[i], PubKey: p.PubKey(), } - // When Textual is wired up, use GetSignBytesWithContext - // ref: https://github.com/cosmos/cosmos-sdk/issues/13747 - signBytes, err := txConfig.SignModeHandler().GetSignBytes(signMode, signerData, tx.GetTx()) + + signBytes, err := authsign.GetSignBytesWithContext(txConfig.SignModeHandler(), context.Background(), signMode, signerData, tx.GetTx()) if err != nil { panic(err) } diff --git a/x/auth/client/cli/tx_multisign.go b/x/auth/client/cli/tx_multisign.go index 2094ac6a0672..34349ec858aa 100644 --- a/x/auth/client/cli/tx_multisign.go +++ b/x/auth/client/cli/tx_multisign.go @@ -1,7 +1,6 @@ package cli import ( - "context" "fmt" "os" "strings" @@ -133,8 +132,7 @@ func makeMultiSignCmd() func(cmd *cobra.Command, args []string) (err error) { PubKey: sig.PubKey, } - // When Textual is wired up, the context argument should be retrieved from the client context. - err = signing.VerifySignature(context.TODO(), sig.PubKey, signingData, sig.Data, txCfg.SignModeHandler(), txBuilder.GetTx()) + err = signing.VerifySignature(cmd.Context(), sig.PubKey, signingData, sig.Data, txCfg.SignModeHandler(), txBuilder.GetTx()) if err != nil { addr, _ := sdk.AccAddressFromHexUnsafe(sig.PubKey.Address().String()) return fmt.Errorf("couldn't verify signature for address %s", addr) @@ -306,8 +304,7 @@ func makeBatchMultisignCmd() func(cmd *cobra.Command, args []string) error { } for _, sig := range signatureBatch { - // When Textual is wired up, the context argument should be retrieved from the client context. - err = signing.VerifySignature(context.TODO(), sig[i].PubKey, signingData, sig[i].Data, txCfg.SignModeHandler(), txBldr.GetTx()) + err = signing.VerifySignature(cmd.Context(), sig[i].PubKey, signingData, sig[i].Data, txCfg.SignModeHandler(), txBldr.GetTx()) if err != nil { return fmt.Errorf("couldn't verify signature: %w %v", err, sig) } diff --git a/x/auth/client/cli/validate_sigs.go b/x/auth/client/cli/validate_sigs.go index 54bfaa0406fb..6a0b8156c7ff 100644 --- a/x/auth/client/cli/validate_sigs.go +++ b/x/auth/client/cli/validate_sigs.go @@ -1,7 +1,6 @@ package cli import ( - "context" "fmt" "github.com/spf13/cobra" @@ -112,8 +111,8 @@ func printAndValidateSigs( Sequence: accSeq, PubKey: pubKey, } - // When Textual is wired up, the context argument should be retrieved from the client context. - err = authsigning.VerifySignature(context.TODO(), pubKey, signingData, sig.Data, signModeHandler, sigTx) + + err = authsigning.VerifySignature(cmd.Context(), pubKey, signingData, sig.Data, signModeHandler, sigTx) if err != nil { return false } diff --git a/x/auth/client/tx.go b/x/auth/client/tx.go index a5091de9e1ec..7a3cf8863618 100644 --- a/x/auth/client/tx.go +++ b/x/auth/client/tx.go @@ -3,7 +3,6 @@ package client import ( "bufio" "bytes" - "context" "fmt" "io" "os" @@ -59,8 +58,7 @@ func SignTx(txFactory tx.Factory, clientCtx client.Context, name string, txBuild } } - // When Textual is wired up, the context argument should be retrieved from the client context. - return tx.Sign(context.TODO(), txFactory, name, txBuilder, overwriteSig) + return tx.Sign(clientCtx.CmdContext, txFactory, name, txBuilder, overwriteSig) } // SignTxWithSignerAddress attaches a signature to a transaction. @@ -88,8 +86,7 @@ func SignTxWithSignerAddress(txFactory tx.Factory, clientCtx client.Context, add } } - // When Textual is wired up, the context argument should be retrieved from the client context. - return tx.Sign(context.TODO(), txFactory, name, txBuilder, overwrite) + return tx.Sign(clientCtx.CmdContext, txFactory, name, txBuilder, overwrite) } // Read and decode a StdTx from the given filename. Can pass "-" to read from stdin. From 2deb48742cd30e2299a8c0d62a5db365e2692052 Mon Sep 17 00:00:00 2001 From: Amaury <1293565+amaurym@users.noreply.github.com> Date: Mon, 20 Mar 2023 09:28:07 +0100 Subject: [PATCH 2/2] Add changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index af35701bdf3a..dadeb80db7b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Features * (x/bank) [#15265](https://github.com/cosmos/cosmos-sdk/pull/15265) Update keeper interface to include `GetAllDenomMetaData`. +* (client) [#15458](https://github.com/cosmos/cosmos-sdk/pull/15458) Add a `CmdContext` field to client.Context initialized to cobra command's context. * (core) [#15133](https://github.com/cosmos/cosmos-sdk/pull/15133) Implement RegisterServices in the module manager. * (x/gov) [#14373](https://github.com/cosmos/cosmos-sdk/pull/14373) Add new proto field `constitution` of type `string` to gov module genesis state, which allows chain builders to lay a strong foundation by specifying purpose. * (x/genutil) [#15301](https://github.com/cosmos/cosmos-sdk/pull/15031) Add application genesis. The genesis is now entirely managed by the application and passed to CometBFT at note instantiation. Functions that were taking a `cmttypes.GenesisDoc{}` now takes a `genutiltypes.AppGenesis{}`.