Skip to content

Commit

Permalink
Merge PR #2874: gaiad gentx subcommands refactoring
Browse files Browse the repository at this point in the history
* gaiad gentx subcommands refactoring

- Replace STDIN/STDOUT redirection in `gaiad gentx` with subcommands
  command line options to redirect streams to file since viper does
  not handle redirection well.
- Use `BuildCreateValidatorMsg` to build a `MsgCreateValidator` rather
  than redirecting to `gaiacli tx stake create-validator`.
- `PrintUnsignedStdTx` now takes an `io.Writer` object.
- Mark `--pubkey`, `--amount` and `--moniker` as required flags
  instead of validating them manually.
- Use stake.NewDescription() to make a new Description - ref #2835

* Refresh PENDING.md
  • Loading branch information
alessio authored and cwgoes committed Nov 21, 2018
1 parent 1ea0e4c commit 3e68e44
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 94 deletions.
1 change: 1 addition & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ BREAKING CHANGES
* [cli] [\#2786](https://github.com/cosmos/cosmos-sdk/pull/2786) Fix redelegation command flow
* [cli] [\#2829](https://github.com/cosmos/cosmos-sdk/pull/2829) add-genesis-account command now validates state when adding accounts
* [cli] [\#2804](https://github.com/cosmos/cosmos-sdk/issues/2804) Check whether key exists before passing it on to `tx create-validator`.
* [cli] [\#2874](https://github.com/cosmos/cosmos-sdk/pull/2874) `gaiacli tx sign` takes an optional `--output-document` flag to support output redirection.

* Gaia

Expand Down
5 changes: 3 additions & 2 deletions client/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"bytes"
"fmt"
"io"
"os"

"github.com/cosmos/cosmos-sdk/client/context"
Expand Down Expand Up @@ -88,7 +89,7 @@ func CalculateGas(queryFunc func(string, common.HexBytes) ([]byte, error), cdc *

// PrintUnsignedStdTx builds an unsigned StdTx and prints it to os.Stdout.
// Don't perform online validation or lookups if offline is true.
func PrintUnsignedStdTx(txBldr authtxb.TxBuilder, cliCtx context.CLIContext, msgs []sdk.Msg, offline bool) (err error) {
func PrintUnsignedStdTx(w io.Writer, txBldr authtxb.TxBuilder, cliCtx context.CLIContext, msgs []sdk.Msg, offline bool) (err error) {
var stdTx auth.StdTx
if offline {
stdTx, err = buildUnsignedStdTxOffline(txBldr, cliCtx, msgs)
Expand All @@ -100,7 +101,7 @@ func PrintUnsignedStdTx(txBldr authtxb.TxBuilder, cliCtx context.CLIContext, msg
}
json, err := txBldr.Codec.MarshalJSON(stdTx)
if err == nil {
fmt.Printf("%s\n", json)
fmt.Fprintf(w, "%s\n", json)
}
return
}
Expand Down
36 changes: 23 additions & 13 deletions cmd/gaia/init/gentx.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@ import (
"os"
"path/filepath"

"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/client/utils"
"github.com/cosmos/cosmos-sdk/cmd/gaia/app"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/server"
sdk "github.com/cosmos/cosmos-sdk/types"
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder"
"github.com/cosmos/cosmos-sdk/x/stake/client/cli"
stakeTypes "github.com/cosmos/cosmos-sdk/x/stake/types"
"github.com/spf13/cobra"
"github.com/spf13/viper"
cfg "github.com/tendermint/tendermint/config"
"github.com/tendermint/tendermint/crypto"
tmcli "github.com/tendermint/tendermint/libs/cli"
Expand Down Expand Up @@ -80,26 +84,33 @@ following delegation and commission default parameters:
}
// Run gaiad tx create-validator
prepareFlagsForTxCreateValidator(config, nodeID, ip, genDoc.ChainID, valPubKey)
createValidatorCmd := cli.GetCmdCreateValidator(cdc)
cliCtx, txBldr, msg, err := cli.BuildCreateValidatorMsg(
context.NewCLIContext().WithCodec(cdc),
authtxb.NewTxBuilderFromCLI().WithCodec(cdc),
)
if err != nil {
return err
}

w, err := ioutil.TempFile("", "gentx")
if err != nil {
return err
}
unsignedGenTxFilename := w.Name()
defer os.Remove(unsignedGenTxFilename)
os.Stdout = w
if err = createValidatorCmd.RunE(nil, args); err != nil {

if err := utils.PrintUnsignedStdTx(w, txBldr, cliCtx, []sdk.Msg{msg}, true); err != nil {
return err
}
w.Close()

prepareFlagsForTxSign()
signCmd := authcmd.GetSignCommand(cdc)
if w, err = prepareOutputFile(config.RootDir, nodeID); err != nil {

outputDocument, err := makeOutputFilepath(config.RootDir, nodeID)
if err != nil {
return err
}
os.Stdout = w
viper.Set("output-document", outputDocument)
return signCmd.RunE(nil, []string{unsignedGenTxFilename})
},
}
Expand Down Expand Up @@ -145,11 +156,10 @@ func prepareFlagsForTxSign() {
viper.Set("offline", true)
}

func prepareOutputFile(rootDir, nodeID string) (w *os.File, err error) {
func makeOutputFilepath(rootDir, nodeID string) (string, error) {
writePath := filepath.Join(rootDir, "config", "gentx")
if err = common.EnsureDir(writePath, 0700); err != nil {
return
if err := common.EnsureDir(writePath, 0700); err != nil {
return "", err
}
filename := filepath.Join(writePath, fmt.Sprintf("gentx-%v.json", nodeID))
return os.Create(filename)
return filepath.Join(writePath, fmt.Sprintf("gentx-%v.json", nodeID)), nil
}
24 changes: 20 additions & 4 deletions x/auth/client/cli/sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ package cli
import (
"fmt"
"io/ioutil"

"github.com/pkg/errors"
"github.com/spf13/viper"
"os"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/utils"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tendermint/go-amino"
)

Expand All @@ -22,6 +22,7 @@ const (
flagValidateSigs = "validate-signatures"
flagOffline = "offline"
flagSigOnly = "signature-only"
flagOutfile = "output-document"
)

// GetSignCommand returns the sign command
Expand Down Expand Up @@ -52,6 +53,8 @@ recommended to set such parameters manually.`,
cmd.Flags().Bool(flagValidateSigs, false, "Print the addresses that must sign the transaction, "+
"those who have already signed it, and make sure that signatures are in the correct order.")
cmd.Flags().Bool(flagOffline, false, "Offline mode. Do not query local cache.")
cmd.Flags().String(flagOutfile, "",
"The document will be written to the given file instead of STDOUT")

// Add the flags here and return the command
return client.PostCommands(cmd)[0]
Expand Down Expand Up @@ -107,7 +110,20 @@ func makeSignCmd(cdc *amino.Codec) func(cmd *cobra.Command, args []string) error
if err != nil {
return err
}
fmt.Printf("%s\n", json)

if viper.GetString(flagOutfile) == "" {
fmt.Printf("%s\n", json)
return
}

fp, err := os.OpenFile(
viper.GetString(flagOutfile), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644,
)
if err != nil {
return err
}
defer fp.Close()
fmt.Fprintf(fp, "%s\n", json)
return
}
}
Expand Down
3 changes: 2 additions & 1 deletion x/bank/client/cli/sendtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder"
bankClient "github.com/cosmos/cosmos-sdk/x/bank/client"
"os"

"github.com/pkg/errors"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -66,7 +67,7 @@ func SendTxCmd(cdc *codec.Codec) *cobra.Command {
// build and sign the transaction, then broadcast to Tendermint
msg := bankClient.CreateMsg(from, to, coins)
if cliCtx.GenerateOnly {
return utils.PrintUnsignedStdTx(txBldr, cliCtx, []sdk.Msg{msg}, false)
return utils.PrintUnsignedStdTx(os.Stdout, txBldr, cliCtx, []sdk.Msg{msg}, false)
}

return utils.CompleteAndBroadcastTxCli(txBldr, cliCtx, []sdk.Msg{msg})
Expand Down
7 changes: 4 additions & 3 deletions x/gov/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"fmt"
"os"

"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/utils"
Expand Down Expand Up @@ -103,7 +104,7 @@ $ gaiacli gov submit-proposal --title="Test Proposal" --description="My awesome
}

if cliCtx.GenerateOnly {
return utils.PrintUnsignedStdTx(txBldr, cliCtx, []sdk.Msg{msg}, false)
return utils.PrintUnsignedStdTx(os.Stdout, txBldr, cliCtx, []sdk.Msg{msg}, false)
}

// Build and sign the transaction, then broadcast to Tendermint
Expand Down Expand Up @@ -183,7 +184,7 @@ func GetCmdDeposit(cdc *codec.Codec) *cobra.Command {
}

if cliCtx.GenerateOnly {
return utils.PrintUnsignedStdTx(txBldr, cliCtx, []sdk.Msg{msg}, false)
return utils.PrintUnsignedStdTx(os.Stdout, txBldr, cliCtx, []sdk.Msg{msg}, false)
}

// Build and sign the transaction, then broadcast to a Tendermint
Expand Down Expand Up @@ -229,7 +230,7 @@ func GetCmdVote(cdc *codec.Codec) *cobra.Command {
}

if cliCtx.GenerateOnly {
return utils.PrintUnsignedStdTx(txBldr, cliCtx, []sdk.Msg{msg}, false)
return utils.PrintUnsignedStdTx(os.Stdout, txBldr, cliCtx, []sdk.Msg{msg}, false)
}

fmt.Printf("Vote[Voter:%s,ProposalID:%d,Option:%s]",
Expand Down
3 changes: 2 additions & 1 deletion x/ibc/client/cli/ibctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"encoding/hex"
"os"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
Expand Down Expand Up @@ -41,7 +42,7 @@ func IBCTransferCmd(cdc *codec.Codec) *cobra.Command {
return err
}
if cliCtx.GenerateOnly {
return utils.PrintUnsignedStdTx(txBldr, cliCtx, []sdk.Msg{msg}, false)
return utils.PrintUnsignedStdTx(os.Stdout, txBldr, cliCtx, []sdk.Msg{msg}, false)
}

return utils.CompleteAndBroadcastTxCli(txBldr, cliCtx, []sdk.Msg{msg})
Expand Down
3 changes: 2 additions & 1 deletion x/slashing/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder"
"github.com/cosmos/cosmos-sdk/x/slashing"
"os"

"github.com/spf13/cobra"
)
Expand All @@ -30,7 +31,7 @@ func GetCmdUnjail(cdc *codec.Codec) *cobra.Command {

msg := slashing.NewMsgUnjail(sdk.ValAddress(valAddr))
if cliCtx.GenerateOnly {
return utils.PrintUnsignedStdTx(txBldr, cliCtx, []sdk.Msg{msg}, false)
return utils.PrintUnsignedStdTx(os.Stdout, txBldr, cliCtx, []sdk.Msg{msg}, false)
}
return utils.CompleteAndBroadcastTxCli(txBldr, cliCtx, []sdk.Msg{msg})
},
Expand Down
Loading

0 comments on commit 3e68e44

Please sign in to comment.