-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathvalidatorTestMessages.js
538 lines (477 loc) · 14.2 KB
/
validatorTestMessages.js
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
const nowDate = new Date().toISOString()
const oldDate = new Date(Date.now() - 10000000).toISOString()
const oldDateNoHex = (Date.now() - 1000000000) / 1000
const oldDateTimestamp = Date.now() - 1000000000
const inTheFuture = new Date(Date.now() + 10000000).toISOString()
const inTheFutureTimestamp = Date.now() + 10000000
const twoMinutesAgo = new Date(Date.now() - 1000 * 60 * 2).toISOString()
const VALIDATOR_MSG_FROM_ADDRESS = '0x2892f6C41E0718eeeDd49D98D648C789668cA67d'
const VALIDATOR_MSG_STATE_ROOT =
'cc43cd5a31f60002f08f18ef311d1c3e3114d52d59257fbcf861c9c3fd6bec24'
const TEST_CHANNEL_NAME = 'awesomeTestChannel'
const balanceTreeExceeds = {
lilPeep: 50,
xxxtentacion: 51,
}
const balanceTreeEquals = {
lilPeep: 50,
xxxtentacion: 50,
}
const balanceTreeUnder = {
lilPeep: 50,
xxxtentacion: 49,
}
function generateMessage(params) {
const { type, timestamp, healthy, received } = params
const validatorMessage = {
from: VALIDATOR_MSG_FROM_ADDRESS,
msg: {
type,
signature:
'0x0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
stateRoot: VALIDATOR_MSG_STATE_ROOT,
},
received: received || nowDate,
}
switch (type) {
case 'Heartbeat':
validatorMessage.msg['timestamp'] = timestamp
break
case 'ApproveState':
validatorMessage.msg['lastEvAggr'] = timestamp
validatorMessage.msg['isHealthy'] = healthy
break
case 'NewState':
break
default:
break
}
return validatorMessage
}
function generateCampaign(params) {
const { validUntil, balanceTree, withdrawPeriodStart } = params
const campaign = {
campaign: {
id: TEST_CHANNEL_NAME,
depositAmount: 100,
depositAsset: 'DAI',
spec: {
validators: [
{
id: 'awesomeLeader',
url: 'https://tom.adex.network',
},
{
id: 'awesomeFollower',
url: 'https://jerry.adex.network',
},
],
},
},
}
if (balanceTree) {
campaign['balanceTree'] = balanceTree
}
if (validUntil) {
campaign.campaign['validUntil'] = validUntil
}
if (withdrawPeriodStart) {
campaign.campaign.spec['withdrawPeriodStart'] = withdrawPeriodStart
}
return campaign
}
const heartbeatMessageOldDate = generateMessage({
type: 'Heartbeat',
timestamp: oldDate,
})
const heartbeatMessageNowDate = generateMessage({
type: 'Heartbeat',
timestamp: nowDate,
})
const heartbeatMessageNowDate2 = generateMessage({
type: 'Heartbeat',
timestamp: nowDate,
})
const newStateMessage = generateMessage({ type: 'NewState' })
const approveStateMessageHealthy = generateMessage({
type: 'ApproveState',
healthy: true,
})
const approveStateMessageUnhealthy = generateMessage({
type: 'ApproveState',
healthy: false,
})
const newStateMessageOld = generateMessage({
type: 'NewState',
healthy: true,
received: oldDate,
})
newStateMessageOld.msg.stateRoot += '1'
const newStateOlderThanMinute = generateMessage({
type: 'NewState',
healthy: true,
received: twoMinutesAgo,
})
const newStateMessageNew = generateMessage({
type: 'NewState',
healthy: true,
received: nowDate,
})
// Empty messages
const initializingMessages1 = {
leaderHeartbeat: [],
followerHeartbeat: [],
newStateLeader: [],
approveStateFollower: [],
}
const initializingMessages2 = {
leaderHeartbeat: [heartbeatMessageNowDate],
followerHeartbeat: [],
newStateLeader: [],
approveStateFollower: [],
}
const initializingMessages3 = {
leaderHeartbeat: [],
followerHeartbeat: [heartbeatMessageNowDate],
newStateLeader: [],
approveStateFollower: [],
}
// Not empty messages
const notInitializingMessages = {
leaderHeartbeat: [heartbeatMessageNowDate],
followerHeartbeat: [heartbeatMessageNowDate],
newStateLeader: [],
approveStateFollower: [],
}
// No recent heartbeat on both
const offlineMessages1 = {
leaderHeartbeat: [heartbeatMessageOldDate],
followerHeartbeat: [heartbeatMessageOldDate],
followerFromLeader: [heartbeatMessageOldDate],
newStateLeader: [],
approveStateFollower: [],
}
// No recent heartbeat on second
const offlineMessages2 = {
leaderHeartbeat: [heartbeatMessageNowDate],
followerHeartbeat: [heartbeatMessageOldDate],
followerFromLeader: [heartbeatMessageOldDate],
newStateLeader: [],
approveStateFollower: [],
}
// No recent heartbeat on first
const offlineMessages3 = {
leaderHeartbeat: [heartbeatMessageOldDate],
followerHeartbeat: [heartbeatMessageNowDate],
followerFromLeader: [heartbeatMessageNowDate],
newStateLeader: [],
approveStateFollower: [],
}
// Recent heartbeat on both
const notOfflineMessages = {
leaderHeartbeat: [heartbeatMessageNowDate],
followerFromLeader: [heartbeatMessageNowDate],
newStateLeader: [],
approveStateFollower: [],
}
// No recent heartbeat messages on both sides
const disconnectedMessages1 = {
leaderHeartbeat: [heartbeatMessageOldDate],
followerHeartbeat: [heartbeatMessageOldDate],
followerFromLeader: [heartbeatMessageOldDate],
newStateLeader: [],
approveStateFollower: [],
}
// No recent leader heartbeat messages on follower
const disconnectedMessages2 = {
leaderHeartbeat: [heartbeatMessageOldDate],
followerHeartbeat: [heartbeatMessageOldDate],
followerFromLeader: [heartbeatMessageNowDate],
newStateLeader: [],
approveStateFollower: [],
}
// No recent follower heartbeat messages on leader
const disconnectedMessages3 = {
leaderHeartbeat: [heartbeatMessageOldDate],
followerHeartbeat: [heartbeatMessageNowDate],
followerFromLeader: [],
newStateLeader: [],
approveStateFollower: [],
}
// Both validators has recent heartbeat messages
const notDisconnectedMessages1 = {
followerHeartbeat: [heartbeatMessageNowDate],
followerFromLeader: [heartbeatMessageNowDate2],
newStateLeader: [],
approveStateFollower: [],
}
// Recent newstate but no approvestate
const invalidMessages = {
leaderHeartbeat: [heartbeatMessageNowDate],
followerHeartbeat: [heartbeatMessageNowDate],
followerFromLeader: [heartbeatMessageNowDate],
newStateLeader: [newStateMessage],
approveStateFollower: [],
latestNewState: [],
}
const invalidMessages2 = {
leaderHeartbeat: [heartbeatMessageNowDate],
followerHeartbeat: [heartbeatMessageNowDate],
followerFromLeader: [heartbeatMessageNowDate],
newStateLeader: [newStateMessageOld],
approveStateFollower: [approveStateMessageHealthy],
latestNewState: [newStateOlderThanMinute],
}
// Recent newstate and approvestate
const notInvalidMessages1 = {
leaderHeartbeat: [heartbeatMessageNowDate],
followerHeartbeat: [heartbeatMessageNowDate],
newStateLeader: [newStateMessage],
approveStateFollower: [approveStateMessageHealthy],
latestNewState: [newStateMessageNew],
}
// // No approvestate but also no recent newstate
// const notInvalidMessages2 = {
// leaderHeartbeat: [heartbeatMessageNowDate],
// followerHeartbeat: [heartbeatMessageNowDate],
// newStateLeader: [],
// approveStateFollower: [],
// latestNewState: [newStateMessageOld],
// }
// // Approvestate but no recent newstate
// const notInvalidMessages3 = {
// leaderHeartbeat: [heartbeatMessageNowDate],
// followerHeartbeat: [heartbeatMessageNowDate],
// newStateLeader: [],
// approveStateFollower: [approveStateMessageHealthy],
// latestNewState: [newStateMessageOld],
// }
const notInvalidMessages4 = {
leaderHeartbeat: [heartbeatMessageNowDate],
followerHeartbeat: [heartbeatMessageNowDate],
newStateLeader: [newStateMessageOld],
approveStateFollower: [approveStateMessageHealthy],
latestNewState: [newStateMessageNew],
}
const notInvalidMessages5 = {
leaderHeartbeat: [heartbeatMessageNowDate],
followerHeartbeat: [heartbeatMessageNowDate],
newStateLeader: [newStateOlderThanMinute],
approveStateFollower: [approveStateMessageHealthy],
latestNewState: [newStateOlderThanMinute],
}
const notInvalidMessages6 = {
leaderHeartbeat: [heartbeatMessageNowDate],
followerHeartbeat: [heartbeatMessageNowDate],
newStateLeader: [newStateMessageNew],
approveStateFollower: [approveStateMessageHealthy],
latestNewState: [newStateMessageNew],
}
// Recent heartbeat and newstate but approvestate reports unhealthy
const unhealthyMessages = {
leaderHeartbeat: [heartbeatMessageNowDate],
followerHeartbeat: [heartbeatMessageNowDate],
followerFromLeader: [heartbeatMessageNowDate],
newStateLeader: [newStateMessage],
approveStateFollower: [approveStateMessageUnhealthy],
latestNewState: [newStateMessageNew],
}
// Recent heartbeat and newstate and approvestate reports healthy
const notUnhealthyMessages1 = {
leaderHeartbeat: [heartbeatMessageNowDate],
followerHeartbeat: [heartbeatMessageNowDate],
newStateLeader: [newStateMessage],
approveStateFollower: [approveStateMessageHealthy],
}
// No newstate messages but everything else is ok
const notUnhealthyMessages2 = {
leaderHeartbeat: [heartbeatMessageNowDate],
followerHeartbeat: [heartbeatMessageNowDate],
newStateLeader: [],
approveStateFollower: [approveStateMessageUnhealthy],
}
// recent heartbeat but newstate not emitted
const readyMessages1 = {
leaderHeartbeat: [heartbeatMessageNowDate],
followerHeartbeat: [heartbeatMessageNowDate],
followerFromLeader: [heartbeatMessageNowDate],
newStateLeader: [],
approveStateFollower: [],
}
// recent heartbeat but newstate is emitted
const notReadyMessages1 = {
leaderHeartbeat: [heartbeatMessageNowDate],
followerHeartbeat: [heartbeatMessageNowDate],
newStateLeader: [newStateMessage],
approveStateFollower: [],
}
// one heartbeat not recent and new state emitted
const notReadyMessages2 = {
leaderHeartbeat: [heartbeatMessageNowDate],
followerHeartbeat: [heartbeatMessageOldDate],
newStateLeader: [newStateMessage],
approveStateFollower: [],
}
// no newstate emitted but one heartbeat is not recent
const notReadyMessages3 = {
leaderHeartbeat: [heartbeatMessageOldDate],
followerHeartbeat: [heartbeatMessageNowDate],
newStateLeader: [],
approveStateFollower: [],
}
/*
// no heartbeat but newstate is emitted
const notReadyMessages4 = {
leaderHeartbeat: [],
followerHeartbeat: [],
newStateLeader: [newStateMessage],
approveStateFollower: []
}
*/
// A situation where it works
const activeMessages = {
leaderHeartbeat: [heartbeatMessageNowDate],
followerHeartbeat: [heartbeatMessageNowDate],
followerFromLeader: [heartbeatMessageNowDate],
newStateLeader: [newStateMessage],
approveStateFollower: [approveStateMessageHealthy],
latestNewState: [newStateMessage],
}
// Working example but we switch isHealthy to false
const notActiveMessages1 = {
leaderHeartbeat: [heartbeatMessageNowDate],
followerHeartbeat: [heartbeatMessageNowDate],
newStateLeader: [newStateMessage],
approveStateFollower: [approveStateMessageUnhealthy],
}
// No newState
const notActiveMessages2 = {
leaderHeartbeat: [heartbeatMessageNowDate],
followerHeartbeat: [heartbeatMessageNowDate],
newStateLeader: [],
approveStateFollower: [approveStateMessageHealthy],
}
// one Heartbeat is not recent
const notActiveMessages3 = {
leaderHeartbeat: [heartbeatMessageOldDate],
followerHeartbeat: [heartbeatMessageNowDate],
newStateLeader: [newStateMessage],
approveStateFollower: [approveStateMessageHealthy],
}
// Total balances is more than depositAmount
const exhausted1 = generateCampaign({ balanceTree: balanceTreeExceeds })
// Total balances is equal to depositAmount
const exhausted2 = generateCampaign({ balanceTree: balanceTreeEquals })
// Total balances is less than depositAmount
const notExhausted = generateCampaign({ balanceTree: balanceTreeUnder })
// Expired
const expiredCampaign = generateCampaign({ validUntil: oldDateNoHex })
// Not expired
const notExpiredCampaign = generateCampaign({ validUntil: inTheFuture })
// the channel is in withdraw period
const withdrawCampaign = generateCampaign({
withdrawPeriodStart: oldDateTimestamp,
})
// Campaign has withdrawPeriodStart but it has not yet passed as a date
const notWithdrawCampaign = generateCampaign({
withdrawPeriodStart: inTheFutureTimestamp,
})
const defaultCampaign = notExhausted.campaign
const expiredConfig = [
activeMessages,
expiredCampaign.campaign,
balanceTreeUnder,
]
const exhaustedConfig = [
activeMessages,
exhausted1.campaign,
balanceTreeExceeds,
]
const withdrawConfig = [
activeMessages,
withdrawCampaign.campaign,
balanceTreeUnder,
]
const initializingConfig = [
initializingMessages2,
notExpiredCampaign.campaign,
balanceTreeUnder,
]
const offlineConfig = [offlineMessages2, defaultCampaign, balanceTreeUnder]
const disconnectedConfig = [
disconnectedMessages3,
notExpiredCampaign.campaign,
balanceTreeUnder,
]
const invalidConfig = [invalidMessages, defaultCampaign, balanceTreeUnder]
const invalidConfigSecondCase = [
invalidMessages2,
defaultCampaign,
balanceTreeUnder,
]
const unhealthyConfig = [unhealthyMessages, defaultCampaign, balanceTreeUnder]
const activeConfig = [activeMessages, defaultCampaign, balanceTreeUnder]
const readyConfig = [readyMessages1, defaultCampaign, balanceTreeUnder]
module.exports = {
initializing: {
first: initializingMessages1,
second: initializingMessages2,
third: initializingMessages3,
},
notInitializing: { first: notInitializingMessages },
offline: {
first: offlineMessages1,
second: offlineMessages2,
third: offlineMessages3,
},
notOffline: { first: notOfflineMessages },
disconnected: {
first: disconnectedMessages1,
second: disconnectedMessages2,
third: disconnectedMessages3,
},
notDisconnected: { first: notDisconnectedMessages1 },
invalid: {
first: invalidMessages,
second: invalidMessages2,
},
notInvalid: {
first: notInvalidMessages1,
// second: notInvalidMessages2,
// third: notInvalidMessages3,
fourth: notInvalidMessages4,
fifth: notInvalidMessages5,
sixth: notInvalidMessages6,
},
unhealthy: { first: unhealthyMessages },
notUnhealthy: { first: notUnhealthyMessages1, second: notUnhealthyMessages2 },
ready: { first: readyMessages1 },
notReady: {
first: notReadyMessages1,
second: notReadyMessages2,
third: notReadyMessages3,
},
active: { first: activeMessages },
notActive: {
first: notActiveMessages1,
second: notActiveMessages2,
third: notActiveMessages3,
},
exhausted: { first: exhausted1, second: exhausted2 },
notExhausted: { first: notExhausted },
expired: { first: expiredCampaign },
notExpired: { first: notExpiredCampaign },
withdraw: { first: withdrawCampaign },
notWithdraw: { first: notWithdrawCampaign },
expiredConfig,
exhaustedConfig,
withdrawConfig,
initializingConfig,
offlineConfig,
disconnectedConfig,
invalidConfig,
invalidConfigSecondCase,
unhealthyConfig,
activeConfig,
readyConfig,
}