-
-
Notifications
You must be signed in to change notification settings - Fork 290
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add bootnode cli command #5876
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
import path from "node:path"; | ||
import {Multiaddr, multiaddr} from "@multiformats/multiaddr"; | ||
import {Discv5, ENR} from "@chainsafe/discv5"; | ||
import {ErrorAborted} from "@lodestar/utils"; | ||
import {HttpMetricsServer, RegistryMetricCreator, getHttpMetricsServer} from "@lodestar/beacon-node"; | ||
|
||
import {GlobalArgs} from "../../options/index.js"; | ||
import {getBeaconConfigFromArgs} from "../../config/index.js"; | ||
import {getNetworkBootnodes, isKnownNetworkName, readBootnodes} from "../../networks/index.js"; | ||
import {onGracefulShutdown, mkdir, writeFile600Perm} from "../../util/index.js"; | ||
import {getVersionData} from "../../util/version.js"; | ||
import {initPeerIdAndEnr} from "../beacon/initPeerIdAndEnr.js"; | ||
import {parseArgs as parseMetricsArgs} from "../../options/beaconNodeOptions/metrics.js"; | ||
import {parseArgs as parseNetworkArgs} from "../../options/beaconNodeOptions/network.js"; | ||
import {getBeaconPaths} from "../beacon/paths.js"; | ||
import {BeaconArgs} from "../beacon/options.js"; | ||
import {initLogger} from "../beacon/handler.js"; | ||
import {BootnodeArgs} from "./options.js"; | ||
|
||
/** | ||
* Runs a bootnode. | ||
*/ | ||
export async function bootnodeHandler(args: BootnodeArgs & GlobalArgs): Promise<void> { | ||
const {discv5Args, metricsArgs, bootnodeDir, network, version, commit, peerId, enr, logger} = | ||
await bootnodeHandlerInit(args); | ||
|
||
const abortController = new AbortController(); | ||
const bindAddrs = discv5Args.bindAddrs; | ||
|
||
logger.info("Lodestar Bootnode", {network, version, commit}); | ||
logger.info("Bind address", bindAddrs); | ||
logger.info("Advertised address", { | ||
ip4: enr.getLocationMultiaddr("udp4")?.toString(), | ||
ip6: enr.getLocationMultiaddr("udp6")?.toString(), | ||
}); | ||
logger.info("Identity", {peerId: peerId.toString(), nodeId: enr.nodeId}); | ||
logger.info("ENR", {enr: enr.encodeTxt()}); | ||
|
||
// bootnode setup | ||
try { | ||
let metricsRegistry: RegistryMetricCreator | undefined; | ||
let metricsServer: HttpMetricsServer | undefined; | ||
if (metricsArgs.enabled) { | ||
metricsRegistry = new RegistryMetricCreator(); | ||
metricsRegistry.static({ | ||
name: "bootnode_version", | ||
help: "Bootnode version", | ||
value: {version, commit, network}, | ||
}); | ||
metricsServer = await getHttpMetricsServer(metricsArgs, {register: metricsRegistry, logger}); | ||
} | ||
|
||
const discv5 = Discv5.create({ | ||
enr, | ||
peerId, | ||
bindAddrs: { | ||
ip4: (bindAddrs.ip4 ? multiaddr(bindAddrs.ip4) : undefined) as Multiaddr, | ||
ip6: bindAddrs.ip6 ? multiaddr(bindAddrs.ip6) : undefined, | ||
}, | ||
config: {enrUpdate: !enr.ip && !enr.ip6}, | ||
metricsRegistry, | ||
}); | ||
|
||
// If there are any bootnodes, add them to the routing table | ||
for (const bootEnrStr of Array.from(new Set(discv5Args.bootEnrs).values())) { | ||
const bootEnr = ENR.decodeTxt(bootEnrStr); | ||
logger.info("Adding bootnode", { | ||
ip4: bootEnr.getLocationMultiaddr("udp4")?.toString(), | ||
ip6: bootEnr.getLocationMultiaddr("udp6")?.toString(), | ||
peerId: (await bootEnr.peerId()).toString(), | ||
nodeId: enr.nodeId, | ||
}); | ||
discv5.addEnr(bootEnr); | ||
} | ||
|
||
// start the server | ||
await discv5.start(); | ||
|
||
// if there are peers in the local routing table, establish a session by running a query | ||
if (discv5.kadValues().length) { | ||
void discv5.findRandomNode(); | ||
} | ||
|
||
discv5.on("multiaddrUpdated", (addr) => { | ||
logger.info("Advertised socket address updated", {addr: addr.toString()}); | ||
}); | ||
|
||
// respond with metrics every 10 seconds | ||
const printInterval = setInterval(() => { | ||
let ip4Only = 0; | ||
let ip6Only = 0; | ||
let ip4ip6 = 0; | ||
let unreachable = 0; | ||
for (const kadEnr of discv5.kadValues()) { | ||
const hasIp4 = kadEnr.getLocationMultiaddr("udp4"); | ||
const hasIp6 = kadEnr.getLocationMultiaddr("udp6"); | ||
if (hasIp4 && hasIp6) { | ||
ip4ip6++; | ||
} else if (hasIp4) { | ||
ip4Only++; | ||
} else if (hasIp6) { | ||
ip6Only++; | ||
} else { | ||
unreachable++; | ||
} | ||
} | ||
logger.info("Server metrics", { | ||
connectedPeers: discv5.connectedPeerCount, | ||
activeSessions: discv5.sessionService.sessionsSize(), | ||
ip4Nodes: ip4Only, | ||
ip6Nodes: ip6Only, | ||
ip4AndIp6Nodes: ip4ip6, | ||
unreachableNodes: unreachable, | ||
}); | ||
}, 10_000); | ||
|
||
// Intercept SIGINT signal, to perform final ops before exiting | ||
onGracefulShutdown(async () => { | ||
if (args.persistNetworkIdentity) { | ||
try { | ||
const enrPath = path.join(bootnodeDir, "enr"); | ||
writeFile600Perm(enrPath, enr.encodeTxt()); | ||
} catch (e) { | ||
logger.warn("Unable to persist enr", {}, e as Error); | ||
} | ||
} | ||
abortController.abort(); | ||
}, logger.info.bind(logger)); | ||
|
||
abortController.signal.addEventListener( | ||
"abort", | ||
async () => { | ||
try { | ||
discv5.removeAllListeners(); | ||
clearInterval(printInterval); | ||
|
||
await metricsServer?.close(); | ||
await discv5.stop(); | ||
logger.debug("Bootnode closed"); | ||
} catch (e) { | ||
logger.error("Error closing bootnode", {}, e as Error); | ||
// Must explicitly exit process due to potential active handles | ||
process.exit(1); | ||
} | ||
}, | ||
{once: true} | ||
); | ||
} catch (e) { | ||
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 bootnodeHandlerInit(args: BootnodeArgs & GlobalArgs) { | ||
const {config, network} = getBeaconConfigFromArgs(args); | ||
const {version, commit} = getVersionData(); | ||
const beaconPaths = getBeaconPaths(args, network); | ||
// Use a separate directory to store bootnode enr + peer-id | ||
const bootnodeDir = path.join(beaconPaths.dataDir, "bootnode"); | ||
const {discv5: discv5Args} = parseNetworkArgs(args); | ||
const metricsArgs = parseMetricsArgs(args); | ||
if (!discv5Args) { | ||
// Unreachable because bootnode requires discv5 to be enabled - duh | ||
throw new Error("unreachable - bootnode requires discv5 to be enabled"); | ||
} | ||
|
||
// initialize directories | ||
mkdir(beaconPaths.dataDir); | ||
mkdir(bootnodeDir); | ||
|
||
// Fetch extra bootnodes | ||
discv5Args.bootEnrs = (discv5Args.bootEnrs ?? []).concat( | ||
args.bootnodesFile ? readBootnodes(args.bootnodesFile) : [], | ||
isKnownNetworkName(network) ? await getNetworkBootnodes(network) : [] | ||
); | ||
|
||
const logger = initLogger(args, beaconPaths.dataDir, config, "bootnode.log"); | ||
const {peerId, enr} = await initPeerIdAndEnr(args as unknown as BeaconArgs, bootnodeDir, logger); | ||
|
||
return {discv5Args, metricsArgs, bootnodeDir, network, version, commit, peerId, enr, logger}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import {CliCommand, CliCommandOptions} from "../../util/index.js"; | ||
import {GlobalArgs} from "../../options/index.js"; | ||
import {bootnodeOptions, BootnodeArgs} from "./options.js"; | ||
import {bootnodeHandler} from "./handler.js"; | ||
|
||
export const bootnode: CliCommand<BootnodeArgs, GlobalArgs> = { | ||
command: "bootnode", | ||
describe: | ||
"Run a discv5 bootnode. This will NOT perform any beacon node functions, rather, it will run a discv5 service that allows nodes on the network to discover one another.", | ||
options: bootnodeOptions as CliCommandOptions<BootnodeArgs>, | ||
handler: bootnodeHandler, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import {Options} from "yargs"; | ||
import {LogArgs, logOptions} from "../../options/logOptions.js"; | ||
import {CliCommandOptions} from "../../util/index.js"; | ||
import {MetricsArgs, options as metricsOptions} from "../../options/beaconNodeOptions/metrics.js"; | ||
import {defaultListenAddress, defaultP2pPort, defaultP2pPort6} from "../../options/beaconNodeOptions/network.js"; | ||
|
||
type BootnodeExtraArgs = { | ||
listenAddress?: string; | ||
port?: number; | ||
listenAddress6?: string; | ||
port6?: number; | ||
bootnodes?: string[]; | ||
bootnodesFile?: string; | ||
persistNetworkIdentity?: boolean; | ||
"enr.ip"?: string; | ||
"enr.ip6"?: string; | ||
"enr.udp"?: number; | ||
"enr.udp6"?: number; | ||
nat?: boolean; | ||
}; | ||
|
||
export const bootnodeExtraOptions: CliCommandOptions<BootnodeExtraArgs> = { | ||
wemeetagain marked this conversation as resolved.
Show resolved
Hide resolved
|
||
listenAddress: { | ||
type: "string", | ||
description: "The IPv4 address to listen for discv5 connections", | ||
defaultDescription: defaultListenAddress, | ||
group: "network", | ||
}, | ||
|
||
port: { | ||
alias: "discoveryPort", | ||
description: "The UDP port to listen on", | ||
type: "number", | ||
defaultDescription: String(defaultP2pPort), | ||
group: "network", | ||
}, | ||
|
||
listenAddress6: { | ||
type: "string", | ||
description: "The IPv6 address to listen for discv5 connections", | ||
group: "network", | ||
}, | ||
|
||
port6: { | ||
alias: "discoveryPort6", | ||
description: "The UDP port to listen on", | ||
type: "number", | ||
defaultDescription: String(defaultP2pPort6), | ||
group: "network", | ||
}, | ||
|
||
bootnodes: { | ||
type: "array", | ||
description: "Additional bootnodes for discv5 discovery", | ||
defaultDescription: JSON.stringify([]), | ||
// Each bootnode entry could be comma separated, just deserialize it into a single array | ||
// as comma separated entries are generally most friendly in ansible kind of setups, i.e. | ||
// [ "en1", "en2,en3" ] => [ 'en1', 'en2', 'en3' ] | ||
coerce: (args: string[]) => args.map((item) => item.split(",")).flat(1), | ||
group: "network", | ||
}, | ||
|
||
bootnodesFile: { | ||
description: "Additional bootnodes for discv5 discovery file path", | ||
type: "string", | ||
group: "network", | ||
}, | ||
|
||
persistNetworkIdentity: { | ||
description: "Whether to reuse the same peer-id across restarts", | ||
default: true, | ||
type: "boolean", | ||
group: "network", | ||
}, | ||
|
||
"enr.ip": { | ||
description: "Override ENR IP entry", | ||
type: "string", | ||
group: "enr", | ||
}, | ||
"enr.udp": { | ||
description: "Override ENR UDP entry", | ||
type: "number", | ||
group: "enr", | ||
}, | ||
"enr.ip6": { | ||
description: "Override ENR IPv6 entry", | ||
type: "string", | ||
group: "enr", | ||
}, | ||
"enr.udp6": { | ||
description: "Override ENR (IPv6-specific) UDP entry", | ||
type: "number", | ||
group: "enr", | ||
}, | ||
nat: { | ||
type: "boolean", | ||
description: "Allow ENR configuration of non-local addresses", | ||
group: "enr", | ||
}, | ||
}; | ||
|
||
export type BootnodeArgs = BootnodeExtraArgs & LogArgs & MetricsArgs; | ||
|
||
export const bootnodeOptions: {[k: string]: Options} = { | ||
...bootnodeExtraOptions, | ||
...logOptions, | ||
...metricsOptions, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Metrics should be done with prometheus as first medium, dumping to logs becomes noise for processes expected to run for days or weeks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree that metrics should be done with prometheus first. I think some logging can be useful tho.
I used this as reference: https://github.com/sigp/lighthouse/blob/dfcb3363c757671eb19d5f8e519b4b94ac74677a/boot_node/src/server.rs#L78-L121