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(core-p2p): remove peer.nethash property #2553

Merged
merged 4 commits into from May 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -37,7 +37,6 @@ export class MockSocketManager {
headers: headers || {
version: "2.2.1",
port: 4000,
nethash: "a63b5a3858afbca23edefac885be74d59f1a26985548a4082f4f479e74fcc348",
height: 1,
"Content-Type": "application/json",
},
Expand Down
1 change: 0 additions & 1 deletion __tests__/integration/core-p2p/__support__/utils.ts
Expand Up @@ -5,7 +5,6 @@ class Helpers {
public headers: any;
constructor() {
this.headers = {
nethash: "d9acd04bde4234a81addb8482333b4ac906bed7be5a9970ce8ada428bd083192",
port: 4000,
version: "2.0.0",
};
Expand Down
1 change: 0 additions & 1 deletion __tests__/integration/core-p2p/peer-communicator.test.ts
Expand Up @@ -30,7 +30,6 @@ beforeEach(() => {
({ communicator, storage } = createPeerService());

stubPeer = createStubPeer({ ip: "127.0.0.1", port: 4009 });
stubPeer.nethash = "a63b5a3858afbca23edefac885be74d59f1a26985548a4082f4f479e74fcc348";
storage.setPeer(stubPeer);
});

Expand Down
Expand Up @@ -22,7 +22,6 @@ let emit;
const headers = {
version: "2.1.0",
port: "4009",
nethash: "a63b5a3858afbca23edefac885be74d59f1a26985548a4082f4f479e74fcc348",
height: 1,
"Content-Type": "application/json",
};
Expand Down
2 changes: 0 additions & 2 deletions __tests__/unit/core-p2p/peer-guard.test.ts
Expand Up @@ -21,7 +21,6 @@ beforeAll(async () => {
beforeEach(async () => {
peerMock = createStubPeer({ ip: "1.0.0.99", port: 4002 });
Object.assign(peerMock, peerMock.headers);
peerMock.nethash = "a63b5a3858afbca23edefac885be74d59f1a26985548a4082f4f479e74fcc348";
});

describe("PeerGuard", () => {
Expand Down Expand Up @@ -57,7 +56,6 @@ describe("PeerGuard", () => {

const dummy = createStubPeer({
ip: "dummy-ip-addr",
nethash: "a63b5a3858afbca23edefac885be74d59f1a26985548a4082f4f479e74fcc348",
version: "2.1.1",
status: 200,
state: {},
Expand Down
2 changes: 0 additions & 2 deletions __tests__/unit/core-p2p/peer.test.ts
Expand Up @@ -20,11 +20,9 @@ describe("Peer", () => {

it("should set and get the headers", () => {
stubPeer.setHeaders({
nethash: "nethash",
version: "version",
});

expect(stubPeer.nethash).toEqual("nethash");
expect(stubPeer.version).toEqual("version");
});
});
2 changes: 0 additions & 2 deletions packages/core-forger/src/client.ts
Expand Up @@ -14,11 +14,9 @@ export class Client {
private headers: {
version: string;
port: number;
nethash: string;
"Content-Type": "application/json";
} = {
version: app.getVersion(),
nethash: app.getConfig().get("network.nethash"),
port: undefined,
"Content-Type": "application/json",
};
Expand Down
1 change: 0 additions & 1 deletion packages/core-interfaces/src/core-p2p/peer-guard.ts
Expand Up @@ -6,7 +6,6 @@ export interface IPeerGuard {
analyze(peer: IPeer): IPunishment;
isWhitelisted(peer: IPeer): boolean;
isValidVersion(peer: IPeer): boolean;
isValidNetwork(peer: IPeer): boolean;
isValidPort(peer: IPeer): boolean;
}

Expand Down
2 changes: 0 additions & 2 deletions packages/core-interfaces/src/core-p2p/peer.ts
Expand Up @@ -7,7 +7,6 @@ export interface IPeer {
ip: string;
port: number;

nethash: string;
version: string;

latency: number;
Expand All @@ -28,7 +27,6 @@ export interface IPeer {
export interface IPeerBroadcast {
ip: string;
port: number;
nethash: string;
version: string;
height: number;
latency: number;
Expand Down
2 changes: 1 addition & 1 deletion packages/core-p2p/src/peer-communicator.ts
Expand Up @@ -109,7 +109,7 @@ export class PeerCommunicator implements P2P.IPeerCommunicator {
}

private parseHeaders(peer: P2P.IPeer, response): void {
for (const key of ["nethash", "version"]) {
for (const key of ["version"]) {
peer[key] = response.headers[key] || peer[key];
}

Expand Down
10 changes: 0 additions & 10 deletions packages/core-p2p/src/peer-guard.ts
Expand Up @@ -86,10 +86,6 @@ export class PeerGuard implements P2P.IPeerGuard {
return this.createPunishment(this.offences.highLatency);
}

if (!this.isValidNetwork(peer)) {
return this.createPunishment(this.offences.invalidNetwork);
}

if (!this.isValidVersion(peer)) {
return this.createPunishment(this.offences.invalidVersion);
}
Expand All @@ -113,12 +109,6 @@ export class PeerGuard implements P2P.IPeerGuard {
.minimumVersions.some((minimumVersion: string) => semver.satisfies(version, minimumVersion));
}

public isValidNetwork(peer: P2P.IPeer): boolean {
const nethash = peer.nethash || (peer.headers && peer.headers.nethash);

return nethash === app.getConfig().get("network.nethash");
}

public isValidPort(peer: P2P.IPeer): boolean {
return peer.port === app.resolveOptions("p2p").port;
}
Expand Down
11 changes: 0 additions & 11 deletions packages/core-p2p/src/peer-processors.ts
Expand Up @@ -13,7 +13,6 @@ export class PeerProcessor implements P2P.IPeerProcessor {
public server: any;
public nextUpdateNetworkStatusScheduled: boolean;

private readonly appConfig = app.getConfig();
private readonly logger: Logger.ILogger = app.resolvePlugin<Logger.ILogger>("logger");
private readonly emitter: EventEmitter.EventEmitter = app.resolvePlugin<EventEmitter.EventEmitter>("event-emitter");

Expand Down Expand Up @@ -69,16 +68,6 @@ export class PeerProcessor implements P2P.IPeerProcessor {
return false;
}

if (!this.guard.isValidNetwork(peer) && !options.seed) {
this.logger.debug(
`Rejected peer ${peer.ip} as it isn't on the same network. Expected: ${this.appConfig.get(
"network.nethash",
)} - Received: ${peer.nethash}`,
);

return false;
}

if (
this.storage.getSameSubnetPeers(peer.ip).length >= app.resolveOptions("p2p").maxSameSubnetPeers &&
!options.seed
Expand Down
5 changes: 1 addition & 4 deletions packages/core-p2p/src/peer.ts
Expand Up @@ -4,7 +4,6 @@ import { Dato, dato } from "@faustbrian/dato";
import { PeerVerificationResult } from "./peer-verifier";

export class Peer implements P2P.IPeer {
public nethash: string;
public version: string;
public latency: number;
public headers: Record<string, string | number>;
Expand All @@ -21,7 +20,6 @@ export class Peer implements P2P.IPeer {
this.headers = {
version: app.getVersion(),
port,
nethash: app.getConfig().get("network.nethash"),
height: undefined,
"Content-Type": "application/json",
};
Expand All @@ -32,7 +30,7 @@ export class Peer implements P2P.IPeer {
}

public setHeaders(headers: Record<string, string>): void {
for (const key of ["nethash", "version"]) {
for (const key of ["version"]) {
this[key] = headers[key] || this[key];
}
}
Expand All @@ -53,7 +51,6 @@ export class Peer implements P2P.IPeer {
return {
ip: this.ip,
port: +this.port,
nethash: this.nethash,
version: this.version,
height: this.state.height,
latency: this.latency,
Expand Down
1 change: 0 additions & 1 deletion packages/core-p2p/src/socket-server/utils/get-headers.ts
Expand Up @@ -3,7 +3,6 @@ import { Blockchain } from "@arkecosystem/core-interfaces";

export const getHeaders = () => {
const headers = {
nethash: app.getConfig().get("network.nethash"),
version: app.getVersion(),
port: app.resolveOptions("p2p").port,
height: undefined,
Expand Down
Expand Up @@ -17,15 +17,11 @@ export const validateHeaders = headers => {
minimum: 1,
maximum: 65535,
},
nethash: {
type: "string",
maxLength: 64,
},
version: {
type: "string",
maxLength: 16,
},
},
required: ["version", "nethash", "port"],
required: ["version", "port"],
});
};
2 changes: 1 addition & 1 deletion packages/core-p2p/src/socket-server/versions/peer.ts
Expand Up @@ -10,7 +10,7 @@ import { InvalidTransactionsError, UnchainedBlockError } from "../errors";
export const acceptNewPeer = async ({ service, req }: { service: P2P.IPeerService; req }): Promise<void> => {
const peer = { ip: req.data.ip };

for (const key of ["nethash", "version", "port"]) {
for (const key of ["version", "port"]) {
peer[key] = req.headers[key];
}

Expand Down