Skip to content
This repository was archived by the owner on May 4, 2024. It is now read-only.

Commit ee1efcb

Browse files
committed
[gas] pass address & type to the gas meter when loading resources
1 parent 99757af commit ee1efcb

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

language/move-vm/runtime/src/interpreter.rs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,7 @@ impl Interpreter {
526526

527527
/// Loads a resource from the data store and return the number of bytes read from the storage.
528528
fn load_resource<'b>(
529+
loader: &Loader,
529530
gas_meter: &mut impl GasMeter,
530531
data_store: &'b mut impl DataStore,
531532
addr: AccountAddress,
@@ -547,7 +548,7 @@ impl Interpreter {
547548
}
548549
None => None,
549550
};
550-
gas_meter.charge_load_resource(opt)?;
551+
gas_meter.charge_load_resource(addr, TypeWithLoader { ty, loader }, opt)?;
551552
}
552553
Ok(gv)
553554
}
@@ -572,7 +573,7 @@ impl Interpreter {
572573
addr: AccountAddress,
573574
ty: &Type,
574575
) -> PartialVMResult<()> {
575-
let res = Self::load_resource(gas_meter, data_store, addr, ty)?.borrow_global();
576+
let res = Self::load_resource(loader, gas_meter, data_store, addr, ty)?.borrow_global();
576577
gas_meter.charge_borrow_global(
577578
is_mut,
578579
is_generic,
@@ -593,7 +594,7 @@ impl Interpreter {
593594
addr: AccountAddress,
594595
ty: &Type,
595596
) -> PartialVMResult<()> {
596-
let gv = Self::load_resource(gas_meter, data_store, addr, ty)?;
597+
let gv = Self::load_resource(loader, gas_meter, data_store, addr, ty)?;
597598
let exists = gv.exists()?;
598599
gas_meter.charge_exists(is_generic, TypeWithLoader { ty, loader }, exists)?;
599600
self.operand_stack.push(Value::bool(exists))?;
@@ -610,21 +611,22 @@ impl Interpreter {
610611
addr: AccountAddress,
611612
ty: &Type,
612613
) -> PartialVMResult<()> {
613-
let resource = match Self::load_resource(gas_meter, data_store, addr, ty)?.move_from() {
614-
Ok(resource) => {
615-
gas_meter.charge_move_from(
616-
is_generic,
617-
TypeWithLoader { ty, loader },
618-
Some(&resource),
619-
)?;
620-
resource
621-
}
622-
Err(err) => {
623-
let val: Option<&Value> = None;
624-
gas_meter.charge_move_from(is_generic, TypeWithLoader { ty, loader }, val)?;
625-
return Err(err);
626-
}
627-
};
614+
let resource =
615+
match Self::load_resource(loader, gas_meter, data_store, addr, ty)?.move_from() {
616+
Ok(resource) => {
617+
gas_meter.charge_move_from(
618+
is_generic,
619+
TypeWithLoader { ty, loader },
620+
Some(&resource),
621+
)?;
622+
resource
623+
}
624+
Err(err) => {
625+
let val: Option<&Value> = None;
626+
gas_meter.charge_move_from(is_generic, TypeWithLoader { ty, loader }, val)?;
627+
return Err(err);
628+
}
629+
};
628630
self.operand_stack.push(resource)?;
629631
Ok(())
630632
}
@@ -640,7 +642,7 @@ impl Interpreter {
640642
ty: &Type,
641643
resource: Value,
642644
) -> PartialVMResult<()> {
643-
let gv = Self::load_resource(gas_meter, data_store, addr, ty)?;
645+
let gv = Self::load_resource(loader, gas_meter, data_store, addr, ty)?;
644646
// NOTE(Gas): To maintain backward compatibility, we need to charge gas after attempting
645647
// the move_to operation.
646648
match gv.move_to(resource) {

language/move-vm/test-utils/src/gas_schedule.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use move_binary_format::{
1717
file_format_common::{instruction_key, Opcodes},
1818
};
1919
use move_core_types::{
20+
account_address::AccountAddress,
2021
gas_algebra::{
2122
AbstractMemorySize, GasQuantity, InternalGas, InternalGasPerAbstractMemoryUnit,
2223
InternalGasUnit, NumArgs, NumBytes, ToUnit, ToUnitFractional,
@@ -409,6 +410,8 @@ impl<'b> GasMeter for GasStatus<'b> {
409410

410411
fn charge_load_resource(
411412
&mut self,
413+
_addr: AccountAddress,
414+
_ty: impl TypeView,
412415
_loaded: Option<(NumBytes, impl ValueView)>,
413416
) -> PartialVMResult<()> {
414417
Ok(())

language/move-vm/types/src/gas.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use crate::views::{TypeView, ValueView};
55
use move_binary_format::{errors::PartialVMResult, file_format::CodeOffset};
66
use move_core_types::{
7+
account_address::AccountAddress,
78
gas_algebra::{InternalGas, NumArgs, NumBytes},
89
language_storage::ModuleId,
910
};
@@ -209,6 +210,8 @@ pub trait GasMeter {
209210
/// session -- identical transactions can have different gas costs. Use at your own risk.
210211
fn charge_load_resource(
211212
&mut self,
213+
addr: AccountAddress,
214+
ty: impl TypeView,
212215
loaded: Option<(NumBytes, impl ValueView)>,
213216
) -> PartialVMResult<()>;
214217

@@ -435,6 +438,8 @@ impl GasMeter for UnmeteredGasMeter {
435438

436439
fn charge_load_resource(
437440
&mut self,
441+
_addr: AccountAddress,
442+
_ty: impl TypeView,
438443
_loaded: Option<(NumBytes, impl ValueView)>,
439444
) -> PartialVMResult<()> {
440445
Ok(())

0 commit comments

Comments
 (0)