-
Notifications
You must be signed in to change notification settings - Fork 67
/
pruning.go
75 lines (66 loc) · 1.68 KB
/
pruning.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
package store
import "fmt"
// PruneBlocks removes block up to (but not including) a height. It returns number of blocks pruned.
func (s *DefaultStore) PruneBlocks(heightInt int64) (uint64, error) {
if heightInt <= 0 {
return 0, fmt.Errorf("height must be greater than 0")
}
height := uint64(heightInt)
if height > s.Height() {
return 0, fmt.Errorf("cannot prune beyond the latest height %v", s.height)
}
base := s.Base()
if height < base {
return 0, fmt.Errorf("cannot prune to height %v, it is lower than base height %v",
height, base)
}
pruned := uint64(0)
batch := s.db.NewBatch()
defer batch.Discard()
flush := func(batch Batch, base uint64) error {
err := batch.Commit()
if err != nil {
return fmt.Errorf("prune up to height %v: %w", base, err)
}
if ok := s.SetBase(base); !ok {
return fmt.Errorf("set base height: %v", base)
}
return nil
}
for h := base; h < height; h++ {
hash, err := s.loadHashFromIndex(h)
if err != nil {
continue
}
if err := batch.Delete(getBlockKey(hash)); err != nil {
return 0, err
}
if err := batch.Delete(getCommitKey(hash)); err != nil {
return 0, err
}
if err := batch.Delete(getIndexKey(h)); err != nil {
return 0, err
}
if err := batch.Delete(getResponsesKey(h)); err != nil {
return 0, err
}
if err := batch.Delete(getValidatorsKey(h)); err != nil {
return 0, err
}
pruned++
// flush every 1000 blocks to avoid batches becoming too large
if pruned%1000 == 0 && pruned > 0 {
err := flush(batch, h)
if err != nil {
return 0, err
}
batch = s.db.NewBatch()
defer batch.Discard()
}
}
err := flush(batch, height)
if err != nil {
return 0, err
}
return pruned, nil
}