From abc991c83bbe5a32667057848214bfcda082fcb4 Mon Sep 17 00:00:00 2001 From: Ruslan Akhtariev Date: Mon, 5 Dec 2022 15:35:01 +0700 Subject: [PATCH 01/13] wip --- go.mod | 3 + go.sum | 4 + simapp/simd/cmd/root.go | 13 +-- .../genutil/client/cli/genaccount.go | 6 +- x/genutil/client/cli/genaccount_test.go | 107 ++++++++++++++++++ x/genutil/client/cli/genesis_root.go | 34 ++++++ 6 files changed, 153 insertions(+), 14 deletions(-) rename simapp/simd/cmd/genaccounts.go => x/genutil/client/cli/genaccount.go (99%) create mode 100644 x/genutil/client/cli/genaccount_test.go create mode 100644 x/genutil/client/cli/genesis_root.go diff --git a/go.mod b/go.mod index 5f4598f378d2..7df7e826dcd3 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( cosmossdk.io/depinject v1.0.0-alpha.3 cosmossdk.io/errors v1.0.0-beta.7 cosmossdk.io/math v1.0.0-beta.4 + cosmossdk.io/simapp v0.0.0-20221203131133-390262f56bbd github.com/99designs/keyring v1.2.1 github.com/armon/go-metrics v0.4.1 github.com/bgentry/speakeasy v0.1.0 @@ -68,6 +69,7 @@ require ( cloud.google.com/go/compute/metadata v0.2.1 // indirect cloud.google.com/go/iam v0.7.0 // indirect cloud.google.com/go/storage v1.27.0 // indirect + cosmossdk.io/tools/rosetta v0.1.0 // indirect filippo.io/edwards25519 v1.0.0-rc.1 // indirect github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect github.com/ChainSafe/go-schnorrkel v0.0.0-20200405005733-88cbf1b4c40d // indirect @@ -77,6 +79,7 @@ require ( github.com/cenkalti/backoff/v4 v4.1.3 // indirect github.com/cespare/xxhash v1.1.0 // indirect github.com/cespare/xxhash/v2 v2.1.2 // indirect + github.com/coinbase/rosetta-sdk-go v0.8.1 // indirect github.com/cosmos/gorocksdb v1.2.0 // indirect github.com/creachadair/taskgroup v0.3.2 // indirect github.com/danieljoos/wincred v1.1.2 // indirect diff --git a/go.sum b/go.sum index 74253b0250fc..ef84015032ef 100644 --- a/go.sum +++ b/go.sum @@ -56,6 +56,8 @@ cosmossdk.io/errors v1.0.0-beta.7 h1:gypHW76pTQGVnHKo6QBkb4yFOJjC+sUGRc5Al3Odj1w cosmossdk.io/errors v1.0.0-beta.7/go.mod h1:mz6FQMJRku4bY7aqS/Gwfcmr/ue91roMEKAmDUDpBfE= cosmossdk.io/math v1.0.0-beta.4 h1:JtKedVLGzA0vv84xjYmZ75RKG35Kf2WwcFu8IjRkIIw= cosmossdk.io/math v1.0.0-beta.4/go.mod h1:An0MllWJY6PxibUpnwGk8jOm+a/qIxlKmL5Zyp9NnaM= +cosmossdk.io/simapp v0.0.0-20221203131133-390262f56bbd h1:8LpsJCPHih03oUOdvBpwK1wX1IULZ5/gWZEayCNJiog= +cosmossdk.io/simapp v0.0.0-20221203131133-390262f56bbd/go.mod h1:KHG3wxKJ8oq8elneHNqQs/zTCMHarjhEHf3Yv/gjCFE= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= filippo.io/edwards25519 v1.0.0-rc.1 h1:m0VOOB23frXZvAOK44usCgLWvtsxIoMCTBGJZlpmGfU= filippo.io/edwards25519 v1.0.0-rc.1/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns= @@ -159,6 +161,8 @@ github.com/cockroachdb/apd/v2 v2.0.2/go.mod h1:DDxRlzC2lo3/vSlmSoS7JkqbbrARPuFOG github.com/cockroachdb/apd/v3 v3.1.0 h1:MK3Ow7LH0W8zkd5GMKA1PvS9qG3bWFI95WaVNfyZJ/w= github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa/go.mod h1:zn76sxSg3SzpJ0PPJaLDCu+Bu0Lg3sKTORVIj19EIF8= github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI= +github.com/coinbase/rosetta-sdk-go v0.8.1 h1:WE+Temc8iz7Ra7sCpV9ymBJx78vItqFJ2xcSiPet1Pc= +github.com/coinbase/rosetta-sdk-go v0.8.1/go.mod h1:tXPR6AIW9ogsH4tYIaFOKOgfJNanCvcyl7JKLd4DToc= github.com/confio/ics23/go v0.9.0 h1:cWs+wdbS2KRPZezoaaj+qBleXgUk5WOQFMP3CQFGTr4= github.com/confio/ics23/go v0.9.0/go.mod h1:4LPZ2NYqnYIVRklaozjNR1FScgDJ2s5Xrp+e/mYVRak= github.com/containerd/continuity v0.3.0 h1:nisirsYROK15TAMVukJOUyGJjz4BNQJBVsNvAXZJ/eg= diff --git a/simapp/simd/cmd/root.go b/simapp/simd/cmd/root.go index 859c53dba360..8af461725451 100644 --- a/simapp/simd/cmd/root.go +++ b/simapp/simd/cmd/root.go @@ -39,9 +39,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 @@ -172,17 +170,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(), @@ -191,9 +181,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(), + genutilcli.GenesisRootCmd(encodingConfig), queryCommand(), txCommand(), keys.Commands(simapp.DefaultNodeHome), diff --git a/simapp/simd/cmd/genaccounts.go b/x/genutil/client/cli/genaccount.go similarity index 99% rename from simapp/simd/cmd/genaccounts.go rename to x/genutil/client/cli/genaccount.go index d04f36d62d20..cfb143daca63 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 ( diff --git a/x/genutil/client/cli/genaccount_test.go b/x/genutil/client/cli/genaccount_test.go new file mode 100644 index 000000000000..6fc2912275ab --- /dev/null +++ b/x/genutil/client/cli/genaccount_test.go @@ -0,0 +1,107 @@ +package cli_test + +import ( + "context" + "fmt" + "testing" + + "github.com/cosmos/cosmos-sdk/crypto/hd" + "github.com/cosmos/cosmos-sdk/crypto/keyring" + sdk "github.com/cosmos/cosmos-sdk/types" + + "github.com/spf13/viper" + "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" + moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" + "github.com/cosmos/cosmos-sdk/x/auth" + genutiltest "github.com/cosmos/cosmos-sdk/x/genutil/client/testutil" +) + +func TestAddGenesisAccountCmd(t *testing.T) { + _, _, addr1 := testdata.KeyTestPubAddr() + tests := []struct { + name string + addr string + denom string + withKeyring bool + expectErr bool + }{ + { + name: "invalid address", + addr: "", + denom: "1000atom", + withKeyring: false, + expectErr: true, + }, + { + name: "valid address", + addr: addr1.String(), + denom: "1000atom", + withKeyring: false, + expectErr: false, + }, + { + name: "multiple denoms", + addr: addr1.String(), + denom: "1000atom, 2000stake", + withKeyring: false, + expectErr: false, + }, + { + name: "with keyring", + addr: "ser", + denom: "1000atom", + withKeyring: true, + expectErr: false, + }, + } + + for _, tc := range tests { + tc := tc + t.Run(tc.name, func(t *testing.T) { + home := t.TempDir() + logger := log.NewNopLogger() + cfg, err := genutiltest.CreateDefaultTendermintConfig(home) + require.NoError(t, err) + + appCodec := moduletestutil.MakeTestEncodingConfig(auth.AppModuleBasic{}).Codec + err = genutiltest.ExecInitCmd(testMbm, home, appCodec) + require.NoError(t, err) + + serverCtx := server.NewContext(viper.New(), cfg, logger) + clientCtx := client.Context{}.WithCodec(appCodec).WithHomeDir(home) + + if tc.withKeyring { + path := hd.CreateHDPath(118, 0, 0).String() + kr, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendMemory, home, nil, appCodec) + require.NoError(t, err) + _, _, err = kr.NewMnemonic(tc.addr, keyring.English, path, keyring.DefaultBIP39Passphrase, hd.Secp256k1) + require.NoError(t, err) + clientCtx = clientCtx.WithKeyring(kr) + } + + ctx := context.Background() + ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx) + ctx = context.WithValue(ctx, server.ServerContextKey, serverCtx) + + cmd := simcmd.AddGenesisAccountCmd(home) + cmd.SetArgs([]string{ + tc.addr, + tc.denom, + fmt.Sprintf("--%s=home", flags.FlagHome), + }) + + if tc.expectErr { + require.Error(t, cmd.ExecuteContext(ctx)) + } else { + require.NoError(t, cmd.ExecuteContext(ctx)) + } + }) + } +} diff --git a/x/genutil/client/cli/genesis_root.go b/x/genutil/client/cli/genesis_root.go new file mode 100644 index 000000000000..a18e4bb7a639 --- /dev/null +++ b/x/genutil/client/cli/genesis_root.go @@ -0,0 +1,34 @@ +package cli + +import ( + "cosmossdk.io/simapp" + "cosmossdk.io/simapp/params" + "github.com/cosmos/cosmos-sdk/client" + 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" +) + +func GenesisRootCmd(encodingConfig params.EncodingConfig) *cobra.Command { + cmd := &cobra.Command{ + Use: "genesis", + Short: "Genesis subcommands", + DisableFlagParsing: false, + SuggestionsMinimumDistance: 2, + RunE: client.ValidateCmd, + } + gentxModule := simapp.ModuleBasics[genutiltypes.ModuleName].(genutil.AppModuleBasic) + + cmd.AddCommand( + GenTxCmd(simapp.ModuleBasics, encodingConfig.TxConfig, + banktypes.GenesisBalancesIterator{}, simapp.DefaultNodeHome), + MigrateGenesisCmd(), + CollectGenTxsCmd(banktypes.GenesisBalancesIterator{}, simapp.DefaultNodeHome, + gentxModule.GenTxValidator), + ValidateGenesisCmd(simapp.ModuleBasics), + AddGenesisAccountCmd(simapp.DefaultNodeHome), + ) + + return cmd +} From fbc9176e1bbb6a2fa7056a2acb4ebf8a3b7c3554 Mon Sep 17 00:00:00 2001 From: Ruslan Akhtariev Date: Mon, 5 Dec 2022 16:17:55 +0700 Subject: [PATCH 02/13] remove old test file, change import path in new one --- go.mod | 2 - go.sum | 2 - simapp/simd/cmd/genaccounts_test.go | 111 ------------------------ x/genutil/client/cli/genaccount_test.go | 4 +- 4 files changed, 2 insertions(+), 117 deletions(-) delete mode 100644 simapp/simd/cmd/genaccounts_test.go diff --git a/go.mod b/go.mod index 7df7e826dcd3..e0b63e91a9d0 100644 --- a/go.mod +++ b/go.mod @@ -69,7 +69,6 @@ require ( cloud.google.com/go/compute/metadata v0.2.1 // indirect cloud.google.com/go/iam v0.7.0 // indirect cloud.google.com/go/storage v1.27.0 // indirect - cosmossdk.io/tools/rosetta v0.1.0 // indirect filippo.io/edwards25519 v1.0.0-rc.1 // indirect github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect github.com/ChainSafe/go-schnorrkel v0.0.0-20200405005733-88cbf1b4c40d // indirect @@ -79,7 +78,6 @@ require ( github.com/cenkalti/backoff/v4 v4.1.3 // indirect github.com/cespare/xxhash v1.1.0 // indirect github.com/cespare/xxhash/v2 v2.1.2 // indirect - github.com/coinbase/rosetta-sdk-go v0.8.1 // indirect github.com/cosmos/gorocksdb v1.2.0 // indirect github.com/creachadair/taskgroup v0.3.2 // indirect github.com/danieljoos/wincred v1.1.2 // indirect diff --git a/go.sum b/go.sum index ef84015032ef..09508120d4da 100644 --- a/go.sum +++ b/go.sum @@ -161,8 +161,6 @@ github.com/cockroachdb/apd/v2 v2.0.2/go.mod h1:DDxRlzC2lo3/vSlmSoS7JkqbbrARPuFOG github.com/cockroachdb/apd/v3 v3.1.0 h1:MK3Ow7LH0W8zkd5GMKA1PvS9qG3bWFI95WaVNfyZJ/w= github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa/go.mod h1:zn76sxSg3SzpJ0PPJaLDCu+Bu0Lg3sKTORVIj19EIF8= github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI= -github.com/coinbase/rosetta-sdk-go v0.8.1 h1:WE+Temc8iz7Ra7sCpV9ymBJx78vItqFJ2xcSiPet1Pc= -github.com/coinbase/rosetta-sdk-go v0.8.1/go.mod h1:tXPR6AIW9ogsH4tYIaFOKOgfJNanCvcyl7JKLd4DToc= github.com/confio/ics23/go v0.9.0 h1:cWs+wdbS2KRPZezoaaj+qBleXgUk5WOQFMP3CQFGTr4= github.com/confio/ics23/go v0.9.0/go.mod h1:4LPZ2NYqnYIVRklaozjNR1FScgDJ2s5Xrp+e/mYVRak= github.com/containerd/continuity v0.3.0 h1:nisirsYROK15TAMVukJOUyGJjz4BNQJBVsNvAXZJ/eg= diff --git a/simapp/simd/cmd/genaccounts_test.go b/simapp/simd/cmd/genaccounts_test.go deleted file mode 100644 index eeea750e5464..000000000000 --- a/simapp/simd/cmd/genaccounts_test.go +++ /dev/null @@ -1,111 +0,0 @@ -package cmd_test - -import ( - "context" - "fmt" - "testing" - - "github.com/cosmos/cosmos-sdk/crypto/hd" - "github.com/cosmos/cosmos-sdk/crypto/keyring" - sdk "github.com/cosmos/cosmos-sdk/types" - - "github.com/spf13/viper" - "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" - 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 { - name string - addr string - denom string - withKeyring bool - expectErr bool - }{ - { - name: "invalid address", - addr: "", - denom: "1000atom", - withKeyring: false, - expectErr: true, - }, - { - name: "valid address", - addr: addr1.String(), - denom: "1000atom", - withKeyring: false, - expectErr: false, - }, - { - name: "multiple denoms", - addr: addr1.String(), - denom: "1000atom, 2000stake", - withKeyring: false, - expectErr: false, - }, - { - name: "with keyring", - addr: "ser", - denom: "1000atom", - withKeyring: true, - expectErr: false, - }, - } - - for _, tc := range tests { - tc := tc - t.Run(tc.name, func(t *testing.T) { - home := t.TempDir() - logger := log.NewNopLogger() - cfg, err := genutiltest.CreateDefaultTendermintConfig(home) - require.NoError(t, err) - - appCodec := moduletestutil.MakeTestEncodingConfig(auth.AppModuleBasic{}).Codec - err = genutiltest.ExecInitCmd(testMbm, home, appCodec) - require.NoError(t, err) - - serverCtx := server.NewContext(viper.New(), cfg, logger) - clientCtx := client.Context{}.WithCodec(appCodec).WithHomeDir(home) - - if tc.withKeyring { - path := hd.CreateHDPath(118, 0, 0).String() - kr, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendMemory, home, nil, appCodec) - require.NoError(t, err) - _, _, err = kr.NewMnemonic(tc.addr, keyring.English, path, keyring.DefaultBIP39Passphrase, hd.Secp256k1) - require.NoError(t, err) - clientCtx = clientCtx.WithKeyring(kr) - } - - ctx := context.Background() - ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx) - ctx = context.WithValue(ctx, server.ServerContextKey, serverCtx) - - cmd := simcmd.AddGenesisAccountCmd(home) - cmd.SetArgs([]string{ - tc.addr, - tc.denom, - fmt.Sprintf("--%s=home", flags.FlagHome), - }) - - if tc.expectErr { - require.Error(t, cmd.ExecuteContext(ctx)) - } else { - require.NoError(t, cmd.ExecuteContext(ctx)) - } - }) - } -} diff --git a/x/genutil/client/cli/genaccount_test.go b/x/genutil/client/cli/genaccount_test.go index 6fc2912275ab..811ebb67c139 100644 --- a/x/genutil/client/cli/genaccount_test.go +++ b/x/genutil/client/cli/genaccount_test.go @@ -13,13 +13,13 @@ 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" moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" "github.com/cosmos/cosmos-sdk/x/auth" + genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli" genutiltest "github.com/cosmos/cosmos-sdk/x/genutil/client/testutil" ) @@ -90,7 +90,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 5d8bdaa24898e16afe22d1bbfca9265a73a6bab8 Mon Sep 17 00:00:00 2001 From: Ruslan Akhtariev Date: Mon, 5 Dec 2022 16:25:22 +0700 Subject: [PATCH 03/13] bring back old genaccount files --- simapp/simd/cmd/genaccount.go | 87 ++++++++++++++++++++++ simapp/simd/cmd/genaccount_test.go | 112 +++++++++++++++++++++++++++++ x/genutil/client/cli/genaccount.go | 2 + 3 files changed, 201 insertions(+) create mode 100644 simapp/simd/cmd/genaccount.go create mode 100644 simapp/simd/cmd/genaccount_test.go diff --git a/simapp/simd/cmd/genaccount.go b/simapp/simd/cmd/genaccount.go new file mode 100644 index 000000000000..8e524fa0a5fa --- /dev/null +++ b/simapp/simd/cmd/genaccount.go @@ -0,0 +1,87 @@ +package cmd + +import ( + "bufio" + "fmt" + + "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 ( + flagVestingStart = "vesting-start-time" + flagVestingEnd = "vesting-end-time" + flagVestingAmt = "vesting-amount" + flagAppendMode = "append" +) + +// AddGenesisAccountCmd returns add-genesis-account cobra Command. +func AddGenesisAccountCmd(defaultNodeHome string) *cobra.Command { + cmd := &cobra.Command{ + Use: "add-genesis-account [address_or_key_name] [coin][,[coin]]", + Short: "Add a genesis account to genesis.json", + Long: `Add a genesis account to genesis.json. The provided account must specify +the account address or key name and a list of initial coins. If a key name is given, +the address will be looked up in the local Keybase. The list of initial tokens must +contain valid denominations. Accounts may optionally be supplied with vesting parameters. +`, + Args: cobra.ExactArgs(2), + RunE: func(cmd *cobra.Command, args []string) error { + clientCtx := client.GetClientContextFromCmd(cmd) + serverCtx := server.GetServerContextFromCmd(cmd) + config := serverCtx.Config + + config.SetRoot(clientCtx.HomeDir) + + var kr keyring.Keyring + addr, err := sdk.AccAddressFromBech32(args[0]) + if err != nil { + inBuf := bufio.NewReader(cmd.InOrStdin()) + keyringBackend, _ := cmd.Flags().GetString(flags.FlagKeyringBackend) + + if keyringBackend != "" && clientCtx.Keyring == nil { + var err error + kr, err = keyring.New(sdk.KeyringServiceName(), keyringBackend, clientCtx.HomeDir, inBuf, clientCtx.Codec) + if err != nil { + return err + } + } else { + kr = clientCtx.Keyring + } + + k, err := kr.Key(args[0]) + if err != nil { + return fmt.Errorf("failed to get address from Keyring: %w", err) + } + + addr, err = k.GetAddress() + if err != nil { + return err + } + } + + appendflag, _ := cmd.Flags().GetBool(flagAppendMode) + vestingStart, _ := cmd.Flags().GetInt64(flagVestingStart) + vestingEnd, _ := cmd.Flags().GetInt64(flagVestingEnd) + vestingAmtStr, _ := cmd.Flags().GetString(flagVestingAmt) + + return auth.AddGenesisAccount(clientCtx.Codec, addr, appendflag, config.GenesisFile(), args[1], vestingAmtStr, vestingStart, vestingEnd) + }, + } + + cmd.Flags().String(flags.FlagHome, defaultNodeHome, "The application home directory") + cmd.Flags().String(flags.FlagKeyringBackend, flags.DefaultKeyringBackend, "Select keyring's backend (os|file|kwallet|pass|test)") + cmd.Flags().String(flagVestingAmt, "", "amount of coins for vesting accounts") + cmd.Flags().Int64(flagVestingStart, 0, "schedule start time (unix epoch) for vesting accounts") + cmd.Flags().Int64(flagVestingEnd, 0, "schedule end time (unix epoch) for vesting accounts") + cmd.Flags().Bool(flagAppendMode, false, "append the coins to an account already in the genesis.json file") + flags.AddQueryFlagsToCmd(cmd) + + return cmd +} diff --git a/simapp/simd/cmd/genaccount_test.go b/simapp/simd/cmd/genaccount_test.go new file mode 100644 index 000000000000..54530d88a876 --- /dev/null +++ b/simapp/simd/cmd/genaccount_test.go @@ -0,0 +1,112 @@ +// This file is deprecated. Use x/genutil/client/cli/genaccount.go +package cmd_test + +import ( + "context" + "fmt" + "testing" + + simcmd "cosmossdk.io/simapp/simd/cmd" + "github.com/cosmos/cosmos-sdk/crypto/hd" + "github.com/cosmos/cosmos-sdk/crypto/keyring" + sdk "github.com/cosmos/cosmos-sdk/types" + + "github.com/cosmos/cosmos-sdk/types/module" + "github.com/cosmos/cosmos-sdk/x/genutil" + "github.com/spf13/viper" + "github.com/stretchr/testify/require" + "github.com/tendermint/tendermint/libs/log" + + "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" + moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" + "github.com/cosmos/cosmos-sdk/x/auth" + 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 { + name string + addr string + denom string + withKeyring bool + expectErr bool + }{ + { + name: "invalid address", + addr: "", + denom: "1000atom", + withKeyring: false, + expectErr: true, + }, + { + name: "valid address", + addr: addr1.String(), + denom: "1000atom", + withKeyring: false, + expectErr: false, + }, + { + name: "multiple denoms", + addr: addr1.String(), + denom: "1000atom, 2000stake", + withKeyring: false, + expectErr: false, + }, + { + name: "with keyring", + addr: "ser", + denom: "1000atom", + withKeyring: true, + expectErr: false, + }, + } + + for _, tc := range tests { + tc := tc + t.Run(tc.name, func(t *testing.T) { + home := t.TempDir() + logger := log.NewNopLogger() + cfg, err := genutiltest.CreateDefaultTendermintConfig(home) + require.NoError(t, err) + + appCodec := moduletestutil.MakeTestEncodingConfig(auth.AppModuleBasic{}).Codec + err = genutiltest.ExecInitCmd(testMbm, home, appCodec) + require.NoError(t, err) + + serverCtx := server.NewContext(viper.New(), cfg, logger) + clientCtx := client.Context{}.WithCodec(appCodec).WithHomeDir(home) + + if tc.withKeyring { + path := hd.CreateHDPath(118, 0, 0).String() + kr, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendMemory, home, nil, appCodec) + require.NoError(t, err) + _, _, err = kr.NewMnemonic(tc.addr, keyring.English, path, keyring.DefaultBIP39Passphrase, hd.Secp256k1) + require.NoError(t, err) + clientCtx = clientCtx.WithKeyring(kr) + } + + ctx := context.Background() + ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx) + ctx = context.WithValue(ctx, server.ServerContextKey, serverCtx) + + cmd := simcmd.AddGenesisAccountCmd(home) + cmd.SetArgs([]string{ + tc.addr, + tc.denom, + fmt.Sprintf("--%s=home", flags.FlagHome), + }) + + if tc.expectErr { + require.Error(t, cmd.ExecuteContext(ctx)) + } else { + require.NoError(t, cmd.ExecuteContext(ctx)) + } + }) + } +} diff --git a/x/genutil/client/cli/genaccount.go b/x/genutil/client/cli/genaccount.go index cfb143daca63..d9dffc909f81 100644 --- a/x/genutil/client/cli/genaccount.go +++ b/x/genutil/client/cli/genaccount.go @@ -1,3 +1,5 @@ +// This file is deprecated. Use x/genutil/client/cli/genaccount.go + package cli import ( From 5b751635024b34fb2d494655250f2e4284536464 Mon Sep 17 00:00:00 2001 From: Ruslan Akhtariev Date: Mon, 5 Dec 2022 17:17:22 +0700 Subject: [PATCH 04/13] move genesis creation function to simapp --- go.mod | 1 - go.sum | 2 -- simapp/simd/cmd/root.go | 27 +++++++++++++++++++++- x/genutil/client/cli/genesis_root.go | 34 ---------------------------- 4 files changed, 26 insertions(+), 38 deletions(-) delete mode 100644 x/genutil/client/cli/genesis_root.go diff --git a/go.mod b/go.mod index e0b63e91a9d0..5f4598f378d2 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,6 @@ require ( cosmossdk.io/depinject v1.0.0-alpha.3 cosmossdk.io/errors v1.0.0-beta.7 cosmossdk.io/math v1.0.0-beta.4 - cosmossdk.io/simapp v0.0.0-20221203131133-390262f56bbd github.com/99designs/keyring v1.2.1 github.com/armon/go-metrics v0.4.1 github.com/bgentry/speakeasy v0.1.0 diff --git a/go.sum b/go.sum index 09508120d4da..74253b0250fc 100644 --- a/go.sum +++ b/go.sum @@ -56,8 +56,6 @@ cosmossdk.io/errors v1.0.0-beta.7 h1:gypHW76pTQGVnHKo6QBkb4yFOJjC+sUGRc5Al3Odj1w cosmossdk.io/errors v1.0.0-beta.7/go.mod h1:mz6FQMJRku4bY7aqS/Gwfcmr/ue91roMEKAmDUDpBfE= cosmossdk.io/math v1.0.0-beta.4 h1:JtKedVLGzA0vv84xjYmZ75RKG35Kf2WwcFu8IjRkIIw= cosmossdk.io/math v1.0.0-beta.4/go.mod h1:An0MllWJY6PxibUpnwGk8jOm+a/qIxlKmL5Zyp9NnaM= -cosmossdk.io/simapp v0.0.0-20221203131133-390262f56bbd h1:8LpsJCPHih03oUOdvBpwK1wX1IULZ5/gWZEayCNJiog= -cosmossdk.io/simapp v0.0.0-20221203131133-390262f56bbd/go.mod h1:KHG3wxKJ8oq8elneHNqQs/zTCMHarjhEHf3Yv/gjCFE= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= filippo.io/edwards25519 v1.0.0-rc.1 h1:m0VOOB23frXZvAOK44usCgLWvtsxIoMCTBGJZlpmGfU= filippo.io/edwards25519 v1.0.0-rc.1/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7eHn5EwJns= diff --git a/simapp/simd/cmd/root.go b/simapp/simd/cmd/root.go index 8af461725451..6b0f94541528 100644 --- a/simapp/simd/cmd/root.go +++ b/simapp/simd/cmd/root.go @@ -39,7 +39,9 @@ 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 @@ -184,7 +186,7 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) { // add keybase, auxiliary RPC, query, genesis, and tx child commands rootCmd.AddCommand( rpc.StatusCommand(), - genutilcli.GenesisRootCmd(encodingConfig), + genesisCommand(encodingConfig), queryCommand(), txCommand(), keys.Commands(simapp.DefaultNodeHome), @@ -198,6 +200,29 @@ func addModuleInitFlags(startCmd *cobra.Command) { crisis.AddModuleInitFlags(startCmd) } +func genesisCommand(encodingConfig params.EncodingConfig) *cobra.Command { + cmd := &cobra.Command{ + Use: "genesis", + Short: "Genesis 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", diff --git a/x/genutil/client/cli/genesis_root.go b/x/genutil/client/cli/genesis_root.go deleted file mode 100644 index a18e4bb7a639..000000000000 --- a/x/genutil/client/cli/genesis_root.go +++ /dev/null @@ -1,34 +0,0 @@ -package cli - -import ( - "cosmossdk.io/simapp" - "cosmossdk.io/simapp/params" - "github.com/cosmos/cosmos-sdk/client" - 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" -) - -func GenesisRootCmd(encodingConfig params.EncodingConfig) *cobra.Command { - cmd := &cobra.Command{ - Use: "genesis", - Short: "Genesis subcommands", - DisableFlagParsing: false, - SuggestionsMinimumDistance: 2, - RunE: client.ValidateCmd, - } - gentxModule := simapp.ModuleBasics[genutiltypes.ModuleName].(genutil.AppModuleBasic) - - cmd.AddCommand( - GenTxCmd(simapp.ModuleBasics, encodingConfig.TxConfig, - banktypes.GenesisBalancesIterator{}, simapp.DefaultNodeHome), - MigrateGenesisCmd(), - CollectGenTxsCmd(banktypes.GenesisBalancesIterator{}, simapp.DefaultNodeHome, - gentxModule.GenTxValidator), - ValidateGenesisCmd(simapp.ModuleBasics), - AddGenesisAccountCmd(simapp.DefaultNodeHome), - ) - - return cmd -} From dd575c0918d69d641c4d1ebac4d90db4bfb00658 Mon Sep 17 00:00:00 2001 From: Ruslan Akhtariev Date: Tue, 6 Dec 2022 09:03:26 +0700 Subject: [PATCH 05/13] deleted old files --- simapp/simd/cmd/genaccount.go | 87 ---------------------- simapp/simd/cmd/genaccount_test.go | 112 ----------------------------- 2 files changed, 199 deletions(-) delete mode 100644 simapp/simd/cmd/genaccount.go delete mode 100644 simapp/simd/cmd/genaccount_test.go diff --git a/simapp/simd/cmd/genaccount.go b/simapp/simd/cmd/genaccount.go deleted file mode 100644 index 8e524fa0a5fa..000000000000 --- a/simapp/simd/cmd/genaccount.go +++ /dev/null @@ -1,87 +0,0 @@ -package cmd - -import ( - "bufio" - "fmt" - - "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 ( - flagVestingStart = "vesting-start-time" - flagVestingEnd = "vesting-end-time" - flagVestingAmt = "vesting-amount" - flagAppendMode = "append" -) - -// AddGenesisAccountCmd returns add-genesis-account cobra Command. -func AddGenesisAccountCmd(defaultNodeHome string) *cobra.Command { - cmd := &cobra.Command{ - Use: "add-genesis-account [address_or_key_name] [coin][,[coin]]", - Short: "Add a genesis account to genesis.json", - Long: `Add a genesis account to genesis.json. The provided account must specify -the account address or key name and a list of initial coins. If a key name is given, -the address will be looked up in the local Keybase. The list of initial tokens must -contain valid denominations. Accounts may optionally be supplied with vesting parameters. -`, - Args: cobra.ExactArgs(2), - RunE: func(cmd *cobra.Command, args []string) error { - clientCtx := client.GetClientContextFromCmd(cmd) - serverCtx := server.GetServerContextFromCmd(cmd) - config := serverCtx.Config - - config.SetRoot(clientCtx.HomeDir) - - var kr keyring.Keyring - addr, err := sdk.AccAddressFromBech32(args[0]) - if err != nil { - inBuf := bufio.NewReader(cmd.InOrStdin()) - keyringBackend, _ := cmd.Flags().GetString(flags.FlagKeyringBackend) - - if keyringBackend != "" && clientCtx.Keyring == nil { - var err error - kr, err = keyring.New(sdk.KeyringServiceName(), keyringBackend, clientCtx.HomeDir, inBuf, clientCtx.Codec) - if err != nil { - return err - } - } else { - kr = clientCtx.Keyring - } - - k, err := kr.Key(args[0]) - if err != nil { - return fmt.Errorf("failed to get address from Keyring: %w", err) - } - - addr, err = k.GetAddress() - if err != nil { - return err - } - } - - appendflag, _ := cmd.Flags().GetBool(flagAppendMode) - vestingStart, _ := cmd.Flags().GetInt64(flagVestingStart) - vestingEnd, _ := cmd.Flags().GetInt64(flagVestingEnd) - vestingAmtStr, _ := cmd.Flags().GetString(flagVestingAmt) - - return auth.AddGenesisAccount(clientCtx.Codec, addr, appendflag, config.GenesisFile(), args[1], vestingAmtStr, vestingStart, vestingEnd) - }, - } - - cmd.Flags().String(flags.FlagHome, defaultNodeHome, "The application home directory") - cmd.Flags().String(flags.FlagKeyringBackend, flags.DefaultKeyringBackend, "Select keyring's backend (os|file|kwallet|pass|test)") - cmd.Flags().String(flagVestingAmt, "", "amount of coins for vesting accounts") - cmd.Flags().Int64(flagVestingStart, 0, "schedule start time (unix epoch) for vesting accounts") - cmd.Flags().Int64(flagVestingEnd, 0, "schedule end time (unix epoch) for vesting accounts") - cmd.Flags().Bool(flagAppendMode, false, "append the coins to an account already in the genesis.json file") - flags.AddQueryFlagsToCmd(cmd) - - return cmd -} diff --git a/simapp/simd/cmd/genaccount_test.go b/simapp/simd/cmd/genaccount_test.go deleted file mode 100644 index 54530d88a876..000000000000 --- a/simapp/simd/cmd/genaccount_test.go +++ /dev/null @@ -1,112 +0,0 @@ -// This file is deprecated. Use x/genutil/client/cli/genaccount.go -package cmd_test - -import ( - "context" - "fmt" - "testing" - - simcmd "cosmossdk.io/simapp/simd/cmd" - "github.com/cosmos/cosmos-sdk/crypto/hd" - "github.com/cosmos/cosmos-sdk/crypto/keyring" - sdk "github.com/cosmos/cosmos-sdk/types" - - "github.com/cosmos/cosmos-sdk/types/module" - "github.com/cosmos/cosmos-sdk/x/genutil" - "github.com/spf13/viper" - "github.com/stretchr/testify/require" - "github.com/tendermint/tendermint/libs/log" - - "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" - moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" - "github.com/cosmos/cosmos-sdk/x/auth" - 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 { - name string - addr string - denom string - withKeyring bool - expectErr bool - }{ - { - name: "invalid address", - addr: "", - denom: "1000atom", - withKeyring: false, - expectErr: true, - }, - { - name: "valid address", - addr: addr1.String(), - denom: "1000atom", - withKeyring: false, - expectErr: false, - }, - { - name: "multiple denoms", - addr: addr1.String(), - denom: "1000atom, 2000stake", - withKeyring: false, - expectErr: false, - }, - { - name: "with keyring", - addr: "ser", - denom: "1000atom", - withKeyring: true, - expectErr: false, - }, - } - - for _, tc := range tests { - tc := tc - t.Run(tc.name, func(t *testing.T) { - home := t.TempDir() - logger := log.NewNopLogger() - cfg, err := genutiltest.CreateDefaultTendermintConfig(home) - require.NoError(t, err) - - appCodec := moduletestutil.MakeTestEncodingConfig(auth.AppModuleBasic{}).Codec - err = genutiltest.ExecInitCmd(testMbm, home, appCodec) - require.NoError(t, err) - - serverCtx := server.NewContext(viper.New(), cfg, logger) - clientCtx := client.Context{}.WithCodec(appCodec).WithHomeDir(home) - - if tc.withKeyring { - path := hd.CreateHDPath(118, 0, 0).String() - kr, err := keyring.New(sdk.KeyringServiceName(), keyring.BackendMemory, home, nil, appCodec) - require.NoError(t, err) - _, _, err = kr.NewMnemonic(tc.addr, keyring.English, path, keyring.DefaultBIP39Passphrase, hd.Secp256k1) - require.NoError(t, err) - clientCtx = clientCtx.WithKeyring(kr) - } - - ctx := context.Background() - ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx) - ctx = context.WithValue(ctx, server.ServerContextKey, serverCtx) - - cmd := simcmd.AddGenesisAccountCmd(home) - cmd.SetArgs([]string{ - tc.addr, - tc.denom, - fmt.Sprintf("--%s=home", flags.FlagHome), - }) - - if tc.expectErr { - require.Error(t, cmd.ExecuteContext(ctx)) - } else { - require.NoError(t, cmd.ExecuteContext(ctx)) - } - }) - } -} From 9e67cc3172ecb5a56e20a3b8f67a239ec7f00c1c Mon Sep 17 00:00:00 2001 From: Ruslan Akhtariev Date: Tue, 6 Dec 2022 09:15:57 +0700 Subject: [PATCH 06/13] add functionality to add blockchain specific genesis subcommands --- simapp/simd/cmd/root.go | 17 ++++++++++++++--- x/genutil/client/cli/genaccount_test.go | 1 + 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/simapp/simd/cmd/root.go b/simapp/simd/cmd/root.go index 6b0f94541528..9e99d2a39fa9 100644 --- a/simapp/simd/cmd/root.go +++ b/simapp/simd/cmd/root.go @@ -200,10 +200,22 @@ func addModuleInitFlags(startCmd *cobra.Command) { crisis.AddModuleInitFlags(startCmd) } -func genesisCommand(encodingConfig params.EncodingConfig) *cobra.Command { +// 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 := 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: "Genesis subcommands", + Short: "Application's genesis-related subcommands", DisableFlagParsing: false, SuggestionsMinimumDistance: 2, RunE: client.ValidateCmd, @@ -222,7 +234,6 @@ func genesisCommand(encodingConfig params.EncodingConfig) *cobra.Command { return cmd } - func queryCommand() *cobra.Command { cmd := &cobra.Command{ Use: "query", diff --git a/x/genutil/client/cli/genaccount_test.go b/x/genutil/client/cli/genaccount_test.go index 811ebb67c139..31886b074805 100644 --- a/x/genutil/client/cli/genaccount_test.go +++ b/x/genutil/client/cli/genaccount_test.go @@ -17,6 +17,7 @@ import ( "github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/server" "github.com/cosmos/cosmos-sdk/testutil/testdata" + moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil" "github.com/cosmos/cosmos-sdk/x/auth" genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli" From 5542cfb7824e11a4d29d37cdbd6cff46e47cabd3 Mon Sep 17 00:00:00 2001 From: Ruslan Akhtariev Date: Tue, 6 Dec 2022 09:16:39 +0700 Subject: [PATCH 07/13] docs update --- x/genutil/client/cli/genaccount.go | 1 + 1 file changed, 1 insertion(+) diff --git a/x/genutil/client/cli/genaccount.go b/x/genutil/client/cli/genaccount.go index d9dffc909f81..cd5fd2c64a43 100644 --- a/x/genutil/client/cli/genaccount.go +++ b/x/genutil/client/cli/genaccount.go @@ -24,6 +24,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]]", From 51ce0d92e2cd3bdd05bee3509f9897330490729d Mon Sep 17 00:00:00 2001 From: Ruslan Akhtariev Date: Tue, 6 Dec 2022 09:20:08 +0700 Subject: [PATCH 08/13] changelog upd --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 925c39afb996..eb31f6675066 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 From 0125743e292254e509614e9d25b17222a15a5ae5 Mon Sep 17 00:00:00 2001 From: Ruslan Akhtariev Date: Tue, 6 Dec 2022 09:25:07 +0700 Subject: [PATCH 09/13] update scripts and docs --- contrib/rosetta/configuration/data.sh | 8 ++++---- docs/architecture/adr-041-in-place-store-migrations.md | 4 ++-- docs/docs/run-node/01-run-node.md | 8 ++++---- simapp/README.md | 6 +++--- tools/cosmovisor/README.md | 6 +++--- 5 files changed, 16 insertions(+), 16 deletions(-) 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/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 From 42623c52d5a0a05482ef8f069b3b511bb9cb9e3b Mon Sep 17 00:00:00 2001 From: Ruslan Akhtariev Date: Wed, 7 Dec 2022 09:47:52 +0700 Subject: [PATCH 10/13] rm unneded doc --- docs/docs/architecture/adr-057-app-wiring.md | 0 x/genutil/client/cli/genaccount.go | 2 -- 2 files changed, 2 deletions(-) create mode 100644 docs/docs/architecture/adr-057-app-wiring.md diff --git a/docs/docs/architecture/adr-057-app-wiring.md b/docs/docs/architecture/adr-057-app-wiring.md new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/x/genutil/client/cli/genaccount.go b/x/genutil/client/cli/genaccount.go index cd5fd2c64a43..043428daf5ab 100644 --- a/x/genutil/client/cli/genaccount.go +++ b/x/genutil/client/cli/genaccount.go @@ -1,5 +1,3 @@ -// This file is deprecated. Use x/genutil/client/cli/genaccount.go - package cli import ( From 9093204af25b8b1bd23ad74f1bc32b6a2220daf5 Mon Sep 17 00:00:00 2001 From: Ruslan Akhtariev Date: Wed, 7 Dec 2022 10:08:05 +0700 Subject: [PATCH 11/13] move core genesis cmd to genutils --- simapp/simd/cmd/root.go | 28 +----------------- x/genutil/client/cli/core_genesis_cmd.go | 36 ++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 27 deletions(-) create mode 100644 x/genutil/client/cli/core_genesis_cmd.go diff --git a/simapp/simd/cmd/root.go b/simapp/simd/cmd/root.go index 9e99d2a39fa9..088bdcb122e1 100644 --- a/simapp/simd/cmd/root.go +++ b/simapp/simd/cmd/root.go @@ -39,9 +39,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 @@ -202,7 +200,7 @@ func addModuleInitFlags(startCmd *cobra.Command) { // 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 := genesisCoreCommand(encodingConfig) + cmd := genutilcli.GenesisCoreCommand(encodingConfig.TxConfig, simapp.ModuleBasics, simapp.DefaultNodeHome) for _, sub_cmd := range cmds { cmd.AddCommand(sub_cmd) @@ -210,30 +208,6 @@ func genesisCommand(encodingConfig params.EncodingConfig, cmds ...*cobra.Command 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", 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..e5aef40e56f5 --- /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(encodingConfig 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, encodingConfig, + banktypes.GenesisBalancesIterator{}, defaultNodeHome), + MigrateGenesisCmd(), + CollectGenTxsCmd(banktypes.GenesisBalancesIterator{}, defaultNodeHome, + gentxModule.GenTxValidator), + ValidateGenesisCmd(moduleBasics), + AddGenesisAccountCmd(defaultNodeHome), + ) + + return cmd +} From f3ab2a786b7c14a6244b392892f60f78a702936c Mon Sep 17 00:00:00 2001 From: Ruslan Akhtariev Date: Wed, 7 Dec 2022 16:35:55 +0700 Subject: [PATCH 12/13] rm empty file --- docs/docs/architecture/adr-057-app-wiring.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 docs/docs/architecture/adr-057-app-wiring.md diff --git a/docs/docs/architecture/adr-057-app-wiring.md b/docs/docs/architecture/adr-057-app-wiring.md deleted file mode 100644 index e69de29bb2d1..000000000000 From 2660722a1df540ab3ad95b7466bd6b2c08fcdda4 Mon Sep 17 00:00:00 2001 From: Ruslan Akhtariev Date: Wed, 7 Dec 2022 16:38:16 +0700 Subject: [PATCH 13/13] docs and little changes --- x/genutil/client/cli/core_genesis_cmd.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/x/genutil/client/cli/core_genesis_cmd.go b/x/genutil/client/cli/core_genesis_cmd.go index e5aef40e56f5..384ddc9c782c 100644 --- a/x/genutil/client/cli/core_genesis_cmd.go +++ b/x/genutil/client/cli/core_genesis_cmd.go @@ -10,9 +10,9 @@ import ( "github.com/spf13/cobra" ) -// genesisCoreCommand adds core sdk's sub-commands into genesis command: +// GenesisCoreCommand adds core sdk's sub-commands into genesis command: // -> gentx, migrate, collect-gentxs, validate-genesis, add-genesis-account -func GenesisCoreCommand(encodingConfig client.TxConfig, moduleBasics module.BasicManager, defaultNodeHome string) *cobra.Command { +func GenesisCoreCommand(txConfig client.TxConfig, moduleBasics module.BasicManager, defaultNodeHome string) *cobra.Command { cmd := &cobra.Command{ Use: "genesis", Short: "Application's genesis-related subcommands", @@ -23,7 +23,7 @@ func GenesisCoreCommand(encodingConfig client.TxConfig, moduleBasics module.Basi gentxModule := moduleBasics[genutiltypes.ModuleName].(genutil.AppModuleBasic) cmd.AddCommand( - GenTxCmd(moduleBasics, encodingConfig, + GenTxCmd(moduleBasics, txConfig, banktypes.GenesisBalancesIterator{}, defaultNodeHome), MigrateGenesisCmd(), CollectGenTxsCmd(banktypes.GenesisBalancesIterator{}, defaultNodeHome,