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