-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
302 lines (266 loc) · 6.67 KB
/
main.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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package main
import (
"bytes"
"fmt"
"log"
"os"
"strconv"
"strings"
"sync"
"github.com/syndtr/goleveldb/leveldb"
)
var (
// Prefixes for all the different stores: applicationDB, blockstore, stateDB, txIndexer
//
// The prefix represents which keys SHOULD be kept. Keys without these prefixes are pruned
// if before the pruned height provided.
appDbPrefixes = [][]byte{
[]byte("s/k:application/"),
[]byte("s/k:auth/"),
[]byte("s/k:gov/"),
[]byte("s/k:main/"),
[]byte("s/k:params/"),
[]byte("s/k:pocketcore/"),
[]byte("s/k:pos/"),
}
blockstorePrefixes = [][]byte{
[]byte("C:"),
[]byte("H:"),
[]byte("SC:"),
[]byte("P:"), // P:%v:%v
}
stateDbPrefixes = [][]byte{
[]byte("abciResponsesKey:"),
[]byte("consensusParamsKey:"),
[]byte("validatorsKey:"),
}
txIndexerPrefixes = [][]byte{
[]byte("tx.height/"),
[]byte("tx.recipient/"),
[]byte("tx.signer/"),
}
// State.DB
keyGenesisDoc = []byte("genesisDoc")
keyStateKey = []byte("stateKey")
// Appliation.DB
keyLatest = []byte("s/latest")
)
func main() {
if len(os.Args) == 1 {
fmt.Print(`
USAGE:
pruner <pruneBeforeBlock> <path to data directory> [application,blockstore,state,txindexer]
`)
return
}
// Validate `pruneBeforeBlock` argument
var pruneBeforeBlock = 0
if len(os.Args) > 1 {
var err error
pruneBeforeBlock, err = strconv.Atoi(os.Args[1])
if err != nil || pruneBeforeBlock < 1 {
log.Fatal("Argument 0, pruneBeforeBlock, must be an integer: ", os.Args[1], pruneBeforeBlock)
}
} else {
log.Fatal("Must specify pruneBeforeBlock as argument 0")
}
// Validate `dir` argument
var dir string
if len(os.Args) > 2 {
dir = os.Args[2]
} else {
log.Fatal("Must specify working directory as argument 1")
}
// Prepare `databases` argument
var databases []string
if len(os.Args) > 3 {
databases = strings.Split(os.Args[3], ",")
} else {
databases = []string{"application", "blockstore", "state", "txindexer"}
}
log.Println("Pruning before block:", pruneBeforeBlock)
// Prune each database in parallel and wait for all of them to complete before returning
var wg sync.WaitGroup
for _, db := range databases {
verify := false
if strings.HasSuffix(db, "!") {
verify = true
db = db[:len(db)-1]
}
switch db {
case "application":
wg.Add(1)
go pruneAppDb(pruneBeforeBlock, dir, &wg, verify)
case "blockstore":
wg.Add(1)
go pruneBlockstore(pruneBeforeBlock, dir, &wg, verify)
case "state":
wg.Add(1)
go pruneStateDb(pruneBeforeBlock, dir, &wg, verify)
case "txindexer":
wg.Add(1)
go pruneTxIndexer(dir, &wg, verify)
default:
log.Println("Ignore unknown database:", db, verify)
}
}
wg.Wait()
log.Println("Completed all tasks.")
}
// pruneTxIndexer prunes the txindexer from dir+"/txindexer.db to dir+"/txindexer-new.db"
// and does not prune any key with the prefix in txIndexerPrefixes.
func pruneTxIndexer(dir string, wg *sync.WaitGroup, verify bool) {
srcDb, err := leveldb.OpenFile(dir+"/txindexer.db", nil)
if err != nil {
log.Fatal("Failed to open txindexer" + dir)
}
dstDb, err := leveldb.OpenFile(dir+"/txindexer-new.db", nil)
if err != nil {
log.Fatal("Failed to open new txindexer copy")
}
defer func() {
srcDb.Close()
dstDb.Close()
wg.Done()
}()
if verify {
verifyTxIndexer(dstDb, srcDb)
return
}
it := srcDb.NewIterator(nil, nil)
for it.Next() {
key := it.Key()
// Drop value of TxResult if the key is not in txIndexerPrefixes
var value []byte
for _, prefix := range txIndexerPrefixes {
if bytes.HasPrefix(key, prefix) {
// Value is TxHash. DO NOT prune.
value = it.Value()
break
}
}
dstDb.Put(key, value, nil)
}
it.Release()
log.Println("Done - txindexer.db")
}
// pruneBlockstore prunes the blockstore from dir+"/blockstore.db" to dir+"/blockstore-new.db"
// and does not prune any key with the prefix in blockstorePrefixes.
func pruneBlockstore(
pruneBeforeBlock int,
dir string,
wg *sync.WaitGroup,
verify bool,
) {
dbb, err := leveldb.OpenFile(dir+"/blockstore.db", nil)
if err != nil {
log.Fatal("Failed to open blockstore" + dir)
}
dbn, err := leveldb.OpenFile(dir+"/blockstore-new.db", nil)
if err != nil {
log.Fatal("Failed to open new blockstore copy")
}
defer func() {
dbb.Close()
dbn.Close()
wg.Done()
}()
if verify {
verifyBlockStore(dbn, dbb, pruneBeforeBlock)
return
}
iter := dbb.NewIterator(nil, nil)
for iter.Next() {
key := iter.Key()
value := iter.Value()
var stringKey = string(key)
var inserted = false
for _, prefix := range blockstorePrefixes {
if bytes.HasPrefix([]byte(stringKey), prefix) {
chunks := strings.SplitN(stringKey, ":", 3)
if len(chunks) < 2 {
log.Fatal("Cannot convert ", stringKey)
}
var intKey, err = strconv.Atoi(chunks[1])
if err != nil {
log.Fatal("Cannot convert ", stringKey)
}
pruneBeforeBlockToCheck := pruneBeforeBlock
if strings.HasPrefix(stringKey, "C:") {
// Why decrement here?
// height-1 is set to the integer part of the blockCommit key.
// See store.SaveBlock in tendermint.
pruneBeforeBlockToCheck--
}
if intKey > 1 && intKey < pruneBeforeBlockToCheck {
dbn.Put(key, nil, nil)
} else {
dbn.Put(key, value, nil)
}
inserted = true
}
}
if !inserted {
dbn.Put(key, value, nil)
}
}
iter.Release()
log.Println("Done - blockstore.db")
}
// pruneStateDb prunes the state.db from dir+"/state.db" to dir+"/state-new.db"
// and does not prune any key with the prefix in stateDbPrefixes.
func pruneStateDb(
pruneBeforeBlock int,
dir string,
wg *sync.WaitGroup,
verify bool,
) {
srcDb, err := leveldb.OpenFile(dir+"/state.db", nil)
if err != nil {
log.Fatal("Failed to open state" + dir)
}
dstDb, err := leveldb.OpenFile(dir+"/state-new.db", nil)
if err != nil {
log.Fatal("Failed to open new state copy")
}
defer func() {
srcDb.Close()
dstDb.Close()
wg.Done()
}()
if verify {
verifyStateDb(dstDb, srcDb, pruneBeforeBlock)
return
}
iter := srcDb.NewIterator(nil, nil)
for iter.Next() {
key := iter.Key()
value := iter.Value()
inserted := false
for _, prefix := range stateDbPrefixes {
if bytes.HasPrefix(key, prefix) {
heightBytes := key[len(prefix):]
var height, err = strconv.Atoi(string(heightBytes))
if err != nil {
log.Fatal("Cannot convert ", string(key))
}
if height > 1 && height < pruneBeforeBlock {
dstDb.Put(key, nil, nil)
} else {
dstDb.Put(key, value, nil)
}
inserted = true
break
}
}
if !inserted {
if !bytes.HasPrefix(key, keyGenesisDoc) &&
!bytes.HasPrefix(key, keyStateKey) {
log.Println("Unknown key: ", string(key))
}
dstDb.Put(key, value, nil)
}
}
iter.Release()
log.Println("Done - state.db")
}