-
-
Notifications
You must be signed in to change notification settings - Fork 290
/
handler.ts
230 lines (203 loc) Β· 8.36 KB
/
handler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import path from "node:path";
import {Registry} from "prom-client";
import {ErrorAborted} from "@lodestar/utils";
import {LevelDbController} from "@lodestar/db";
import {BeaconNode, BeaconDb} from "@lodestar/beacon-node";
import {ChainForkConfig, createBeaconConfig} from "@lodestar/config";
import {ACTIVE_PRESET, PresetName} from "@lodestar/params";
import {ProcessShutdownCallback} from "@lodestar/validator";
import {LoggerNode, getNodeLogger} from "@lodestar/logger/node";
import {GlobalArgs, parseBeaconNodeArgs} from "../../options/index.js";
import {BeaconNodeOptions, getBeaconConfigFromArgs} from "../../config/index.js";
import {getNetworkBootnodes, getNetworkData, isKnownNetworkName, readBootnodes} from "../../networks/index.js";
import {
onGracefulShutdown,
mkdir,
writeFile600Perm,
cleanOldLogFiles,
parseLoggerArgs,
pruneOldFilesInDir,
} from "../../util/index.js";
import {getVersionData} from "../../util/version.js";
import {LogArgs} from "../../options/logOptions.js";
import {BeaconArgs} from "./options.js";
import {getBeaconPaths} from "./paths.js";
import {initBeaconState} from "./initBeaconState.js";
import {initPeerIdAndEnr} from "./initPeerIdAndEnr.js";
const DEFAULT_RETENTION_SSZ_OBJECTS_HOURS = 15 * 24;
const HOURS_TO_MS = 3600 * 1000;
/**
* Runs a beacon node.
*/
export async function beaconHandler(args: BeaconArgs & GlobalArgs): Promise<void> {
const {config, options, beaconPaths, network, version, commit, peerId, logger} = await beaconHandlerInit(args);
// initialize directories
mkdir(beaconPaths.dataDir);
mkdir(beaconPaths.beaconDir);
mkdir(beaconPaths.dbDir);
const abortController = new AbortController();
logger.info("Lodestar", {network, version, commit});
// Callback for beacon to request forced exit, for e.g. in case of irrecoverable
// forkchoice errors
const processShutdownCallback: ProcessShutdownCallback = (err) => {
logger.error("Process shutdown requested", {}, err);
process.kill(process.pid, "SIGINT");
};
if (ACTIVE_PRESET === PresetName.minimal) logger.info("ACTIVE_PRESET == minimal preset");
// additional metrics registries
const metricsRegistries: Registry[] = [];
let networkRegistry: Registry | undefined;
if (options.metrics.enabled) {
networkRegistry = new Registry();
metricsRegistries.push(networkRegistry);
}
const db = new BeaconDb(config, await LevelDbController.create(options.db, {metrics: null, logger}));
logger.info("Connected to LevelDB database", {path: options.db.name});
// BeaconNode setup
try {
const {anchorState, wsCheckpoint} = await initBeaconState(
options,
args,
config,
db,
logger,
abortController.signal
);
const beaconConfig = createBeaconConfig(config, anchorState.genesisValidatorsRoot);
const node = await BeaconNode.init({
opts: options,
config: beaconConfig,
db,
logger,
processShutdownCallback,
peerId,
peerStoreDir: beaconPaths.peerStoreDir,
anchorState,
wsCheckpoint,
metricsRegistries,
});
// dev debug option to have access to the BN instance
if (args.attachToGlobalThis) {
(globalThis as unknown as {bn: BeaconNode}).bn = node;
}
// Prune invalid SSZ objects every interval
const {persistInvalidSszObjectsDir} = args;
const pruneInvalidSSZObjectsInterval = persistInvalidSszObjectsDir
? setInterval(() => {
try {
pruneOldFilesInDir(
persistInvalidSszObjectsDir,
(args.persistInvalidSszObjectsRetentionHours ?? DEFAULT_RETENTION_SSZ_OBJECTS_HOURS) * HOURS_TO_MS
);
} catch (e) {
logger.warn("Error pruning invalid SSZ objects", {persistInvalidSszObjectsDir}, e as Error);
}
// Run every ~1 hour
}, HOURS_TO_MS)
: null;
// Intercept SIGINT signal, to perform final ops before exiting
onGracefulShutdown(async () => {
if (args.persistNetworkIdentity) {
try {
const networkIdentity = await node.network.getNetworkIdentity();
const enrPath = path.join(beaconPaths.beaconDir, "enr");
writeFile600Perm(enrPath, networkIdentity.enr);
} catch (e) {
logger.warn("Unable to persist enr", {}, e as Error);
}
}
abortController.abort();
if (pruneInvalidSSZObjectsInterval !== null) {
clearInterval(pruneInvalidSSZObjectsInterval);
}
}, logger.info.bind(logger));
abortController.signal.addEventListener(
"abort",
async () => {
try {
await node.close();
logger.debug("Beacon node closed");
// Explicitly exit until active handles issue is resolved
// See https://github.com/ChainSafe/lodestar/issues/5642
process.exit(0);
} catch (e) {
logger.error("Error closing beacon node", {}, e as Error);
// Make sure db is always closed gracefully
await db.close();
// Must explicitly exit process due to potential active handles
process.exit(1);
}
},
{once: true}
);
} catch (e) {
await db.close();
if (e instanceof ErrorAborted) {
logger.info(e.message); // Let the user know the abort was received but don't print as error
} else {
throw e;
}
}
}
/** Separate function to simplify unit testing of options merging */
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export async function beaconHandlerInit(args: BeaconArgs & GlobalArgs) {
const {config, network} = getBeaconConfigFromArgs(args);
const beaconNodeOptions = new BeaconNodeOptions(parseBeaconNodeArgs(args));
const {version, commit} = getVersionData();
const beaconPaths = getBeaconPaths(args, network);
// TODO: Rename db.name to db.path or db.location
beaconNodeOptions.set({db: {name: beaconPaths.dbDir}});
beaconNodeOptions.set({chain: {persistInvalidSszObjectsDir: beaconPaths.persistInvalidSszObjectsDir}});
// Add metrics metadata to show versioning + network info in Prometheus + Grafana
beaconNodeOptions.set({metrics: {metadata: {version, commit, network}}});
beaconNodeOptions.set({metrics: {validatorMonitorLogs: args.validatorMonitorLogs}});
// Add detailed version string for API node/version endpoint
beaconNodeOptions.set({api: {version}});
// Combine bootnodes from different sources
const bootnodes = (beaconNodeOptions.get().network?.discv5?.bootEnrs ?? []).concat(
args.bootnodesFile ? readBootnodes(args.bootnodesFile) : [],
isKnownNetworkName(network) ? await getNetworkBootnodes(network) : []
);
// Deduplicate and set combined bootnodes
beaconNodeOptions.set({network: {discv5: {bootEnrs: [...new Set(bootnodes)]}}});
// Set known depositContractDeployBlock
if (isKnownNetworkName(network)) {
const {depositContractDeployBlock} = getNetworkData(network);
beaconNodeOptions.set({eth1: {depositContractDeployBlock}});
}
const logger = initLogger(args, beaconPaths.dataDir, config);
const {peerId, enr} = await initPeerIdAndEnr(args, beaconPaths.beaconDir, logger);
// Inject ENR to beacon options
beaconNodeOptions.set({network: {discv5: {enr: enr.encodeTxt(), config: {enrUpdate: !enr.ip && !enr.ip6}}}});
if (args.private) {
beaconNodeOptions.set({network: {private: true}});
} else {
const versionStr = `Lodestar/${version}`;
const simpleVersionStr = version.split("/")[0];
// Add simple version string for libp2p agent version
beaconNodeOptions.set({network: {version: simpleVersionStr}});
// Add User-Agent header to all builder requests
beaconNodeOptions.set({executionBuilder: {userAgent: versionStr}});
// Set jwt version with version string
beaconNodeOptions.set({executionEngine: {jwtVersion: versionStr}, eth1: {jwtVersion: versionStr}});
}
// Render final options
const options = beaconNodeOptions.getWithDefaults();
return {config, options, beaconPaths, network, version, commit, peerId, logger};
}
export function initLogger(
args: LogArgs & Pick<GlobalArgs, "dataDir">,
dataDir: string,
config: ChainForkConfig,
fileName = "beacon.log"
): LoggerNode {
const defaultLogFilepath = path.join(dataDir, fileName);
const logger = getNodeLogger(parseLoggerArgs(args, {defaultLogFilepath}, config));
try {
cleanOldLogFiles(args, {defaultLogFilepath});
} catch (e) {
logger.debug("Not able to delete log files", {}, e as Error);
}
return logger;
}