Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(peer) removing banning logic #634

Merged
merged 4 commits into from
Nov 11, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/p2p/NodeList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const reputationEventWeight = {
[ReputationEvent.PacketTimeout]: -1,
[ReputationEvent.SwapFailure]: -10,
[ReputationEvent.SwapSuccess]: 1,
[ReputationEvent.InvalidMessage]: -1,
[ReputationEvent.UnknownMessageType]: -1,
[ReputationEvent.UnparsableMessage]: -1,
ImmanuelSegol marked this conversation as resolved.
Show resolved Hide resolved
};

// TODO: inform node about getting banned
Expand Down
24 changes: 8 additions & 16 deletions lib/p2p/Peer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import assert from 'assert';
import net, { Socket } from 'net';
import { EventEmitter } from 'events';
import { ReputationEvent } from '../types/enums';
import Parser, { ParserError, ParserErrorType } from './Parser';
import * as packets from './packets/types';
import Logger from '../Logger';
Expand Down Expand Up @@ -29,9 +30,9 @@ interface Peer {
on(event: 'handshake', listener: () => void): this;
once(event: 'open', listener: () => void): this;
once(event: 'close', listener: () => void): this;
once(event: 'ban', listener: () => void): this;
once(event: 'reputationEvent', listener: (event: ReputationEvent) => void): this;
ImmanuelSegol marked this conversation as resolved.
Show resolved Hide resolved
emit(event: 'connect'): boolean;
emit(event: 'ban'): boolean;
emit(event: 'reputationEvent', reputationEvent: ReputationEvent): boolean;
emit(event: 'open'): boolean;
emit(event: 'close'): boolean;
emit(event: 'error', err: Error): boolean;
Expand All @@ -53,7 +54,6 @@ class Peer extends EventEmitter {
private pingTimer?: NodeJS.Timer;
private responseMap: Map<string, PendingResponseEntry> = new Map();
private connectTime!: number;
private banScore = 0;
private lastRecv = 0;
private lastSend = 0;
private handshakeState?: HandshakeState;
Expand Down Expand Up @@ -251,16 +251,8 @@ class Peer extends EventEmitter {
}
}

private increaseBan = (score: number): boolean => {
this.banScore += score;

if (this.banScore >= 100) { // TODO: make configurable
this.logger.debug(`Ban threshold exceeded (${this.nodePubKey})`);
this.emit('ban');
return true;
}

return false;
private increaseBan = (event: ReputationEvent) => {
ImmanuelSegol marked this conversation as resolved.
Show resolved Hide resolved
this.emit('reputationEvent', event);
}

/**
Expand Down Expand Up @@ -482,15 +474,15 @@ class Peer extends EventEmitter {
switch (err.type) {
case ParserErrorType.UnparseableMessage:
this.logger.warn(`Unparsable peer message: ${err.payload}`);
this.increaseBan(10);
this.increaseBan(ReputationEvent.UnparsableMessage);
break;
case ParserErrorType.InvalidMessage:
this.logger.warn(`Invalid peer message: ${err.payload}`);
this.increaseBan(10);
this.increaseBan(ReputationEvent.InvalidMessage);
break;
case ParserErrorType.UnknownPacketType:
this.logger.warn(`Unknown peer message type: ${err.payload}`);
this.increaseBan(20);
this.increaseBan(ReputationEvent.UnknownMessageType);
}
});
}
Expand Down
9 changes: 3 additions & 6 deletions lib/p2p/Pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -647,13 +647,10 @@ class Pool extends EventEmitter {
this.emit('peer.close', peer);
});

peer.once('ban', async () => {
this.logger.debug(`Banning peer (${peer.nodePubKey})`);
peer.once('reputationEvent', async (event) => {
this.logger.debug(`Peer (${peer.nodePubKey}), received reputation event: ${ReputationEvent[event]}`);
if (peer.nodePubKey) {
await this.nodes.ban(peer.nodePubKey);
}
if (peer.connected) {
peer.close();
await this.nodes.addReputationEvent(peer.nodePubKey, event);
}
});
}
Expand Down
3 changes: 3 additions & 0 deletions lib/types/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ export enum ReputationEvent {
PacketTimeout = 2,
SwapFailure = 3,
SwapSuccess = 4,
UnparsableMessage = 5,
ImmanuelSegol marked this conversation as resolved.
Show resolved Hide resolved
InvalidMessage = 6,
UnknownMessageType = 7,
ImmanuelSegol marked this conversation as resolved.
Show resolved Hide resolved
}

export enum SwapFailureReason {
Expand Down