forked from dymensionxyz/dymint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pruning_test.go
90 lines (78 loc) · 2.53 KB
/
pruning_test.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
package store_test
import (
"testing"
"github.com/dymensionxyz/dymint/store"
"github.com/dymensionxyz/dymint/testutil"
"github.com/dymensionxyz/dymint/types"
"github.com/stretchr/testify/assert"
)
func TestStorePruning(t *testing.T) {
t.Parallel()
pruningHeight := uint64(3)
cases := []struct {
name string
blocks []*types.Block
pruningHeight uint64
expectedBase uint64
expectedHeight uint64
shouldError bool
}{
{"blocks with pruning", []*types.Block{
testutil.GetRandomBlock(1, 0),
testutil.GetRandomBlock(2, 0),
testutil.GetRandomBlock(3, 0),
testutil.GetRandomBlock(4, 0),
testutil.GetRandomBlock(5, 0),
}, pruningHeight, pruningHeight, 5, false},
{"blocks out of order", []*types.Block{
testutil.GetRandomBlock(2, 0),
testutil.GetRandomBlock(3, 0),
testutil.GetRandomBlock(1, 0),
}, pruningHeight, pruningHeight, 3, false},
{"with a gap", []*types.Block{
testutil.GetRandomBlock(1, 0),
testutil.GetRandomBlock(9, 0),
testutil.GetRandomBlock(10, 0),
}, pruningHeight, pruningHeight, 10, false},
{"pruning beyond latest height", []*types.Block{
testutil.GetRandomBlock(1, 0),
testutil.GetRandomBlock(2, 0),
}, pruningHeight, 1, 2, true}, // should error because pruning height > latest height
{"pruning height 0", []*types.Block{
testutil.GetRandomBlock(1, 0),
testutil.GetRandomBlock(2, 0),
testutil.GetRandomBlock(3, 0),
}, 0, 1, 3, true},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
assert := assert.New(t)
bstore := store.New(store.NewDefaultInMemoryKVStore())
assert.Equal(uint64(0), bstore.Height())
for _, block := range c.blocks {
_, err := bstore.SaveBlock(block, &types.Commit{}, nil)
bstore.SetHeight(block.Header.Height)
assert.NoError(err)
}
_, err := bstore.PruneBlocks(int64(c.pruningHeight))
if c.shouldError {
assert.Error(err)
} else {
assert.NoError(err)
assert.Equal(pruningHeight, bstore.Base())
assert.Equal(c.expectedHeight, bstore.Height())
assert.Equal(c.expectedBase, bstore.Base())
// Check if pruned blocks are really removed from the store
for h := uint64(1); h < pruningHeight; h++ {
_, err := bstore.LoadBlock(h)
assert.Error(err, "Block at height %d should be pruned", h)
_, err = bstore.LoadBlockResponses(h)
assert.Error(err, "BlockResponse at height %d should be pruned", h)
_, err = bstore.LoadCommit(h)
assert.Error(err, "Commit at height %d should be pruned", h)
}
}
})
}
}
// TODO: prune twice