From 3614266dad0e50e80d72b41305f3982057e7d39b Mon Sep 17 00:00:00 2001 From: jolestar Date: Tue, 9 Aug 2022 00:49:39 +0800 Subject: [PATCH] [vm] check gas when execute readonly function --- vm/vm-runtime/src/starcoin_vm.rs | 48 ++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/vm/vm-runtime/src/starcoin_vm.rs b/vm/vm-runtime/src/starcoin_vm.rs index db3665e3c5..4b7b110fab 100644 --- a/vm/vm-runtime/src/starcoin_vm.rs +++ b/vm/vm-runtime/src/starcoin_vm.rs @@ -138,12 +138,13 @@ impl StarcoinVM { ); let instruction_schedule = { let data = self - .execute_readonly_function( + .execute_readonly_function_internal( state, &ModuleId::new(core_code_address(), G_VM_CONFIG_IDENTIFIER.to_owned()), G_INSTRUCTION_SCHEDULE_IDENTIFIER.as_ident_str(), vec![], vec![], + false, )? .pop() .ok_or_else(|| { @@ -155,12 +156,13 @@ impl StarcoinVM { }; let native_schedule = { let data = self - .execute_readonly_function( + .execute_readonly_function_internal( state, &ModuleId::new(core_code_address(), G_VM_CONFIG_IDENTIFIER.to_owned()), G_NATIVE_SCHEDULE_IDENTIFIER.as_ident_str(), vec![], vec![], + false, )? .pop() .ok_or_else(|| { @@ -170,12 +172,13 @@ impl StarcoinVM { }; let gas_constants = { let data = self - .execute_readonly_function( + .execute_readonly_function_internal( state, &ModuleId::new(core_code_address(), G_VM_CONFIG_IDENTIFIER.to_owned()), G_GAS_CONSTANTS_IDENTIFIER.as_ident_str(), vec![], vec![], + false, )? .pop() .ok_or_else(|| { @@ -1105,6 +1108,25 @@ impl StarcoinVM { function_name: &IdentStr, type_params: Vec, args: Vec>, + ) -> Result>, VMStatus> { + self.execute_readonly_function_internal( + state_view, + module, + function_name, + type_params, + args, + true, + ) + } + + fn execute_readonly_function_internal( + &mut self, + state_view: &S, + module: &ModuleId, + function_name: &IdentStr, + type_params: Vec, + args: Vec>, + check_gas: bool, ) -> Result>, VMStatus> { let _timer = self.metrics.as_ref().map(|metrics| { metrics @@ -1114,9 +1136,23 @@ impl StarcoinVM { }); let data_cache = StateViewCache::new(state_view); - let cost_table = &G_ZERO_COST_SCHEDULE; - let mut gas_status = { - let mut gas_status = GasStatus::new(cost_table, GasUnits::new(0)); + let mut gas_status = if check_gas { + if let Err(err) = self.load_configs(state_view) { + warn!( + "Load config error at execute_readonly_function_internal: {}", + err + ); + return Err(VMStatus::Error(StatusCode::VM_STARTUP_FAILURE)); + } + let gas_constants = &self.get_gas_schedule()?.gas_constants; + let mut gas_status = GasStatus::new( + &G_LATEST_GAS_SCHEDULE, + GasUnits::new(gas_constants.maximum_number_of_gas_units.get()), + ); + gas_status.set_metering(true); + gas_status + } else { + let mut gas_status = GasStatus::new(&G_ZERO_COST_SCHEDULE, GasUnits::new(0)); gas_status.set_metering(false); gas_status };