From f7ffe2ce94e40c7d8169f9ecbca9172ce52f5bce Mon Sep 17 00:00:00 2001 From: Wei Tang Date: Sun, 19 Nov 2023 23:39:46 +0100 Subject: [PATCH] Fix issue with call stipend --- src/gasometer.rs | 7 ++++++- src/standard/gasometer/mod.rs | 14 ++++++++++++-- src/standard/invoker/mod.rs | 14 ++++++-------- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/gasometer.rs b/src/gasometer.rs index d64203f2..3b9e1329 100644 --- a/src/gasometer.rs +++ b/src/gasometer.rs @@ -37,7 +37,12 @@ pub trait Gasometer: Sized { } fn record_codedeposit(&mut self, len: usize) -> Result<(), ExitError>; fn gas(&self) -> U256; - fn submeter(&mut self, gas_limit: U256, code: &[u8]) -> Result; + fn submeter( + &mut self, + gas_limit: U256, + call_has_value: bool, + code: &[u8], + ) -> Result; fn merge(&mut self, other: Self, strategy: MergeStrategy); } diff --git a/src/standard/gasometer/mod.rs b/src/standard/gasometer/mod.rs index 9b633397..fe3e69a1 100644 --- a/src/standard/gasometer/mod.rs +++ b/src/standard/gasometer/mod.rs @@ -203,14 +203,24 @@ impl<'config, S: AsRef, H: RuntimeBackend> GasometerT for Ga self.gas().into() } - fn submeter(&mut self, gas_limit: U256, code: &[u8]) -> Result { - let gas_limit = if gas_limit > U256::from(u64::MAX) { + fn submeter( + &mut self, + gas_limit: U256, + call_has_value: bool, + code: &[u8], + ) -> Result { + let mut gas_limit = if gas_limit > U256::from(u64::MAX) { return Err(ExitException::OutOfGas.into()); } else { gas_limit.as_u64() }; self.record_cost(gas_limit)?; + + if call_has_value { + gas_limit = gas_limit.saturating_add(self.config.call_stipend); + } + Ok(Self::new(gas_limit, code, self.config)) } diff --git a/src/standard/invoker/mod.rs b/src/standard/invoker/mod.rs index ee31b520..565208b3 100644 --- a/src/standard/invoker/mod.rs +++ b/src/standard/invoker/mod.rs @@ -356,14 +356,12 @@ where machine.gasometer.gas() }; let target_gas = trap_data.target_gas().unwrap_or(after_gas); - let mut gas_limit = min(after_gas, target_gas); + let gas_limit = min(after_gas, target_gas); - match &trap_data { - CallCreateTrapData::Call(call) if call.has_value() => { - gas_limit = gas_limit.saturating_add(U256::from(self.config.call_stipend)); - } - _ => (), - } + let call_has_value = match &trap_data { + CallCreateTrapData::Call(call) if call.has_value() => true, + _ => false, + }; let is_static = if machine.is_static { true @@ -377,7 +375,7 @@ where let transaction_context = machine.machine.state.as_ref().transaction_context.clone(); let code = trap_data.code(handler); - let submeter = match machine.gasometer.submeter(gas_limit, &code) { + let submeter = match machine.gasometer.submeter(gas_limit, call_has_value, &code) { Ok(submeter) => submeter, Err(err) => return Capture::Exit(Err(err)), };