Skip to content

Commit

Permalink
Add traces to mipsevm
Browse files Browse the repository at this point in the history
  • Loading branch information
refcell committed Sep 23, 2023
1 parent abfb950 commit e3961c7
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 25 deletions.
4 changes: 2 additions & 2 deletions crates/mipsevm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
16 changes: 3 additions & 13 deletions crates/mipsevm/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ impl MipsEVM<CacheDB<EmptyDB>> {
/// execution.
pub fn step(&mut self, witness: StepWitness) -> Result<B256> {
if witness.has_preimage() {
#[cfg(feature = "tracing")]
tracing::info!(
crate::debug!(
target: "mipsevm::evm",
"Reading preimage key {:x} at offset {}",
witness.preimage_key,
Expand All @@ -129,11 +128,7 @@ impl MipsEVM<CacheDB<EmptyDB>> {
})?;
}

#[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);
Expand All @@ -151,12 +146,7 @@ impl MipsEVM<CacheDB<EmptyDB>> {
{
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());
Expand Down
2 changes: 2 additions & 0 deletions crates/mipsevm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#![feature(generic_const_exprs)]
#![allow(incomplete_features, dead_code)]

pub(crate) mod traces;

mod memory;
pub use self::memory::Memory;

Expand Down
49 changes: 49 additions & 0 deletions crates/mipsevm/src/traces.rs
Original file line number Diff line number Diff line change
@@ -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");
}
}
13 changes: 3 additions & 10 deletions crates/mipsevm/src/witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,25 +71,18 @@ impl StepWitness {
/// - `None` if the [StepWitness] does not have a preimage request.
pub fn encode_preimage_oracle_input(&self) -> Option<Bytes> {
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;
}

Expand Down

0 comments on commit e3961c7

Please sign in to comment.