-
Notifications
You must be signed in to change notification settings - Fork 672
/
test_sender.go
422 lines (376 loc) · 17.4 KB
/
test_sender.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package common
import (
"context"
"errors"
"github.com/stretchr/testify/require"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow"
"github.com/ava-labs/avalanchego/utils/set"
)
var (
_ Sender = (*SenderTest)(nil)
_ AppSender = (*FakeSender)(nil)
errAccept = errors.New("unexpectedly called Accept")
errSendAppRequest = errors.New("unexpectedly called SendAppRequest")
errSendAppResponse = errors.New("unexpectedly called SendAppResponse")
errSendAppGossip = errors.New("unexpectedly called SendAppGossip")
errSendAppGossipSpecific = errors.New("unexpectedly called SendAppGossipSpecific")
)
// SenderTest is a test sender
type SenderTest struct {
T require.TestingT
CantAccept,
CantSendGetStateSummaryFrontier, CantSendStateSummaryFrontier,
CantSendGetAcceptedStateSummary, CantSendAcceptedStateSummary,
CantSendGetAcceptedFrontier, CantSendAcceptedFrontier,
CantSendGetAccepted, CantSendAccepted,
CantSendGet, CantSendGetAncestors, CantSendPut, CantSendAncestors,
CantSendPullQuery, CantSendPushQuery, CantSendChits,
CantSendGossip,
CantSendAppRequest, CantSendAppResponse, CantSendAppGossip, CantSendAppGossipSpecific,
CantSendCrossChainAppRequest, CantSendCrossChainAppResponse bool
AcceptF func(*snow.ConsensusContext, ids.ID, []byte) error
SendGetStateSummaryFrontierF func(context.Context, set.Set[ids.NodeID], uint32)
SendStateSummaryFrontierF func(context.Context, ids.NodeID, uint32, []byte)
SendGetAcceptedStateSummaryF func(context.Context, set.Set[ids.NodeID], uint32, []uint64)
SendAcceptedStateSummaryF func(context.Context, ids.NodeID, uint32, []ids.ID)
SendGetAcceptedFrontierF func(context.Context, set.Set[ids.NodeID], uint32)
SendAcceptedFrontierF func(context.Context, ids.NodeID, uint32, ids.ID)
SendGetAcceptedF func(context.Context, set.Set[ids.NodeID], uint32, []ids.ID)
SendAcceptedF func(context.Context, ids.NodeID, uint32, []ids.ID)
SendGetF func(context.Context, ids.NodeID, uint32, ids.ID)
SendGetAncestorsF func(context.Context, ids.NodeID, uint32, ids.ID)
SendPutF func(context.Context, ids.NodeID, uint32, []byte)
SendAncestorsF func(context.Context, ids.NodeID, uint32, [][]byte)
SendPushQueryF func(context.Context, set.Set[ids.NodeID], uint32, []byte, uint64)
SendPullQueryF func(context.Context, set.Set[ids.NodeID], uint32, ids.ID, uint64)
SendChitsF func(context.Context, ids.NodeID, uint32, ids.ID, ids.ID, ids.ID)
SendGossipF func(context.Context, []byte)
SendAppRequestF func(context.Context, set.Set[ids.NodeID], uint32, []byte) error
SendAppResponseF func(context.Context, ids.NodeID, uint32, []byte) error
SendAppGossipF func(context.Context, []byte) error
SendAppGossipSpecificF func(context.Context, set.Set[ids.NodeID], []byte) error
SendCrossChainAppRequestF func(context.Context, ids.ID, uint32, []byte)
SendCrossChainAppResponseF func(context.Context, ids.ID, uint32, []byte)
}
// Default set the default callable value to [cant]
func (s *SenderTest) Default(cant bool) {
s.CantAccept = cant
s.CantSendGetStateSummaryFrontier = cant
s.CantSendStateSummaryFrontier = cant
s.CantSendGetAcceptedStateSummary = cant
s.CantSendAcceptedStateSummary = cant
s.CantSendGetAcceptedFrontier = cant
s.CantSendAcceptedFrontier = cant
s.CantSendGetAccepted = cant
s.CantSendAccepted = cant
s.CantSendGet = cant
s.CantSendGetAccepted = cant
s.CantSendPut = cant
s.CantSendAncestors = cant
s.CantSendPullQuery = cant
s.CantSendPushQuery = cant
s.CantSendChits = cant
s.CantSendGossip = cant
s.CantSendAppRequest = cant
s.CantSendAppResponse = cant
s.CantSendAppGossip = cant
s.CantSendAppGossipSpecific = cant
s.CantSendCrossChainAppRequest = cant
s.CantSendCrossChainAppResponse = cant
}
// Accept calls AcceptF if it was initialized. If it wasn't initialized and this
// function shouldn't be called and testing was initialized, then testing will
// fail.
func (s *SenderTest) Accept(ctx *snow.ConsensusContext, containerID ids.ID, container []byte) error {
if s.AcceptF != nil {
return s.AcceptF(ctx, containerID, container)
}
if !s.CantAccept {
return nil
}
if s.T != nil {
require.FailNow(s.T, errAccept.Error())
}
return errAccept
}
// SendGetStateSummaryFrontier calls SendGetStateSummaryFrontierF if it was
// initialized. If it wasn't initialized and this function shouldn't be called
// and testing was initialized, then testing will fail.
func (s *SenderTest) SendGetStateSummaryFrontier(ctx context.Context, validatorIDs set.Set[ids.NodeID], requestID uint32) {
if s.SendGetStateSummaryFrontierF != nil {
s.SendGetStateSummaryFrontierF(ctx, validatorIDs, requestID)
} else if s.CantSendGetStateSummaryFrontier && s.T != nil {
require.FailNow(s.T, "Unexpectedly called SendGetStateSummaryFrontier")
}
}
// SendStateSummaryFrontier calls SendStateSummaryFrontierF if it was
// initialized. If it wasn't initialized and this function shouldn't be called
// and testing was initialized, then testing will fail.
func (s *SenderTest) SendStateSummaryFrontier(ctx context.Context, validatorID ids.NodeID, requestID uint32, summary []byte) {
if s.SendStateSummaryFrontierF != nil {
s.SendStateSummaryFrontierF(ctx, validatorID, requestID, summary)
} else if s.CantSendStateSummaryFrontier && s.T != nil {
require.FailNow(s.T, "Unexpectedly called SendStateSummaryFrontier")
}
}
// SendGetAcceptedStateSummary calls SendGetAcceptedStateSummaryF if it was
// initialized. If it wasn't initialized and this function shouldn't be called
// and testing was initialized, then testing will fail.
func (s *SenderTest) SendGetAcceptedStateSummary(ctx context.Context, nodeIDs set.Set[ids.NodeID], requestID uint32, heights []uint64) {
if s.SendGetAcceptedStateSummaryF != nil {
s.SendGetAcceptedStateSummaryF(ctx, nodeIDs, requestID, heights)
} else if s.CantSendGetAcceptedStateSummary && s.T != nil {
require.FailNow(s.T, "Unexpectedly called SendGetAcceptedStateSummaryF")
}
}
// SendAcceptedStateSummary calls SendAcceptedStateSummaryF if it was
// initialized. If it wasn't initialized and this function shouldn't be called
// and testing was initialized, then testing will fail.
func (s *SenderTest) SendAcceptedStateSummary(ctx context.Context, validatorID ids.NodeID, requestID uint32, summaryIDs []ids.ID) {
if s.SendAcceptedStateSummaryF != nil {
s.SendAcceptedStateSummaryF(ctx, validatorID, requestID, summaryIDs)
} else if s.CantSendAcceptedStateSummary && s.T != nil {
require.FailNow(s.T, "Unexpectedly called SendAcceptedStateSummary")
}
}
// SendGetAcceptedFrontier calls SendGetAcceptedFrontierF if it was initialized.
// If it wasn't initialized and this function shouldn't be called and testing
// was initialized, then testing will fail.
func (s *SenderTest) SendGetAcceptedFrontier(ctx context.Context, validatorIDs set.Set[ids.NodeID], requestID uint32) {
if s.SendGetAcceptedFrontierF != nil {
s.SendGetAcceptedFrontierF(ctx, validatorIDs, requestID)
} else if s.CantSendGetAcceptedFrontier && s.T != nil {
require.FailNow(s.T, "Unexpectedly called SendGetAcceptedFrontier")
}
}
// SendAcceptedFrontier calls SendAcceptedFrontierF if it was initialized. If it
// wasn't initialized and this function shouldn't be called and testing was
// initialized, then testing will fail.
func (s *SenderTest) SendAcceptedFrontier(ctx context.Context, validatorID ids.NodeID, requestID uint32, containerID ids.ID) {
if s.SendAcceptedFrontierF != nil {
s.SendAcceptedFrontierF(ctx, validatorID, requestID, containerID)
} else if s.CantSendAcceptedFrontier && s.T != nil {
require.FailNow(s.T, "Unexpectedly called SendAcceptedFrontier")
}
}
// SendGetAccepted calls SendGetAcceptedF if it was initialized. If it wasn't
// initialized and this function shouldn't be called and testing was
// initialized, then testing will fail.
func (s *SenderTest) SendGetAccepted(ctx context.Context, nodeIDs set.Set[ids.NodeID], requestID uint32, containerIDs []ids.ID) {
if s.SendGetAcceptedF != nil {
s.SendGetAcceptedF(ctx, nodeIDs, requestID, containerIDs)
} else if s.CantSendGetAccepted && s.T != nil {
require.FailNow(s.T, "Unexpectedly called SendGetAccepted")
}
}
// SendAccepted calls SendAcceptedF if it was initialized. If it wasn't
// initialized and this function shouldn't be called and testing was
// initialized, then testing will fail.
func (s *SenderTest) SendAccepted(ctx context.Context, validatorID ids.NodeID, requestID uint32, containerIDs []ids.ID) {
if s.SendAcceptedF != nil {
s.SendAcceptedF(ctx, validatorID, requestID, containerIDs)
} else if s.CantSendAccepted && s.T != nil {
require.FailNow(s.T, "Unexpectedly called SendAccepted")
}
}
// SendGet calls SendGetF if it was initialized. If it wasn't initialized and
// this function shouldn't be called and testing was initialized, then testing
// will fail.
func (s *SenderTest) SendGet(ctx context.Context, vdr ids.NodeID, requestID uint32, containerID ids.ID) {
if s.SendGetF != nil {
s.SendGetF(ctx, vdr, requestID, containerID)
} else if s.CantSendGet && s.T != nil {
require.FailNow(s.T, "Unexpectedly called SendGet")
}
}
// SendGetAncestors calls SendGetAncestorsF if it was initialized. If it wasn't
// initialized and this function shouldn't be called and testing was
// initialized, then testing will fail.
func (s *SenderTest) SendGetAncestors(ctx context.Context, validatorID ids.NodeID, requestID uint32, containerID ids.ID) {
if s.SendGetAncestorsF != nil {
s.SendGetAncestorsF(ctx, validatorID, requestID, containerID)
} else if s.CantSendGetAncestors && s.T != nil {
require.FailNow(s.T, "Unexpectedly called SendCantSendGetAncestors")
}
}
// SendPut calls SendPutF if it was initialized. If it wasn't initialized and
// this function shouldn't be called and testing was initialized, then testing
// will fail.
func (s *SenderTest) SendPut(ctx context.Context, vdr ids.NodeID, requestID uint32, container []byte) {
if s.SendPutF != nil {
s.SendPutF(ctx, vdr, requestID, container)
} else if s.CantSendPut && s.T != nil {
require.FailNow(s.T, "Unexpectedly called SendPut")
}
}
// SendAncestors calls SendAncestorsF if it was initialized. If it wasn't
// initialized and this function shouldn't be called and testing was
// initialized, then testing will fail.
func (s *SenderTest) SendAncestors(ctx context.Context, vdr ids.NodeID, requestID uint32, containers [][]byte) {
if s.SendAncestorsF != nil {
s.SendAncestorsF(ctx, vdr, requestID, containers)
} else if s.CantSendAncestors && s.T != nil {
require.FailNow(s.T, "Unexpectedly called SendAncestors")
}
}
// SendPushQuery calls SendPushQueryF if it was initialized. If it wasn't
// initialized and this function shouldn't be called and testing was
// initialized, then testing will fail.
func (s *SenderTest) SendPushQuery(ctx context.Context, vdrs set.Set[ids.NodeID], requestID uint32, container []byte, requestedHeight uint64) {
if s.SendPushQueryF != nil {
s.SendPushQueryF(ctx, vdrs, requestID, container, requestedHeight)
} else if s.CantSendPushQuery && s.T != nil {
require.FailNow(s.T, "Unexpectedly called SendPushQuery")
}
}
// SendPullQuery calls SendPullQueryF if it was initialized. If it wasn't
// initialized and this function shouldn't be called and testing was
// initialized, then testing will fail.
func (s *SenderTest) SendPullQuery(ctx context.Context, vdrs set.Set[ids.NodeID], requestID uint32, containerID ids.ID, requestedHeight uint64) {
if s.SendPullQueryF != nil {
s.SendPullQueryF(ctx, vdrs, requestID, containerID, requestedHeight)
} else if s.CantSendPullQuery && s.T != nil {
require.FailNow(s.T, "Unexpectedly called SendPullQuery")
}
}
// SendChits calls SendChitsF if it was initialized. If it wasn't initialized
// and this function shouldn't be called and testing was initialized, then
// testing will fail.
func (s *SenderTest) SendChits(ctx context.Context, vdr ids.NodeID, requestID uint32, preferredID ids.ID, preferredIDAtHeight ids.ID, acceptedID ids.ID) {
if s.SendChitsF != nil {
s.SendChitsF(ctx, vdr, requestID, preferredID, preferredIDAtHeight, acceptedID)
} else if s.CantSendChits && s.T != nil {
require.FailNow(s.T, "Unexpectedly called SendChits")
}
}
// SendGossip calls SendGossipF if it was initialized. If it wasn't initialized
// and this function shouldn't be called and testing was initialized, then
// testing will fail.
func (s *SenderTest) SendGossip(ctx context.Context, container []byte) {
if s.SendGossipF != nil {
s.SendGossipF(ctx, container)
} else if s.CantSendGossip && s.T != nil {
require.FailNow(s.T, "Unexpectedly called SendGossip")
}
}
// SendCrossChainAppRequest calls SendCrossChainAppRequestF if it was
// initialized. If it wasn't initialized and this function shouldn't be called
// and testing was initialized, then testing will fail.
func (s *SenderTest) SendCrossChainAppRequest(ctx context.Context, chainID ids.ID, requestID uint32, appRequestBytes []byte) error {
if s.SendCrossChainAppRequestF != nil {
s.SendCrossChainAppRequestF(ctx, chainID, requestID, appRequestBytes)
} else if s.CantSendCrossChainAppRequest && s.T != nil {
require.FailNow(s.T, "Unexpectedly called SendCrossChainAppRequest")
}
return nil
}
// SendCrossChainAppResponse calls SendCrossChainAppResponseF if it was
// initialized. If it wasn't initialized and this function shouldn't be called
// and testing was initialized, then testing will fail.
func (s *SenderTest) SendCrossChainAppResponse(ctx context.Context, chainID ids.ID, requestID uint32, appResponseBytes []byte) error {
if s.SendCrossChainAppResponseF != nil {
s.SendCrossChainAppResponseF(ctx, chainID, requestID, appResponseBytes)
} else if s.CantSendCrossChainAppResponse && s.T != nil {
require.FailNow(s.T, "Unexpectedly called SendCrossChainAppResponse")
}
return nil
}
// SendAppRequest calls SendAppRequestF if it was initialized. If it wasn't
// initialized and this function shouldn't be called and testing was
// initialized, then testing will fail.
func (s *SenderTest) SendAppRequest(ctx context.Context, nodeIDs set.Set[ids.NodeID], requestID uint32, appRequestBytes []byte) error {
switch {
case s.SendAppRequestF != nil:
return s.SendAppRequestF(ctx, nodeIDs, requestID, appRequestBytes)
case s.CantSendAppRequest && s.T != nil:
require.FailNow(s.T, errSendAppRequest.Error())
}
return errSendAppRequest
}
// SendAppResponse calls SendAppResponseF if it was initialized. If it wasn't
// initialized and this function shouldn't be called and testing was
// initialized, then testing will fail.
func (s *SenderTest) SendAppResponse(ctx context.Context, nodeID ids.NodeID, requestID uint32, appResponseBytes []byte) error {
switch {
case s.SendAppResponseF != nil:
return s.SendAppResponseF(ctx, nodeID, requestID, appResponseBytes)
case s.CantSendAppResponse && s.T != nil:
require.FailNow(s.T, errSendAppResponse.Error())
}
return errSendAppResponse
}
// SendAppGossip calls SendAppGossipF if it was initialized. If it wasn't
// initialized and this function shouldn't be called and testing was
// initialized, then testing will fail.
func (s *SenderTest) SendAppGossip(ctx context.Context, appGossipBytes []byte) error {
switch {
case s.SendAppGossipF != nil:
return s.SendAppGossipF(ctx, appGossipBytes)
case s.CantSendAppGossip && s.T != nil:
require.FailNow(s.T, errSendAppGossip.Error())
}
return errSendAppGossip
}
// SendAppGossipSpecific calls SendAppGossipSpecificF if it was initialized. If it wasn't
// initialized and this function shouldn't be called and testing was
// initialized, then testing will fail.
func (s *SenderTest) SendAppGossipSpecific(ctx context.Context, nodeIDs set.Set[ids.NodeID], appGossipBytes []byte) error {
switch {
case s.SendAppGossipSpecificF != nil:
return s.SendAppGossipSpecificF(ctx, nodeIDs, appGossipBytes)
case s.CantSendAppGossipSpecific && s.T != nil:
require.FailNow(s.T, errSendAppGossipSpecific.Error())
}
return errSendAppGossipSpecific
}
// FakeSender is used for testing
type FakeSender struct {
SentAppRequest, SentAppResponse,
SentAppGossip, SentAppGossipSpecific,
SentCrossChainAppRequest, SentCrossChainAppResponse chan []byte
}
func (f FakeSender) SendAppRequest(_ context.Context, _ set.Set[ids.NodeID], _ uint32, bytes []byte) error {
if f.SentAppRequest == nil {
return nil
}
f.SentAppRequest <- bytes
return nil
}
func (f FakeSender) SendAppResponse(_ context.Context, _ ids.NodeID, _ uint32, bytes []byte) error {
if f.SentAppResponse == nil {
return nil
}
f.SentAppResponse <- bytes
return nil
}
func (f FakeSender) SendAppGossip(_ context.Context, bytes []byte) error {
if f.SentAppGossip == nil {
return nil
}
f.SentAppGossip <- bytes
return nil
}
func (f FakeSender) SendAppGossipSpecific(_ context.Context, _ set.Set[ids.NodeID], bytes []byte) error {
if f.SentAppGossipSpecific == nil {
return nil
}
f.SentAppGossipSpecific <- bytes
return nil
}
func (f FakeSender) SendCrossChainAppRequest(_ context.Context, _ ids.ID, _ uint32, bytes []byte) error {
if f.SentCrossChainAppRequest == nil {
return nil
}
f.SentCrossChainAppRequest <- bytes
return nil
}
func (f FakeSender) SendCrossChainAppResponse(_ context.Context, _ ids.ID, _ uint32, bytes []byte) error {
if f.SentCrossChainAppResponse == nil {
return nil
}
f.SentCrossChainAppResponse <- bytes
return nil
}