Merge PR #2881: Don't call gaiacli tx sign. Use utils.SignStdTx() instead.
This is to avoid command redirection and reduce the use of viper's global variables. Closes: #2875
This commit is contained in:
parent
c21961b21d
commit
4c36b0fe05
|
@ -11,6 +11,7 @@ BREAKING CHANGES
|
||||||
* [cli] [\#2829](https://github.com/cosmos/cosmos-sdk/pull/2829) add-genesis-account command now validates state when adding accounts
|
* [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] [\#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.
|
* [cli] [\#2874](https://github.com/cosmos/cosmos-sdk/pull/2874) `gaiacli tx sign` takes an optional `--output-document` flag to support output redirection.
|
||||||
|
* [cli] [\#2875](https://github.com/cosmos/cosmos-sdk/pull/2875) Refactor `gaiad gentx` and avoid redirection to `gaiacli tx sign` for tx signing.
|
||||||
|
|
||||||
* Gaia
|
* Gaia
|
||||||
|
|
||||||
|
|
|
@ -606,10 +606,11 @@ func getTestingHomeDirs() (string, string) {
|
||||||
|
|
||||||
func initializeFixtures(t *testing.T) (chainID, servAddr, port string) {
|
func initializeFixtures(t *testing.T) (chainID, servAddr, port string) {
|
||||||
tests.ExecuteT(t, fmt.Sprintf("gaiad --home=%s unsafe-reset-all", gaiadHome), "")
|
tests.ExecuteT(t, fmt.Sprintf("gaiad --home=%s unsafe-reset-all", gaiadHome), "")
|
||||||
|
os.RemoveAll(filepath.Join(gaiadHome, "config", "gentx"))
|
||||||
executeWrite(t, fmt.Sprintf("gaiacli keys delete --home=%s foo", gaiacliHome), app.DefaultKeyPass)
|
executeWrite(t, fmt.Sprintf("gaiacli keys delete --home=%s foo", gaiacliHome), app.DefaultKeyPass)
|
||||||
executeWrite(t, fmt.Sprintf("gaiacli keys delete --home=%s bar", gaiacliHome), app.DefaultKeyPass)
|
executeWrite(t, fmt.Sprintf("gaiacli keys delete --home=%s bar", gaiacliHome), app.DefaultKeyPass)
|
||||||
executeWrite(t, fmt.Sprintf("gaiacli keys add --home=%s foo", gaiacliHome), app.DefaultKeyPass)
|
executeWriteCheckErr(t, fmt.Sprintf("gaiacli keys add --home=%s foo", gaiacliHome), app.DefaultKeyPass)
|
||||||
executeWrite(t, fmt.Sprintf("gaiacli keys add --home=%s bar", gaiacliHome), app.DefaultKeyPass)
|
executeWriteCheckErr(t, fmt.Sprintf("gaiacli keys add --home=%s bar", gaiacliHome), app.DefaultKeyPass)
|
||||||
fooAddr, _ := executeGetAddrPK(t, fmt.Sprintf(
|
fooAddr, _ := executeGetAddrPK(t, fmt.Sprintf(
|
||||||
"gaiacli keys show foo --output=json --home=%s", gaiacliHome))
|
"gaiacli keys show foo --output=json --home=%s", gaiacliHome))
|
||||||
chainID = executeInit(t, fmt.Sprintf("gaiad init -o --moniker=foo --home=%s", gaiadHome))
|
chainID = executeInit(t, fmt.Sprintf("gaiad init -o --moniker=foo --home=%s", gaiadHome))
|
||||||
|
@ -623,10 +624,10 @@ func initializeFixtures(t *testing.T) (chainID, servAddr, port string) {
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
genDoc.AppState = appStateJSON
|
genDoc.AppState = appStateJSON
|
||||||
genDoc.SaveAs(genFile)
|
genDoc.SaveAs(genFile)
|
||||||
executeWrite(t, fmt.Sprintf(
|
executeWriteCheckErr(t, fmt.Sprintf(
|
||||||
"gaiad gentx --name=foo --home=%s --home-client=%s", gaiadHome, gaiacliHome),
|
"gaiad gentx --name=foo --home=%s --home-client=%s", gaiadHome, gaiacliHome),
|
||||||
app.DefaultKeyPass)
|
app.DefaultKeyPass)
|
||||||
executeWrite(t, fmt.Sprintf("gaiad collect-gentxs --home=%s", gaiadHome), app.DefaultKeyPass)
|
executeWriteCheckErr(t, fmt.Sprintf("gaiad collect-gentxs --home=%s", gaiadHome), app.DefaultKeyPass)
|
||||||
// get a free port, also setup some common flags
|
// get a free port, also setup some common flags
|
||||||
servAddr, port, err = server.FreeTCPAddr()
|
servAddr, port, err = server.FreeTCPAddr()
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
@ -662,6 +663,10 @@ func readGenesisFile(t *testing.T, genFile string) types.GenesisDoc {
|
||||||
//___________________________________________________________________________________
|
//___________________________________________________________________________________
|
||||||
// executors
|
// executors
|
||||||
|
|
||||||
|
func executeWriteCheckErr(t *testing.T, cmdStr string, writes ...string) {
|
||||||
|
require.True(t, executeWrite(t, cmdStr, writes...))
|
||||||
|
}
|
||||||
|
|
||||||
func executeWrite(t *testing.T, cmdStr string, writes ...string) (exitSuccess bool) {
|
func executeWrite(t *testing.T, cmdStr string, writes ...string) (exitSuccess bool) {
|
||||||
exitSuccess, _, _ = executeWriteRetStdStreams(t, cmdStr, writes...)
|
exitSuccess, _, _ = executeWriteRetStdStreams(t, cmdStr, writes...)
|
||||||
return
|
return
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
package init
|
package init
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -17,7 +19,7 @@ import (
|
||||||
"github.com/cosmos/cosmos-sdk/codec"
|
"github.com/cosmos/cosmos-sdk/codec"
|
||||||
"github.com/cosmos/cosmos-sdk/server"
|
"github.com/cosmos/cosmos-sdk/server"
|
||||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||||
authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli"
|
"github.com/cosmos/cosmos-sdk/x/auth"
|
||||||
authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder"
|
authtxb "github.com/cosmos/cosmos-sdk/x/auth/client/txbuilder"
|
||||||
"github.com/cosmos/cosmos-sdk/x/stake/client/cli"
|
"github.com/cosmos/cosmos-sdk/x/stake/client/cli"
|
||||||
stakeTypes "github.com/cosmos/cosmos-sdk/x/stake/types"
|
stakeTypes "github.com/cosmos/cosmos-sdk/x/stake/types"
|
||||||
|
@ -71,7 +73,9 @@ following delegation and commission default parameters:
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if _, err = kb.Get(viper.GetString(client.FlagName)); err != nil {
|
|
||||||
|
name := viper.GetString(client.FlagName)
|
||||||
|
if _, err := kb.Get(name); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,34 +88,40 @@ following delegation and commission default parameters:
|
||||||
}
|
}
|
||||||
// Run gaiad tx create-validator
|
// Run gaiad tx create-validator
|
||||||
prepareFlagsForTxCreateValidator(config, nodeID, ip, genDoc.ChainID, valPubKey)
|
prepareFlagsForTxCreateValidator(config, nodeID, ip, genDoc.ChainID, valPubKey)
|
||||||
cliCtx, txBldr, msg, err := cli.BuildCreateValidatorMsg(
|
txBldr := authtxb.NewTxBuilderFromCLI().WithCodec(cdc)
|
||||||
context.NewCLIContext().WithCodec(cdc),
|
cliCtx := context.NewCLIContext().WithCodec(cdc)
|
||||||
authtxb.NewTxBuilderFromCLI().WithCodec(cdc),
|
cliCtx, txBldr, msg, err := cli.BuildCreateValidatorMsg(cliCtx, txBldr)
|
||||||
)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
w, err := ioutil.TempFile("", "gentx")
|
// write the unsigned transaction to the buffer
|
||||||
if err != nil {
|
w := bytes.NewBuffer([]byte{})
|
||||||
return err
|
|
||||||
}
|
|
||||||
unsignedGenTxFilename := w.Name()
|
|
||||||
defer os.Remove(unsignedGenTxFilename)
|
|
||||||
|
|
||||||
if err := utils.PrintUnsignedStdTx(w, txBldr, cliCtx, []sdk.Msg{msg}, true); err != nil {
|
if err := utils.PrintUnsignedStdTx(w, txBldr, cliCtx, []sdk.Msg{msg}, true); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
prepareFlagsForTxSign()
|
// read the transaction
|
||||||
signCmd := authcmd.GetSignCommand(cdc)
|
stdTx, err := readUnsignedGenTxFile(cdc, w)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// sign the transaction and write it to the output file
|
||||||
|
signedTx, err := utils.SignStdTx(txBldr, cliCtx, name, stdTx, false, true)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
outputDocument, err := makeOutputFilepath(config.RootDir, nodeID)
|
outputDocument, err := makeOutputFilepath(config.RootDir, nodeID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
viper.Set("output-document", outputDocument)
|
if err := writeSignedGenTx(cdc, outputDocument, signedTx); err != nil {
|
||||||
return signCmd.RunE(nil, []string{unsignedGenTxFilename})
|
return err
|
||||||
|
}
|
||||||
|
fmt.Fprintf(os.Stderr, "Genesis transaction written to %q\n", outputDocument)
|
||||||
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -152,10 +162,6 @@ func prepareFlagsForTxCreateValidator(config *cfg.Config, nodeID, ip, chainID st
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func prepareFlagsForTxSign() {
|
|
||||||
viper.Set("offline", true)
|
|
||||||
}
|
|
||||||
|
|
||||||
func makeOutputFilepath(rootDir, nodeID string) (string, error) {
|
func makeOutputFilepath(rootDir, nodeID string) (string, error) {
|
||||||
writePath := filepath.Join(rootDir, "config", "gentx")
|
writePath := filepath.Join(rootDir, "config", "gentx")
|
||||||
if err := common.EnsureDir(writePath, 0700); err != nil {
|
if err := common.EnsureDir(writePath, 0700); err != nil {
|
||||||
|
@ -163,3 +169,28 @@ func makeOutputFilepath(rootDir, nodeID string) (string, error) {
|
||||||
}
|
}
|
||||||
return filepath.Join(writePath, fmt.Sprintf("gentx-%v.json", nodeID)), nil
|
return filepath.Join(writePath, fmt.Sprintf("gentx-%v.json", nodeID)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func readUnsignedGenTxFile(cdc *codec.Codec, r io.Reader) (auth.StdTx, error) {
|
||||||
|
var stdTx auth.StdTx
|
||||||
|
bytes, err := ioutil.ReadAll(r)
|
||||||
|
if err != nil {
|
||||||
|
return stdTx, err
|
||||||
|
}
|
||||||
|
err = cdc.UnmarshalJSON(bytes, &stdTx)
|
||||||
|
return stdTx, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// nolint: errcheck
|
||||||
|
func writeSignedGenTx(cdc *codec.Codec, outputDocument string, tx auth.StdTx) error {
|
||||||
|
outputFile, err := os.OpenFile(outputDocument, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0644)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer outputFile.Close()
|
||||||
|
json, err := cdc.MarshalJSON(tx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = fmt.Fprintf(outputFile, "%s\n", json)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue