Skip to content

Commit

Permalink
test for dropping bad scoring peers
Browse files Browse the repository at this point in the history
  • Loading branch information
Maddiaa0 committed Dec 20, 2024
1 parent 1a21483 commit 8269106
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
36 changes: 36 additions & 0 deletions yarn-project/p2p/src/services/peer_manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,42 @@ describe('PeerManager', () => {
expect(peer2?.status).toBe('cached'); // in dial queue
});

it('should disconnect from unhealthy peers during heartbeat', async () => {
// Create two peers with different states
const bannedPeerId = await createSecp256k1PeerId();
const disconnectPeerId = await createSecp256k1PeerId();
const healthyPeerId = await createSecp256k1PeerId();

// Mock the connections to return our test peers
mockLibP2PNode.getConnections.mockReturnValue([
{ remotePeer: bannedPeerId },
{ remotePeer: disconnectPeerId },
{ remotePeer: healthyPeerId },
]);

// Set the peer scores to trigger different states
peerManager.penalizePeer(bannedPeerId, PeerErrorSeverity.LowToleranceError); // Will set score below -100
peerManager.penalizePeer(bannedPeerId, PeerErrorSeverity.LowToleranceError); // Additional penalty to ensure banned state

peerManager.penalizePeer(disconnectPeerId, PeerErrorSeverity.LowToleranceError); // Will set score between -100 and -50
peerManager.penalizePeer(disconnectPeerId, PeerErrorSeverity.HighToleranceError);

// Trigger heartbeat which should call pruneUnhealthyPeers
peerManager.heartbeat();

await sleep(100);

// Verify that hangUp was called for both unhealthy peers
expect(mockLibP2PNode.hangUp).toHaveBeenCalledWith(bannedPeerId);
expect(mockLibP2PNode.hangUp).toHaveBeenCalledWith(disconnectPeerId);

// Verify that hangUp was not called for the healthy peer
expect(mockLibP2PNode.hangUp).not.toHaveBeenCalledWith(healthyPeerId);

// Verify hangUp was called exactly twice (once for each unhealthy peer)
expect(mockLibP2PNode.hangUp).toHaveBeenCalledTimes(2);
});

it('should properly clean up peers on stop', async () => {
const enr = await createMockENR();
await discoveredPeerCallback(enr);
Expand Down
3 changes: 2 additions & 1 deletion yarn-project/p2p/src/services/peer_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ export class PeerManager extends WithTracer {
const connectedHealthyPeers: Connection[] = [];

for (const peer of connections) {
const score = this.peerScoring.getScore(peer.remotePeer.toString());
const score = this.peerScoring.getScoreState(peer.remotePeer.toString());
switch (score) {
// TODO: add goodbye and give reasons
case PeerScoreState.Banned:
Expand All @@ -242,6 +242,7 @@ export class PeerManager extends WithTracer {

// TODO: send a goodbye with a reason to the peer
private async disconnectPeer(peer: PeerId) {
this.logger.debug(`Disconnecting peer ${peer.toString()}`);
await this.libP2PNode.hangUp(peer);
}

Expand Down

0 comments on commit 8269106

Please sign in to comment.