Skip to content

Commit bf63339

Browse files
authored
fix!: Remove EVM emulator gas stipend (#82)
# What ❔ Removes the EVM emulator gas stipend and replaces it with a memory stipend. ## Why ❔ Aligns with the changes in the legacy VM.
1 parent 70a6e30 commit bf63339

File tree

11 files changed

+17
-197
lines changed

11 files changed

+17
-197
lines changed

crates/vm2-interface/src/state_interface.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,6 @@ pub trait CallframeInterface {
115115
fn gas(&self) -> u32;
116116
/// Sets the remaining amount of gas.
117117
fn set_gas(&mut self, new_gas: u32);
118-
/// Additional gas provided for the duration of this callframe.
119-
fn stipend(&self) -> u32;
120118

121119
/// Returns the context value for this call. This context is accessible via [`ContextU128`](crate::opcodes::ContextU128) opcode.
122120
fn context_u128(&self) -> u128;
@@ -379,10 +377,6 @@ pub(crate) mod testonly {
379377
unimplemented!()
380378
}
381379

382-
fn stipend(&self) -> u32 {
383-
unimplemented!()
384-
}
385-
386380
fn context_u128(&self) -> u128 {
387381
unimplemented!()
388382
}

crates/vm2/src/callframe.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ use crate::{
1313
Instruction, World,
1414
};
1515

16+
// FIXME: use `zkevm_opcode_defs::system_params` once it's released
17+
const NEW_EVM_FRAME_MEMORY_STIPEND: u32 = 56 << 10;
18+
1619
#[derive(Debug)]
1720
pub(crate) struct Callframe<T, W> {
1821
pub(crate) address: H160,
@@ -25,7 +28,6 @@ pub(crate) struct Callframe<T, W> {
2528
pub(crate) stack: Box<Stack>,
2629
pub(crate) sp: u16,
2730
pub(crate) gas: u32,
28-
pub(crate) stipend: u32,
2931
pub(crate) near_calls: Vec<NearCallFrame>,
3032
pub(crate) pc: *const Instruction<T, W>,
3133
pub(crate) program: Program<T, W>,
@@ -68,15 +70,17 @@ impl<T, W> Callframe<T, W> {
6870
aux_heap: HeapId,
6971
calldata_heap: HeapId,
7072
gas: u32,
71-
stipend: u32,
7273
exception_handler: u16,
7374
context_u128: u128,
7475
is_static: bool,
76+
is_evm_interpreter: bool,
7577
world_before_this_frame: Snapshot,
7678
) -> Self {
7779
let is_kernel = is_kernel(address);
7880
let heap_size = if is_kernel {
7981
NEW_KERNEL_FRAME_MEMORY_STIPEND
82+
} else if is_evm_interpreter {
83+
NEW_EVM_FRAME_MEMORY_STIPEND
8084
} else {
8185
NEW_FRAME_MEMORY_STIPEND
8286
};
@@ -99,7 +103,6 @@ impl<T, W> Callframe<T, W> {
99103
heaps_i_am_keeping_alive: vec![],
100104
sp: 0,
101105
gas,
102-
stipend,
103106
exception_handler,
104107
near_calls: vec![],
105108
world_before_this_frame,
@@ -253,7 +256,6 @@ impl<T, W> Clone for Callframe<T, W> {
253256
stack: self.stack.clone(),
254257
sp: self.sp,
255258
gas: self.gas,
256-
stipend: self.stipend,
257259
near_calls: self.near_calls.clone(),
258260
pc: self.pc,
259261
program: self.program.clone(),
@@ -279,7 +281,6 @@ impl<T, W> PartialEq for Callframe<T, W> {
279281
&& self.stack == other.stack
280282
&& self.sp == other.sp
281283
&& self.gas == other.gas
282-
&& self.stipend == other.stipend
283284
&& self.near_calls == other.near_calls
284285
&& self.pc == other.pc
285286
&& self.program == other.program

crates/vm2/src/instruction_handlers/far_call.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
use primitive_types::U256;
2-
use zkevm_opcode_defs::{
3-
system_params::{EVM_SIMULATOR_STIPEND, MSG_VALUE_SIMULATOR_ADDITIVE_COST},
4-
ADDRESS_MSG_VALUE,
5-
};
2+
use zkevm_opcode_defs::{system_params::MSG_VALUE_SIMULATOR_ADDITIVE_COST, ADDRESS_MSG_VALUE};
63
use zksync_vm2_interface::{
74
opcodes::{FarCall, TypeLevelCallingMode},
85
Tracer,
@@ -62,7 +59,7 @@ where
6259
0
6360
};
6461

65-
let failing_part = (|| {
62+
let fallible_part = (|| {
6663
let decommit_result = vm.world_diff.decommit(
6764
world,
6865
tracer,
@@ -108,25 +105,16 @@ where
108105

109106
// A far call pushes a new frame and returns from it in the next instruction if it panics.
110107
let (calldata, program, is_evm_interpreter) =
111-
failing_part.unwrap_or_else(|| (U256::zero().into(), Program::new_panicking(), false));
112-
113-
let stipend = if is_evm_interpreter {
114-
EVM_SIMULATOR_STIPEND
115-
} else {
116-
0
117-
};
118-
let new_frame_gas = new_frame_gas
119-
.checked_add(stipend)
120-
.expect("stipend must not cause overflow");
108+
fallible_part.unwrap_or_else(|| (U256::zero().into(), Program::new_panicking(), false));
121109

122110
let new_frame_is_static = IS_STATIC || vm.state.current_frame.is_static;
123111
vm.push_frame::<M>(
124112
u256_into_address(destination_address),
125113
program,
126114
new_frame_gas,
127-
stipend,
128115
exception_handler,
129116
new_frame_is_static && !is_evm_interpreter,
117+
is_evm_interpreter,
130118
calldata.memory_page,
131119
vm.world_diff.snapshot(),
132120
);

crates/vm2/src/instruction_handlers/ret.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,7 @@ fn naked_ret<T: Tracer, W: World<T>, RT: TypeLevelReturnType, const TO_LABEL: bo
5555
result
5656
};
5757

58-
let leftover_gas = vm
59-
.state
60-
.current_frame
61-
.gas
62-
.saturating_sub(vm.state.current_frame.stipend);
58+
let leftover_gas = vm.state.current_frame.gas;
6359

6460
let Some(FrameRemnant {
6561
exception_handler,

crates/vm2/src/single_instruction_test/callframe.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use arbitrary::Arbitrary;
22
use primitive_types::H160;
3-
use zkevm_opcode_defs::EVM_SIMULATOR_STIPEND;
43
use zksync_vm2_interface::{HeapId, Tracer};
54

65
use super::stack::{Stack, StackPool};
@@ -40,9 +39,7 @@ impl<'a, T: Tracer, W: World<T>> Arbitrary<'a> for Callframe<T, W> {
4039
is_kernel: is_kernel(address),
4140
stack: Box::new(Stack::new_arbitrary(u, calldata_heap, base_page)?),
4241
sp: u.arbitrary()?,
43-
// It is assumed that it is always possible to add the stipend
44-
gas: u.int_in_range(0..=u32::MAX - EVM_SIMULATOR_STIPEND)?,
45-
stipend: u.arbitrary()?,
42+
gas: u.arbitrary()?,
4643
near_calls: vec![],
4744
pc: program.instruction(0).unwrap(),
4845
program,
@@ -78,7 +75,6 @@ impl<T: Tracer, W: World<T>> Callframe<T, W> {
7875
stack: StackPool {}.get(),
7976
sp: 0,
8077
gas: 0,
81-
stipend: 0,
8278
near_calls: vec![],
8379
pc: std::ptr::null(),
8480
program: Program::for_decommit(),

crates/vm2/src/single_instruction_test/state_to_zk_evm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ fn vm2_frame_to_zk_evm_frames<T, W>(
8686
heap_bound: frame.heap_size,
8787
aux_heap_bound: frame.aux_heap_size,
8888
total_pubdata_spent: PubdataCost(0),
89-
stipend: frame.stipend,
89+
stipend: 0,
9090
};
9191

9292
let mut result = vec![far_frame];

crates/vm2/src/state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl<T, W> State<T, W> {
6363
gas,
6464
0,
6565
0,
66-
0,
66+
false,
6767
false,
6868
world_before_this_frame,
6969
),

crates/vm2/src/tests/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@
33
mod bytecode_behaviour;
44
mod far_call_decommitment;
55
mod panic;
6-
mod stipend;
76
mod trace_failing_far_call;

crates/vm2/src/tests/stipend.rs

Lines changed: 0 additions & 150 deletions
This file was deleted.

crates/vm2/src/tracing.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,6 @@ impl<T: Tracer, W: World<T>> CallframeInterface for CallframeWrapper<'_, T, W> {
194194
self.frame.is_kernel
195195
}
196196

197-
fn stipend(&self) -> u32 {
198-
self.frame.stipend
199-
}
200-
201197
fn context_u128(&self) -> u128 {
202198
self.frame.context_u128
203199
}
@@ -472,9 +468,9 @@ mod test {
472468
H160::from_low_u64_be(1),
473469
program.clone(),
474470
(*counter).into(),
475-
0,
476471
*counter,
477472
false,
473+
false,
478474
HeapId::from_u32_unchecked(5),
479475
vm.world_diff.snapshot(),
480476
);

0 commit comments

Comments
 (0)