Skip to content

Commit

Permalink
[gas] fix abstract gas cost for storage (#9501)
Browse files Browse the repository at this point in the history
  • Loading branch information
vgao1996 authored and xbtmatt committed Aug 13, 2023
1 parent 7eb9a12 commit 0bb2b4c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 38 deletions.
33 changes: 8 additions & 25 deletions aptos-move/aptos-vm-types/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,6 @@ pub struct StoragePricingV2 {
}

impl StoragePricingV2 {
pub fn zeros() -> Self {
Self::new_without_storage_curves(LATEST_GAS_FEATURE_VERSION, &AptosGasParameters::zeros())
}

pub fn new_with_storage_curves(
feature_version: u64,
storage_gas_schedule: &StorageGasSchedule,
Expand All @@ -117,22 +113,6 @@ impl StoragePricingV2 {
}
}

pub fn new_without_storage_curves(
feature_version: u64,
gas_params: &AptosGasParameters,
) -> Self {
Self {
feature_version,
free_write_bytes_quota: Self::get_free_write_bytes_quota(feature_version, gas_params),
per_item_read: gas_params.vm.txn.storage_io_per_state_slot_read,
per_item_create: gas_params.vm.txn.storage_io_per_state_slot_write,
per_item_write: gas_params.vm.txn.storage_io_per_state_slot_write,
per_byte_read: gas_params.vm.txn.storage_io_per_state_byte_read,
per_byte_create: gas_params.vm.txn.storage_io_per_state_byte_write,
per_byte_write: gas_params.vm.txn.storage_io_per_state_byte_write,
}
}

fn get_free_write_bytes_quota(
feature_version: u64,
gas_params: &AptosGasParameters,
Expand Down Expand Up @@ -253,10 +233,10 @@ impl StoragePricing {
gas_params,
)),
},
10.. => V2(StoragePricingV2::new_without_storage_curves(
10.. => V3(StoragePricingV3 {
feature_version,
gas_params,
)),
free_write_bytes_quota: gas_params.vm.txn.free_write_bytes_quota,
}),
}
}

Expand Down Expand Up @@ -424,9 +404,12 @@ impl StorageGasParameters {
}
}

pub fn free_and_unlimited() -> Self {
pub fn unlimited(free_write_bytes_quota: NumBytes) -> Self {
Self {
pricing: StoragePricing::V2(StoragePricingV2::zeros()),
pricing: StoragePricing::V3(StoragePricingV3 {
feature_version: LATEST_GAS_FEATURE_VERSION,
free_write_bytes_quota,
}),
change_set_configs: ChangeSetConfigs::unlimited_at_gas_feature_version(
LATEST_GAS_FEATURE_VERSION,
),
Expand Down
28 changes: 18 additions & 10 deletions aptos-move/aptos-vm/src/aptos_vm_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,25 +90,33 @@ impl AptosVMImpl {
let storage_gas_params =
StorageGasParameters::new(gas_feature_version, gas_params, &storage);

if let StoragePricing::V2(pricing) = &storage_gas_params.pricing {
// Overwrite table io gas parameters with global io pricing.
let g = &mut gas_params.natives.table;
match gas_feature_version {
0..=1 => (),
2..=6 => {
// Overwrite table io gas parameters with global io pricing.
let g = &mut gas_params.natives.table;
match gas_feature_version {
0..=1 => (),
2..=6 => {
if let StoragePricing::V2(pricing) = &storage_gas_params.pricing {
g.common_load_base_legacy = pricing.per_item_read * NumArgs::new(1);
g.common_load_base_new = 0.into();
g.common_load_per_byte = pricing.per_byte_read;
g.common_load_failure = 0.into();
},
7.. => {
}
}
7..=9 => {
if let StoragePricing::V2(pricing) = &storage_gas_params.pricing {
g.common_load_base_legacy = 0.into();
g.common_load_base_new = pricing.per_item_read * NumArgs::new(1);
g.common_load_per_byte = pricing.per_byte_read;
g.common_load_failure = 0.into();
},
}
}
10.. => {
g.common_load_base_legacy = 0.into();
g.common_load_base_new = gas_params.vm.txn.storage_io_per_state_slot_read * NumArgs::new(1);
g.common_load_per_byte = gas_params.vm.txn.storage_io_per_state_byte_read;
g.common_load_failure = 0.into();
}
}
};
Ok(storage_gas_params)
},
Err(err) => Err(format!("Failed to initialize storage gas params due to failure to load main gas parameters: {}", err)),
Expand Down
2 changes: 1 addition & 1 deletion aptos-move/e2e-tests/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ impl FakeExecutor {
//// TODO: fill in these with proper values
LATEST_GAS_FEATURE_VERSION,
InitialGasSchedule::initial(),
StorageGasParameters::free_and_unlimited(),
StorageGasParameters::unlimited(0.into()),
10000000000000,
),
// coeff_buffer: BTreeMap::new(),
Expand Down
2 changes: 1 addition & 1 deletion aptos-move/e2e-testsuite/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ aptos-crypto = { workspace = true }
aptos-framework = { workspace = true }
aptos-gas-algebra = { workspace = true }
aptos-gas-meter = { workspace = true }
aptos-gas-schedule = { workspace = true }
aptos-gas-schedule = { workspace = true, features = ["testing"] }
aptos-keygen = { workspace = true }
aptos-language-e2e-tests = { workspace = true }
aptos-logger = { workspace = true }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ fn failed_transaction_cleanup_test() {
};

let gas_params = AptosGasParameters::zeros();
let storage_gas_params = StorageGasParameters::free_and_unlimited();
let storage_gas_params =
StorageGasParameters::unlimited(gas_params.vm.txn.free_write_bytes_quota);

let change_set_configs = storage_gas_params.change_set_configs.clone();

Expand Down

0 comments on commit 0bb2b4c

Please sign in to comment.