Skip to content

Commit

Permalink
chore(host): Hint Parsing Cleanup (#844)
Browse files Browse the repository at this point in the history
* chore(host): cleanup hint parsing

* fix: fmt

* fix: fmt

* fixes
  • Loading branch information
refcell authored Nov 27, 2024
1 parent da7beb5 commit d1f46f2
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 44 deletions.
15 changes: 12 additions & 3 deletions bin/host/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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::<Client>::new(url);
ReqwestProvider::new(RpcClient::new(http, true))
}

/// Creates the providers associated with the [HostCli] configuration.
///
/// ## Returns
Expand All @@ -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"))?,
);

Expand Down
7 changes: 4 additions & 3 deletions bin/host/src/fetcher/mod.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion bin/host/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
34 changes: 0 additions & 34 deletions bin/host/src/util.rs

This file was deleted.

2 changes: 1 addition & 1 deletion book/src/sdk/pipeline/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
35 changes: 34 additions & 1 deletion crates/proof-sdk/proof/src/hint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<hint_type> <hint_data>`, where `<hint_type>` is a string that
/// represents the type of hint, and `<hint_data>` 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<Self, HintParsingError> {
let mut parts = s.split(' ').collect::<Vec<_>>();

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 {
Expand Down
2 changes: 1 addition & 1 deletion crates/proof-sdk/proof/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit d1f46f2

Please sign in to comment.