|
1 | 1 | package badger |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "os" |
5 | 4 | "testing" |
6 | 5 |
|
7 | 6 | "github.com/stretchr/testify/assert" |
8 | 7 | "github.com/stretchr/testify/require" |
9 | 8 |
|
10 | 9 | "github.com/onflow/flow-go/state" |
11 | 10 | "github.com/onflow/flow-go/storage" |
12 | | - "github.com/onflow/flow-go/storage/operation/pebbleimpl" |
| 11 | + "github.com/onflow/flow-go/storage/operation/dbtest" |
13 | 12 | "github.com/onflow/flow-go/utils/unittest" |
14 | 13 | ) |
15 | 14 |
|
16 | 15 | // TestUnknownSnapshotReference verifies that AtBlockID returns a snapshot that |
17 | | -// returns [state.ErrUnknownSnapshotReference] for all methods when given an unknown block ID. |
| 16 | +// returns state.ErrUnknownSnapshotReference for all methods when given an unknown block ID. |
18 | 17 | func TestUnknownSnapshotReference(t *testing.T) { |
19 | | - // Setup |
20 | | - genesis, err := unittest.ClusterBlock.Genesis() |
21 | | - require.NoError(t, err) |
22 | | - |
23 | | - dbdir := unittest.TempDir(t) |
24 | | - defer os.RemoveAll(dbdir) |
25 | | - |
26 | | - pdb := unittest.PebbleDB(t, dbdir) |
27 | | - defer pdb.Close() |
28 | | - db := pebbleimpl.ToDB(pdb) |
29 | | - |
30 | | - lockManager := storage.NewTestingLockManager() |
31 | | - |
32 | | - root := unittest.RootSnapshotFixture(unittest.IdentityListFixture(5, unittest.WithAllRoles())) |
33 | | - epochCounter := root.Encodable().SealingSegment.LatestProtocolStateEntry().EpochEntry.EpochCounter() |
34 | | - |
35 | | - clusterStateRoot, err := NewStateRoot(genesis, unittest.QuorumCertificateFixture(), epochCounter) |
36 | | - require.NoError(t, err) |
37 | | - clusterState, err := Bootstrap(db, lockManager, clusterStateRoot) |
38 | | - require.NoError(t, err) |
39 | | - |
40 | | - // Test |
41 | | - unknownBlockID := unittest.IdentifierFixture() |
42 | | - snapshot := clusterState.AtBlockID(unknownBlockID) |
43 | | - |
44 | | - // Verify that Collection() returns state.ErrUnknownSnapshotReference |
45 | | - _, err = snapshot.Collection() |
46 | | - assert.ErrorIs(t, err, state.ErrUnknownSnapshotReference) |
47 | | - |
48 | | - // Verify that Head() returns state.ErrUnknownSnapshotReference |
49 | | - _, err = snapshot.Head() |
50 | | - assert.Error(t, err) |
51 | | - assert.ErrorIs(t, err, state.ErrUnknownSnapshotReference) |
52 | | - |
53 | | - // Verify that Pending() returns state.ErrUnknownSnapshotReference |
54 | | - _, err = snapshot.Pending() |
55 | | - assert.Error(t, err) |
56 | | - assert.ErrorIs(t, err, state.ErrUnknownSnapshotReference) |
| 18 | + dbtest.RunWithDB(t, func(t *testing.T, db storage.DB) { |
| 19 | + lockManager := storage.NewTestingLockManager() |
| 20 | + |
| 21 | + // Setup |
| 22 | + genesis, err := unittest.ClusterBlock.Genesis() |
| 23 | + require.NoError(t, err) |
| 24 | + |
| 25 | + root := unittest.RootSnapshotFixture(unittest.IdentityListFixture(5, unittest.WithAllRoles())) |
| 26 | + epochCounter := root.Encodable().SealingSegment.LatestProtocolStateEntry().EpochEntry.EpochCounter() |
| 27 | + |
| 28 | + clusterStateRoot, err := NewStateRoot(genesis, unittest.QuorumCertificateFixture(), epochCounter) |
| 29 | + require.NoError(t, err) |
| 30 | + clusterState, err := Bootstrap(db, lockManager, clusterStateRoot) |
| 31 | + require.NoError(t, err) |
| 32 | + |
| 33 | + // Test |
| 34 | + unknownBlockID := unittest.IdentifierFixture() |
| 35 | + snapshot := clusterState.AtBlockID(unknownBlockID) |
| 36 | + |
| 37 | + // Verify that Collection() returns state.ErrUnknownSnapshotReference |
| 38 | + _, err = snapshot.Collection() |
| 39 | + assert.Error(t, err) |
| 40 | + assert.ErrorIs(t, err, state.ErrUnknownSnapshotReference) |
| 41 | + |
| 42 | + // Verify that Head() returns state.ErrUnknownSnapshotReference |
| 43 | + _, err = snapshot.Head() |
| 44 | + assert.Error(t, err) |
| 45 | + assert.ErrorIs(t, err, state.ErrUnknownSnapshotReference) |
| 46 | + |
| 47 | + // Verify that Pending() returns state.ErrUnknownSnapshotReference |
| 48 | + _, err = snapshot.Pending() |
| 49 | + assert.Error(t, err) |
| 50 | + assert.ErrorIs(t, err, state.ErrUnknownSnapshotReference) |
| 51 | + }) |
57 | 52 | } |
58 | 53 |
|
59 | 54 | // TestValidSnapshotReference verifies that AtBlockID returns a working snapshot |
60 | | -// when given a valid block ID (which in this test is the genesis block ID). |
| 55 | +// when given a valid block ID (the genesis block). |
61 | 56 | func TestValidSnapshotReference(t *testing.T) { |
62 | | - // Setup |
63 | | - genesis, err := unittest.ClusterBlock.Genesis() |
64 | | - require.NoError(t, err) |
65 | | - |
66 | | - dbdir := unittest.TempDir(t) |
67 | | - defer os.RemoveAll(dbdir) |
68 | | - |
69 | | - pdb := unittest.PebbleDB(t, dbdir) |
70 | | - defer pdb.Close() |
71 | | - db := pebbleimpl.ToDB(pdb) |
72 | | - |
73 | | - lockManager := storage.NewTestingLockManager() |
74 | | - |
75 | | - root := unittest.RootSnapshotFixture(unittest.IdentityListFixture(5, unittest.WithAllRoles())) |
76 | | - epochCounter := root.Encodable().SealingSegment.LatestProtocolStateEntry().EpochEntry.EpochCounter() |
77 | | - |
78 | | - clusterStateRoot, err := NewStateRoot(genesis, unittest.QuorumCertificateFixture(), epochCounter) |
79 | | - require.NoError(t, err) |
80 | | - clusterState, err := Bootstrap(db, lockManager, clusterStateRoot) |
81 | | - require.NoError(t, err) |
82 | | - |
83 | | - // Test with valid block ID (genesis block) |
84 | | - snapshot := clusterState.AtBlockID(genesis.ID()) |
85 | | - |
86 | | - // Verify that Collection() works correctly |
87 | | - collection, err := snapshot.Collection() |
88 | | - assert.NoError(t, err) |
89 | | - assert.Equal(t, &genesis.Payload.Collection, collection) |
90 | | - |
91 | | - // Verify that Head() works correctly |
92 | | - head, err := snapshot.Head() |
93 | | - assert.NoError(t, err) |
94 | | - assert.Equal(t, genesis.ToHeader().ID(), head.ID()) |
95 | | - |
96 | | - // Verify that Pending() works correctly (should return empty list for genesis) |
97 | | - pending, err := snapshot.Pending() |
98 | | - assert.NoError(t, err) |
99 | | - assert.Empty(t, pending) |
| 57 | + dbtest.RunWithDB(t, func(t *testing.T, db storage.DB) { |
| 58 | + lockManager := storage.NewTestingLockManager() |
| 59 | + |
| 60 | + // Setup |
| 61 | + genesis, err := unittest.ClusterBlock.Genesis() |
| 62 | + require.NoError(t, err) |
| 63 | + |
| 64 | + root := unittest.RootSnapshotFixture(unittest.IdentityListFixture(5, unittest.WithAllRoles())) |
| 65 | + epochCounter := root.Encodable().SealingSegment.LatestProtocolStateEntry().EpochEntry.EpochCounter() |
| 66 | + |
| 67 | + clusterStateRoot, err := NewStateRoot(genesis, unittest.QuorumCertificateFixture(), epochCounter) |
| 68 | + require.NoError(t, err) |
| 69 | + clusterState, err := Bootstrap(db, lockManager, clusterStateRoot) |
| 70 | + require.NoError(t, err) |
| 71 | + |
| 72 | + // Test with valid block ID (genesis block) |
| 73 | + snapshot := clusterState.AtBlockID(genesis.ID()) |
| 74 | + |
| 75 | + // Verify that Collection() works correctly |
| 76 | + collection, err := snapshot.Collection() |
| 77 | + assert.NoError(t, err) |
| 78 | + assert.Equal(t, &genesis.Payload.Collection, collection) |
| 79 | + |
| 80 | + // Verify that Head() works correctly |
| 81 | + head, err := snapshot.Head() |
| 82 | + assert.NoError(t, err) |
| 83 | + assert.Equal(t, genesis.ToHeader().ID(), head.ID()) |
| 84 | + |
| 85 | + // Verify that Pending() works correctly (should return empty list for genesis) |
| 86 | + pending, err := snapshot.Pending() |
| 87 | + assert.NoError(t, err) |
| 88 | + assert.Empty(t, pending) |
| 89 | + }) |
100 | 90 | } |
0 commit comments