Skip to content

Commit

Permalink
Remove per slot free quota for write gas
Browse files Browse the repository at this point in the history
  • Loading branch information
msmouse committed Dec 11, 2023
1 parent a73f3ae commit ad64c24
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 33 deletions.
4 changes: 2 additions & 2 deletions aptos-move/aptos-gas-schedule/src/gas_schedule/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ crate::gas_schedule::macros::define_gas_parameters!(
],
[memory_quota: AbstractValueSize, { 1.. => "memory_quota" }, 10_000_000],
[
free_write_bytes_quota: NumBytes,
{ 5.. => "free_write_bytes_quota" },
legacy_free_write_bytes_quota: NumBytes,
{ 5..=11 => "free_write_bytes_quota", 12.. => "legacy_free_write_bytes_quota" },
1024, // 1KB free per state write
],
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ pub enum FeatureFlag {
VMBinaryFormatV7,
ResourceGroupsChargeAsSizeSum,
CommissionChangeDelegationPool,
EphemeralBytesFee,
}

fn generate_features_blob(writer: &CodeWriter, data: &[u64]) {
Expand Down Expand Up @@ -244,6 +245,7 @@ impl From<FeatureFlag> for AptosFeatureFlag {
FeatureFlag::CommissionChangeDelegationPool => {
AptosFeatureFlag::COMMISSION_CHANGE_DELEGATION_POOL
},
FeatureFlag::EphemeralBytesFee => AptosFeatureFlag::EPHEMERAL_BYTES_FEE,
}
}
}
Expand Down Expand Up @@ -318,6 +320,7 @@ impl From<AptosFeatureFlag> for FeatureFlag {
AptosFeatureFlag::COMMISSION_CHANGE_DELEGATION_POOL => {
FeatureFlag::CommissionChangeDelegationPool
},
AptosFeatureFlag::EPHEMERAL_BYTES_FEE => FeatureFlag::EphemeralBytesFee,
}
}
}
Expand Down
45 changes: 28 additions & 17 deletions aptos-move/aptos-vm-types/src/storage/io_pricing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use aptos_gas_schedule::{
AptosGasParameters, VMGasParameters,
};
use aptos_types::{
on_chain_config::{ConfigStorage, StorageGasSchedule},
on_chain_config::{ConfigStorage, Features, StorageGasSchedule},
state_store::state_key::StateKey,
write_set::WriteOpSize,
};
Expand Down Expand Up @@ -84,7 +84,7 @@ impl IoPricingV1 {

#[derive(Clone, Debug)]
pub struct IoPricingV2 {
pub feature_version: u64,
pub gas_feature_version: u64,
pub free_write_bytes_quota: NumBytes,
pub per_item_read: InternalGasPerArg,
pub per_item_create: InternalGasPerArg,
Expand All @@ -96,13 +96,16 @@ pub struct IoPricingV2 {

impl IoPricingV2 {
pub fn new_with_storage_curves(
feature_version: u64,
gas_feature_version: u64,
storage_gas_schedule: &StorageGasSchedule,
gas_params: &AptosGasParameters,
) -> Self {
Self {
feature_version,
free_write_bytes_quota: Self::get_free_write_bytes_quota(feature_version, gas_params),
gas_feature_version,
free_write_bytes_quota: Self::get_free_write_bytes_quota(
gas_feature_version,
gas_params,
),
per_item_read: storage_gas_schedule.per_item_read.into(),
per_item_create: storage_gas_schedule.per_item_create.into(),
per_item_write: storage_gas_schedule.per_item_write.into(),
Expand All @@ -113,21 +116,21 @@ impl IoPricingV2 {
}

fn get_free_write_bytes_quota(
feature_version: u64,
gas_feature_version: u64,
gas_params: &AptosGasParameters,
) -> NumBytes {
match feature_version {
match gas_feature_version {
0 => unreachable!("PricingV2 not applicable for feature version 0"),
1..=2 => 0.into(),
3..=4 => 1024.into(),
5.. => gas_params.vm.txn.free_write_bytes_quota,
5.. => gas_params.vm.txn.legacy_free_write_bytes_quota,
}
}

fn write_op_size(&self, key: &StateKey, value_size: u64) -> NumBytes {
let value_size = NumBytes::new(value_size);

if self.feature_version >= 3 {
if self.gas_feature_version >= 3 {
let key_size = NumBytes::new(key.size() as u64);
(key_size + value_size)
.checked_sub(self.free_write_bytes_quota)
Expand Down Expand Up @@ -166,7 +169,7 @@ impl IoPricingV2 {
// No storage curve. New gas parameter representation.
#[derive(Debug, Clone)]
pub struct IoPricingV3 {
pub feature_version: u64,
pub gas_feature_version: u64,
pub free_write_bytes_quota: NumBytes,
}

Expand Down Expand Up @@ -213,27 +216,35 @@ pub enum IoPricing {

impl IoPricing {
pub fn new(
feature_version: u64,
gas_feature_version: u64,
features: &Features,
gas_params: &AptosGasParameters,
config_storage: &impl ConfigStorage,
) -> IoPricing {
use aptos_types::on_chain_config::OnChainConfig;
use IoPricing::*;

match feature_version {
match gas_feature_version {
0 => V1(IoPricingV1::new(gas_params)),
1..=9 => match StorageGasSchedule::fetch_config(config_storage) {
None => V1(IoPricingV1::new(gas_params)),
Some(schedule) => V2(IoPricingV2::new_with_storage_curves(
feature_version,
gas_feature_version,
&schedule,
gas_params,
)),
},
10.. => V3(IoPricingV3 {
feature_version,
free_write_bytes_quota: gas_params.vm.txn.free_write_bytes_quota,
}),
10.. => {
let free_write_bytes_quota = if features.is_ephemeral_bytes_fee_enabled() {
NumBytes::zero()
} else {
gas_params.vm.txn.legacy_free_write_bytes_quota
};
V3(IoPricingV3 {
gas_feature_version,
free_write_bytes_quota,
})
},
}
}

Expand Down
11 changes: 6 additions & 5 deletions aptos-move/aptos-vm-types/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::storage::{
space_pricing::DiskSpacePricing,
};
use aptos_gas_schedule::{AptosGasParameters, LATEST_GAS_FEATURE_VERSION};
use aptos_types::on_chain_config::ConfigStorage;
use aptos_types::on_chain_config::{ConfigStorage, Features};
use move_core_types::gas_algebra::NumBytes;
use std::fmt::Debug;

Expand All @@ -24,13 +24,14 @@ pub struct StorageGasParameters {

impl StorageGasParameters {
pub fn new(
feature_version: u64,
gas_feature_version: u64,
features: &Features,
gas_params: &AptosGasParameters,
config_storage: &impl ConfigStorage,
) -> Self {
let io_pricing = IoPricing::new(feature_version, gas_params, config_storage);
let io_pricing = IoPricing::new(gas_feature_version, features, gas_params, config_storage);
let space_pricing = DiskSpacePricing::v1();
let change_set_configs = ChangeSetConfigs::new(feature_version, gas_params);
let change_set_configs = ChangeSetConfigs::new(gas_feature_version, gas_params);

Self {
io_pricing,
Expand All @@ -42,7 +43,7 @@ impl StorageGasParameters {
pub fn unlimited(free_write_bytes_quota: NumBytes) -> Self {
Self {
io_pricing: IoPricing::V3(IoPricingV3 {
feature_version: LATEST_GAS_FEATURE_VERSION,
gas_feature_version: LATEST_GAS_FEATURE_VERSION,
free_write_bytes_quota,
}),
space_pricing: DiskSpacePricing::v1(),
Expand Down
2 changes: 1 addition & 1 deletion aptos-move/aptos-vm-types/src/storage/space_pricing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl DiskSpacePricing {
value_size: u64,
) -> NumBytes {
let size = NumBytes::new(key.size() as u64) + NumBytes::new(value_size);
size.checked_sub(params.free_write_bytes_quota)
size.checked_sub(params.legacy_free_write_bytes_quota)
.unwrap_or(NumBytes::zero())
}

Expand Down
5 changes: 2 additions & 3 deletions aptos-move/aptos-vm/src/aptos_vm_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,12 @@ impl AptosVMImpl {
let _timer = TIMER.timer_with(&["impl_new"]);
// Get the gas parameters
let (mut gas_params, gas_feature_version) = gas_config(resolver);
let features = Features::fetch_config(resolver).unwrap_or_default();

let storage_gas_params = match &mut gas_params {
Ok(gas_params) => {
let storage_gas_params =
StorageGasParameters::new(gas_feature_version, gas_params, resolver);
StorageGasParameters::new(gas_feature_version, &features, gas_params, resolver);

// Overwrite table io gas parameters with global io pricing.
let g = &mut gas_params.natives.table;
Expand Down Expand Up @@ -134,8 +135,6 @@ impl AptosVMImpl {
Err(_) => (NativeGasParameters::zeros(), MiscGasParameters::zeros()),
};

let features = Features::fetch_config(resolver).unwrap_or_default();

// If no chain ID is in storage, we assume we are in a testing environment and use ChainId::TESTING
let chain_id = ChainId::fetch_config(resolver).unwrap_or_else(ChainId::test);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ fn io_limit_reached_by_new_bytes() {
// Allow 10 value bytes charged at most.
gas_params.vm.txn.max_io_gas = 110_000_000.into();
// Make the key bytes free, only play around value sizes.
gas_params.vm.txn.free_write_bytes_quota = state_key_size();
gas_params.vm.txn.legacy_free_write_bytes_quota = state_key_size();
});

test_create_multiple_items(&mut h, &acc, |status| {
Expand All @@ -112,7 +112,7 @@ fn storage_limit_reached_by_new_bytes() {
// Allow 10 value bytes charged at most.
gas_params.vm.txn.max_storage_fee = 11_000_000.into();
// Make the key bytes free, only play around value sizes.
gas_params.vm.txn.free_write_bytes_quota = state_key_size();
gas_params.vm.txn.legacy_free_write_bytes_quota = state_key_size();
});

test_create_multiple_items(&mut h, &acc, |status| {
Expand All @@ -134,7 +134,7 @@ fn out_of_gas_while_charging_write_gas() {
// Bump max gas allowed
gas_params.vm.txn.maximum_number_of_gas_units = 1_000_000_000.into();
// Make the key bytes free, only play around value sizes.
gas_params.vm.txn.free_write_bytes_quota = state_key_size();
gas_params.vm.txn.legacy_free_write_bytes_quota = state_key_size();
});
// Allow 10 value bytes charged at most. Notice this is in external units.
h.set_max_gas_per_txn(110_000);
Expand All @@ -156,7 +156,7 @@ fn out_of_gas_while_charging_storage_fee() {
// Bump max gas allowed
gas_params.vm.txn.maximum_number_of_gas_units = 1_000_000_000.into();
// Make the key bytes free, only play around value sizes.
gas_params.vm.txn.free_write_bytes_quota = state_key_size();
gas_params.vm.txn.legacy_free_write_bytes_quota = state_key_size();
});
// Allow 10 value bytes charged at most. Notice this is in external units,
// which is 1/100x octas or 1Mx internal units.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ fn failed_transaction_cleanup_test() {

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

let change_set_configs = storage_gas_params.change_set_configs.clone();

Expand Down
6 changes: 6 additions & 0 deletions types/src/on_chain_config/aptos_features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub enum FeatureFlag {
VM_BINARY_FORMAT_V7 = 40,
RESOURCE_GROUPS_CHARGE_AS_SIZE_SUM = 41,
COMMISSION_CHANGE_DELEGATION_POOL = 42,
EPHEMERAL_BYTES_FEE = 43,
}

/// Representation of features on chain as a bitset.
Expand All @@ -73,6 +74,7 @@ impl Default for Features {
features.enable(APTOS_UNIQUE_IDENTIFIERS);
features.enable(SIGNATURE_CHECKER_V2_SCRIPT_FIX);
features.enable(AGGREGATOR_V2_API);
features.enable(EPHEMERAL_BYTES_FEE);

features
}
Expand Down Expand Up @@ -143,4 +145,8 @@ impl Features {
pub fn is_resource_group_charge_as_size_sum_enabled(&self) -> bool {
self.is_enabled(FeatureFlag::RESOURCE_GROUPS_CHARGE_AS_SIZE_SUM)
}

pub fn is_ephemeral_bytes_fee_enabled(&self) -> bool {
self.is_enabled(FeatureFlag::EPHEMERAL_BYTES_FEE)
}
}

0 comments on commit ad64c24

Please sign in to comment.