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 #14149

Merged
merged 14 commits into from
Dec 7, 2022
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,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

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
45 changes: 36 additions & 9 deletions simapp/simd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,17 +172,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 @@ -191,9 +183,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 @@ -207,6 +200,40 @@ 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 {
Copy link
Member

@julienrbrt julienrbrt Dec 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not so useful if genesisCoreCommand is still in simapp.
We can move genesisCoreCommand under x/genutil/... but do not import simapp (so, adding BasicManager as an argument and the nodeHomePath)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's a fair point, changes addressed

cmd := genesisCoreCommand(encodingConfig)

for _, sub_cmd := range cmds {
cmd.AddCommand(sub_cmd)
}
return cmd
}

// genesisCoreCommand adds core sdk's sub-commands into genesis command:
// -> gentx, migrate, collect-gentxs, validate-genesis, add-genesis-account
func genesisCoreCommand(encodingConfig params.EncodingConfig) *cobra.Command {
cmd := &cobra.Command{
Use: "genesis",
Short: "Application's genesis-related subcommands",
DisableFlagParsing: false,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
gentxModule := simapp.ModuleBasics[genutiltypes.ModuleName].(genutil.AppModuleBasic)

cmd.AddCommand(
genutilcli.GenTxCmd(simapp.ModuleBasics, encodingConfig.TxConfig,
banktypes.GenesisBalancesIterator{}, simapp.DefaultNodeHome),
genutilcli.MigrateGenesisCmd(),
genutilcli.CollectGenTxsCmd(banktypes.GenesisBalancesIterator{}, simapp.DefaultNodeHome,
gentxModule.GenTxValidator),
genutilcli.ValidateGenesisCmd(simapp.ModuleBasics),
genutilcli.AddGenesisAccountCmd(simapp.DefaultNodeHome),
)

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
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package cmd
// This file is deprecated. Use x/genutil/client/cli/genaccount.go
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// This file is deprecated. Use x/genutil/client/cli/genaccount.go

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not resolved. We can remove this comment.


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 +24,7 @@ const (
)

// AddGenesisAccountCmd returns add-genesis-account cobra Command.
pysel marked this conversation as resolved.
Show resolved Hide resolved
// 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