Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(simapp): Genesis related commands under one genesis command (backport #14149) #14199

Merged
merged 2 commits into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions contrib/rosetta/configuration/data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions docs/architecture/adr-041-in-place-store-migrations.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions docs/docs/run-node/01-run-node.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:
Expand All @@ -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`
Expand Down
6 changes: 3 additions & 3 deletions simapp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 12 additions & 11 deletions simapp/simd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(),
Expand All @@ -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),
Expand All @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions tools/cosmovisor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions x/genutil/client/cli/core_genesis_cmd.go
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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 (
Expand All @@ -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]]",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd_test
package cli_test

import (
"context"
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down