forked from czarcas7ic/cosmprund
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpruner.go
More file actions
278 lines (232 loc) · 7.67 KB
/
pruner.go
File metadata and controls
278 lines (232 loc) · 7.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package cmd
import (
"context"
"fmt"
"path/filepath"
"github.com/cometbft/cometbft/state"
"github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
authzkeeper "github.com/cosmos/cosmos-sdk/x/authz/keeper"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
packetforwardtypes "github.com/cosmos/ibc-apps/middleware/packet-forward-middleware/v7/packetforward/types"
icqtypes "github.com/cosmos/ibc-apps/modules/async-icq/v7/types"
icahosttypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/host/types"
ibctransfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
ibchost "github.com/cosmos/ibc-go/v7/modules/core/exported"
db "github.com/cometbft/cometbft-db"
tmstore "github.com/cometbft/cometbft/store"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
consensusparamtypes "github.com/cosmos/cosmos-sdk/x/consensus/types"
crisistypes "github.com/cosmos/cosmos-sdk/x/crisis/types"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
evidencetypes "github.com/cosmos/cosmos-sdk/x/evidence/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
paramstypes "github.com/cosmos/cosmos-sdk/x/params/types"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
"github.com/neilotoole/errgroup"
"github.com/spf13/cobra"
"github.com/syndtr/goleveldb/leveldb/opt"
"github.com/binaryholdings/cosmos-pruner/internal/rootmulti"
)
// load db
// load app store and prune
// if immutable tree is not deletable we should import and export current state
func pruneCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "prune [path_to_home]",
Short: "prune data from the application store and block store",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
logger.Info("Starting pruning...")
ctx := cmd.Context()
errs, _ := errgroup.WithContext(ctx)
var err error
// Tendermint pruning (blockstore.db, state.db)
if tendermint {
errs.Go(func() error {
if err = pruneTMData(args[0]); err != nil {
return err
}
return nil
})
}
if cosmosSdk {
err = pruneAppState(args[0])
if err != nil {
return err
}
return nil
}
return errs.Wait()
},
}
return cmd
}
func pruneAppState(home string) error {
// this has the potential to expand size, should just use state sync
// dbType := db.BackendType(backend)
dbDir := rootify(dataDir, home)
o := opt.Options{
DisableSeeksCompaction: true,
}
// Get BlockStore
appDB, err := db.NewGoLevelDBWithOpts("application", dbDir, &o)
if err != nil {
return err
}
//TODO: need to get all versions in the store, setting randomly is too slow
logger.Info("pruning application state")
// only mount keys from core sdk
// todo allow for other keys to be mounted
keys := types.NewKVStoreKeys(
authtypes.StoreKey, banktypes.StoreKey, authzkeeper.StoreKey, stakingtypes.StoreKey, distrtypes.StoreKey, slashingtypes.StoreKey, ibchost.StoreKey,
icahosttypes.StoreKey,
icqtypes.StoreKey,
evidencetypes.StoreKey, minttypes.StoreKey, govtypes.StoreKey, ibctransfertypes.StoreKey,
packetforwardtypes.StoreKey,
paramstypes.StoreKey, consensusparamtypes.StoreKey, capabilitytypes.StoreKey, crisistypes.StoreKey, upgradetypes.StoreKey,
// feegrant.StoreKey,
)
if app == "osmosis" {
osmoKeys := types.NewKVStoreKeys(
"downtimedetector",
"hooks-for-ibc",
"lockup", //lockuptypes.StoreKey,
"concentratedliquidity",
"gamm", // gammtypes.StoreKey,
"cosmwasmpool",
"poolmanager",
"twap",
"epochs", // epochstypes.StoreKey,
"protorev",
"txfees", // txfeestypes.StoreKey,
"incentives", // incentivestypes.StoreKey,
"poolincentives", //poolincentivestypes.StoreKey,
"tokenfactory", //tokenfactorytypes.StoreKey,
"valsetpref",
"superfluid", // superfluidtypes.StoreKey,
"wasm", // wasm.StoreKey,
//"rate-limited-ibc", // there is no store registered for this module
)
for key, value := range osmoKeys {
keys[key] = value
}
}
// TODO: cleanup app state
appStore := rootmulti.NewStore(appDB, logger)
// Configure IAVL fast node
// Default (false): fast node enabled for queries
// With flag (true): fast node disabled for faster pruning
appStore.SetIAVLDisableFastNode(disableFastNode)
if disableFastNode {
logger.Info("IAVL fast node disabled (faster pruning mode)")
} else {
logger.Info("IAVL fast node enabled (default mode)")
}
for _, value := range keys {
appStore.MountStoreWithDB(value, storetypes.StoreTypeIAVL, nil)
}
err = appStore.LoadLatestVersion()
if err != nil {
return err
}
latestHeight := rootmulti.GetLatestVersion(appDB)
// 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)
}
// var pruningHeights []int64
// for height := int64(1); height < latestHeight; height++ {
// if height < latestHeight-int64(versions) {
// pruningHeights = append(pruningHeights, height)
// }
// }
// Prune the last X versions
// This is the most efficient way to prune the application state
// as it only needs to delete the last X versions
pruneHeight := latestHeight - int64(versions)
if pruneHeight <= 0 {
logger.Error("no heights to prune")
return nil
}
pruningHeights := []int64{pruneHeight}
//pruningHeight := []int64{latestHeight - int64(versions)}
if err = appStore.PruneStores(false, pruningHeights); err != nil {
return err
}
logger.Info("pruning application state complete")
logger.Info("compacting application state")
if err := appDB.Compact(nil, nil); err != nil {
return err
}
logger.Info("compacting application state complete")
//create a new app store
return nil
}
// pruneTMData prunes the tendermint blocks and state based on the amount of blocks to keep
func pruneTMData(home string) error {
dbDir := rootify(dataDir, home)
o := opt.Options{
DisableSeeksCompaction: true,
}
// Get BlockStore
blockStoreDB, err := db.NewGoLevelDBWithOpts("blockstore", dbDir, &o)
if err != nil {
return err
}
blockStore := tmstore.NewBlockStore(blockStoreDB)
base := blockStore.Base()
pruneHeight := blockStore.Height() - int64(blocks)
// Check if there's anything to prune
if pruneHeight <= base {
logger.Error("no blocks to prune", "base", base, "target", pruneHeight)
return nil
} // Get StateStore
errs, _ := errgroup.WithContext(context.Background())
errs.Go(func() error {
logger.Info("pruning block store")
// prune block store
blocks, err = blockStore.PruneBlocks(pruneHeight)
if err != nil {
return err
}
logger.Info("pruning block store complete")
logger.Info("compacting block store")
if err := blockStoreDB.Compact(nil, nil); err != nil {
return err
}
logger.Info("compacting block store complete")
return nil
})
logger.Info("pruning state store")
stateDB, err := db.NewGoLevelDBWithOpts("state", dbDir, &o)
if err != nil {
return err
}
stateStore := state.NewStore(stateDB, state.StoreOptions{
DiscardABCIResponses: true,
})
// prune state store
err = stateStore.PruneStates(base, pruneHeight)
if err != nil {
return err
}
logger.Info("pruning state store complete")
logger.Info("compacting state store")
if err := stateDB.Compact(nil, nil); err != nil {
return err
}
logger.Info("compacting state store complete")
return nil
}
// Utils
func rootify(path, root string) string {
if filepath.IsAbs(path) {
return path
}
return filepath.Join(root, path)
}