From f7d51837f5040007197857da1ff7554633c80ffb Mon Sep 17 00:00:00 2001 From: clabby Date: Mon, 3 Feb 2025 12:55:56 -0500 Subject: [PATCH] feat(host): Re-export default CLIs (#992) --- Cargo.lock | 10 ++--- bin/host/Cargo.toml | 9 ++++ bin/host/src/bin/host.rs | 66 ++++++++++++++++++++++++++++ bin/host/src/cli/mod.rs | 42 ++---------------- bin/host/src/cli/parser.rs | 6 ++- bin/host/src/interop/cli.rs | 5 ++- bin/host/src/interop/fetcher.rs | 3 +- bin/host/src/interop/local_kv.rs | 2 +- bin/host/src/interop/orchestrator.rs | 11 +++-- bin/host/src/lib.rs | 10 +++++ bin/host/src/main.rs | 34 -------------- bin/host/src/single/cli.rs | 2 +- bin/host/src/single/fetcher.rs | 2 +- bin/host/src/single/local_kv.rs | 2 +- bin/host/src/single/orchestrator.rs | 9 ++-- 15 files changed, 115 insertions(+), 98 deletions(-) create mode 100644 bin/host/src/bin/host.rs delete mode 100644 bin/host/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 7ad1b115e..9109f822f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -446,7 +446,7 @@ dependencies = [ "alloy-rlp", "alloy-serde", "alloy-sol-types", - "itertools 0.14.0", + "itertools 0.13.0", "serde", "serde_json", "thiserror 2.0.11", @@ -2433,9 +2433,9 @@ dependencies = [ [[package]] name = "itertools" -version = "0.14.0" +version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285" +checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" dependencies = [ "either", ] @@ -5387,9 +5387,9 @@ dependencies = [ [[package]] name = "webpki-roots" -version = "0.26.8" +version = "0.26.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2210b291f7ea53617fbafcc4939f10914214ec15aace5ba62293a668f322c5c9" +checksum = "5d642ff16b7e79272ae451b7322067cdc17cadf68c23264be9d94a32319efe7e" dependencies = [ "rustls-pki-types", ] diff --git a/bin/host/Cargo.toml b/bin/host/Cargo.toml index f611a646e..6eb984d63 100644 --- a/bin/host/Cargo.toml +++ b/bin/host/Cargo.toml @@ -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" diff --git a/bin/host/src/bin/host.rs b/bin/host/src/bin/host.rs new file mode 100644 index 000000000..f4c07f969 --- /dev/null +++ b/bin/host/src/bin/host.rs @@ -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(()) +} diff --git a/bin/host/src/cli/mod.rs b/bin/host/src/cli/mod.rs index 9fd6273fc..d1117753e 100644 --- a/bin/host/src/cli/mod.rs +++ b/bin/host/src/cli/mod.rs @@ -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)))) diff --git a/bin/host/src/cli/parser.rs b/bin/host/src/cli/parser.rs index 4af993f0e..93ea29b27 100644 --- a/bin/host/src/cli/parser.rs +++ b/bin/host/src/cli/parser.rs @@ -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 { +pub fn parse_b256(s: &str) -> Result { 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 { +pub fn parse_bytes(s: &str) -> Result { hex::decode(s).map_err(|e| format!("Invalid hex string: {}", e)).map(Bytes::from) } diff --git a/bin/host/src/interop/cli.rs b/bin/host/src/interop/cli.rs index d40164580..9b529c9fd 100644 --- a/bin/host/src/interop/cli.rs +++ b/bin/host/src/interop/cli.rs @@ -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}; diff --git a/bin/host/src/interop/fetcher.rs b/bin/host/src/interop/fetcher.rs index 4c9462e8a..2faf1acac 100644 --- a/bin/host/src/interop/fetcher.rs +++ b/bin/host/src/interop/fetcher.rs @@ -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, @@ -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, diff --git a/bin/host/src/interop/local_kv.rs b/bin/host/src/interop/local_kv.rs index 0e1c7771f..b96c90990 100644 --- a/bin/host/src/interop/local_kv.rs +++ b/bin/host/src/interop/local_kv.rs @@ -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, diff --git a/bin/host/src/interop/orchestrator.rs b/bin/host/src/interop/orchestrator.rs index 15834e057..eaa2a7e00 100644 --- a/bin/host/src/interop/orchestrator.rs +++ b/bin/host/src/interop/orchestrator.rs @@ -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; diff --git a/bin/host/src/lib.rs b/bin/host/src/lib.rs index 1694e0cc3..69c340cfa 100644 --- a/bin/host/src/lib.rs +++ b/bin/host/src/lib.rs @@ -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; diff --git a/bin/host/src/main.rs b/bin/host/src/main.rs deleted file mode 100644 index 64f52fe61..000000000 --- a/bin/host/src/main.rs +++ /dev/null @@ -1,34 +0,0 @@ -//! 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 crate::cli::{init_tracing_subscriber, HostCli, HostMode}; -use anyhow::Result; -use clap::Parser; -use kona_host::DetachedHostOrchestrator; -use tracing::info; - -pub mod cli; -pub mod eth; -pub mod interop; -pub mod single; - -#[tokio::main(flavor = "multi_thread")] -async fn main() -> Result<()> { - let cfg = HostCli::parse(); - init_tracing_subscriber(cfg.v)?; - - match cfg.mode { - HostMode::Single(cfg) => { - cfg.run().await?; - } - HostMode::Super(cfg) => { - cfg.run().await?; - } - } - - info!("Exiting host program."); - Ok(()) -} diff --git a/bin/host/src/single/cli.rs b/bin/host/src/single/cli.rs index 42dc61229..313c625fa 100644 --- a/bin/host/src/single/cli.rs +++ b/bin/host/src/single/cli.rs @@ -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; diff --git a/bin/host/src/single/fetcher.rs b/bin/host/src/single/fetcher.rs index c747448bd..eb2a26890 100644 --- a/bin/host/src/single/fetcher.rs +++ b/bin/host/src/single/fetcher.rs @@ -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, @@ -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, diff --git a/bin/host/src/single/local_kv.rs b/bin/host/src/single/local_kv.rs index 05b4e718a..2ac660b8a 100644 --- a/bin/host/src/single/local_kv.rs +++ b/bin/host/src/single/local_kv.rs @@ -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, diff --git a/bin/host/src/single/orchestrator.rs b/bin/host/src/single/orchestrator.rs index 02f8b4fde..0673ea411 100644 --- a/bin/host/src/single/orchestrator.rs +++ b/bin/host/src/single/orchestrator.rs @@ -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;