Skip to content

Commit d78bb49

Browse files
committed
[gas] abstract gas algebra part 1 -- instructions & operations
1 parent a927af2 commit d78bb49

File tree

19 files changed

+1004
-468
lines changed

19 files changed

+1004
-468
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

aptos-move/aptos-debugger/src/lib.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
use anyhow::{format_err, Result};
55
use aptos_gas::{
6-
AbstractValueSizeGasParameters, ChangeSetConfigs, NativeGasParameters, StandardGasMeter,
7-
LATEST_GAS_FEATURE_VERSION,
6+
AbstractValueSizeGasParameters, ChangeSetConfigs, NativeGasParameters, StandardGasAlgebra,
7+
StandardGasMeter, LATEST_GAS_FEATURE_VERSION,
88
};
99
use aptos_gas_profiling::{GasProfiler, TransactionGasLog};
1010
use aptos_memory_usage_tracker::MemoryTrackedGasMeter;
@@ -80,12 +80,13 @@ impl AptosDebugger {
8080
&txn,
8181
&log_context,
8282
|gas_feature_version, gas_params, storage_gas_params, balance| {
83-
let gas_meter = MemoryTrackedGasMeter::new(StandardGasMeter::new(
84-
gas_feature_version,
85-
gas_params,
86-
storage_gas_params,
87-
balance,
88-
));
83+
let gas_meter =
84+
MemoryTrackedGasMeter::new(StandardGasMeter::new(StandardGasAlgebra::new(
85+
gas_feature_version,
86+
gas_params,
87+
storage_gas_params,
88+
balance,
89+
)));
8990
let gas_profiler = match txn.payload() {
9091
TransactionPayload::Script(_) => GasProfiler::new_script(gas_meter),
9192
TransactionPayload::EntryFunction(entry_func) => GasProfiler::new_function(

aptos-move/aptos-gas-profiling/src/profiler.rs

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::log::{
55
CallFrame, EventStorage, ExecutionAndIOCosts, ExecutionGasEvent, FrameName, StorageFees,
66
TransactionGasLog, WriteOpType, WriteStorage, WriteTransient,
77
};
8-
use aptos_gas::{AptosGasMeter, AptosGasParameters, Fee, Gas, GasScalingFactor};
8+
use aptos_gas::{AptosGasMeter, Fee};
99
use aptos_types::{
1010
contract_event::ContractEvent, state_store::state_key::StateKey, write_set::WriteOp,
1111
};
@@ -474,16 +474,10 @@ impl<G> AptosGasMeter for GasProfiler<G>
474474
where
475475
G: AptosGasMeter,
476476
{
477-
delegate! {
478-
fn feature_version(&self) -> u64;
479-
480-
fn gas_params(&self) -> &AptosGasParameters;
481-
482-
fn balance(&self) -> Gas;
477+
type Algebra = G::Algebra;
483478

484-
fn gas_unit_scaling_factor(&self) -> GasScalingFactor;
485-
486-
fn io_gas_per_write(&self, key: &StateKey, op: &WriteOp) -> InternalGas;
479+
delegate! {
480+
fn algebra(&self) -> &Self::Algebra;
487481

488482
fn storage_fee_per_write(&self, key: &StateKey, op: &WriteOp) -> Fee;
489483

@@ -492,20 +486,10 @@ where
492486
fn storage_discount_for_events(&self, total_cost: Fee) -> Fee;
493487

494488
fn storage_fee_for_transaction_storage(&self, txn_size: NumBytes) -> Fee;
495-
496-
fn execution_gas_used(&self) -> Gas;
497-
498-
fn io_gas_used(&self) -> Gas;
499-
500-
fn storage_fee_used_in_gas_units(&self) -> Gas;
501-
502-
fn storage_fee_used(&self) -> Fee;
503489
}
504490

505491
delegate_mut! {
506-
fn charge_execution(&mut self, amount: InternalGas) -> PartialVMResult<()>;
507-
508-
fn charge_io(&mut self, amount: InternalGas) -> PartialVMResult<()>;
492+
fn algebra_mut(&mut self) -> &mut Self::Algebra;
509493

510494
fn charge_storage_fee(
511495
&mut self,
@@ -514,22 +498,17 @@ where
514498
) -> PartialVMResult<()>;
515499
}
516500

517-
fn charge_io_gas_for_write_set<'a>(
518-
&mut self,
519-
ops: impl IntoIterator<Item = (&'a StateKey, &'a WriteOp)>,
520-
) -> VMResult<()> {
521-
for (key, op) in ops {
522-
let cost = self.io_gas_per_write(key, op);
523-
self.total_exec_io += cost;
524-
self.write_set_transient.push(WriteTransient {
525-
key: key.clone(),
526-
cost,
527-
op_type: write_op_type(op),
528-
});
529-
self.charge_io(cost)
530-
.map_err(|e| e.finish(Location::Undefined))?;
531-
}
532-
Ok(())
501+
fn charge_io_gas_for_write(&mut self, key: &StateKey, op: &WriteOp) -> VMResult<()> {
502+
let (cost, res) = self.delegate_charge(|base| base.charge_io_gas_for_write(key, op));
503+
504+
self.total_exec_io += cost;
505+
self.write_set_transient.push(WriteTransient {
506+
key: key.clone(),
507+
cost,
508+
op_type: write_op_type(op),
509+
});
510+
511+
res
533512
}
534513

535514
fn charge_storage_fee_for_all<'a>(

aptos-move/aptos-gas/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ aptos-types = { workspace = true }
2424
aptos-vm-types = { workspace = true }
2525
bcs = { workspace = true }
2626
clap = { workspace = true }
27+
either = { workspace = true }
2728
move-binary-format = { workspace = true }
2829
move-core-types = { workspace = true }
2930
move-model = { workspace = true }
3031
move-table-extension = { workspace = true }
3132
move-vm-types = { workspace = true }
33+
paste = { workspace = true }
3234

3335
[dev-dependencies]
3436
tempfile = { workspace = true }

0 commit comments

Comments
 (0)