Skip to content

Instantly share code, notes, and snippets.

@jmcph4
Last active June 1, 2024 07:24
Show Gist options
  • Select an option

  • Save jmcph4/80b61b4c0bfb5da0376e2e85558670a9 to your computer and use it in GitHub Desktop.

Select an option

Save jmcph4/80b61b4c0bfb5da0376e2e85558670a9 to your computer and use it in GitHub Desktop.
Start Lighthouse programmatically
[package]
name = "hookable"
version = "0.1.0"
edition = "2021"
[dependencies]
eyre = "0.6.12"
tokio = { version = "1.38.0", features = ["full"] }
slog = "2.7.0"
lighthouse = { git = "https://github.com/sigp/lighthouse" }
beacon_node = { git = "https://github.com/sigp/lighthouse" }
task_executor = { git = "https://github.com/sigp/lighthouse" }
eth2 = { git = "https://github.com/sigp/lighthouse" }
environment = { git = "https://github.com/sigp/lighthouse" }
eth2_network_config = { git = "https://github.com/sigp/lighthouse" }
use beacon_node::{ClientConfig, ProductionBeaconNode};
use environment::{
Environment, EnvironmentBuilder, LoggerConfig, RuntimeContext,
};
use eth2::types::eth_spec::MainnetEthSpec;
use eth2_network_config::{Eth2NetworkConfig, DEFAULT_HARDCODED_NETWORK};
use eyre::eyre;
use slog::info;
use task_executor::ShutdownReason;
fn main() -> eyre::Result<()> {
let mut environment: Environment<MainnetEthSpec> = EnvironmentBuilder::mainnet(
)
.initialize_logger(LoggerConfig::default())
.map_err(|e| eyre!("{e}"))?
.multi_threaded_tokio_runtime()
.map_err(|e| eyre!("{e}"))?
.eth2_network_config(
Eth2NetworkConfig::constant(DEFAULT_HARDCODED_NETWORK)
.map_err(|e| eyre!("{e}"))?
.expect("failed to construct hardcoded Eth2 network configuration"),
)
.map_err(|e| eyre!("{e}"))?
.build()
.map_err(|e| eyre!("{e}"))?;
let ctx: RuntimeContext<MainnetEthSpec> = environment.core_context();
let log = ctx.log().clone();
let cfg = ClientConfig::default();
ctx.executor.clone().spawn(
async move {
ProductionBeaconNode::new(ctx, cfg)
.await
.map_err(|e| eyre!("{e}"))
.expect("failed to start production-ready Beacon Node");
},
"beacon_node",
);
let shutdown_reason = environment
.block_until_shutdown_requested()
.map_err(|e| eyre!("{e}"))?;
info!(log, "Shutting down.."; "reason" => ?shutdown_reason);
environment.fire_signal();
// Shutdown the environment once all tasks have completed.
environment.shutdown_on_idle();
match shutdown_reason {
ShutdownReason::Success(_) => Ok(()),
ShutdownReason::Failure(msg) => Err(msg.to_string()),
}
.map_err(|e| eyre!("{e}"))?;
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment