From 488e06d8dc12d842fc3eb2270a3568b2b8d2288d Mon Sep 17 00:00:00 2001 From: Gregory Markou Date: Fri, 3 May 2019 19:41:35 -0400 Subject: [PATCH] update based on pr comments --- src/rpc/api/index.ts | 4 +-- src/rpc/api/interfaces/index.ts | 5 ---- src/rpc/api/{modules => validator}/index.ts | 2 ++ .../validator.ts => validator/interface.ts} | 10 ++++---- .../api/{modules => validator}/validator.ts | 14 +++++------ test/unit/rpc/jsonRpcOverHttp.test.ts | 7 +++--- test/unit/rpc/jsonRpcOverWs.test.ts | 7 +++--- .../utils/mocks/rpc}/validator.ts | 25 ++++++++++++------- 8 files changed, 39 insertions(+), 35 deletions(-) delete mode 100644 src/rpc/api/interfaces/index.ts rename src/rpc/api/{modules => validator}/index.ts (54%) rename src/rpc/api/{interfaces/validator.ts => validator/interface.ts} (88%) rename src/rpc/api/{modules => validator}/validator.ts (78%) rename {src/rpc/api/mock => test/utils/mocks/rpc}/validator.ts (67%) diff --git a/src/rpc/api/index.ts b/src/rpc/api/index.ts index f757ab1c5d80..c23d239883a8 100644 --- a/src/rpc/api/index.ts +++ b/src/rpc/api/index.ts @@ -1,3 +1 @@ -export * from "./interfaces"; -export * from "./mock/validator"; -export * from "./modules"; +export * from "./validator"; diff --git a/src/rpc/api/interfaces/index.ts b/src/rpc/api/interfaces/index.ts deleted file mode 100644 index 5677a4ad1fab..000000000000 --- a/src/rpc/api/interfaces/index.ts +++ /dev/null @@ -1,5 +0,0 @@ -import {IValidatorApi} from "./validator"; - -export { - IValidatorApi -}; diff --git a/src/rpc/api/modules/index.ts b/src/rpc/api/validator/index.ts similarity index 54% rename from src/rpc/api/modules/index.ts rename to src/rpc/api/validator/index.ts index c621d33a07cc..3e45c3457afc 100644 --- a/src/rpc/api/modules/index.ts +++ b/src/rpc/api/validator/index.ts @@ -1,5 +1,7 @@ import {ValidatorApi} from "./validator"; +import {IValidatorApi} from "./interface"; export { ValidatorApi, + IValidatorApi }; diff --git a/src/rpc/api/interfaces/validator.ts b/src/rpc/api/validator/interface.ts similarity index 88% rename from src/rpc/api/interfaces/validator.ts rename to src/rpc/api/validator/interface.ts index 6b0107449b89..e8d232b60b7d 100644 --- a/src/rpc/api/interfaces/validator.ts +++ b/src/rpc/api/validator/interface.ts @@ -5,7 +5,7 @@ import { Attestation, AttestationData, BeaconBlock, bytes, bytes32, bytes48, Fork, IndexedAttestation, number64, Shard, Slot, SyncingStatus, uint64, ValidatorDuty -} from "../../../types"; +} from "../../../types/index"; export interface IValidatorApi { /** @@ -18,7 +18,7 @@ export interface IValidatorApi { * Requests the BeaconNode to provide which fork version it is currently on. * @returns {Promise<{fork: Fork; chainId: uint64}>} */ - getFork(): Promise<{fork: Fork; chainId: number64}>; + getFork(): Promise; /** * Requests the genesis_time parameter from the BeaconNode, which should be consistent across all BeaconNodes that follow the same beacon chain. @@ -34,10 +34,10 @@ export interface IValidatorApi { /** * Requests the BeaconNode to provide a set of “duties”, which are actions that should be performed by ValidatorClients. This API call should be polled at every slot, to ensure that any chain reorganisations are catered for, and to ensure that the currently connected BeaconNode is properly synchronised. - * @param {bytes48[]} validatorPubkeys - * @returns {Promise<{currentVersion: bytes4; validatorDuties: ValidatorDuty[]}>} A list of unique validator public keys, where each item is a 0x encoded hex string. + * @param {bytes48[]} validatorPubkey + * @returns {Promise<{currentVersion: bytes4; validatorDuty: ValidatorDuty}>} A list of unique validator public keys, where each item is a 0x encoded hex string. */ - getDuties(validatorPubkeys: bytes48[]): Promise<{currentVersion: Fork; validatorDuties: ValidatorDuty[]}>; + getDuties(validatorPubkey: bytes48): Promise<{currentVersion: Fork; validatorDuty: ValidatorDuty}>; /** * Requests a BeaconNode to produce a valid block, which can then be signed by a ValidatorClient. diff --git a/src/rpc/api/modules/validator.ts b/src/rpc/api/validator/validator.ts similarity index 78% rename from src/rpc/api/modules/validator.ts rename to src/rpc/api/validator/validator.ts index f101551c81f4..65dd22ffec77 100644 --- a/src/rpc/api/modules/validator.ts +++ b/src/rpc/api/validator/validator.ts @@ -1,12 +1,12 @@ import { Attestation, AttestationData, BeaconBlock, bytes32, Deposit, Shard, Slot, Eth1Data, uint64, - Fork, SyncingStatus, ValidatorDuty, bytes48, bytes, IndexedAttestation, number64 + Fork, SyncingStatus, ValidatorDuty, bytes48, bytes, IndexedAttestation, number64, BeaconState } from "../../../types"; import {DB} from "../../../db"; import {BeaconChain} from "../../../chain"; import {OpPool} from "../../../opPool"; -import {IValidatorApi} from "../interfaces"; +import {IValidatorApi} from "./interface"; export class ValidatorApi implements IValidatorApi { private chain: BeaconChain; @@ -23,9 +23,9 @@ export class ValidatorApi implements IValidatorApi { return Buffer.alloc(32); } - public async getFork(): Promise<{fork: Fork; chainId: number64}> { - // eslint-disable-next-line @typescript-eslint/no-object-literal-type-assertion - return {} as {fork: Fork; chainId: number64}; + public async getFork(): Promise { + const state: BeaconState = await this.db.getState(); + return state.fork; } public async getGenesisTime(): Promise { @@ -37,9 +37,9 @@ export class ValidatorApi implements IValidatorApi { return {} as boolean | SyncingStatus; } - public async getDuties(validatorPubkeys: bytes48[]): Promise<{currentVersion: Fork; validatorDuties: ValidatorDuty[]}> { + public async getDuties(validatorPubkey: bytes48): Promise<{currentVersion: Fork; validatorDuty: ValidatorDuty}> { // eslint-disable-next-line @typescript-eslint/no-object-literal-type-assertion - return {} as {currentVersion: Fork; validatorDuties: ValidatorDuty[]}; + return {} as {currentVersion: Fork; validatorDuty: ValidatorDuty}; } public async produceBlock(slot: Slot, randaoReveal: bytes): Promise { diff --git a/test/unit/rpc/jsonRpcOverHttp.test.ts b/test/unit/rpc/jsonRpcOverHttp.test.ts index 3cd76e063dd8..2e17dfbab3ee 100644 --- a/test/unit/rpc/jsonRpcOverHttp.test.ts +++ b/test/unit/rpc/jsonRpcOverHttp.test.ts @@ -1,6 +1,7 @@ import {assert} from "chai"; import * as request from "supertest"; -import {JSONRPC, MockAPI} from "../../../src/rpc"; +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"; @@ -12,7 +13,7 @@ describe("Json RPC over http", () => { logger.silent(true); const rpcServer = new HttpServer({port: 32421}); server = rpcServer.server; - rpc = new JSONRPC({}, {transport: rpcServer, api: new MockAPI()}); + rpc = new JSONRPC({}, {transport: rpcServer, api: new MockValidatorApi()}); await rpc.start(); }); after(async () => { @@ -60,7 +61,7 @@ describe("Json RPC over http", () => { }); }); it("should fail to start on existing port", (done) => { - const rpc = new JSONRPC({}, {transport: new HttpServer({port: 32421}), api: new MockAPI()}); + const rpc = new JSONRPC({}, {transport: new HttpServer({port: 32421}), api: new MockValidatorApi()}); rpc.start() .then(async () => { await rpc.stop(); diff --git a/test/unit/rpc/jsonRpcOverWs.test.ts b/test/unit/rpc/jsonRpcOverWs.test.ts index 8d1f22055331..85f20df971f1 100644 --- a/test/unit/rpc/jsonRpcOverWs.test.ts +++ b/test/unit/rpc/jsonRpcOverWs.test.ts @@ -1,12 +1,13 @@ import { assert } from "chai"; import * as jsonRpc from "noice-json-rpc"; import Websocket from "ws"; -import {MockAPI, JSONRPC, IValidatorApi, WSServer} from "../../../src/rpc"; +import {JSONRPC, IValidatorApi, WSServer} from "../../../src/rpc"; 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 MockAPI()}); + const rpc = new JSONRPC({}, {transport: new WSServer({port: 32420}), api: new MockValidatorApi()}); let client; let ws; let clientApi: {BeaconChain: IValidatorApi}; @@ -36,7 +37,7 @@ describe("Json RPC over WS", () => { assert.ok(eth1Data); }); it("should get validator duties", async () => { - const root = await clientApi.BeaconChain.getDuties([Buffer.alloc(48)]); + const root = await clientApi.BeaconChain.getDuties(Buffer.alloc(48)); assert.ok(root); }); it("should produce a block for the validator", async () => { diff --git a/src/rpc/api/mock/validator.ts b/test/utils/mocks/rpc/validator.ts similarity index 67% rename from src/rpc/api/mock/validator.ts rename to test/utils/mocks/rpc/validator.ts index ebc9041595f0..58e52bbd8453 100644 --- a/src/rpc/api/mock/validator.ts +++ b/test/utils/mocks/rpc/validator.ts @@ -1,19 +1,22 @@ -import {Attestation, AttestationData, BeaconBlock, bytes32, Deposit, Shard, Slot, Eth1Data} from "../../../types/index"; +import {Attestation, AttestationData, BeaconBlock, bytes32, Deposit, Shard, Slot, Eth1Data} from "../../../../src/types"; -import {getEmptyBlock} from "../../../chain/genesis"; +import {getEmptyBlock} from "../../../../src/chain/genesis"; -import {IValidatorApi} from "../interfaces/index"; -import {bytes, bytes48, Fork, number64, SyncingStatus, uint64, ValidatorDuty} from "../../../types"; +import {IValidatorApi} from "../../../../src/rpc/api/validator"; +import {bytes, bytes48, Fork, number64, SyncingStatus, uint64, ValidatorDuty} from "../../../../src/types"; export interface MockAPIOpts { head?: BeaconBlock; + version?: bytes32; + fork?: Fork; + chainId?: number64; pendingAttestations?: Attestation[]; getPendingDeposits?: Deposit[]; Eth1Data?: Eth1Data; attestationData?: AttestationData; } -export class MockAPI implements IValidatorApi { +export class MockValidatorApi implements IValidatorApi { private version: bytes32; private fork: Fork; private chainId: number64; @@ -22,13 +25,17 @@ export class MockAPI implements IValidatorApi { public constructor(opts?: MockAPIOpts) { this.attestations = opts && opts.pendingAttestations || []; this.head = opts && opts.head || getEmptyBlock(); + this.version = opts && opts.version || Buffer.alloc(0); + this.fork = opts && opts.fork || {previousVersion: Buffer.alloc(0), currentVersion: Buffer.alloc(0), epoch: 0} + this.chainId = opts && opts.chainId || 0; } + public async getClientVersion(): Promise { return this.version; } - public async getFork(): Promise<{fork: Fork; chainId: number64}> { - return {fork: this.fork, chainId: this.chainId}; + public async getFork(): Promise { + return this.fork; } public async getGenesisTime(): Promise { @@ -40,9 +47,9 @@ export class MockAPI implements IValidatorApi { return false; } - public async getDuties(validatorPubkeys: bytes48[]): Promise<{currentVersion: Fork; validatorDuties: ValidatorDuty[]}> { + public async getDuties(validatorPubkey: bytes48): Promise<{currentVersion: Fork; validatorDuty: ValidatorDuty}> { // eslint-disable-next-line @typescript-eslint/no-object-literal-type-assertion - return {} as {currentVersion: Fork; validatorDuties: ValidatorDuty[]}; + return {} as {currentVersion: Fork; validatorDuty: ValidatorDuty}; } public async produceBlock(slot: Slot, randaoReveal: bytes): Promise {