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