From abfb9501c2642ca3eb8180bc315dddd1846ecae6 Mon Sep 17 00:00:00 2001 From: Andreas Bigger Date: Sat, 23 Sep 2023 16:10:03 -0400 Subject: [PATCH 1/2] Introduce tracing macros to the preimage crate. --- crates/preimage/Cargo.toml | 4 +-- crates/preimage/src/hints.rs | 3 +++ crates/preimage/src/lib.rs | 2 ++ crates/preimage/src/traces.rs | 49 +++++++++++++++++++++++++++++++++++ 4 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 crates/preimage/src/traces.rs diff --git a/crates/preimage/Cargo.toml b/crates/preimage/Cargo.toml index 870d6cd..f2516e1 100644 --- a/crates/preimage/Cargo.toml +++ b/crates/preimage/Cargo.toml @@ -15,10 +15,10 @@ tokio = { version = "1.32.0", features = ["full"] } # misc anyhow = "1.0.75" -tracing = "0.1.37" +tracing = { version = "0.1.37", optional = true } [dev-dependencies] rand = "0.8.5" [features] -tracing = [] +tracing = ["dep:tracing"] diff --git a/crates/preimage/src/hints.rs b/crates/preimage/src/hints.rs index ceee05f..f2e3d79 100644 --- a/crates/preimage/src/hints.rs +++ b/crates/preimage/src/hints.rs @@ -27,10 +27,12 @@ impl Hinter for HintWriter { hint_bytes[0..4].copy_from_slice((hint.len() as u32).to_be_bytes().as_ref()); hint_bytes[4..].copy_from_slice(hint); + crate::debug!("Sending hint: {:?}", hint_bytes); self.tx.send(hint_bytes)?; let n = self.rx.recv()?; if n.len() != 1 { + crate::error!("Failed to read hint ack, received response: {:?}", n); anyhow::bail!( "Failed to read invalid pre-image hint ack, received response: {:?}", n @@ -74,6 +76,7 @@ impl HintReader { if let Err(e) = router(&payload) { // Write back on error to unblock the hint writer. self.tx.send(vec![0])?; + crate::error!("Failed to handle hint: {:?}", e); anyhow::bail!("Failed to handle hint: {:?}", e); } diff --git a/crates/preimage/src/lib.rs b/crates/preimage/src/lib.rs index 8714f5e..a9cee6c 100644 --- a/crates/preimage/src/lib.rs +++ b/crates/preimage/src/lib.rs @@ -1,6 +1,8 @@ #![doc = include_str!("../README.md")] #![allow(dead_code, unused_variables)] +pub(crate) mod traces; + mod oracle; pub use oracle::{OracleClient, OracleServer}; diff --git a/crates/preimage/src/traces.rs b/crates/preimage/src/traces.rs new file mode 100644 index 0000000..d161b5b --- /dev/null +++ b/crates/preimage/src/traces.rs @@ -0,0 +1,49 @@ +/// Performs a tracing debug if the `tracing` feature is enabled. +#[macro_export] +macro_rules! debug { + ($($arg:tt)*) => { + #[cfg(feature = "tracing")] + tracing::debug!($($arg)*); + }; +} +pub use debug; + +/// Performs a tracing info if the `tracing` feature is enabled. +#[macro_export] +macro_rules! info { + ($($arg:tt)*) => { + #[cfg(feature = "tracing")] + tracing::info!($($arg)*); + }; +} +pub use info; + +/// Performs a tracing error if the `tracing` feature is enabled. +#[macro_export] +macro_rules! error { + ($($arg:tt)*) => { + #[cfg(feature = "tracing")] + tracing::error!($($arg)*); + }; +} +pub use error; + +/// Performs a tracing warn if the `tracing` feature is enabled. +#[macro_export] +macro_rules! warn { + ($($arg:tt)*) => { + #[cfg(feature = "tracing")] + tracing::warn!($($arg)*); + }; +} +pub use crate::warn; + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_debug() { + debug!("test"); + } +} From e3961c7f6ca112ec61a5541f378d2585a52f3f5d Mon Sep 17 00:00:00 2001 From: Andreas Bigger Date: Sat, 23 Sep 2023 18:21:43 -0400 Subject: [PATCH 2/2] Add traces to mipsevm --- crates/mipsevm/Cargo.toml | 4 +-- crates/mipsevm/src/evm.rs | 16 +++--------- crates/mipsevm/src/lib.rs | 2 ++ crates/mipsevm/src/traces.rs | 49 +++++++++++++++++++++++++++++++++++ crates/mipsevm/src/witness.rs | 13 +++------- 5 files changed, 59 insertions(+), 25 deletions(-) create mode 100644 crates/mipsevm/src/traces.rs diff --git a/crates/mipsevm/Cargo.toml b/crates/mipsevm/Cargo.toml index 3416d12..683d349 100644 --- a/crates/mipsevm/Cargo.toml +++ b/crates/mipsevm/Cargo.toml @@ -18,14 +18,14 @@ preimage-oracle = { path = "../preimage" } # misc anyhow = "1.0.75" -tracing = "0.1.37" +tracing = { version = "0.1.37", optional = true } [dev-dependencies] rand = "0.8.5" criterion = { version = "0.5.1", features = ["html_reports"] } [features] -tracing = [] +tracing = ["dep:tracing"] [[bench]] name = "memory" diff --git a/crates/mipsevm/src/evm.rs b/crates/mipsevm/src/evm.rs index 1779b84..394632a 100644 --- a/crates/mipsevm/src/evm.rs +++ b/crates/mipsevm/src/evm.rs @@ -106,8 +106,7 @@ impl MipsEVM> { /// execution. pub fn step(&mut self, witness: StepWitness) -> Result { if witness.has_preimage() { - #[cfg(feature = "tracing")] - tracing::info!( + crate::debug!( target: "mipsevm::evm", "Reading preimage key {:x} at offset {}", witness.preimage_key, @@ -129,11 +128,7 @@ impl MipsEVM> { })?; } - #[cfg(feature = "tracing")] - tracing::debug!( - target: "mipsevm::evm", - "Performing EVM step", - ); + crate::debug!(target: "mipsevm::evm", "Performing EVM step"); let step_input = witness.encode_step_input(); self.fill_tx_env(TransactTo::Call(MIPS_ADDR.into()), step_input.0); @@ -151,12 +146,7 @@ impl MipsEVM> { { let output = B256::from_slice(&output); - #[cfg(feature = "tracing")] - tracing::debug!( - target: "mipsevm::evm", - "EVM step successful with resulting post-state hash: {:x}", - output, - ); + crate::debug!(target: "mipsevm::evm", "EVM step successful with resulting post-state hash: {:x}", output); if logs.len() != 1 { anyhow::bail!("Expected 1 log, got {}", logs.len()); diff --git a/crates/mipsevm/src/lib.rs b/crates/mipsevm/src/lib.rs index afb30d9..0a46a4d 100644 --- a/crates/mipsevm/src/lib.rs +++ b/crates/mipsevm/src/lib.rs @@ -2,6 +2,8 @@ #![feature(generic_const_exprs)] #![allow(incomplete_features, dead_code)] +pub(crate) mod traces; + mod memory; pub use self::memory::Memory; diff --git a/crates/mipsevm/src/traces.rs b/crates/mipsevm/src/traces.rs new file mode 100644 index 0000000..d161b5b --- /dev/null +++ b/crates/mipsevm/src/traces.rs @@ -0,0 +1,49 @@ +/// Performs a tracing debug if the `tracing` feature is enabled. +#[macro_export] +macro_rules! debug { + ($($arg:tt)*) => { + #[cfg(feature = "tracing")] + tracing::debug!($($arg)*); + }; +} +pub use debug; + +/// Performs a tracing info if the `tracing` feature is enabled. +#[macro_export] +macro_rules! info { + ($($arg:tt)*) => { + #[cfg(feature = "tracing")] + tracing::info!($($arg)*); + }; +} +pub use info; + +/// Performs a tracing error if the `tracing` feature is enabled. +#[macro_export] +macro_rules! error { + ($($arg:tt)*) => { + #[cfg(feature = "tracing")] + tracing::error!($($arg)*); + }; +} +pub use error; + +/// Performs a tracing warn if the `tracing` feature is enabled. +#[macro_export] +macro_rules! warn { + ($($arg:tt)*) => { + #[cfg(feature = "tracing")] + tracing::warn!($($arg)*); + }; +} +pub use crate::warn; + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_debug() { + debug!("test"); + } +} diff --git a/crates/mipsevm/src/witness.rs b/crates/mipsevm/src/witness.rs index be4d687..b3a80fa 100644 --- a/crates/mipsevm/src/witness.rs +++ b/crates/mipsevm/src/witness.rs @@ -71,25 +71,18 @@ impl StepWitness { /// - `None` if the [StepWitness] does not have a preimage request. pub fn encode_preimage_oracle_input(&self) -> Option { if self.preimage_key == B256::ZERO { - #[cfg(feature = "tracing")] - tracing::warn!("Cannot encode preimage oracle input without preimage"); + crate::error!(target: "mipsevm::step_witness", "Cannot encode preimage oracle input without preimage key"); return None; } match KeyType::from(self.preimage_key[0]) { KeyType::_Illegal => { - #[cfg(feature = "tracing")] - tracing::error!("Illegal key type"); + crate::error!(target: "mipsevm::step_witness", "Illegal key type"); None } KeyType::Local => { if self.preimage_value.len() > 32 + 8 { - #[cfg(feature = "tracing")] - tracing::error!( - target: "mipsevm::step_witness", - "Local preimage value exceeds maximum size of 32 bytes with key 0x{:x}", - self.preimage_key - ); + crate::error!(target: "mipsevm::step_witness", "Local preimage value exceeds maximum size of 32 bytes with key 0x{:x}", self.preimage_key); return None; }