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-forger): memory leak, log spam, cleanup #2341

Merged
merged 27 commits into from
Mar 31, 2019
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
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
30 changes: 15 additions & 15 deletions __tests__/integration/core-forger/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { httpie } from "@arkecosystem/core-utils";
import "jest-extended";
import nock from "nock";
import { Client } from "../../../packages/core-forger/src/client";
import { HostNoResponseError } from "../../../packages/core-forger/src/errors";
import { sampleBlocks } from "./__fixtures__/blocks";

jest.setTimeout(30000);
Expand Down Expand Up @@ -52,7 +53,7 @@ describe("Client", () => {
return requestBody;
});

await client.__chooseHost();
await client.selectHost();

const wasBroadcasted = await client.broadcast(sampleBlock.toJson());
expect(wasBroadcasted).toBeTruthy();
Expand Down Expand Up @@ -84,7 +85,7 @@ describe("Client", () => {
.get("/internal/transactions/forging")
.reply(200, { data: expectedResponse });

await client.__chooseHost();
await client.selectHost();
const response = await client.getTransactions();

expect(response).toEqual(expectedResponse);
Expand All @@ -100,7 +101,7 @@ describe("Client", () => {
.get("/internal/network/state")
.reply(200, { data: expectedResponse });

await client.__chooseHost();
await client.selectHost();
const response = await client.getNetworkState();

expect(response).toEqual(expectedResponse);
Expand All @@ -115,23 +116,22 @@ describe("Client", () => {
.get("/internal/blockchain/sync")
.reply(200);

await client.selectHost();
await client.syncCheck();

expect(httpie.get).toHaveBeenCalledWith(`${host}/internal/blockchain/sync`, expect.any(Object));
});
});

describe("getUsernames", () => {
it("should fetch usernames", async () => {
jest.spyOn(httpie, "get");
const expectedResponse = { foo: "bar" };
nock(host)
.get("/internal/utils/usernames")
.reply(200, { data: expectedResponse });

const response = await client.getUsernames();
describe("selectHost", () => {
it("should fallback to responsive host", async () => {
client = new Client(["http://127.0.0.2:4000", "http://127.0.0.3:4000", host]);
await expect(client.selectHost()).toResolve();
});

expect(response).toEqual(expectedResponse);
it("should throw error when no host is responsive", async () => {
client = new Client(["http://127.0.0.2:4000", "http://127.0.0.3:4000"]);
await expect(client.selectHost()).rejects.toThrowError(HostNoResponseError);
});
});

Expand All @@ -145,15 +145,15 @@ describe("Client", () => {
return [200];
});

await client.__chooseHost();
await client.selectHost();
await client.emitEvent("foo", "bar");

expect(httpie.post).toHaveBeenCalledWith(`${host}/internal/utils/events`, {
body: JSON.stringify({ event: "foo", body: "bar" }),
headers: {
"Content-Type": "application/json",
nethash: {},
port: "4000",
port: 4000,
version: "2.3.0",
"x-auth": "forger",
},
Expand Down
38 changes: 8 additions & 30 deletions __tests__/unit/core-forger/manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,11 @@ describe("Forger Manager", () => {
it("should be ok with configured delegates", async () => {
const secret = "a secret";
forgeManager.secrets = [secret];
// @ts-ignore
forgeManager.client.getUsernames.mockReturnValue([]);

const delegates = await forgeManager.loadDelegates();

expect(delegates).toBeArray();
delegates.forEach(value => expect(value).toBeInstanceOf(Delegate));
expect(forgeManager.client.getUsernames).toHaveBeenCalled();
});
});

Expand Down Expand Up @@ -86,7 +83,7 @@ describe("Forger Manager", () => {

describe("__monitor", () => {
it("should emit failed event if error while monitoring", async () => {
forgeManager.client.getUsernames.mockRejectedValue(new Error("oh bollocks"));
forgeManager.client.getRound.mockRejectedValue(new Error("oh bollocks"));

setTimeout(() => forgeManager.stop(), 1000);
await forgeManager.__monitor();
Expand Down Expand Up @@ -120,31 +117,12 @@ describe("Forger Manager", () => {
});
});

describe("__isDelegateActivated", () => {
it("should be ok", async () => {
forgeManager.delegates = [
{
username: "arkxdev",
publicKey: "0310ad026647eed112d1a46145eed58b8c19c67c505a67f1199361a511ce7860c0",
},
];

const forger = await forgeManager.__isDelegateActivated(
"0310ad026647eed112d1a46145eed58b8c19c67c505a67f1199361a511ce7860c0",
);

expect(forger).toBeObject();
expect(forger.username).toBe("arkxdev");
expect(forger.publicKey).toBe("0310ad026647eed112d1a46145eed58b8c19c67c505a67f1199361a511ce7860c0");
});
});

describe("__parseNetworkState", () => {
describe("parseNetworkState", () => {
it("should be TRUE when quorum > 0.66", async () => {
const networkState = new NetworkState(NetworkStateStatus.Default);
Object.assign(networkState, { getQuorum: () => 0.9, nodeHeight: 100, lastBlockId: "1233443" });

const canForge = await forgeManager.__parseNetworkState(networkState, delegate);
const canForge = await forgeManager.parseNetworkState(networkState, delegate);

expect(canForge).toBeTrue();
});
Expand All @@ -153,7 +131,7 @@ describe("Forger Manager", () => {
const networkState = new NetworkState(NetworkStateStatus.Unknown);
Object.assign(networkState, { getQuorum: () => 1, nodeHeight: 100, lastBlockId: "1233443" });

const canForge = await forgeManager.__parseNetworkState(networkState, delegate);
const canForge = await forgeManager.parseNetworkState(networkState, delegate);

expect(canForge).toBeFalse();
});
Expand All @@ -162,21 +140,21 @@ describe("Forger Manager", () => {
const networkState = new NetworkState(NetworkStateStatus.Default);
Object.assign(networkState, { getQuorum: () => 0.65, nodeHeight: 100, lastBlockId: "1233443" });

const canForge = await forgeManager.__parseNetworkState(networkState, delegate);
const canForge = await forgeManager.parseNetworkState(networkState, delegate);

expect(canForge).toBeFalse();
});

it("should be FALSE when coldStart is active", async () => {
const networkState = new NetworkState(NetworkStateStatus.ColdStart);
const canForge = await forgeManager.__parseNetworkState(networkState, delegate);
const canForge = await forgeManager.parseNetworkState(networkState, delegate);

expect(canForge).toBeFalse();
});

it("should be FALSE when minimumNetworkReach is not sufficient", async () => {
const networkState = new NetworkState(NetworkStateStatus.BelowMinimumPeers);
const canForge = await forgeManager.__parseNetworkState(networkState, delegate);
const canForge = await forgeManager.parseNetworkState(networkState, delegate);

expect(canForge).toBeFalse();
});
Expand All @@ -200,7 +178,7 @@ describe("Forger Manager", () => {
},
});

const canForge = await forgeManager.__parseNetworkState(networkState, delegate);
const canForge = await forgeManager.parseNetworkState(networkState, delegate);
expect(canForge).toBeFalse();
});
});
Expand Down
1 change: 1 addition & 0 deletions packages/core-database/src/database-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ export class DatabaseService implements Database.IDatabaseService {

this.forgingDelegates = delegates.map(delegate => {
delegate.round = +delegate.round;
delegate.username = this.walletManager.findByPublicKey(delegate.publicKey).username;
return delegate;
});

Expand Down
3 changes: 0 additions & 3 deletions packages/core-forger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,12 @@
"@arkecosystem/core-p2p": "^2.3.0-next.3",
"@arkecosystem/core-utils": "^2.3.0-next.3",
"@arkecosystem/crypto": "^2.3.0-next.3",
"delay": "^4.1.0",
"lodash.isempty": "^4.4.0",
"lodash.sample": "^4.2.1",
"lodash.uniq": "^4.5.0",
"pluralize": "^7.0.0"
},
"devDependencies": {
"@types/lodash.isempty": "^4.4.6",
"@types/lodash.sample": "^4.2.6",
"@types/lodash.uniq": "^4.5.6",
"@types/pluralize": "^0.0.29"
},
Expand Down
Loading