Skip to content

Commit

Permalink
trivial: split out StoragePricing
Browse files Browse the repository at this point in the history
  • Loading branch information
msmouse committed Dec 10, 2023
1 parent 8d0aa5e commit a978be3
Show file tree
Hide file tree
Showing 15 changed files with 225 additions and 207 deletions.
6 changes: 3 additions & 3 deletions aptos-move/aptos-abstract-gas-usage/src/algebra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use aptos_gas_algebra::{
};
use aptos_gas_meter::GasAlgebra;
use aptos_gas_schedule::VMGasParameters;
use aptos_vm_types::storage::StorageGasParameters;
use aptos_vm_types::storage::io_pricing::IoPricing;
use move_binary_format::errors::PartialVMResult;
use std::sync::{Arc, Mutex};

Expand All @@ -29,8 +29,8 @@ impl<A: GasAlgebra> GasAlgebra for CalibrationAlgebra<A> {
self.base.vm_gas_params()
}

fn storage_gas_params(&self) -> &StorageGasParameters {
self.base.storage_gas_params()
fn io_pricing(&self) -> &IoPricing {
self.base.io_pricing()
}

fn balance_internal(&self) -> InternalGas {
Expand Down
4 changes: 3 additions & 1 deletion aptos-move/aptos-debugger/src/aptos_debugger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ use aptos_vm::{
AptosVM, VMExecutor,
};
use aptos_vm_logging::log_schema::AdapterLogSchema;
use aptos_vm_types::{change_set::VMChangeSet, output::VMOutput, storage::ChangeSetConfigs};
use aptos_vm_types::{
change_set::VMChangeSet, output::VMOutput, storage::change_set_configs::ChangeSetConfigs,
};
use move_binary_format::errors::VMResult;
use std::{path::Path, sync::Arc};

Expand Down
6 changes: 3 additions & 3 deletions aptos-move/aptos-gas-meter/src/algebra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::traits::GasAlgebra;
use aptos_gas_algebra::{Fee, FeePerGasUnit, Gas, GasExpression, Octa};
use aptos_gas_schedule::VMGasParameters;
use aptos_logger::error;
use aptos_vm_types::storage::StorageGasParameters;
use aptos_vm_types::storage::{io_pricing::IoPricing, StorageGasParameters};
use move_binary_format::errors::{PartialVMError, PartialVMResult};
use move_core_types::{
gas_algebra::{InternalGas, InternalGasUnit},
Expand Down Expand Up @@ -83,8 +83,8 @@ impl GasAlgebra for StandardGasAlgebra {
&self.vm_gas_params
}

fn storage_gas_params(&self) -> &StorageGasParameters {
&self.storage_gas_params
fn io_pricing(&self) -> &IoPricing {
&self.storage_gas_params.io_pricing
}

fn balance_internal(&self) -> InternalGas {
Expand Down
8 changes: 2 additions & 6 deletions aptos-move/aptos-gas-meter/src/meter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,7 @@ where
return Err(PartialVMError::new(StatusCode::UNKNOWN_INVARIANT_VIOLATION_ERROR).with_message("in legacy versions, number of bytes loaded must be zero when the resource does not exist ".to_string()));
}
let cost = self
.storage_gas_params()
.pricing
.io_pricing()
.calculate_read_gas(val.is_some(), bytes_loaded);
self.algebra.charge_io(cost)
}
Expand Down Expand Up @@ -489,10 +488,7 @@ where
}

fn charge_io_gas_for_write(&mut self, key: &StateKey, op_size: &WriteOpSize) -> VMResult<()> {
let cost = self
.storage_gas_params()
.pricing
.io_gas_per_write(key, op_size);
let cost = self.io_pricing().io_gas_per_write(key, op_size);

self.algebra
.charge_io(cost)
Expand Down
8 changes: 4 additions & 4 deletions aptos-move/aptos-gas-meter/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use aptos_types::{
state_store::{state_key::StateKey, state_value::StateValueMetadata},
write_set::WriteOpSize,
};
use aptos_vm_types::{change_set::VMChangeSet, storage::StorageGasParameters};
use aptos_vm_types::{change_set::VMChangeSet, storage::io_pricing::IoPricing};
use move_binary_format::errors::{Location, PartialVMResult, VMResult};
use move_core_types::gas_algebra::{InternalGas, InternalGasUnit, NumBytes};
use move_vm_types::gas::GasMeter as MoveGasMeter;
Expand All @@ -24,7 +24,7 @@ pub trait GasAlgebra {
fn vm_gas_params(&self) -> &VMGasParameters;

/// Returns the struct containing the storage-specific gas parameters.
fn storage_gas_params(&self) -> &StorageGasParameters;
fn io_pricing(&self) -> &IoPricing;

/// Returns the current balance, measured in internal gas units.
fn balance_internal(&self) -> InternalGas;
Expand Down Expand Up @@ -212,8 +212,8 @@ pub trait AptosGasMeter: MoveGasMeter {
}

// Returns a reference to the struct containing all storage gas parameters.
fn storage_gas_params(&self) -> &StorageGasParameters {
self.algebra().storage_gas_params()
fn io_pricing(&self) -> &IoPricing {
self.algebra().io_pricing()
}

/// Returns the remaining balance, measured in (external) gas units.
Expand Down
124 changes: 124 additions & 0 deletions aptos-move/aptos-vm-types/src/storage/change_set_configs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0

use crate::{change_set::VMChangeSet, check_change_set::CheckChangeSet};
use aptos_gas_schedule::AptosGasParameters;
use move_core_types::vm_status::{err_msg, StatusCode, VMStatus};

#[derive(Clone, Debug)]
pub struct ChangeSetConfigs {
gas_feature_version: u64,
max_bytes_per_write_op: u64,
max_bytes_all_write_ops_per_transaction: u64,
max_bytes_per_event: u64,
max_bytes_all_events_per_transaction: u64,
max_write_ops_per_transaction: u64,
}

impl ChangeSetConfigs {
pub fn unlimited_at_gas_feature_version(gas_feature_version: u64) -> Self {
Self::new_impl(
gas_feature_version,
u64::MAX,
u64::MAX,
u64::MAX,
u64::MAX,
u64::MAX,
)
}

pub fn new(feature_version: u64, gas_params: &AptosGasParameters) -> Self {
if feature_version >= 5 {
Self::from_gas_params(feature_version, gas_params)
} else if feature_version >= 3 {
Self::for_feature_version_3()
} else {
Self::unlimited_at_gas_feature_version(feature_version)
}
}

fn new_impl(
gas_feature_version: u64,
max_bytes_per_write_op: u64,
max_bytes_all_write_ops_per_transaction: u64,
max_bytes_per_event: u64,
max_bytes_all_events_per_transaction: u64,
max_write_ops_per_transaction: u64,
) -> Self {
Self {
gas_feature_version,
max_bytes_per_write_op,
max_bytes_all_write_ops_per_transaction,
max_bytes_per_event,
max_bytes_all_events_per_transaction,
max_write_ops_per_transaction,
}
}

pub fn legacy_resource_creation_as_modification(&self) -> bool {
// Bug fixed at gas_feature_version 3 where (non-group) resource creation was converted to
// modification.
// Modules and table items were not affected (https://github.com/aptos-labs/aptos-core/pull/4722/commits/7c5e52297e8d1a6eac67a68a804ab1ca2a0b0f37).
// Resource groups and state values with metadata were not affected because they were
// introduced later than feature_version 3 on all networks.
self.gas_feature_version < 3
}

fn for_feature_version_3() -> Self {
const MB: u64 = 1 << 20;

Self::new_impl(3, MB, u64::MAX, MB, 10 * MB, u64::MAX)
}

fn from_gas_params(gas_feature_version: u64, gas_params: &AptosGasParameters) -> Self {
let params = &gas_params.vm.txn;
Self::new_impl(
gas_feature_version,
params.max_bytes_per_write_op.into(),
params.max_bytes_all_write_ops_per_transaction.into(),
params.max_bytes_per_event.into(),
params.max_bytes_all_events_per_transaction.into(),
params.max_write_ops_per_transaction.into(),
)
}
}

impl CheckChangeSet for ChangeSetConfigs {
fn check_change_set(&self, change_set: &VMChangeSet) -> Result<(), VMStatus> {
const ERR: StatusCode = StatusCode::STORAGE_WRITE_LIMIT_REACHED;

if self.max_write_ops_per_transaction != 0
&& change_set.num_write_ops() as u64 > self.max_write_ops_per_transaction
{
return Err(VMStatus::error(ERR, err_msg("Too many write ops.")));
}

let mut write_set_size = 0;
for (key, op_size) in change_set.write_set_size_iter() {
if let Some(len) = op_size.write_len() {
let write_op_size = len + (key.size() as u64);
if write_op_size > self.max_bytes_per_write_op {
return Err(VMStatus::error(ERR, None));
}
write_set_size += write_op_size;
}
if write_set_size > self.max_bytes_all_write_ops_per_transaction {
return Err(VMStatus::error(ERR, None));
}
}

let mut total_event_size = 0;
for (event, _) in change_set.events() {
let size = event.event_data().len() as u64;
if size > self.max_bytes_per_event {
return Err(VMStatus::error(ERR, None));
}
total_event_size += size;
if total_event_size > self.max_bytes_all_events_per_transaction {
return Err(VMStatus::error(ERR, None));
}
}

Ok(())
}
}
Loading

0 comments on commit a978be3

Please sign in to comment.