-
Notifications
You must be signed in to change notification settings - Fork 189
/
Copy pathcore_test.go
352 lines (279 loc) · 12.2 KB
/
core_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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package ingestion
import (
"testing"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/onflow/flow-go/engine"
"github.com/onflow/flow-go/model/flow"
mockmempool "github.com/onflow/flow-go/module/mempool/mock"
"github.com/onflow/flow-go/module/metrics"
"github.com/onflow/flow-go/module/signature"
"github.com/onflow/flow-go/module/trace"
"github.com/onflow/flow-go/state/cluster"
"github.com/onflow/flow-go/state/protocol"
mockprotocol "github.com/onflow/flow-go/state/protocol/mock"
mockstorage "github.com/onflow/flow-go/storage/mock"
"github.com/onflow/flow-go/utils/unittest"
)
func TestIngestionCore(t *testing.T) {
suite.Run(t, new(IngestionCoreSuite))
}
type IngestionCoreSuite struct {
suite.Suite
accessID flow.Identifier
collID flow.Identifier
conID flow.Identifier
execID flow.Identifier
verifID flow.Identifier
head *flow.Header
finalIdentities flow.IdentityList // identities at finalized state
refIdentities flow.IdentityList // identities at reference block state
epochCounter uint64 // epoch for the cluster originating the guarantee
clusterMembers flow.IdentityList // members of the cluster originating the guarantee
clusterID flow.ChainID // chain ID of the cluster originating the guarantee
final *mockprotocol.Snapshot // finalized state snapshot
ref *mockprotocol.Snapshot // state snapshot w.r.t. reference block
query *mockprotocol.EpochQuery
epoch *mockprotocol.CommittedEpoch
headers *mockstorage.Headers
pool *mockmempool.Guarantees
core *Core
}
func (suite *IngestionCoreSuite) SetupTest() {
head := unittest.BlockHeaderFixture()
head.Height = 2 * flow.DefaultTransactionExpiry
access := unittest.IdentityFixture(unittest.WithRole(flow.RoleAccess))
con := unittest.IdentityFixture(unittest.WithRole(flow.RoleConsensus))
coll := unittest.IdentityFixture(unittest.WithRole(flow.RoleCollection))
exec := unittest.IdentityFixture(unittest.WithRole(flow.RoleExecution))
verif := unittest.IdentityFixture(unittest.WithRole(flow.RoleVerification))
suite.accessID = access.NodeID
suite.conID = con.NodeID
suite.collID = coll.NodeID
suite.execID = exec.NodeID
suite.verifID = verif.NodeID
suite.epochCounter = 1
suite.clusterMembers = flow.IdentityList{coll}
suite.clusterID = cluster.CanonicalClusterID(suite.epochCounter, suite.clusterMembers.NodeIDs())
identities := flow.IdentityList{access, con, coll, exec, verif}
suite.finalIdentities = identities.Copy()
suite.refIdentities = identities.Copy()
metrics := metrics.NewNoopCollector()
tracer := trace.NewNoopTracer()
state := &mockprotocol.State{}
final := &mockprotocol.Snapshot{}
ref := &mockprotocol.Snapshot{}
suite.query = &mockprotocol.EpochQuery{}
suite.epoch = &mockprotocol.CommittedEpoch{}
headers := &mockstorage.Headers{}
pool := &mockmempool.Guarantees{}
cluster := &mockprotocol.Cluster{}
// this state basically works like a normal protocol state
// returning everything correctly, using the created header
// as head of the protocol state
state.On("Final").Return(final)
final.On("Head").Return(head, nil)
final.On("Identity", mock.Anything).Return(
func(nodeID flow.Identifier) *flow.Identity {
identity, _ := suite.finalIdentities.ByNodeID(nodeID)
return identity
},
func(nodeID flow.Identifier) error {
_, ok := suite.finalIdentities.ByNodeID(nodeID)
if !ok {
return protocol.IdentityNotFoundError{NodeID: nodeID}
}
return nil
},
)
final.On("Identities", mock.Anything).Return(
func(selector flow.IdentityFilter[flow.Identity]) flow.IdentityList {
return suite.finalIdentities.Filter(selector)
},
nil,
)
ref.On("Epochs").Return(suite.query)
suite.query.On("Current").Return(suite.epoch, nil)
cluster.On("Members").Return(suite.clusterMembers.ToSkeleton())
suite.epoch.On("ClusterByChainID", mock.Anything).Return(
func(chainID flow.ChainID) protocol.Cluster {
if chainID == suite.clusterID {
return cluster
}
return nil
},
func(chainID flow.ChainID) error {
if chainID == suite.clusterID {
return nil
}
return protocol.ErrClusterNotFound
})
state.On("AtBlockID", mock.Anything).Return(ref)
ref.On("Identity", mock.Anything).Return(
func(nodeID flow.Identifier) *flow.Identity {
identity, _ := suite.refIdentities.ByNodeID(nodeID)
return identity
},
func(nodeID flow.Identifier) error {
_, ok := suite.refIdentities.ByNodeID(nodeID)
if !ok {
return protocol.IdentityNotFoundError{NodeID: nodeID}
}
return nil
},
)
// we need to return the head as it's also used as reference block
headers.On("ByBlockID", head.ID()).Return(head, nil)
// only used for metrics, nobody cares
pool.On("Size").Return(uint(0))
ingest := NewCore(unittest.Logger(), tracer, metrics, state, headers, pool)
suite.head = head
suite.final = final
suite.ref = ref
suite.headers = headers
suite.pool = pool
suite.core = ingest
}
func (suite *IngestionCoreSuite) TestOnGuaranteeNewFromCollection() {
guarantee := suite.validGuarantee()
// the guarantee is not part of the memory pool yet
suite.pool.On("Has", guarantee.ID()).Return(false)
suite.pool.On("Add", guarantee).Return(true)
// submit the guarantee as if it was sent by a collection node
err := suite.core.OnGuarantee(suite.collID, guarantee)
suite.Assert().NoError(err, "should not error on new guarantee from collection node")
// check that the guarantee has been added to the mempool
suite.pool.AssertCalled(suite.T(), "Add", guarantee)
}
func (suite *IngestionCoreSuite) TestOnGuaranteeOld() {
guarantee := suite.validGuarantee()
// the guarantee is part of the memory pool
suite.pool.On("Has", guarantee.ID()).Return(true)
suite.pool.On("Add", guarantee).Return(true)
// submit the guarantee as if it was sent by a collection node
err := suite.core.OnGuarantee(suite.collID, guarantee)
suite.Assert().NoError(err, "should not error on old guarantee")
// check that the guarantee has _not_ been added to the mempool
suite.pool.AssertNotCalled(suite.T(), "Add", guarantee)
}
func (suite *IngestionCoreSuite) TestOnGuaranteeNotAdded() {
guarantee := suite.validGuarantee()
// the guarantee is not already part of the memory pool
suite.pool.On("Has", guarantee.ID()).Return(false)
suite.pool.On("Add", guarantee).Return(false)
// submit the guarantee as if it was sent by a collection node
err := suite.core.OnGuarantee(suite.collID, guarantee)
suite.Assert().NoError(err, "should not error when guarantee was already added")
// check that the guarantee has been added to the mempool
suite.pool.AssertCalled(suite.T(), "Add", guarantee)
}
// TestOnGuaranteeNoGuarantors tests that a collection without any guarantors is rejected.
// We expect an engine.InvalidInputError.
func (suite *IngestionCoreSuite) TestOnGuaranteeNoGuarantors() {
// create a guarantee without any signers
guarantee := suite.validGuarantee()
guarantee.SignerIndices = nil
// the guarantee is not part of the memory pool
suite.pool.On("Has", guarantee.ID()).Return(false)
suite.pool.On("Add", guarantee).Return(true)
// submit the guarantee as if it was sent by a consensus node
err := suite.core.OnGuarantee(suite.collID, guarantee)
suite.Assert().Error(err, "should error with missing guarantor")
suite.Assert().True(engine.IsInvalidInputError(err))
// check that the guarantee has _not_ been added to the mempool
suite.pool.AssertNotCalled(suite.T(), "Add", guarantee)
}
func (suite *IngestionCoreSuite) TestOnGuaranteeExpired() {
// create an alternative block
header := unittest.BlockHeaderFixture()
header.Height = suite.head.Height - flow.DefaultTransactionExpiry - 1
suite.headers.On("ByBlockID", header.ID()).Return(header, nil)
// create a guarantee signed by the collection node and referencing the
// current head of the protocol state
guarantee := suite.validGuarantee()
guarantee.ReferenceBlockID = header.ID()
// the guarantee is not part of the memory pool
suite.pool.On("Has", guarantee.ID()).Return(false)
suite.pool.On("Add", guarantee).Return(true)
// submit the guarantee as if it was sent by a consensus node
err := suite.core.OnGuarantee(suite.collID, guarantee)
suite.Assert().Error(err, "should error with expired collection")
suite.Assert().True(engine.IsOutdatedInputError(err))
}
// TestOnGuaranteeReferenceBlockFromWrongEpoch validates that guarantees which contain a ChainID
// that is inconsistent with the reference block (ie. the ChainID either refers to a non-existent
// cluster, or a cluster for a different epoch) should be considered invalid inputs.
func (suite *IngestionCoreSuite) TestOnGuaranteeReferenceBlockFromWrongEpoch() {
// create a guarantee from a cluster in a different epoch
guarantee := suite.validGuarantee()
guarantee.ChainID = cluster.CanonicalClusterID(suite.epochCounter+1, suite.clusterMembers.NodeIDs())
// the guarantee is not part of the memory pool
suite.pool.On("Has", guarantee.ID()).Return(false)
// submit the guarantee as if it was sent by a collection node
err := suite.core.OnGuarantee(suite.collID, guarantee)
suite.Assert().Error(err, "should error with expired collection")
suite.Assert().True(engine.IsInvalidInputError(err))
}
// TestOnGuaranteeInvalidGuarantor verifiers that collections with any _unknown_
// signer are rejected.
func (suite *IngestionCoreSuite) TestOnGuaranteeInvalidGuarantor() {
// create a guarantee and add random (unknown) signer ID
guarantee := suite.validGuarantee()
guarantee.SignerIndices = []byte{4}
// the guarantee is not part of the memory pool
suite.pool.On("Has", guarantee.ID()).Return(false)
suite.pool.On("Add", guarantee).Return(true)
// submit the guarantee as if it was sent by a collection node
err := suite.core.OnGuarantee(suite.collID, guarantee)
suite.Assert().Error(err, "should error with invalid guarantor")
suite.Assert().True(engine.IsInvalidInputError(err), err)
suite.Assert().True(signature.IsInvalidSignerIndicesError(err), err)
// check that the guarantee has _not_ been added to the mempool
suite.pool.AssertNotCalled(suite.T(), "Add", guarantee)
}
// test that just after an epoch boundary we still accept guarantees from collectors
// in clusters from the previous epoch (and collectors which are leaving the network
// at this epoch boundary).
func (suite *IngestionCoreSuite) TestOnGuaranteeEpochEnd() {
// The finalized state contains the identity of a collector that:
// * was active in the previous epoch but is leaving as of the current epoch
// * wasn't ejected and has positive initial weight
// This happens when we finalize the final block of the epoch during
// which this node requested to unstake
colID, ok := suite.finalIdentities.ByNodeID(suite.collID)
suite.Require().True(ok)
colID.EpochParticipationStatus = flow.EpochParticipationStatusLeaving
guarantee := suite.validGuarantee()
// the guarantee is not part of the memory pool
suite.pool.On("Has", guarantee.ID()).Return(false)
suite.pool.On("Add", guarantee).Return(true).Once()
// submit the guarantee as if it was sent by the collection node which
// is leaving at the current epoch boundary
err := suite.core.OnGuarantee(suite.collID, guarantee)
suite.Assert().NoError(err, "should not error with collector from ending epoch")
// check that the guarantee has been added to the mempool
suite.pool.AssertExpectations(suite.T())
}
func (suite *IngestionCoreSuite) TestOnGuaranteeUnknownOrigin() {
guarantee := suite.validGuarantee()
// the guarantee is not part of the memory pool
suite.pool.On("Has", guarantee.ID()).Return(false)
suite.pool.On("Add", guarantee).Return(true)
// submit the guarantee with an unknown origin
err := suite.core.OnGuarantee(unittest.IdentifierFixture(), guarantee)
suite.Assert().Error(err)
suite.Assert().True(engine.IsInvalidInputError(err))
suite.pool.AssertNotCalled(suite.T(), "Add", guarantee)
}
// validGuarantee returns a valid collection guarantee based on the suite state.
func (suite *IngestionCoreSuite) validGuarantee() *flow.CollectionGuarantee {
guarantee := unittest.CollectionGuaranteeFixture()
guarantee.ChainID = suite.clusterID
signerIndices, err := signature.EncodeSignersToIndices(
[]flow.Identifier{suite.collID}, []flow.Identifier{suite.collID})
require.NoError(suite.T(), err)
guarantee.SignerIndices = signerIndices
guarantee.ReferenceBlockID = suite.head.ID()
return guarantee
}