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-json-rpc): reduce complexity and add types #2422

Merged
merged 4 commits into from Apr 15, 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
2 changes: 1 addition & 1 deletion __tests__/integration/core-json-rpc/__support__/request.ts
Expand Up @@ -2,7 +2,7 @@ import { httpie } from "@arkecosystem/core-utils";
import uuid from "uuid/v4";

export async function sendRequest(method, params: any = {}) {
const id = uuid();
const id: string = uuid();
const response = await httpie.post("http://localhost:8080/", {
body: {
jsonrpc: "2.0",
Expand Down
19 changes: 3 additions & 16 deletions __tests__/integration/core-json-rpc/__support__/setup.ts
@@ -1,36 +1,23 @@
import { app } from "@arkecosystem/core-container";
import { tmpdir } from "os";
import { registerWithContainer, setUpContainer } from "../../../utils/helpers/container";
import { setUpContainer } from "../../../utils/helpers/container";

jest.setTimeout(60000);

const options = {
enabled: true,
host: "0.0.0.0",
port: 8080,
allowRemote: false,
whitelist: ["127.0.0.1", "::ffff:127.0.0.1"],
};

export async function setUp() {
// @ts-ignore
process.env.CORE_JSON_RPC_ENABLED = true;
process.env.DISABLE_P2P_SERVER = "true"; // no need for p2p server here
process.env.CORE_PATH_CACHE = tmpdir();

await setUpContainer({
exclude: ["@arkecosystem/core-webhooks", "@arkecosystem/core-forger", "@arkecosystem/core-json-rpc"],
exclude: ["@arkecosystem/core-webhooks", "@arkecosystem/core-forger"],
exit: "@arkecosystem/core-json-rpc",
});

const { plugin } = require("../../../../packages/core-json-rpc/src");
await registerWithContainer(plugin, options);

return app;
}

export async function tearDown() {
await app.tearDown();

const { plugin } = require("../../../../packages/core-json-rpc/src");
await plugin.deregister(app, options);
}
23 changes: 6 additions & 17 deletions __tests__/integration/core-json-rpc/blocks.test.ts
@@ -1,6 +1,7 @@
import "jest-extended";

import { app } from "@arkecosystem/core-container";
import { Peer } from "@arkecosystem/core-p2p/src/peer";
import "jest-extended";
import nock from "nock";
import { sendRequest } from "./__support__/request";
import { setUp, tearDown } from "./__support__/setup";
Expand All @@ -13,32 +14,20 @@ let mockHost;
beforeAll(async () => {
await setUp();

peerMock = new Peer("1.0.0.99", 4000);
Object.assign(peerMock, peerMock.headers, { status: "OK" });
peerMock = new Peer("1.0.0.99", 4003); // @NOTE: we use the Public API port

app.resolvePlugin("p2p")
.getStorage()
.setPeer(peerMock);

nock("http://localhost", { allowUnmocked: true });

mockHost = nock("http://localhost:4003");
mockHost = nock(peerMock.url);
});

afterAll(async () => {
nock.cleanAll();
await tearDown();
});
afterAll(async () => await tearDown());

beforeEach(async () => {
nock(peerMock.url)
.get("/peer/status")
.reply(200, { success: true, height: 1 }, peerMock.headers);
});

afterEach(async () => {
nock.cleanAll();
});
afterEach(async () => nock.cleanAll());

describe("Blocks", () => {
describe("POST blocks.latest", () => {
Expand Down
25 changes: 8 additions & 17 deletions __tests__/integration/core-json-rpc/transactions.test.ts
@@ -1,7 +1,8 @@
import "jest-extended";

import { app } from "@arkecosystem/core-container";
import { Peer } from "@arkecosystem/core-p2p";
import { Crypto } from "@arkecosystem/crypto";
import "jest-extended";
import nock from "nock";
import { sendRequest } from "./__support__/request";
import { setUp, tearDown } from "./__support__/setup";
Expand All @@ -16,31 +17,20 @@ let mockHost;
beforeAll(async () => {
await setUp();

peerMock = new Peer("1.0.0.99", 4000);
Object.assign(peerMock, peerMock.headers, { status: "OK" });
peerMock = new Peer("1.0.0.99", 4003); // @NOTE: we use the Public API port

app.resolvePlugin("p2p")
.getStorage()
.setPeer(peerMock);

nock("http://localhost", { allowUnmocked: true });

mockHost = nock("http://localhost:4003");
mockHost = nock(peerMock.url);
});

afterAll(async () => {
nock.cleanAll();
await tearDown();
});
beforeEach(async () => {
nock(peerMock.url)
.get("/peer/status")
.reply(200, { success: true, height: 1 }, peerMock.headers);
});
afterAll(async () => await tearDown());

afterEach(async () => {
nock.cleanAll();
});
afterEach(async () => nock.cleanAll());

describe("Transactions", () => {
describe("POST transactions.info", () => {
Expand Down Expand Up @@ -118,9 +108,10 @@ describe("Transactions", () => {

describe("POST transactions.bip38.create", () => {
it("should create a new transaction", async () => {
const userId = require("crypto")
const userId: string = require("crypto")
.randomBytes(32)
.toString("hex");

await sendRequest("wallets.bip38.create", {
bip38: "this is a top secret passphrase",
userId,
Expand Down
45 changes: 13 additions & 32 deletions __tests__/integration/core-json-rpc/wallets.test.ts
Expand Up @@ -14,54 +14,26 @@ let mockHost;
beforeAll(async () => {
await setUp();

peerMock = new Peer("1.0.0.99", 4000);
peerMock = new Peer("1.0.0.99", 4003); // @NOTE: we use the Public API port

app.resolvePlugin("p2p")
.getStorage()
.setPeer(peerMock);

nock("http://localhost", { allowUnmocked: true });

mockHost = nock("http://localhost:4003");
mockHost = nock(peerMock.url);
});

afterAll(async () => {
nock.cleanAll();
await tearDown();
});
afterAll(async () => await tearDown());

beforeEach(async () => {
nock(peerMock.url)
.get("/api/loader/autoconfigure")
.reply(200, { network: {} }, peerMock.headers);

nock(peerMock.url)
.get("/peer/status")
.reply(200, { success: true, height: 5 }, peerMock.headers);

nock(peerMock.url)
.get("/peer/list")
.reply(
200,
{
success: true,
peers: [
{
status: "OK",
ip: peerMock.ip,
port: 4002,
height: 5,
latency: 8,
},
],
},
peerMock.headers,
);
});

afterEach(async () => {
nock.cleanAll();
});
afterEach(async () => nock.cleanAll());

describe("Wallets", () => {
describe("POST wallets.info", () => {
Expand Down Expand Up @@ -118,6 +90,15 @@ describe("Wallets", () => {
});

it("should fail to get transactions for the given wallet", async () => {
mockHost
.get("/api/transactions")
.query({
offset: 0,
orderBy: "timestamp:desc",
ownerId: "AUDud8tvyVZa67p3QY7XPRUTjRGnWQQ9Xv",
})
.reply(200, { meta: { totalCount: 0 }, data: [] }, peerMock.headers);

const response = await sendRequest("wallets.transactions", {
address: "AUDud8tvyVZa67p3QY7XPRUTjRGnWQQ9Xv",
});
Expand Down
22 changes: 22 additions & 0 deletions packages/core-json-rpc/src/interfaces.ts
@@ -0,0 +1,22 @@
import { Interfaces } from "@arkecosystem/crypto";

export interface IResponse<T> {
jsonrpc: "2.0";
id: string | number;
result: T;
}

export interface IResponseError {
jsonrpc: "2.0";
id: string | number;
error: {
code: number;
message: string;
data: string;
};
}

export interface IWallet {
keys: Interfaces.IKeyPair;
wif: string;
}
16 changes: 12 additions & 4 deletions packages/core-json-rpc/src/server/index.ts
@@ -1,7 +1,7 @@
import { app } from "@arkecosystem/core-container";
import { createServer, mountServer, plugins } from "@arkecosystem/core-http-utils";
import { Logger } from "@arkecosystem/core-interfaces";
import { registerMethods } from "./methods";
import * as modules from "./modules";
import { Processor } from "./services/processor";

export async function startServer(options) {
Expand Down Expand Up @@ -29,13 +29,21 @@ export async function startServer(options) {
});
}

// @ts-ignore
registerMethods(server);
for (const module of Object.values(modules)) {
for (const method of Object.values(module)) {
// @ts-ignore
server.app.schemas[method.name] = method.schema;

delete method.schema;

server.method(method);
}
}

server.route({
method: "POST",
path: "/",
async handler(request, h) {
async handler(request) {
const processor = new Processor();

return Array.isArray(request.payload)
Expand Down
18 changes: 0 additions & 18 deletions packages/core-json-rpc/src/server/methods/blocks/info.ts

This file was deleted.

11 changes: 0 additions & 11 deletions packages/core-json-rpc/src/server/methods/blocks/latest.ts

This file was deleted.

27 changes: 0 additions & 27 deletions packages/core-json-rpc/src/server/methods/blocks/transactions.ts

This file was deleted.

40 changes: 0 additions & 40 deletions packages/core-json-rpc/src/server/methods/index.ts

This file was deleted.