From e8f68ca601a40011778b3b191b8388a71319b8b9 Mon Sep 17 00:00:00 2001 From: dapplion <35266934+dapplion@users.noreply.github.com> Date: Tue, 14 Jun 2022 21:04:25 +0200 Subject: [PATCH 1/4] Modularize lodestar-api --- packages/api/package.json | 17 +++++++-- .../api/src/{ => beacon}/client/beacon.ts | 2 +- .../api/src/{ => beacon}/client/config.ts | 2 +- packages/api/src/{ => beacon}/client/debug.ts | 2 +- .../api/src/{ => beacon}/client/events.ts | 2 +- packages/api/src/{ => beacon}/client/index.ts | 5 +-- .../src/{ => beacon}/client/lightclient.ts | 2 +- .../api/src/{ => beacon}/client/lodestar.ts | 2 +- packages/api/src/{ => beacon}/client/node.ts | 2 +- .../api/src/{ => beacon}/client/validator.ts | 2 +- packages/api/src/beacon/index.ts | 38 +++++++++++++++++++ .../src/{ => beacon}/routes/beacon/block.ts | 2 +- .../src/{ => beacon}/routes/beacon/index.ts | 2 +- .../src/{ => beacon}/routes/beacon/pool.ts | 2 +- .../src/{ => beacon}/routes/beacon/state.ts | 2 +- .../api/src/{ => beacon}/routes/config.ts | 2 +- packages/api/src/{ => beacon}/routes/debug.ts | 2 +- .../api/src/{ => beacon}/routes/events.ts | 2 +- packages/api/src/{ => beacon}/routes/index.ts | 0 .../src/{ => beacon}/routes/lightclient.ts | 4 +- .../api/src/{ => beacon}/routes/lodestar.ts | 2 +- packages/api/src/{ => beacon}/routes/node.ts | 2 +- .../api/src/{ => beacon}/routes/validator.ts | 2 +- .../api/src/{ => beacon}/server/beacon.ts | 2 +- .../api/src/{ => beacon}/server/config.ts | 2 +- packages/api/src/{ => beacon}/server/debug.ts | 2 +- .../api/src/{ => beacon}/server/events.ts | 2 +- packages/api/src/{ => beacon}/server/index.ts | 33 ++++------------ .../src/{ => beacon}/server/lightclient.ts | 2 +- .../api/src/{ => beacon}/server/lodestar.ts | 2 +- packages/api/src/{ => beacon}/server/node.ts | 2 +- .../api/src/{ => beacon}/server/validator.ts | 2 +- packages/api/src/builder/client.ts | 13 +++++++ packages/api/src/builder/index.ts | 22 +++++++++++ packages/api/src/builder/routes.ts | 28 ++++++++++++++ packages/api/src/index.ts | 9 +++-- packages/api/src/interface.ts | 16 ++++---- packages/api/src/keymanager/client.ts | 2 +- packages/api/src/keymanager/index.ts | 16 ++++++-- packages/api/src/keymanager/server.ts | 13 ++++++- packages/api/src/server/utils/index.ts | 1 - .../{client/utils => utils/client}/client.ts | 4 +- .../{client/utils => utils/client}/format.ts | 0 .../utils => utils/client}/httpClient.ts | 2 +- .../{client/utils => utils/client}/index.ts | 0 .../{client/utils => utils/client}/metrics.ts | 0 .../server/genericJsonServer.ts} | 28 ++------------ packages/api/src/utils/server/index.ts | 3 ++ .../api/src/utils/server/registerRoute.ts | 15 ++++++++ packages/api/src/utils/server/types.ts | 34 +++++++++++++++++ packages/api/test/unit/beacon.test.ts | 6 +-- packages/api/test/unit/client/format.test.ts | 4 +- .../api/test/unit/client/httpClient.test.ts | 2 +- packages/api/test/unit/config.test.ts | 6 +-- packages/api/test/unit/debug.test.ts | 10 ++--- packages/api/test/unit/events.test.ts | 8 ++-- packages/api/test/unit/lightclient.test.ts | 6 +-- packages/api/test/unit/node.test.ts | 6 +-- packages/api/test/unit/validator.test.ts | 6 +-- packages/api/test/utils/genericServerTest.ts | 6 +-- packages/keymanager-server/src/server.ts | 7 +--- .../light-client/test/lightclientApiServer.ts | 2 +- packages/lodestar/src/api/rest/index.ts | 2 +- 63 files changed, 282 insertions(+), 144 deletions(-) rename packages/api/src/{ => beacon}/client/beacon.ts (86%) rename packages/api/src/{ => beacon}/client/config.ts (86%) rename packages/api/src/{ => beacon}/client/debug.ts (97%) rename packages/api/src/{ => beacon}/client/events.ts (97%) rename packages/api/src/{ => beacon}/client/index.ts (90%) rename packages/api/src/{ => beacon}/client/lightclient.ts (96%) rename packages/api/src/{ => beacon}/client/lodestar.ts (86%) rename packages/api/src/{ => beacon}/client/node.ts (86%) rename packages/api/src/{ => beacon}/client/validator.ts (86%) create mode 100644 packages/api/src/beacon/index.ts rename packages/api/src/{ => beacon}/routes/beacon/block.ts (99%) rename packages/api/src/{ => beacon}/routes/beacon/index.ts (98%) rename packages/api/src/{ => beacon}/routes/beacon/pool.ts (99%) rename packages/api/src/{ => beacon}/routes/beacon/state.ts (99%) rename packages/api/src/{ => beacon}/routes/config.ts (98%) rename packages/api/src/{ => beacon}/routes/debug.ts (99%) rename packages/api/src/{ => beacon}/routes/events.ts (99%) rename packages/api/src/{ => beacon}/routes/index.ts (100%) rename packages/api/src/{ => beacon}/routes/lightclient.ts (98%) rename packages/api/src/{ => beacon}/routes/lodestar.ts (99%) rename packages/api/src/{ => beacon}/routes/node.ts (99%) rename packages/api/src/{ => beacon}/routes/validator.ts (99%) rename packages/api/src/{ => beacon}/server/beacon.ts (84%) rename packages/api/src/{ => beacon}/server/config.ts (84%) rename packages/api/src/{ => beacon}/server/debug.ts (94%) rename packages/api/src/{ => beacon}/server/events.ts (98%) rename packages/api/src/{ => beacon}/server/index.ts (67%) rename packages/api/src/{ => beacon}/server/lightclient.ts (92%) rename packages/api/src/{ => beacon}/server/lodestar.ts (84%) rename packages/api/src/{ => beacon}/server/node.ts (90%) rename packages/api/src/{ => beacon}/server/validator.ts (84%) create mode 100644 packages/api/src/builder/client.ts create mode 100644 packages/api/src/builder/index.ts create mode 100644 packages/api/src/builder/routes.ts delete mode 100644 packages/api/src/server/utils/index.ts rename packages/api/src/{client/utils => utils/client}/client.ts (96%) rename packages/api/src/{client/utils => utils/client}/format.ts (100%) rename packages/api/src/{client/utils => utils/client}/httpClient.ts (98%) rename packages/api/src/{client/utils => utils/client}/index.ts (100%) rename packages/api/src/{client/utils => utils/client}/metrics.ts (100%) rename packages/api/src/{server/utils/server.ts => utils/server/genericJsonServer.ts} (65%) create mode 100644 packages/api/src/utils/server/index.ts create mode 100644 packages/api/src/utils/server/registerRoute.ts create mode 100644 packages/api/src/utils/server/types.ts diff --git a/packages/api/package.json b/packages/api/package.json index db3da7b385e9..11b75fedd5f2 100644 --- a/packages/api/package.json +++ b/packages/api/package.json @@ -17,14 +17,23 @@ ".": { "import": "./lib/index.js" }, + "./beacon": { + "import": "./lib/beacon/index.js" + }, + "./beacon/server": { + "import": "./lib/beacon/server/index.js" + }, + "./builder": { + "import": "./lib/builder/index.js" + }, + "./builder/server": { + "import": "./lib/builder/server/index.js" + }, "./keymanager": { "import": "./lib/keymanager/index.js" }, "./keymanager/server": { - "import": "./lib/keymanager/server.js" - }, - "./server": { - "import": "./lib/server/index.js" + "import": "./lib/keymanager/server/index.js" } }, "typesVersions": { diff --git a/packages/api/src/client/beacon.ts b/packages/api/src/beacon/client/beacon.ts similarity index 86% rename from packages/api/src/client/beacon.ts rename to packages/api/src/beacon/client/beacon.ts index 2954017548e5..43684a23130a 100644 --- a/packages/api/src/client/beacon.ts +++ b/packages/api/src/beacon/client/beacon.ts @@ -1,6 +1,6 @@ import {IChainForkConfig} from "@chainsafe/lodestar-config"; import {Api, ReqTypes, routesData, getReqSerializers, getReturnTypes} from "../routes/beacon/index.js"; -import {IHttpClient, generateGenericJsonClient} from "./utils/index.js"; +import {IHttpClient, generateGenericJsonClient} from "../../utils/client/index.js"; /** * REST HTTP client for beacon routes diff --git a/packages/api/src/client/config.ts b/packages/api/src/beacon/client/config.ts similarity index 86% rename from packages/api/src/client/config.ts rename to packages/api/src/beacon/client/config.ts index a4a6f06d13c4..a277e6269241 100644 --- a/packages/api/src/client/config.ts +++ b/packages/api/src/beacon/client/config.ts @@ -1,6 +1,6 @@ import {IChainForkConfig} from "@chainsafe/lodestar-config"; import {Api, ReqTypes, routesData, getReqSerializers, getReturnTypes} from "../routes/config.js"; -import {IHttpClient, generateGenericJsonClient} from "./utils/index.js"; +import {IHttpClient, generateGenericJsonClient} from "../../utils/client/index.js"; /** * REST HTTP client for config routes diff --git a/packages/api/src/client/debug.ts b/packages/api/src/beacon/client/debug.ts similarity index 97% rename from packages/api/src/client/debug.ts rename to packages/api/src/beacon/client/debug.ts index 7775e0677c52..6eccc3b792aa 100644 --- a/packages/api/src/client/debug.ts +++ b/packages/api/src/beacon/client/debug.ts @@ -1,6 +1,6 @@ import {IChainForkConfig} from "@chainsafe/lodestar-config"; import {Api, ReqTypes, routesData, getReqSerializers, getReturnTypes, StateFormat} from "../routes/debug.js"; -import {IHttpClient, getFetchOptsSerializers, generateGenericJsonClient} from "./utils/index.js"; +import {IHttpClient, getFetchOptsSerializers, generateGenericJsonClient} from "../../utils/client/index.js"; /** * REST HTTP client for debug routes diff --git a/packages/api/src/client/events.ts b/packages/api/src/beacon/client/events.ts similarity index 97% rename from packages/api/src/client/events.ts rename to packages/api/src/beacon/client/events.ts index 875daee42796..24205784a640 100644 --- a/packages/api/src/client/events.ts +++ b/packages/api/src/beacon/client/events.ts @@ -1,7 +1,7 @@ import EventSource from "eventsource"; import {IChainForkConfig} from "@chainsafe/lodestar-config"; import {Api, BeaconEvent, routesData, getEventSerdes} from "../routes/events.js"; -import {stringifyQuery} from "./utils/format.js"; +import {stringifyQuery} from "../../utils/client/format.js"; /** * REST HTTP client for events routes diff --git a/packages/api/src/client/index.ts b/packages/api/src/beacon/client/index.ts similarity index 90% rename from packages/api/src/client/index.ts rename to packages/api/src/beacon/client/index.ts index 7e3a48ea54da..afec1f0da089 100644 --- a/packages/api/src/client/index.ts +++ b/packages/api/src/beacon/client/index.ts @@ -1,7 +1,6 @@ import {IChainForkConfig} from "@chainsafe/lodestar-config"; -import {Api} from "../interface.js"; -import {IHttpClient, HttpClient, HttpClientOptions, HttpClientModules, HttpError} from "./utils/index.js"; -export {HttpClient, HttpClientOptions, HttpError}; +import {Api} from "../../interface.js"; +import {IHttpClient, HttpClient, HttpClientOptions, HttpClientModules} from "../../utils/client/index.js"; import * as beacon from "./beacon.js"; import * as configApi from "./config.js"; diff --git a/packages/api/src/client/lightclient.ts b/packages/api/src/beacon/client/lightclient.ts similarity index 96% rename from packages/api/src/client/lightclient.ts rename to packages/api/src/beacon/client/lightclient.ts index 80f1ca20edd4..7f7c7902ac47 100644 --- a/packages/api/src/client/lightclient.ts +++ b/packages/api/src/beacon/client/lightclient.ts @@ -1,7 +1,7 @@ import {IChainForkConfig} from "@chainsafe/lodestar-config"; import {deserializeProof} from "@chainsafe/persistent-merkle-tree"; import {Api, ReqTypes, routesData, getReqSerializers, getReturnTypes} from "../routes/lightclient.js"; -import {IHttpClient, getFetchOptsSerializers, generateGenericJsonClient} from "./utils/index.js"; +import {IHttpClient, getFetchOptsSerializers, generateGenericJsonClient} from "../../utils/client/index.js"; /** * REST HTTP client for lightclient routes diff --git a/packages/api/src/client/lodestar.ts b/packages/api/src/beacon/client/lodestar.ts similarity index 86% rename from packages/api/src/client/lodestar.ts rename to packages/api/src/beacon/client/lodestar.ts index 6695f640c0d8..3d73f6be5a16 100644 --- a/packages/api/src/client/lodestar.ts +++ b/packages/api/src/beacon/client/lodestar.ts @@ -1,6 +1,6 @@ import {IChainForkConfig} from "@chainsafe/lodestar-config"; import {Api, ReqTypes, routesData, getReqSerializers, getReturnTypes} from "../routes/lodestar.js"; -import {IHttpClient, generateGenericJsonClient} from "./utils/index.js"; +import {IHttpClient, generateGenericJsonClient} from "../../utils/client/index.js"; /** * REST HTTP client for lodestar routes diff --git a/packages/api/src/client/node.ts b/packages/api/src/beacon/client/node.ts similarity index 86% rename from packages/api/src/client/node.ts rename to packages/api/src/beacon/client/node.ts index e7a04f0bd952..34cff9a966b4 100644 --- a/packages/api/src/client/node.ts +++ b/packages/api/src/beacon/client/node.ts @@ -1,6 +1,6 @@ import {IChainForkConfig} from "@chainsafe/lodestar-config"; import {Api, ReqTypes, routesData, getReqSerializers, getReturnTypes} from "../routes/node.js"; -import {IHttpClient, generateGenericJsonClient} from "./utils/index.js"; +import {IHttpClient, generateGenericJsonClient} from "../../utils/client/index.js"; /** * REST HTTP client for beacon routes diff --git a/packages/api/src/client/validator.ts b/packages/api/src/beacon/client/validator.ts similarity index 86% rename from packages/api/src/client/validator.ts rename to packages/api/src/beacon/client/validator.ts index 1924bb3464d5..c187c5f93857 100644 --- a/packages/api/src/client/validator.ts +++ b/packages/api/src/beacon/client/validator.ts @@ -1,6 +1,6 @@ import {IChainForkConfig} from "@chainsafe/lodestar-config"; import {Api, ReqTypes, routesData, getReqSerializers, getReturnTypes} from "../routes/validator.js"; -import {IHttpClient, generateGenericJsonClient} from "./utils/index.js"; +import {IHttpClient, generateGenericJsonClient} from "../../utils/client/index.js"; /** * REST HTTP client for validator routes diff --git a/packages/api/src/beacon/index.ts b/packages/api/src/beacon/index.ts new file mode 100644 index 000000000000..6c64968a645d --- /dev/null +++ b/packages/api/src/beacon/index.ts @@ -0,0 +1,38 @@ +import {Api as BeaconApi} from "./routes/beacon/index.js"; +import {Api as ConfigApi} from "./routes/config.js"; +import {Api as DebugApi} from "./routes/debug.js"; +import {Api as EventsApi} from "./routes/events.js"; +import {Api as LightclientApi} from "./routes/lightclient.js"; +import {Api as LodestarApi} from "./routes/lodestar.js"; +import {Api as NodeApi} from "./routes/node.js"; +import {Api as ValidatorApi} from "./routes/validator.js"; + +// NOTE: Don't export server here so it's not bundled to all consumers + +export * as routes from "./routes/index.js"; +export {getClient} from "./client/index.js"; + +export type Api = { + beacon: BeaconApi; + config: ConfigApi; + debug: DebugApi; + events: EventsApi; + lightclient: LightclientApi; + lodestar: LodestarApi; + node: NodeApi; + validator: ValidatorApi; +}; + +// Declare namespaces for CLI options +export type ApiNamespace = keyof Api; +const allNamespacesObj: {[K in keyof Api]: true} = { + beacon: true, + config: true, + debug: true, + events: true, + lightclient: true, + lodestar: true, + node: true, + validator: true, +}; +export const allNamespaces = Object.keys(allNamespacesObj) as ApiNamespace[]; diff --git a/packages/api/src/routes/beacon/block.ts b/packages/api/src/beacon/routes/beacon/block.ts similarity index 99% rename from packages/api/src/routes/beacon/block.ts rename to packages/api/src/beacon/routes/beacon/block.ts index ea70603d1f85..d51a78bc9390 100644 --- a/packages/api/src/routes/beacon/block.ts +++ b/packages/api/src/beacon/routes/beacon/block.ts @@ -13,7 +13,7 @@ import { TypeJson, ReqSerializers, ReqSerializer, -} from "../../utils/index.js"; +} from "../../../utils/index.js"; // See /packages/api/src/routes/index.ts for reasoning and instructions to add new routes diff --git a/packages/api/src/routes/beacon/index.ts b/packages/api/src/beacon/routes/beacon/index.ts similarity index 98% rename from packages/api/src/routes/beacon/index.ts rename to packages/api/src/beacon/routes/beacon/index.ts index b285aa1e6085..1a561820e30e 100644 --- a/packages/api/src/routes/beacon/index.ts +++ b/packages/api/src/beacon/routes/beacon/index.ts @@ -1,6 +1,6 @@ import {IChainForkConfig} from "@chainsafe/lodestar-config"; import {phase0, ssz} from "@chainsafe/lodestar-types"; -import {RoutesData, ReturnTypes, reqEmpty, ContainerData} from "../../utils/index.js"; +import {RoutesData, ReturnTypes, reqEmpty, ContainerData} from "../../../utils/index.js"; import * as block from "./block.js"; import * as pool from "./pool.js"; import * as state from "./state.js"; diff --git a/packages/api/src/routes/beacon/pool.ts b/packages/api/src/beacon/routes/beacon/pool.ts similarity index 99% rename from packages/api/src/routes/beacon/pool.ts rename to packages/api/src/beacon/routes/beacon/pool.ts index 67a669e13992..214a50670b11 100644 --- a/packages/api/src/routes/beacon/pool.ts +++ b/packages/api/src/beacon/routes/beacon/pool.ts @@ -9,7 +9,7 @@ import { ReqSerializers, reqEmpty, ReqEmpty, -} from "../../utils/index.js"; +} from "../../../utils/index.js"; // See /packages/api/src/routes/index.ts for reasoning and instructions to add new routes diff --git a/packages/api/src/routes/beacon/state.ts b/packages/api/src/beacon/routes/beacon/state.ts similarity index 99% rename from packages/api/src/routes/beacon/state.ts rename to packages/api/src/beacon/routes/beacon/state.ts index f427e4472335..44336d4ab5c0 100644 --- a/packages/api/src/routes/beacon/state.ts +++ b/packages/api/src/beacon/routes/beacon/state.ts @@ -8,7 +8,7 @@ import { Schema, ReqSerializers, ReqSerializer, -} from "../../utils/index.js"; +} from "../../../utils/index.js"; // See /packages/api/src/routes/index.ts for reasoning and instructions to add new routes diff --git a/packages/api/src/routes/config.ts b/packages/api/src/beacon/routes/config.ts similarity index 98% rename from packages/api/src/routes/config.ts rename to packages/api/src/beacon/routes/config.ts index ad197c6ac999..3ff665b162f5 100644 --- a/packages/api/src/routes/config.ts +++ b/packages/api/src/beacon/routes/config.ts @@ -12,7 +12,7 @@ import { ReqSerializers, RoutesData, sameType, -} from "../utils/index.js"; +} from "../../utils/index.js"; // See /packages/api/src/routes/index.ts for reasoning and instructions to add new routes diff --git a/packages/api/src/routes/debug.ts b/packages/api/src/beacon/routes/debug.ts similarity index 99% rename from packages/api/src/routes/debug.ts rename to packages/api/src/beacon/routes/debug.ts index 2e2843ef44d3..8bd5b48fde9d 100644 --- a/packages/api/src/routes/debug.ts +++ b/packages/api/src/beacon/routes/debug.ts @@ -13,7 +13,7 @@ import { ReqSerializers, ReqEmpty, ReqSerializer, -} from "../utils/index.js"; +} from "../../utils/index.js"; import {StateId} from "./beacon/state.js"; // See /packages/api/src/routes/index.ts for reasoning and instructions to add new routes diff --git a/packages/api/src/routes/events.ts b/packages/api/src/beacon/routes/events.ts similarity index 99% rename from packages/api/src/routes/events.ts rename to packages/api/src/beacon/routes/events.ts index 776d1d083aaa..fb4ca79417b3 100644 --- a/packages/api/src/routes/events.ts +++ b/packages/api/src/beacon/routes/events.ts @@ -1,7 +1,7 @@ import {Epoch, phase0, Slot, ssz, StringType, RootHex, altair, UintNum64} from "@chainsafe/lodestar-types"; import {ContainerType, Type, VectorCompositeType} from "@chainsafe/ssz"; import {FINALIZED_ROOT_DEPTH} from "@chainsafe/lodestar-params"; -import {RouteDef, TypeJson} from "../utils/index.js"; +import {RouteDef, TypeJson} from "../../utils/index.js"; // See /packages/api/src/routes/index.ts for reasoning and instructions to add new routes diff --git a/packages/api/src/routes/index.ts b/packages/api/src/beacon/routes/index.ts similarity index 100% rename from packages/api/src/routes/index.ts rename to packages/api/src/beacon/routes/index.ts diff --git a/packages/api/src/routes/lightclient.ts b/packages/api/src/beacon/routes/lightclient.ts similarity index 98% rename from packages/api/src/routes/lightclient.ts rename to packages/api/src/beacon/routes/lightclient.ts index 17acaf10a612..3bcba9f46860 100644 --- a/packages/api/src/routes/lightclient.ts +++ b/packages/api/src/beacon/routes/lightclient.ts @@ -12,8 +12,8 @@ import { ReqSerializers, reqEmpty, ReqEmpty, -} from "../utils/index.js"; -import {queryParseProofPathsArr, querySerializeProofPathsArr} from "../utils/serdes.js"; +} from "../../utils/index.js"; +import {queryParseProofPathsArr, querySerializeProofPathsArr} from "../../utils/serdes.js"; import {LightclientHeaderUpdate, LightclientFinalizedUpdate} from "./events.js"; // Re-export for convenience when importing routes.lightclient.LightclientHeaderUpdate diff --git a/packages/api/src/routes/lodestar.ts b/packages/api/src/beacon/routes/lodestar.ts similarity index 99% rename from packages/api/src/routes/lodestar.ts rename to packages/api/src/beacon/routes/lodestar.ts index c6a949e904f7..76934383fb77 100644 --- a/packages/api/src/routes/lodestar.ts +++ b/packages/api/src/beacon/routes/lodestar.ts @@ -8,7 +8,7 @@ import { RoutesData, sameType, Schema, -} from "../utils/index.js"; +} from "../../utils/index.js"; import {FilterGetPeers, NodePeer, PeerDirection, PeerState} from "./node.js"; // See /packages/api/src/routes/index.ts for reasoning and instructions to add new routes diff --git a/packages/api/src/routes/node.ts b/packages/api/src/beacon/routes/node.ts similarity index 99% rename from packages/api/src/routes/node.ts rename to packages/api/src/beacon/routes/node.ts index b93a43fa53a2..03ef481c866f 100644 --- a/packages/api/src/routes/node.ts +++ b/packages/api/src/beacon/routes/node.ts @@ -11,7 +11,7 @@ import { ReqSerializers, ReqEmpty, sameType, -} from "../utils/index.js"; +} from "../../utils/index.js"; // See /packages/api/src/routes/index.ts for reasoning and instructions to add new routes diff --git a/packages/api/src/routes/validator.ts b/packages/api/src/beacon/routes/validator.ts similarity index 99% rename from packages/api/src/routes/validator.ts rename to packages/api/src/beacon/routes/validator.ts index 5ee5534e8089..2fa9c2b67f42 100644 --- a/packages/api/src/routes/validator.ts +++ b/packages/api/src/beacon/routes/validator.ts @@ -24,7 +24,7 @@ import { reqOnlyBody, ReqSerializers, jsonType, -} from "../utils/index.js"; +} from "../../utils/index.js"; // See /packages/api/src/routes/index.ts for reasoning and instructions to add new routes diff --git a/packages/api/src/server/beacon.ts b/packages/api/src/beacon/server/beacon.ts similarity index 84% rename from packages/api/src/server/beacon.ts rename to packages/api/src/beacon/server/beacon.ts index e9c2bf912502..487f567cda6d 100644 --- a/packages/api/src/server/beacon.ts +++ b/packages/api/src/beacon/server/beacon.ts @@ -1,6 +1,6 @@ import {IChainForkConfig} from "@chainsafe/lodestar-config"; import {Api, ReqTypes, routesData, getReturnTypes, getReqSerializers} from "../routes/beacon/index.js"; -import {ServerRoutes, getGenericJsonServer} from "./utils/index.js"; +import {ServerRoutes, getGenericJsonServer} from "../../utils/server/index.js"; export function getRoutes(config: IChainForkConfig, api: Api): ServerRoutes { // All routes return JSON, use a server auto-generator diff --git a/packages/api/src/server/config.ts b/packages/api/src/beacon/server/config.ts similarity index 84% rename from packages/api/src/server/config.ts rename to packages/api/src/beacon/server/config.ts index ad1ba68c6576..8260bd0c6697 100644 --- a/packages/api/src/server/config.ts +++ b/packages/api/src/beacon/server/config.ts @@ -1,6 +1,6 @@ import {IChainForkConfig} from "@chainsafe/lodestar-config"; import {Api, ReqTypes, routesData, getReturnTypes, getReqSerializers} from "../routes/config.js"; -import {ServerRoutes, getGenericJsonServer} from "./utils/index.js"; +import {ServerRoutes, getGenericJsonServer} from "../../utils/server/index.js"; export function getRoutes(config: IChainForkConfig, api: Api): ServerRoutes { // All routes return JSON, use a server auto-generator diff --git a/packages/api/src/server/debug.ts b/packages/api/src/beacon/server/debug.ts similarity index 94% rename from packages/api/src/server/debug.ts rename to packages/api/src/beacon/server/debug.ts index b2b35838538e..9c15f5ffd296 100644 --- a/packages/api/src/server/debug.ts +++ b/packages/api/src/beacon/server/debug.ts @@ -1,6 +1,6 @@ import {IChainForkConfig} from "@chainsafe/lodestar-config"; import {Api, ReqTypes, routesData, getReturnTypes, getReqSerializers} from "../routes/debug.js"; -import {ServerRoutes, getGenericJsonServer} from "./utils/index.js"; +import {ServerRoutes, getGenericJsonServer} from "../../utils/server/index.js"; export function getRoutes(config: IChainForkConfig, api: Api): ServerRoutes { const reqSerializers = getReqSerializers(); diff --git a/packages/api/src/server/events.ts b/packages/api/src/beacon/server/events.ts similarity index 98% rename from packages/api/src/server/events.ts rename to packages/api/src/beacon/server/events.ts index ffe134794c05..787984077390 100644 --- a/packages/api/src/server/events.ts +++ b/packages/api/src/beacon/server/events.ts @@ -1,6 +1,6 @@ import {IChainForkConfig} from "@chainsafe/lodestar-config"; import {Api, ReqTypes, routesData, getEventSerdes} from "../routes/events.js"; -import {ServerRoutes} from "./utils/index.js"; +import {ServerRoutes} from "../../utils/server/index.js"; export function getRoutes(config: IChainForkConfig, api: Api): ServerRoutes { const eventSerdes = getEventSerdes(); diff --git a/packages/api/src/server/index.ts b/packages/api/src/beacon/server/index.ts similarity index 67% rename from packages/api/src/server/index.ts rename to packages/api/src/beacon/server/index.ts index ce5cd75356d2..7617d99b1ca3 100644 --- a/packages/api/src/server/index.ts +++ b/packages/api/src/beacon/server/index.ts @@ -1,8 +1,6 @@ -import {FastifyInstance} from "fastify"; import {IChainForkConfig} from "@chainsafe/lodestar-config"; -// eslint-disable-next-line import/no-extraneous-dependencies -import {Api} from "../interface.js"; -import {ServerRoute} from "./utils/index.js"; +import {Api} from "../../interface.js"; +import {ServerInstance, ServerRoute, RouteConfig, registerRoute} from "../../utils/server/index.js"; import * as beacon from "./beacon.js"; import * as configApi from "./config.js"; @@ -13,12 +11,11 @@ import * as lodestar from "./lodestar.js"; import * as node from "./node.js"; import * as validator from "./validator.js"; -export type RouteConfig = { - operationId: ServerRoute["id"]; -}; +// Re-export for convenience +export {RouteConfig}; export function registerRoutes( - server: FastifyInstance, + server: ServerInstance, config: IChainForkConfig, api: Api, enabledNamespaces: (keyof Api)[] @@ -48,22 +45,8 @@ export function registerRoutes( throw Error(`Unknown api namespace ${namespace}`); } - registerRoutesGroup(server, routes()); - } -} - -export function registerRoutesGroup( - fastify: FastifyInstance, - // eslint-disable-next-line @typescript-eslint/no-explicit-any - routes: Record> -): void { - for (const route of Object.values(routes)) { - fastify.route({ - url: route.url, - method: route.method, - handler: route.handler, - schema: route.schema, - config: {operationId: route.id} as RouteConfig, - }); + for (const route of Object.values(routes())) { + registerRoute(server, route); + } } } diff --git a/packages/api/src/server/lightclient.ts b/packages/api/src/beacon/server/lightclient.ts similarity index 92% rename from packages/api/src/server/lightclient.ts rename to packages/api/src/beacon/server/lightclient.ts index ec7629d2ab0d..df04f88e1a22 100644 --- a/packages/api/src/server/lightclient.ts +++ b/packages/api/src/beacon/server/lightclient.ts @@ -1,7 +1,7 @@ import {IChainForkConfig} from "@chainsafe/lodestar-config"; import {serializeProof} from "@chainsafe/persistent-merkle-tree"; import {Api, ReqTypes, routesData, getReturnTypes, getReqSerializers} from "../routes/lightclient.js"; -import {ServerRoutes, getGenericJsonServer} from "./utils/index.js"; +import {ServerRoutes, getGenericJsonServer} from "../../utils/server/index.js"; export function getRoutes(config: IChainForkConfig, api: Api): ServerRoutes { const reqSerializers = getReqSerializers(); diff --git a/packages/api/src/server/lodestar.ts b/packages/api/src/beacon/server/lodestar.ts similarity index 84% rename from packages/api/src/server/lodestar.ts rename to packages/api/src/beacon/server/lodestar.ts index cc2223f1b6d4..eef9154ceaaf 100644 --- a/packages/api/src/server/lodestar.ts +++ b/packages/api/src/beacon/server/lodestar.ts @@ -1,6 +1,6 @@ import {IChainForkConfig} from "@chainsafe/lodestar-config"; import {Api, ReqTypes, routesData, getReturnTypes, getReqSerializers} from "../routes/lodestar.js"; -import {ServerRoutes, getGenericJsonServer} from "./utils/index.js"; +import {ServerRoutes, getGenericJsonServer} from "../../utils/server/index.js"; export function getRoutes(config: IChainForkConfig, api: Api): ServerRoutes { // All routes return JSON, use a server auto-generator diff --git a/packages/api/src/server/node.ts b/packages/api/src/beacon/server/node.ts similarity index 90% rename from packages/api/src/server/node.ts rename to packages/api/src/beacon/server/node.ts index 20a64fe46081..34cdf5623d57 100644 --- a/packages/api/src/server/node.ts +++ b/packages/api/src/beacon/server/node.ts @@ -1,6 +1,6 @@ import {IChainForkConfig} from "@chainsafe/lodestar-config"; import {Api, ReqTypes, routesData, getReturnTypes, getReqSerializers} from "../routes/node.js"; -import {ServerRoutes, getGenericJsonServer} from "./utils/index.js"; +import {ServerRoutes, getGenericJsonServer} from "../../utils/server/index.js"; export function getRoutes(config: IChainForkConfig, api: Api): ServerRoutes { // All routes return JSON, use a server auto-generator diff --git a/packages/api/src/server/validator.ts b/packages/api/src/beacon/server/validator.ts similarity index 84% rename from packages/api/src/server/validator.ts rename to packages/api/src/beacon/server/validator.ts index beb2ac19df82..795530346f6a 100644 --- a/packages/api/src/server/validator.ts +++ b/packages/api/src/beacon/server/validator.ts @@ -1,6 +1,6 @@ import {IChainForkConfig} from "@chainsafe/lodestar-config"; import {Api, ReqTypes, routesData, getReturnTypes, getReqSerializers} from "../routes/validator.js"; -import {ServerRoutes, getGenericJsonServer} from "./utils/index.js"; +import {ServerRoutes, getGenericJsonServer} from "../../utils/server/index.js"; export function getRoutes(config: IChainForkConfig, api: Api): ServerRoutes { // All routes return JSON, use a server auto-generator diff --git a/packages/api/src/builder/client.ts b/packages/api/src/builder/client.ts new file mode 100644 index 000000000000..a5368722539a --- /dev/null +++ b/packages/api/src/builder/client.ts @@ -0,0 +1,13 @@ +import {IChainForkConfig} from "@chainsafe/lodestar-config"; +import {IHttpClient, generateGenericJsonClient} from "../utils/client/index.js"; +import {Api, ReqTypes, routesData, getReqSerializers, getReturnTypes} from "./routes.js"; + +/** + * REST HTTP client for builder routes + */ +export function getClient(_config: IChainForkConfig, httpClient: IHttpClient): Api { + const reqSerializers = getReqSerializers(); + const returnTypes = getReturnTypes(); + // All routes return JSON, use a client auto-generator + return generateGenericJsonClient(routesData, reqSerializers, returnTypes, httpClient); +} diff --git a/packages/api/src/builder/index.ts b/packages/api/src/builder/index.ts new file mode 100644 index 000000000000..82108ed3d060 --- /dev/null +++ b/packages/api/src/builder/index.ts @@ -0,0 +1,22 @@ +import {IChainForkConfig} from "@chainsafe/lodestar-config"; +import {HttpClient, HttpClientModules, HttpClientOptions, IHttpClient} from "../utils/client/httpClient.js"; +import {Api} from "./routes.js"; +import * as builder from "./client.js"; + +// NOTE: Don't export server here so it's not bundled to all consumers + +export {Api}; + +// Note: build API does not have namespaces as routes are declared at the "root" namespace + +type ClientModules = HttpClientModules & { + config: IChainForkConfig; + httpClient?: IHttpClient; +}; + +export function getClient(opts: HttpClientOptions, modules: ClientModules): Api { + const {config} = modules; + const httpClient = modules.httpClient ?? new HttpClient(opts, modules); + + return builder.getClient(config, httpClient); +} diff --git a/packages/api/src/builder/routes.ts b/packages/api/src/builder/routes.ts new file mode 100644 index 000000000000..359ed6a7cbf1 --- /dev/null +++ b/packages/api/src/builder/routes.ts @@ -0,0 +1,28 @@ +import {ReturnTypes, RoutesData, ReqSerializers, reqEmpty, ReqEmpty} from "../utils/index.js"; +// See /packages/api/src/routes/index.ts for reasoning and instructions to add new routes + +export type Api = { + checkStatus(): Promise; +}; + +/** + * Define javascript values for each route + */ +export const routesData: RoutesData = { + checkStatus: {url: "/eth/v1/builder/status", method: "GET"}, +}; + +/* eslint-disable @typescript-eslint/naming-convention */ +export type ReqTypes = { + checkStatus: ReqEmpty; +}; + +export function getReqSerializers(): ReqSerializers { + return { + checkStatus: reqEmpty, + }; +} + +export function getReturnTypes(): ReturnTypes { + return {}; +} diff --git a/packages/api/src/index.ts b/packages/api/src/index.ts index cfa43380def8..2efc74d6f086 100644 --- a/packages/api/src/index.ts +++ b/packages/api/src/index.ts @@ -1,5 +1,6 @@ -export * as routes from "./routes/index.js"; -export * from "./interface.js"; -export {getClient, HttpClient, HttpClientOptions, HttpError} from "./client/index.js"; +// Re-exporting beacon only for backwards compatibility +export * from "./beacon/index.js"; -// Node: Don't export server here so it's not bundled to all consumers +export {HttpClient, IHttpClient, HttpClientOptions, HttpClientModules, HttpError} from "./utils/client/index.js"; + +// NOTE: Don't export server here so it's not bundled to all consumers diff --git a/packages/api/src/interface.ts b/packages/api/src/interface.ts index e54a6ade0992..5fb2db3a1a56 100644 --- a/packages/api/src/interface.ts +++ b/packages/api/src/interface.ts @@ -1,11 +1,11 @@ -import {Api as BeaconApi} from "./routes/beacon/index.js"; -import {Api as ConfigApi} from "./routes/config.js"; -import {Api as DebugApi} from "./routes/debug.js"; -import {Api as EventsApi} from "./routes/events.js"; -import {Api as LightclientApi} from "./routes/lightclient.js"; -import {Api as LodestarApi} from "./routes/lodestar.js"; -import {Api as NodeApi} from "./routes/node.js"; -import {Api as ValidatorApi} from "./routes/validator.js"; +import {Api as BeaconApi} from "./beacon/routes/beacon/index.js"; +import {Api as ConfigApi} from "./beacon/routes/config.js"; +import {Api as DebugApi} from "./beacon/routes/debug.js"; +import {Api as EventsApi} from "./beacon/routes/events.js"; +import {Api as LightclientApi} from "./beacon/routes/lightclient.js"; +import {Api as LodestarApi} from "./beacon/routes/lodestar.js"; +import {Api as NodeApi} from "./beacon/routes/node.js"; +import {Api as ValidatorApi} from "./beacon/routes/validator.js"; export type Api = { beacon: BeaconApi; diff --git a/packages/api/src/keymanager/client.ts b/packages/api/src/keymanager/client.ts index 5f5433ffa31c..923ff7dda515 100644 --- a/packages/api/src/keymanager/client.ts +++ b/packages/api/src/keymanager/client.ts @@ -1,5 +1,5 @@ import {IChainForkConfig} from "@chainsafe/lodestar-config"; -import {IHttpClient, generateGenericJsonClient} from "../client/utils/index.js"; +import {IHttpClient, generateGenericJsonClient} from "../utils/client/index.js"; import {Api, ReqTypes, routesData, getReqSerializers, getReturnTypes} from "./routes.js"; export function getClient(_config: IChainForkConfig, httpClient: IHttpClient): Api { diff --git a/packages/api/src/keymanager/index.ts b/packages/api/src/keymanager/index.ts index 9ef6a6dd1528..544ec946635e 100644 --- a/packages/api/src/keymanager/index.ts +++ b/packages/api/src/keymanager/index.ts @@ -1,16 +1,24 @@ import {IChainForkConfig} from "@chainsafe/lodestar-config"; -import {HttpClient, HttpClientOptions} from "../client/index.js"; -import {IHttpClient} from "../client/utils/index.js"; +import {} from "../beacon/client/index.js"; +import {IHttpClient, HttpClient, HttpClientModules, HttpClientOptions} from "../utils/client/index.js"; import {Api} from "./routes.js"; import * as keymanager from "./client.js"; +// NOTE: Don't export server here so it's not bundled to all consumers + export {ImportStatus, DeletionStatus, KeystoreStr, SlashingProtectionData, PubkeyHex, Api} from "./routes.js"; +type ClientModules = HttpClientModules & { + config: IChainForkConfig; + httpClient?: IHttpClient; +}; + /** * REST HTTP client for all keymanager routes */ -export function getClient(config: IChainForkConfig, opts: HttpClientOptions, httpClient?: IHttpClient): Api { - if (!httpClient) httpClient = new HttpClient(opts); +export function getClient(opts: HttpClientOptions, modules: ClientModules): Api { + const {config} = modules; + const httpClient = modules.httpClient ?? new HttpClient(opts, modules); return keymanager.getClient(config, httpClient); } diff --git a/packages/api/src/keymanager/server.ts b/packages/api/src/keymanager/server.ts index cf53050afe18..c0140a4b52c8 100644 --- a/packages/api/src/keymanager/server.ts +++ b/packages/api/src/keymanager/server.ts @@ -1,8 +1,19 @@ import {IChainForkConfig} from "@chainsafe/lodestar-config"; -import {ServerRoutes, getGenericJsonServer} from "../server/utils/index.js"; +import {ServerInstance, ServerRoutes, getGenericJsonServer, registerRoute, RouteConfig} from "../utils/server/index.js"; import {Api, ReqTypes, routesData, getReturnTypes, getReqSerializers} from "./routes.js"; +// Re-export for convenience +export {RouteConfig}; + export function getRoutes(config: IChainForkConfig, api: Api): ServerRoutes { // All routes return JSON, use a server auto-generator return getGenericJsonServer({routesData, getReturnTypes, getReqSerializers}, config, api); } + +export function registerRoutes(server: ServerInstance, config: IChainForkConfig, api: Api): void { + const routes = getRoutes(config, api); + + for (const route of Object.values(routes)) { + registerRoute(server, route); + } +} diff --git a/packages/api/src/server/utils/index.ts b/packages/api/src/server/utils/index.ts deleted file mode 100644 index 93c7ba6928f9..000000000000 --- a/packages/api/src/server/utils/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from "./server.js"; diff --git a/packages/api/src/client/utils/client.ts b/packages/api/src/utils/client/client.ts similarity index 96% rename from packages/api/src/client/utils/client.ts rename to packages/api/src/utils/client/client.ts index 24606b1cdd7d..32d6eefb2f0b 100644 --- a/packages/api/src/client/utils/client.ts +++ b/packages/api/src/utils/client/client.ts @@ -1,5 +1,5 @@ import {mapValues} from "@chainsafe/lodestar-utils"; -import {compileRouteUrlFormater} from "../../utils/urlFormat.js"; +import {compileRouteUrlFormater} from "../urlFormat.js"; import { RouteDef, ReqGeneric, @@ -9,7 +9,7 @@ import { ReqSerializer, ReqSerializers, RoutesData, -} from "../../utils/types.js"; +} from "../types.js"; import {FetchOpts, IHttpClient} from "./httpClient.js"; // See /packages/api/src/routes/index.ts for reasoning diff --git a/packages/api/src/client/utils/format.ts b/packages/api/src/utils/client/format.ts similarity index 100% rename from packages/api/src/client/utils/format.ts rename to packages/api/src/utils/client/format.ts diff --git a/packages/api/src/client/utils/httpClient.ts b/packages/api/src/utils/client/httpClient.ts similarity index 98% rename from packages/api/src/client/utils/httpClient.ts rename to packages/api/src/utils/client/httpClient.ts index f82ab7e1f687..ace39b2121f7 100644 --- a/packages/api/src/client/utils/httpClient.ts +++ b/packages/api/src/utils/client/httpClient.ts @@ -1,6 +1,6 @@ import {fetch} from "cross-fetch"; import {ErrorAborted, ILogger, TimeoutError} from "@chainsafe/lodestar-utils"; -import {ReqGeneric, RouteDef} from "../../utils/index.js"; +import {ReqGeneric, RouteDef} from "../index.js"; import {stringifyQuery, urlJoin} from "./format.js"; import {Metrics} from "./metrics.js"; diff --git a/packages/api/src/client/utils/index.ts b/packages/api/src/utils/client/index.ts similarity index 100% rename from packages/api/src/client/utils/index.ts rename to packages/api/src/utils/client/index.ts diff --git a/packages/api/src/client/utils/metrics.ts b/packages/api/src/utils/client/metrics.ts similarity index 100% rename from packages/api/src/client/utils/metrics.ts rename to packages/api/src/utils/client/metrics.ts diff --git a/packages/api/src/server/utils/server.ts b/packages/api/src/utils/server/genericJsonServer.ts similarity index 65% rename from packages/api/src/server/utils/server.ts rename to packages/api/src/utils/server/genericJsonServer.ts index 2cf1a6649d26..96864b73150b 100644 --- a/packages/api/src/server/utils/server.ts +++ b/packages/api/src/utils/server/genericJsonServer.ts @@ -1,36 +1,14 @@ -import * as fastify from "fastify"; import {mapValues} from "@chainsafe/lodestar-utils"; // eslint-disable-next-line import/no-extraneous-dependencies import {IChainForkConfig} from "@chainsafe/lodestar-config"; -import {ReqGeneric, RouteGeneric, ReturnTypes, TypeJson, Resolves, RouteGroupDefinition} from "../../utils/types.js"; -import {getFastifySchema} from "../../utils/schema.js"; +import {ReqGeneric, RouteGeneric, ReturnTypes, TypeJson, Resolves, RouteGroupDefinition} from "../types.js"; +import {getFastifySchema} from "../schema.js"; +import {ServerRoute} from "./types.js"; // See /packages/api/src/routes/index.ts for reasoning /* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/naming-convention */ -export type ServerRoute = { - url: string; - method: fastify.HTTPMethods; - handler: FastifyHandler; - schema?: fastify.FastifySchema; - /** OperationId as defined in https://github.com/ethereum/beacon-APIs/blob/v2.1.0/apis/beacon/blocks/attestations.yaml#L2 */ - id: string; -}; - -/** Adaptor for Fastify v3.x.x route type which has a ton of arguments */ -type FastifyHandler = fastify.RouteHandlerMethod< - fastify.RawServerDefault, - fastify.RawRequestDefaultExpression, - fastify.RawReplyDefaultExpression, - { - Body: Req["body"]; - Querystring: Req["query"]; - Params: Req["params"]; - }, - fastify.ContextConfigDefault ->; - export type ServerRoutes, ReqTypes extends {[K in keyof Api]: ReqGeneric}> = { [K in keyof Api]: ServerRoute; }; diff --git a/packages/api/src/utils/server/index.ts b/packages/api/src/utils/server/index.ts new file mode 100644 index 000000000000..5a0227a01916 --- /dev/null +++ b/packages/api/src/utils/server/index.ts @@ -0,0 +1,3 @@ +export * from "./genericJsonServer.js"; +export * from "./registerRoute.js"; +export * from "./types.js"; diff --git a/packages/api/src/utils/server/registerRoute.ts b/packages/api/src/utils/server/registerRoute.ts new file mode 100644 index 000000000000..8a3cbf01b34b --- /dev/null +++ b/packages/api/src/utils/server/registerRoute.ts @@ -0,0 +1,15 @@ +import {ServerInstance, RouteConfig, ServerRoute} from "./types.js"; + +export function registerRoute( + server: ServerInstance, + // eslint-disable-next-line @typescript-eslint/no-explicit-any + route: ServerRoute +): void { + server.route({ + url: route.url, + method: route.method, + handler: route.handler, + schema: route.schema, + config: {operationId: route.id} as RouteConfig, + }); +} diff --git a/packages/api/src/utils/server/types.ts b/packages/api/src/utils/server/types.ts new file mode 100644 index 000000000000..e4c390db0c73 --- /dev/null +++ b/packages/api/src/utils/server/types.ts @@ -0,0 +1,34 @@ +import {FastifyInstance} from "fastify"; +import * as fastify from "fastify"; +// eslint-disable-next-line import/no-extraneous-dependencies +import {ReqGeneric} from "../types.js"; + +export type ServerInstance = FastifyInstance; + +export type RouteConfig = { + operationId: ServerRoute["id"]; +}; + +export type ServerRoute = { + url: string; + method: fastify.HTTPMethods; + handler: FastifyHandler; + schema?: fastify.FastifySchema; + /** OperationId as defined in https://github.com/ethereum/beacon-APIs/blob/v2.1.0/apis/beacon/blocks/attestations.yaml#L2 */ + id: string; +}; + +/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/naming-convention */ + +/** Adaptor for Fastify v3.x.x route type which has a ton of arguments */ +export type FastifyHandler = fastify.RouteHandlerMethod< + fastify.RawServerDefault, + fastify.RawRequestDefaultExpression, + fastify.RawReplyDefaultExpression, + { + Body: Req["body"]; + Querystring: Req["query"]; + Params: Req["params"]; + }, + fastify.ContextConfigDefault +>; diff --git a/packages/api/test/unit/beacon.test.ts b/packages/api/test/unit/beacon.test.ts index 67c1feff02cd..7cbd776854be 100644 --- a/packages/api/test/unit/beacon.test.ts +++ b/packages/api/test/unit/beacon.test.ts @@ -2,9 +2,9 @@ import {ForkName} from "@chainsafe/lodestar-params"; import {ssz} from "@chainsafe/lodestar-types"; import {config} from "@chainsafe/lodestar-config/default"; import {toHexString} from "@chainsafe/ssz"; -import {Api, ReqTypes, BlockHeaderResponse, ValidatorResponse} from "../../src/routes/beacon/index.js"; -import {getClient} from "../../src/client/beacon.js"; -import {getRoutes} from "../../src/server/beacon.js"; +import {Api, ReqTypes, BlockHeaderResponse, ValidatorResponse} from "../../src/beacon/routes/beacon/index.js"; +import {getClient} from "../../src/beacon/client/beacon.js"; +import {getRoutes} from "../../src/beacon/server/beacon.js"; import {runGenericServerTest} from "../utils/genericServerTest.js"; describe("beacon", () => { diff --git a/packages/api/test/unit/client/format.test.ts b/packages/api/test/unit/client/format.test.ts index 818ba5ac549f..0e388c3cb825 100644 --- a/packages/api/test/unit/client/format.test.ts +++ b/packages/api/test/unit/client/format.test.ts @@ -1,6 +1,6 @@ import {expect} from "chai"; -import {EventType} from "../../../src/routes/events.js"; -import {stringifyQuery} from "../../../src/client/utils/format.js"; +import {EventType} from "../../../src/beacon/routes/events.js"; +import {stringifyQuery} from "../../../src/utils/client/format.js"; describe("client / utils / format", () => { it("Should repeat topic query", () => { diff --git a/packages/api/test/unit/client/httpClient.test.ts b/packages/api/test/unit/client/httpClient.test.ts index 4ff890295101..308874ece09f 100644 --- a/packages/api/test/unit/client/httpClient.test.ts +++ b/packages/api/test/unit/client/httpClient.test.ts @@ -4,7 +4,7 @@ import chaiAsPromised from "chai-as-promised"; import fastify, {RouteOptions} from "fastify"; import {ErrorAborted, TimeoutError} from "@chainsafe/lodestar-utils"; import {AbortController} from "@chainsafe/abort-controller"; -import {HttpClient, HttpError} from "../../../src/client/utils/index.js"; +import {HttpClient, HttpError} from "../../../src/utils/client/index.js"; chai.use(chaiAsPromised); diff --git a/packages/api/test/unit/config.test.ts b/packages/api/test/unit/config.test.ts index f250b6d27d1c..a5deeac13d35 100644 --- a/packages/api/test/unit/config.test.ts +++ b/packages/api/test/unit/config.test.ts @@ -3,9 +3,9 @@ import {ssz} from "@chainsafe/lodestar-types"; import {chainConfigToJson} from "@chainsafe/lodestar-config"; import {config, chainConfig} from "@chainsafe/lodestar-config/default"; import {activePreset, presetToJson} from "@chainsafe/lodestar-params"; -import {Api, ReqTypes, getReturnTypes} from "../../src/routes/config.js"; -import {getClient} from "../../src/client/config.js"; -import {getRoutes} from "../../src/server/config.js"; +import {Api, ReqTypes, getReturnTypes} from "../../src/beacon/routes/config.js"; +import {getClient} from "../../src/beacon/client/config.js"; +import {getRoutes} from "../../src/beacon/server/config.js"; import {runGenericServerTest} from "../utils/genericServerTest.js"; /* eslint-disable @typescript-eslint/naming-convention */ diff --git a/packages/api/test/unit/debug.test.ts b/packages/api/test/unit/debug.test.ts index 05705ce4815c..4fb50c5e64ea 100644 --- a/packages/api/test/unit/debug.test.ts +++ b/packages/api/test/unit/debug.test.ts @@ -3,13 +3,13 @@ import {ForkName} from "@chainsafe/lodestar-params"; import {ssz} from "@chainsafe/lodestar-types"; import {toHexString} from "@chainsafe/ssz"; import {config} from "@chainsafe/lodestar-config/default"; -import {Api, ReqTypes, routesData} from "../../src/routes/debug.js"; -import {getClient} from "../../src/client/debug.js"; -import {getRoutes} from "../../src/server/debug.js"; +import {Api, ReqTypes, routesData} from "../../src/beacon/routes/debug.js"; +import {getClient} from "../../src/beacon/client/debug.js"; +import {getRoutes} from "../../src/beacon/server/debug.js"; import {runGenericServerTest} from "../utils/genericServerTest.js"; import {getMockApi, getTestServer} from "../utils/utils.js"; -import {registerRoutesGroup} from "../../src/server/index.js"; -import {HttpClient} from "../../src/index.js"; +import {registerRoutesGroup} from "../../src/beacon/server/index.js"; +import {HttpClient} from "../../src/utils/client/httpClient.js"; describe("debug", function () { // Extend timeout since states are very big diff --git a/packages/api/test/unit/events.test.ts b/packages/api/test/unit/events.test.ts index 70b5351e8c41..7b99c9846f8b 100644 --- a/packages/api/test/unit/events.test.ts +++ b/packages/api/test/unit/events.test.ts @@ -2,11 +2,11 @@ import {expect} from "chai"; import {AbortController} from "@chainsafe/abort-controller"; import {sleep} from "@chainsafe/lodestar-utils"; import {config} from "@chainsafe/lodestar-config/default"; -import {Api, routesData, EventType, BeaconEvent} from "../../src/routes/events.js"; -import {getClient} from "../../src/client/events.js"; -import {getRoutes} from "../../src/server/events.js"; +import {Api, routesData, EventType, BeaconEvent} from "../../src/beacon/routes/events.js"; +import {getClient} from "../../src/beacon/client/events.js"; +import {getRoutes} from "../../src/beacon/server/events.js"; import {getMockApi, getTestServer} from "../utils/utils.js"; -import {registerRoutesGroup} from "../../src/server/index.js"; +import {registerRoutesGroup} from "../../src/beacon/server/index.js"; describe("events", () => { const rootHex = "0x" + "01".repeat(32); diff --git a/packages/api/test/unit/lightclient.test.ts b/packages/api/test/unit/lightclient.test.ts index f9cc42831efd..ee4ba26ed893 100644 --- a/packages/api/test/unit/lightclient.test.ts +++ b/packages/api/test/unit/lightclient.test.ts @@ -2,9 +2,9 @@ import {ssz} from "@chainsafe/lodestar-types"; import {config} from "@chainsafe/lodestar-config/default"; import {ProofType} from "@chainsafe/persistent-merkle-tree"; import {toHexString} from "@chainsafe/ssz"; -import {Api, ReqTypes} from "../../src/routes/lightclient.js"; -import {getClient} from "../../src/client/lightclient.js"; -import {getRoutes} from "../../src/server/lightclient.js"; +import {Api, ReqTypes} from "../../src/beacon/routes/lightclient.js"; +import {getClient} from "../../src/beacon/client/lightclient.js"; +import {getRoutes} from "../../src/beacon/server/lightclient.js"; import {runGenericServerTest} from "../utils/genericServerTest.js"; const root = Uint8Array.from(Buffer.alloc(32, 1)); diff --git a/packages/api/test/unit/node.test.ts b/packages/api/test/unit/node.test.ts index 834f2affdb8a..042c799a1e6f 100644 --- a/packages/api/test/unit/node.test.ts +++ b/packages/api/test/unit/node.test.ts @@ -1,8 +1,8 @@ import {ssz} from "@chainsafe/lodestar-types"; import {config} from "@chainsafe/lodestar-config/default"; -import {Api, ReqTypes, NodePeer} from "../../src/routes/node.js"; -import {getClient} from "../../src/client/node.js"; -import {getRoutes} from "../../src/server/node.js"; +import {Api, ReqTypes, NodePeer} from "../../src/beacon/routes/node.js"; +import {getClient} from "../../src/beacon/client/node.js"; +import {getRoutes} from "../../src/beacon/server/node.js"; import {runGenericServerTest} from "../utils/genericServerTest.js"; describe("node", () => { diff --git a/packages/api/test/unit/validator.test.ts b/packages/api/test/unit/validator.test.ts index b3bcbe6fb879..b045e4e10183 100644 --- a/packages/api/test/unit/validator.test.ts +++ b/packages/api/test/unit/validator.test.ts @@ -1,9 +1,9 @@ import {ForkName} from "@chainsafe/lodestar-params"; import {ssz} from "@chainsafe/lodestar-types"; import {config} from "@chainsafe/lodestar-config/default"; -import {Api, ReqTypes} from "../../src/routes/validator.js"; -import {getClient} from "../../src/client/validator.js"; -import {getRoutes} from "../../src/server/validator.js"; +import {Api, ReqTypes} from "../../src/beacon/routes/validator.js"; +import {getClient} from "../../src/beacon/client/validator.js"; +import {getRoutes} from "../../src/beacon/server/validator.js"; import {runGenericServerTest} from "../utils/genericServerTest.js"; const ZERO_HASH = Buffer.alloc(32, 0); diff --git a/packages/api/test/utils/genericServerTest.ts b/packages/api/test/utils/genericServerTest.ts index df6d2ffa0c40..ead71b4365a7 100644 --- a/packages/api/test/utils/genericServerTest.ts +++ b/packages/api/test/utils/genericServerTest.ts @@ -1,9 +1,9 @@ import {expect} from "chai"; import {IChainForkConfig} from "@chainsafe/lodestar-config"; import {RouteGeneric, ReqGeneric, Resolves} from "../../src/utils/index.js"; -import {FetchOpts, HttpClient, IHttpClient} from "../../src/client/utils/index.js"; -import {ServerRoutes} from "../../src/server/utils/index.js"; -import {registerRoutesGroup} from "../../src/server/index.js"; +import {FetchOpts, HttpClient, IHttpClient} from "../../src/utils/client/index.js"; +import {ServerRoutes} from "../../src/utils/server/genericJsonServer.js"; +import {registerRoutesGroup} from "../../src/beacon/server/index.js"; import {getMockApi, getTestServer} from "./utils.js"; type IgnoreVoid = T extends void ? undefined : T; diff --git a/packages/keymanager-server/src/server.ts b/packages/keymanager-server/src/server.ts index ba53b0080e5f..94de1cee0dd9 100644 --- a/packages/keymanager-server/src/server.ts +++ b/packages/keymanager-server/src/server.ts @@ -7,10 +7,8 @@ import fastify, {FastifyError, FastifyInstance} from "fastify"; import fastifyCors from "fastify-cors"; import bearerAuthPlugin from "fastify-bearer-auth"; import {toHexString} from "@chainsafe/ssz"; -export {allNamespaces} from "@chainsafe/lodestar-api"; import {Api} from "@chainsafe/lodestar-api/keymanager"; -import {getRoutes} from "@chainsafe/lodestar-api/keymanager/server"; -import {registerRoutesGroup, RouteConfig} from "@chainsafe/lodestar-api/server"; +import {registerRoutes, RouteConfig} from "@chainsafe/lodestar-api/keymanager/server"; import {ErrorAborted, ILogger} from "@chainsafe/lodestar-utils"; import {IChainForkConfig} from "@chainsafe/lodestar-config"; @@ -76,8 +74,7 @@ export class KeymanagerServer { }); // Instantiate and register the keymanager routes - const routes = getRoutes(modules.config, modules.api); - registerRoutesGroup(server, routes); + registerRoutes(server, modules.config, modules.api); // To parse our ApiError -> statusCode server.setErrorHandler((err, req, res) => { diff --git a/packages/light-client/test/lightclientApiServer.ts b/packages/light-client/test/lightclientApiServer.ts index 266c14f0c029..8bb49231b57f 100644 --- a/packages/light-client/test/lightclientApiServer.ts +++ b/packages/light-client/test/lightclientApiServer.ts @@ -2,7 +2,7 @@ import querystring from "querystring"; import fastify, {FastifyInstance} from "fastify"; import fastifyCors from "fastify-cors"; import {Api, routes} from "@chainsafe/lodestar-api"; -import {registerRoutes} from "@chainsafe/lodestar-api/server"; +import {registerRoutes} from "@chainsafe/lodestar-api/beacon/server"; import {IChainForkConfig} from "@chainsafe/lodestar-config"; import {JsonPath} from "@chainsafe/ssz"; import {altair, RootHex, SyncPeriod} from "@chainsafe/lodestar-types"; diff --git a/packages/lodestar/src/api/rest/index.ts b/packages/lodestar/src/api/rest/index.ts index 34c3d0aab6b9..e47d511b656e 100644 --- a/packages/lodestar/src/api/rest/index.ts +++ b/packages/lodestar/src/api/rest/index.ts @@ -2,7 +2,7 @@ import querystring from "querystring"; import fastify, {FastifyError, FastifyInstance} from "fastify"; import fastifyCors from "fastify-cors"; import {Api} from "@chainsafe/lodestar-api"; -import {registerRoutes, RouteConfig} from "@chainsafe/lodestar-api/server"; +import {registerRoutes, RouteConfig} from "@chainsafe/lodestar-api/beacon/server"; import {ErrorAborted, ILogger} from "@chainsafe/lodestar-utils"; import {IBeaconConfig} from "@chainsafe/lodestar-config"; import {IMetrics} from "../../metrics/index.js"; From 219746d823685b59d8bb1cd149a42e8649f24bfc Mon Sep 17 00:00:00 2001 From: dapplion <35266934+dapplion@users.noreply.github.com> Date: Wed, 15 Jun 2022 07:50:30 +0200 Subject: [PATCH 2/4] Fix imports --- .../api/src/keymanager/{server.ts => server/index.ts} | 10 ++++++++-- packages/api/test/unit/keymanager.test.ts | 2 +- 2 files changed, 9 insertions(+), 3 deletions(-) rename packages/api/src/keymanager/{server.ts => server/index.ts} (80%) diff --git a/packages/api/src/keymanager/server.ts b/packages/api/src/keymanager/server/index.ts similarity index 80% rename from packages/api/src/keymanager/server.ts rename to packages/api/src/keymanager/server/index.ts index c0140a4b52c8..b73866fb7a57 100644 --- a/packages/api/src/keymanager/server.ts +++ b/packages/api/src/keymanager/server/index.ts @@ -1,6 +1,12 @@ import {IChainForkConfig} from "@chainsafe/lodestar-config"; -import {ServerInstance, ServerRoutes, getGenericJsonServer, registerRoute, RouteConfig} from "../utils/server/index.js"; -import {Api, ReqTypes, routesData, getReturnTypes, getReqSerializers} from "./routes.js"; +import { + ServerInstance, + ServerRoutes, + getGenericJsonServer, + registerRoute, + RouteConfig, +} from "../../utils/server/index.js"; +import {Api, ReqTypes, routesData, getReturnTypes, getReqSerializers} from "../routes.js"; // Re-export for convenience export {RouteConfig}; diff --git a/packages/api/test/unit/keymanager.test.ts b/packages/api/test/unit/keymanager.test.ts index 905a67cd00fa..661a1f9e3321 100644 --- a/packages/api/test/unit/keymanager.test.ts +++ b/packages/api/test/unit/keymanager.test.ts @@ -1,7 +1,7 @@ import {config} from "@chainsafe/lodestar-config/default"; import {Api, DeletionStatus, ImportStatus, ReqTypes} from "../../src/keymanager/routes.js"; import {getClient} from "../../src/keymanager/client.js"; -import {getRoutes} from "../../src/keymanager/server.js"; +import {getRoutes} from "../../src/keymanager/server/index.js"; import {runGenericServerTest} from "../utils/genericServerTest.js"; describe("keymanager", () => { From c1be1637e37da9fa9e5df40a39624ea5d847bac9 Mon Sep 17 00:00:00 2001 From: dapplion <35266934+dapplion@users.noreply.github.com> Date: Wed, 15 Jun 2022 07:55:00 +0200 Subject: [PATCH 3/4] Move beacon API interface to routes --- packages/api/src/beacon/client/index.ts | 2 +- packages/api/src/beacon/index.ts | 21 ++-------------- packages/api/src/beacon/routes/index.ts | 20 +++++++++++++++ packages/api/src/beacon/server/index.ts | 2 +- packages/api/src/interface.ts | 33 ------------------------- 5 files changed, 24 insertions(+), 54 deletions(-) delete mode 100644 packages/api/src/interface.ts diff --git a/packages/api/src/beacon/client/index.ts b/packages/api/src/beacon/client/index.ts index afec1f0da089..6510858d5b7a 100644 --- a/packages/api/src/beacon/client/index.ts +++ b/packages/api/src/beacon/client/index.ts @@ -1,5 +1,5 @@ import {IChainForkConfig} from "@chainsafe/lodestar-config"; -import {Api} from "../../interface.js"; +import {Api} from "../routes/index.js"; import {IHttpClient, HttpClient, HttpClientOptions, HttpClientModules} from "../../utils/client/index.js"; import * as beacon from "./beacon.js"; diff --git a/packages/api/src/beacon/index.ts b/packages/api/src/beacon/index.ts index 6c64968a645d..d94f09edcd23 100644 --- a/packages/api/src/beacon/index.ts +++ b/packages/api/src/beacon/index.ts @@ -1,27 +1,10 @@ -import {Api as BeaconApi} from "./routes/beacon/index.js"; -import {Api as ConfigApi} from "./routes/config.js"; -import {Api as DebugApi} from "./routes/debug.js"; -import {Api as EventsApi} from "./routes/events.js"; -import {Api as LightclientApi} from "./routes/lightclient.js"; -import {Api as LodestarApi} from "./routes/lodestar.js"; -import {Api as NodeApi} from "./routes/node.js"; -import {Api as ValidatorApi} from "./routes/validator.js"; +import {Api} from "./routes/index.js"; // NOTE: Don't export server here so it's not bundled to all consumers export * as routes from "./routes/index.js"; export {getClient} from "./client/index.js"; - -export type Api = { - beacon: BeaconApi; - config: ConfigApi; - debug: DebugApi; - events: EventsApi; - lightclient: LightclientApi; - lodestar: LodestarApi; - node: NodeApi; - validator: ValidatorApi; -}; +export {Api}; // Declare namespaces for CLI options export type ApiNamespace = keyof Api; diff --git a/packages/api/src/beacon/routes/index.ts b/packages/api/src/beacon/routes/index.ts index f35b784b48d0..da320080042b 100644 --- a/packages/api/src/beacon/routes/index.ts +++ b/packages/api/src/beacon/routes/index.ts @@ -1,3 +1,12 @@ +import {Api as BeaconApi} from "./beacon/index.js"; +import {Api as ConfigApi} from "./config.js"; +import {Api as DebugApi} from "./debug.js"; +import {Api as EventsApi} from "./events.js"; +import {Api as LightclientApi} from "./lightclient.js"; +import {Api as LodestarApi} from "./lodestar.js"; +import {Api as NodeApi} from "./node.js"; +import {Api as ValidatorApi} from "./validator.js"; + export * as beacon from "./beacon/index.js"; export * as config from "./config.js"; export * as debug from "./debug.js"; @@ -7,6 +16,17 @@ export * as lodestar from "./lodestar.js"; export * as node from "./node.js"; export * as validator from "./validator.js"; +export type Api = { + beacon: BeaconApi; + config: ConfigApi; + debug: DebugApi; + events: EventsApi; + lightclient: LightclientApi; + lodestar: LodestarApi; + node: NodeApi; + validator: ValidatorApi; +}; + // Reasoning of the API definitions // ================================ // diff --git a/packages/api/src/beacon/server/index.ts b/packages/api/src/beacon/server/index.ts index 7617d99b1ca3..ded4f602b87f 100644 --- a/packages/api/src/beacon/server/index.ts +++ b/packages/api/src/beacon/server/index.ts @@ -1,5 +1,5 @@ import {IChainForkConfig} from "@chainsafe/lodestar-config"; -import {Api} from "../../interface.js"; +import {Api} from "../routes/index.js"; import {ServerInstance, ServerRoute, RouteConfig, registerRoute} from "../../utils/server/index.js"; import * as beacon from "./beacon.js"; diff --git a/packages/api/src/interface.ts b/packages/api/src/interface.ts deleted file mode 100644 index 5fb2db3a1a56..000000000000 --- a/packages/api/src/interface.ts +++ /dev/null @@ -1,33 +0,0 @@ -import {Api as BeaconApi} from "./beacon/routes/beacon/index.js"; -import {Api as ConfigApi} from "./beacon/routes/config.js"; -import {Api as DebugApi} from "./beacon/routes/debug.js"; -import {Api as EventsApi} from "./beacon/routes/events.js"; -import {Api as LightclientApi} from "./beacon/routes/lightclient.js"; -import {Api as LodestarApi} from "./beacon/routes/lodestar.js"; -import {Api as NodeApi} from "./beacon/routes/node.js"; -import {Api as ValidatorApi} from "./beacon/routes/validator.js"; - -export type Api = { - beacon: BeaconApi; - config: ConfigApi; - debug: DebugApi; - events: EventsApi; - lightclient: LightclientApi; - lodestar: LodestarApi; - node: NodeApi; - validator: ValidatorApi; -}; - -// Declare namespaces for CLI options -export type ApiNamespace = keyof Api; -const allNamespacesObj: {[K in keyof Api]: true} = { - beacon: true, - config: true, - debug: true, - events: true, - lightclient: true, - lodestar: true, - node: true, - validator: true, -}; -export const allNamespaces = Object.keys(allNamespacesObj) as ApiNamespace[]; From 1fd542cea3c4e3ff4c3bc807184004f1bc7eaf02 Mon Sep 17 00:00:00 2001 From: dapplion <35266934+dapplion@users.noreply.github.com> Date: Wed, 15 Jun 2022 08:06:30 +0200 Subject: [PATCH 4/4] Move tests to same src dir structure --- packages/api/src/builder/server/index.ts | 25 +++++++++++++++++++ .../api/test/unit/{ => beacon}/beacon.test.ts | 10 ++++---- .../api/test/unit/{ => beacon}/config.test.ts | 10 ++++---- .../api/test/unit/{ => beacon}/debug.test.ts | 21 ++++++++-------- .../api/test/unit/{ => beacon}/events.test.ts | 17 +++++++------ .../unit/{ => beacon}/lightclient.test.ts | 10 ++++---- .../api/test/unit/{ => beacon}/node.test.ts | 10 ++++---- .../test/unit/{ => beacon}/validator.test.ts | 10 ++++---- .../api/test/unit/builder/builder.test.ts | 14 +++++++++++ .../unit/{ => keymanager}/keymanager.test.ts | 8 +++--- packages/api/test/utils/genericServerTest.ts | 7 +++--- 11 files changed, 92 insertions(+), 50 deletions(-) create mode 100644 packages/api/src/builder/server/index.ts rename packages/api/test/unit/{ => beacon}/beacon.test.ts (94%) rename packages/api/test/unit/{ => beacon}/config.test.ts (83%) rename packages/api/test/unit/{ => beacon}/debug.test.ts (73%) rename packages/api/test/unit/{ => beacon}/events.test.ts (82%) rename packages/api/test/unit/{ => beacon}/lightclient.test.ts (85%) rename packages/api/test/unit/{ => beacon}/node.test.ts (81%) rename packages/api/test/unit/{ => beacon}/validator.test.ts (90%) create mode 100644 packages/api/test/unit/builder/builder.test.ts rename packages/api/test/unit/{ => keymanager}/keymanager.test.ts (79%) diff --git a/packages/api/src/builder/server/index.ts b/packages/api/src/builder/server/index.ts new file mode 100644 index 000000000000..b73866fb7a57 --- /dev/null +++ b/packages/api/src/builder/server/index.ts @@ -0,0 +1,25 @@ +import {IChainForkConfig} from "@chainsafe/lodestar-config"; +import { + ServerInstance, + ServerRoutes, + getGenericJsonServer, + registerRoute, + RouteConfig, +} from "../../utils/server/index.js"; +import {Api, ReqTypes, routesData, getReturnTypes, getReqSerializers} from "../routes.js"; + +// Re-export for convenience +export {RouteConfig}; + +export function getRoutes(config: IChainForkConfig, api: Api): ServerRoutes { + // All routes return JSON, use a server auto-generator + return getGenericJsonServer({routesData, getReturnTypes, getReqSerializers}, config, api); +} + +export function registerRoutes(server: ServerInstance, config: IChainForkConfig, api: Api): void { + const routes = getRoutes(config, api); + + for (const route of Object.values(routes)) { + registerRoute(server, route); + } +} diff --git a/packages/api/test/unit/beacon.test.ts b/packages/api/test/unit/beacon/beacon.test.ts similarity index 94% rename from packages/api/test/unit/beacon.test.ts rename to packages/api/test/unit/beacon/beacon.test.ts index 7cbd776854be..bb5ceebddee4 100644 --- a/packages/api/test/unit/beacon.test.ts +++ b/packages/api/test/unit/beacon/beacon.test.ts @@ -2,12 +2,12 @@ import {ForkName} from "@chainsafe/lodestar-params"; import {ssz} from "@chainsafe/lodestar-types"; import {config} from "@chainsafe/lodestar-config/default"; import {toHexString} from "@chainsafe/ssz"; -import {Api, ReqTypes, BlockHeaderResponse, ValidatorResponse} from "../../src/beacon/routes/beacon/index.js"; -import {getClient} from "../../src/beacon/client/beacon.js"; -import {getRoutes} from "../../src/beacon/server/beacon.js"; -import {runGenericServerTest} from "../utils/genericServerTest.js"; +import {Api, ReqTypes, BlockHeaderResponse, ValidatorResponse} from "../../../src/beacon/routes/beacon/index.js"; +import {getClient} from "../../../src/beacon/client/beacon.js"; +import {getRoutes} from "../../../src/beacon/server/beacon.js"; +import {runGenericServerTest} from "../../utils/genericServerTest.js"; -describe("beacon", () => { +describe("beacon / beacon", () => { const root = Buffer.alloc(32, 1); const balance = 32e9; const pubkeyHex = toHexString(Buffer.alloc(48, 1)); diff --git a/packages/api/test/unit/config.test.ts b/packages/api/test/unit/beacon/config.test.ts similarity index 83% rename from packages/api/test/unit/config.test.ts rename to packages/api/test/unit/beacon/config.test.ts index a5deeac13d35..c30996c00cad 100644 --- a/packages/api/test/unit/config.test.ts +++ b/packages/api/test/unit/beacon/config.test.ts @@ -3,14 +3,14 @@ import {ssz} from "@chainsafe/lodestar-types"; import {chainConfigToJson} from "@chainsafe/lodestar-config"; import {config, chainConfig} from "@chainsafe/lodestar-config/default"; import {activePreset, presetToJson} from "@chainsafe/lodestar-params"; -import {Api, ReqTypes, getReturnTypes} from "../../src/beacon/routes/config.js"; -import {getClient} from "../../src/beacon/client/config.js"; -import {getRoutes} from "../../src/beacon/server/config.js"; -import {runGenericServerTest} from "../utils/genericServerTest.js"; +import {Api, ReqTypes, getReturnTypes} from "../../../src/beacon/routes/config.js"; +import {getClient} from "../../../src/beacon/client/config.js"; +import {getRoutes} from "../../../src/beacon/server/config.js"; +import {runGenericServerTest} from "../../utils/genericServerTest.js"; /* eslint-disable @typescript-eslint/naming-convention */ -describe("config", () => { +describe("beacon / config", () => { const configJson = chainConfigToJson(chainConfig); const presetJson = presetToJson(activePreset); const jsonSpec = {...configJson, ...presetJson}; diff --git a/packages/api/test/unit/debug.test.ts b/packages/api/test/unit/beacon/debug.test.ts similarity index 73% rename from packages/api/test/unit/debug.test.ts rename to packages/api/test/unit/beacon/debug.test.ts index 4fb50c5e64ea..33e3a152fec2 100644 --- a/packages/api/test/unit/debug.test.ts +++ b/packages/api/test/unit/beacon/debug.test.ts @@ -3,15 +3,15 @@ import {ForkName} from "@chainsafe/lodestar-params"; import {ssz} from "@chainsafe/lodestar-types"; import {toHexString} from "@chainsafe/ssz"; import {config} from "@chainsafe/lodestar-config/default"; -import {Api, ReqTypes, routesData} from "../../src/beacon/routes/debug.js"; -import {getClient} from "../../src/beacon/client/debug.js"; -import {getRoutes} from "../../src/beacon/server/debug.js"; -import {runGenericServerTest} from "../utils/genericServerTest.js"; -import {getMockApi, getTestServer} from "../utils/utils.js"; -import {registerRoutesGroup} from "../../src/beacon/server/index.js"; -import {HttpClient} from "../../src/utils/client/httpClient.js"; +import {Api, ReqTypes, routesData} from "../../../src/beacon/routes/debug.js"; +import {getClient} from "../../../src/beacon/client/debug.js"; +import {getRoutes} from "../../../src/beacon/server/debug.js"; +import {runGenericServerTest} from "../../utils/genericServerTest.js"; +import {getMockApi, getTestServer} from "../../utils/utils.js"; +import {registerRoute} from "../../../src/utils/server/registerRoute.js"; +import {HttpClient} from "../../../src/utils/client/httpClient.js"; -describe("debug", function () { +describe("beacon / debug", function () { // Extend timeout since states are very big this.timeout(30 * 1000); const root = Buffer.alloc(32, 1); @@ -44,8 +44,9 @@ describe("debug", function () { describe("getState() in SSZ format", () => { const {baseUrl, server} = getTestServer(); const mockApi = getMockApi(routesData); - const routes = getRoutes(config, mockApi); - registerRoutesGroup(server, routes); + for (const route of Object.values(getRoutes(config, mockApi))) { + registerRoute(server, route); + } for (const method of ["getState" as const, "getStateV2" as const]) { it(method, async () => { diff --git a/packages/api/test/unit/events.test.ts b/packages/api/test/unit/beacon/events.test.ts similarity index 82% rename from packages/api/test/unit/events.test.ts rename to packages/api/test/unit/beacon/events.test.ts index 7b99c9846f8b..88ddc61e6b35 100644 --- a/packages/api/test/unit/events.test.ts +++ b/packages/api/test/unit/beacon/events.test.ts @@ -2,18 +2,19 @@ import {expect} from "chai"; import {AbortController} from "@chainsafe/abort-controller"; import {sleep} from "@chainsafe/lodestar-utils"; import {config} from "@chainsafe/lodestar-config/default"; -import {Api, routesData, EventType, BeaconEvent} from "../../src/beacon/routes/events.js"; -import {getClient} from "../../src/beacon/client/events.js"; -import {getRoutes} from "../../src/beacon/server/events.js"; -import {getMockApi, getTestServer} from "../utils/utils.js"; -import {registerRoutesGroup} from "../../src/beacon/server/index.js"; +import {Api, routesData, EventType, BeaconEvent} from "../../../src/beacon/routes/events.js"; +import {getClient} from "../../../src/beacon/client/events.js"; +import {getRoutes} from "../../../src/beacon/server/events.js"; +import {registerRoute} from "../../../src/utils/server/registerRoute.js"; +import {getMockApi, getTestServer} from "../../utils/utils.js"; -describe("events", () => { +describe("beacon / events", () => { const rootHex = "0x" + "01".repeat(32); const {baseUrl, server} = getTestServer(); const mockApi = getMockApi(routesData); - const routes = getRoutes(config, mockApi); - registerRoutesGroup(server, routes); + for (const route of Object.values(getRoutes(config, mockApi))) { + registerRoute(server, route); + } let controller: AbortController; beforeEach(() => (controller = new AbortController())); diff --git a/packages/api/test/unit/lightclient.test.ts b/packages/api/test/unit/beacon/lightclient.test.ts similarity index 85% rename from packages/api/test/unit/lightclient.test.ts rename to packages/api/test/unit/beacon/lightclient.test.ts index ee4ba26ed893..0ac6bf810f08 100644 --- a/packages/api/test/unit/lightclient.test.ts +++ b/packages/api/test/unit/beacon/lightclient.test.ts @@ -2,14 +2,14 @@ import {ssz} from "@chainsafe/lodestar-types"; import {config} from "@chainsafe/lodestar-config/default"; import {ProofType} from "@chainsafe/persistent-merkle-tree"; import {toHexString} from "@chainsafe/ssz"; -import {Api, ReqTypes} from "../../src/beacon/routes/lightclient.js"; -import {getClient} from "../../src/beacon/client/lightclient.js"; -import {getRoutes} from "../../src/beacon/server/lightclient.js"; -import {runGenericServerTest} from "../utils/genericServerTest.js"; +import {Api, ReqTypes} from "../../../src/beacon/routes/lightclient.js"; +import {getClient} from "../../../src/beacon/client/lightclient.js"; +import {getRoutes} from "../../../src/beacon/server/lightclient.js"; +import {runGenericServerTest} from "../../utils/genericServerTest.js"; const root = Uint8Array.from(Buffer.alloc(32, 1)); -describe("lightclient", () => { +describe("beacon / lightclient", () => { const lightClientUpdate = ssz.altair.LightClientUpdate.defaultValue(); const syncAggregate = ssz.altair.SyncAggregate.defaultValue(); const header = ssz.phase0.BeaconBlockHeader.defaultValue(); diff --git a/packages/api/test/unit/node.test.ts b/packages/api/test/unit/beacon/node.test.ts similarity index 81% rename from packages/api/test/unit/node.test.ts rename to packages/api/test/unit/beacon/node.test.ts index 042c799a1e6f..0a4a67262837 100644 --- a/packages/api/test/unit/node.test.ts +++ b/packages/api/test/unit/beacon/node.test.ts @@ -1,11 +1,11 @@ import {ssz} from "@chainsafe/lodestar-types"; import {config} from "@chainsafe/lodestar-config/default"; -import {Api, ReqTypes, NodePeer} from "../../src/beacon/routes/node.js"; -import {getClient} from "../../src/beacon/client/node.js"; -import {getRoutes} from "../../src/beacon/server/node.js"; -import {runGenericServerTest} from "../utils/genericServerTest.js"; +import {Api, ReqTypes, NodePeer} from "../../../src/beacon/routes/node.js"; +import {getClient} from "../../../src/beacon/client/node.js"; +import {getRoutes} from "../../../src/beacon/server/node.js"; +import {runGenericServerTest} from "../../utils/genericServerTest.js"; -describe("node", () => { +describe("beacon / node", () => { const peerIdStr = "peerId"; const nodePeer: NodePeer = { peerId: peerIdStr, diff --git a/packages/api/test/unit/validator.test.ts b/packages/api/test/unit/beacon/validator.test.ts similarity index 90% rename from packages/api/test/unit/validator.test.ts rename to packages/api/test/unit/beacon/validator.test.ts index b045e4e10183..ed5b72a8eefa 100644 --- a/packages/api/test/unit/validator.test.ts +++ b/packages/api/test/unit/beacon/validator.test.ts @@ -1,14 +1,14 @@ import {ForkName} from "@chainsafe/lodestar-params"; import {ssz} from "@chainsafe/lodestar-types"; import {config} from "@chainsafe/lodestar-config/default"; -import {Api, ReqTypes} from "../../src/beacon/routes/validator.js"; -import {getClient} from "../../src/beacon/client/validator.js"; -import {getRoutes} from "../../src/beacon/server/validator.js"; -import {runGenericServerTest} from "../utils/genericServerTest.js"; +import {Api, ReqTypes} from "../../../src/beacon/routes/validator.js"; +import {getClient} from "../../../src/beacon/client/validator.js"; +import {getRoutes} from "../../../src/beacon/server/validator.js"; +import {runGenericServerTest} from "../../utils/genericServerTest.js"; const ZERO_HASH = Buffer.alloc(32, 0); -describe("validator", () => { +describe("beacon / validator", () => { runGenericServerTest(config, getClient, getRoutes, { getAttesterDuties: { args: [1000, [1, 2, 3]], diff --git a/packages/api/test/unit/builder/builder.test.ts b/packages/api/test/unit/builder/builder.test.ts new file mode 100644 index 000000000000..2efcf14b8502 --- /dev/null +++ b/packages/api/test/unit/builder/builder.test.ts @@ -0,0 +1,14 @@ +import {config} from "@chainsafe/lodestar-config/default"; +import {Api, ReqTypes} from "../../../src/builder/routes.js"; +import {getClient} from "../../../src/builder/client.js"; +import {getRoutes} from "../../../src/builder/server/index.js"; +import {runGenericServerTest} from "../../utils/genericServerTest.js"; + +describe("builder", () => { + runGenericServerTest(config, getClient, getRoutes, { + checkStatus: { + args: [], + res: undefined, + }, + }); +}); diff --git a/packages/api/test/unit/keymanager.test.ts b/packages/api/test/unit/keymanager/keymanager.test.ts similarity index 79% rename from packages/api/test/unit/keymanager.test.ts rename to packages/api/test/unit/keymanager/keymanager.test.ts index 661a1f9e3321..adb49e3803ce 100644 --- a/packages/api/test/unit/keymanager.test.ts +++ b/packages/api/test/unit/keymanager/keymanager.test.ts @@ -1,8 +1,8 @@ import {config} from "@chainsafe/lodestar-config/default"; -import {Api, DeletionStatus, ImportStatus, ReqTypes} from "../../src/keymanager/routes.js"; -import {getClient} from "../../src/keymanager/client.js"; -import {getRoutes} from "../../src/keymanager/server/index.js"; -import {runGenericServerTest} from "../utils/genericServerTest.js"; +import {Api, DeletionStatus, ImportStatus, ReqTypes} from "../../../src/keymanager/routes.js"; +import {getClient} from "../../../src/keymanager/client.js"; +import {getRoutes} from "../../../src/keymanager/server/index.js"; +import {runGenericServerTest} from "../../utils/genericServerTest.js"; describe("keymanager", () => { runGenericServerTest(config, getClient, getRoutes, { diff --git a/packages/api/test/utils/genericServerTest.ts b/packages/api/test/utils/genericServerTest.ts index ead71b4365a7..81b6adb47c1f 100644 --- a/packages/api/test/utils/genericServerTest.ts +++ b/packages/api/test/utils/genericServerTest.ts @@ -3,7 +3,7 @@ import {IChainForkConfig} from "@chainsafe/lodestar-config"; import {RouteGeneric, ReqGeneric, Resolves} from "../../src/utils/index.js"; import {FetchOpts, HttpClient, IHttpClient} from "../../src/utils/client/index.js"; import {ServerRoutes} from "../../src/utils/server/genericJsonServer.js"; -import {registerRoutesGroup} from "../../src/beacon/server/index.js"; +import {registerRoute} from "../../src/utils/server/registerRoute.js"; import {getMockApi, getTestServer} from "./utils.js"; type IgnoreVoid = T extends void ? undefined : T; @@ -31,8 +31,9 @@ export function runGenericServerTest< const httpClient = new HttpClientSpy({baseUrl}); const client = getClient(config, httpClient); - const routes = getRoutes(config, mockApi); - registerRoutesGroup(server, routes); + for (const route of Object.values(getRoutes(config, mockApi))) { + registerRoute(server, route); + } for (const key of Object.keys(testCases)) { const routeId = key as keyof Api;