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

feat(hera): Subcommands #63

Merged
merged 6 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 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/hera/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ categories.workspace = true
# Local Dependencies
rollup = { path = "../../crates/rollup" }

# Superchain
superchain-registry.workspace = true

# Workspace
eyre.workspace = true
alloy.workspace = true
Expand Down
68 changes: 32 additions & 36 deletions bin/hera/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,49 @@
#![doc(issue_tracker_base_url = "https://github.com/paradigmxyz/op-rs/issues/")]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

use alloy::primitives::address;
use clap::Parser;
use clap::{Parser, Subcommand};
use eyre::Result;
use op_net::driver::NetworkDriver;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};

/// The default L2 chain ID to use. This corresponds to OP Mainnet.
pub const DEFAULT_L2_CHAIN_ID: u64 = 10;
mod network;
mod node;

/// The Hera CLI Arguments.
#[derive(Debug, Clone, Parser)]
pub struct HeraArgs {
/// Chain ID of the L2 network
#[clap(long = "hera.l2-chain-id", default_value_t = DEFAULT_L2_CHAIN_ID)]
#[derive(Parser, Clone, Debug)]
#[command(author, version, about, long_about = None)]
pub(crate) struct HeraArgs {
/// Global arguments for the Hera CLI.
#[clap(flatten)]
pub global: GlobalArgs,
/// The subcommand to run.
#[clap(subcommand)]
pub subcommand: HeraSubcommand,
}

/// Global arguments for the Hera CLI.
#[derive(Parser, Clone, Debug)]
pub(crate) struct GlobalArgs {
refcell marked this conversation as resolved.
Show resolved Hide resolved
/// The L2 chain ID to use.
#[clap(long, short = 'c', default_value = "10", help = "The L2 chain ID to use")]
pub l2_chain_id: u64,
}

/// Subcommands for the CLI.
#[derive(Debug, Clone, Subcommand)]
pub(crate) enum HeraSubcommand {
/// Run the standalone Hera node.
Node(node::NodeCommand),
/// Networking utility commands.
Network(network::NetworkCommand),
}

#[tokio::main]
async fn main() -> Result<()> {
let args = HeraArgs::parse();
rollup::init_telemetry_stack(8090)?;

tracing::info!("Hera OP Stack Rollup node");

let signer = address!("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 9099);
let mut driver = NetworkDriver::builder()
.with_chain_id(args.l2_chain_id)
.with_unsafe_block_signer(signer)
.with_gossip_addr(socket)
.build()
.expect("Failed to builder network driver");

// Call `.start()` on the driver.
let recv = driver.take_unsafe_block_recv().ok_or(eyre::eyre!("No unsafe block receiver"))?;
driver.start().expect("Failed to start network driver");

tracing::info!("NetworkDriver started successfully.");

loop {
match recv.recv() {
Ok(block) => {
tracing::info!("Received unsafe block: {:?}", block);
}
Err(e) => {
tracing::warn!("Failed to receive unsafe block: {:?}", e);
}
}
match args.subcommand {
HeraSubcommand::Node(node) => node.run(&args.global).await?,
HeraSubcommand::Network(network) => network.run(&args.global).await?,
}
Ok(())
}
65 changes: 65 additions & 0 deletions bin/hera/src/network.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//! Networking subcommand for Hera.

use crate::GlobalArgs;
use clap::Args;
use eyre::Result;
use op_net::driver::NetworkDriver;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use superchain_registry::ROLLUP_CONFIGS;

/// The Hera network subcommand.
#[derive(Debug, Clone, Args)]
#[non_exhaustive]
pub struct NetworkCommand {
/// Run peer discovery.
#[clap(long, short = 'p', help = "Runs peer discovery")]
pub peer: bool,
refcell marked this conversation as resolved.
Show resolved Hide resolved
/// Run the gossip driver.
#[clap(long, short = 'g', help = "Runs the unsafe block gossipping service")]
pub gossip: bool,
}

impl NetworkCommand {
/// Run the network subcommand.
pub async fn run(&self, args: &GlobalArgs) -> Result<()> {
if self.peer {
println!("Running peer discovery");
}
if self.gossip {
println!("Running gossip driver");
}
let signer = ROLLUP_CONFIGS
.get(&args.l2_chain_id)
.ok_or(eyre::eyre!("No rollup config found for chain ID"))?
.genesis
.system_config
.as_ref()
.ok_or(eyre::eyre!("No system config found for chain ID"))?
.batcher_address;
let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 9099);
refcell marked this conversation as resolved.
Show resolved Hide resolved
let mut driver = NetworkDriver::builder()
.with_chain_id(args.l2_chain_id)
.with_unsafe_block_signer(signer)
.with_gossip_addr(socket)
.build()
.expect("Failed to builder network driver");

// Call `.start()` on the driver.
let recv =
driver.take_unsafe_block_recv().ok_or(eyre::eyre!("No unsafe block receiver"))?;
driver.start().expect("Failed to start network driver");

tracing::info!("NetworkDriver started successfully.");

loop {
match recv.recv() {
Ok(block) => {
tracing::info!("Received unsafe block: {:?}", block);
}
Err(e) => {
tracing::warn!("Failed to receive unsafe block: {:?}", e);
}
}
}
}
}
17 changes: 17 additions & 0 deletions bin/hera/src/node.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//! Node subcommand for Hera.

use crate::GlobalArgs;
use clap::Args;
use eyre::Result;

/// The Hera node subcommand.
#[derive(Debug, Clone, Args)]
#[non_exhaustive]
pub struct NodeCommand {}

impl NodeCommand {
/// Run the node subcommand.
pub async fn run(&self, _args: &GlobalArgs) -> Result<()> {
unimplemented!()
}
}