foundry_evm_coverage/
inspector.rsuse crate::{HitMap, HitMaps};
use alloy_primitives::B256;
use revm::{interpreter::Interpreter, Database, EvmContext, Inspector};
#[derive(Clone, Debug, Default)]
pub struct CoverageCollector {
pub maps: HitMaps,
}
impl<DB: Database> Inspector<DB> for CoverageCollector {
#[inline]
fn initialize_interp(&mut self, interp: &mut Interpreter, _context: &mut EvmContext<DB>) {
self.maps
.entry(get_contract_hash(interp))
.or_insert_with(|| HitMap::new(interp.contract.bytecode.original_bytes()));
}
#[inline]
fn step(&mut self, interp: &mut Interpreter, _context: &mut EvmContext<DB>) {
self.maps
.entry(get_contract_hash(interp))
.and_modify(|map| map.hit(interp.program_counter()));
}
}
fn get_contract_hash(interp: &mut Interpreter) -> B256 {
let mut hash = interp.contract.hash.expect("Contract hash is None");
if hash == B256::ZERO {
hash = interp.contract.bytecode.hash_slow();
}
hash
}