|
| 1 | +package store |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + |
| 8 | + "cosmossdk.io/log" |
| 9 | + |
| 10 | + "github.com/KYVENetwork/supervysor/cmd/supervysor/commands/helpers" |
| 11 | + "github.com/syndtr/goleveldb/leveldb/opt" |
| 12 | + |
| 13 | + "github.com/tendermint/tendermint/state" |
| 14 | + tmStore "github.com/tendermint/tendermint/store" |
| 15 | + db "github.com/tendermint/tm-db" |
| 16 | +) |
| 17 | + |
| 18 | +func PruneGoLevelDB(home string, untilHeight int64, statePruning bool, logger log.Logger) error { |
| 19 | + o := opt.Options{ |
| 20 | + DisableSeeksCompaction: true, |
| 21 | + } |
| 22 | + |
| 23 | + // Get BlockStore |
| 24 | + blockStoreDB, err := db.NewGoLevelDBWithOpts("blockstore", filepath.Join(home, "data"), &o) |
| 25 | + if err != nil { |
| 26 | + return fmt.Errorf("failed to create blockStoreDB: %w", err) |
| 27 | + } |
| 28 | + blockStore := tmStore.NewBlockStore(blockStoreDB) |
| 29 | + |
| 30 | + // Get StateStore |
| 31 | + stateDB, err := db.NewGoLevelDBWithOpts("state", filepath.Join(home, "data"), &o) |
| 32 | + if err != nil { |
| 33 | + return fmt.Errorf("failed to create stateStoreDB: %w", err) |
| 34 | + } |
| 35 | + stateStore := state.NewStore(stateDB) |
| 36 | + |
| 37 | + base := blockStore.Base() |
| 38 | + |
| 39 | + logger.Info("blockstore base", "base", base) |
| 40 | + |
| 41 | + if untilHeight < base { |
| 42 | + logger.Error("Error: base height is higher than prune height", "base height", base, "until height", untilHeight) |
| 43 | + os.Exit(0) |
| 44 | + } |
| 45 | + |
| 46 | + blocks, err := blockStore.PruneBlocks(untilHeight) |
| 47 | + if err != nil { |
| 48 | + logger.Error(err.Error()) |
| 49 | + os.Exit(0) |
| 50 | + } |
| 51 | + |
| 52 | + if err = blockStoreDB.ForceCompact(nil, nil); err != nil { |
| 53 | + logger.Error(err.Error()) |
| 54 | + os.Exit(0) |
| 55 | + } |
| 56 | + |
| 57 | + if statePruning { |
| 58 | + err = stateStore.PruneStates(base, untilHeight) |
| 59 | + if err != nil { |
| 60 | + logger.Error(err.Error()) |
| 61 | + os.Exit(0) |
| 62 | + } |
| 63 | + |
| 64 | + if err = stateDB.ForceCompact(nil, nil); err != nil { |
| 65 | + logger.Error(err.Error()) |
| 66 | + os.Exit(0) |
| 67 | + } |
| 68 | + |
| 69 | + logger.Info(fmt.Sprintf("Pruned %d blocks and the state until %d, new base height is %d", blocks, untilHeight, blockStore.Base())) |
| 70 | + } else { |
| 71 | + logger.Info(fmt.Sprintf("Pruned %d blocks until %d, new base height is %d", blocks, untilHeight, blockStore.Base())) |
| 72 | + } |
| 73 | + |
| 74 | + return nil |
| 75 | +} |
| 76 | + |
| 77 | +func PruneAnyDB(home string, untilHeight int64, statePruning bool, logger log.Logger) error { |
| 78 | + config, err := helpers.LoadConfig(home) |
| 79 | + if err != nil { |
| 80 | + return fmt.Errorf("failed to load config: %w", err) |
| 81 | + } |
| 82 | + |
| 83 | + blockStoreDB, blockStore, err := GetBlockstoreDBs(config) |
| 84 | + defer func(blockStoreDB db.DB) { |
| 85 | + err = blockStoreDB.Close() |
| 86 | + if err != nil { |
| 87 | + logger.Error(err.Error()) |
| 88 | + os.Exit(0) |
| 89 | + } |
| 90 | + }(blockStoreDB) |
| 91 | + |
| 92 | + if err != nil { |
| 93 | + panic(fmt.Errorf("failed to load blockstore db: %w", err)) |
| 94 | + } |
| 95 | + |
| 96 | + base := blockStore.Base() |
| 97 | + |
| 98 | + logger.Info("blockstore base", "base", base) |
| 99 | + |
| 100 | + if untilHeight < base { |
| 101 | + logger.Error("Error: base height is higher than prune height", "base height", base, "until height", untilHeight) |
| 102 | + os.Exit(0) |
| 103 | + } |
| 104 | + |
| 105 | + blocks, err := blockStore.PruneBlocks(untilHeight) |
| 106 | + if err != nil { |
| 107 | + logger.Error(err.Error()) |
| 108 | + os.Exit(0) |
| 109 | + } |
| 110 | + |
| 111 | + if statePruning { |
| 112 | + stateStoreDB, stateStore, err := GetStateDBs(config) |
| 113 | + defer func(stateStoreDB db.DB) { |
| 114 | + err = stateStoreDB.Close() |
| 115 | + if err != nil { |
| 116 | + logger.Error(err.Error()) |
| 117 | + os.Exit(0) |
| 118 | + } |
| 119 | + }(stateStoreDB) |
| 120 | + |
| 121 | + if err = stateStore.PruneStates(base, untilHeight); err != nil { |
| 122 | + logger.Error(err.Error()) |
| 123 | + os.Exit(0) |
| 124 | + } |
| 125 | + |
| 126 | + logger.Info(fmt.Sprintf("Pruned %d blocks and the state until %d, new base height is %d", blocks, untilHeight, blockStore.Base())) |
| 127 | + } else { |
| 128 | + logger.Info(fmt.Sprintf("Pruned %d blocks until %d, new base height is %d", blocks, untilHeight, blockStore.Base())) |
| 129 | + } |
| 130 | + |
| 131 | + return nil |
| 132 | +} |
0 commit comments