github.com/Finschia/finschia-sdk@v0.49.1/client/pruning/main.go (about) 1 package pruning 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 8 "github.com/Finschia/ostracon/libs/log" 9 "github.com/spf13/cobra" 10 "github.com/spf13/viper" 11 dbm "github.com/tendermint/tm-db" 12 13 "github.com/Finschia/finschia-sdk/client/flags" 14 "github.com/Finschia/finschia-sdk/server" 15 servertypes "github.com/Finschia/finschia-sdk/server/types" 16 "github.com/Finschia/finschia-sdk/store/rootmulti" 17 storetypes "github.com/Finschia/finschia-sdk/store/types" 18 sdk "github.com/Finschia/finschia-sdk/types" 19 ) 20 21 const FlagAppDBBackend = "app-db-backend" 22 23 // PruningCmd prunes the sdk root multi store history versions based on the pruning options 24 // specified by command flags. 25 // 26 //nolint:golint 27 func PruningCmd(appCreator servertypes.AppCreator) *cobra.Command { 28 cmd := &cobra.Command{ 29 Use: "prune", 30 Short: "Prune app history states by keeping the recent heights and deleting old heights", 31 Long: `Prune app history states by keeping the recent heights and deleting old heights. 32 The pruning option is provided via the '--pruning' flag or alternatively with '--pruning-keep-recent' 33 34 For '--pruning' the options are as follows: 35 36 default: the last 362880 states are kept 37 nothing: all historic states will be saved, nothing will be deleted (i.e. archiving node) 38 everything: 2 latest states will be kept 39 custom: allow pruning options to be manually specified through 'pruning-keep-recent'. 40 besides pruning options, database home directory and database backend type should also be specified via flags 41 '--home' and '--app-db-backend'. 42 valid app-db-backend type includes 'goleveldb', 'cleveldb', 'rocksdb', 'boltdb', and 'badgerdb'. 43 `, 44 Example: `prune --home './' --app-db-backend 'goleveldb' --pruning 'custom' --pruning-keep-recent 100 -- 45 pruning-keep-every 10, --pruning-interval 10`, 46 RunE: func(cmd *cobra.Command, _ []string) error { 47 vp := viper.New() 48 49 // Bind flags to the Context's Viper so we can get pruning options. 50 if err := vp.BindPFlags(cmd.Flags()); err != nil { 51 return err 52 } 53 pruningOptions, err := server.GetPruningOptionsFromFlags(vp) 54 if err != nil { 55 return err 56 } 57 fmt.Printf("get pruning options from command flags, keep-recent: %v\n", 58 pruningOptions.KeepRecent, 59 ) 60 61 home := vp.GetString(flags.FlagHome) 62 db, err := openDB(home) 63 if err != nil { 64 return err 65 } 66 67 logger := log.NewOCLogger(log.NewSyncWriter(os.Stdout)) 68 app := appCreator(logger, db, nil, vp) 69 cms := app.CommitMultiStore() 70 71 rootMultiStore, ok := cms.(*rootmulti.Store) 72 if !ok { 73 return fmt.Errorf("currently only support the pruning of rootmulti.Store type") 74 } 75 latestHeight := rootmulti.GetLatestVersion(db) 76 // valid heights should be greater than 0. 77 if latestHeight <= 0 { 78 return fmt.Errorf("the database has no valid heights to prune, the latest height: %v", latestHeight) 79 } 80 81 var pruningHeights []int64 82 for height := int64(1); height < latestHeight; height++ { 83 if height < latestHeight-int64(pruningOptions.KeepRecent) { 84 pruningHeights = append(pruningHeights, height) 85 } 86 } 87 if len(pruningHeights) == 0 { 88 fmt.Printf("no heights to prune\n") 89 return nil 90 } 91 fmt.Printf( 92 "pruning heights start from %v, end at %v\n", 93 pruningHeights[0], 94 pruningHeights[len(pruningHeights)-1], 95 ) 96 97 rootMultiStore.PruneStores(false, pruningHeights) 98 if err != nil { 99 return err 100 } 101 fmt.Printf("successfully pruned the application root multi stores\n") 102 return nil 103 }, 104 } 105 106 cmd.Flags().String(flags.FlagHome, "", "The database home directory") 107 cmd.Flags().String(FlagAppDBBackend, "", "The type of database for application and snapshots databases") 108 cmd.Flags().String(server.FlagPruning, storetypes.PruningOptionDefault, "Pruning strategy (default|nothing|everything|custom)") 109 cmd.Flags().Uint64(server.FlagPruningKeepRecent, 0, "Number of recent heights to keep on disk (ignored if pruning is not 'custom')") 110 cmd.Flags().Uint64(server.FlagPruningKeepEvery, 0, 111 `Offset heights to keep on disk after 'keep-every' (ignored if pruning is not 'custom'), 112 this is not used by this command but kept for compatibility with the complete pruning options`) 113 cmd.Flags().Uint64(server.FlagPruningInterval, 10, 114 `Height interval at which pruned heights are removed from disk (ignored if pruning is not 'custom'), 115 this is not used by this command but kept for compatibility with the complete pruning options`) 116 117 return cmd 118 } 119 120 func openDB(rootDir string) (dbm.DB, error) { 121 dataDir := filepath.Join(rootDir, "data") 122 return sdk.NewLevelDB("application", dataDir) 123 }