/
forceprune.go
205 lines (166 loc) · 5.68 KB
/
forceprune.go
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
package main
// DONTCOVER
import (
"fmt"
"os/exec"
"strconv"
tmdb "github.com/cometbft/cometbft-db"
"github.com/cosmos/cosmos-sdk/client"
"github.com/spf13/cobra"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/opt"
"github.com/syndtr/goleveldb/leveldb/util"
tmstore "github.com/tendermint/tendermint/store"
"github.com/tendermint/tendermint/config"
)
const (
batchMaxSize = 1000
keyValidators = "validatorsKey:"
keyConsensusParams = "consensusParamsKey:"
keyABCIResponses = "abciResponsesKey:"
fullHeight = "full_height"
minHeight = "min_height"
defaultFullHeight = "188000"
defaultMinHeight = "1000"
)
// forceprune gets cmd to convert any bech32 address to an osmo prefix.
func forceprune() *cobra.Command {
cmd := &cobra.Command{
Use: "forceprune",
Short: "Example blackfuryd forceprune -f 188000 -m 1000, which would keep blockchain and state data of last 188000 blocks (approximately 2 weeks) and ABCI responses of last 1000 blocks.",
Long: "Forceprune options prunes and compacts blockstore.db and state.db. One needs to shut down chain before running forceprune. By default it keeps last 188000 blocks (approximately 2 weeks of data) blockstore and state db (validator and consensus information) and 1000 blocks of abci responses from state.db. Everything beyond these heights in blockstore and state.db is pruned. ABCI Responses are stored in index db and so redundant especially if one is running pruned nodes. As a result we are removing ABCI data from state.db aggressively by default. One can override height for blockstore.db and state.db by using -f option and for abci response by using -m option. Example blackfuryd forceprune -f 188000 -m 1000.",
RunE: func(cmd *cobra.Command, args []string) error {
fullHeightFlag, err := cmd.Flags().GetString(fullHeight)
if err != nil {
return err
}
minHeightFlag, err := cmd.Flags().GetString(minHeight)
if err != nil {
return err
}
clientCtx := client.GetClientContextFromCmd(cmd)
conf := config.DefaultConfig()
dbPath := clientCtx.HomeDir + "/" + conf.DBPath
cmdr := exec.Command("blackfuryd", "status")
err = cmdr.Run()
if err == nil {
// continue only if throws errror
return nil
}
fullHeight, err := strconv.ParseInt(fullHeightFlag, 10, 64)
if err != nil {
return err
}
minHeight, err := strconv.ParseInt(minHeightFlag, 10, 64)
if err != nil {
return err
}
startHeight, currentHeight, err := pruneBlockStoreAndGetHeights(dbPath, fullHeight)
if err != nil {
return err
}
err = compactBlockStore(dbPath)
if err != nil {
return err
}
err = forcepruneStateStore(dbPath, startHeight, currentHeight, minHeight, fullHeight)
if err != nil {
return err
}
fmt.Println("Done ...")
return nil
},
}
cmd.Flags().StringP(fullHeight, "f", defaultFullHeight, "Full height to chop to")
cmd.Flags().StringP(minHeight, "m", defaultMinHeight, "Min height for ABCI to chop to")
return cmd
}
// pruneBlockStoreAndGetHeights prunes blockstore and returns the startHeight and currentHeight.
func pruneBlockStoreAndGetHeights(dbPath string, fullHeight int64) (
startHeight int64, currentHeight int64, err error,
) {
opts := opt.Options{
DisableSeeksCompaction: true,
}
dbbs, err := tmdb.NewGoLevelDBWithOpts("blockstore", dbPath, &opts)
if err != nil {
return 0, 0, err
}
defer dbbs.Close()
bs := tmstore.NewBlockStore(dbbs)
startHeight = bs.Base()
currentHeight = bs.Height()
fmt.Println("Pruning Block Store ...")
prunedBlocks, err := bs.PruneBlocks(currentHeight - fullHeight)
if err != nil {
return 0, 0, err
}
fmt.Println("Pruned Block Store ...", prunedBlocks)
// N.B: We duplicate the call to db_bs.Close() on top of
// the call in defer statement above to make sure that the resources
// are properly released and any potential error from Close()
// is handled. Close() should be idempotent so this is acceptable.
if err := dbbs.Close(); err != nil {
return 0, 0, err
}
return startHeight, currentHeight, nil
}
// compactBlockStore compacts block storage.
func compactBlockStore(dbPath string) (err error) {
compactOpts := opt.Options{
DisableSeeksCompaction: true,
}
fmt.Println("Compacting Block Store ...")
db, err := leveldb.OpenFile(dbPath+"/blockstore.db", &compactOpts)
defer func() {
err = db.Close()
}()
if err != nil {
return err
}
return db.CompactRange(*util.BytesPrefix([]byte{}))
}
// forcepruneStateStore prunes and compacts state storage.
func forcepruneStateStore(dbPath string, startHeight, currentHeight, minHeight, fullHeight int64) error {
opts := opt.Options{
DisableSeeksCompaction: true,
}
db, err := leveldb.OpenFile(dbPath+"/state.db", &opts)
if err != nil {
return err
}
defer db.Close()
stateDBKeys := []string{keyValidators, keyConsensusParams, keyABCIResponses}
fmt.Println("Pruning State Store ...")
for i, s := range stateDBKeys {
fmt.Println(i, s)
var retainHeight int64
if s == keyABCIResponses {
retainHeight = currentHeight - minHeight
} else {
retainHeight = currentHeight - fullHeight
}
batch := new(leveldb.Batch)
curBatchSize := uint64(0)
fmt.Println(startHeight, currentHeight, retainHeight)
for c := startHeight; c < retainHeight; c++ {
batch.Delete([]byte(s + strconv.FormatInt(c, 10)))
curBatchSize++
if curBatchSize%batchMaxSize == 0 && curBatchSize > 0 {
err := db.Write(batch, nil)
if err != nil {
return err
}
batch.Reset()
batch = new(leveldb.Batch)
}
}
err := db.Write(batch, nil)
if err != nil {
return err
}
batch.Reset()
}
fmt.Println("Compacting State Store ...")
return db.CompactRange(*util.BytesPrefix([]byte{}))
}