Skip to content

Commit

Permalink
Customize fastify bodyLimit (#4541)
Browse files Browse the repository at this point in the history
  • Loading branch information
dapplion committed Sep 13, 2022
1 parent d2025cc commit 15db5c7
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 5 deletions.
2 changes: 2 additions & 0 deletions packages/beacon-node/src/api/rest/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type RestApiServerOpts = {
cors?: string;
address?: string;
bearerToken?: string;
bodyLimit?: number;
};

export type RestApiServerModules = {
Expand Down Expand Up @@ -42,6 +43,7 @@ export class RestApiServer {
logger: false,
ajv: {customOptions: {coerceTypes: "array"}},
querystringParser: querystring.parse,
bodyLimit: opts.bodyLimit,
});

this.activeSockets = new HttpActiveSocketsTracker(server.server, metrics);
Expand Down
9 changes: 4 additions & 5 deletions packages/beacon-node/src/api/rest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@ import {registerRoutes} from "@lodestar/api/beacon/server";
import {ErrorAborted, ILogger} from "@lodestar/utils";
import {IChainForkConfig} from "@lodestar/config";
import {NodeIsSyncing} from "../impl/errors.js";
import {RestApiServer, RestApiServerModules, RestApiServerMetrics} from "./base.js";
import {RestApiServer, RestApiServerModules, RestApiServerMetrics, RestApiServerOpts} from "./base.js";
export {allNamespaces} from "@lodestar/api";

export type BeaconRestApiServerOpts = {
export type BeaconRestApiServerOpts = Omit<RestApiServerOpts, "bearerToken"> & {
enabled: boolean;
api: (keyof Api)[];
port: number;
cors?: string;
address?: string;
};

export const beaconRestApiServerOpts: BeaconRestApiServerOpts = {
Expand All @@ -21,6 +18,8 @@ export const beaconRestApiServerOpts: BeaconRestApiServerOpts = {
address: "127.0.0.1",
port: 9596,
cors: "*",
// beacon -> validator API is trusted, and for large amounts of keys the payload is multi-MB
bodyLimit: 10 * 1024 * 1024, // 10MB
};

export type BeaconRestApiServerModules = RestApiServerModules & {
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/cmds/validator/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ export async function validatorHandler(args: IValidatorCliArgs & IGlobalArgs): P
port: args["keymanager.port"],
cors: args["keymanager.cors"],
isAuthEnabled: args["keymanager.authEnabled"],
bodyLimit: args["keymanager.bodyLimit"],
tokenDir: dbPath,
},
{config, logger, api: keymanagerApi, metrics: metrics ? metrics.keymanagerApiRest : null}
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/cmds/validator/keymanager/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export const keymanagerRestApiServerOptsDefault: KeymanagerRestApiServerOpts = {
port: 5062,
cors: "*",
isAuthEnabled: true,
// Slashing protection DB has been reported to be 3MB https://github.com/ChainSafe/lodestar/issues/4530
bodyLimit: 20 * 1024 * 1024, // 20MB
};

export type KeymanagerRestApiServerModules = RestApiServerModules & {
Expand Down
6 changes: 6 additions & 0 deletions packages/cli/src/cmds/validator/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export type KeymanagerArgs = {
"keymanager.port"?: number;
"keymanager.address"?: string;
"keymanager.cors"?: string;
"keymanager.bodyLimit"?: number;
};

export const keymanagerOptions: ICliCommandOptions<KeymanagerArgs> = {
Expand Down Expand Up @@ -87,6 +88,11 @@ export const keymanagerOptions: ICliCommandOptions<KeymanagerArgs> = {
defaultDescription: keymanagerRestApiServerOptsDefault.cors,
group: "keymanager",
},
"keymanager.bodyLimit": {
hidden: true,
type: "number",
description: "Defines the maximum payload, in bytes, the server is allowed to accept",
},
};

export const validatorOptions: ICliCommandOptions<IValidatorCliArgs> = {
Expand Down
7 changes: 7 additions & 0 deletions packages/cli/src/options/beaconNodeOptions/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface IApiArgs {
rest: boolean;
"rest.address": string;
"rest.port": number;
"rest.bodyLimit": number;
}

export function parseArgs(args: IApiArgs): IBeaconNodeOptions["api"] {
Expand All @@ -21,6 +22,7 @@ export function parseArgs(args: IApiArgs): IBeaconNodeOptions["api"] {
enabled: args["rest"],
address: args["rest.address"],
port: args["rest.port"],
bodyLimit: args["rest.bodyLimit"],
},
};
}
Expand Down Expand Up @@ -75,4 +77,9 @@ export const options: ICliCommandOptions<IApiArgs> = {
defaultDescription: String(defaultOptions.api.rest.port),
group: "api",
},
"rest.bodyLimit": {
hidden: true,
type: "number",
description: "Defines the maximum payload, in bytes, the server is allowed to accept",
},
};
2 changes: 2 additions & 0 deletions packages/cli/test/unit/options/beaconNodeOptions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe("options / beaconNodeOptions", () => {
rest: true,
"rest.address": "127.0.0.1",
"rest.port": 7654,
"rest.bodyLimit": 30e6,

"chain.blsVerifyAllMultiThread": true,
"chain.blsVerifyAllMainThread": true,
Expand Down Expand Up @@ -82,6 +83,7 @@ describe("options / beaconNodeOptions", () => {
enabled: true,
address: "127.0.0.1",
port: 7654,
bodyLimit: 30e6,
},
},
chain: {
Expand Down

0 comments on commit 15db5c7

Please sign in to comment.