Skip to content

Commit

Permalink
Handle invalid persisted ENR (#4481)
Browse files Browse the repository at this point in the history
  • Loading branch information
dapplion authored Aug 25, 2022
1 parent d943b51 commit 506630e
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions packages/cli/src/cmds/init/handler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from "node:fs";
import {IChainForkConfig} from "@lodestar/config";
import {ENR} from "@chainsafe/discv5";
import {
BeaconNodeOptions,
getBeaconConfigFromArgs,
Expand Down Expand Up @@ -81,10 +82,23 @@ export async function persistOptionsAndConfig(args: IBeaconArgs & IGlobalArgs):
initEnr(beaconPaths.enrFile, peerId);
} else {
// Verify that the peerId matches the ENR
const enr = readEnr(beaconPaths.enrFile);
const peerIdPrev = await enr.peerId();
if (peerIdPrev.toB58String() !== peerId.toB58String()) {
let enr: ENR | null = null;
try {
// Note: it has happened that the ENR file gets corrupted, if that happens don't kill the node
// https://github.com/ChainSafe/lodestar/issues/4082
enr = readEnr(beaconPaths.enrFile);
} catch (e) {
// eslint-disable-next-line no-console
console.error(`Persisted ENR is invalid, creating a new ENR: ${(e as Error).message}`);
}

if (!enr) {
initEnr(beaconPaths.enrFile, peerId);
} else {
const peerIdPrev = await enr.peerId();
if (peerIdPrev.toB58String() !== peerId.toB58String()) {
initEnr(beaconPaths.enrFile, peerId);
}
}
}
}

0 comments on commit 506630e

Please sign in to comment.