Skip to content

Commit

Permalink
[vm] check gas when execute readonly function
Browse files Browse the repository at this point in the history
  • Loading branch information
jolestar committed Aug 8, 2022
1 parent f3c9687 commit 3614266
Showing 1 changed file with 42 additions and 6 deletions.
48 changes: 42 additions & 6 deletions vm/vm-runtime/src/starcoin_vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(|| {
Expand All @@ -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(|| {
Expand All @@ -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(|| {
Expand Down Expand Up @@ -1105,6 +1108,25 @@ impl StarcoinVM {
function_name: &IdentStr,
type_params: Vec<TypeTag>,
args: Vec<Vec<u8>>,
) -> Result<Vec<Vec<u8>>, VMStatus> {
self.execute_readonly_function_internal(
state_view,
module,
function_name,
type_params,
args,
true,
)
}

fn execute_readonly_function_internal<S: StateView>(
&mut self,
state_view: &S,
module: &ModuleId,
function_name: &IdentStr,
type_params: Vec<TypeTag>,
args: Vec<Vec<u8>>,
check_gas: bool,
) -> Result<Vec<Vec<u8>>, VMStatus> {
let _timer = self.metrics.as_ref().map(|metrics| {
metrics
Expand All @@ -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
};
Expand Down

0 comments on commit 3614266

Please sign in to comment.