Skip to content

Commit

Permalink
Always deduct gas.
Browse files Browse the repository at this point in the history
  • Loading branch information
olonho committed Nov 4, 2021
1 parent 8a0e8e8 commit 7d87464
Showing 1 changed file with 38 additions and 28 deletions.
66 changes: 38 additions & 28 deletions runtime/near-vm-logic/src/gas_counter.rs
Expand Up @@ -95,7 +95,6 @@ impl GasCounter {
let promise_gas = use_gas - burn_gas;
let new_promises_gas =
self.promises_gas.checked_add(promise_gas).ok_or(HostError::IntegerOverflow)?;
println!("deduct_gas p={} b={} newp={}", promise_gas, burn_gas, new_promises_gas);
let new_burnt_gas =
self.fast_counter.burnt_gas.checked_add(burn_gas).ok_or(HostError::IntegerOverflow)?;
let new_used_gas =
Expand All @@ -111,24 +110,57 @@ impl GasCounter {
self.promises_gas = new_promises_gas;
Ok(())
} else {
let res = if new_burnt_gas > self.max_gas_burnt {
if new_burnt_gas > self.max_gas_burnt {
self.fast_counter.burnt_gas = self.max_gas_burnt;
self.promises_gas = min(new_promises_gas, self.prepaid_gas);
self.promises_gas = 0;
Err(HostError::GasLimitExceeded.into())
} else if new_used_gas > self.prepaid_gas {
self.promises_gas = 0;
self.fast_counter.burnt_gas = new_burnt_gas;
self.promises_gas = self.prepaid_gas - self.fast_counter.burnt_gas;
Err(HostError::GasExceeded.into())
} else {
unreachable!()
}
}
}

/*
let err = if new_burnt_gas > self.max_gas_burnt {
HostError::GasLimitExceeded.into()
} else if new_used_gas > self.prepaid_gas {
HostError::GasExceeded.into()
} else {
unreachable!()
};
self.promises_gas = min(new_used_gas, self.prepaid_gas) - self.fast_counter.burnt_gas;
let max_burnt_gas = min(self.max_gas_burnt, self.prepaid_gas);
self.burnt_gas = min(new_burnt_gas, max_burnt_gas);
self.used_gas = min(new_used_gas, self.prepaid_gas);
*/

res
pub fn burn_gas(&mut self, value: Gas) -> Result<()> {
let new_burnt_gas =
self.fast_counter.burnt_gas.checked_add(value).ok_or(HostError::IntegerOverflow)?;
if new_burnt_gas <= self.fast_counter.gas_limit {
self.fast_counter.burnt_gas = new_burnt_gas;
Ok(())
} else {
println!("ohh, limit is {}", self.fast_counter.gas_limit);
if new_burnt_gas > self.max_gas_burnt {
self.fast_counter.burnt_gas = self.max_gas_burnt;
Err(HostError::GasLimitExceeded.into())
} else {
self.fast_counter.burnt_gas = new_burnt_gas;
Err(HostError::GasExceeded.into())
}
}
}

pub fn pay_wasm_gas(&mut self, opcodes: u32) -> Result<()> {
let value = Gas::from(opcodes) * self.fast_counter.opcode_cost;
self.burn_gas(value)
}

#[inline]
fn inc_ext_costs_counter(&mut self, cost: ExtCosts, value: u64) {
with_ext_cost_counter(|cc| *cc.entry(cost).or_default() += value)
Expand All @@ -144,28 +176,6 @@ impl GasCounter {
self.profile.add_action_cost(action, value)
}

pub fn pay_wasm_gas(&mut self, opcodes: u32) -> Result<()> {
let value = Gas::from(opcodes) * self.fast_counter.opcode_cost;
self.burn_gas(value)
}

pub fn burn_gas(&mut self, value: Gas) -> Result<()> {
let new_burnt_gas =
self.fast_counter.burnt_gas.checked_add(value).ok_or(HostError::IntegerOverflow)?;
if new_burnt_gas <= self.fast_counter.gas_limit {
self.fast_counter.burnt_gas = new_burnt_gas;
Ok(())
} else {
println!("ohh, limit is {}", self.fast_counter.gas_limit);
self.fast_counter.burnt_gas = self.fast_counter.gas_limit;
if self.fast_counter.gas_limit == self.max_gas_burnt {
Err(HostError::GasLimitExceeded.into())
} else {
Err(HostError::GasExceeded.into())
}
}
}

/// A helper function to pay a multiple of a cost.
pub fn pay_per(&mut self, cost: ExtCosts, num: u64) -> Result<()> {
let use_gas = num
Expand Down

0 comments on commit 7d87464

Please sign in to comment.