Skip to content

Commit

Permalink
[gas] add gas charges for dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
vgao1996 committed Feb 15, 2024
1 parent c179c89 commit a2cecd6
Show file tree
Hide file tree
Showing 36 changed files with 670 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
"state_change_hash": "",
"event_root_hash": "",
"state_checkpoint_hash": null,
"gas_used": "4",
"gas_used": "5",
"success": true,
"vm_status": "Executed successfully",
"accumulator_root_hash": "",
Expand Down Expand Up @@ -280,7 +280,7 @@
"io_gas_units": "1",
"storage_fee_octas": "0",
"storage_fee_refund_octas": "0",
"total_charge_gas_units": "4"
"total_charge_gas_units": "5"
}
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
"state_change_hash": "",
"event_root_hash": "",
"state_checkpoint_hash": null,
"gas_used": "4",
"gas_used": "5",
"success": true,
"vm_status": "Executed successfully",
"accumulator_root_hash": "",
Expand Down Expand Up @@ -285,7 +285,7 @@
"io_gas_units": "1",
"storage_fee_octas": "0",
"storage_fee_refund_octas": "0",
"total_charge_gas_units": "4"
"total_charge_gas_units": "5"
}
}
],
Expand Down Expand Up @@ -412,7 +412,7 @@
"state_change_hash": "",
"event_root_hash": "",
"state_checkpoint_hash": null,
"gas_used": "4",
"gas_used": "5",
"success": true,
"vm_status": "Executed successfully",
"accumulator_root_hash": "",
Expand Down Expand Up @@ -578,7 +578,7 @@
"io_gas_units": "1",
"storage_fee_octas": "0",
"storage_fee_refund_octas": "0",
"total_charge_gas_units": "4"
"total_charge_gas_units": "5"
}
}
],
Expand Down Expand Up @@ -705,7 +705,7 @@
"state_change_hash": "",
"event_root_hash": "",
"state_checkpoint_hash": null,
"gas_used": "4",
"gas_used": "5",
"success": true,
"vm_status": "Executed successfully",
"accumulator_root_hash": "",
Expand Down Expand Up @@ -871,7 +871,7 @@
"io_gas_units": "1",
"storage_fee_octas": "0",
"storage_fee_refund_octas": "0",
"total_charge_gas_units": "4"
"total_charge_gas_units": "5"
}
}
],
Expand Down Expand Up @@ -998,7 +998,7 @@
"state_change_hash": "",
"event_root_hash": "",
"state_checkpoint_hash": null,
"gas_used": "4",
"gas_used": "5",
"success": true,
"vm_status": "Executed successfully",
"accumulator_root_hash": "",
Expand Down Expand Up @@ -1164,7 +1164,7 @@
"io_gas_units": "1",
"storage_fee_octas": "0",
"storage_fee_refund_octas": "0",
"total_charge_gas_units": "4"
"total_charge_gas_units": "5"
}
}
],
Expand Down Expand Up @@ -1291,7 +1291,7 @@
"state_change_hash": "",
"event_root_hash": "",
"state_checkpoint_hash": null,
"gas_used": "4",
"gas_used": "5",
"success": true,
"vm_status": "Executed successfully",
"accumulator_root_hash": "",
Expand Down Expand Up @@ -1457,7 +1457,7 @@
"io_gas_units": "1",
"storage_fee_octas": "0",
"storage_fee_refund_octas": "0",
"total_charge_gas_units": "4"
"total_charge_gas_units": "5"
}
}
],
Expand Down Expand Up @@ -1584,7 +1584,7 @@
"state_change_hash": "",
"event_root_hash": "",
"state_checkpoint_hash": null,
"gas_used": "4",
"gas_used": "5",
"success": true,
"vm_status": "Executed successfully",
"accumulator_root_hash": "",
Expand Down Expand Up @@ -1750,7 +1750,7 @@
"io_gas_units": "1",
"storage_fee_octas": "0",
"storage_fee_refund_octas": "0",
"total_charge_gas_units": "4"
"total_charge_gas_units": "5"
}
}
],
Expand Down Expand Up @@ -1877,7 +1877,7 @@
"state_change_hash": "",
"event_root_hash": "",
"state_checkpoint_hash": null,
"gas_used": "4",
"gas_used": "5",
"success": true,
"vm_status": "Executed successfully",
"accumulator_root_hash": "",
Expand Down Expand Up @@ -2043,7 +2043,7 @@
"io_gas_units": "1",
"storage_fee_octas": "0",
"storage_fee_refund_octas": "0",
"total_charge_gas_units": "4"
"total_charge_gas_units": "5"
}
}
],
Expand Down Expand Up @@ -2170,7 +2170,7 @@
"state_change_hash": "",
"event_root_hash": "",
"state_checkpoint_hash": null,
"gas_used": "4",
"gas_used": "5",
"success": true,
"vm_status": "Executed successfully",
"accumulator_root_hash": "",
Expand Down Expand Up @@ -2336,7 +2336,7 @@
"io_gas_units": "1",
"storage_fee_octas": "0",
"storage_fee_refund_octas": "0",
"total_charge_gas_units": "4"
"total_charge_gas_units": "5"
}
}
],
Expand Down
7 changes: 6 additions & 1 deletion aptos-move/aptos-abstract-gas-usage/src/algebra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
// SPDX-License-Identifier: Apache-2.0

use aptos_gas_algebra::{
DynamicExpression, Fee, FeePerGasUnit, GasExpression, InternalGas, InternalGasUnit, Octa,
DynamicExpression, Fee, FeePerGasUnit, GasExpression, InternalGas, InternalGasUnit, NumBytes,
Octa,
};
use aptos_gas_meter::GasAlgebra;
use aptos_gas_schedule::VMGasParameters;
Expand Down Expand Up @@ -80,6 +81,10 @@ impl<A: GasAlgebra> GasAlgebra for CalibrationAlgebra<A> {
.charge_storage_fee(abstract_amount, gas_unit_price)
}

fn count_dependency(&mut self, size: NumBytes) -> PartialVMResult<()> {
self.base.count_dependency(size)
}

Check warning on line 86 in aptos-move/aptos-abstract-gas-usage/src/algebra.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/aptos-abstract-gas-usage/src/algebra.rs#L84-L86

Added lines #L84 - L86 were not covered by tests

fn execution_gas_used(&self) -> InternalGas {
self.base.execution_gas_used()
}
Expand Down
5 changes: 5 additions & 0 deletions aptos-move/aptos-gas-algebra/src/algebra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ pub type FeePerSlot = GasQuantity<UnitDiv<Octa, Slot>>;

pub type FeePerByte = GasQuantity<UnitDiv<Octa, Byte>>;

/// Unit of module
pub enum Module {}

pub type NumModules = GasQuantity<Module>;

/***************************************************************************************************
* Unit Conversion
*
Expand Down
24 changes: 23 additions & 1 deletion aptos-move/aptos-gas-meter/src/algebra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

use crate::traits::GasAlgebra;
use aptos_gas_algebra::{Fee, FeePerGasUnit, Gas, GasExpression, Octa};
use aptos_gas_algebra::{Fee, FeePerGasUnit, Gas, GasExpression, NumBytes, NumModules, Octa};
use aptos_gas_schedule::VMGasParameters;
use aptos_logger::error;
use aptos_vm_types::storage::{
Expand Down Expand Up @@ -32,6 +32,9 @@ pub struct StandardGasAlgebra {
storage_fee_in_internal_units: InternalGas,
// The storage fee consumed by the storage operations.
storage_fee_used: Fee,

num_dependencies: NumModules,
total_dependency_size: NumBytes,
}

impl StandardGasAlgebra {
Expand All @@ -53,6 +56,8 @@ impl StandardGasAlgebra {
io_gas_used: 0.into(),
storage_fee_in_internal_units: 0.into(),
storage_fee_used: 0.into(),
num_dependencies: 0.into(),
total_dependency_size: 0.into(),
}
}
}
Expand Down Expand Up @@ -234,6 +239,23 @@ impl GasAlgebra for StandardGasAlgebra {
Ok(())
}

fn count_dependency(&mut self, size: NumBytes) -> PartialVMResult<()> {
if self.feature_version >= 14 {
self.num_dependencies += 1.into();
self.total_dependency_size += size;

if self.num_dependencies > self.vm_gas_params.txn.max_num_dependencies {
return Err(PartialVMError::new(StatusCode::TOO_MANY_DEPENDENCIES));

Check warning on line 248 in aptos-move/aptos-gas-meter/src/algebra.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/aptos-gas-meter/src/algebra.rs#L248

Added line #L248 was not covered by tests
}
if self.total_dependency_size > self.vm_gas_params.txn.max_total_dependency_size {
return Err(PartialVMError::new(
StatusCode::TOTAL_DEPENDENCY_SIZE_TOO_BIG,
));

Check warning on line 253 in aptos-move/aptos-gas-meter/src/algebra.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/aptos-gas-meter/src/algebra.rs#L251-L253

Added lines #L251 - L253 were not covered by tests
}
}

Check warning on line 255 in aptos-move/aptos-gas-meter/src/algebra.rs

View check run for this annotation

Codecov / codecov/patch

aptos-move/aptos-gas-meter/src/algebra.rs#L255

Added line #L255 was not covered by tests
Ok(())
}

fn execution_gas_used(&self) -> InternalGas {
self.execution_gas_used
}
Expand Down
11 changes: 11 additions & 0 deletions aptos-move/aptos-gas-meter/src/meter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,17 @@ where

self.algebra.charge_execution(cost)
}

#[inline]
fn charge_dependency(&mut self, module_id: &ModuleId, size: NumBytes) -> PartialVMResult<()> {
// TODO: How about 0xA550C18?
if self.feature_version() >= 14 && !module_id.address().is_special() {
self.algebra
.charge_execution(DEPENDENCY_PER_MODULE + DEPENDENCY_PER_BYTE * size)?;
self.algebra.count_dependency(size)?;
}
Ok(())
}
}

impl<A> AptosGasMeter for StandardGasMeter<A>
Expand Down
3 changes: 3 additions & 0 deletions aptos-move/aptos-gas-meter/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ pub trait GasAlgebra {
gas_unit_price: FeePerGasUnit,
) -> PartialVMResult<()>;

/// Counts a dependency against the limits.
fn count_dependency(&mut self, size: NumBytes) -> PartialVMResult<()>;

/// Returns the amount of gas used under the execution category.
fn execution_gas_used(&self) -> InternalGas;

Expand Down
20 changes: 18 additions & 2 deletions aptos-move/aptos-gas-profiling/src/erased.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

use crate::{
log::{
CallFrame, EventStorage, ExecutionAndIOCosts, ExecutionGasEvent, StorageFees, WriteStorage,
WriteTransient,
CallFrame, Dependency, EventStorage, ExecutionAndIOCosts, ExecutionGasEvent, StorageFees,
WriteStorage, WriteTransient,
},
render::Render,
FrameName, TransactionGasLog,
Expand Down Expand Up @@ -191,12 +191,28 @@ impl WriteTransient {
}
}

impl Dependency {
fn to_erased(&self) -> Node<InternalGas> {
Node::new(format!("{}", Render(&self.id)), self.cost)
}
}

impl ExecutionAndIOCosts {
/// Convert the gas log into a type-erased representation.
pub fn to_erased(&self) -> TypeErasedExecutionAndIoCosts {
let mut nodes = vec![];

nodes.push(Node::new("intrinsic", self.intrinsic_cost));

if !self.dependencies.is_empty() {
let deps = Node::new_with_children(
"dependencies",
0,
self.dependencies.iter().map(|dep| dep.to_erased()),
);
nodes.push(deps);
}

nodes.push(self.call_graph.to_erased());

let writes = Node::new_with_children(
Expand Down
4 changes: 4 additions & 0 deletions aptos-move/aptos-gas-profiling/src/flamegraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ impl ExecutionAndIOCosts {
path: &'a mut Vec<String>,
}

for dep in &self.dependencies {
lines.push(format!("dependencies;{}", Render(&dep.id)), dep.cost)
}

impl<'a> Rec<'a> {
fn visit(&mut self, frame: &CallFrame) {
self.path.push(format!("{}", frame.name));
Expand Down
18 changes: 16 additions & 2 deletions aptos-move/aptos-gas-profiling/src/log.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0

use aptos_gas_algebra::{Fee, GasScalingFactor, InternalGas};
use aptos_gas_algebra::{Fee, GasScalingFactor, InternalGas, NumBytes};
use aptos_types::state_store::state_key::StateKey;
use move_binary_format::{file_format::CodeOffset, file_format_common::Opcodes};
use move_core_types::{
Expand Down Expand Up @@ -94,18 +94,28 @@ pub struct EventStorage {
pub cost: Fee,
}

#[derive(Debug, Clone)]
/// Struct representing the cost of a dependency.
pub struct Dependency {
pub id: ModuleId,
pub size: NumBytes,
pub cost: InternalGas,
}

/// Struct containing all execution and io costs.
#[derive(Debug)]
pub struct ExecutionAndIOCosts {
pub gas_scaling_factor: GasScalingFactor,
pub total: InternalGas,

pub intrinsic_cost: InternalGas,
pub dependencies: Vec<Dependency>,
pub call_graph: CallFrame,
pub write_set_transient: Vec<WriteTransient>,
}

#[derive(Debug)]
// Struct containing all types of storage fees.
/// Struct containing all types of storage fees.
pub struct StorageFees {
pub total: Fee,
pub total_refund: Fee,
Expand Down Expand Up @@ -219,6 +229,10 @@ impl ExecutionAndIOCosts {

total += self.intrinsic_cost;

for dep in &self.dependencies {
total += dep.cost;
}

for op in self.gas_events() {
match op {
Loc(..) | Call(..) => (),
Expand Down
Loading

0 comments on commit a2cecd6

Please sign in to comment.