From f44aea0f9e0ef776855125b2f8e332332ad97b98 Mon Sep 17 00:00:00 2001 From: zakir Date: Tue, 25 Oct 2022 16:13:36 +0800 Subject: [PATCH] fix: gas prices flags --- app/cli/collect_gentx.go | 18 +++++++++++++++ {app/cli => cmd}/init.go | 50 ++++++++-------------------------------- cmd/root.go | 25 ++++++++++++++------ types/version_test.go | 7 ++++++ 4 files changed, 52 insertions(+), 48 deletions(-) rename {app/cli => cmd}/init.go (64%) diff --git a/app/cli/collect_gentx.go b/app/cli/collect_gentx.go index 2d8a300..4e21960 100644 --- a/app/cli/collect_gentx.go +++ b/app/cli/collect_gentx.go @@ -219,3 +219,21 @@ func CollectTxs(cdc codec.Codec, txJSONDecoder sdk.TxDecoder, moniker, genTxsDir return appGenTxs, persistentPeers, nil } + +type PrintInfo struct { + Moniker string `json:"moniker" yaml:"moniker"` + ChainID string `json:"chain_id" yaml:"chain_id"` + NodeID string `json:"node_id" yaml:"node_id"` + GenTxsDir string `json:"gentxs_dir" yaml:"gentxs_dir"` + AppMessage json.RawMessage `json:"app_message" yaml:"app_message"` +} + +func NewPrintInfo(moniker, chainID, nodeID, genTxsDir string, appMessage json.RawMessage) PrintInfo { + return PrintInfo{ + Moniker: moniker, + ChainID: chainID, + NodeID: nodeID, + GenTxsDir: genTxsDir, + AppMessage: appMessage, + } +} diff --git a/app/cli/init.go b/cmd/init.go similarity index 64% rename from app/cli/init.go rename to cmd/init.go index 3778a7c..fb5017c 100644 --- a/app/cli/init.go +++ b/cmd/init.go @@ -1,4 +1,4 @@ -package cli +package cmd import ( "bufio" @@ -9,7 +9,6 @@ import ( "path/filepath" "github.com/cosmos/cosmos-sdk/codec" - tmrand "github.com/tendermint/tendermint/libs/rand" tmproto "github.com/tendermint/tendermint/proto/tendermint/types" "github.com/cosmos/cosmos-sdk/client" @@ -24,6 +23,9 @@ import ( tmos "github.com/tendermint/tendermint/libs/os" "github.com/tendermint/tendermint/types" bip39 "github.com/tyler-smith/go-bip39" + + "github.com/pundix/pundix/app/cli" + pxtypes "github.com/pundix/pundix/types" ) const ( @@ -32,35 +34,11 @@ const ( // FlagRecover defines a flag to initialize the private validator key from a specific seed. FlagRecover = "recover" - - // FlagStakingDenom defines a flag to set the default coin denomination - FlagStakingDenom = "denom" - - // FlagMintDistributionDenom x/mint module mint token denom. - FlagMintDistributionDenom = "mint-denom" ) -type PrintInfo struct { - Moniker string `json:"moniker" yaml:"moniker"` - ChainID string `json:"chain_id" yaml:"chain_id"` - NodeID string `json:"node_id" yaml:"node_id"` - GenTxsDir string `json:"gentxs_dir" yaml:"gentxs_dir"` - AppMessage json.RawMessage `json:"app_message" yaml:"app_message"` -} - -func NewPrintInfo(moniker, chainID, nodeID, genTxsDir string, appMessage json.RawMessage) PrintInfo { - return PrintInfo{ - Moniker: moniker, - ChainID: chainID, - NodeID: nodeID, - GenTxsDir: genTxsDir, - AppMessage: appMessage, - } -} - // InitCmd returns a command that initializes all files needed for Tendermint // and the respective application. -func InitCmd(nodeHome, chainId, stakingDenom, mintDenom string, genGenesis func(stakingDenom, mintDenom string, cdc codec.JSONCodec) map[string]json.RawMessage, consensusParams *tmproto.ConsensusParams) *cobra.Command { +func InitCmd(nodeHome string, genGenesis func(stakingDenom, mintDenom string, cdc codec.JSONCodec) map[string]json.RawMessage, consensusParams *tmproto.ConsensusParams) *cobra.Command { cmd := &cobra.Command{ Use: "init [moniker]", Short: "Initialize private validator, p2p, genesis, and application configuration files", @@ -76,7 +54,7 @@ func InitCmd(nodeHome, chainId, stakingDenom, mintDenom string, genGenesis func( chainID, _ := cmd.Flags().GetString(flags.FlagChainID) if chainID == "" { - chainID = fmt.Sprintf("test-chain-%v", tmrand.Str(6)) + return fmt.Errorf("chain id cannot be empty") } // Get bip39 mnemonic @@ -110,15 +88,7 @@ func InitCmd(nodeHome, chainId, stakingDenom, mintDenom string, genGenesis func( if !overwrite && tmos.FileExists(genFile) { return fmt.Errorf("genesis.json file already exists: %v", genFile) } - flagStakingDenom, err := cmd.Flags().GetString(FlagStakingDenom) - if err != nil || flagStakingDenom == "" { - return fmt.Errorf("invalid staking denom: %v", err) - } - flagMintDistributionDenom, err := cmd.Flags().GetString(FlagMintDistributionDenom) - if err != nil || flagMintDistributionDenom == "" { - return fmt.Errorf("invalid mint denom: %v", err) - } - appState, err := json.MarshalIndent(genGenesis(flagStakingDenom, flagMintDistributionDenom, clientCtx.Codec), "", " ") + appState, err := json.MarshalIndent(genGenesis(pxtypes.StakingBondDenom(), pxtypes.MintDenom(), clientCtx.Codec), "", " ") if err != nil { return fmt.Errorf("failed to marshall default genesis state: %s", err.Error()) } @@ -143,7 +113,7 @@ func InitCmd(nodeHome, chainId, stakingDenom, mintDenom string, genGenesis func( return fmt.Errorf("failed to export gensis file: %s", err.Error()) } - toPrint := NewPrintInfo(config.Moniker, chainID, nodeID, "", appState) + toPrint := cli.NewPrintInfo(config.Moniker, chainID, nodeID, "", appState) cfg.WriteConfigFile(filepath.Join(config.RootDir, "config", "config.toml"), config) @@ -158,9 +128,7 @@ func InitCmd(nodeHome, chainId, stakingDenom, mintDenom string, genGenesis func( cmd.Flags().String(tmcli.HomeFlag, nodeHome, "node's home directory") cmd.Flags().Bool(FlagOverwrite, false, "overwrite the genesis.json file") cmd.Flags().Bool(FlagRecover, false, "provide seed phrase to recover existing key instead of creating") - cmd.Flags().String(flags.FlagChainID, chainId, "genesis file chain-id, if left blank will be randomly created") - cmd.Flags().String(FlagStakingDenom, stakingDenom, "set the default staking coin denomination") - cmd.Flags().String(FlagMintDistributionDenom, mintDenom, "set the default mint coin denomination") + cmd.Flags().String(flags.FlagChainID, "", "genesis file chain-id, if left blank will be randomly created") cmd.Flags().StringP(tmcli.OutputFlag, "o", "json", "Output format (text|json)") return cmd diff --git a/cmd/root.go b/cmd/root.go index bb0760e..d4b2811 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -6,7 +6,6 @@ import ( "io" "os" "path/filepath" - "strings" "github.com/cosmos/cosmos-sdk/baseapp" "github.com/cosmos/cosmos-sdk/client" @@ -91,6 +90,17 @@ func NewRootCmd() *cobra.Command { pxtypes.SetChainId(initClientCtx.ChainID) } + if f := cmd.Flags().Lookup(flags.FlagGasPrices); f != nil { + gasPricesStr, _ := cmd.Flags().GetString(flags.FlagGasPrices) + gasPrices, err := sdk.ParseCoinsNormalized(gasPricesStr) + if err != nil { + return err + } + if err := f.Value.Set(gasPrices.String()); err != nil { + panic(err) + } + } + defConfig := config.DefaultConfig() defConfig.MinGasPrices = fmt.Sprintf("2000000000000%s", pxtypes.StakingBondDenom()) if err := server.InterceptConfigsPreRunHandler(cmd, config.DefaultConfigTemplate, defConfig); err != nil { @@ -105,7 +115,7 @@ func NewRootCmd() *cobra.Command { flags.FlagKeyringBackend: keyring.BackendOS, flags.FlagGas: "auto", flags.FlagGasAdjustment: "1.5", - flags.FlagGasPrices: fmt.Sprintf("2000000000000%s", pxtypes.StakingBondDenom()), + flags.FlagGasPrices: "0.000002PUNDIX", }) return rootCmd } @@ -115,7 +125,7 @@ func initRootCmd(rootCmd *cobra.Command, encodingConfig app.EncodingConfig) { sdkCfgCmd.AddCommand(cli.AppTomlCmd(), cli.ConfigTomlCmd()) rootCmd.AddCommand( - cli.InitCmd(app.DefaultNodeHome, pxtypes.ChainId(), pxtypes.StakingBondDenom(), pxtypes.MintDenom(), app.NewDefAppGenesisByDenom, app.CustomConsensusParams()), + InitCmd(app.DefaultNodeHome, app.NewDefAppGenesisByDenom, app.CustomConsensusParams()), cli.CollectGenTxsCmd(banktypes.GenesisBalancesIterator{}, app.DefaultNodeHome), //genutilcli.MigrateGenesisCmd(), cli.GenTxCmd(app.ModuleBasics, encodingConfig.TxConfig, banktypes.GenesisBalancesIterator{}, app.DefaultNodeHome), @@ -265,9 +275,10 @@ func (a appCreator) newApp(logger log.Logger, db dbm.DB, traceStore io.Writer, a panic(err) } - gasPrice := cast.ToString(appOpts.Get(server.FlagMinGasPrices)) - if strings.Contains(gasPrice, ".") { - panic("Invalid gas price, cannot contain decimals") + gasPricesStr := cast.ToString(appOpts.Get(server.FlagMinGasPrices)) + gasPrices, err := sdk.ParseCoinsNormalized(gasPricesStr) + if err != nil { + panic(err) } return app.New( @@ -278,7 +289,7 @@ func (a appCreator) newApp(logger log.Logger, db dbm.DB, traceStore io.Writer, a // this line is used by starport scaffolding # stargate/root/appArgument appOpts, baseapp.SetPruning(pruningOpts), - baseapp.SetMinGasPrices(gasPrice), + baseapp.SetMinGasPrices(gasPrices.String()), baseapp.SetMinRetainBlocks(cast.ToUint64(appOpts.Get(server.FlagMinRetainBlocks))), baseapp.SetHaltHeight(cast.ToUint64(appOpts.Get(server.FlagHaltHeight))), baseapp.SetHaltTime(cast.ToUint64(appOpts.Get(server.FlagHaltTime))), diff --git a/types/version_test.go b/types/version_test.go index 0fa1889..76d1970 100644 --- a/types/version_test.go +++ b/types/version_test.go @@ -20,3 +20,10 @@ func TestRegisterDenom(t *testing.T) { assert.NoError(t, err) assert.Equal(t, sdk.NewCoins(sdk.NewCoin(StakingBondDenom(), sdk.NewInt(1e18))), coin) } + +func TestParseGasPrices(t *testing.T) { + SetChainId(MainnetChainId) + coin, err := sdk.ParseCoinsNormalized("0.000002PUNDIX") + assert.NoError(t, err) + t.Log(coin.String()) +}