1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
use crate::StateInterface;

macro_rules! forall_simple_opcodes {
    ($m:ident) => {
        $m!(Nop);
        $m!(Add);
        $m!(Sub);
        $m!(And);
        $m!(Or);
        $m!(Xor);
        $m!(ShiftLeft);
        $m!(ShiftRight);
        $m!(RotateLeft);
        $m!(RotateRight);
        $m!(Mul);
        $m!(Div);
        $m!(NearCall);
        $m!(Jump);
        $m!(Event);
        $m!(L2ToL1Message);
        $m!(Decommit);
        $m!(This);
        $m!(Caller);
        $m!(CodeAddress);
        $m!(ErgsLeft);
        $m!(SP);
        $m!(ContextMeta);
        $m!(ContextU128);
        $m!(SetContextU128);
        $m!(IncrementTxNumber);
        $m!(AuxMutating0);
        $m!(PrecompileCall);
        $m!(HeapRead);
        $m!(HeapWrite);
        $m!(AuxHeapRead);
        $m!(AuxHeapWrite);
        $m!(PointerRead);
        $m!(PointerAdd);
        $m!(PointerSub);
        $m!(PointerPack);
        $m!(PointerShrink);
        $m!(StorageRead);
        $m!(StorageWrite);
        $m!(TransientStorageRead);
        $m!(TransientStorageWrite);
    };
}

macro_rules! pub_struct {
    ($x:ident) => {
        #[doc = concat!("`", stringify!($x), "` opcode.")]
        #[derive(Debug)]
        pub struct $x;
    };
}

/// EraVM opcodes.
pub mod opcodes {
    use std::marker::PhantomData;

    use super::{CallingMode, ReturnType};

    forall_simple_opcodes!(pub_struct);

    /// `FarCall` group of opcodes distinguished by the calling mode (normal, delegate, or mimic).
    #[derive(Debug)]
    pub struct FarCall<M: TypeLevelCallingMode>(PhantomData<M>);

    /// `Ret` group of opcodes distinguished by the return type (normal, panic, or revert).
    #[derive(Debug)]
    pub struct Ret<T: TypeLevelReturnType>(PhantomData<T>);

    /// Normal [`Ret`]urn mode / [`FarCall`] mode.
    #[derive(Debug)]
    pub struct Normal;

    /// Delegate [`FarCall`] mode.
    #[derive(Debug)]
    pub struct Delegate;

    /// Mimic [`FarCall`] mode.
    #[derive(Debug)]
    pub struct Mimic;

    /// Revert [`Ret`]urn mode.
    #[derive(Debug)]
    pub struct Revert;

    /// Panic [`Ret`]urn mode.
    #[derive(Debug)]
    pub struct Panic;

    /// Calling mode for the [`FarCall`] opcodes.
    pub trait TypeLevelCallingMode {
        /// Constant corresponding to this mode allowing to easily `match` it.
        const VALUE: CallingMode;
    }

    impl TypeLevelCallingMode for Normal {
        const VALUE: CallingMode = CallingMode::Normal;
    }

    impl TypeLevelCallingMode for Delegate {
        const VALUE: CallingMode = CallingMode::Delegate;
    }

    impl TypeLevelCallingMode for Mimic {
        const VALUE: CallingMode = CallingMode::Mimic;
    }

    /// Return type for the [`Ret`] opcodes.
    pub trait TypeLevelReturnType {
        /// Constant corresponding to this return type allowing to easily `match` it.
        const VALUE: ReturnType;
    }

    impl TypeLevelReturnType for Normal {
        const VALUE: ReturnType = ReturnType::Normal;
    }

    impl TypeLevelReturnType for Revert {
        const VALUE: ReturnType = ReturnType::Revert;
    }

    impl TypeLevelReturnType for Panic {
        const VALUE: ReturnType = ReturnType::Panic;
    }
}

/// All supported EraVM opcodes in a single enumeration.
#[allow(missing_docs)]
#[derive(PartialEq, Eq, Debug, Copy, Clone, Hash)]
pub enum Opcode {
    Nop,
    Add,
    Sub,
    And,
    Or,
    Xor,
    ShiftLeft,
    ShiftRight,
    RotateLeft,
    RotateRight,
    Mul,
    Div,
    NearCall,
    FarCall(CallingMode),
    Ret(ReturnType),
    Jump,
    Event,
    L2ToL1Message,
    Decommit,
    This,
    Caller,
    CodeAddress,
    ErgsLeft,
    SP,
    ContextMeta,
    ContextU128,
    SetContextU128,
    IncrementTxNumber,
    AuxMutating0,
    PrecompileCall,
    HeapRead,
    HeapWrite,
    AuxHeapRead,
    AuxHeapWrite,
    PointerRead,
    PointerAdd,
    PointerSub,
    PointerPack,
    PointerShrink,
    StorageRead,
    StorageWrite,
    TransientStorageRead,
    TransientStorageWrite,
}

/// All supported calling modes for [`FarCall`](opcodes::FarCall) opcode.
#[derive(PartialEq, Eq, Debug, Copy, Clone, Hash)]
pub enum CallingMode {
    /// Normal calling mode.
    Normal,
    /// Delegate calling mode (similar to `delegatecall` in EVM).
    Delegate,
    /// Mimic calling mode (can only be used by system contracts; allows to emulate `eth_call` semantics while retaining the bootloader).
    Mimic,
}

/// All supported return types for the [`Ret`](opcodes::Ret) opcode.
#[derive(PartialEq, Eq, Debug, Copy, Clone, Hash)]
pub enum ReturnType {
    /// Normal return.
    Normal,
    /// Revert (e.g., a result of a Solidity `revert`).
    Revert,
    /// Panic, i.e. a non-revert abnormal control flow termination (e.g., out of gas).
    Panic,
}

impl ReturnType {
    /// Checks if this return type is [normal](Self::Normal).
    pub fn is_failure(&self) -> bool {
        *self != ReturnType::Normal
    }
}

/// Trait mapping opcodes as types to the corresponding variants of the [`Opcode`] enum.
pub trait OpcodeType {
    /// `Opcode` variant corresponding to this opcode type.
    const VALUE: Opcode;
}

macro_rules! impl_opcode {
    ($x:ident) => {
        impl OpcodeType for opcodes::$x {
            const VALUE: Opcode = Opcode::$x;
        }
    };
}

forall_simple_opcodes!(impl_opcode);

impl<M: opcodes::TypeLevelCallingMode> OpcodeType for opcodes::FarCall<M> {
    const VALUE: Opcode = Opcode::FarCall(M::VALUE);
}

impl<T: opcodes::TypeLevelReturnType> OpcodeType for opcodes::Ret<T> {
    const VALUE: Opcode = Opcode::Ret(T::VALUE);
}

/// EraVM instruction tracer.
///
/// [`Self::before_instruction()`] is called just before the actual instruction is executed.
/// If the instruction is skipped, `before_instruction` will be called with [`Nop`](opcodes::Nop).
/// [`Self::after_instruction()`] is called once the instruction is executed and the program
/// counter has advanced.
///
/// # Examples
///
/// Here `FarCallCounter` counts the number of far calls.
///
/// ```
/// # use zksync_vm2_interface::{Tracer, StateInterface, OpcodeType, Opcode};
/// struct FarCallCounter(usize);
///
/// impl Tracer for FarCallCounter {
///     fn before_instruction<OP: OpcodeType, S: StateInterface>(&mut self, state: &mut S) {
///         match OP::VALUE {
///             Opcode::FarCall(_) => self.0 += 1,
///             _ => {}
///         }
///     }
/// }
/// ```
pub trait Tracer {
    /// Executes logic before an instruction handler.
    ///
    /// The default implementation does nothing.
    fn before_instruction<OP: OpcodeType, S: StateInterface>(&mut self, _state: &mut S) {}
    /// Executes logic after an instruction handler.
    ///
    /// The default implementation does nothing.
    fn after_instruction<OP: OpcodeType, S: StateInterface>(&mut self, _state: &mut S) {}

    /// Provides cycle statistics for "complex" instructions from the prover perspective (mostly precompile calls).
    ///
    /// The default implementation does nothing.
    fn on_extra_prover_cycles(&mut self, _stats: CycleStats) {}
}

/// Cycle statistics emitted by the VM and supplied to [`Tracer::on_extra_prover_cycles()`].
#[derive(Debug, Clone, Copy)]
pub enum CycleStats {
    /// Call to the `keccak256` precompile with the specified number of hash cycles.
    Keccak256(u32),
    /// Call to the `sha256` precompile with the specified number of hash cycles.
    Sha256(u32),
    /// Call to the `ecrecover` precompile with the specified number of hash cycles.
    EcRecover(u32),
    /// Call to the `secp256r1_verify` precompile with the specified number of hash cycles.
    Secp256r1Verify(u32),
    /// Decommitting an opcode.
    Decommit(u32),
    /// Reading a slot from the VM storage.
    StorageRead,
    /// Writing a slot to the VM storage.
    StorageWrite,
}

/// No-op tracer implementation.
impl Tracer for () {}

// Multiple tracers can be combined by building a linked list out of tuples.
impl<A: Tracer, B: Tracer> Tracer for (A, B) {
    fn before_instruction<OP: OpcodeType, S: StateInterface>(&mut self, state: &mut S) {
        self.0.before_instruction::<OP, S>(state);
        self.1.before_instruction::<OP, S>(state);
    }

    fn after_instruction<OP: OpcodeType, S: StateInterface>(&mut self, state: &mut S) {
        self.0.after_instruction::<OP, S>(state);
        self.1.after_instruction::<OP, S>(state);
    }

    fn on_extra_prover_cycles(&mut self, stats: CycleStats) {
        self.0.on_extra_prover_cycles(stats);
        self.1.on_extra_prover_cycles(stats);
    }
}

#[cfg(test)]
mod tests {
    use super::{CallingMode, OpcodeType};
    use crate::{opcodes, DummyState, Tracer};

    struct FarCallCounter(usize);

    impl Tracer for FarCallCounter {
        fn before_instruction<OP: OpcodeType, S: crate::StateInterface>(&mut self, _: &mut S) {
            if let super::Opcode::FarCall(CallingMode::Normal) = OP::VALUE {
                self.0 += 1;
            }
        }
    }

    #[test]
    fn test_tracer() {
        let mut tracer = FarCallCounter(0);

        tracer.before_instruction::<opcodes::Nop, _>(&mut DummyState);
        assert_eq!(tracer.0, 0);

        tracer.before_instruction::<opcodes::FarCall<opcodes::Normal>, _>(&mut DummyState);
        assert_eq!(tracer.0, 1);

        tracer.before_instruction::<opcodes::FarCall<opcodes::Mimic>, _>(&mut DummyState);
        assert_eq!(tracer.0, 1);
    }

    #[test]
    fn test_aggregate_tracer() {
        let mut tracer = (FarCallCounter(0), (FarCallCounter(0), FarCallCounter(0)));

        tracer.before_instruction::<opcodes::Nop, _>(&mut DummyState);
        assert_eq!(tracer.0 .0, 0);
        assert_eq!(tracer.1 .0 .0, 0);
        assert_eq!(tracer.1 .1 .0, 0);

        tracer.before_instruction::<opcodes::FarCall<opcodes::Normal>, _>(&mut DummyState);
        assert_eq!(tracer.0 .0, 1);
        assert_eq!(tracer.1 .0 .0, 1);
        assert_eq!(tracer.1 .1 .0, 1);
    }
}