Skip to content

Commit

Permalink
Fix an issue in memory gas panic
Browse files Browse the repository at this point in the history
  • Loading branch information
sorpaas committed Nov 23, 2023
1 parent 9e63796 commit f9339f9
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions src/standard/gasometer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,30 @@ impl<'config> Gasometer<'config> {

/// Record an explicit cost.
pub fn record_cost(&mut self, cost: u64) -> Result<(), ExitError> {
let all_gas_cost = self.total_used_gas() + cost;
if self.gas_limit < all_gas_cost {
let all_gas_cost = self.total_used_gas().checked_add(cost);
if let Some(all_gas_cost) = all_gas_cost {
if self.gas_limit < all_gas_cost {
Err(ExitException::OutOfGas.into())
} else {
self.used_gas += cost;
Ok(())
}
} else {
Err(ExitException::OutOfGas.into())
}
}

pub fn set_memory_gas(&mut self, memory_cost: u64) -> Result<(), ExitError> {
let all_gas_cost = self.used_gas.checked_add(memory_cost);
if let Some(all_gas_cost) = all_gas_cost {
if self.gas_limit < all_gas_cost {
Err(ExitException::OutOfGas.into())
} else {
self.memory_gas = memory_cost;
Ok(())
}
} else {
self.used_gas += cost;
Ok(())
Err(ExitException::OutOfGas.into())
}
}

Expand Down Expand Up @@ -235,7 +253,7 @@ impl<'config, S: AsRef<RuntimeState>, H: RuntimeBackend> GasometerT<S, H> for Ga
if let Some(memory_gas) = memory_gas {
let memory_cost = memory_gas.cost()?;
if let Some(memory_cost) = memory_cost {
gasometer.memory_gas = max(gasometer.memory_gas, memory_cost);
gasometer.set_memory_gas(max(gasometer.memory_gas, memory_cost))?;
}
}

Expand Down

0 comments on commit f9339f9

Please sign in to comment.