From d1f46f25b30d79f2cf3fbab2ac8c88f5ffec1639 Mon Sep 17 00:00:00 2001 From: refcell Date: Wed, 27 Nov 2024 12:19:22 -0500 Subject: [PATCH] chore(host): Hint Parsing Cleanup (#844) * chore(host): cleanup hint parsing * fix: fmt * fix: fmt * fixes --- bin/host/src/cli/mod.rs | 15 ++++++++++--- bin/host/src/fetcher/mod.rs | 7 +++--- bin/host/src/lib.rs | 1 - bin/host/src/util.rs | 34 ----------------------------- book/src/sdk/pipeline/intro.md | 2 +- crates/proof-sdk/proof/src/hint.rs | 35 +++++++++++++++++++++++++++++- crates/proof-sdk/proof/src/lib.rs | 2 +- 7 files changed, 52 insertions(+), 44 deletions(-) delete mode 100644 bin/host/src/util.rs diff --git a/bin/host/src/cli/mod.rs b/bin/host/src/cli/mod.rs index a4206960d..4acdab56b 100644 --- a/bin/host/src/cli/mod.rs +++ b/bin/host/src/cli/mod.rs @@ -6,16 +6,18 @@ use crate::{ DiskKeyValueStore, LocalKeyValueStore, MemoryKeyValueStore, SharedKeyValueStore, SplitKeyValueStore, }, - util, }; use alloy_primitives::B256; use alloy_provider::ReqwestProvider; +use alloy_rpc_client::RpcClient; +use alloy_transport_http::Http; use anyhow::{anyhow, Result}; use clap::{ builder::styling::{AnsiColor, Color, Style}, ArgAction, Parser, }; use op_alloy_genesis::RollupConfig; +use reqwest::Client; use serde::Serialize; use std::{path::PathBuf, sync::Arc}; use tokio::sync::RwLock; @@ -128,6 +130,13 @@ impl HostCli { self.l1_beacon_address.is_none() } + /// Returns an HTTP provider for the given URL. + fn http_provider(url: &str) -> ReqwestProvider { + let url = url.parse().unwrap(); + let http = Http::::new(url); + ReqwestProvider::new(RpcClient::new(http, true)) + } + /// Creates the providers associated with the [HostCli] configuration. /// /// ## Returns @@ -142,10 +151,10 @@ impl HostCli { ) .await .map_err(|e| anyhow!("Failed to load blob provider configuration: {e}"))?; - let l1_provider = util::http_provider( + let l1_provider = Self::http_provider( self.l1_node_address.as_ref().ok_or(anyhow!("Provider must be set"))?, ); - let l2_provider = util::http_provider( + let l2_provider = Self::http_provider( self.l2_node_address.as_ref().ok_or(anyhow!("L2 node address must be set"))?, ); diff --git a/bin/host/src/fetcher/mod.rs b/bin/host/src/fetcher/mod.rs index 644306b97..32c85643c 100644 --- a/bin/host/src/fetcher/mod.rs +++ b/bin/host/src/fetcher/mod.rs @@ -1,7 +1,7 @@ //! This module contains the [Fetcher] struct, which is responsible for fetching preimages from a //! remote source. -use crate::{blobs::OnlineBlobProvider, kv::KeyValueStore, util}; +use crate::{blobs::OnlineBlobProvider, kv::KeyValueStore}; use alloy_consensus::{Header, TxEnvelope, EMPTY_ROOT_HASH}; use alloy_eips::{ eip2718::Encodable2718, @@ -17,7 +17,7 @@ use alloy_rpc_types::{ }; use anyhow::{anyhow, Result}; use kona_preimage::{PreimageKey, PreimageKeyType}; -use kona_proof::HintType; +use kona_proof::{Hint, HintType}; use op_alloy_protocol::BlockInfo; use op_alloy_rpc_types_engine::OpPayloadAttributes; use std::sync::Arc; @@ -97,7 +97,8 @@ where /// Fetch the preimage for the given hint and insert it into the key-value store. async fn prefetch(&self, hint: &str) -> Result<()> { - let (hint_type, hint_data) = util::parse_hint(hint)?; + let hint = Hint::parse(hint)?; + let (hint_type, hint_data) = hint.split(); trace!(target: "fetcher", "Fetching hint: {hint_type} {hint_data}"); match hint_type { diff --git a/bin/host/src/lib.rs b/bin/host/src/lib.rs index 9f3029633..45af2fd1f 100644 --- a/bin/host/src/lib.rs +++ b/bin/host/src/lib.rs @@ -10,7 +10,6 @@ pub mod fetcher; pub mod kv; pub mod preimage; pub mod server; -pub mod util; use anyhow::Result; use fetcher::Fetcher; diff --git a/bin/host/src/util.rs b/bin/host/src/util.rs deleted file mode 100644 index d4e92adf2..000000000 --- a/bin/host/src/util.rs +++ /dev/null @@ -1,34 +0,0 @@ -//! Contains utility functions and helpers for the host program. - -use alloy_primitives::{hex, Bytes}; -use alloy_provider::ReqwestProvider; -use alloy_rpc_client::RpcClient; -use alloy_transport_http::Http; -use anyhow::{anyhow, Result}; -use kona_proof::HintType; -use reqwest::Client; - -/// Parses a hint from a string. -/// -/// Hints are of the format ` `, where `` is a string that -/// represents the type of hint, and `` is the data associated with the hint -/// (bytes encoded as hex UTF-8). -pub(crate) fn parse_hint(s: &str) -> Result<(HintType, Bytes)> { - let mut parts = s.split(' ').collect::>(); - - if parts.len() != 2 { - anyhow::bail!("Invalid hint format: {}", s); - } - - let hint_type = HintType::try_from(parts.remove(0))?; - let hint_data = hex::decode(parts.remove(0)).map_err(|e| anyhow!(e))?.into(); - - Ok((hint_type, hint_data)) -} - -/// Returns an HTTP provider for the given URL. -pub(crate) fn http_provider(url: &str) -> ReqwestProvider { - let url = url.parse().unwrap(); - let http = Http::::new(url); - ReqwestProvider::new(RpcClient::new(http, true)) -} diff --git a/book/src/sdk/pipeline/intro.md b/book/src/sdk/pipeline/intro.md index daa05b610..146aae8cf 100644 --- a/book/src/sdk/pipeline/intro.md +++ b/book/src/sdk/pipeline/intro.md @@ -95,7 +95,7 @@ the [`PipelineBuilder`][builder] to instantiate a [`DerivationPipeline`][dp]. use std::sync::Arc; use op_alloy_protocol::BlockInfo; use op_alloy_genesis::RollupConfig; -use superchain_derive::*; +use hilo_providers_alloy::*; // Use a default rollup config. let rollup_config = Arc::new(RollupConfig::default()); diff --git a/crates/proof-sdk/proof/src/hint.rs b/crates/proof-sdk/proof/src/hint.rs index f63af9b5f..f77fadc30 100644 --- a/crates/proof-sdk/proof/src/hint.rs +++ b/crates/proof-sdk/proof/src/hint.rs @@ -5,9 +5,42 @@ use alloc::{ string::{String, ToString}, vec::Vec, }; -use alloy_primitives::hex; +use alloy_primitives::{hex, Bytes}; use core::fmt::Display; +/// A [Hint] is parsed in the format ` `, where `` is a string that +/// represents the type of hint, and `` is the data associated with the hint (bytes +/// encoded as hex UTF-8). +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Hint { + /// The type of hint. + pub hint_type: HintType, + /// The data associated with the hint. + pub hint_data: Bytes, +} + +impl Hint { + /// Parses a hint from a string. + pub fn parse(s: &str) -> Result { + let mut parts = s.split(' ').collect::>(); + + if parts.len() != 2 { + return Err(HintParsingError(alloc::format!("Invalid hint format: {}", s))); + } + + let hint_type = HintType::try_from(parts.remove(0))?; + let hint_data = + hex::decode(parts.remove(0)).map_err(|e| HintParsingError(e.to_string()))?.into(); + + Ok(Self { hint_type, hint_data }) + } + + /// Splits the [Hint] into its components. + pub fn split(self) -> (HintType, Bytes) { + (self.hint_type, self.hint_data) + } +} + /// The [HintType] enum is used to specify the type of hint that was received. #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] pub enum HintType { diff --git a/crates/proof-sdk/proof/src/lib.rs b/crates/proof-sdk/proof/src/lib.rs index 25a7dd9ff..b3bfa1796 100644 --- a/crates/proof-sdk/proof/src/lib.rs +++ b/crates/proof-sdk/proof/src/lib.rs @@ -21,7 +21,7 @@ pub mod errors; pub mod executor; mod hint; -pub use hint::HintType; +pub use hint::{Hint, HintType}; pub mod boot; pub use boot::BootInfo;