-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathrange_test.go
More file actions
139 lines (120 loc) · 4.06 KB
/
Copy pathrange_test.go
File metadata and controls
139 lines (120 loc) · 4.06 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
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
package transactions
import (
"fmt"
"testing"
"github.com/jordanschalm/lockctx"
"github.com/stretchr/testify/require"
"github.com/onflow/flow-go/cmd/util/cmd/common"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/state/protocol/mock"
"github.com/onflow/flow-go/storage"
"github.com/onflow/flow-go/storage/operation/dbtest"
"github.com/onflow/flow-go/utils/unittest"
)
func TestFindBlockTransactions(t *testing.T) {
dbtest.RunWithDB(t, func(t *testing.T, db storage.DB) {
lockManager := storage.NewTestingLockManager()
// prepare two blocks
// block 1 has 2 collections
// block 2 has 1 collection
col1 := unittest.ClusterPayloadFixture(1)
col2 := unittest.ClusterPayloadFixture(2)
col3 := unittest.ClusterPayloadFixture(3)
b1 := unittest.BlockFixture(
unittest.Block.WithHeight(4),
unittest.Block.WithPayload(
flow.Payload{
Guarantees: []*flow.CollectionGuarantee{
&flow.CollectionGuarantee{
CollectionID: col1.Collection.ID(),
ReferenceBlockID: col1.ReferenceBlockID,
},
&flow.CollectionGuarantee{
CollectionID: col2.Collection.ID(),
ReferenceBlockID: col2.ReferenceBlockID,
},
},
ProtocolStateID: unittest.IdentifierFixture(),
},
),
)
b2 := unittest.BlockFixture(
unittest.Block.WithHeight(5),
unittest.Block.WithPayload(
flow.Payload{
Guarantees: []*flow.CollectionGuarantee{
&flow.CollectionGuarantee{
CollectionID: col3.Collection.ID(),
ReferenceBlockID: col3.ReferenceBlockID,
},
},
ProtocolStateID: unittest.IdentifierFixture(),
},
),
)
// prepare dependencies
storages, err := common.InitStorages(db, b1.ChainID)
require.NoError(t, err)
payloads, collections := storages.Payloads, storages.Collections
snap4 := &mock.Snapshot{}
snap4.On("Head").Return(b1.ToHeader(), nil)
snap5 := &mock.Snapshot{}
snap5.On("Head").Return(b2.ToHeader(), nil)
state := &mock.State{}
state.On("AtHeight", uint64(4)).Return(snap4, nil)
state.On("AtHeight", uint64(5)).Return(snap5, nil)
// store into database
p1 := unittest.ProposalFromBlock(b1)
p2 := unittest.ProposalFromBlock(b2)
err = unittest.WithLock(t, lockManager, storage.LockInsertBlock, func(lctx lockctx.Context) error {
return db.WithReaderBatchWriter(func(rw storage.ReaderBatchWriter) error {
err := storages.Blocks.BatchStore(lctx, rw, p1)
if err != nil {
return err
}
return storages.Blocks.BatchStore(lctx, rw, p2)
})
})
require.NoError(t, err)
_, err = collections.Store(&col1.Collection)
require.NoError(t, err)
_, err = collections.Store(&col2.Collection)
require.NoError(t, err)
_, err = collections.Store(&col3.Collection)
require.NoError(t, err)
f := &Finder{
State: state,
Payloads: payloads,
Collections: collections,
}
// fetch from database
fetched, err := f.GetByHeightRange(4, 5)
require.NoError(t, err)
// check fetched correct data
require.Len(t, fetched, 2)
require.Len(t, fetched[0].Collections, 2)
require.Len(t, fetched[1].Collections, 1)
require.Len(t, fetched[0].Collections[0].Transactions, 1)
require.Len(t, fetched[0].Collections[1].Transactions, 2)
require.Len(t, fetched[1].Collections[0].Transactions, 3)
require.Equal(t,
fetched[0].Collections[0].Transactions[0].TxID,
col1.Collection.Transactions[0].ID().String(),
)
// unhappy path: endHeight is lower than startHeight
_, err = f.GetByHeightRange(5, 4)
require.Error(t, err)
require.Contains(t, fmt.Sprintf("%v", err), "must be smaller")
// unhapp path: range not exist
snapNotFound := &mock.Snapshot{}
snapNotFound.On("Head").Return(nil, storage.ErrNotFound)
state.On("AtHeight", uint64(99998)).Return(snapNotFound, nil)
_, err = f.GetByHeightRange(99998, 99999)
require.Error(t, err)
require.Contains(t, fmt.Sprintf("%v", err), "could not find header by height 99998")
// unhapp path: must not start from 0
_, err = f.GetByHeightRange(0, 3)
require.Error(t, err)
require.Contains(t, fmt.Sprintf("%v", err), "start-height must not be 0")
})
}