From 8fb0b593b3f71e6810304b4e639d3d59d97901e4 Mon Sep 17 00:00:00 2001 From: Ruslan Akhtariev <46343690+RusAkh@users.noreply.github.com> Date: Wed, 7 Dec 2022 19:17:19 +0700 Subject: [PATCH 1/2] feat(simapp): move genesis related commands under one `genesis` command (#14149) (cherry picked from commit ecb4ca2a82c45296ae9409ec7c7294d902067b61) --- CHANGELOG.md | 1 + contrib/rosetta/configuration/data.sh | 8 ++--- .../adr-041-in-place-store-migrations.md | 4 +-- docs/docs/run-node/01-run-node.md | 8 ++--- simapp/README.md | 6 ++-- simapp/simd/cmd/root.go | 23 ++++++------ tools/cosmovisor/README.md | 6 ++-- x/genutil/client/cli/core_genesis_cmd.go | 36 +++++++++++++++++++ .../genutil/client/cli/genaccount.go | 7 ++-- .../genutil/client/cli/genaccount_test.go | 11 +++--- 10 files changed, 73 insertions(+), 37 deletions(-) create mode 100644 x/genutil/client/cli/core_genesis_cmd.go rename simapp/simd/cmd/genaccounts.go => x/genutil/client/cli/genaccount.go (95%) rename simapp/simd/cmd/genaccounts_test.go => x/genutil/client/cli/genaccount_test.go (91%) diff --git a/CHANGELOG.md b/CHANGELOG.md index e78f0bb668ba..16f7b480744f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -214,6 +214,7 @@ extension interfaces. `module.Manager.Modules` is now of type `map[string]interf * (x/genutil) [#13535](https://github.com/cosmos/cosmos-sdk/pull/13535) Replace in `simd init`, the `--staking-bond-denom` flag with `--default-denom` which is used for all default denomination in the genesis, instead of only staking. * (tx) [#12659](https://github.com/cosmos/cosmos-sdk/pull/12659) Remove broadcast mode `block`. +* (genesis) [#14149](https://github.com/cosmos/cosmos-sdk/pull/14149) Add `simd genesis` command, which contains all genesis-related sub-commands. ### Bug Fixes diff --git a/contrib/rosetta/configuration/data.sh b/contrib/rosetta/configuration/data.sh index 22bdcbacb0b3..017b2e642f9c 100644 --- a/contrib/rosetta/configuration/data.sh +++ b/contrib/rosetta/configuration/data.sh @@ -19,15 +19,15 @@ addr=$(simd keys show fd -a --keyring-backend=test) val_addr=$(simd keys show fd --keyring-backend=test --bech val -a) # give the accounts some money -simd add-genesis-account "$addr" 1000000000000stake --keyring-backend=test +simd genesis add-genesis-account "$addr" 1000000000000stake --keyring-backend=test # save configs for the daemon -simd gentx fd 10000000stake --chain-id testing --keyring-backend=test +simd genesis gentx fd 10000000stake --chain-id testing --keyring-backend=test # input genTx to the genesis file -simd collect-gentxs +simd genesis collect-gentxs # verify genesis file is fine -simd validate-genesis +simd genesis validate-genesis echo changing network settings sed -i 's/127.0.0.1/0.0.0.0/g' /root/.simapp/config/config.toml diff --git a/docs/architecture/adr-041-in-place-store-migrations.md b/docs/architecture/adr-041-in-place-store-migrations.md index 41dfa1520a31..2237b610dfe7 100644 --- a/docs/architecture/adr-041-in-place-store-migrations.md +++ b/docs/architecture/adr-041-in-place-store-migrations.md @@ -14,7 +14,7 @@ This ADR introduces a mechanism to perform in-place state store migrations durin ## Context -When a chain upgrade introduces state-breaking changes inside modules, the current procedure consists of exporting the whole state into a JSON file (via the `simd export` command), running migration scripts on the JSON file (`simd migrate` command), clearing the stores (`simd unsafe-reset-all` command), and starting a new chain with the migrated JSON file as new genesis (optionally with a custom initial block height). An example of such a procedure can be seen [in the Cosmos Hub 3->4 migration guide](https://github.com/cosmos/gaia/blob/v4.0.3/docs/migration/cosmoshub-3.md#upgrade-procedure). +When a chain upgrade introduces state-breaking changes inside modules, the current procedure consists of exporting the whole state into a JSON file (via the `simd export` command), running migration scripts on the JSON file (`simd genesis migrate` command), clearing the stores (`simd unsafe-reset-all` command), and starting a new chain with the migrated JSON file as new genesis (optionally with a custom initial block height). An example of such a procedure can be seen [in the Cosmos Hub 3->4 migration guide](https://github.com/cosmos/gaia/blob/v4.0.3/docs/migration/cosmoshub-3.md#upgrade-procedure). This procedure is cumbersome for multiple reasons: @@ -155,7 +155,7 @@ While modules MUST register their migration functions when bumping ConsensusVers ### Neutral -* The Cosmos SDK will continue to support JSON migrations via the existing `simd export` and `simd migrate` commands. +* The Cosmos SDK will continue to support JSON migrations via the existing `simd export` and `simd genesis migrate` commands. * The current ADR does not allow creating, renaming or deleting stores, only modifying existing store keys and values. The Cosmos SDK already has the `StoreLoader` for those operations. ## Further Discussions diff --git a/docs/docs/run-node/01-run-node.md b/docs/docs/run-node/01-run-node.md index 0661b538dfcc..eacb058f0331 100644 --- a/docs/docs/run-node/01-run-node.md +++ b/docs/docs/run-node/01-run-node.md @@ -79,7 +79,7 @@ Before starting the chain, you need to populate the state with at least one acco Now that you have created a local account, go ahead and grant it some `stake` tokens in your chain's genesis file. Doing so will also make sure your chain is aware of this account's existence: ```bash -simd add-genesis-account $MY_VALIDATOR_ADDRESS 100000000000stake +simd genesis add-genesis-account $MY_VALIDATOR_ADDRESS 100000000000stake ``` Recall that `$MY_VALIDATOR_ADDRESS` is a variable that holds the address of the `my_validator` key in the [keyring](./00-keyring.md#adding-keys-to-the-keyring). Also note that the tokens in the Cosmos SDK have the `{amount}{denom}` format: `amount` is is a 18-digit-precision decimal number, and `denom` is the unique token identifier with its denomination key (e.g. `atom` or `uatom`). Here, we are granting `stake` tokens, as `stake` is the token identifier used for staking in [`simapp`](https://github.com/cosmos/cosmos-sdk/tree/main/simapp). For your own chain with its own staking denom, that token identifier should be used instead. @@ -88,10 +88,10 @@ Now that your account has some tokens, you need to add a validator to your chain ```bash # Create a gentx. -simd gentx my_validator 100000000stake --chain-id my-test-chain --keyring-backend test +simd genesis gentx my_validator 100000000stake --chain-id my-test-chain --keyring-backend test # Add the gentx to the genesis file. -simd collect-gentxs +simd genesis collect-gentxs ``` A `gentx` does three things: @@ -103,7 +103,7 @@ A `gentx` does three things: For more information on `gentx`, use the following command: ```bash -simd gentx --help +simd genesis gentx --help ``` ## Configuring the Node Using `app.toml` and `config.toml` diff --git a/simapp/README.md b/simapp/README.md index 2496b7088a55..a235d3ed33f5 100644 --- a/simapp/README.md +++ b/simapp/README.md @@ -22,14 +22,14 @@ in this testnet. two names can be anything, but you will need to use the same "chain id" in the following steps. 4. `$ ./simd keys add [key_name]`. This will create a new key, with a name of your choosing. Save the output of this command somewhere; you'll need the address generated here later. -5. `$ ./simd add-genesis-account [key_name] [amount]`, where `key_name` is the same key name as +5. `$ ./simd genesis add-genesis-account [key_name] [amount]`, where `key_name` is the same key name as before; and `amount` is something like `10000000000000000000000000stake`. -6. `$ ./simd gentx [key_name] [amount] --chain-id [chain-id]`. This will create the genesis +6. `$ ./simd genesis gentx [key_name] [amount] --chain-id [chain-id]`. This will create the genesis transaction for your new chain. Here `amount` should be at least `1000000000stake`. If you provide too much or too little, you will encounter an error when starting your node. 7. Now, one person needs to create the genesis file `genesis.json` using the genesis transactions from every participant, by gathering all the genesis transactions under `config/gentx` and then - calling `$ ./simd collect-gentxs`. This will create a new `genesis.json` file that includes data + calling `$ ./simd genesis collect-gentxs`. This will create a new `genesis.json` file that includes data from all the validators (we sometimes call it the "super genesis file" to distinguish it from single-validator genesis files). 8. Once you've received the super genesis file, overwrite your original `genesis.json` file with diff --git a/simapp/simd/cmd/root.go b/simapp/simd/cmd/root.go index eafc26cd2ffb..791ce1a312a4 100644 --- a/simapp/simd/cmd/root.go +++ b/simapp/simd/cmd/root.go @@ -32,9 +32,7 @@ import ( "github.com/cosmos/cosmos-sdk/x/auth/types" banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" "github.com/cosmos/cosmos-sdk/x/crisis" - "github.com/cosmos/cosmos-sdk/x/genutil" genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli" - genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types" ) // NewRootCmd creates a new root command for simd. It is called once in the @@ -165,17 +163,9 @@ lru_size = 0` func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) { cfg := sdk.GetConfig() cfg.Seal() - gentxModule := simapp.ModuleBasics[genutiltypes.ModuleName].(genutil.AppModuleBasic) rootCmd.AddCommand( genutilcli.InitCmd(simapp.ModuleBasics, simapp.DefaultNodeHome), - genutilcli.CollectGenTxsCmd(banktypes.GenesisBalancesIterator{}, simapp.DefaultNodeHome, - gentxModule.GenTxValidator), - genutilcli.MigrateGenesisCmd(), - genutilcli.GenTxCmd(simapp.ModuleBasics, encodingConfig.TxConfig, - banktypes.GenesisBalancesIterator{}, simapp.DefaultNodeHome), - genutilcli.ValidateGenesisCmd(simapp.ModuleBasics), - AddGenesisAccountCmd(simapp.DefaultNodeHome), NewTestnetCmd(simapp.ModuleBasics, banktypes.GenesisBalancesIterator{}), debug.Cmd(), config.Cmd(), @@ -184,9 +174,10 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) { server.AddCommands(rootCmd, simapp.DefaultNodeHome, newApp, appExport, addModuleInitFlags) - // add keybase, auxiliary RPC, query, and tx child commands + // add keybase, auxiliary RPC, query, genesis, and tx child commands rootCmd.AddCommand( rpc.StatusCommand(), + genesisCommand(encodingConfig), queryCommand(), txCommand(), keys.Commands(simapp.DefaultNodeHome), @@ -200,6 +191,16 @@ func addModuleInitFlags(startCmd *cobra.Command) { crisis.AddModuleInitFlags(startCmd) } +// genesisCommand builds genesis-related `simd genesis` command. Users may provide application specific commands as a parameter +func genesisCommand(encodingConfig params.EncodingConfig, cmds ...*cobra.Command) *cobra.Command { + cmd := genutilcli.GenesisCoreCommand(encodingConfig.TxConfig, simapp.ModuleBasics, simapp.DefaultNodeHome) + + for _, sub_cmd := range cmds { + cmd.AddCommand(sub_cmd) + } + return cmd +} + func queryCommand() *cobra.Command { cmd := &cobra.Command{ Use: "query", diff --git a/tools/cosmovisor/README.md b/tools/cosmovisor/README.md index cca08691b638..e2bf5f947873 100644 --- a/tools/cosmovisor/README.md +++ b/tools/cosmovisor/README.md @@ -298,9 +298,9 @@ Create a validator, and setup genesis transaction: ```shell ./build/simd keys add validator -./build/simd add-genesis-account validator 1000000000stake --keyring-backend test -./build/simd gentx validator 1000000stake --chain-id test -./build/simd collect-gentxs +./build/simd genesis add-genesis-account validator 1000000000stake --keyring-backend test +./build/simd genesis gentx validator 1000000stake --chain-id test +./build/simd genesis collect-gentxs ``` #### Prepare Cosmovisor and Start the Chain diff --git a/x/genutil/client/cli/core_genesis_cmd.go b/x/genutil/client/cli/core_genesis_cmd.go new file mode 100644 index 000000000000..384ddc9c782c --- /dev/null +++ b/x/genutil/client/cli/core_genesis_cmd.go @@ -0,0 +1,36 @@ +package cli + +import ( + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/types/module" + banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" + "github.com/cosmos/cosmos-sdk/x/genutil" + genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types" + + "github.com/spf13/cobra" +) + +// GenesisCoreCommand adds core sdk's sub-commands into genesis command: +// -> gentx, migrate, collect-gentxs, validate-genesis, add-genesis-account +func GenesisCoreCommand(txConfig client.TxConfig, moduleBasics module.BasicManager, defaultNodeHome string) *cobra.Command { + cmd := &cobra.Command{ + Use: "genesis", + Short: "Application's genesis-related subcommands", + DisableFlagParsing: false, + SuggestionsMinimumDistance: 2, + RunE: client.ValidateCmd, + } + gentxModule := moduleBasics[genutiltypes.ModuleName].(genutil.AppModuleBasic) + + cmd.AddCommand( + GenTxCmd(moduleBasics, txConfig, + banktypes.GenesisBalancesIterator{}, defaultNodeHome), + MigrateGenesisCmd(), + CollectGenTxsCmd(banktypes.GenesisBalancesIterator{}, defaultNodeHome, + gentxModule.GenTxValidator), + ValidateGenesisCmd(moduleBasics), + AddGenesisAccountCmd(defaultNodeHome), + ) + + return cmd +} diff --git a/simapp/simd/cmd/genaccounts.go b/x/genutil/client/cli/genaccount.go similarity index 95% rename from simapp/simd/cmd/genaccounts.go rename to x/genutil/client/cli/genaccount.go index d04f36d62d20..043428daf5ab 100644 --- a/simapp/simd/cmd/genaccounts.go +++ b/x/genutil/client/cli/genaccount.go @@ -1,17 +1,17 @@ -package cmd +package cli import ( "bufio" "fmt" - "github.com/spf13/cobra" - "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/crypto/keyring" "github.com/cosmos/cosmos-sdk/server" sdk "github.com/cosmos/cosmos-sdk/types" auth "github.com/cosmos/cosmos-sdk/x/auth/helpers" + + "github.com/spf13/cobra" ) const ( @@ -22,6 +22,7 @@ const ( ) // AddGenesisAccountCmd returns add-genesis-account cobra Command. +// This command is provided as a default, applications are expected to provide their own command if custom genesis accounts are needed. func AddGenesisAccountCmd(defaultNodeHome string) *cobra.Command { cmd := &cobra.Command{ Use: "add-genesis-account [address_or_key_name] [coin][,[coin]]", diff --git a/simapp/simd/cmd/genaccounts_test.go b/x/genutil/client/cli/genaccount_test.go similarity index 91% rename from simapp/simd/cmd/genaccounts_test.go rename to x/genutil/client/cli/genaccount_test.go index eeea750e5464..31886b074805 100644 --- a/simapp/simd/cmd/genaccounts_test.go +++ b/x/genutil/client/cli/genaccount_test.go @@ -1,4 +1,4 @@ -package cmd_test +package cli_test import ( "context" @@ -13,20 +13,17 @@ import ( "github.com/stretchr/testify/require" "github.com/tendermint/tendermint/libs/log" - simcmd "cosmossdk.io/simapp/simd/cmd" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/server" "github.com/cosmos/cosmos-sdk/testutil/testdata" - "github.com/cosmos/cosmos-sdk/types/module" + moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" "github.com/cosmos/cosmos-sdk/x/auth" - "github.com/cosmos/cosmos-sdk/x/genutil" + genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli" genutiltest "github.com/cosmos/cosmos-sdk/x/genutil/client/testutil" ) -var testMbm = module.NewBasicManager(genutil.AppModuleBasic{}) - func TestAddGenesisAccountCmd(t *testing.T) { _, _, addr1 := testdata.KeyTestPubAddr() tests := []struct { @@ -94,7 +91,7 @@ func TestAddGenesisAccountCmd(t *testing.T) { ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx) ctx = context.WithValue(ctx, server.ServerContextKey, serverCtx) - cmd := simcmd.AddGenesisAccountCmd(home) + cmd := genutilcli.AddGenesisAccountCmd(home) cmd.SetArgs([]string{ tc.addr, tc.denom, From 5742c6a1db8d30edd68ff5119c6acb58e8277c44 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Wed, 7 Dec 2022 13:24:07 +0100 Subject: [PATCH 2/2] fix changelog --- CHANGELOG.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 16f7b480744f..58322e2e51c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,11 @@ Ref: https://keepachangelog.com/en/1.0.0/ ### Features * (client) [#14051](https://github.com/cosmos/cosmos-sdk/pull/14051) Add `--grpc` client option. +* (x/gentutil) [#14149](https://github.com/cosmos/cosmos-sdk/pull/14149) Add `genutilcli.GenesisCoreCommand` command, which contains all genesis-related sub-commands. + +### CLI Breaking Changes + +* (genesis) [#14149](https://github.com/cosmos/cosmos-sdk/pull/14149) Add `simd genesis` command, which contains all genesis-related sub-commands. ## [v0.47.0-alpha2](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.47.0-alpha2) - 2022-12-06 @@ -214,7 +219,6 @@ extension interfaces. `module.Manager.Modules` is now of type `map[string]interf * (x/genutil) [#13535](https://github.com/cosmos/cosmos-sdk/pull/13535) Replace in `simd init`, the `--staking-bond-denom` flag with `--default-denom` which is used for all default denomination in the genesis, instead of only staking. * (tx) [#12659](https://github.com/cosmos/cosmos-sdk/pull/12659) Remove broadcast mode `block`. -* (genesis) [#14149](https://github.com/cosmos/cosmos-sdk/pull/14149) Add `simd genesis` command, which contains all genesis-related sub-commands. ### Bug Fixes