Skip to content

Commit

Permalink
update: flags, remove confirmation
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianToledano committed Nov 22, 2024
1 parent 2949a28 commit 11ebebf
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 124 deletions.
74 changes: 24 additions & 50 deletions client/v2/autocli/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"strconv"

flags2 "github.com/cosmos/cosmos-sdk/client/flags"

"github.com/spf13/cobra"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
Expand Down Expand Up @@ -268,7 +270,7 @@ func (b *Builder) getContext(cmd *cobra.Command) (context.Context, error) {
k keyring.Keyring
err error
)
if cmd.Flags().Lookup("keyring-dir") != nil && cmd.Flags().Lookup("keyring-backend") != nil {
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
Expand All @@ -283,7 +285,6 @@ func (b *Builder) getContext(cmd *cobra.Command) (context.Context, error) {
ValidatorAddressCodec: b.ValidatorAddressCodec,
ConsensusAddressCodec: b.ConsensusAddressCodec,
Cdc: b.Cdc,
OutputWriter: cmd.OutOrStdout(),
Keyring: k,
EnabledSignmodes: signModesToApiSignModes(b.EnabledSignModes),
}
Expand All @@ -305,57 +306,30 @@ func (b *Builder) preRunE() func(cmd *cobra.Command, args []string) error {
}
}

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

if cmd.Flags().Lookup("chain-id") != nil && !cmd.Flags().Changed("chain-id") {
if err = cmd.Flags().Set("chain-id", conf.ChainID); err != nil {
return err
}
}

if cmd.Flags().Lookup("keyring-backend") != nil && !cmd.Flags().Changed("keyring-backend") {
if err = cmd.Flags().Set("keyring-backend", conf.KeyringBackend); err != nil {
return err
}
}

if cmd.Flags().Lookup("from") != nil && !cmd.Flags().Changed("from") {
if err = cmd.Flags().Set("from", conf.KeyringDefaultKeyName); err != nil {
return err
}
}

if cmd.Flags().Lookup("output") != nil && !cmd.Flags().Changed("output") {
if err = cmd.Flags().Set("output", conf.Output); err != nil {
return err
}
}

if cmd.Flags().Lookup("node") != nil && !cmd.Flags().Changed("node") {
if err = cmd.Flags().Set("node", conf.Node); err != nil {
return err
}
}

if cmd.Flags().Lookup("broadcast-mode") != nil && !cmd.Flags().Changed("broadcast-mode") {
if err = cmd.Flags().Set("broadcast-mode", conf.BroadcastMode); 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,
flags2.FlagGRPCInsecure: strconv.FormatBool(conf.GRPC.Insecure),
}

if cmd.Flags().Lookup("grpc-addr") != nil && !cmd.Flags().Changed("grpc-addr") {
if err = cmd.Flags().Set("grpc-addr", conf.GRPC.Address); err != nil {
return err
}
}

if cmd.Flags().Lookup("grpc-insecure") != nil && !cmd.Flags().Changed("grpc-insecure") {
if err = cmd.Flags().Set("grpc-insecure", strconv.FormatBool(conf.GRPC.Insecure)); err != nil {
return err
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 warning

Code scanning / CodeQL

Iteration over map Warning

Iteration over map may be a possible source of non-determinism

Expand All @@ -371,8 +345,8 @@ func getQueryClientConn(cdc codec.Codec) func(cmd *cobra.Command) (grpc.ClientCo
creds := grpcinsecure.NewCredentials()

insecure := true
if cmd.Flags().Lookup("grpc-insecure") != nil {
insecure, err = cmd.Flags().GetBool("grpc-insecure")
if cmd.Flags().Lookup(flags.FlagGrpcInsecure) != nil {
insecure, err = cmd.Flags().GetBool(flags.FlagGrpcInsecure)
if err != nil {
return nil, err
}
Expand All @@ -382,16 +356,16 @@ func getQueryClientConn(cdc codec.Codec) func(cmd *cobra.Command) (grpc.ClientCo
}

var addr string
if cmd.Flags().Lookup("grpc-addr") != nil {
addr, err = cmd.Flags().GetString("grpc-addr")
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("node")
node, err := cmd.Flags().GetString(flags.FlagNode)
if err != nil {
return nil, err
}
Expand Down
4 changes: 0 additions & 4 deletions client/v2/autocli/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package context
import (
gocontext "context"
"errors"
"io"

"github.com/spf13/pflag"

apisigning "cosmossdk.io/api/cosmos/tx/signing/v1beta1"
Expand All @@ -31,8 +29,6 @@ type Context struct {

Cdc codec.Codec

OutputWriter io.Writer

Keyring keyring.Keyring

EnabledSignmodes []apisigning.SignMode
Expand Down
18 changes: 18 additions & 0 deletions client/v2/internal/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package flags

// This defines flag names that can be used in autocli.
const (
// FlagChainID is the flag to specify the chain ID of the network.
FlagChainID = "chain-id"

// FlagFrom is the flag to set the from address with which to sign the transaction.
FlagFrom = "from"

Expand All @@ -14,9 +17,24 @@ const (
// FlagNoPrompt is the flag to not use a prompt for commands.
FlagNoPrompt = "no-prompt"

// FlagKeyringDir is the flag to specify the directory where the keyring is stored.
FlagKeyringDir = "keyring-dir"
// FlagKeyringBackend is the flag to specify which backend to use for the keyring (e.g. os, file, test).
FlagKeyringBackend = "keyring-backend"

// FlagNoProposal is the flag convert a gov proposal command into a normal command.
// This is used to allow user of chains with custom authority to not use gov submit proposals for usual proposal commands.
FlagNoProposal = "no-proposal"

// FlagNode is the flag to specify the node address to connect to.
FlagNode = "node"
// FlagBroadcastMode is the flag to specify the broadcast mode for transactions.
FlagBroadcastMode = "broadcast-mode"

// FlagGrpcAddress is the flag to specify the gRPC server address to connect to.
FlagGrpcAddress = "grpc-addr"
// FlagGrpcInsecure is the flag to allow insecure gRPC connections.
FlagGrpcInsecure = "grpc-insecure"
)

// List of supported output formats
Expand Down
6 changes: 4 additions & 2 deletions client/v2/internal/print/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import (

"github.com/spf13/cobra"
"sigs.k8s.io/yaml"

"cosmossdk.io/client/v2/internal/flags"
)

const (
jsonOutput = "json"
textOutput = "text"
jsonOutput = flags.OutputFormatJSON
textOutput = flags.OutputFormatText
)

// Printer handles formatted output of different types of data
Expand Down
4 changes: 2 additions & 2 deletions client/v2/offchain/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ func SignFile() *cobra.Command {
}

keyringBackend := c.KeyringBackend
if !cmd.Flags().Changed(flags.FlagKeyringBackend) {
_ = cmd.Flags().Set(flags.FlagKeyringBackend, keyringBackend)
if !cmd.Flags().Changed(v2flags.FlagKeyringBackend) {
_ = cmd.Flags().Set(v2flags.FlagKeyringBackend, keyringBackend)
}

bz, err := os.ReadFile(args[1])
Expand Down
71 changes: 5 additions & 66 deletions client/v2/tx/tx.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package tx

import (
"bufio"
"context"
"errors"
"fmt"
"os"

"github.com/cosmos/gogoproto/grpc"
"github.com/cosmos/gogoproto/proto"
"github.com/spf13/pflag"
Expand All @@ -16,10 +12,9 @@ import (
"cosmossdk.io/client/v2/broadcast"
"cosmossdk.io/client/v2/broadcast/comet"
"cosmossdk.io/client/v2/internal/account"
"cosmossdk.io/client/v2/internal/print"
"cosmossdk.io/client/v2/internal/flags"
"cosmossdk.io/core/transaction"

"github.com/cosmos/cosmos-sdk/client/input"
"github.com/cosmos/cosmos-sdk/codec"
)

Expand Down Expand Up @@ -55,8 +50,7 @@ func GenerateOrBroadcastTxCLIWithBroadcaster(
return dryRun(txf, msgs...)
}

skipConfirm, _ := clientCtx.Flags.GetBool("yes")
return BroadcastTx(ctx, txf, broadcaster, skipConfirm, msgs...)
return BroadcastTx(ctx, txf, broadcaster, msgs...)
}

// GenerateOrBroadcastTxCLI will either generate and print an unsigned transaction
Expand All @@ -81,8 +75,8 @@ func GenerateOrBroadcastTxCLI(

// getCometBroadcaster returns a new CometBFT broadcaster based on the provided context and flag set.
func getCometBroadcaster(cdc codec.Codec, flagSet *pflag.FlagSet) (broadcast.Broadcaster, error) {
url, _ := flagSet.GetString("node")
mode, _ := flagSet.GetString("broadcast-mode")
url, _ := flagSet.GetString(flags.FlagNode)
mode, _ := flagSet.GetString(flags.FlagBroadcastMode)
return comet.NewCometBFTBroadcaster(url, mode, cdc)
}

Expand Down Expand Up @@ -166,13 +160,7 @@ func SimulateTx(ctx clientcontext.Context, conn grpc.ClientConn, msgs ...transac
// BroadcastTx attempts to generate, sign and broadcast a transaction with the
// given set of messages. It will also simulate gas requirements if necessary.
// It will return an error upon failure.
func BroadcastTx(
ctx context.Context,
txf Factory,
broadcaster broadcast.Broadcaster,
skipConfirm bool,
msgs ...transaction.Msg,
) ([]byte, error) {
func BroadcastTx(ctx context.Context, txf Factory, broadcaster broadcast.Broadcaster, msgs ...transaction.Msg) ([]byte, error) {
if txf.simulateAndExecute() {
err := txf.calculateGas(msgs...)
if err != nil {
Expand All @@ -185,55 +173,6 @@ func BroadcastTx(
return nil, err
}

if !skipConfirm {
encoder := txf.txConfig.TxJSONEncoder()
if encoder == nil {
return nil, errors.New("failed to encode transaction: tx json encoder is nil")
}

unsigTx, err := txf.getTx()
if err != nil {
return nil, err
}
txBytes, err := encoder(unsigTx)
if err != nil {
return nil, fmt.Errorf("failed to encode transaction: %w", err)
}

clientCtx, err := clientcontext.ClientContextFromGoContext(ctx)
if err == nil {
format, err := clientCtx.Flags.GetString("format")
if err != nil {
return nil, err
}
printer := print.Printer{
Output: clientCtx.OutputWriter,
OutputFormat: format,
}
if err := printer.PrintRaw(txBytes); err != nil {
_, err = fmt.Fprintf(os.Stderr, "error: %v\n%s\n", err, txBytes)
return nil, err
}
} else {
// default output writer and format
_, err = os.Stdout.Write(txBytes)
if err != nil {
return nil, err
}
}

buf := bufio.NewReader(os.Stdin)
ok, err := input.GetConfirmation("confirm transaction before signing and broadcasting", buf, os.Stderr)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error: %v\ncanceled transaction\n", err)
return nil, err
}
if !ok {
_, _ = fmt.Fprintln(os.Stderr, "canceled transaction")
return nil, nil
}
}

signedTx, err := txf.sign(ctx, true)
if err != nil {
return nil, err
Expand Down

0 comments on commit 11ebebf

Please sign in to comment.