Skip to content

Commit 601e511

Browse files
committed
refactor(core-p2p): Use acceptNewPeer() from discoverPeers()
acceptNewPeer() will call ping(), thus avoiding a peers without a state in the list of peers. After this change addPeer() is unused and has been removed.
1 parent 0f36254 commit 601e511

File tree

3 files changed

+2
-47
lines changed

3 files changed

+2
-47
lines changed

packages/core-p2p/__tests__/court/guard.test.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { defaults } from "../../src/defaults";
55
import { Peer } from "../../src/peer";
66
import { setUp, tearDown } from "../__support__/setup";
77

8-
const CORE_ENV = process.env.CORE_ENV;
98
let guard;
109
let peerMock;
1110

@@ -35,18 +34,14 @@ beforeEach(async () => {
3534
describe("Guard", () => {
3635
describe("isSuspended", () => {
3736
it("should return true", async () => {
38-
process.env.CORE_ENV = "false";
3937
await guard.monitor.acceptNewPeer(peerMock);
40-
process.env.CORE_ENV = CORE_ENV;
4138

4239
expect(guard.isSuspended(peerMock)).toBe(true);
4340
});
4441

4542
it("should return false because passed", async () => {
46-
process.env.CORE_ENV = "false";
4743
await guard.monitor.acceptNewPeer(peerMock);
4844
guard.suspensions[peerMock.ip].until = dayjs().subtract(1, "minute");
49-
process.env.CORE_ENV = CORE_ENV;
5045

5146
expect(guard.isSuspended(peerMock)).toBe(false);
5247
});

packages/core-p2p/__tests__/monitor.test.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,9 @@ describe("Monitor", () => {
7171
peerMock.headers,
7272
]);
7373

74-
process.env.CORE_ENV = "false";
75-
7674
await monitor.acceptNewPeer(peerMock);
7775

7876
expect(monitor.peers[peerMock.ip]).toBeObject();
79-
80-
process.env.CORE_ENV = "test";
8177
});
8278
});
8379

packages/core-p2p/src/monitor.ts

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,7 @@ export class Monitor implements P2P.IMonitor {
140140
return;
141141
}
142142

143-
if (
144-
!isValidPeer(peer) ||
145-
this.guard.isSuspended(peer) ||
146-
this.pendingPeers[peer.ip] ||
147-
process.env.CORE_ENV === "test"
148-
) {
143+
if (!isValidPeer(peer) || this.guard.isSuspended(peer) || this.pendingPeers[peer.ip]) {
149144
return;
150145
}
151146

@@ -390,9 +385,7 @@ export class Monitor implements P2P.IMonitor {
390385
const hisPeers = await peer.getPeers();
391386

392387
for (const p of hisPeers) {
393-
if (isValidPeer(p) && !this.getPeer(p.ip)) {
394-
this.addPeer(p);
395-
}
388+
this.acceptNewPeer(p);
396389
}
397390
} catch (error) {
398391
// Just try with the next peer from shuffledPeers.
@@ -769,35 +762,6 @@ export class Monitor implements P2P.IMonitor {
769762
}
770763
}
771764

772-
/**
773-
* Add a new peer after it passes a few checks.
774-
* @param {Peer} peer
775-
* @return {void}
776-
*/
777-
private addPeer(peer) {
778-
if (this.guard.isBlacklisted(peer)) {
779-
return;
780-
}
781-
782-
if (!this.guard.isValidVersion(peer)) {
783-
return;
784-
}
785-
786-
if (!this.guard.isValidNetwork(peer)) {
787-
return;
788-
}
789-
790-
if (!this.guard.isValidMilestoneHash(peer)) {
791-
return;
792-
}
793-
794-
if (!this.guard.isValidPort(peer)) {
795-
return;
796-
}
797-
798-
this.peers[peer.ip] = new Peer(peer.ip, peer.port);
799-
}
800-
801765
/**
802766
* Schedule the next update network status.
803767
* @param {Number} nextUpdateInSeconds

0 commit comments

Comments
 (0)