@@ -70,9 +70,8 @@ var keys = []string{
70
70
"7184c1701569e3a4c4d2ddce691edd983b81e42e09196d332e1ae2f1e062cff4" ,
71
71
}
72
72
73
- const NumNodes = 16 // must not exceed the number of keys (32)
74
-
75
73
type TestData struct {
74
+ started int
76
75
counter [NumNodes ]int
77
76
mutex sync.RWMutex
78
77
}
@@ -84,21 +83,29 @@ type TestNode struct {
84
83
filerID string
85
84
}
86
85
86
+ const NumNodes = 8 // must not exceed the number of keys (32)
87
+
87
88
var result TestData
88
89
var nodes [NumNodes ]* TestNode
89
90
var sharedKey = hexutil .MustDecode ("0x03ca634cae0d49acb401d8a4c6b6fe8c55b70d115bf400769cc1400f3258cd31" )
91
+ var wrongKey = hexutil .MustDecode ("0xf91156714d7ec88d3edc1c652c2181dbb3044e8771c683f3b30d33c12b986b11" )
90
92
var sharedTopic = TopicType {0xF , 0x1 , 0x2 , 0 }
91
- var expectedMessage = []byte ("per rectum ad astra" )
93
+ var wrongTopic = TopicType {0 , 0 , 0 , 0 }
94
+ var expectedMessage = []byte ("per aspera ad astra" )
95
+ var unexpectedMessage = []byte ("per rectum ad astra" )
92
96
var masterBloomFilter []byte
93
97
var masterPow = 0.00000001
94
98
var round = 1
99
+ var debugMode = false
100
+ var prevTime time.Time
101
+ var cntPrev int
95
102
96
103
func TestSimulation (t * testing.T ) {
97
104
// create a chain of whisper nodes,
98
105
// installs the filters with shared (predefined) parameters
99
106
initialize (t )
100
107
101
- // each node sends a number of random (undecryptable) messages
108
+ // each node sends one random (not decryptable) message
102
109
for i := 0 ; i < NumNodes ; i ++ {
103
110
sendMsg (t , false , i )
104
111
}
@@ -115,7 +122,6 @@ func TestSimulation(t *testing.T) {
115
122
116
123
// send new pow and bloom exchange messages
117
124
resetParams (t )
118
- round ++
119
125
120
126
// node #1 sends one expected (decryptable) message
121
127
sendMsg (t , true , 1 )
@@ -140,6 +146,8 @@ func resetParams(t *testing.T) {
140
146
for i := 0 ; i < NumNodes ; i ++ {
141
147
nodes [i ].shh .SetBloomFilter (masterBloomFilter )
142
148
}
149
+
150
+ round ++
143
151
}
144
152
145
153
func initBloom (t * testing.T ) {
@@ -219,15 +227,22 @@ func initialize(t *testing.T) {
219
227
nodes [i ] = & node
220
228
}
221
229
222
- for i := 1 ; i < NumNodes ; i ++ {
223
- go nodes [i ].server . Start ( )
230
+ for i := 0 ; i < NumNodes ; i ++ {
231
+ go startServer ( t , nodes [i ].server )
224
232
}
225
233
226
- // we need to wait until the first node actually starts
227
- err = nodes [0 ].server .Start ()
234
+ waitForServersToStart (t )
235
+ }
236
+
237
+ func startServer (t * testing.T , s * p2p.Server ) {
238
+ err := s .Start ()
228
239
if err != nil {
229
240
t .Fatalf ("failed to start the fisrt server." )
230
241
}
242
+
243
+ result .mutex .Lock ()
244
+ defer result .mutex .Unlock ()
245
+ result .started ++
231
246
}
232
247
233
248
func stopServers () {
@@ -246,8 +261,10 @@ func checkPropagation(t *testing.T, includingNodeZero bool) {
246
261
return
247
262
}
248
263
249
- const cycle = 50
250
- const iterations = 200
264
+ prevTime = time .Now ()
265
+ // (cycle * iterations) should not exceed 50 seconds, since TTL=50
266
+ const cycle = 200 // time in milliseconds
267
+ const iterations = 250
251
268
252
269
first := 0
253
270
if ! includingNodeZero {
@@ -262,29 +279,29 @@ func checkPropagation(t *testing.T, includingNodeZero bool) {
262
279
}
263
280
264
281
mail := f .Retrieve ()
265
- if ! validateMail (t , i , mail ) {
266
- return
267
- }
282
+ validateMail (t , i , mail )
268
283
269
284
if isTestComplete () {
285
+ checkTestStatus ()
270
286
return
271
287
}
272
288
}
273
289
290
+ checkTestStatus ()
274
291
time .Sleep (cycle * time .Millisecond )
275
292
}
276
293
277
- t .Fatalf ("Test was not complete: timeout %d seconds. nodes=%v" , iterations * cycle / 1000 , nodes )
278
-
279
294
if ! includingNodeZero {
280
295
f := nodes [0 ].shh .GetFilter (nodes [0 ].filerID )
281
296
if f != nil {
282
297
t .Fatalf ("node zero received a message with low PoW." )
283
298
}
284
299
}
300
+
301
+ t .Fatalf ("Test was not complete (%d round): timeout %d seconds. nodes=%v" , round , iterations * cycle / 1000 , nodes )
285
302
}
286
303
287
- func validateMail (t * testing.T , index int , mail []* ReceivedMessage ) bool {
304
+ func validateMail (t * testing.T , index int , mail []* ReceivedMessage ) {
288
305
var cnt int
289
306
for _ , m := range mail {
290
307
if bytes .Equal (m .Payload , expectedMessage ) {
@@ -294,22 +311,42 @@ func validateMail(t *testing.T, index int, mail []*ReceivedMessage) bool {
294
311
295
312
if cnt == 0 {
296
313
// no messages received yet: nothing is wrong
297
- return true
314
+ return
298
315
}
299
316
if cnt > 1 {
300
317
t .Fatalf ("node %d received %d." , index , cnt )
301
- return false
302
318
}
303
319
304
- if cnt > 0 {
320
+ if cnt == 1 {
305
321
result .mutex .Lock ()
306
322
defer result .mutex .Unlock ()
307
323
result .counter [index ] += cnt
308
324
if result .counter [index ] > 1 {
309
325
t .Fatalf ("node %d accumulated %d." , index , result .counter [index ])
310
326
}
311
327
}
312
- return true
328
+ }
329
+
330
+ func checkTestStatus () {
331
+ var cnt int
332
+ var arr [NumNodes ]int
333
+
334
+ for i := 0 ; i < NumNodes ; i ++ {
335
+ arr [i ] = nodes [i ].server .PeerCount ()
336
+ envelopes := nodes [i ].shh .Envelopes ()
337
+ if len (envelopes ) >= NumNodes {
338
+ cnt ++
339
+ }
340
+ }
341
+
342
+ if debugMode {
343
+ if cntPrev != cnt {
344
+ fmt .Printf (" %v \t number of nodes that have received all msgs: %d, number of peers per node: %v \n " ,
345
+ time .Since (prevTime ), cnt , arr )
346
+ prevTime = time .Now ()
347
+ cntPrev = cnt
348
+ }
349
+ }
313
350
}
314
351
315
352
func isTestComplete () bool {
@@ -324,7 +361,7 @@ func isTestComplete() bool {
324
361
325
362
for i := 0 ; i < NumNodes ; i ++ {
326
363
envelopes := nodes [i ].shh .Envelopes ()
327
- if len (envelopes ) < 2 {
364
+ if len (envelopes ) < NumNodes + 1 {
328
365
return false
329
366
}
330
367
}
@@ -339,9 +376,10 @@ func sendMsg(t *testing.T, expected bool, id int) {
339
376
340
377
opt := MessageParams {KeySym : sharedKey , Topic : sharedTopic , Payload : expectedMessage , PoW : 0.00000001 , WorkTime : 1 }
341
378
if ! expected {
342
- opt .KeySym [0 ]++
343
- opt .Topic [0 ]++
344
- opt .Payload = opt .Payload [1 :]
379
+ opt .KeySym = wrongKey
380
+ opt .Topic = wrongTopic
381
+ opt .Payload = unexpectedMessage
382
+ opt .Payload [0 ] = byte (id )
345
383
}
346
384
347
385
msg , err := NewSentMessage (& opt )
@@ -459,3 +497,14 @@ func checkBloomFilterExchange(t *testing.T) {
459
497
time .Sleep (50 * time .Millisecond )
460
498
}
461
499
}
500
+
501
+ func waitForServersToStart (t * testing.T ) {
502
+ const iterations = 200
503
+ for j := 0 ; j < iterations ; j ++ {
504
+ time .Sleep (50 * time .Millisecond )
505
+ if result .started == NumNodes {
506
+ return
507
+ }
508
+ }
509
+ t .Fatalf ("Failed to start all the servers, running: %d" , result .started )
510
+ }
0 commit comments