Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: more lightweight second gas limit #76

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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