-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathstore_test.go
More file actions
99 lines (80 loc) · 2.46 KB
/
store_test.go
File metadata and controls
99 lines (80 loc) · 2.46 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
package evm
import (
"context"
"encoding/binary"
"testing"
ds "github.com/ipfs/go-datastore"
dssync "github.com/ipfs/go-datastore/sync"
"github.com/stretchr/testify/require"
)
// newTestDatastore creates an in-memory datastore for testing.
func newTestDatastore(t *testing.T) ds.Batching {
t.Helper()
// Wrap the in-memory MapDatastore to satisfy the Batching interface.
return dssync.MutexWrap(ds.NewMapDatastore())
}
func TestPruneExecMeta_PrunesUpToTargetHeight(t *testing.T) {
t.Parallel()
ctx := context.Background()
db := newTestDatastore(t)
store := NewEVMStore(db)
// Seed ExecMeta entries at heights 1..5
for h := uint64(1); h <= 5; h++ {
meta := &ExecMeta{Height: h}
require.NoError(t, store.SaveExecMeta(ctx, meta))
}
// Sanity: all heights should be present
for h := uint64(1); h <= 5; h++ {
meta, err := store.GetExecMeta(ctx, h)
require.NoError(t, err)
require.NotNil(t, meta)
require.Equal(t, h, meta.Height)
}
// Prune up to height 3
require.NoError(t, store.PruneExecMeta(ctx, 3))
// Heights 1..3 should be gone
for h := uint64(1); h <= 3; h++ {
meta, err := store.GetExecMeta(ctx, h)
require.NoError(t, err)
require.Nil(t, meta)
}
// Heights 4..5 should remain
for h := uint64(4); h <= 5; h++ {
meta, err := store.GetExecMeta(ctx, h)
require.NoError(t, err)
require.NotNil(t, meta)
}
// Re-pruning with the same height should be a no-op
require.NoError(t, store.PruneExecMeta(ctx, 3))
}
func TestPruneExecMeta_TracksLastPrunedHeight(t *testing.T) {
t.Parallel()
ctx := context.Background()
db := newTestDatastore(t)
store := NewEVMStore(db)
// Seed ExecMeta entries at heights 1..5
for h := uint64(1); h <= 5; h++ {
meta := &ExecMeta{Height: h}
require.NoError(t, store.SaveExecMeta(ctx, meta))
}
// First prune up to 2
require.NoError(t, store.PruneExecMeta(ctx, 2))
// Then prune up to 4; heights 3..4 should be deleted in this run
require.NoError(t, store.PruneExecMeta(ctx, 4))
// Verify all heights 1..4 are gone, 5 remains
for h := uint64(1); h <= 4; h++ {
meta, err := store.GetExecMeta(ctx, h)
require.NoError(t, err)
require.Nil(t, meta)
}
meta, err := store.GetExecMeta(ctx, 5)
require.NoError(t, err)
require.NotNil(t, meta)
require.Equal(t, uint64(5), meta.Height)
// Ensure last-pruned marker is set to 4
raw, err := db.Get(ctx, ds.NewKey(lastPrunedExecMetaKey))
require.NoError(t, err)
require.Len(t, raw, 8)
last := binary.BigEndian.Uint64(raw)
require.Equal(t, uint64(4), last)
}