diff --git a/src/rpc/api/interface.ts b/src/rpc/api/interface.ts new file mode 100644 index 000000000000..4df21dc7ec2f --- /dev/null +++ b/src/rpc/api/interface.ts @@ -0,0 +1,7 @@ +export interface IApi { + /** + * Name space for API commands + */ + namespace: string; +} + diff --git a/src/rpc/api/validator/interface.ts b/src/rpc/api/validator/interface.ts index e8d232b60b7d..919c95fd7839 100644 --- a/src/rpc/api/validator/interface.ts +++ b/src/rpc/api/validator/interface.ts @@ -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} An ASCII-encoded hex string which uniquely defines the implementation of the BeaconNode and its current software version. diff --git a/src/rpc/api/validator/validator.ts b/src/rpc/api/validator/validator.ts index 65dd22ffec77..3a60d705f18c 100644 --- a/src/rpc/api/validator/validator.ts +++ b/src/rpc/api/validator/validator.ts @@ -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; diff --git a/src/rpc/protocol/jsonRpc.ts b/src/rpc/protocol/jsonRpc.ts index 9abe171f8157..3b18918b5e07 100644 --- a/src/rpc/protocol/jsonRpc.ts +++ b/src/rpc/protocol/jsonRpc.ts @@ -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 { diff --git a/test/unit/rpc/jsonRpcOverHttp.test.ts b/test/unit/rpc/validator/jsonRpcOverHttp.test.ts similarity index 81% rename from test/unit/rpc/jsonRpcOverHttp.test.ts rename to test/unit/rpc/validator/jsonRpcOverHttp.test.ts index 2e17dfbab3ee..28bb95f93e1a 100644 --- a/test/unit/rpc/jsonRpcOverHttp.test.ts +++ b/test/unit/rpc/validator/jsonRpcOverHttp.test.ts @@ -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; @@ -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) @@ -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) diff --git a/test/unit/rpc/jsonRpcOverWs.test.ts b/test/unit/rpc/validator/jsonRpcOverWs.test.ts similarity index 50% rename from test/unit/rpc/jsonRpcOverWs.test.ts rename to test/unit/rpc/validator/jsonRpcOverWs.test.ts index 85f20df971f1..152bdc1a6e68 100644 --- a/test/unit/rpc/jsonRpcOverWs.test.ts +++ b/test/unit/rpc/validator/jsonRpcOverWs.test.ts @@ -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"); @@ -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) {} }) diff --git a/test/utils/mocks/rpc/validator.ts b/test/utils/mocks/rpc/validator.ts index 58e52bbd8453..0ee4bf1f2996 100644 --- a/test/utils/mocks/rpc/validator.ts +++ b/test/utils/mocks/rpc/validator.ts @@ -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);