Skip to content

Commit

Permalink
add single api namespacing
Browse files Browse the repository at this point in the history
  • Loading branch information
GregTheGreek committed May 4, 2019
1 parent 488e06d commit 5cd174b
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 32 deletions.
7 changes: 7 additions & 0 deletions src/rpc/api/interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface IApi {
/**
* Name space for API commands
*/
namespace: string;
}

3 changes: 2 additions & 1 deletion src/rpc/api/validator/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import {
BeaconBlock, bytes, bytes32, bytes48, Fork, IndexedAttestation, number64, Shard, Slot, SyncingStatus, uint64,
ValidatorDuty
} from "../../../types/index";
import {IApi} from "../interface";

export interface IValidatorApi {
export interface IValidatorApi extends IApi {
/**
* Requests that the BeaconNode identify information about its implementation in a format similar to a HTTP User-Agent field.
* @returns {Promise<bytes32>} An ASCII-encoded hex string which uniquely defines the implementation of the BeaconNode and its current software version.
Expand Down
2 changes: 2 additions & 0 deletions src/rpc/api/validator/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import {OpPool} from "../../../opPool";
import {IValidatorApi} from "./interface";

export class ValidatorApi implements IValidatorApi {
public namespace: string;
private chain: BeaconChain;
private db: DB;
private opPool: OpPool;

public constructor(opts, {chain, db, opPool}) {
this.namespace = "validator";
this.chain = chain;
this.db = db;
this.opPool = opPool;
Expand Down
2 changes: 1 addition & 1 deletion src/rpc/protocol/jsonRpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class JSONRPC {
methods[name] = api[name].bind(api);
}
}
this.jsonRpcApi.BeaconChain.expose(methods);
this.jsonRpcApi[api.namespace].expose(methods);
}

public async start(): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {assert} from "chai";
import * as request from "supertest";
import {JSONRPC} from "../../../src/rpc";
import {MockValidatorApi} from "../../utils/mocks/rpc/validator";
import HttpServer from "../../../src/rpc/transport/http";
import {generateRPCCall} from "../../utils/rpcCall";
import logger from "../../../src/logger/winston";
import {JSONRPC} from "../../../../src/rpc/index";
import {MockValidatorApi} from "../../../utils/mocks/rpc/validator";
import HttpServer from "../../../../src/rpc/transport/http";
import {generateRPCCall} from "../../../utils/rpcCall";
import logger from "../../../../src/logger/winston";

describe("Json RPC over http", () => {
let rpc;
Expand All @@ -20,10 +20,10 @@ describe("Json RPC over http", () => {
await rpc.stop();
logger.silent(false);
});
it("should get the chain head", (done) => {
it("should get the version", (done) => {
request.default(server)
.post('/')
.send(generateRPCCall('BeaconChain.getFork', []))
.send(generateRPCCall('validator.getFork', []))
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200)
Expand All @@ -38,7 +38,7 @@ describe("Json RPC over http", () => {
it("should fail for unknown methods", (done) => {
request.default(server)
.post('/')
.send(generateRPCCall('BeaconChain.notExistingMethod', []))
.send(generateRPCCall('validator.notExistingMethod', []))
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(200)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { assert } from "chai";
import * as jsonRpc from "noice-json-rpc";
import Websocket from "ws";
import {JSONRPC, IValidatorApi, WSServer} from "../../../src/rpc";
import { generateEmptyBlock } from "../../utils/block";
import {MockValidatorApi} from "../../utils/mocks/rpc/validator";
import { generateEmptyAttestation } from "../../utils/attestation";
import {JSONRPC, IValidatorApi, WSServer} from "../../../../src/rpc/index";
import { generateEmptyBlock } from "../../../utils/block";
import {MockValidatorApi} from "../../../utils/mocks/rpc/validator";
import { generateEmptyAttestation } from "../../../utils/attestation";

describe("Json RPC over WS", () => {
const rpc = new JSONRPC({}, {transport: new WSServer({port: 32420}), api: new MockValidatorApi()});
let client;
let ws;
let clientApi: {BeaconChain: IValidatorApi};
let clientApi: {validator: IValidatorApi};
before(async () => {
await rpc.start();
ws = new Websocket("ws://localhost:32420");
Expand All @@ -20,45 +20,45 @@ describe("Json RPC over WS", () => {
after(async () => {
await rpc.stop();
});
it("should get the chain version", async () => {
const head = await clientApi.BeaconChain.getClientVersion();
assert.ok(head);
it("should get the client version", async () => {
const version = await clientApi.validator.getClientVersion();
assert.ok(version);
});
it("should get the fork version", async () => {
const attestations = await clientApi.BeaconChain.getFork();
assert.ok(attestations);
const fork = await clientApi.validator.getFork();
assert.ok(fork);
});
it("should get the genesis time", async () => {
const deposits = await clientApi.BeaconChain.getGenesisTime();
assert.ok(deposits);
const time = await clientApi.validator.getGenesisTime();
assert.ok(time);
});
it("should get the sync status", async () => {
const eth1Data = await clientApi.BeaconChain.getSyncingStatus();
assert.ok(eth1Data);
const status = await clientApi.validator.getSyncingStatus();
assert.ok(status);
});
it("should get validator duties", async () => {
const root = await clientApi.BeaconChain.getDuties(Buffer.alloc(48));
assert.ok(root);
const duties = await clientApi.validator.getDuties(Buffer.alloc(48));
assert.ok(duties);
});
it("should produce a block for the validator", async () => {
const data = await clientApi.BeaconChain.produceBlock(0, Buffer.alloc(0));
assert.ok(data);
const block = await clientApi.validator.produceBlock(0, Buffer.alloc(0));
assert.ok(block);
});
it("should produce an attestation", async () => {
await clientApi.BeaconChain.produceAttestation(0,1);
await clientApi.validator.produceAttestation(0,1);
assert.ok(true);
});
it("should accept an attestation submission", async () => {
await clientApi.BeaconChain.publishAttestation(generateEmptyAttestation());
await clientApi.validator.publishAttestation(generateEmptyAttestation());
assert.ok(true);
});
it("should accept a block submission", async () => {
await clientApi.BeaconChain.publishBlock(generateEmptyBlock());
await clientApi.validator.publishBlock(generateEmptyBlock());
assert.ok(true);
});
it("should fail for unknown methods", async () => {
try {
await (clientApi.BeaconChain as any).foo();
await (clientApi.validator as any).foo();
assert.fail('Unknown/undefined method should fail');
} catch (e) {}
})
Expand Down
2 changes: 2 additions & 0 deletions test/utils/mocks/rpc/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ export interface MockAPIOpts {
}

export class MockValidatorApi implements IValidatorApi {
public namespace: string;
private version: bytes32;
private fork: Fork;
private chainId: number64;
private attestations;
private head: BeaconBlock;
public constructor(opts?: MockAPIOpts) {
this.namespace = "validator";
this.attestations = opts && opts.pendingAttestations || [];
this.head = opts && opts.head || getEmptyBlock();
this.version = opts && opts.version || Buffer.alloc(0);
Expand Down

0 comments on commit 5cd174b

Please sign in to comment.