revm_inspector/
inspect.rs

1use context::result::ExecResultAndState;
2use handler::{ExecuteCommitEvm, ExecuteEvm};
3
4/// InspectEvm is a API that allows inspecting the EVM.
5///
6/// It extends the `ExecuteEvm` trait and enabled setting inspector
7///
8pub trait InspectEvm: ExecuteEvm {
9    /// The inspector type used for inspecting EVM execution.
10    type Inspector;
11
12    /// Set the inspector for the EVM.
13    ///
14    /// this function is used to change inspector during execution.
15    /// This function can't change Inspector type, changing inspector type can be done in
16    /// `Evm` with `with_inspector` function.
17    fn set_inspector(&mut self, inspector: Self::Inspector);
18
19    /// Inspect the EVM with the given transaction.
20    fn inspect_one_tx(&mut self, tx: Self::Tx) -> Result<Self::ExecutionResult, Self::Error>;
21
22    /// Inspect the EVM and finalize the state.
23    fn inspect_tx(
24        &mut self,
25        tx: Self::Tx,
26    ) -> Result<ExecResultAndState<Self::ExecutionResult, Self::State>, Self::Error> {
27        let output = self.inspect_one_tx(tx)?;
28        let state = self.finalize();
29        Ok(ExecResultAndState::new(output, state))
30    }
31
32    /// Inspect the EVM with the given inspector and transaction, and finalize the state.
33    fn inspect(
34        &mut self,
35        tx: Self::Tx,
36        inspector: Self::Inspector,
37    ) -> Result<ExecResultAndState<Self::ExecutionResult, Self::State>, Self::Error> {
38        let output = self.inspect_one(tx, inspector)?;
39        let state = self.finalize();
40        Ok(ExecResultAndState::new(output, state))
41    }
42
43    /// Inspect the EVM with the given inspector and transaction.
44    fn inspect_one(
45        &mut self,
46        tx: Self::Tx,
47        inspector: Self::Inspector,
48    ) -> Result<Self::ExecutionResult, Self::Error> {
49        self.set_inspector(inspector);
50        self.inspect_one_tx(tx)
51    }
52}
53
54/// InspectCommitEvm is a API that allows inspecting similar to `InspectEvm` but it has
55/// functions that commit the state diff to the database.
56///
57/// Functions return CommitOutput from [`ExecuteCommitEvm`] trait.
58pub trait InspectCommitEvm: InspectEvm + ExecuteCommitEvm {
59    /// Inspect the EVM with the current inspector and previous transaction by replaying, similar to [`InspectEvm::inspect_tx`]
60    /// and commit the state diff to the database.
61    fn inspect_tx_commit(&mut self, tx: Self::Tx) -> Result<Self::ExecutionResult, Self::Error> {
62        let output = self.inspect_one_tx(tx)?;
63        self.commit_inner();
64        Ok(output)
65    }
66
67    /// Inspect the EVM with the given transaction and inspector similar to [`InspectEvm::inspect`]
68    /// and commit the state diff to the database.
69    fn inspect_commit(
70        &mut self,
71        tx: Self::Tx,
72        inspector: Self::Inspector,
73    ) -> Result<Self::ExecutionResult, Self::Error> {
74        let output = self.inspect_one(tx, inspector)?;
75        self.commit_inner();
76        Ok(output)
77    }
78}