From d5f162c4fb626edcc565d2021b18671d12b644f5 Mon Sep 17 00:00:00 2001 From: emidev98 Date: Wed, 11 Oct 2023 18:07:57 +0300 Subject: [PATCH 1/2] fix: app genesis creation --- app/app.go | 2 +- cmd/allianced/cmd/genaccounts.go | 200 -------------------------- cmd/allianced/cmd/genaccounts_test.go | 111 -------------- cmd/allianced/cmd/root.go | 12 +- cmd/allianced/main.go | 3 +- 5 files changed, 13 insertions(+), 315 deletions(-) delete mode 100644 cmd/allianced/cmd/genaccounts.go delete mode 100644 cmd/allianced/cmd/genaccounts_test.go diff --git a/app/app.go b/app/app.go index 4b1a65ef..5592baa2 100644 --- a/app/app.go +++ b/app/app.go @@ -156,8 +156,8 @@ var ( // and genesis verification. ModuleBasics = module.NewBasicManager( auth.AppModuleBasic{}, + genutil.NewAppModuleBasic(genutiltypes.DefaultMessageValidator), authzmodule.AppModuleBasic{}, - genutil.AppModuleBasic{}, bank.AppModuleBasic{}, capability.AppModuleBasic{}, staking.AppModuleBasic{}, diff --git a/cmd/allianced/cmd/genaccounts.go b/cmd/allianced/cmd/genaccounts.go deleted file mode 100644 index 862a061a..00000000 --- a/cmd/allianced/cmd/genaccounts.go +++ /dev/null @@ -1,200 +0,0 @@ -package cmd - -import ( - "bufio" - "encoding/json" - "errors" - "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" - authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" - authvesting "github.com/cosmos/cosmos-sdk/x/auth/vesting/types" - 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" -) - -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 - } - } - - coins, err := sdk.ParseCoinsNormalized(args[1]) - if err != nil { - return fmt.Errorf("failed to parse coins: %w", err) - } - - vestingStart, _ := cmd.Flags().GetInt64(flagVestingStart) - vestingEnd, _ := cmd.Flags().GetInt64(flagVestingEnd) - vestingAmtStr, _ := cmd.Flags().GetString(flagVestingAmt) - - vestingAmt, err := sdk.ParseCoinsNormalized(vestingAmtStr) - if err != nil { - return fmt.Errorf("failed to parse vesting amount: %w", err) - } - - // create concrete account type based on input parameters - var genAccount authtypes.GenesisAccount - - balances := banktypes.Balance{Address: addr.String(), Coins: coins.Sort()} - baseAccount := authtypes.NewBaseAccount(addr, nil, 0, 0) - - if !vestingAmt.IsZero() { - baseVestingAccount := authvesting.NewBaseVestingAccount(baseAccount, vestingAmt.Sort(), vestingEnd) - - if (balances.Coins.IsZero() && !baseVestingAccount.OriginalVesting.IsZero()) || - baseVestingAccount.OriginalVesting.IsAnyGT(balances.Coins) { - return errors.New("vesting amount cannot be greater than total amount") - } - - switch { - case vestingStart != 0 && vestingEnd != 0: - genAccount = authvesting.NewContinuousVestingAccountRaw(baseVestingAccount, vestingStart) - - case vestingEnd != 0: - genAccount = authvesting.NewDelayedVestingAccountRaw(baseVestingAccount) - - default: - return errors.New("invalid vesting parameters; must supply start and end time or end time") - } - } else { - genAccount = baseAccount - } - - if err := genAccount.Validate(); err != nil { - return fmt.Errorf("failed to validate new genesis account: %w", err) - } - - genFile := config.GenesisFile() - appState, genDoc, err := genutiltypes.GenesisStateFromGenFile(genFile) - if err != nil { - return fmt.Errorf("failed to unmarshal genesis state: %w", err) - } - - authGenState := authtypes.GetGenesisStateFromAppState(clientCtx.Codec, appState) - - accs, err := authtypes.UnpackAccounts(authGenState.Accounts) - if err != nil { - return fmt.Errorf("failed to get accounts from any: %w", err) - } - - bankGenState := banktypes.GetGenesisStateFromAppState(clientCtx.Codec, appState) - if accs.Contains(addr) { - appendflag, _ := cmd.Flags().GetBool(flagAppendMode) - if !appendflag { - return fmt.Errorf("cannot add account at existing address %s", addr) - } - - genesisB := banktypes.GetGenesisStateFromAppState(clientCtx.Codec, appState) - for idx, acc := range genesisB.Balances { - if acc.Address != addr.String() { - continue - } - - updatedCoins := acc.Coins.Add(coins...) - bankGenState.Balances[idx] = banktypes.Balance{Address: addr.String(), Coins: updatedCoins.Sort()} - break - } - } else { - // Add the new account to the set of genesis accounts and sanitize the accounts afterwards. - accs = append(accs, genAccount) - accs = authtypes.SanitizeGenesisAccounts(accs) - - genAccs, err := authtypes.PackAccounts(accs) - if err != nil { - return fmt.Errorf("failed to convert accounts into any's: %w", err) - } - authGenState.Accounts = genAccs - - authGenStateBz, err := clientCtx.Codec.MarshalJSON(&authGenState) - if err != nil { - return fmt.Errorf("failed to marshal auth genesis state: %w", err) - } - appState[authtypes.ModuleName] = authGenStateBz - - bankGenState.Balances = append(bankGenState.Balances, balances) - } - - bankGenState.Balances = banktypes.SanitizeGenesisBalances(bankGenState.Balances) - - bankGenState.Supply = bankGenState.Supply.Add(balances.Coins...) - - bankGenStateBz, err := clientCtx.Codec.MarshalJSON(bankGenState) - if err != nil { - return fmt.Errorf("failed to marshal bank genesis state: %w", err) - } - appState[banktypes.ModuleName] = bankGenStateBz - - appStateJSON, err := json.Marshal(appState) - if err != nil { - return fmt.Errorf("failed to marshal application genesis state: %w", err) - } - - genDoc.AppState = appStateJSON - return genutil.ExportGenesisFile(genDoc, genFile) - }, - } - - 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/cmd/allianced/cmd/genaccounts_test.go b/cmd/allianced/cmd/genaccounts_test.go deleted file mode 100644 index ab5a81ce..00000000 --- a/cmd/allianced/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/cometbft/cometbft/libs/log" - "github.com/spf13/viper" - "github.com/stretchr/testify/require" - - "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" - "github.com/cosmos/cosmos-sdk/x/genutil" - genutiltest "github.com/cosmos/cosmos-sdk/x/genutil/client/testutil" - - "github.com/terra-money/alliance/app" - alliancecmd "github.com/terra-money/alliance/cmd/allianced/cmd" -) - -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, 2000bbn", - 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 := app.MakeTestEncodingConfig().Marshaler - 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, clientCtx.Codec) - 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 := alliancecmd.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/cmd/allianced/cmd/root.go b/cmd/allianced/cmd/root.go index cf59390e..454e0b60 100644 --- a/cmd/allianced/cmd/root.go +++ b/cmd/allianced/cmd/root.go @@ -167,7 +167,7 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) { genutilcli.MigrateGenesisCmd(), genutilcli.GenTxCmd(app.ModuleBasics, encodingConfig.TxConfig, banktypes.GenesisBalancesIterator{}, app.DefaultNodeHome), genutilcli.ValidateGenesisCmd(app.ModuleBasics), - AddGenesisAccountCmd(app.DefaultNodeHome), + genesisCommand(encodingConfig), tmcli.NewCompletionCmd(rootCmd, true), NewTestnetCmd(app.ModuleBasics, banktypes.GenesisBalancesIterator{}), debug.Cmd(), @@ -189,6 +189,16 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) { rootCmd.AddCommand(rosettaCmd.RosettaCommand(encodingConfig.InterfaceRegistry, encodingConfig.Marshaler)) } +// genesisCommand builds genesis-related `terrad 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, app.ModuleBasics, app.DefaultNodeHome) + + for _, sub_cmd := range cmds { + cmd.AddCommand(sub_cmd) + } + return cmd +} + func addModuleInitFlags(startCmd *cobra.Command) { crisis.AddModuleInitFlags(startCmd) } diff --git a/cmd/allianced/main.go b/cmd/allianced/main.go index bd68be96..df31f252 100644 --- a/cmd/allianced/main.go +++ b/cmd/allianced/main.go @@ -11,10 +11,9 @@ import ( ) func main() { - // app.SetAddressPrefixes() //TODO: either implement or remove rootCmd, _ := cmd.NewRootCmd() - if err := svrcmd.Execute(rootCmd, "ALLIANCED", app.DefaultNodeHome); err != nil { + if err := svrcmd.Execute(rootCmd, "allianced", app.DefaultNodeHome); err != nil { switch e := err.(type) { case server.ErrorCode: os.Exit(e.Code) From 807ce1ea96bebcd1f3ba6f839bff4d69f185a4a6 Mon Sep 17 00:00:00 2001 From: emidev98 Date: Mon, 16 Oct 2023 08:30:19 +0200 Subject: [PATCH 2/2] fix: linting --- cmd/allianced/cmd/root.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/allianced/cmd/root.go b/cmd/allianced/cmd/root.go index 454e0b60..8c549cdf 100644 --- a/cmd/allianced/cmd/root.go +++ b/cmd/allianced/cmd/root.go @@ -193,8 +193,8 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) { func genesisCommand(encodingConfig params.EncodingConfig, cmds ...*cobra.Command) *cobra.Command { cmd := genutilcli.GenesisCoreCommand(encodingConfig.TxConfig, app.ModuleBasics, app.DefaultNodeHome) - for _, sub_cmd := range cmds { - cmd.AddCommand(sub_cmd) + for _, subCmd := range cmds { + cmd.AddCommand(subCmd) } return cmd }