Skip to content

Commit

Permalink
feat(host): Re-export default CLIs (#992)
Browse files Browse the repository at this point in the history
  • Loading branch information
clabby authored Feb 3, 2025
1 parent 17ffc4f commit f7d5183
Show file tree
Hide file tree
Showing 15 changed files with 115 additions and 98 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

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

9 changes: 9 additions & 0 deletions bin/host/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,12 @@ tracing-subscriber = { workspace = true, features = ["fmt"] }

[dev-dependencies]
proptest.workspace = true

[features]
default = ["single", "interop"]
single = []
interop = ["single"]

[[bin]]
name = "kona-host"
path = "src/bin/host.rs"
66 changes: 66 additions & 0 deletions bin/host/src/bin/host.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//! Main entrypoint for the host binary.
#![warn(missing_debug_implementations, missing_docs, unreachable_pub, rustdoc::all)]
#![deny(unused_must_use, rust_2018_idioms)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

use anyhow::Result;
use clap::{ArgAction, Parser, Subcommand};
use kona_host::{
cli::{cli_styles, init_tracing_subscriber},
DetachedHostOrchestrator,
};
use serde::Serialize;
use tracing::info;

const ABOUT: &str = "
kona-host is a CLI application that runs the Kona pre-image server and client program. The host
can run in two modes: server mode and native mode. In server mode, the host runs the pre-image
server and waits for the client program in the parent process to request pre-images. In native
mode, the host runs the client program in a separate thread with the pre-image server in the
primary thread.
";

/// The host binary CLI application arguments.
#[derive(Parser, Serialize, Clone, Debug)]
#[command(about = ABOUT, version, styles = cli_styles())]
pub struct HostCli {
/// Verbosity level (0-2)
#[arg(long, short, action = ArgAction::Count)]
pub v: u8,
/// Host mode
#[clap(subcommand)]
pub mode: HostMode,
}

/// Operation modes for the host binary.
#[derive(Subcommand, Serialize, Clone, Debug)]
#[allow(clippy::large_enum_variant)]
pub enum HostMode {
/// Run the host in single-chain mode.
#[cfg(feature = "single")]
Single(kona_host::single::SingleChainHostCli),
/// Run the host in super-chain (interop) mode.
#[cfg(feature = "interop")]
Super(kona_host::interop::InteropHostCli),
}

#[tokio::main(flavor = "multi_thread")]
async fn main() -> Result<()> {
let cfg = HostCli::parse();
init_tracing_subscriber(cfg.v)?;

match cfg.mode {
#[cfg(feature = "single")]
HostMode::Single(cfg) => {
cfg.run().await?;
}
#[cfg(feature = "interop")]
HostMode::Super(cfg) => {
cfg.run().await?;
}
}

info!("Exiting host program.");
Ok(())
}
42 changes: 3 additions & 39 deletions bin/host/src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,50 +1,14 @@
//! This module contains all CLI-specific code for the host binary.
use crate::{interop::InteropHostCli, single::SingleChainHostCli};
use clap::{
builder::styling::{AnsiColor, Color, Style},
ArgAction, Parser, Subcommand,
};
use serde::Serialize;
use clap::builder::styling::{AnsiColor, Color, Style};

mod parser;
pub(crate) use parser::{parse_b256, parse_bytes};
pub mod parser;

mod tracing_util;
pub use tracing_util::init_tracing_subscriber;

const ABOUT: &str = "
kona-host is a CLI application that runs the Kona pre-image server and client program. The host
can run in two modes: server mode and native mode. In server mode, the host runs the pre-image
server and waits for the client program in the parent process to request pre-images. In native
mode, the host runs the client program in a separate thread with the pre-image server in the
primary thread.
";

/// The host binary CLI application arguments.
#[derive(Parser, Serialize, Clone, Debug)]
#[command(about = ABOUT, version, styles = cli_styles())]
pub struct HostCli {
/// Verbosity level (0-2)
#[arg(long, short, action = ArgAction::Count)]
pub v: u8,
/// Host mode
#[clap(subcommand)]
pub mode: HostMode,
}

/// Operation modes for the host binary.
#[derive(Subcommand, Serialize, Clone, Debug)]
#[allow(clippy::large_enum_variant)]
pub enum HostMode {
/// Run the host in single-chain mode.
Single(SingleChainHostCli),
/// Run the host in super-chain (interop) mode.
Super(InteropHostCli),
}

/// Styles for the CLI application.
pub(crate) const fn cli_styles() -> clap::builder::Styles {
pub const fn cli_styles() -> clap::builder::Styles {
clap::builder::Styles::styled()
.usage(Style::new().bold().underline().fg_color(Some(Color::Ansi(AnsiColor::Yellow))))
.header(Style::new().bold().underline().fg_color(Some(Color::Ansi(AnsiColor::Yellow))))
Expand Down
6 changes: 4 additions & 2 deletions bin/host/src/cli/parser.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
//! Parser functions for CLI arguments.
use alloy_primitives::{hex, Bytes, B256};
use std::str::FromStr;

/// Parse a string slice into [B256].
pub(crate) fn parse_b256(s: &str) -> Result<B256, String> {
pub fn parse_b256(s: &str) -> Result<B256, String> {
B256::from_str(s).map_err(|_| format!("Invalid B256 value: {}", s))
}

/// Parse a string slice into [Bytes].
pub(crate) fn parse_bytes(s: &str) -> Result<Bytes, String> {
pub fn parse_bytes(s: &str) -> Result<Bytes, String> {
hex::decode(s).map_err(|e| format!("Invalid hex string: {}", e)).map(Bytes::from)
}
5 changes: 4 additions & 1 deletion bin/host/src/interop/cli.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
//! This module contains all CLI-specific code for the interop entrypoint.
use super::local_kv::DEFAULT_CHAIN_ID;
use crate::cli::{cli_styles, parse_b256, parse_bytes};
use crate::cli::{
cli_styles,
parser::{parse_b256, parse_bytes},
};
use alloy_primitives::{Bytes, B256};
use alloy_rlp::Decodable;
use anyhow::{anyhow, Result};
Expand Down
3 changes: 1 addition & 2 deletions bin/host/src/interop/fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//! preimages from a remote source serving the super-chain (interop) proof mode.
use super::InteropHostCli;
use crate::single::SingleChainFetcher;
use crate::{single::SingleChainFetcher, KeyValueStore, PreimageServer};
use alloy_consensus::{Header, Sealed, TxEnvelope, EMPTY_ROOT_HASH};
use alloy_eips::{
eip2718::Encodable2718,
Expand All @@ -19,7 +19,6 @@ use anyhow::{anyhow, Result};
use async_trait::async_trait;
use kona_driver::Driver;
use kona_executor::TrieDBProvider;
use kona_host::{KeyValueStore, PreimageServer};
use kona_preimage::{
errors::{PreimageOracleError, PreimageOracleResult},
BidirectionalChannel, HintReader, HintRouter, HintWriter, OracleReader, OracleServer,
Expand Down
2 changes: 1 addition & 1 deletion bin/host/src/interop/local_kv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
//! using the [InteropHostCli] config.
use super::InteropHostCli;
use crate::KeyValueStore;
use alloy_primitives::{keccak256, B256};
use anyhow::Result;
use kona_host::KeyValueStore;
use kona_preimage::PreimageKey;
use kona_proof_interop::boot::{
L1_HEAD_KEY, L2_AGREED_PRE_STATE_KEY, L2_CLAIMED_POST_STATE_KEY, L2_CLAIMED_TIMESTAMP_KEY,
Expand Down
11 changes: 5 additions & 6 deletions bin/host/src/interop/orchestrator.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
//! [SingleChainHostCli]'s [HostOrchestrator] + [DetachedHostOrchestrator] implementations.
//! [InteropHostCli]'s [HostOrchestrator] + [DetachedHostOrchestrator] implementations.
use super::{InteropFetcher, InteropHostCli, LocalKeyValueStore};
use crate::eth::http_provider;
use crate::{
eth::http_provider, DetachedHostOrchestrator, DiskKeyValueStore, Fetcher, HostOrchestrator,
MemoryKeyValueStore, SharedKeyValueStore, SplitKeyValueStore,
};
use alloy_primitives::map::HashMap;
use alloy_provider::{Provider, RootProvider};
use anyhow::{anyhow, Result};
use async_trait::async_trait;
use kona_host::{
DetachedHostOrchestrator, DiskKeyValueStore, Fetcher, HostOrchestrator, MemoryKeyValueStore,
SharedKeyValueStore, SplitKeyValueStore,
};
use kona_preimage::{HintWriter, NativeChannel, OracleReader};
use kona_providers_alloy::{OnlineBeaconClient, OnlineBlobProvider};
use std::sync::Arc;
Expand Down
10 changes: 10 additions & 0 deletions bin/host/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,13 @@ pub use preimage::{

mod server;
pub use server::PreimageServer;

pub mod cli;

pub mod eth;

#[cfg(feature = "single")]
pub mod single;

#[cfg(feature = "interop")]
pub mod interop;
34 changes: 0 additions & 34 deletions bin/host/src/main.rs

This file was deleted.

2 changes: 1 addition & 1 deletion bin/host/src/single/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! This module contains all CLI-specific code for the single chain entrypoint.
use crate::cli::{cli_styles, parse_b256};
use crate::cli::{cli_styles, parser::parse_b256};
use alloy_primitives::B256;
use anyhow::{anyhow, Result};
use clap::Parser;
Expand Down
2 changes: 1 addition & 1 deletion bin/host/src/single/fetcher.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! This module contains the [SingleChainFetcher] struct, which is responsible for fetching
//! preimages from a remote source serving the single-chain proof mode.
use crate::KeyValueStore;
use alloy_consensus::{Header, TxEnvelope, EMPTY_ROOT_HASH};
use alloy_eips::{
eip2718::Encodable2718,
Expand All @@ -15,7 +16,6 @@ use alloy_rpc_types::{
Transaction,
};
use anyhow::{anyhow, Result};
use kona_host::KeyValueStore;
use kona_preimage::{
errors::{PreimageOracleError, PreimageOracleResult},
HintRouter, PreimageFetcher, PreimageKey, PreimageKeyType,
Expand Down
2 changes: 1 addition & 1 deletion bin/host/src/single/local_kv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
//! using the [SingleChainHostCli] config.
use super::SingleChainHostCli;
use crate::KeyValueStore;
use alloy_primitives::B256;
use anyhow::Result;
use kona_host::KeyValueStore;
use kona_preimage::PreimageKey;
use kona_proof::boot::{
L1_HEAD_KEY, L2_CHAIN_ID_KEY, L2_CLAIM_BLOCK_NUMBER_KEY, L2_CLAIM_KEY, L2_OUTPUT_ROOT_KEY,
Expand Down
9 changes: 4 additions & 5 deletions bin/host/src/single/orchestrator.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
//! [SingleChainHostCli]'s [HostOrchestrator] + [DetachedHostOrchestrator] implementations.
use super::{LocalKeyValueStore, SingleChainFetcher, SingleChainHostCli};
use crate::eth::http_provider;
use crate::{
eth::http_provider, DetachedHostOrchestrator, DiskKeyValueStore, Fetcher, HostOrchestrator,
MemoryKeyValueStore, SharedKeyValueStore, SplitKeyValueStore,
};
use alloy_provider::RootProvider;
use anyhow::{anyhow, Result};
use async_trait::async_trait;
use kona_host::{
DetachedHostOrchestrator, DiskKeyValueStore, Fetcher, HostOrchestrator, MemoryKeyValueStore,
SharedKeyValueStore, SplitKeyValueStore,
};
use kona_preimage::{HintWriter, NativeChannel, OracleReader};
use kona_providers_alloy::{OnlineBeaconClient, OnlineBlobProvider};
use std::sync::Arc;
Expand Down

0 comments on commit f7d5183

Please sign in to comment.