Skip to content

Commit

Permalink
Gas metering for the transaction size (#1858)
Browse files Browse the repository at this point in the history
* Gas metering for the tx size

* use the exists struct filed as the gas parameter

* cargo fmt

* use one field as the gas parameter

---------

Co-authored-by: jolestar <[email protected]>
  • Loading branch information
steelgeek091 and jolestar authored Jun 11, 2024
1 parent 6b8af76 commit 6fe3b21
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
2 changes: 1 addition & 1 deletion moveos/moveos/src/gas/parameters/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use crate::gas::table::StorageGasParameter;

crate::gas::native::define_gas_parameters_for_natives!(StorageGasParameter, "storage", [
[.io_read_price, "io_read_price", (5 + 1) * MUL],
[.storage_fee_per_transaction_byte, "storage_fee_per_transaction_byte", (5 + 1) * MUL],
[.storage_fee_per_event_byte, "storage_fee_per_event_byte", (5 + 1) * MUL],
[.storage_fee_per_op_new_byte, "storage_fee_per_op_new_byte", (5 + 1) * MUL],
[.storage_fee_per_op_modify_byte, "storage_fee_per_op_modify_byte", (5 + 1) * MUL],
[.storage_fee_per_op_delete, "storage_fee_per_op_delete", (5 + 1) * MUL],
[.storage_fee_per_transaction_byte, "storage_fee_per_transaction_byte", 20],
]);
45 changes: 25 additions & 20 deletions moveos/moveos/src/gas/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ pub const STACK_SIZE_TIER_DEFAULT: u64 = 1;

pub static ZERO_COST_SCHEDULE: Lazy<CostTable> = Lazy::new(zero_cost_schedule);

#[derive(Clone, Debug, Default, Serialize, PartialEq, Eq, Deserialize)]
#[derive(Clone, Debug, Serialize, PartialEq, Eq, Deserialize)]
pub struct StorageGasParameter {
pub io_read_price: u64,
pub storage_fee_per_transaction_byte: u64,
pub storage_fee_per_event_byte: u64,
pub storage_fee_per_op_new_byte: u64,
pub storage_fee_per_op_modify_byte: u64,
pub storage_fee_per_op_delete: u64,
pub io_read_price: InternalGas,
pub storage_fee_per_transaction_byte: InternalGas,
pub storage_fee_per_event_byte: InternalGas,
pub storage_fee_per_op_new_byte: InternalGas,
pub storage_fee_per_op_modify_byte: InternalGas,
pub storage_fee_per_op_delete: InternalGas,
}

#[derive(Clone, Debug, Serialize, PartialEq, Eq, Deserialize)]
Expand Down Expand Up @@ -105,12 +105,12 @@ impl AbstractValueSizeGasParameter {
impl StorageGasParameter {
pub fn zeros() -> Self {
Self {
io_read_price: 0,
storage_fee_per_transaction_byte: 0,
storage_fee_per_event_byte: 0,
storage_fee_per_op_new_byte: 0,
storage_fee_per_op_modify_byte: 0,
storage_fee_per_op_delete: 0,
io_read_price: 0.into(),
storage_fee_per_transaction_byte: 0.into(),
storage_fee_per_event_byte: 0.into(),
storage_fee_per_op_new_byte: 0.into(),
storage_fee_per_op_modify_byte: 0.into(),
storage_fee_per_op_delete: 0.into(),
}
}
}
Expand Down Expand Up @@ -493,7 +493,7 @@ pub fn zero_cost_schedule() -> CostTable {
instruction_tiers: zero_tier.clone(),
stack_size_tiers: zero_tier.clone(),
stack_height_tiers: zero_tier,
storage_gas_parameter: StorageGasParameter::default(),
storage_gas_parameter: StorageGasParameter::zeros(),
instruction_gas_parameter: InstructionParameter::zeros(),
abstract_value_parameter: AbstractValueSizeGasParameter::zeros(),
}
Expand Down Expand Up @@ -738,19 +738,24 @@ impl ClassifiedGasMeter for MoveOSGasMeter {

// fn charge_io_read(&mut self) {}

fn charge_io_write(&mut self, data_size: u64) -> PartialVMResult<()> {
fn charge_io_write(&mut self, tx_size: u64) -> PartialVMResult<()> {
if !self.charge {
return Ok(());
}

let fee = self
let tx_gas_parameter: u64 = self
.cost_table
.storage_gas_parameter
.storage_fee_per_transaction_byte
* data_size;
let new_value = self.storage_gas_used.borrow().add(InternalGas::from(fee));
*self.storage_gas_used.borrow_mut() = new_value;
self.deduct_gas(InternalGas::from(fee))
.into();

match tx_size.checked_mul(tx_gas_parameter) {
None => {
self.gas_left = InternalGas::from(0);
Err(PartialVMError::new(StatusCode::OUT_OF_GAS))
}
Some(final_gas) => self.charge_v1(InternalGas::from(final_gas)),
}
}
//TODO cleanup
// fn charge_event(&mut self, events: &[TransactionEvent]) -> PartialVMResult<()> {
Expand Down
5 changes: 3 additions & 2 deletions moveos/moveos/src/moveos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use std::sync::Arc;

use crate::gas::table::{
get_gas_schedule_entries, initial_cost_schedule, CostTable, MoveOSGasMeter,
get_gas_schedule_entries, initial_cost_schedule, ClassifiedGasMeter, CostTable, MoveOSGasMeter,
};
use crate::vm::moveos_vm::{MoveOSSession, MoveOSVM};
use anyhow::{bail, Result};
Expand Down Expand Up @@ -281,7 +281,8 @@ impl MoveOS {
let system_env = ctx.map.clone();

let cost_table = self.load_cost_table(&root)?;
let gas_meter = MoveOSGasMeter::new(cost_table, ctx.max_gas_amount);
let mut gas_meter = MoveOSGasMeter::new(cost_table, ctx.max_gas_amount);
gas_meter.charge_io_write(ctx.tx_size)?;

// Temporary behavior, will enable this in the future.
// gas_meter.charge_io_write(ctx.tx_size)?;
Expand Down

0 comments on commit 6fe3b21

Please sign in to comment.