evm_interpreter/machine/
mod.rs

1mod memory;
2mod stack;
3
4use alloc::{rc::Rc, vec::Vec};
5
6pub use self::{memory::Memory, stack::Stack};
7
8/// Core execution layer for EVM.
9pub struct Machine<S> {
10	/// Program data.
11	pub(crate) data: Rc<Vec<u8>>,
12	/// Program code.
13	pub(crate) code: Rc<Vec<u8>>,
14	/// Return value. Note the difference between `retbuf`.
15	/// A `retval` holds what's returned by the current machine, with `RETURN` or `REVERT` opcode.
16	/// A `retbuf` holds the buffer of returned value by sub-calls.
17	pub retval: Vec<u8>,
18	/// Memory.
19	pub memory: Memory,
20	/// Stack.
21	pub stack: Stack,
22	/// Extra state,
23	pub state: S,
24}
25
26impl<S> Machine<S> {
27	/// Create a new machine with given code and data.
28	pub fn new(
29		code: Rc<Vec<u8>>,
30		data: Rc<Vec<u8>>,
31		stack_limit: usize,
32		memory_limit: usize,
33		state: S,
34	) -> Self {
35		Self {
36			data,
37			code,
38			retval: Vec::new(),
39			memory: Memory::new(memory_limit),
40			stack: Stack::new(stack_limit),
41			state,
42		}
43	}
44
45	/// Machine code.
46	pub fn code(&self) -> &[u8] {
47		&self.code
48	}
49
50	/// Whether the machine has empty code.
51	#[must_use]
52	pub fn is_empty(&self) -> bool {
53		self.code.is_empty()
54	}
55}