github.com/cosmos/cosmos-sdk@v0.50.10/client/pruning/main.go (about)

     1  package pruning
     2  
     3  import (
     4  	"fmt"
     5  	"path/filepath"
     6  
     7  	dbm "github.com/cosmos/cosmos-db"
     8  	"github.com/spf13/cobra"
     9  	"github.com/spf13/viper"
    10  
    11  	"cosmossdk.io/log"
    12  	pruningtypes "cosmossdk.io/store/pruning/types"
    13  	"cosmossdk.io/store/rootmulti"
    14  
    15  	"github.com/cosmos/cosmos-sdk/client/flags"
    16  	"github.com/cosmos/cosmos-sdk/server"
    17  	servertypes "github.com/cosmos/cosmos-sdk/server/types"
    18  )
    19  
    20  const FlagAppDBBackend = "app-db-backend"
    21  
    22  // Cmd prunes the sdk root multi store history versions based on the pruning options
    23  // specified by command flags.
    24  func Cmd(appCreator servertypes.AppCreator, defaultNodeHome string) *cobra.Command {
    25  	cmd := &cobra.Command{
    26  		Use:   "prune [pruning-method]",
    27  		Short: "Prune app history states by keeping the recent heights and deleting old heights",
    28  		Long: `Prune app history states by keeping the recent heights and deleting old heights.
    29  The pruning option is provided via the 'pruning' argument or alternatively with '--pruning-keep-recent'
    30  
    31  - default: the last 362880 states are kept
    32  - nothing: all historic states will be saved, nothing will be deleted (i.e. archiving node)
    33  - everything: 2 latest states will be kept
    34  - custom: allow pruning options to be manually specified through 'pruning-keep-recent'
    35  
    36  Note: When the --app-db-backend flag is not specified, the default backend type is 'goleveldb'.
    37  Supported app-db-backend types include 'goleveldb', 'rocksdb', 'pebbledb'.`,
    38  		Example: "prune custom --pruning-keep-recent 100 --app-db-backend 'goleveldb'",
    39  		Args:    cobra.RangeArgs(0, 1),
    40  		RunE: func(cmd *cobra.Command, args []string) error {
    41  			// bind flags to the Context's Viper so we can get pruning options.
    42  			vp := viper.New()
    43  			if err := vp.BindPFlags(cmd.Flags()); err != nil {
    44  				return err
    45  			}
    46  
    47  			// use the first argument if present to set the pruning method
    48  			if len(args) > 0 {
    49  				vp.Set(server.FlagPruning, args[0])
    50  			} else {
    51  				vp.Set(server.FlagPruning, pruningtypes.PruningOptionDefault)
    52  			}
    53  			pruningOptions, err := server.GetPruningOptionsFromFlags(vp)
    54  			if err != nil {
    55  				return err
    56  			}
    57  
    58  			cmd.Printf("get pruning options from command flags, strategy: %v, keep-recent: %v\n",
    59  				pruningOptions.Strategy,
    60  				pruningOptions.KeepRecent,
    61  			)
    62  
    63  			home := vp.GetString(flags.FlagHome)
    64  			if home == "" {
    65  				home = defaultNodeHome
    66  			}
    67  
    68  			db, err := openDB(home, server.GetAppDBBackend(vp))
    69  			if err != nil {
    70  				return err
    71  			}
    72  
    73  			logger := log.NewLogger(cmd.OutOrStdout())
    74  			app := appCreator(logger, db, nil, vp)
    75  			cms := app.CommitMultiStore()
    76  
    77  			rootMultiStore, ok := cms.(*rootmulti.Store)
    78  			if !ok {
    79  				return fmt.Errorf("currently only support the pruning of rootmulti.Store type")
    80  			}
    81  			latestHeight := rootmulti.GetLatestVersion(db)
    82  			// valid heights should be greater than 0.
    83  			if latestHeight <= 0 {
    84  				return fmt.Errorf("the database has no valid heights to prune, the latest height: %v", latestHeight)
    85  			}
    86  
    87  			pruningHeight := latestHeight - int64(pruningOptions.KeepRecent)
    88  			cmd.Printf("pruning heights up to %v\n", pruningHeight)
    89  
    90  			err = rootMultiStore.PruneStores(pruningHeight)
    91  			if err != nil {
    92  				return err
    93  			}
    94  
    95  			cmd.Println("successfully pruned the application root multi stores")
    96  			return nil
    97  		},
    98  	}
    99  
   100  	cmd.Flags().String(flags.FlagHome, defaultNodeHome, "The application home directory")
   101  	cmd.Flags().String(FlagAppDBBackend, "", "The type of database for application and snapshots databases")
   102  	cmd.Flags().Uint64(server.FlagPruningKeepRecent, 0, "Number of recent heights to keep on disk (ignored if pruning is not 'custom')")
   103  	cmd.Flags().Uint64(server.FlagPruningInterval, 10,
   104  		`Height interval at which pruned heights are removed from disk (ignored if pruning is not 'custom'), 
   105  		this is not used by this command but kept for compatibility with the complete pruning options`)
   106  
   107  	return cmd
   108  }
   109  
   110  func openDB(rootDir string, backendType dbm.BackendType) (dbm.DB, error) {
   111  	dataDir := filepath.Join(rootDir, "data")
   112  	return dbm.NewDB("application", backendType, dataDir)
   113  }