Skip to content
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

smol fix #123

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions bin/node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ hilo-engine.workspace = true
# hilo = { workspace = true, features = ["registry"] }

# Alloy
alloy-rpc-types-engine = { workspace = true, features = ["jwt"] }

# op-alloy
op-alloy-genesis.workspace = true
op-alloy-registry.workspace = true

Expand Down
47 changes: 39 additions & 8 deletions bin/node/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
//! CLI arguments for the Hilo Node.

use std::{fs::File, path::PathBuf, sync::Arc};
use std::{fs::File, path::PathBuf};

use clap::Parser;
use eyre::{bail, Context, Result};
use serde_json::from_reader;
use tracing::debug;
use url::Url;

use alloy_rpc_types_engine::JwtSecret;
use op_alloy_genesis::RollupConfig;
use op_alloy_registry::ROLLUP_CONFIGS;

Expand Down Expand Up @@ -39,6 +40,14 @@ pub(crate) struct NodeArgs {
)]
pub metrics_port: u16,

/// URL of the checkpoint sync server to fetch checkpoints from.
#[clap(long = "checkpoint-sync-url")]
pub checkpoint_sync_url: Option<Url>,

/// An RPC URL to run the node's rpc server.
#[clap(long = "rpc-url")]
pub rpc_url: Option<Url>,

/// Chain ID of the L2 network
#[clap(long = "l2-chain-id", default_value_t = DEFAULT_L2_CHAIN_ID)]
pub l2_chain_id: u64,
Expand Down Expand Up @@ -101,44 +110,66 @@ pub(crate) struct NodeArgs {
#[allow(unused)]
impl NodeArgs {
/// Get the L2 rollup config, either from a file or the superchain registry.
pub fn get_l2_config(&self) -> Result<Arc<RollupConfig>> {
pub fn get_l2_config(&self) -> Result<RollupConfig> {
match &self.l2_config_file {
Some(path) => {
debug!("Loading l2 config from file: {:?}", path);
let file = File::open(path).wrap_err("Failed to open l2 config file")?;
Ok(Arc::new(from_reader(file).wrap_err("Failed to read l2 config file")?))
Ok(from_reader(file).wrap_err("Failed to read l2 config file")?)
}
None => {
debug!("Loading l2 config from superchain registry");
let Some(cfg) = ROLLUP_CONFIGS.get(&self.l2_chain_id).cloned() else {
bail!("Failed to find l2 config for chain ID {}", self.l2_chain_id);
};
Ok(Arc::new(cfg))
Ok(cfg)
}
}
}

/// Returns the JWT secret for the engine API
/// using the provided [PathBuf]. If the file is not found,
/// it will return the default JWT secret.
pub fn jwt_secret(&self) -> Option<String> {
pub fn jwt_secret(&self) -> Option<JwtSecret> {
match std::fs::read_to_string(&self.l2_engine_jwt_secret) {
Ok(content) => Some(content),
Ok(content) => JwtSecret::from_hex(content).ok(),
Err(_) => Self::default_jwt_secret(),
}
}

/// Uses the current directory to attempt to read
/// the JWT secret from a file named `jwt.hex`.
/// If the file is not found, it will return `None`.
pub fn default_jwt_secret() -> Option<String> {
pub fn default_jwt_secret() -> Option<JwtSecret> {
let cur_dir = std::env::current_dir().ok()?;
match std::fs::read_to_string(cur_dir.join("jwt.hex")) {
Ok(content) => Some(content),
Ok(content) => JwtSecret::from_hex(content).ok(),
Err(_) => {
tracing::error!("Failed to read JWT secret from file: {:?}", cur_dir);
None
}
}
}
}

impl From<NodeArgs> for hilo_node::Config {
fn from(args: NodeArgs) -> Self {
let rollup_config = args.get_l2_config().unwrap();
let jwt_secret = args.jwt_secret().unwrap();
Self {
l2_chain_id: args.l2_chain_id,
l1_rpc_url: args.l1_rpc_url,
l1_beacon_url: args.l1_beacon_client_url,
l2_rpc_url: args.l2_rpc_url,
l2_engine_url: args.l2_engine_api_url,
rollup_config,
jwt_secret,
checkpoint_sync_url: args.checkpoint_sync_url,
sync_mode: args.sync_mode,
rpc_url: args.rpc_url,
devnet: false,
// metrics_port: args.metrics_port,
// l1_blob_archiver_url: args.l1_blob_archiver_url,
}
}
}
19 changes: 12 additions & 7 deletions crates/node/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Contains the configuration for the hilo-node.

use crate::SyncMode;
use alloy_rpc_types_engine::JwtSecret;
use op_alloy_genesis::RollupConfig;
use serde::{Deserialize, Serialize};
Expand All @@ -8,27 +9,31 @@ use url::Url;
/// The global node configuration.
#[derive(Debug, Clone, Deserialize)]
pub struct Config {
/// The L2 Chain ID.
pub l2_chain_id: u64,
/// The L1 chain RPC URL
pub l1_rpc_url: String,
pub l1_rpc_url: Url,
/// The base chain beacon client RPC URL
pub l1_beacon_url: String,
pub l1_beacon_url: Url,
/// The L2 chain RPC URL
pub l2_rpc_url: String,
pub l2_rpc_url: Url,
/// The L2 engine API URL
pub l2_engine_url: String,
pub l2_engine_url: Url,
/// The rollup config
pub rollup_config: RollupConfig,
/// Engine API JWT Secret.
/// This is used to authenticate with the engine API
#[serde(deserialize_with = "deserialize_jwt_secret")]
pub jwt_secret: JwtSecret,
/// A trusted L2 RPC URL to use for fast/checkpoint syncing
pub checkpoint_sync_url: Option<String>,
/// The port of the `Magi` RPC server
pub rpc_url: Url,
pub checkpoint_sync_url: Option<Url>,
/// The hilo-node RPC server
pub rpc_url: Option<Url>,
/// The devnet mode.
/// If devnet is enabled.
pub devnet: bool,
/// The mode to sync.
pub sync_mode: SyncMode,
}

impl Serialize for Config {
Expand Down
2 changes: 1 addition & 1 deletion crates/node/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use serde::{Deserialize, Serialize};

/// Sync Mode Specifies how `magi` should sync the L2 chain
/// Sync Mode Specifies how to sync the L2 chain
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum SyncMode {
/// Fast sync mode
Expand Down