Skip to content

Commit

Permalink
fix: eventstream invalid topic error
Browse files Browse the repository at this point in the history
  • Loading branch information
nflaig committed Jul 23, 2023
1 parent 3257345 commit f07ac88
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 18 deletions.
11 changes: 9 additions & 2 deletions packages/api/src/beacon/server/events.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {ChainForkConfig} from "@lodestar/config";
import {ErrorAborted} from "@lodestar/utils";
import {Api, ReqTypes, routesData, getEventSerdes} from "../routes/events.js";
import {ServerRoutes} from "../../utils/server/index.js";
import {Api, ReqTypes, routesData, getEventSerdes, eventTypes} from "../routes/events.js";
import {ApiError, ServerRoutes} from "../../utils/server/index.js";
import {ServerApi} from "../../interfaces.js";

export function getRoutes(config: ChainForkConfig, api: ServerApi<Api>): ServerRoutes<Api, ReqTypes> {
Expand All @@ -15,6 +15,13 @@ export function getRoutes(config: ChainForkConfig, api: ServerApi<Api>): ServerR
id: "eventstream",

handler: async (req, res) => {
const validTopics = new Set(Object.values(eventTypes));
for (const topic of req.query.topics) {
if (!validTopics.has(topic)) {
throw new ApiError(400, `Invalid topic: ${topic}`);
}
}

const controller = new AbortController();

try {
Expand Down
5 changes: 4 additions & 1 deletion packages/api/src/beacon/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {ChainForkConfig} from "@lodestar/config";
import {Api} from "../routes/index.js";
import {ServerInstance, ServerRoute, RouteConfig, registerRoute} from "../../utils/server/index.js";
import {ServerInstance, ServerRoute, ApiError, RouteConfig, registerRoute} from "../../utils/server/index.js";

import {ServerApi} from "../../interfaces.js";
import * as beacon from "./beacon.js";
Expand All @@ -13,6 +13,9 @@ import * as node from "./node.js";
import * as proof from "./proof.js";
import * as validator from "./validator.js";

// Re-export for usage in beacon-node
export {ApiError};

// Re-export for convenience
export {RouteConfig};

Expand Down
9 changes: 9 additions & 0 deletions packages/api/src/utils/server/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {HttpErrorCodes} from "../client/httpStatusCode.js";

export class ApiError extends Error {
statusCode: HttpErrorCodes;
constructor(statusCode: HttpErrorCodes, message?: string) {
super(message);
this.statusCode = statusCode;
}
}
1 change: 1 addition & 0 deletions packages/api/src/utils/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./genericJsonServer.js";
export * from "./registerRoute.js";
export * from "./errors.js";
export * from "./types.js";
10 changes: 2 additions & 8 deletions packages/beacon-node/src/api/impl/errors.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import {HttpErrorCodes} from "@lodestar/api";
import {ApiError} from "@lodestar/api/beacon/server";

export class ApiError extends Error {
statusCode: HttpErrorCodes;
constructor(statusCode: HttpErrorCodes, message?: string) {
super(message);
this.statusCode = statusCode;
}
}
export {ApiError};

export class StateNotFound extends ApiError {
constructor() {
Expand Down
7 changes: 0 additions & 7 deletions packages/beacon-node/src/api/impl/events/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import {routes, ServerApi} from "@lodestar/api";
import {ApiModules} from "../types.js";
import {ApiError} from "../errors.js";

export function getEventsApi({chain}: Pick<ApiModules, "chain" | "config">): ServerApi<routes.events.Api> {
const validTopics = new Set(Object.values(routes.events.eventTypes));

return {
async eventstream(topics, signal, onEvent) {
const onAbortFns: (() => void)[] = [];

for (const topic of topics) {
if (!validTopics.has(topic)) {
throw new ApiError(400, `Unknown topic ${topic}`);
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const handler = (data: any): void => {
// TODO: What happens if this handler throws? Does it break the other chain.emitter listeners?
Expand Down

0 comments on commit f07ac88

Please sign in to comment.