@@ -70,9 +70,8 @@ var keys = []string{
7070 "7184c1701569e3a4c4d2ddce691edd983b81e42e09196d332e1ae2f1e062cff4" ,
7171}
7272
73- const NumNodes = 16 // must not exceed the number of keys (32)
74-
7573type TestData struct {
74+ started int
7675 counter [NumNodes ]int
7776 mutex sync.RWMutex
7877}
@@ -84,21 +83,29 @@ type TestNode struct {
8483 filerID string
8584}
8685
86+ const NumNodes = 8 // must not exceed the number of keys (32)
87+
8788var result TestData
8889var nodes [NumNodes ]* TestNode
8990var sharedKey = hexutil .MustDecode ("0x03ca634cae0d49acb401d8a4c6b6fe8c55b70d115bf400769cc1400f3258cd31" )
91+ var wrongKey = hexutil .MustDecode ("0xf91156714d7ec88d3edc1c652c2181dbb3044e8771c683f3b30d33c12b986b11" )
9092var 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" )
9296var masterBloomFilter []byte
9397var masterPow = 0.00000001
9498var round = 1
99+ var debugMode = false
100+ var prevTime time.Time
101+ var cntPrev int
95102
96103func TestSimulation (t * testing.T ) {
97104 // create a chain of whisper nodes,
98105 // installs the filters with shared (predefined) parameters
99106 initialize (t )
100107
101- // each node sends a number of random (undecryptable) messages
108+ // each node sends one random (not decryptable) message
102109 for i := 0 ; i < NumNodes ; i ++ {
103110 sendMsg (t , false , i )
104111 }
@@ -115,7 +122,6 @@ func TestSimulation(t *testing.T) {
115122
116123 // send new pow and bloom exchange messages
117124 resetParams (t )
118- round ++
119125
120126 // node #1 sends one expected (decryptable) message
121127 sendMsg (t , true , 1 )
@@ -140,6 +146,8 @@ func resetParams(t *testing.T) {
140146 for i := 0 ; i < NumNodes ; i ++ {
141147 nodes [i ].shh .SetBloomFilter (masterBloomFilter )
142148 }
149+
150+ round ++
143151}
144152
145153func initBloom (t * testing.T ) {
@@ -219,15 +227,22 @@ func initialize(t *testing.T) {
219227 nodes [i ] = & node
220228 }
221229
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 )
224232 }
225233
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 ()
228239 if err != nil {
229240 t .Fatalf ("failed to start the fisrt server." )
230241 }
242+
243+ result .mutex .Lock ()
244+ defer result .mutex .Unlock ()
245+ result .started ++
231246}
232247
233248func stopServers () {
@@ -246,8 +261,10 @@ func checkPropagation(t *testing.T, includingNodeZero bool) {
246261 return
247262 }
248263
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
251268
252269 first := 0
253270 if ! includingNodeZero {
@@ -262,29 +279,29 @@ func checkPropagation(t *testing.T, includingNodeZero bool) {
262279 }
263280
264281 mail := f .Retrieve ()
265- if ! validateMail (t , i , mail ) {
266- return
267- }
282+ validateMail (t , i , mail )
268283
269284 if isTestComplete () {
285+ checkTestStatus ()
270286 return
271287 }
272288 }
273289
290+ checkTestStatus ()
274291 time .Sleep (cycle * time .Millisecond )
275292 }
276293
277- t .Fatalf ("Test was not complete: timeout %d seconds. nodes=%v" , iterations * cycle / 1000 , nodes )
278-
279294 if ! includingNodeZero {
280295 f := nodes [0 ].shh .GetFilter (nodes [0 ].filerID )
281296 if f != nil {
282297 t .Fatalf ("node zero received a message with low PoW." )
283298 }
284299 }
300+
301+ t .Fatalf ("Test was not complete (%d round): timeout %d seconds. nodes=%v" , round , iterations * cycle / 1000 , nodes )
285302}
286303
287- func validateMail (t * testing.T , index int , mail []* ReceivedMessage ) bool {
304+ func validateMail (t * testing.T , index int , mail []* ReceivedMessage ) {
288305 var cnt int
289306 for _ , m := range mail {
290307 if bytes .Equal (m .Payload , expectedMessage ) {
@@ -294,22 +311,42 @@ func validateMail(t *testing.T, index int, mail []*ReceivedMessage) bool {
294311
295312 if cnt == 0 {
296313 // no messages received yet: nothing is wrong
297- return true
314+ return
298315 }
299316 if cnt > 1 {
300317 t .Fatalf ("node %d received %d." , index , cnt )
301- return false
302318 }
303319
304- if cnt > 0 {
320+ if cnt == 1 {
305321 result .mutex .Lock ()
306322 defer result .mutex .Unlock ()
307323 result .counter [index ] += cnt
308324 if result .counter [index ] > 1 {
309325 t .Fatalf ("node %d accumulated %d." , index , result .counter [index ])
310326 }
311327 }
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+ }
313350}
314351
315352func isTestComplete () bool {
@@ -324,7 +361,7 @@ func isTestComplete() bool {
324361
325362 for i := 0 ; i < NumNodes ; i ++ {
326363 envelopes := nodes [i ].shh .Envelopes ()
327- if len (envelopes ) < 2 {
364+ if len (envelopes ) < NumNodes + 1 {
328365 return false
329366 }
330367 }
@@ -339,9 +376,10 @@ func sendMsg(t *testing.T, expected bool, id int) {
339376
340377 opt := MessageParams {KeySym : sharedKey , Topic : sharedTopic , Payload : expectedMessage , PoW : 0.00000001 , WorkTime : 1 }
341378 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 )
345383 }
346384
347385 msg , err := NewSentMessage (& opt )
@@ -459,3 +497,14 @@ func checkBloomFilterExchange(t *testing.T) {
459497 time .Sleep (50 * time .Millisecond )
460498 }
461499}
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