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): don't test private methods in the peer guard #2359

Merged
merged 4 commits into from
Apr 3, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import "../mocks/core-container";
import "./mocks/core-container";

import { dato } from "@faustbrian/dato";
import { config as localConfig } from "../../../../packages/core-p2p/src/config";
import { guard } from "../../../../packages/core-p2p/src/court/guard";
import { offences } from "../../../../packages/core-p2p/src/court/offences";
import { defaults } from "../../../../packages/core-p2p/src/defaults";
import { monitor } from "../../../../packages/core-p2p/src/monitor";
import { Peer } from "../../../../packages/core-p2p/src/peer";
import { SocketErrors } from "../../../../packages/core-p2p/src/socket-server/constants";

import delay from "delay";
import "jest-extended";
import { config as localConfig } from "../../../packages/core-p2p/src/config";
import { defaults } from "../../../packages/core-p2p/src/defaults";
import { guard } from "../../../packages/core-p2p/src/guard";
import { monitor } from "../../../packages/core-p2p/src/monitor";
import { Peer } from "../../../packages/core-p2p/src/peer";
import { SocketErrors } from "../../../packages/core-p2p/src/socket-server/constants";

let peerMock;

Expand Down Expand Up @@ -97,10 +96,11 @@ describe("Guard", () => {
});
});

describe("__determineOffence", () => {
describe("suspend", () => {
const convertToMinutes = actual => Math.ceil(actual.diff(dato()) / 1000) / 60;

const dummy = {
ip: "dummy-ip-addr",
nethash: "a63b5a3858afbca23edefac885be74d59f1a26985548a4082f4f479e74fcc348",
version: "2.1.1",
status: 200,
Expand All @@ -114,109 +114,104 @@ describe("Guard", () => {
it('should return a 1 year suspension for "Blacklisted"', () => {
guard.config.set("blacklist", ["dummy-ip-addr"]);

const { until, reason } = guard.__determineOffence({
guard.suspend({
nethash: "a63b5a3858afbca23edefac885be74d59f1a26985548a4082f4f479e74fcc348",
ip: "dummy-ip-addr",
});

const { until, reason } = guard.get(dummy.ip);

expect(reason).toBe("Blacklisted");
expect(convertToMinutes(until)).toBeGreaterThanOrEqual(525600);

guard.config.set("blacklist", []);
});

it('should return a 5 minutes suspension for "No Common Blocks"', () => {
const { until, reason } = guard.__determineOffence({
guard.suspend({
...dummy,
...{
commonBlocks: false,
},
});

const { until, reason } = guard.get(dummy.ip);

expect(reason).toBe("No Common Blocks");
expect(convertToMinutes(until)).toBe(5);
});

it('should return a 5 minute suspension for "Invalid Version"', () => {
const { until, reason } = guard.__determineOffence({
nethash: "a63b5a3858afbca23edefac885be74d59f1a26985548a4082f4f479e74fcc348",
guard.suspend({
...dummy,
version: "1.0.0",
status: 200,
delay: 1000,
socket: {
OPEN: "open",
getState: () => "open",
},
});

const { until, reason } = guard.get(dummy.ip);

expect(reason).toBe("Invalid Version");
expect(convertToMinutes(until)).toBe(5);
});

it('should return a 10 minutes suspension for "Node is not at height"', () => {
guard.monitor.getNetworkHeight = jest.fn(() => 154);

const { until, reason } = guard.__determineOffence({
guard.suspend({
...dummy,
state: {
height: 1,
},
});

const { until, reason } = guard.get(dummy.ip);

expect(reason).toBe("Node is not at height");
expect(convertToMinutes(until)).toBe(10);
});

it('should return a 2 minutes suspension for "Timeout"', () => {
const { until, reason } = guard.__determineOffence({
guard.suspend({
...dummy,
...{ delay: -1 },
});

const { until, reason } = guard.get(dummy.ip);

expect(reason).toBe("Timeout");
expect(convertToMinutes(until)).toBe(2);
});

it('should return a 1 minutes suspension for "High Latency"', () => {
const { until, reason } = guard.__determineOffence({
guard.suspend({
...dummy,
...{ delay: 3000 },
});

const { until, reason } = guard.get(dummy.ip);

expect(reason).toBe("High Latency");
expect(convertToMinutes(until)).toBe(1);
});

it('should return a 30 seconds suspension for "Application not ready"', () => {
const { until, reason } = guard.__determineOffence({
guard.suspend({
...dummy,
socketError: SocketErrors.AppNotReady,
});

const { until, reason } = guard.get(dummy.ip);

expect(reason).toBe("Application is not ready");
expect(convertToMinutes(until)).toBe(0.5);
});

it('should return a 10 minutes suspension for "Unknown"', () => {
const { until, reason } = guard.__determineOffence(dummy);
guard.suspend(dummy);

const { until, reason } = guard.get(dummy.ip);

expect(reason).toBe("Unknown");
expect(convertToMinutes(until)).toBe(10);
});
});

describe("__determinePunishment", () => {
it("should be true if the threshold is met", () => {
const actual = guard.__determinePunishment({}, offences.REPEAT_OFFENDER);

expect(actual).toHaveProperty("until");
expect(actual.until).toBeObject();

expect(actual).toHaveProperty("reason");
expect(actual.reason).toBeString();

expect(actual).toHaveProperty("weight");
expect(actual.weight).toBeNumber();
});
});
});
3 changes: 3 additions & 0 deletions packages/core-interfaces/src/core-p2p/peer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { models, Transaction } from "@arkecosystem/crypto";

export interface IPeer {
readonly ip: string;
readonly port: string;

setHeaders(headers: Record<string, any>): void;

/**
Expand Down
1 change: 0 additions & 1 deletion packages/core-p2p/src/court/index.ts

This file was deleted.

102 changes: 0 additions & 102 deletions packages/core-p2p/src/court/offences.ts

This file was deleted.

Loading