foundry_evm/inspectors/
chisel_state.rs

1use alloy_primitives::U256;
2use revm::{
3    interpreter::{InstructionResult, Interpreter},
4    Database, EvmContext, Inspector,
5};
6
7/// An inspector for Chisel
8#[derive(Clone, Debug, Default)]
9pub struct ChiselState {
10    /// The PC of the final instruction
11    pub final_pc: usize,
12    /// The final state of the REPL contract call
13    pub state: Option<(Vec<U256>, Vec<u8>, InstructionResult)>,
14}
15
16impl ChiselState {
17    /// Create a new Chisel state inspector.
18    #[inline]
19    pub fn new(final_pc: usize) -> Self {
20        Self { final_pc, state: None }
21    }
22}
23
24impl<DB: Database> Inspector<DB> for ChiselState {
25    #[cold]
26    fn step_end(&mut self, interp: &mut Interpreter, _context: &mut EvmContext<DB>) {
27        // If we are at the final pc of the REPL contract execution, set the state.
28        // Subtraction can't overflow because `pc` is always at least 1 in `step_end`.
29        if self.final_pc == interp.program_counter() - 1 {
30            self.state = Some((
31                interp.stack.data().clone(),
32                interp.shared_memory.context_memory().to_vec(),
33                interp.instruction_result,
34            ))
35        }
36    }
37}