@@ -309,6 +309,37 @@ func TestThrottling(t *testing.T) {
309
309
}
310
310
}
311
311
312
+ // Tests that synchronisation from multiple peers works as intended (multi thread sanity test).
313
+ func TestMultiSynchronisation (t * testing.T ) {
314
+ // Create various peers with various parts of the chain
315
+ targetPeers := 16
316
+ targetBlocks := targetPeers * blockCacheLimit - 15
317
+
318
+ hashes := createHashes (targetBlocks , knownHash )
319
+ blocks := createBlocksFromHashes (hashes )
320
+
321
+ tester := newTester ()
322
+ for i := 0 ; i < targetPeers ; i ++ {
323
+ id := fmt .Sprintf ("peer #%d" , i )
324
+ tester .newPeer (id , hashes [i * blockCacheLimit :], blocks )
325
+ }
326
+ // Synchronise with the middle peer and make sure half of the blocks were retrieved
327
+ id := fmt .Sprintf ("peer #%d" , targetPeers / 2 )
328
+ if err := tester .sync (id ); err != nil {
329
+ t .Fatalf ("failed to synchronise blocks: %v" , err )
330
+ }
331
+ if imported := len (tester .ownBlocks ); imported != len (tester .peerHashes [id ]) {
332
+ t .Fatalf ("synchronised block mismatch: have %v, want %v" , imported , len (tester .peerHashes [id ]))
333
+ }
334
+ // Synchronise with the best peer and make sure everything is retrieved
335
+ if err := tester .sync ("peer #0" ); err != nil {
336
+ t .Fatalf ("failed to synchronise blocks: %v" , err )
337
+ }
338
+ if imported := len (tester .ownBlocks ); imported != targetBlocks + 1 {
339
+ t .Fatalf ("synchronised block mismatch: have %v, want %v" , imported , targetBlocks + 1 )
340
+ }
341
+ }
342
+
312
343
// Tests that if a peer returns an invalid chain with a block pointing to a non-
313
344
// existing parent, it is correctly detected and handled.
314
345
func TestNonExistingParentAttack (t * testing.T ) {
0 commit comments