Skip to content

Commit

Permalink
peerIdFile cli option
Browse files Browse the repository at this point in the history
  • Loading branch information
twoeths committed Feb 20, 2023
1 parent f4057cf commit 36be594
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
17 changes: 11 additions & 6 deletions packages/cli/src/cmds/beacon/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -42,10 +42,6 @@ export async function beaconHandler(args: IBeaconArgs & IGlobalArgs): Promise<vo
logger.debug("Not able to delete log files", logParams, e as Error);
}

onGracefulShutdown(async () => {
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
Expand Down Expand Up @@ -97,6 +93,15 @@ export async function beaconHandler(args: IBeaconArgs & IGlobalArgs): Promise<vo

if (args.attachToGlobalThis) ((globalThis as unknown) as {bn: BeaconNode}).bn = node;

onGracefulShutdown(async () => {
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();
Expand Down Expand Up @@ -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);

Expand Down
6 changes: 6 additions & 0 deletions packages/cli/src/options/globalOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {paramsOptions, IParamsArgs} from "./paramsOptions.js";
interface IGlobalSingleArgs {
dataDir?: string;
network?: NetworkName;
peerIdFile?: string;
paramsFile: string;
preset: string;
}
Expand All @@ -25,6 +26,11 @@ const globalSingleOptions: ICliCommandOptions<IGlobalSingleArgs> = {
choices: networkNames,
},

peerIdFile: {
description: "Peer id file path",
type: "string",
},

paramsFile: {
description: "Network configuration file",
type: "string",
Expand Down

0 comments on commit 36be594

Please sign in to comment.