Skip to content

Commit 30a9939

Browse files
committed
eth/downloader: sanity test for multi peer syncs
1 parent fc7abd9 commit 30a9939

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

eth/downloader/downloader_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,37 @@ func TestThrottling(t *testing.T) {
309309
}
310310
}
311311

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+
312343
// Tests that if a peer returns an invalid chain with a block pointing to a non-
313344
// existing parent, it is correctly detected and handled.
314345
func TestNonExistingParentAttack(t *testing.T) {

0 commit comments

Comments
 (0)