Skip to content

Commit

Permalink
[gas] trivial: move StorageGasParameters to its own module
Browse files Browse the repository at this point in the history
  • Loading branch information
msmouse committed Dec 10, 2022
1 parent 2334576 commit 7bb2f06
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 96 deletions.
3 changes: 1 addition & 2 deletions aptos-move/aptos-gas/src/gas_meter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ use crate::{
algebra::{AbstractValueSize, Gas},
instr::InstructionGasParameters,
misc::MiscGasParameters,
transaction::StorageGasParameters,
transaction::TransactionGasParameters,
transaction::{StorageGasParameters, TransactionGasParameters},
};
use aptos_types::{
account_config::CORE_CODE_ADDRESS, state_store::state_key::StateKey, write_set::WriteOp,
Expand Down
3 changes: 1 addition & 2 deletions aptos-move/aptos-gas/src/instr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
//! initial values in the genesis and a mapping between the Rust representation and the on-chain
//! gas schedule.
use crate::algebra::InternalGasPerAbstractValueUnit;
use crate::gas_meter::EXECUTION_GAS_MULTIPLIER as MUL;
use crate::{algebra::InternalGasPerAbstractValueUnit, gas_meter::EXECUTION_GAS_MULTIPLIER as MUL};
use move_binary_format::errors::PartialVMResult;
use move_core_types::gas_algebra::{InternalGas, InternalGasPerArg, InternalGasPerByte};
use move_vm_types::gas::SimpleInstruction;
Expand Down
6 changes: 4 additions & 2 deletions aptos-move/aptos-gas/src/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

use std::collections::BTreeMap;

use crate::algebra::{AbstractValueSize, AbstractValueSizePerArg};
use crate::gas_meter::{FromOnChainGasSchedule, InitialGasSchedule, ToOnChainGasSchedule};
use crate::{
algebra::{AbstractValueSize, AbstractValueSizePerArg},
gas_meter::{FromOnChainGasSchedule, InitialGasSchedule, ToOnChainGasSchedule},
};
use move_core_types::u256::U256;
use move_core_types::{account_address::AccountAddress, gas_algebra::NumArgs};
use move_vm_types::views::{ValueView, ValueVisitor};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,102 +5,15 @@
//! in the genesis and a mapping between the Rust representation and the on-chain gas schedule.
use crate::algebra::{AbstractValueSize, FeePerGasUnit, Gas, GasScalingFactor, GasUnit};
use aptos_types::{
on_chain_config::StorageGasSchedule, state_store::state_key::StateKey, write_set::WriteOp,
};
use aptos_types::{state_store::state_key::StateKey, write_set::WriteOp};
use move_core_types::gas_algebra::{
InternalGas, InternalGasPerArg, InternalGasPerByte, InternalGasUnit, NumArgs, NumBytes,
ToUnitFractionalWithParams, ToUnitWithParams,
};

#[derive(Clone, Debug)]
pub struct StorageGasParameters {
pub per_item_read: InternalGasPerArg,
pub per_item_create: InternalGasPerArg,
pub per_item_write: InternalGasPerArg,
pub per_byte_read: InternalGasPerByte,
pub per_byte_create: InternalGasPerByte,
pub per_byte_write: InternalGasPerByte,
}

impl From<StorageGasSchedule> for StorageGasParameters {
fn from(gas_schedule: StorageGasSchedule) -> Self {
Self {
per_item_read: gas_schedule.per_item_read.into(),
per_item_create: gas_schedule.per_item_create.into(),
per_item_write: gas_schedule.per_item_write.into(),
per_byte_read: gas_schedule.per_byte_read.into(),
per_byte_create: gas_schedule.per_byte_create.into(),
per_byte_write: gas_schedule.per_byte_write.into(),
}
}
}

impl StorageGasParameters {
pub fn zeros() -> Self {
Self {
per_item_read: 0.into(),
per_item_create: 0.into(),
per_item_write: 0.into(),
per_byte_read: 0.into(),
per_byte_create: 0.into(),
per_byte_write: 0.into(),
}
}
}

impl StorageGasParameters {
pub fn calculate_write_set_gas<'a>(
&self,
ops: impl IntoIterator<Item = (&'a StateKey, &'a WriteOp)>,
feature_version: u64,
) -> InternalGas {
use WriteOp::*;

let mut num_items_create = NumArgs::zero();
let mut num_items_write = NumArgs::zero();
let mut num_bytes_create = NumBytes::zero();
let mut num_bytes_write = NumBytes::zero();
mod storage;

for (key, op) in ops.into_iter() {
match &op {
Creation(data) => {
num_items_create += 1.into();
num_bytes_create += Self::write_op_size(key, data, feature_version);
}
Modification(data) => {
num_items_write += 1.into();
num_bytes_write += Self::write_op_size(key, data, feature_version);
}
Deletion => (),
}
}

num_items_create * self.per_item_create
+ num_items_write * self.per_item_write
+ num_bytes_create * self.per_byte_create
+ num_bytes_write * self.per_byte_write
}

fn write_op_size(key: &StateKey, value: &[u8], feature_version: u64) -> NumBytes {
let value_size = NumBytes::new(value.len() as u64);

if feature_version > 2 {
let key_size = NumBytes::new(key.size() as u64);
let kb = NumBytes::new(1024);
(key_size + value_size)
.checked_sub(kb)
.unwrap_or(NumBytes::zero())
} else {
let key_size = NumBytes::new(
key.encode()
.expect("Should be able to serialize state key")
.len() as u64,
);
key_size + value_size
}
}
}
pub use storage::StorageGasParameters;

crate::params::define_gas_parameters!(
TransactionGasParameters,
Expand Down
98 changes: 98 additions & 0 deletions aptos-move/aptos-gas/src/transaction/storage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright (c) Aptos
// SPDX-License-Identifier: Apache-2.0

use aptos_types::{
on_chain_config::StorageGasSchedule, state_store::state_key::StateKey, write_set::WriteOp,
};
use move_core_types::gas_algebra::{
InternalGas, InternalGasPerArg, InternalGasPerByte, NumArgs, NumBytes,
};

#[derive(Clone, Debug)]
pub struct StorageGasParameters {
pub per_item_read: InternalGasPerArg,
pub per_item_create: InternalGasPerArg,
pub per_item_write: InternalGasPerArg,
pub per_byte_read: InternalGasPerByte,
pub per_byte_create: InternalGasPerByte,
pub per_byte_write: InternalGasPerByte,
}

impl From<StorageGasSchedule> for StorageGasParameters {
fn from(gas_schedule: StorageGasSchedule) -> Self {
Self {
per_item_read: gas_schedule.per_item_read.into(),
per_item_create: gas_schedule.per_item_create.into(),
per_item_write: gas_schedule.per_item_write.into(),
per_byte_read: gas_schedule.per_byte_read.into(),
per_byte_create: gas_schedule.per_byte_create.into(),
per_byte_write: gas_schedule.per_byte_write.into(),
}
}
}

impl StorageGasParameters {
pub fn zeros() -> Self {
Self {
per_item_read: 0.into(),
per_item_create: 0.into(),
per_item_write: 0.into(),
per_byte_read: 0.into(),
per_byte_create: 0.into(),
per_byte_write: 0.into(),
}
}
}

impl StorageGasParameters {
pub fn calculate_write_set_gas<'a>(
&self,
ops: impl IntoIterator<Item = (&'a StateKey, &'a WriteOp)>,
feature_version: u64,
) -> InternalGas {
use aptos_types::write_set::WriteOp::*;

let mut num_items_create = NumArgs::zero();
let mut num_items_write = NumArgs::zero();
let mut num_bytes_create = NumBytes::zero();
let mut num_bytes_write = NumBytes::zero();

for (key, op) in ops.into_iter() {
match &op {
Creation(data) => {
num_items_create += 1.into();
num_bytes_create += Self::write_op_size(key, data, feature_version);
}
Modification(data) => {
num_items_write += 1.into();
num_bytes_write += Self::write_op_size(key, data, feature_version);
}
Deletion => (),
}
}

num_items_create * self.per_item_create
+ num_items_write * self.per_item_write
+ num_bytes_create * self.per_byte_create
+ num_bytes_write * self.per_byte_write
}

fn write_op_size(key: &StateKey, value: &[u8], feature_version: u64) -> NumBytes {
let value_size = NumBytes::new(value.len() as u64);

if feature_version > 2 {
let key_size = NumBytes::new(key.size() as u64);
let kb = NumBytes::new(1024);
(key_size + value_size)
.checked_sub(kb)
.unwrap_or(NumBytes::zero())
} else {
let key_size = NumBytes::new(
key.encode()
.expect("Should be able to serialize state key")
.len() as u64,
);
key_size + value_size
}
}
}

0 comments on commit 7bb2f06

Please sign in to comment.