Skip to main content

evm/
invoker.rs

1use alloc::vec::Vec;
2use evm_interpreter::{Capture, ExitError, ExitResult, Interpreter};
3
4/// Control for an invoker.
5pub enum InvokerControl<I, S> {
6	/// Pushing the call stack.
7	Enter(I),
8	/// Directly exit, not pushing the call stack.
9	DirectExit(InvokerExit<S>),
10}
11
12/// The exit value of an invoker call, containing the result, substate, and return data.
13pub struct InvokerExit<S> {
14	/// The exit result.
15	pub result: ExitResult,
16	/// The substate, if available.
17	pub substate: Option<S>,
18	/// The return data.
19	pub retval: Vec<u8>,
20}
21
22/// An invoker, responsible for pushing/poping values in the call stack.
23pub trait Invoker<H> {
24	/// State type.
25	type State;
26	/// Interpreter type.
27	type Interpreter: Interpreter<H, State = Self::State>;
28	/// Possible interrupt type that may be returned by the call stack.
29	type Interrupt;
30
31	/// Type for transaction arguments.
32	type TransactArgs;
33	/// The invoke of a top-layer transaction call stack. When finalizing a
34	/// transaction, this invoke is used to figure out the finalization routine.
35	type TransactInvoke;
36	/// The returned value of the transaction.
37	type TransactValue;
38	/// The invoke of a sub-layer call stack. When exiting a call stack, this
39	/// invoke is used to figure out the exit routine.
40	type SubstackInvoke;
41
42	/// Create a new transaction with the given transaction arguments.
43	#[allow(clippy::type_complexity)]
44	fn new_transact(
45		&self,
46		args: Self::TransactArgs,
47		handler: &mut H,
48	) -> Result<
49		(
50			Self::TransactInvoke,
51			InvokerControl<Self::Interpreter, Self::State>,
52		),
53		ExitError,
54	>;
55
56	/// Finalize a transaction.
57	fn finalize_transact(
58		&self,
59		invoke: &Self::TransactInvoke,
60		exit: InvokerExit<Self::State>,
61		handler: &mut H,
62	) -> Result<Self::TransactValue, ExitError>;
63
64	/// Enter a sub-layer call stack.
65	#[allow(clippy::type_complexity)]
66	fn enter_substack(
67		&self,
68		trap: <Self::Interpreter as Interpreter<H>>::Trap,
69		machine: &mut Self::Interpreter,
70		handler: &mut H,
71		depth: usize,
72	) -> Capture<
73		Result<
74			(
75				Self::SubstackInvoke,
76				InvokerControl<Self::Interpreter, Self::State>,
77			),
78			ExitError,
79		>,
80		Self::Interrupt,
81	>;
82
83	/// Exit a sub-layer call stack.
84	fn exit_substack(
85		&self,
86		trap_data: Self::SubstackInvoke,
87		exit: InvokerExit<Self::State>,
88		parent: &mut Self::Interpreter,
89		handler: &mut H,
90	) -> Result<(), ExitError>;
91}