Skip to content

Commit

Permalink
more lightweight second gas limit
Browse files Browse the repository at this point in the history
  • Loading branch information
joonazan committed Sep 30, 2024
1 parent a233d44 commit d3af2aa
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion crates/vm2/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl<T, W> State<T, W> {
impl<T: Tracer, W> State<T, W> {
/// Returns the total unspent gas in the VM, including stipends.
pub(crate) fn total_unspent_gas(&self) -> u32 {
self.current_frame.gas
self.current_frame.contained_gas()
+ self
.previous_frames
.iter()
Expand Down
24 changes: 17 additions & 7 deletions crates/vm2/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,16 @@ impl<T: Tracer, W: World<T>> VirtualMachine<T, W> {
/// Needed to support account validation gas limit.
/// We cannot simply reduce the available gas, as contracts might behave differently
/// depending on remaining gas.
pub fn resume_with_additional_gas_limit(
pub fn run_with_additional_gas_limit(
&mut self,
world: &mut W,
tracer: &mut T,
gas_limit: u32,
) -> Option<(u32, ExecutionEnd)> {
let minimum_gas = self.state.total_unspent_gas().saturating_sub(gas_limit);
let starting_gas = self.state.total_unspent_gas();
// Going below this indicates that we may have reached the gas limit.
// But it may also be that a call that left some of the gas in the previous frame.
let mut minimum_gas = self.state.current_frame.gas.saturating_sub(gas_limit);

let end = unsafe {
loop {
Expand All @@ -113,15 +116,22 @@ impl<T: Tracer, W: World<T>> VirtualMachine<T, W> {
break end;
}

if self.state.total_unspent_gas() < minimum_gas {
return None;
if self.state.current_frame.gas < minimum_gas {
let spent = starting_gas.saturating_sub(self.state.total_unspent_gas());
if spent > gas_limit {
return None;
}
minimum_gas = self
.state
.current_frame
.gas
.saturating_sub(gas_limit - spent);
}
}
};

self.state
.total_unspent_gas()
.checked_sub(minimum_gas)
starting_gas
.checked_sub(self.state.total_unspent_gas())
.map(|left| (left, end))
}

Expand Down

0 comments on commit d3af2aa

Please sign in to comment.