Skip to content

Commit

Permalink
feat(preimage): tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
clabby committed Apr 6, 2024
1 parent d223847 commit 8f3d29c
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 8 deletions.
32 changes: 32 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion crates/preimage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ homepage.workspace = true
# workspace
anyhow.workspace = true
cfg-if.workspace = true
tracing.workspace = true
alloy-primitives = "0.7.0"

# local
kona-common = { path = "../common", version = "0.0.1" }

[dev-dependencies]
tokio = { version = "1.36.0", features = ["full"] }
tempfile = "3.10.0"
alloy-primitives = "0.7.0"
26 changes: 19 additions & 7 deletions crates/preimage/src/hint.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{traits::HintWriterClient, HintReaderServer, PipeHandle};
use alloc::{string::String, vec};
use anyhow::Result;
use tracing::{debug, error};

/// A [HintWriter] is a high-level interface to the hint pipe. It provides a way to write hints to
/// the host.
Expand All @@ -26,13 +27,19 @@ impl HintWriterClient for HintWriter {
hint_bytes[0..4].copy_from_slice(u32::to_be_bytes(hint.len() as u32).as_ref());
hint_bytes[4..].copy_from_slice(hint.as_bytes());

debug!("Writing hint \"{hint}\"");

// Write the hint to the host.
self.pipe_handle.write(&hint_bytes)?;

debug!("Successfully wrote hint");

// Read the hint acknowledgement from the host.
let mut hint_ack = [0u8; 1];
self.pipe_handle.read_exact(&mut hint_ack)?;

debug!("Received hint acknowledgement");

Ok(())
}
}
Expand All @@ -59,22 +66,27 @@ impl HintReaderServer for HintReader {
let len = u32::from_be_bytes(len_buf);

// Read the raw hint payload.
let mut payload = vec![0u8; len as usize];
self.pipe_handle.read_exact(payload.as_mut_slice())?;
let mut raw_payload = vec![0u8; len as usize];
self.pipe_handle.read_exact(raw_payload.as_mut_slice())?;
let payload = String::from_utf8(raw_payload)
.map_err(|e| anyhow::anyhow!("Failed to decode hint payload: {e}"))?;

debug!("Successfully read hint: {payload}");

// Route the hint
if let Err(e) = route_hint(
String::from_utf8(payload)
.map_err(|e| anyhow::anyhow!("Failed to decode hint payload: {e}"))?,
) {
if let Err(e) = route_hint(payload) {
// Write back on error to prevent blocking the client.
self.pipe_handle.write(&[0x00])?;
anyhow::bail!("Failed to handle hint: {e}");

error!("Failed to route hint: {e}");
anyhow::bail!("Failed to rout hint: {e}");
}

// Write back an acknowledgement to the client to unblock their process.
self.pipe_handle.write(&[0x00])?;

debug!("Successfully routed and acknowledged hint");

Ok(())
}
}
Expand Down
9 changes: 9 additions & 0 deletions crates/preimage/src/key.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
//! Contains the [PreimageKey] type, which is used to identify preimages that may be fetched from
//! the preimage oracle.
use alloy_primitives::B256;

/// <https://specs.optimism.io/experimental/fault-proof/index.html#pre-image-key-types>
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Hash)]
#[repr(u8)]
Expand Down Expand Up @@ -93,6 +95,13 @@ impl TryFrom<[u8; 32]> for PreimageKey {
}
}

impl core::fmt::Display for PreimageKey {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let raw: [u8; 32] = (*self).into();
write!(f, "{}", B256::from(raw))
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
9 changes: 9 additions & 0 deletions crates/preimage/src/oracle.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{PipeHandle, PreimageKey, PreimageOracleClient, PreimageOracleServer};
use alloc::vec::Vec;
use anyhow::{bail, Result};
use tracing::debug;

/// An [OracleReader] is a high-level interface to the preimage oracle.
#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -36,9 +37,13 @@ impl PreimageOracleClient for OracleReader {
let length = self.write_key(key)?;
let mut data_buffer = alloc::vec![0; length];

debug!("Reading data from preimage oracle. Key {}", key);

// Grab a read lock on the preimage pipe to read the data.
self.pipe_handle.read_exact(&mut data_buffer)?;

debug!("Successfully read data from preimage oracle. Key: {}", key);

Ok(data_buffer)
}

Expand All @@ -48,13 +53,17 @@ impl PreimageOracleClient for OracleReader {
// Write the key to the host and read the length of the preimage.
let length = self.write_key(key)?;

debug!("Reading data from preimage oracle. Key {}", key);

// Ensure the buffer is the correct size.
if buf.len() != length {
bail!("Buffer size {} does not match preimage size {}", buf.len(), length);
}

self.pipe_handle.read_exact(buf)?;

debug!("Successfully read data from preimage oracle. Key: {}", key);

Ok(())
}
}
Expand Down

0 comments on commit 8f3d29c

Please sign in to comment.