Skip to content

Commit

Permalink
update based on pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
GregTheGreek committed May 3, 2019
1 parent e0d1f4d commit 488e06d
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 35 deletions.
4 changes: 1 addition & 3 deletions src/rpc/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
export * from "./interfaces";
export * from "./mock/validator";
export * from "./modules";
export * from "./validator";
5 changes: 0 additions & 5 deletions src/rpc/api/interfaces/index.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {ValidatorApi} from "./validator";
import {IValidatorApi} from "./interface";

export {
ValidatorApi,
IValidatorApi
};
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand All @@ -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<Fork>;

/**
* Requests the genesis_time parameter from the BeaconNode, which should be consistent across all BeaconNodes that follow the same beacon chain.
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Fork> {
const state: BeaconState = await this.db.getState();
return state.fork;
}

public async getGenesisTime(): Promise<number64> {
Expand All @@ -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<BeaconBlock> {
Expand Down
7 changes: 4 additions & 3 deletions test/unit/rpc/jsonRpcOverHttp.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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 () => {
Expand Down Expand Up @@ -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();
Expand Down
7 changes: 4 additions & 3 deletions test/unit/rpc/jsonRpcOverWs.test.ts
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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 () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<bytes32> {
return this.version;
}

public async getFork(): Promise<{fork: Fork; chainId: number64}> {
return {fork: this.fork, chainId: this.chainId};
public async getFork(): Promise<Fork> {
return this.fork;
}

public async getGenesisTime(): Promise<number64> {
Expand All @@ -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<BeaconBlock> {
Expand Down

0 comments on commit 488e06d

Please sign in to comment.