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: add more interfaces and types #2415

Merged
merged 19 commits into from
Apr 14, 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
14 changes: 8 additions & 6 deletions __tests__/integration/core-forger/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ beforeAll(async () => {
socketManager = new MockSocketManager();
await socketManager.init();

client = new Client({
port: 4009,
ip: "127.0.0.1",
});
client = new Client([
{
hostname: "127.0.0.1",
port: 4009,
},
]);

await delay(1000);
});
Expand All @@ -48,12 +50,12 @@ describe("Client", () => {
it("should accept multiple hosts as constructor parameter", () => {
const hosts = [
{
hostname: "127.0.0.1",
port: 4000,
ip: "127.0.0.1",
},
{
hostname: "127.0.0.2",
port: 4000,
ip: "127.0.0.2",
},
];

Expand Down
3 changes: 3 additions & 0 deletions __tests__/integration/core-vote-report/__support__/setup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { app } from "@arkecosystem/core-container";
import { asValue } from "awilix";
import { defaults } from "../../../../packages/core-vote-report/src/defaults";
import { startServer } from "../../../../packages/core-vote-report/src/server";
import { setUpContainer } from "../../../utils/helpers/container";
Expand All @@ -11,6 +12,8 @@ export async function setUp() {
exit: "@arkecosystem/core-blockchain",
});

app.register("pkg.vote-report.opts", asValue(defaults));

server = await startServer(defaults);
}

Expand Down
12 changes: 4 additions & 8 deletions __tests__/integration/core-vote-report/server.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import got from "got";
import "jest-extended";
import { setUp, tearDown } from "./__support__/setup";

beforeAll(async () => {
await setUp();
});
import got from "got";
import { setUp, tearDown } from "./__support__/setup";

afterAll(async () => {
await tearDown();
});
beforeAll(async () => await setUp());
afterAll(async () => await tearDown());

describe("Server", () => {
it("should render the page", async () => {
Expand Down
4 changes: 2 additions & 2 deletions __tests__/unit/core-forger/manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ afterAll(async () => {
});

beforeEach(() => {
defaults.hosts = [{ ip: "127.0.0.1", port: 4000 }];
defaults.hosts = [{ hostname: "127.0.0.1", port: 4000 }];
forgeManager = new ForgerManager(defaults);
});

Expand Down Expand Up @@ -82,7 +82,7 @@ describe("Forger Manager", () => {
it("should emit failed event if error while monitoring", async () => {
forgeManager.client.getRound.mockRejectedValue(new Error("oh bollocks"));

setTimeout(() => forgeManager.stop(), 1000);
setTimeout(() => forgeManager.stopForging(), 1000);
await forgeManager.monitor();

expect(forgeManager.client.emitEvent).toHaveBeenCalledWith("forger.failed", "oh bollocks");
Expand Down
8 changes: 4 additions & 4 deletions __tests__/unit/core-utils/capped-set.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import "jest-extended";

import "./mocks/core-container";

import { app } from "@arkecosystem/core-container";
import "jest-extended";
import { CappedSet } from "../../../packages/core-utils/src/capped-set";

describe("CappedSet", () => {
it("basic", () => {
const cappedSet = new CappedSet();
const cappedSet = new CappedSet<number>();

cappedSet.add(20);

Expand All @@ -16,7 +16,7 @@ describe("CappedSet", () => {

it("overflow", () => {
const maxSize = 10;
const cappedSet = new CappedSet(maxSize);
const cappedSet = new CappedSet<number>(maxSize);

for (let i = 0; i < 15; i++) {
cappedSet.add(i);
Expand Down
39 changes: 27 additions & 12 deletions __tests__/unit/core-webhooks/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,29 @@ import "jest-extended";
process.env.CORE_PATH_CACHE = process.env.HOME;

import { database } from "../../../packages/core-webhooks/src/database";
import { IWebhook } from "../../../packages/core-webhooks/src/interfaces";

const dummyWebhook: IWebhook = {
id: "id",
token: "token",
event: "event",
target: "target",
enabled: true,
conditions: [
{
key: "key",
value: "value",
condition: "condition",
},
],
};

beforeEach(() => database.make());
afterEach(() => database.reset());

describe("Conditions - between", () => {
it("should paginate all webhooks", () => {
database.create({ hello: "world" });
database.create(dummyWebhook);

const { count, rows } = database.paginate({ offset: 0, limit: 1 });

Expand All @@ -18,44 +34,43 @@ describe("Conditions - between", () => {
});

it("should find a webhook by its id", () => {
const webhook = database.create({ hello: "world" });
const webhook = database.create(dummyWebhook);

expect(database.findById(webhook.id)).toEqual(webhook);
});

it("should find webhooks by their event", () => {
const webhook = database.create({ hello: "world", event: "dummy" });
database.create({ hello: "world", event: "dummy" });
const webhook: IWebhook = database.create(dummyWebhook);

const { count, rows } = database.findByEvent("dummy");
const { count, rows } = database.findByEvent("event");

expect(count).toBe(2);
expect(rows).toHaveLength(2);
expect(count).toBe(1);
expect(rows).toHaveLength(1);
expect(rows[0]).toEqual(webhook);
});

it("should return an empty array if there are no webhooks for an event", () => {
const { count, rows } = database.findByEvent("dummy");
const { count, rows } = database.findByEvent("event");

expect(count).toBe(0);
expect(rows).toHaveLength(0);
});

it("should create a new webhook", () => {
const webhook = database.create({ hello: "world" });
const webhook: IWebhook = database.create(dummyWebhook);

expect(database.create(webhook)).toEqual(webhook);
});

it("should update an existing webhook", () => {
const webhook = database.create({ hello: "world" });
const updated = database.update(webhook.id, { world: "hello" });
const webhook: IWebhook = database.create(dummyWebhook);
const updated: IWebhook = database.update(webhook.id, dummyWebhook);

expect(database.findById(webhook.id)).toEqual(updated);
});

it("should delete an existing webhook", () => {
const webhook = database.create({ hello: "world" });
const webhook: IWebhook = database.create(dummyWebhook);

expect(database.findById(webhook.id)).toEqual(webhook);

Expand Down
7 changes: 1 addition & 6 deletions __tests__/utils/config/testnet/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,7 @@ module.exports = {
whitelist: ["127.0.0.1", "::ffff:127.0.0.1"],
},
},
"@arkecosystem/core-forger": {
hosts: [{
ip: "127.0.0.1",
port: process.env.CORE_P2P_PORT || 4000,
}, ],
},
"@arkecosystem/core-forger": {},
"@arkecosystem/core-json-rpc": {
enabled: process.env.CORE_JSON_RPC_ENABLED,
host: process.env.CORE_JSON_RPC_HOST || "0.0.0.0",
Expand Down
7 changes: 1 addition & 6 deletions __tests__/utils/config/unitnet/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,7 @@ module.exports = {
whitelist: ["127.0.0.1", "::ffff:127.0.0.1"],
},
},
"@arkecosystem/core-forger": {
hosts: [{
ip: "127.0.0.1",
port: process.env.CORE_P2P_PORT || 4000,
}, ],
},
"@arkecosystem/core-forger": {},
"@arkecosystem/core-json-rpc": {
enabled: process.env.CORE_JSON_RPC_ENABLED,
host: process.env.CORE_JSON_RPC_HOST || "0.0.0.0",
Expand Down
35 changes: 19 additions & 16 deletions packages/core-blockchain/src/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,8 @@ export class Blockchain implements blockchain.IBlockchain {
)} from ${remoteAddress}`,
);

const currentSlot = Crypto.slots.getSlotNumber();
const receivedSlot = Crypto.slots.getSlotNumber(block.timestamp);
const currentSlot: number = Crypto.slots.getSlotNumber();
const receivedSlot: number = Crypto.slots.getSlotNumber(block.timestamp);

if (receivedSlot > currentSlot) {
logger.info(`Discarded block ${block.height.toLocaleString()} because it takes a future slot.`);
Expand All @@ -268,7 +268,7 @@ export class Blockchain implements blockchain.IBlockchain {
/**
* Enqueue blocks in process queue and set last downloaded block to last item in list.
*/
public enqueueBlocks(blocks: any[]): void {
public enqueueBlocks(blocks: Interfaces.IBlockData[]): void {
if (blocks.length === 0) {
return;
}
Expand All @@ -287,11 +287,14 @@ export class Blockchain implements blockchain.IBlockchain {

// If the current chain height is H and we will be removing blocks [N, H],
// then blocksToRemove[] will contain blocks [N - 1, H - 1].
const blocksToRemove = await this.database.getBlocks(this.state.getLastBlock().data.height - nblocks, nblocks);
const blocksToRemove: Interfaces.IBlockData[] = await this.database.getBlocks(
this.state.getLastBlock().data.height - nblocks,
nblocks,
);

const revertLastBlock = async () => {
// tslint:disable-next-line:no-shadowed-variable
const lastBlock = this.state.getLastBlock();
const lastBlock: Interfaces.IBlock = this.state.getLastBlock();

// TODO: if revertBlock Failed, it might corrupt the database because one block could be left stored
await this.database.revertBlock(lastBlock);
Expand Down Expand Up @@ -319,12 +322,13 @@ export class Blockchain implements blockchain.IBlockchain {
await __removeBlocks(numberOfBlocks - 1);
};

const lastBlock = this.state.getLastBlock();
const lastBlock: Interfaces.IBlock = this.state.getLastBlock();

if (nblocks >= lastBlock.data.height) {
nblocks = lastBlock.data.height - 1;
}

const resetHeight = lastBlock.data.height - nblocks;
const resetHeight: number = lastBlock.data.height - nblocks;
logger.info(`Removing ${pluralize("block", nblocks, true)}. Reset to height ${resetHeight.toLocaleString()}`);

this.state.lastDownloadedBlock = lastBlock;
Expand All @@ -344,7 +348,7 @@ export class Blockchain implements blockchain.IBlockchain {
* @return {void}
*/
public async removeTopBlocks(count: number): Promise<void> {
const blocks = await this.database.getTopBlocks(count);
const blocks: Interfaces.IBlockData[] = await this.database.getTopBlocks(count);

logger.info(
`Removing ${pluralize(
Expand All @@ -354,11 +358,9 @@ export class Blockchain implements blockchain.IBlockchain {
)} from height ${(blocks[0] as any).height.toLocaleString()}`,
);

for (let block of blocks) {
block = Block.fromData(block);

this.database.enqueueDeleteRound(block.data.height);
this.database.enqueueDeleteBlock(block);
for (const block of blocks) {
this.database.enqueueDeleteRound(block.height);
this.database.enqueueDeleteBlock(Block.fromData(block));
}

await this.database.commitQueuedQueries();
Expand All @@ -369,11 +371,12 @@ export class Blockchain implements blockchain.IBlockchain {
* Process the given block.
*/
public async processBlock(block: Interfaces.IBlock, callback): Promise<any> {
const result = await this.blockProcessor.process(block);
const result: BlockProcessorResult = await this.blockProcessor.process(block);

if (result === BlockProcessorResult.Accepted || result === BlockProcessorResult.DiscardedButCanBeBroadcasted) {
// broadcast only current block
const blocktime = config.getMilestone(block.data.height).blocktime;
const blocktime: number = config.getMilestone(block.data.height).blocktime;

if (this.state.started && Crypto.slots.getSlotNumber() * blocktime <= block.data.timestamp) {
this.p2p.getMonitor().broadcastBlock(block);
}
Expand Down Expand Up @@ -419,7 +422,7 @@ export class Blockchain implements blockchain.IBlockchain {
* @return {Object}
*/
public getUnconfirmedTransactions(blockSize: number): { transactions: string[]; poolSize: number; count: number } {
const transactions = this.transactionPool.getTransactionsForForging(blockSize);
const transactions: string[] = this.transactionPool.getTransactionsForForging(blockSize);

return {
transactions,
Expand Down
Loading