Skip to content

Commit b8d006b

Browse files
chore: refactor ConnectionManager for readability & maintainability (#1658)
* restructure shouldDialPeer for readability & maintainability: * fix return & add logs * fix: check on dialPeer instead of attemptDial * rm: console log
1 parent e6527e9 commit b8d006b

File tree

2 files changed

+69
-47
lines changed

2 files changed

+69
-47
lines changed

packages/core/src/lib/connection_manager.ts

+63-34
Original file line numberDiff line numberDiff line change
@@ -314,13 +314,13 @@ export class ConnectionManager
314314
}
315315

316316
private async attemptDial(peerId: PeerId): Promise<void> {
317+
if (!(await this.shouldDialPeer(peerId))) return;
318+
317319
if (this.currentActiveDialCount >= this.options.maxParallelDials) {
318320
this.pendingPeerDialQueue.push(peerId);
319321
return;
320322
}
321323

322-
if (!(await this.shouldDialPeer(peerId))) return;
323-
324324
this.dialPeer(peerId).catch((err) => {
325325
log(`Error dialing peer ${peerId.toString()} : ${err}`);
326326
});
@@ -331,34 +331,7 @@ export class ConnectionManager
331331
void (async () => {
332332
const { id: peerId } = evt.detail;
333333

334-
if (!(await this.isPeerTopicConfigured(peerId))) {
335-
const shardInfo = await this.getPeerShardInfo(
336-
peerId,
337-
this.libp2p.peerStore
338-
);
339-
log(
340-
`Discovered peer ${peerId.toString()} with ShardInfo ${shardInfo} is not part of any of the configured pubsub topics (${
341-
this.configuredPubSubTopics
342-
}).
343-
Not dialing.`
344-
);
345-
return;
346-
}
347-
348-
const isBootstrap = (await this.getTagNamesForPeer(peerId)).includes(
349-
Tags.BOOTSTRAP
350-
);
351-
352-
this.dispatchEvent(
353-
new CustomEvent<PeerId>(
354-
isBootstrap
355-
? EPeersByDiscoveryEvents.PEER_DISCOVERY_BOOTSTRAP
356-
: EPeersByDiscoveryEvents.PEER_DISCOVERY_PEER_EXCHANGE,
357-
{
358-
detail: peerId
359-
}
360-
)
361-
);
334+
await this.dispatchDiscoveryEvent(peerId);
362335

363336
try {
364337
await this.attemptDial(peerId);
@@ -421,15 +394,54 @@ export class ConnectionManager
421394
};
422395

423396
/**
424-
* Checks if the peer is dialable based on the following conditions:
425-
* 1. If the peer is a bootstrap peer, it is only dialable if the number of current bootstrap connections is less than the max allowed.
426-
* 2. If the peer is not a bootstrap peer
397+
* Checks if the peer should be dialed based on the following conditions:
398+
* 1. If the peer is already connected, don't dial
399+
* 2. If the peer is not part of any of the configured pubsub topics, don't dial
400+
* 3. If the peer is not dialable based on bootstrap status, don't dial
401+
* @returns true if the peer should be dialed, false otherwise
427402
*/
428403
private async shouldDialPeer(peerId: PeerId): Promise<boolean> {
404+
// if we're already connected to the peer, don't dial
429405
const isConnected = this.libp2p.getConnections(peerId).length > 0;
406+
if (isConnected) {
407+
log(`Already connected to peer ${peerId.toString()}. Not dialing.`);
408+
return false;
409+
}
430410

431-
if (isConnected) return false;
411+
// if the peer is not part of any of the configured pubsub topics, don't dial
412+
if (!(await this.isPeerTopicConfigured(peerId))) {
413+
const shardInfo = await this.getPeerShardInfo(
414+
peerId,
415+
this.libp2p.peerStore
416+
);
417+
log(
418+
`Discovered peer ${peerId.toString()} with ShardInfo ${shardInfo} is not part of any of the configured pubsub topics (${
419+
this.configuredPubSubTopics
420+
}).
421+
Not dialing.`
422+
);
423+
return false;
424+
}
432425

426+
// if the peer is not dialable based on bootstrap status, don't dial
427+
if (!(await this.isPeerDialableBasedOnBootstrapStatus(peerId))) {
428+
log(
429+
`Peer ${peerId.toString()} is not dialable based on bootstrap status. Not dialing.`
430+
);
431+
return false;
432+
}
433+
434+
return true;
435+
}
436+
437+
/**
438+
* Checks if the peer is dialable based on the following conditions:
439+
* 1. If the peer is a bootstrap peer, it is only dialable if the number of current bootstrap connections is less than the max allowed.
440+
* 2. If the peer is not a bootstrap peer
441+
*/
442+
private async isPeerDialableBasedOnBootstrapStatus(
443+
peerId: PeerId
444+
): Promise<boolean> {
433445
const tagNames = await this.getTagNamesForPeer(peerId);
434446

435447
const isBootstrap = tagNames.some((tagName) => tagName === Tags.BOOTSTRAP);
@@ -449,6 +461,23 @@ export class ConnectionManager
449461
return false;
450462
}
451463

464+
private async dispatchDiscoveryEvent(peerId: PeerId): Promise<void> {
465+
const isBootstrap = (await this.getTagNamesForPeer(peerId)).includes(
466+
Tags.BOOTSTRAP
467+
);
468+
469+
this.dispatchEvent(
470+
new CustomEvent<PeerId>(
471+
isBootstrap
472+
? EPeersByDiscoveryEvents.PEER_DISCOVERY_BOOTSTRAP
473+
: EPeersByDiscoveryEvents.PEER_DISCOVERY_PEER_EXCHANGE,
474+
{
475+
detail: peerId
476+
}
477+
)
478+
);
479+
}
480+
452481
/**
453482
* Fetches the tag names for a given peer
454483
*/

packages/tests/tests/sharding/peer_management.spec.ts

+6-13
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe("Static Sharding: Peer Management", function () {
2020
let nwaku2: NimGoNode;
2121
let nwaku3: NimGoNode;
2222

23-
let attemptDialSpy: SinonSpy;
23+
let dialPeerSpy: SinonSpy;
2424

2525
beforeEach(async function () {
2626
this.timeout(15000);
@@ -32,7 +32,7 @@ describe("Static Sharding: Peer Management", function () {
3232
afterEach(async function () {
3333
this.timeout(15000);
3434
await tearDownNodes([nwaku1, nwaku2, nwaku3], waku);
35-
attemptDialSpy && attemptDialSpy.restore();
35+
dialPeerSpy && dialPeerSpy.restore();
3636
});
3737

3838
it("all px service nodes subscribed to the shard topic should be dialed", async function () {
@@ -80,10 +80,7 @@ describe("Static Sharding: Peer Management", function () {
8080

8181
await waku.start();
8282

83-
attemptDialSpy = Sinon.spy(
84-
(waku as any).connectionManager,
85-
"attemptDial"
86-
);
83+
dialPeerSpy = Sinon.spy((waku as any).connectionManager, "dialPeer");
8784

8885
const pxPeersDiscovered = new Set<PeerId>();
8986

@@ -105,7 +102,7 @@ describe("Static Sharding: Peer Management", function () {
105102

106103
await delay(1000);
107104

108-
expect(attemptDialSpy.callCount).to.equal(3);
105+
expect(dialPeerSpy.callCount).to.equal(3);
109106
});
110107

111108
it("px service nodes not subscribed to the shard should not be dialed", async function () {
@@ -151,10 +148,7 @@ describe("Static Sharding: Peer Management", function () {
151148
}
152149
});
153150

154-
attemptDialSpy = Sinon.spy(
155-
(waku as any).connectionManager,
156-
"attemptDial"
157-
);
151+
dialPeerSpy = Sinon.spy((waku as any).connectionManager, "dialPeer");
158152

159153
await waku.start();
160154

@@ -177,8 +171,7 @@ describe("Static Sharding: Peer Management", function () {
177171
});
178172

179173
await delay(1000);
180-
181-
expect(attemptDialSpy.callCount).to.equal(2);
174+
expect(dialPeerSpy.callCount).to.equal(2);
182175
});
183176
});
184177
});

0 commit comments

Comments
 (0)