-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add brain api * fix tag imports * Implement middleware validation * fix path-to-regexp breaking change * remove logging
- Loading branch information
1 parent
fc30cc7
commit a17ac7c
Showing
16 changed files
with
145 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const corsOptions = { | ||
origin: ["http://csm-lido.dappnode", "http://csm-lido.testnet.dappnode"] // TODO: update with DAppNodePackage-lido-csm.dnp.dappnode.eth domains | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { startBrainApi } from "./startBrainApi.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./validators/index.js"; |
3 changes: 3 additions & 0 deletions
3
packages/brain/src/modules/apiServers/brain/routes/validators/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import validatorsRouter from "./route.js"; | ||
|
||
export { validatorsRouter }; |
45 changes: 45 additions & 0 deletions
45
packages/brain/src/modules/apiServers/brain/routes/validators/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { Tag } from "@stakingbrain/common"; | ||
import express from "express"; | ||
import logger from "../../../../logger/index.js"; | ||
import { brainDb } from "../../../../../index.js"; | ||
import { validateQueryParams } from "./validation.js"; | ||
import { RequestParsed } from "./types.js"; | ||
|
||
const validatorsRouter = express.Router(); | ||
|
||
const validatorsEndpoint = "/api/v0/brain/validators"; | ||
|
||
validatorsRouter.get(validatorsEndpoint, validateQueryParams, async (req: RequestParsed, res) => { | ||
const { format, tag } = req.query; | ||
|
||
try { | ||
const validators = brainDb.getData(); | ||
|
||
const tagValidatorsMap = new Map<Tag, string[]>(); | ||
|
||
for (const [pubkey, details] of Object.entries(validators)) { | ||
if (tag && !tag.includes(details.tag)) continue; | ||
|
||
const tagList = tagValidatorsMap.get(details.tag) || []; | ||
|
||
if (format === "index") { | ||
if (!details.index) { | ||
logger.warn( | ||
`Validator ${pubkey} does not have an index, a possible cause is that the deposit has not been processed yet` | ||
); | ||
continue; | ||
} | ||
tagList.push(details.index.toString()); | ||
} else tagList.push(pubkey); | ||
|
||
tagValidatorsMap.set(details.tag, tagList); | ||
} | ||
|
||
res.send(Object.fromEntries(tagValidatorsMap)); | ||
} catch (e) { | ||
logger.error(e); | ||
res.status(500).send({ message: "Internal server error" }); | ||
} | ||
}); | ||
|
||
export default validatorsRouter; |
20 changes: 20 additions & 0 deletions
20
packages/brain/src/modules/apiServers/brain/routes/validators/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Request } from "express"; | ||
import { Tag } from "@stakingbrain/common"; // Assuming this is defined somewhere | ||
|
||
// The query parameters before they are parsed or validated | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-empty-object-type | ||
export type RequestReceived = Request<{}, any, any, QueryParamsReceived>; | ||
|
||
type QueryParamsReceived = { | ||
format: "pubkey" | "index"; | ||
tag?: Tag[] | Tag; // Can be an array or a single value before validation | ||
}; | ||
|
||
// The query parameters after they are validated and parsed | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-empty-object-type | ||
export type RequestParsed = Request<{}, any, any, QueryParamsParsed>; | ||
|
||
type QueryParamsParsed = { | ||
format: "pubkey" | "index"; | ||
tag?: Tag[]; // After validation, tag should be an array | ||
}; |
36 changes: 36 additions & 0 deletions
36
packages/brain/src/modules/apiServers/brain/routes/validators/validation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { Response, NextFunction } from "express"; | ||
import { Tag, tags } from "@stakingbrain/common"; | ||
import { RequestReceived } from "./types.js"; | ||
|
||
// Validation middleware for query parameters | ||
export function validateQueryParams(req: RequestReceived, res: Response, next: NextFunction): void { | ||
const { format, tag } = req.query; | ||
|
||
// Validate format | ||
if (!format || (format !== "pubkey" && format !== "index")) { | ||
res.status(400).json({ message: "format is required and must be either 'pubkey' or 'index'" }); | ||
return; | ||
} | ||
|
||
// Validate tag | ||
if (tag) { | ||
// tag may be of type string or array of strings otherwise return 400 | ||
if (typeof tag !== "string" && !Array.isArray(tag)) { | ||
res.status(400).json({ message: "tag must be a string or an array of strings" }); | ||
} | ||
|
||
// if tag is a string, convert it to an array | ||
const tagsArray = Array.isArray(tag) ? tag : [tag]; | ||
const invalidTag = tagsArray.find((t) => !tags.includes(t as Tag)); | ||
|
||
if (invalidTag) { | ||
res.status(400).json({ message: `invalid tag received: ${invalidTag}. Allowed tags are ${tags.join(", ")}` }); | ||
return; | ||
} | ||
|
||
// If validation passed, update req.query.tag to ensure it is always an array for downstream middleware | ||
req.query.tag = tagsArray; | ||
} | ||
|
||
next(); // Continue to the next middleware or route handler | ||
} |
22 changes: 22 additions & 0 deletions
22
packages/brain/src/modules/apiServers/brain/startBrainApi.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import express from "express"; | ||
import cors from "cors"; | ||
import logger from "../../logger/index.js"; | ||
import http from "node:http"; | ||
import { params } from "../../../params.js"; | ||
import { corsOptions } from "./config.js"; | ||
import { validatorsRouter } from "./routes/index.js"; | ||
|
||
export function startBrainApi(): http.Server { | ||
const app = express(); | ||
app.use(express.json()); | ||
app.use(cors(corsOptions)); | ||
|
||
app.use(validatorsRouter); | ||
|
||
const server = new http.Server(app); | ||
server.listen(params.brainPort, () => { | ||
logger.info(`Brain API listening on port ${params.brainPort}`); | ||
}); | ||
|
||
return server; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from "./ui/index.js"; | ||
export * from "./launchpad/index.js"; | ||
export * from "./brain/index.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters