2019-05-16 08:25:32 -07:00
|
|
|
package cli
|
2019-02-08 13:45:41 -08:00
|
|
|
|
2018-10-19 11:00:27 -07:00
|
|
|
import (
|
2019-11-14 06:17:21 -08:00
|
|
|
"bufio"
|
2018-11-26 03:50:33 -08:00
|
|
|
"bytes"
|
2019-05-16 08:25:32 -07:00
|
|
|
"encoding/json"
|
2018-10-19 11:00:27 -07:00
|
|
|
"fmt"
|
2018-11-26 03:50:33 -08:00
|
|
|
"io"
|
2018-11-19 09:02:34 -08:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2019-08-30 06:59:41 -07:00
|
|
|
"github.com/pkg/errors"
|
2018-11-21 15:44:13 -08:00
|
|
|
"github.com/spf13/cobra"
|
2020-01-16 13:46:51 -08:00
|
|
|
tmos "github.com/tendermint/tendermint/libs/os"
|
2019-04-08 10:54:45 -07:00
|
|
|
tmtypes "github.com/tendermint/tendermint/types"
|
2018-12-10 06:27:25 -08:00
|
|
|
|
2020-06-01 05:46:03 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
2019-12-11 01:45:26 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
2020-07-29 15:33:42 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/client/tx"
|
2020-03-25 08:20:36 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
2018-10-19 11:00:27 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/server"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-06-05 16:26:17 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/module"
|
2020-07-08 10:57:45 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/version"
|
2020-01-24 08:40:56 -08:00
|
|
|
authclient "github.com/cosmos/cosmos-sdk/x/auth/client"
|
2019-05-16 08:25:32 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/genutil"
|
2019-07-24 14:37:03 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/genutil/types"
|
2020-07-06 14:48:54 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking/client/cli"
|
2018-10-19 11:00:27 -07:00
|
|
|
)
|
|
|
|
|
2019-05-18 01:42:24 -07:00
|
|
|
// GenTxCmd builds the application's gentx command.
|
2020-07-25 01:10:04 -07:00
|
|
|
func GenTxCmd(mbm module.BasicManager, txEncCfg client.TxEncodingConfig, genBalIterator types.GenesisBalancesIterator, defaultNodeHome string) *cobra.Command {
|
2019-05-28 01:44:04 -07:00
|
|
|
ipDefault, _ := server.ExternalIP()
|
2020-07-06 14:48:54 -07:00
|
|
|
fsCreateValidator, defaultsDesc := cli.CreateValidatorMsgFlagSet(ipDefault)
|
2019-05-28 01:44:04 -07:00
|
|
|
|
2018-10-19 11:00:27 -07:00
|
|
|
cmd := &cobra.Command{
|
2020-12-17 08:27:30 -08:00
|
|
|
Use: "gentx [key_name] [amount]",
|
2018-10-19 11:00:27 -07:00
|
|
|
Short: "Generate a genesis tx carrying a self delegation",
|
2020-12-17 08:27:30 -08:00
|
|
|
Args: cobra.ExactArgs(2),
|
2020-07-08 10:57:45 -07:00
|
|
|
Long: fmt.Sprintf(`Generate a genesis transaction that creates a validator with a self-delegation,
|
|
|
|
that is signed by the key in the Keyring referenced by a given name. A node ID and Bech32 consensus
|
|
|
|
pubkey may optionally be provided. If they are omitted, they will be retrieved from the priv_validator.json
|
2020-12-14 13:44:15 -08:00
|
|
|
file. The following default parameters are included:
|
2020-07-08 10:57:45 -07:00
|
|
|
%s
|
2020-12-14 13:44:15 -08:00
|
|
|
|
2020-07-08 10:57:45 -07:00
|
|
|
Example:
|
2020-12-17 08:27:30 -08:00
|
|
|
$ %s gentx my-key-name 1000000stake --home=/path/to/home/dir --keyring-backend=os --chain-id=test-chain-1 \
|
2020-07-08 10:57:45 -07:00
|
|
|
--moniker="myvalidator" \
|
|
|
|
--commission-max-change-rate=0.01 \
|
|
|
|
--commission-max-rate=1.0 \
|
|
|
|
--commission-rate=0.07 \
|
|
|
|
--details="..." \
|
|
|
|
--security-contact="..." \
|
|
|
|
--website="..."
|
|
|
|
`, defaultsDesc, version.AppName,
|
|
|
|
),
|
2018-10-19 11:00:27 -07:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2020-07-07 08:40:46 -07:00
|
|
|
serverCtx := server.GetServerContextFromCmd(cmd)
|
2021-07-01 12:06:11 -07:00
|
|
|
clientCtx, err := client.GetClientTxContext(cmd)
|
2020-12-14 14:09:51 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-11 04:49:39 -07:00
|
|
|
cdc := clientCtx.Codec
|
2020-07-07 08:40:46 -07:00
|
|
|
|
|
|
|
config := serverCtx.Config
|
2020-07-08 10:57:45 -07:00
|
|
|
config.SetRoot(clientCtx.HomeDir)
|
2020-07-07 08:40:46 -07:00
|
|
|
|
|
|
|
nodeID, valPubKey, err := genutil.InitializeNodeValidatorFiles(serverCtx.Config)
|
2018-10-19 11:00:27 -07:00
|
|
|
if err != nil {
|
2019-08-30 06:59:41 -07:00
|
|
|
return errors.Wrap(err, "failed to initialize node validator files")
|
2018-10-19 11:00:27 -07:00
|
|
|
}
|
2019-02-01 17:03:57 -08:00
|
|
|
|
2020-07-08 10:57:45 -07:00
|
|
|
// read --nodeID, if empty take it from priv_validator.json
|
|
|
|
if nodeIDString, _ := cmd.Flags().GetString(cli.FlagNodeID); nodeIDString != "" {
|
2019-02-05 20:57:36 -08:00
|
|
|
nodeID = nodeIDString
|
|
|
|
}
|
2020-07-06 14:48:54 -07:00
|
|
|
|
2020-07-08 10:57:45 -07:00
|
|
|
// read --pubkey, if empty take it from priv_validator.json
|
2021-08-02 08:06:24 -07:00
|
|
|
if pkStr, _ := cmd.Flags().GetString(cli.FlagPubKey); pkStr != "" {
|
|
|
|
if err := clientCtx.Codec.UnmarshalInterfaceJSON([]byte(pkStr), &valPubKey); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to unmarshal validator public key")
|
2019-05-28 01:44:04 -07:00
|
|
|
}
|
2018-10-19 11:00:27 -07:00
|
|
|
}
|
2018-12-04 01:57:44 -08:00
|
|
|
|
2019-04-08 10:54:45 -07:00
|
|
|
genDoc, err := tmtypes.GenesisDocFromFile(config.GenesisFile())
|
2018-11-04 20:26:46 -08:00
|
|
|
if err != nil {
|
2019-08-30 06:59:41 -07:00
|
|
|
return errors.Wrapf(err, "failed to read genesis doc file %s", config.GenesisFile())
|
2018-11-04 20:26:46 -08:00
|
|
|
}
|
2018-10-19 11:00:27 -07:00
|
|
|
|
2019-05-16 08:25:32 -07:00
|
|
|
var genesisState map[string]json.RawMessage
|
2020-08-11 07:22:30 -07:00
|
|
|
if err = json.Unmarshal(genDoc.AppState, &genesisState); err != nil {
|
2019-08-30 06:59:41 -07:00
|
|
|
return errors.Wrap(err, "failed to unmarshal genesis state")
|
2018-12-04 01:57:44 -08:00
|
|
|
}
|
|
|
|
|
2020-07-25 01:10:04 -07:00
|
|
|
if err = mbm.ValidateGenesis(cdc, txEncCfg, genesisState); err != nil {
|
2019-08-30 06:59:41 -07:00
|
|
|
return errors.Wrap(err, "failed to validate genesis state")
|
2019-03-20 11:02:11 -07:00
|
|
|
}
|
|
|
|
|
2019-11-14 06:17:21 -08:00
|
|
|
inBuf := bufio.NewReader(cmd.InOrStdin())
|
2020-07-08 10:57:45 -07:00
|
|
|
|
|
|
|
name := args[0]
|
2020-08-03 23:41:58 -07:00
|
|
|
key, err := clientCtx.Keyring.Key(name)
|
2018-12-04 01:57:44 -08:00
|
|
|
if err != nil {
|
2020-07-08 10:57:45 -07:00
|
|
|
return errors.Wrapf(err, "failed to fetch '%s' from the keyring", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
moniker := config.Moniker
|
|
|
|
if m, _ := cmd.Flags().GetString(cli.FlagMoniker); m != "" {
|
|
|
|
moniker = m
|
2018-11-15 21:01:34 -08:00
|
|
|
}
|
|
|
|
|
2020-07-08 10:57:45 -07:00
|
|
|
// set flags for creating a gentx
|
|
|
|
createValCfg, err := cli.PrepareConfigForTxCreateValidator(cmd.Flags(), moniker, nodeID, genDoc.ChainID, valPubKey)
|
2020-07-06 14:48:54 -07:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error creating configuration to create validator msg")
|
|
|
|
}
|
2018-12-04 01:57:44 -08:00
|
|
|
|
2020-12-17 08:27:30 -08:00
|
|
|
amount := args[1]
|
2020-11-16 03:34:54 -08:00
|
|
|
coins, err := sdk.ParseCoinsNormalized(amount)
|
2018-12-04 01:57:44 -08:00
|
|
|
if err != nil {
|
2019-08-30 06:59:41 -07:00
|
|
|
return errors.Wrap(err, "failed to parse coins")
|
2018-12-04 01:57:44 -08:00
|
|
|
}
|
2021-09-20 05:02:15 -07:00
|
|
|
addr, err := key.GetAddress()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = genutil.ValidateAccountInGenesis(genesisState, genBalIterator, addr, coins, cdc)
|
2018-12-04 01:57:44 -08:00
|
|
|
if err != nil {
|
2019-08-30 06:59:41 -07:00
|
|
|
return errors.Wrap(err, "failed to validate account in genesis")
|
2018-12-04 01:57:44 -08:00
|
|
|
}
|
|
|
|
|
2020-07-29 15:33:42 -07:00
|
|
|
txFactory := tx.NewFactoryCLI(clientCtx, cmd.Flags())
|
2020-07-06 14:48:54 -07:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "error creating tx builder")
|
|
|
|
}
|
2021-09-20 05:02:15 -07:00
|
|
|
pub, err := key.GetAddress()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
clientCtx = clientCtx.WithInput(inBuf).WithFromAddress(pub)
|
2019-03-25 08:27:24 -07:00
|
|
|
|
2021-01-05 07:08:56 -08:00
|
|
|
// The following line comes from a discrepancy between the `gentx`
|
|
|
|
// and `create-validator` commands:
|
|
|
|
// - `gentx` expects amount as an arg,
|
|
|
|
// - `create-validator` expects amount as a required flag.
|
|
|
|
// ref: https://github.com/cosmos/cosmos-sdk/issues/8251
|
|
|
|
// Since gentx doesn't set the amount flag (which `create-validator`
|
|
|
|
// reads from), we copy the amount arg into the valCfg directly.
|
|
|
|
//
|
|
|
|
// Ideally, the `create-validator` command should take a validator
|
|
|
|
// config file instead of so many flags.
|
|
|
|
// ref: https://github.com/cosmos/cosmos-sdk/issues/8177
|
|
|
|
createValCfg.Amount = amount
|
|
|
|
|
2019-03-25 08:27:24 -07:00
|
|
|
// create a 'create-validator' message
|
2020-07-29 15:33:42 -07:00
|
|
|
txBldr, msg, err := cli.BuildCreateValidatorMsg(clientCtx, createValCfg, txFactory, true)
|
2018-11-21 15:44:13 -08:00
|
|
|
if err != nil {
|
2019-08-30 06:59:41 -07:00
|
|
|
return errors.Wrap(err, "failed to build create-validator message")
|
2018-11-21 15:44:13 -08:00
|
|
|
}
|
2018-10-19 11:00:27 -07:00
|
|
|
|
2020-03-25 08:20:36 -07:00
|
|
|
if key.GetType() == keyring.TypeOffline || key.GetType() == keyring.TypeMulti {
|
2020-04-22 13:20:16 -07:00
|
|
|
cmd.PrintErrln("Offline key passed in. Use `tx sign` command to sign.")
|
2021-06-28 04:42:08 -07:00
|
|
|
return txBldr.PrintUnsignedTx(clientCtx, msg)
|
2019-02-27 10:31:11 -08:00
|
|
|
}
|
|
|
|
|
2018-11-26 03:50:33 -08:00
|
|
|
// write the unsigned transaction to the buffer
|
|
|
|
w := bytes.NewBuffer([]byte{})
|
2020-06-01 05:46:03 -07:00
|
|
|
clientCtx = clientCtx.WithOutput(w)
|
2019-02-27 10:31:11 -08:00
|
|
|
|
2022-04-20 07:54:29 -07:00
|
|
|
if err = msg.ValidateBasic(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-28 04:42:08 -07:00
|
|
|
if err = txBldr.PrintUnsignedTx(clientCtx, msg); err != nil {
|
2019-08-30 06:59:41 -07:00
|
|
|
return errors.Wrap(err, "failed to print unsigned std tx")
|
2018-10-19 11:00:27 -07:00
|
|
|
}
|
2018-11-21 15:44:13 -08:00
|
|
|
|
2018-11-26 03:50:33 -08:00
|
|
|
// read the transaction
|
2020-08-03 23:41:58 -07:00
|
|
|
stdTx, err := readUnsignedGenTxFile(clientCtx, w)
|
2018-11-26 03:50:33 -08:00
|
|
|
if err != nil {
|
2019-08-30 06:59:41 -07:00
|
|
|
return errors.Wrap(err, "failed to read unsigned gen tx file")
|
2018-10-19 11:00:27 -07:00
|
|
|
}
|
|
|
|
|
2018-11-26 03:50:33 -08:00
|
|
|
// sign the transaction and write it to the output file
|
2020-08-03 23:41:58 -07:00
|
|
|
txBuilder, err := clientCtx.TxConfig.WrapTxBuilder(stdTx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating tx builder: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-12-14 13:44:15 -08:00
|
|
|
err = authclient.SignTx(txFactory, clientCtx, name, txBuilder, true, true)
|
2018-11-26 03:50:33 -08:00
|
|
|
if err != nil {
|
2019-08-30 06:59:41 -07:00
|
|
|
return errors.Wrap(err, "failed to sign std tx")
|
2018-11-26 03:50:33 -08:00
|
|
|
}
|
2018-11-21 15:44:13 -08:00
|
|
|
|
2020-07-06 14:48:54 -07:00
|
|
|
outputDocument, _ := cmd.Flags().GetString(flags.FlagOutputDocument)
|
2018-12-06 03:03:58 -08:00
|
|
|
if outputDocument == "" {
|
|
|
|
outputDocument, err = makeOutputFilepath(config.RootDir, nodeID)
|
|
|
|
if err != nil {
|
2019-08-30 06:59:41 -07:00
|
|
|
return errors.Wrap(err, "failed to create output file path")
|
2018-12-06 03:03:58 -08:00
|
|
|
}
|
2018-10-19 11:00:27 -07:00
|
|
|
}
|
2018-12-04 01:57:44 -08:00
|
|
|
|
2020-08-03 23:41:58 -07:00
|
|
|
if err := writeSignedGenTx(clientCtx, outputDocument, stdTx); err != nil {
|
2019-08-30 06:59:41 -07:00
|
|
|
return errors.Wrap(err, "failed to write signed gen tx")
|
2018-11-26 03:50:33 -08:00
|
|
|
}
|
2018-12-04 01:57:44 -08:00
|
|
|
|
2020-04-22 13:20:16 -07:00
|
|
|
cmd.PrintErrf("Genesis transaction written to %q\n", outputDocument)
|
2018-11-26 03:50:33 -08:00
|
|
|
return nil
|
2018-10-19 11:00:27 -07:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-07-17 07:20:31 -07:00
|
|
|
cmd.Flags().String(flags.FlagHome, defaultNodeHome, "The application home directory")
|
2020-07-08 10:57:45 -07:00
|
|
|
cmd.Flags().String(flags.FlagOutputDocument, "", "Write the genesis transaction JSON document to the given file instead of the default location")
|
|
|
|
cmd.Flags().String(flags.FlagChainID, "", "The network chain ID")
|
2019-05-28 01:44:04 -07:00
|
|
|
cmd.Flags().AddFlagSet(fsCreateValidator)
|
2020-07-11 01:13:46 -07:00
|
|
|
flags.AddTxFlagsToCmd(cmd)
|
2019-05-28 01:44:04 -07:00
|
|
|
|
2018-10-19 11:00:27 -07:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2019-05-16 08:25:32 -07:00
|
|
|
func makeOutputFilepath(rootDir, nodeID string) (string, error) {
|
|
|
|
writePath := filepath.Join(rootDir, "config", "gentx")
|
2020-01-16 13:46:51 -08:00
|
|
|
if err := tmos.EnsureDir(writePath, 0700); err != nil {
|
2019-05-16 08:25:32 -07:00
|
|
|
return "", err
|
2018-12-04 01:57:44 -08:00
|
|
|
}
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2019-05-16 08:25:32 -07:00
|
|
|
return filepath.Join(writePath, fmt.Sprintf("gentx-%v.json", nodeID)), nil
|
|
|
|
}
|
2018-12-04 01:57:44 -08:00
|
|
|
|
2020-08-03 23:41:58 -07:00
|
|
|
func readUnsignedGenTxFile(clientCtx client.Context, r io.Reader) (sdk.Tx, error) {
|
2021-10-13 00:38:22 -07:00
|
|
|
bz, err := io.ReadAll(r)
|
2020-08-03 23:41:58 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2020-08-03 23:41:58 -07:00
|
|
|
aTx, err := clientCtx.TxConfig.TxJSONDecoder()(bz)
|
2020-05-08 01:46:12 -07:00
|
|
|
if err != nil {
|
2020-08-03 23:41:58 -07:00
|
|
|
return nil, err
|
2018-12-04 01:57:44 -08:00
|
|
|
}
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2020-08-03 23:41:58 -07:00
|
|
|
return aTx, err
|
2019-05-16 08:25:32 -07:00
|
|
|
}
|
2018-12-04 01:57:44 -08:00
|
|
|
|
2020-08-03 23:41:58 -07:00
|
|
|
func writeSignedGenTx(clientCtx client.Context, outputDocument string, tx sdk.Tx) error {
|
2019-05-16 08:25:32 -07:00
|
|
|
outputFile, err := os.OpenFile(outputDocument, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-26 13:30:36 -07:00
|
|
|
defer outputFile.Close()
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2020-08-03 23:41:58 -07:00
|
|
|
json, err := clientCtx.TxConfig.TxJSONEncoder()(tx)
|
2020-05-08 01:46:12 -07:00
|
|
|
if err != nil {
|
2019-05-16 08:25:32 -07:00
|
|
|
return err
|
|
|
|
}
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2019-05-16 08:25:32 -07:00
|
|
|
_, err = fmt.Fprintf(outputFile, "%s\n", json)
|
2020-05-02 12:26:59 -07:00
|
|
|
|
2019-05-16 08:25:32 -07:00
|
|
|
return err
|
2018-12-04 01:57:44 -08:00
|
|
|
}
|