Skip to content
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

feat(client): Add cobra's context to clientCtx #15458

Merged
merged 4 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: rename to Context, or is there a reason it called cmdContext?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The struct is already called Context. I thought being explicit here was useful to know what we're talking about. Can rename to simply Context if other people also feel the same.

}

// 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