Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
- name: Setup Node.js
uses: actions/setup-node@v7
with:
node-version: 24.22.1
node-version: 24.22.3

- name: Enable Corepack
run: corepack enable
Expand Down
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
24.22.1
24.22.3
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ARG DOCKER_IMAGE=node:24.22.1-alpine3.24
ARG DOCKER_IMAGE=node:24.22.3-alpine3.24

# Build
FROM ${DOCKER_IMAGE} AS build-stage
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.brain.dev
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ARG DOCKER_IMAGE=node:24.22.1-alpine3.24
ARG DOCKER_IMAGE=node:24.22.3-alpine3.24
FROM ${DOCKER_IMAGE} AS build-stage

RUN apk add --no-cache --upgrade ca-certificates ca-certificates-bundle && update-ca-certificates
Expand Down
2 changes: 2 additions & 0 deletions docs/gloas-builder-config.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Gloas / ePBS validator configuration

> **Disabled for now.** This feature is switched off with `featureFlags.builderConfig` in `packages/common/src/featureFlags.ts`. While it is off, the UI hides builder settings, the builder RPCs and builder fields in updates are rejected, reload does not apply stored settings, and `BUILDER_IDS` is ignored (its one-time import still runs once the feature is enabled). Settings already saved stay in the database unchanged, and overrides already applied to a validator client by 0.2.0-rc.0 are not removed.

Brain manages configuration. The validator client signs slot-specific proposer
preferences and builder authorizations and supplies the resolved configuration
in proposal requests. The beacon node solicits and selects bids. Web3Signer
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "module",
"private": true,
"engines": {
"node": ">=24.22.1"
"node": ">=24.22.3"
},
"workspaces": [
"packages/*"
Expand Down
4 changes: 3 additions & 1 deletion packages/brain/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
"main": "dist/index.js",
"types": "dist/types.d.ts",
"scripts": {
"test": "yarn run test:unit",
"test": "yarn run test:unit && yarn run test:e2e",
"test:unit": "mocha --exit --recursive 'test/**/*.unit.test.ts'",
"test:e2e": "mocha --exit --recursive 'test/**/*.e2e.test.ts'",
"dev": "yarn run link:ui && tsx --watch src/index.ts",
"link:ui": "ln -s ../ui/build/ ./uiBuild",
"build": "yarn run clean:dist && yarn run clean:ln && tsc -p tsconfig.json",
Expand All @@ -33,6 +34,7 @@
"mocha": "^12.0.3",
"rimraf": "^6.1.3",
"sinon": "^22.1.0",
"socket.io-client": "^4.8.4",
"typescript": "~6.0.3"
}
}
46 changes: 31 additions & 15 deletions packages/brain/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { params } from "./params.js";
import { getCrons } from "./modules/cron/index.js";
import { brainConfig } from "./modules/config/index.js";
import { importBuilderIds } from "./modules/config/importBuilderIds.js";
import { BeaconStatusStore } from "./modules/beaconStatus/index.js";
import { featureFlags } from "@stakingbrain/common";

logger.info(`Starting brain...`);

Expand All @@ -34,22 +36,30 @@ await brainDb.initialize(signerApi, validatorApi);
logger.debug(`Loaded ${Object.keys(brainDb.getData()).length} validators`);

// Import builder choices once before cron/API writes can begin.
try {
const imported = importBuilderIds({
builderIds: process.env.BUILDER_IDS,
network: config.chain.network,
brainDb,
markerPath: path.join(path.dirname(brainDbPath), "builder-config-imported")
});
if (imported)
logger.info(`Builder settings imported: ${imported.imported} validators; ${imported.skipped} preserved`);
} catch (error) {
// A failed optional handoff must not interrupt ordinary key/fee reconciliation.
logger.error("Unable to import BUILDER_IDS; import will retry at next startup", error);
if (featureFlags.builderConfig) {
try {
const imported = importBuilderIds({
builderIds: process.env.BUILDER_IDS,
network: config.chain.network,
brainDb,
markerPath: path.join(path.dirname(brainDbPath), "builder-config-imported")
});
if (imported)
logger.info(`Builder settings imported: ${imported.imported} validators; ${imported.skipped} preserved`);
} catch (error) {
// A failed optional handoff must not interrupt ordinary key/fee reconciliation.
logger.error("Unable to import BUILDER_IDS; import will retry at next startup", error);
}
} else if (process.env.BUILDER_IDS) {
// Leave the one-time marker unwritten so the import still runs once the feature is enabled.
logger.warn("BUILDER_IDS is set but builder configuration is disabled in this version; ignoring it");
}

// Beacon chain details (index, withdrawal credentials) for listing validators.
const beaconStatus = new BeaconStatusStore(brainDb, beaconchainApi);

// CRON
const { reloadValidatorsCronTask } = getCrons({
const { reloadValidatorsCronTask, beaconStatusCronTask } = getCrons({
sendNotification: true,
postgresClient,
prometheusApi,
Expand All @@ -59,9 +69,13 @@ const { reloadValidatorsCronTask } = getCrons({
brainDb,
chainConfig: config.chain,
beaconchainApi,
dappmanagerApi
dappmanagerApi,
beaconStatus
});
reloadValidatorsCronTask.start();
beaconStatusCronTask.start();
// Reads only: load beacon details now rather than after the first interval.
void beaconStatusCronTask.runNow();
//trackValidatorsPerformanceCronTask.start();

// Start server APIs
Expand All @@ -74,13 +88,15 @@ const { uiServer, launchpadServer, brainApiServer } = getServers({
beaconchainApi,
brainDb,
reloadValidatorsCronTask,
allowedOriginsFromEnv: config.apis.cors
allowedOriginsFromEnv: config.apis.cors,
beaconStatus
});

// Graceful shutdown
function handle(signal: string): void {
logger.info(`${signal} received. Shutting down...`);
reloadValidatorsCronTask.stop();
beaconStatusCronTask.stop();
//trackValidatorsPerformanceCronTask.stop();
brainDb.close();
postgresClient.close().catch((err) => logger.error(`Error closing postgres client`, err)); // postgresClient db connection is the only external resource that needs to be closed
Expand Down
7 changes: 5 additions & 2 deletions packages/brain/src/modules/apiClients/beaconchain/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,16 +166,19 @@ export class BeaconchainApi extends StandardApi {
*/
public async postStateValidators({
stateId,
body
body,
timeout
}: {
stateId: BlockId;
body: { ids: string[]; statuses: ValidatorStatus[] };
timeout?: number;
}): Promise<BeaconchainValidatorStatePostResponse> {
try {
return await this.request({
method: "POST",
endpoint: path.join(this.beaconchainEndpoint, "states", stateId, "validators"),
body: JSON.stringify(body)
body: JSON.stringify(body),
timeout
});
} catch (e) {
throw new BeaconchainApiError(`Error getting (POST) state validators from beaconchain. ${e.message}`);
Expand Down
8 changes: 7 additions & 1 deletion packages/brain/src/modules/apiClients/signer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import path from "node:path";
import { prefix0xPubkey } from "../prefix0xPubkey.js";
import { SignerApiError } from "./error.js";

/** Longer than the UI's RPC timeout: an import already sent should be recorded even if the UI gave up. */
export const SIGNER_IMPORT_TIMEOUT_MS = 10 * 60 * 1000;

/**
* Key Manager API standard
* https://ethereum.github.io/keymanager-APIs/
Expand Down Expand Up @@ -121,7 +124,10 @@ export class Web3SignerApi extends StandardApi {
method: "POST",
endpoint: this.localKeymanagerEndpoint,
body: JSON.stringify(postRequest),
headers: this.originHeader
headers: this.originHeader,
// Keystore decryption is slow. Dropping the connection does not stop the signer, it only
// discards the result, so wait long enough to record the outcome of large imports.
timeout: SIGNER_IMPORT_TIMEOUT_MS
});
} catch (e) {
throw new SignerApiError(`Error importing (POST) keystores to remote signer. ${e.message}`);
Expand Down
9 changes: 6 additions & 3 deletions packages/brain/src/modules/apiServers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { BeaconchainApi } from "../apiClients/beaconchain/index.js";
import { CronJob } from "../cron/cron.js";
import { startLaunchpadApi } from "./launchpad/index.js";
import http from "http";
import { BeaconStatusStore } from "../beaconStatus/index.js";

export const getServers = ({
brainConfig,
Expand All @@ -19,7 +20,8 @@ export const getServers = ({
beaconchainApi,
brainDb,
reloadValidatorsCronTask,
allowedOriginsFromEnv
allowedOriginsFromEnv,
beaconStatus
}: {
brainConfig: BrainConfig;
uiBuildPath: string;
Expand All @@ -30,6 +32,7 @@ export const getServers = ({
brainDb: BrainDataBase;
reloadValidatorsCronTask: CronJob;
allowedOriginsFromEnv: string | string[] | null;
beaconStatus: BeaconStatusStore;
}): {
uiServer: http.Server;
launchpadServer: http.Server;
Expand All @@ -45,13 +48,13 @@ export const getServers = ({
validatorApi,
beaconchainApi,
postgresClient,
allowedOriginsFromEnv
allowedOriginsFromEnv,
beaconStatus
}),
launchpadServer: startLaunchpadApi({
brainDb,
signerApi,
validatorApi,
beaconchainApi,
reloadValidatorsCronTask,
network: brainConfig.chain.network,
signerUrl: brainConfig.apis.signerUrl,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import express from "express";
import { isValidBlsPubkey } from "@stakingbrain/common";
import { updateValidators } from "../../../ui/calls/updateValidators.js";
import { getValidators } from "../../../ui/calls/getValidators.js";
import { BrainPubkeysFeeRecipients } from "../../types.js";
import { validateUpdateFeeRecipientRequestBody } from "./validation.js";
import type { CustomValidatorGetResponse } from "../../../ui/calls/types.js";
import logger from "../../../../logger/index.js";
import { BrainDataBase } from "../../../../db/index.js";
import { CronJob } from "../../../../cron/cron.js";
import { BeaconchainApi } from "../../../../apiClients/beaconchain/index.js";
import { Web3SignerApi } from "../../../../apiClients/signer/index.js";
import { ValidatorApi } from "../../../../apiClients/validator/index.js";
import { ValidatorOperationBusyError } from "../../../../db/withValidatorOperationLock.js";

/** Stored fee recipients; client status is not needed, so reading them makes no client calls. */
const storedFeeRecipients = (brainDb: BrainDataBase) =>
Object.entries(brainDb.getData()).map(([pubkey, { feeRecipient }]) => ({ pubkey, feeRecipient }));

/**
* Retrieves fee recipient information for specified validators via GET request.
Expand Down Expand Up @@ -40,30 +41,21 @@ import { ValidatorApi } from "../../../../apiClients/validator/index.js";

export const createFeeRecipientsRouter = ({
brainDb,
signerApi,
signerUrl,
validatorApi,
beaconchainApi,
reloadValidatorsCronTask
}: {
brainDb: BrainDataBase;
signerApi: Web3SignerApi;
signerUrl: string;
validatorApi: ValidatorApi;
beaconchainApi: BeaconchainApi;
reloadValidatorsCronTask: CronJob;
}) => {
const feeRecipientsRouter = express.Router();
const feeRecipientsEndpoint = "/eth/v1/feeRecipients";

feeRecipientsRouter.get(feeRecipientsEndpoint, async (req, res) => {
try {
const validators: CustomValidatorGetResponse[] = await getValidators({
brainDb,
signerApi,
validatorApi,
beaconchainApi
});
const validators = storedFeeRecipients(brainDb);
const pubkeysParam = req.query.pubkeys as string | undefined;

if (!pubkeysParam) {
Expand Down Expand Up @@ -143,12 +135,7 @@ export const createFeeRecipientsRouter = ({
}

try {
const currentValidators: CustomValidatorGetResponse[] = await getValidators({
brainDb,
signerApi,
validatorApi,
beaconchainApi
});
const currentValidators = storedFeeRecipients(brainDb);

const validatorsToUpdate = currentValidators.filter((validator) =>
requestBody.validators.some(
Expand Down Expand Up @@ -177,7 +164,8 @@ export const createFeeRecipientsRouter = ({
res.status(200).send();
} catch (error) {
logger.error("Failed to update validators:", error);
res.status(500).send({ message: "Internal server error" });
if (error instanceof ValidatorOperationBusyError) res.status(503).send({ message: error.message });
else res.status(500).send({ message: "Internal server error" });
}
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Network, Tag } from "@stakingbrain/common";
import { ValidatorOperationBusyError } from "../../../../db/withValidatorOperationLock.js";
import express from "express";
import { deleteValidators } from "../../../ui/calls/deleteValidators.js";
import { importValidators } from "../../../ui/calls/importValidators.js";
Expand All @@ -11,6 +12,13 @@ import { BrainDataBase } from "../../../../db/index.js";
import { Web3SignerApi } from "../../../../apiClients/signer/index.js";
import { ValidatorApi } from "../../../../apiClients/validator/index.js";

/** Busy is temporary and safe to retry: nothing was changed. Other failures stay opaque. */
function sendOperationError(res: express.Response, error: Error): void {
logger.error(error);
if (error instanceof ValidatorOperationBusyError) res.status(503).send({ message: error.message });
else res.status(500).send({ message: "Internal server error" });
}

export const createKeystoresRouter = ({
signerApi,
reloadValidatorsCronTask,
Expand Down Expand Up @@ -62,8 +70,7 @@ export const createKeystoresRouter = ({

res.status(200).send(importResponse);
} catch (e) {
logger.error(e);
res.status(500).send({ message: "Internal server error" });
sendOperationError(res, e);
}
});

Expand All @@ -89,8 +96,7 @@ export const createKeystoresRouter = ({

res.status(200).send(deleteResponse);
} catch (e) {
logger.error(e);
res.status(500).send({ message: "Internal server error" });
sendOperationError(res, e);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ import { createKeystoresRouter, createFeeRecipientsRouter } from "./routes/index
import { CronJob } from "../../cron/cron.js";
import { BrainDataBase } from "../../db/index.js";
import { Network, RPC_MAX_PAYLOAD_BYTES } from "@stakingbrain/common";
import { BeaconchainApi } from "../../apiClients/beaconchain/index.js";
import { Web3SignerApi } from "../../apiClients/signer/index.js";
import { ValidatorApi } from "../../apiClients/validator/index.js";

export function startLaunchpadApi({
signerApi,
validatorApi,
beaconchainApi,
reloadValidatorsCronTask,
brainDb,
network,
Expand All @@ -24,7 +22,6 @@ export function startLaunchpadApi({
}: {
signerApi: Web3SignerApi;
validatorApi: ValidatorApi;
beaconchainApi: BeaconchainApi;
reloadValidatorsCronTask: CronJob;
brainDb: BrainDataBase;
network: Network;
Expand All @@ -35,14 +32,21 @@ export function startLaunchpadApi({
app.use(express.json({ limit: RPC_MAX_PAYLOAD_BYTES, inflate: false }));
app.use(cors({ origin: allowedOriginsFromEnv ?? allowedOrigins }));

app.use(createKeystoresRouter({ reloadValidatorsCronTask, brainDb, network, validatorApi, signerApi, signerUrl }));
app.use(
createFeeRecipientsRouter({
createKeystoresRouter({
reloadValidatorsCronTask,
brainDb,
network,
validatorApi,
signerApi,
signerUrl
})
);
app.use(
createFeeRecipientsRouter({
brainDb,
signerUrl,
validatorApi,
beaconchainApi,
reloadValidatorsCronTask
})
);
Expand Down
Loading
Loading