|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "io" |
| 6 | + "os" |
| 7 | + |
| 8 | + "github.com/spf13/cobra" |
| 9 | + "github.com/spf13/viper" |
| 10 | + |
| 11 | + "github.com/cosmos/cosmos-sdk/client" |
| 12 | + "github.com/cosmos/cosmos-sdk/client/config" |
| 13 | + "github.com/cosmos/cosmos-sdk/client/debug" |
| 14 | + "github.com/cosmos/cosmos-sdk/client/flags" |
| 15 | + "github.com/cosmos/cosmos-sdk/client/keys" |
| 16 | + "github.com/cosmos/cosmos-sdk/client/pruning" |
| 17 | + "github.com/cosmos/cosmos-sdk/client/rpc" |
| 18 | + "github.com/cosmos/cosmos-sdk/client/snapshot" |
| 19 | + "github.com/cosmos/cosmos-sdk/server" |
| 20 | + serverconfig "github.com/cosmos/cosmos-sdk/server/config" |
| 21 | + servertypes "github.com/cosmos/cosmos-sdk/server/types" |
| 22 | + simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" |
| 23 | + sdk "github.com/cosmos/cosmos-sdk/types" |
| 24 | + authcmd "github.com/cosmos/cosmos-sdk/x/auth/client/cli" |
| 25 | + "github.com/cosmos/cosmos-sdk/x/auth/types" |
| 26 | + "github.com/cosmos/cosmos-sdk/x/crisis" |
| 27 | + genutilcli "github.com/cosmos/cosmos-sdk/x/genutil/client/cli" |
| 28 | + |
| 29 | + dbm "github.com/cometbft/cometbft-db" |
| 30 | + cmtcfg "github.com/cometbft/cometbft/config" |
| 31 | + "github.com/cometbft/cometbft/libs/log" |
| 32 | + |
| 33 | + "github.com/cosmos/ibc-go/modules/apps/callbacks/testing/simapp" |
| 34 | + "github.com/cosmos/ibc-go/modules/apps/callbacks/testing/simapp/params" |
| 35 | +) |
| 36 | + |
| 37 | +// NewRootCmd creates a new root command for simd. It is called once in the |
| 38 | +// main function. |
| 39 | +func NewRootCmd() *cobra.Command { |
| 40 | + // we "pre"-instantiate the application for getting the injected/configured encoding configuration |
| 41 | + tempApp := simapp.NewSimApp(log.NewNopLogger(), dbm.NewMemDB(), nil, true, simtestutil.NewAppOptionsWithFlagHome(simapp.DefaultNodeHome)) |
| 42 | + encodingConfig := params.EncodingConfig{ |
| 43 | + InterfaceRegistry: tempApp.InterfaceRegistry(), |
| 44 | + Codec: tempApp.AppCodec(), |
| 45 | + TxConfig: tempApp.TxConfig(), |
| 46 | + Amino: tempApp.LegacyAmino(), |
| 47 | + } |
| 48 | + |
| 49 | + initClientCtx := client.Context{}. |
| 50 | + WithCodec(encodingConfig.Codec). |
| 51 | + WithInterfaceRegistry(encodingConfig.InterfaceRegistry). |
| 52 | + WithTxConfig(encodingConfig.TxConfig). |
| 53 | + WithLegacyAmino(encodingConfig.Amino). |
| 54 | + WithInput(os.Stdin). |
| 55 | + WithAccountRetriever(types.AccountRetriever{}). |
| 56 | + WithHomeDir(simapp.DefaultNodeHome). |
| 57 | + WithViper("") // In simapp, we don't use any prefix for env variables. |
| 58 | + |
| 59 | + rootCmd := &cobra.Command{ |
| 60 | + Use: "simd", |
| 61 | + Short: "simulation app", |
| 62 | + PersistentPreRunE: func(cmd *cobra.Command, _ []string) error { |
| 63 | + // set the default command outputs |
| 64 | + cmd.SetOut(cmd.OutOrStdout()) |
| 65 | + cmd.SetErr(cmd.ErrOrStderr()) |
| 66 | + |
| 67 | + initClientCtx, err := client.ReadPersistentCommandFlags(initClientCtx, cmd.Flags()) |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + |
| 72 | + initClientCtx, err = config.ReadFromClientConfig(initClientCtx) |
| 73 | + if err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + |
| 77 | + if err := client.SetCmdClientContextHandler(initClientCtx, cmd); err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + |
| 81 | + customAppTemplate, customAppConfig := initAppConfig() |
| 82 | + customTMConfig := initTendermintConfig() |
| 83 | + |
| 84 | + return server.InterceptConfigsPreRunHandler(cmd, customAppTemplate, customAppConfig, customTMConfig) |
| 85 | + }, |
| 86 | + } |
| 87 | + |
| 88 | + initRootCmd(rootCmd, encodingConfig) |
| 89 | + |
| 90 | + return rootCmd |
| 91 | +} |
| 92 | + |
| 93 | +// initTendermintConfig helps to override default Tendermint Config values. |
| 94 | +// return cmtcfg.DefaultConfig if no custom configuration is required for the application. |
| 95 | +func initTendermintConfig() *cmtcfg.Config { |
| 96 | + cfg := cmtcfg.DefaultConfig() |
| 97 | + |
| 98 | + // these values put a higher strain on node memory |
| 99 | + // cfg.P2P.MaxNumInboundPeers = 100 |
| 100 | + // cfg.P2P.MaxNumOutboundPeers = 40 |
| 101 | + |
| 102 | + return cfg |
| 103 | +} |
| 104 | + |
| 105 | +// initAppConfig helps to override default appConfig template and configs. |
| 106 | +// return "", nil if no custom configuration is required for the application. |
| 107 | +func initAppConfig() (string, interface{}) { |
| 108 | + // The following code snippet is just for reference. |
| 109 | + |
| 110 | + // WASMConfig defines configuration for the wasm module. |
| 111 | + type WASMConfig struct { |
| 112 | + // This is the maximum sdk gas (wasm and storage) that we allow for any x/wasm "smart" queries |
| 113 | + QueryGasLimit uint64 `mapstructure:"query_gas_limit"` |
| 114 | + |
| 115 | + // Address defines the gRPC-web server to listen on |
| 116 | + LruSize uint64 `mapstructure:"lru_size"` |
| 117 | + } |
| 118 | + |
| 119 | + type CustomAppConfig struct { |
| 120 | + serverconfig.Config |
| 121 | + |
| 122 | + WASM WASMConfig `mapstructure:"wasm"` |
| 123 | + } |
| 124 | + |
| 125 | + // Optionally allow the chain developer to overwrite the SDK's default |
| 126 | + // server config. |
| 127 | + srvCfg := serverconfig.DefaultConfig() |
| 128 | + // The SDK's default minimum gas price is set to "" (empty value) inside |
| 129 | + // app.toml. If left empty by validators, the node will halt on startup. |
| 130 | + // However, the chain developer can set a default app.toml value for their |
| 131 | + // validators here. |
| 132 | + // |
| 133 | + // In summary: |
| 134 | + // - if you leave srvCfg.MinGasPrices = "", all validators MUST tweak their |
| 135 | + // own app.toml config, |
| 136 | + // - if you set srvCfg.MinGasPrices non-empty, validators CAN tweak their |
| 137 | + // own app.toml to override, or use this default value. |
| 138 | + // |
| 139 | + // In simapp, we set the min gas prices to 0. |
| 140 | + srvCfg.MinGasPrices = "0stake" |
| 141 | + // srvCfg.BaseConfig.IAVLDisableFastNode = true // disable fastnode by default |
| 142 | + |
| 143 | + customAppConfig := CustomAppConfig{ |
| 144 | + Config: *srvCfg, |
| 145 | + WASM: WASMConfig{ |
| 146 | + LruSize: 1, |
| 147 | + QueryGasLimit: 300000, |
| 148 | + }, |
| 149 | + } |
| 150 | + |
| 151 | + customAppTemplate := serverconfig.DefaultConfigTemplate + ` |
| 152 | +[wasm] |
| 153 | +# This is the maximum sdk gas (wasm and storage) that we allow for any x/wasm "smart" queries |
| 154 | +query_gas_limit = 300000 |
| 155 | +# This is the number of wasm vm instances we keep cached in memory for speed-up |
| 156 | +# Warning: this is currently unstable and may lead to crashes, best to keep for 0 unless testing locally |
| 157 | +lru_size = 0` |
| 158 | + |
| 159 | + return customAppTemplate, customAppConfig |
| 160 | +} |
| 161 | + |
| 162 | +func initRootCmd(rootCmd *cobra.Command, encodingConfig params.EncodingConfig) { |
| 163 | + cfg := sdk.GetConfig() |
| 164 | + cfg.Seal() |
| 165 | + |
| 166 | + rootCmd.AddCommand( |
| 167 | + genutilcli.InitCmd(simapp.ModuleBasics, simapp.DefaultNodeHome), |
| 168 | + debug.Cmd(), |
| 169 | + config.Cmd(), |
| 170 | + pruning.PruningCmd(newApp), |
| 171 | + snapshot.Cmd(newApp), |
| 172 | + ) |
| 173 | + |
| 174 | + server.AddCommands(rootCmd, simapp.DefaultNodeHome, newApp, appExport, addModuleInitFlags) |
| 175 | + |
| 176 | + // add keybase, auxiliary RPC, query, genesis, and tx child commands |
| 177 | + rootCmd.AddCommand( |
| 178 | + rpc.StatusCommand(), |
| 179 | + genesisCommand(encodingConfig), |
| 180 | + queryCommand(), |
| 181 | + txCommand(), |
| 182 | + keys.Commands(simapp.DefaultNodeHome), |
| 183 | + ) |
| 184 | +} |
| 185 | + |
| 186 | +func addModuleInitFlags(startCmd *cobra.Command) { |
| 187 | + crisis.AddModuleInitFlags(startCmd) |
| 188 | +} |
| 189 | + |
| 190 | +// genesisCommand builds genesis-related `simd genesis` command. Users may provide application specific commands as a parameter |
| 191 | +func genesisCommand(encodingConfig params.EncodingConfig, cmds ...*cobra.Command) *cobra.Command { |
| 192 | + cmd := genutilcli.GenesisCoreCommand(encodingConfig.TxConfig, simapp.ModuleBasics, simapp.DefaultNodeHome) |
| 193 | + |
| 194 | + for _, subCmd := range cmds { |
| 195 | + cmd.AddCommand(subCmd) |
| 196 | + } |
| 197 | + return cmd |
| 198 | +} |
| 199 | + |
| 200 | +func queryCommand() *cobra.Command { |
| 201 | + cmd := &cobra.Command{ |
| 202 | + Use: "query", |
| 203 | + Aliases: []string{"q"}, |
| 204 | + Short: "Querying subcommands", |
| 205 | + DisableFlagParsing: false, |
| 206 | + SuggestionsMinimumDistance: 2, |
| 207 | + RunE: client.ValidateCmd, |
| 208 | + } |
| 209 | + |
| 210 | + cmd.AddCommand( |
| 211 | + authcmd.GetAccountCmd(), |
| 212 | + rpc.ValidatorCommand(), |
| 213 | + rpc.BlockCommand(), |
| 214 | + authcmd.QueryTxsByEventsCmd(), |
| 215 | + authcmd.QueryTxCmd(), |
| 216 | + ) |
| 217 | + |
| 218 | + simapp.ModuleBasics.AddQueryCommands(cmd) |
| 219 | + |
| 220 | + return cmd |
| 221 | +} |
| 222 | + |
| 223 | +func txCommand() *cobra.Command { |
| 224 | + cmd := &cobra.Command{ |
| 225 | + Use: "tx", |
| 226 | + Short: "Transactions subcommands", |
| 227 | + DisableFlagParsing: false, |
| 228 | + SuggestionsMinimumDistance: 2, |
| 229 | + RunE: client.ValidateCmd, |
| 230 | + } |
| 231 | + |
| 232 | + cmd.AddCommand( |
| 233 | + authcmd.GetSignCommand(), |
| 234 | + authcmd.GetSignBatchCommand(), |
| 235 | + authcmd.GetMultiSignCommand(), |
| 236 | + authcmd.GetMultiSignBatchCmd(), |
| 237 | + authcmd.GetValidateSignaturesCommand(), |
| 238 | + authcmd.GetBroadcastCommand(), |
| 239 | + authcmd.GetEncodeCommand(), |
| 240 | + authcmd.GetDecodeCommand(), |
| 241 | + authcmd.GetAuxToFeeCommand(), |
| 242 | + ) |
| 243 | + |
| 244 | + simapp.ModuleBasics.AddTxCommands(cmd) |
| 245 | + |
| 246 | + return cmd |
| 247 | +} |
| 248 | + |
| 249 | +// newApp creates the application |
| 250 | +func newApp( |
| 251 | + logger log.Logger, |
| 252 | + db dbm.DB, |
| 253 | + traceStore io.Writer, |
| 254 | + appOpts servertypes.AppOptions, |
| 255 | +) servertypes.Application { |
| 256 | + baseappOptions := server.DefaultBaseappOptions(appOpts) |
| 257 | + |
| 258 | + return simapp.NewSimApp( |
| 259 | + logger, db, traceStore, true, |
| 260 | + appOpts, |
| 261 | + baseappOptions..., |
| 262 | + ) |
| 263 | +} |
| 264 | + |
| 265 | +// appExport creates a new simapp (optionally at a given height) and exports state. |
| 266 | +func appExport( |
| 267 | + logger log.Logger, |
| 268 | + db dbm.DB, |
| 269 | + traceStore io.Writer, |
| 270 | + height int64, |
| 271 | + forZeroHeight bool, |
| 272 | + jailAllowedAddrs []string, |
| 273 | + appOpts servertypes.AppOptions, |
| 274 | + modulesToExport []string, |
| 275 | +) (servertypes.ExportedApp, error) { |
| 276 | + var simApp *simapp.SimApp |
| 277 | + |
| 278 | + // this check is necessary as we use the flag in x/upgrade. |
| 279 | + // we can exit more gracefully by checking the flag here. |
| 280 | + homePath, ok := appOpts.Get(flags.FlagHome).(string) |
| 281 | + if !ok || homePath == "" { |
| 282 | + return servertypes.ExportedApp{}, errors.New("application home not set") |
| 283 | + } |
| 284 | + |
| 285 | + viperAppOpts, ok := appOpts.(*viper.Viper) |
| 286 | + if !ok { |
| 287 | + return servertypes.ExportedApp{}, errors.New("appOpts is not viper.Viper") |
| 288 | + } |
| 289 | + |
| 290 | + // overwrite the FlagInvCheckPeriod |
| 291 | + viperAppOpts.Set(server.FlagInvCheckPeriod, 1) |
| 292 | + appOpts = viperAppOpts |
| 293 | + |
| 294 | + if height != -1 { |
| 295 | + simApp = simapp.NewSimApp(logger, db, traceStore, false, appOpts) |
| 296 | + |
| 297 | + if err := simApp.LoadHeight(height); err != nil { |
| 298 | + return servertypes.ExportedApp{}, err |
| 299 | + } |
| 300 | + } else { |
| 301 | + simApp = simapp.NewSimApp(logger, db, traceStore, true, appOpts) |
| 302 | + } |
| 303 | + |
| 304 | + return simApp.ExportAppStateAndValidators(forZeroHeight, jailAllowedAddrs, modulesToExport) |
| 305 | +} |
0 commit comments