From 36be5945765759c2945ed43aac8407efb0a0b7d0 Mon Sep 17 00:00:00 2001 From: Tuyen Nguyen Date: Mon, 20 Feb 2023 15:19:24 +0700 Subject: [PATCH] peerIdFile cli option --- packages/cli/src/cmds/beacon/handler.ts | 17 +++++++++++------ packages/cli/src/options/globalOptions.ts | 6 ++++++ 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/packages/cli/src/cmds/beacon/handler.ts b/packages/cli/src/cmds/beacon/handler.ts index e7a0d8134cf8..2d28d130aa87 100644 --- a/packages/cli/src/cmds/beacon/handler.ts +++ b/packages/cli/src/cmds/beacon/handler.ts @@ -10,7 +10,7 @@ import {ACTIVE_PRESET, PresetName} from "@lodestar/params"; import {ProcessShutdownCallback} from "@lodestar/validator"; import {IGlobalArgs, parseBeaconNodeArgs} from "../../options/index.js"; -import {BeaconNodeOptions, exportToJSON, getBeaconConfigFromArgs} from "../../config/index.js"; +import {BeaconNodeOptions, exportToJSON, getBeaconConfigFromArgs, readPeerId} from "../../config/index.js"; import {getNetworkBootnodes, getNetworkData, isKnownNetworkName, readBootnodes} from "../../networks/index.js"; import {onGracefulShutdown, getCliLogger, mkdir, writeFile600Perm, cleanOldLogFiles} from "../../util/index.js"; import {getVersionData} from "../../util/version.js"; @@ -42,10 +42,6 @@ export async function beaconHandler(args: IBeaconArgs & IGlobalArgs): Promise { - abortController.abort(); - }, logger.info.bind(logger)); - logger.info("Lodestar", {network, version, commit}); // Callback for beacon to request forced exit, for e.g. in case of irrecoverable // forkchoice errors @@ -97,6 +93,15 @@ export async function beaconHandler(args: IBeaconArgs & IGlobalArgs): Promise { + const enr = await node.network.getEnr(); + if (args.peerIdFile && enr) { + const enrPath = path.join(beaconPaths.beaconDir, "enr"); + writeFile600Perm(enrPath, enr.encodeTxt()); + } + abortController.abort(); + }, logger.info.bind(logger)); + abortController.signal.addEventListener("abort", () => node.close(), {once: true}); } catch (e) { await db.stop(); @@ -140,7 +145,7 @@ export async function beaconHandlerInit(args: IBeaconArgs & IGlobalArgs) { } // Create new PeerId everytime by default, unless peerIdFile is provided - const peerId = await createSecp256k1PeerId(); + const peerId = args.peerIdFile ? await readPeerId(args.peerIdFile) : await createSecp256k1PeerId(); const enr = SignableENR.createV4(createKeypairFromPeerId(peerId)); overwriteEnrWithCliArgs(enr, args); diff --git a/packages/cli/src/options/globalOptions.ts b/packages/cli/src/options/globalOptions.ts index a732a042a354..5b974e50a37d 100644 --- a/packages/cli/src/options/globalOptions.ts +++ b/packages/cli/src/options/globalOptions.ts @@ -6,6 +6,7 @@ import {paramsOptions, IParamsArgs} from "./paramsOptions.js"; interface IGlobalSingleArgs { dataDir?: string; network?: NetworkName; + peerIdFile?: string; paramsFile: string; preset: string; } @@ -25,6 +26,11 @@ const globalSingleOptions: ICliCommandOptions = { choices: networkNames, }, + peerIdFile: { + description: "Peer id file path", + type: "string", + }, + paramsFile: { description: "Network configuration file", type: "string",