Skip to main content

zksync_vm2/
instruction.rs

1use std::fmt;
2
3use zksync_vm2_interface::ShouldStop;
4
5use crate::{addressing_modes::Arguments, vm::VirtualMachine};
6
7/// Single EraVM instruction (an opcode + [`Arguments`]).
8///
9/// Managing instructions is warranted for low-level tests; prefer using [`Program`](crate::Program)s to decode instructions
10/// from EraVM bytecodes.
11pub struct Instruction<T, W> {
12    pub(crate) handler: Handler<T, W>,
13    pub(crate) arguments: Arguments,
14}
15
16impl<T, W> fmt::Debug for Instruction<T, W> {
17    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
18        formatter
19            .debug_struct("Instruction")
20            .field("arguments", &self.arguments)
21            .finish_non_exhaustive()
22    }
23}
24
25pub(crate) type Handler<T, W> = fn(&mut VirtualMachine<T, W>, &mut W, &mut T) -> ExecutionStatus;
26
27#[derive(Debug)]
28pub(crate) enum ExecutionStatus {
29    Running,
30    Stopped(ExecutionEnd),
31}
32
33impl ExecutionStatus {
34    #[must_use]
35    #[inline(always)]
36    pub(crate) fn merge_tracer(self, should_stop: ShouldStop) -> Self {
37        match (&self, should_stop) {
38            (Self::Running, ShouldStop::Stop) => Self::Stopped(ExecutionEnd::StoppedByTracer),
39            _ => self,
40        }
41    }
42}
43
44impl From<ShouldStop> for ExecutionStatus {
45    fn from(should_stop: ShouldStop) -> Self {
46        match should_stop {
47            ShouldStop::Stop => Self::Stopped(ExecutionEnd::StoppedByTracer),
48            ShouldStop::Continue => Self::Running,
49        }
50    }
51}
52
53/// VM stop reason returned from [`VirtualMachine::run()`].
54#[derive(Debug, PartialEq)]
55pub enum ExecutionEnd {
56    /// The executed program has finished and returned the specified data.
57    ProgramFinished(Vec<u8>),
58    /// The executed program has reverted returning the specified data.
59    Reverted(Vec<u8>),
60    /// The executed program has panicked.
61    Panicked,
62    /// Returned when the bootloader writes to the heap location specified by [`hook_address`](crate::Settings.hook_address).
63    SuspendedOnHook(u32),
64    /// One of the tracers decided it is time to stop the VM.
65    StoppedByTracer,
66}