diff --git a/vm/vm-runtime/src/starcoin_vm.rs b/vm/vm-runtime/src/starcoin_vm.rs index 5e97dc0b7a..2c0839bc5f 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(|| { @@ -1098,6 +1101,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 @@ -1107,9 +1129,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 };