diff --git a/client/pruning/main.go b/client/pruning/main.go deleted file mode 100644 index cc50bc612ca4..000000000000 --- a/client/pruning/main.go +++ /dev/null @@ -1,120 +0,0 @@ -package pruning - -import ( - "errors" - "fmt" - "path/filepath" - - dbm "github.com/cosmos/cosmos-db" - "github.com/spf13/cobra" - "github.com/spf13/viper" - - corestore "cosmossdk.io/core/store" - pruningtypes "cosmossdk.io/store/pruning/types" - "cosmossdk.io/store/rootmulti" - - "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/client/flags" - "github.com/cosmos/cosmos-sdk/server" - servertypes "github.com/cosmos/cosmos-sdk/server/types" - "github.com/cosmos/cosmos-sdk/version" -) - -const FlagAppDBBackend = "app-db-backend" - -// Cmd prunes the sdk root multi store history versions based on the pruning options -// specified by command flags. -func Cmd[T servertypes.Application](appCreator servertypes.AppCreator[T]) *cobra.Command { - cmd := &cobra.Command{ - Use: "prune [pruning-method]", - Short: "Prune app history states by keeping the recent heights and deleting old heights", - Long: `Prune app history states by keeping the recent heights and deleting old heights. -The pruning option is provided via the 'pruning' argument or alternatively with '--pruning-keep-recent' - -- default: the last 362880 states are kept -- nothing: all historic states will be saved, nothing will be deleted (i.e. archiving node) -- everything: 2 latest states will be kept -- custom: allow pruning options to be manually specified through 'pruning-keep-recent' - -Note: When the --app-db-backend flag is not specified, the default backend type is 'goleveldb'. -Supported app-db-backend types include 'goleveldb', 'rocksdb', 'pebbledb'.`, - Example: fmt.Sprintf("%s prune custom --pruning-keep-recent 100 --app-db-backend 'goleveldb'", version.AppName), - Args: cobra.RangeArgs(0, 1), - RunE: func(cmd *cobra.Command, args []string) error { - // bind flags to the Context's Viper so we can get pruning options. - vp := viper.New() - if err := vp.BindPFlags(cmd.Flags()); err != nil { - return err - } - if err := vp.BindPFlags(cmd.PersistentFlags()); err != nil { - return err - } - - // must disable async pruning - vp.Set(server.FlagIAVLSyncPruning, true) - - // use the first argument if present to set the pruning method - if len(args) > 0 { - vp.Set(server.FlagPruning, args[0]) - } else { - vp.Set(server.FlagPruning, pruningtypes.PruningOptionDefault) - } - pruningOptions, err := server.GetPruningOptionsFromFlags(vp) - if err != nil { - return err - } - - cmd.Printf("get pruning options from command flags, strategy: %v, keep-recent: %v\n", - pruningOptions.Strategy, - pruningOptions.KeepRecent, - ) - - home := vp.GetString(flags.FlagHome) - db, err := openDB(home, server.GetAppDBBackend(vp)) - if err != nil { - return err - } - - // in our test, it's important to close db explicitly for pebbledb to write to disk. - defer db.Close() - - logger := client.GetLoggerFromCmd(cmd) - app := appCreator(logger, db, nil, vp) - cms := app.CommitMultiStore() - - rootMultiStore, ok := cms.(*rootmulti.Store) - if !ok { - return errors.New("currently only support the pruning of rootmulti.Store type") - } - latestHeight := rootmulti.GetLatestVersion(db) - // valid heights should be greater than 0. - if latestHeight <= 0 { - return fmt.Errorf("the database has no valid heights to prune, the latest height: %v", latestHeight) - } - - pruningHeight := latestHeight - int64(pruningOptions.KeepRecent) - cmd.Printf("pruning heights up to %v\n", pruningHeight) - - err = rootMultiStore.PruneStores(pruningHeight) - if err != nil { - return err - } - - cmd.Println("successfully pruned the application root multi stores") - return nil - }, - } - - cmd.Flags().String(FlagAppDBBackend, "", "The type of database for application and snapshots databases") - cmd.Flags().Uint64(server.FlagPruningKeepRecent, 0, "Number of recent heights to keep on disk (ignored if pruning is not 'custom')") - cmd.Flags().Uint64(server.FlagPruningInterval, 10, - `Height interval at which pruned heights are removed from disk (ignored if pruning is not 'custom'), - this is not used by this command but kept for compatibility with the complete pruning options`) - - return cmd -} - -func openDB(rootDir string, backendType dbm.BackendType) (corestore.KVStoreWithBatch, error) { - dataDir := filepath.Join(rootDir, "data") - return dbm.NewDB("application", backendType, dataDir) -} diff --git a/x/genutil/client/cli/commands.go b/x/genutil/client/cli/commands.go deleted file mode 100644 index 00042ec69b8d..000000000000 --- a/x/genutil/client/cli/commands.go +++ /dev/null @@ -1,47 +0,0 @@ -package cli - -import ( - "encoding/json" - - "github.com/spf13/cobra" - - banktypes "cosmossdk.io/x/bank/types" - - "github.com/cosmos/cosmos-sdk/client" - servertypes "github.com/cosmos/cosmos-sdk/server/types" - "github.com/cosmos/cosmos-sdk/x/genutil" - genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types" -) - -type genesisMM interface { - DefaultGenesis() map[string]json.RawMessage - ValidateGenesis(genesisData map[string]json.RawMessage) error -} - -// Commands adds core sdk's sub-commands into genesis command. -func Commands(genutilModule genutil.AppModule, genMM genesisMM, appExport servertypes.AppExporter) *cobra.Command { - return CommandsWithCustomMigrationMap(genutilModule, genMM, appExport, MigrationMap) -} - -// CommandsWithCustomMigrationMap adds core sdk's sub-commands into genesis command with custom migration map. -// This custom migration map can be used by the application to add its own migration map. -func CommandsWithCustomMigrationMap(genutilModule genutil.AppModule, genMM genesisMM, appExport servertypes.AppExporter, migrationMap genutiltypes.MigrationMap) *cobra.Command { - cmd := &cobra.Command{ - Use: "genesis", - Short: "Application's genesis-related subcommands", - DisableFlagParsing: false, - SuggestionsMinimumDistance: 2, - RunE: client.ValidateCmd, - } - cmd.AddCommand( - GenTxCmd(genMM, banktypes.GenesisBalancesIterator{}), - MigrateGenesisCmd(migrationMap), - CollectGenTxsCmd(genutilModule.GenTxValidator()), - ValidateGenesisCmd(genMM), - AddGenesisAccountCmd(), - AddBulkGenesisAccountCmd(), - ExportCmd(appExport), - ) - - return cmd -} diff --git a/x/genutil/client/cli/export.go b/x/genutil/client/cli/export.go deleted file mode 100644 index 986b9c1cdb2d..000000000000 --- a/x/genutil/client/cli/export.go +++ /dev/null @@ -1,127 +0,0 @@ -package cli - -import ( - "bytes" - "encoding/json" - "fmt" - "io" - "os" - - "github.com/spf13/cobra" - - "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/client/flags" - "github.com/cosmos/cosmos-sdk/server" - servertypes "github.com/cosmos/cosmos-sdk/server/types" - "github.com/cosmos/cosmos-sdk/version" - genutiltypes "github.com/cosmos/cosmos-sdk/x/genutil/types" -) - -const ( - flagTraceStore = "trace-store" - flagHeight = "height" - flagForZeroHeight = "for-zero-height" - flagJailAllowedAddrs = "jail-allowed-addrs" - flagModulesToExport = "modules-to-export" -) - -// ExportCmd dumps app state to JSON. -func ExportCmd(appExporter servertypes.AppExporter) *cobra.Command { - cmd := &cobra.Command{ - Use: "export", - Short: "Export state to JSON", - Args: cobra.NoArgs, - RunE: func(cmd *cobra.Command, _ []string) error { - config := client.GetConfigFromCmd(cmd) - viper := client.GetViperFromCmd(cmd) - logger := client.GetLoggerFromCmd(cmd) - - if _, err := os.Stat(config.GenesisFile()); os.IsNotExist(err) { - return err - } - - db, err := server.OpenDB(config.RootDir, server.GetAppDBBackend(viper)) - if err != nil { - return err - } - - if appExporter == nil { - if _, err := fmt.Fprintln(cmd.ErrOrStderr(), "WARNING: App exporter not defined. Returning genesis file."); err != nil { - return err - } - - // Open file in read-only mode so we can copy it to stdout. - // It is possible that the genesis file is large, - // so we don't need to read it all into memory - // before we stream it out. - f, err := os.OpenFile(config.GenesisFile(), os.O_RDONLY, 0) - if err != nil { - return err - } - defer f.Close() - - if _, err := io.Copy(cmd.OutOrStdout(), f); err != nil { - return err - } - - return nil - } - - traceWriterFile, _ := cmd.Flags().GetString(flagTraceStore) - traceWriter, cleanup, err := server.SetupTraceWriter(logger, traceWriterFile) //resleak:notresource - if err != nil { - return err - } - defer cleanup() - - height, _ := cmd.Flags().GetInt64(flagHeight) - forZeroHeight, _ := cmd.Flags().GetBool(flagForZeroHeight) - jailAllowedAddrs, _ := cmd.Flags().GetStringSlice(flagJailAllowedAddrs) - modulesToExport, _ := cmd.Flags().GetStringSlice(flagModulesToExport) - outputDocument, _ := cmd.Flags().GetString(flags.FlagOutputDocument) - - exported, err := appExporter(logger, db, traceWriter, height, forZeroHeight, jailAllowedAddrs, viper, modulesToExport) - if err != nil { - return fmt.Errorf("error exporting state: %w", err) - } - - appGenesis, err := genutiltypes.AppGenesisFromFile(config.GenesisFile()) - if err != nil { - return err - } - - // set current binary version - appGenesis.AppName = version.AppName - appGenesis.AppVersion = version.Version - - appGenesis.AppState = exported.AppState - appGenesis.InitialHeight = exported.Height - appGenesis.Consensus = genutiltypes.NewConsensusGenesis(exported.ConsensusParams, exported.Validators) - - out, err := json.Marshal(appGenesis) - if err != nil { - return err - } - - if outputDocument == "" { - // Copy the entire genesis file to stdout. - _, err := io.Copy(cmd.OutOrStdout(), bytes.NewReader(out)) - return err - } - - if err = appGenesis.SaveAs(outputDocument); err != nil { - return err - } - - return nil - }, - } - - cmd.Flags().Int64(flagHeight, -1, "Export state from a particular height (-1 means latest height)") - cmd.Flags().Bool(flagForZeroHeight, false, "Export state to start at height zero (perform preproccessing)") - cmd.Flags().StringSlice(flagJailAllowedAddrs, []string{}, "Comma-separated list of operator addresses of jailed validators to unjail") - cmd.Flags().StringSlice(flagModulesToExport, []string{}, "Comma-separated list of modules to export. If empty, will export all modules") - cmd.Flags().String(flags.FlagOutputDocument, "", "Exported state is written to the given file instead of STDOUT") - - return cmd -} diff --git a/x/genutil/client/cli/gentx.go b/x/genutil/client/cli/gentx.go index c56c772afc7a..4c176a409a89 100644 --- a/x/genutil/client/cli/gentx.go +++ b/x/genutil/client/cli/gentx.go @@ -26,6 +26,11 @@ import ( "github.com/cosmos/cosmos-sdk/x/genutil/types" ) +type genesisMM interface { + DefaultGenesis() map[string]json.RawMessage + ValidateGenesis(genesisData map[string]json.RawMessage) error +} + // GenTxCmd builds the application's gentx command. func GenTxCmd(genMM genesisMM, genBalIterator types.GenesisBalancesIterator) *cobra.Command { ipDefault, _ := server.ExternalIP()