Skip to content

Commit

Permalink
Merge pull request #459 from fuzzland/fix-cheatcode-unitests
Browse files Browse the repository at this point in the history
Fix cheatcode unitests
  • Loading branch information
jacob-chia authored Apr 3, 2024
2 parents 450684d + 159db5a commit 2e65356
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 16 deletions.
28 changes: 23 additions & 5 deletions src/evm/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -822,8 +822,7 @@ where
res: (InstructionResult, Gas, Bytes),
) -> (InstructionResult, Gas, Bytes) {
// Check assert result
let res = self.check_assert_result(res);
if res.0 == Revert {
if let Some(res) = self.check_assert_result(&res) {
return res;
}

Expand All @@ -841,16 +840,25 @@ where
self.check_expected_calls(res)
}

fn check_assert_result(&mut self, res: (InstructionResult, Gas, Bytes)) -> (InstructionResult, Gas, Bytes) {
fn check_assert_result(
&mut self,
res: &(InstructionResult, Gas, Bytes),
) -> Option<(InstructionResult, Gas, Bytes)> {
debug!("check_assert_result: {:?}, assert_msg: {:?}", res, self.assert_msg);
if let Some(ref msg) = self.assert_msg {
return (InstructionResult::Revert, res.1, msg.abi_encode().into());
return Some((InstructionResult::Revert, res.1, msg.abi_encode().into()));
}

res
None
}

/// Check expected reverts
fn check_expected_revert(&mut self, res: (InstructionResult, Gas, Bytes)) -> (InstructionResult, Gas, Bytes) {
debug!(
"check_expected_revert: {:?}, depth: {}, expected_revert: {:?}",
res, self.call_depth, self.expected_revert
);

// Check if we should check for reverts
if self.expected_revert.is_none() {
return res;
Expand Down Expand Up @@ -901,6 +909,11 @@ where
call: &CallInputs,
res: (InstructionResult, Gas, Bytes),
) -> (InstructionResult, Gas, Bytes) {
debug!(
"check_expected_emits: {:?}, depth: {}, expected_emits: {:?}",
res, self.call_depth, self.expected_emits
);

let should_check_emits = self
.expected_emits
.iter()
Expand All @@ -927,6 +940,11 @@ where

/// Check expected calls
fn check_expected_calls(&mut self, res: (InstructionResult, Gas, Bytes)) -> (InstructionResult, Gas, Bytes) {
debug!(
"check_expected_calls: {:?}, depth: {}, expected_calls: {:?}",
res, self.call_depth, self.expected_calls
);

// Only check expected calls at the root call
if self.call_depth > 0 {
return res;
Expand Down
3 changes: 3 additions & 0 deletions src/evm/middlewares/cheatcode/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -655,12 +655,15 @@ mod tests {
vm::{EVMExecutor, EVMState},
},
generic_vm::vm_executor::GenericVM,
logger,
state::FuzzState,
state_input::StagedVMState,
};

#[test]
fn test_foundry_contract() {
logger::init_test();

let mut state: EVMFuzzState = FuzzState::new(0);

// Reverter.sol: tests/presets/cheatcode/Reverter.sol
Expand Down
24 changes: 24 additions & 0 deletions src/logger.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use tracing::Level;
use tracing_subscriber::FmtSubscriber;

pub fn init() {
let level = if cfg!(debug_assertions) {
Level::DEBUG
} else {
Level::INFO
};
init_logger(level, false);
}

pub fn init_test() {
init_logger(Level::DEBUG, true);
}

fn init_logger(level: Level, with_target: bool) {
let subscriber_builder = FmtSubscriber::builder()
.compact()
.with_target(with_target)
.without_time();
let subscriber = subscriber_builder.with_max_level(level).finish();
tracing::subscriber::set_global_default(subscriber).expect("failed to initialize logger");
}
13 changes: 2 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub mod fuzzers;
pub mod generic_vm;
pub mod indexed_corpus;
pub mod input;
pub mod logger;
pub mod minimizer;
pub mod mutation_utils;
pub mod oracle;
Expand All @@ -29,8 +30,6 @@ pub mod r#move;

use clap::{Parser, Subcommand};
use evm::{evm_main, EvmArgs};
use tracing::Level;
use tracing_subscriber::FmtSubscriber;

#[cfg(feature = "sui_support")]
use crate::r#move::{move_main, MoveArgs};
Expand Down Expand Up @@ -62,15 +61,7 @@ enum Commands {

fn main() {
init_sentry();

// initialize logger
let subscriber_builder = FmtSubscriber::builder().compact().with_target(false).without_time();
#[cfg(debug_assertions)]
let subscriber = subscriber_builder.with_max_level(Level::DEBUG).finish();
#[cfg(not(debug_assertions))]
let subscriber = subscriber_builder.with_max_level(Level::INFO).finish();

tracing::subscriber::set_global_default(subscriber).expect("failed to initialize logger");
logger::init();

let args = Cli::parse();
match args.command {
Expand Down

0 comments on commit 2e65356

Please sign in to comment.