Skip to content

Commit

Permalink
feat(client): Add cobra's context to clientCtx (#15458)
Browse files Browse the repository at this point in the history
Co-authored-by: Facundo Medica <14063057+facundomedica@users.noreply.github.com>
  • Loading branch information
amaury1093 and facundomedica committed Mar 20, 2023
1 parent f4a6feb commit a4a3c81
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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{}`.
Expand Down
11 changes: 11 additions & 0 deletions client/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"bufio"
"context"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions client/tx/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions simapp/simd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions simapp/simd/cmd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"bufio"
"context"
"encoding/json"
"fmt"
"net"
Expand Down Expand Up @@ -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
}

Expand Down
3 changes: 1 addition & 2 deletions testutil/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions testutil/sims/tx_helpers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sims

import (
"context"
"math/rand"
"testing"
"time"
Expand Down Expand Up @@ -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)
}
Expand Down
7 changes: 2 additions & 5 deletions x/auth/client/cli/tx_multisign.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cli

import (
"context"
"fmt"
"os"
"strings"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down
5 changes: 2 additions & 3 deletions x/auth/client/cli/validate_sigs.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cli

import (
"context"
"fmt"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -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
}
Expand Down
7 changes: 2 additions & 5 deletions x/auth/client/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package client
import (
"bufio"
"bytes"
"context"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit a4a3c81

Please sign in to comment.