Skip to content

Commit

Permalink
Control tiered io write functionality through feature flags (#2909)
Browse files Browse the repository at this point in the history
* Tired io write is controlled by feature

* remove the if statement

* cargo clippy
  • Loading branch information
steelgeek091 authored Nov 16, 2024
1 parent 999cfcf commit 91a6c75
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 14 deletions.
14 changes: 10 additions & 4 deletions crates/rooch/src/tx_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ pub fn execute_tx_locally(
let (move_mv, object_runtime, client_resolver, action, cost_table) =
prepare_execute_env(root_object_meta, &client_resolver, tx.clone());

let mut gas_meter =
MoveOSGasMeter::new(cost_table, GasScheduleConfig::CLI_DEFAULT_MAX_GAS_AMOUNT);
let mut gas_meter = MoveOSGasMeter::new(
cost_table,
GasScheduleConfig::CLI_DEFAULT_MAX_GAS_AMOUNT,
true,
);
gas_meter.charge_io_write(tx.tx_size()).unwrap();

let mut moveos_session = MoveOSSession::new(
Expand Down Expand Up @@ -112,8 +115,11 @@ pub fn execute_tx_locally_with_gas_profile(
let (move_mv, object_runtime, client_resolver, action, cost_table) =
prepare_execute_env(root_object_meta, &client_resolver, tx.clone());

let mut gas_meter =
MoveOSGasMeter::new(cost_table, GasScheduleConfig::CLI_DEFAULT_MAX_GAS_AMOUNT);
let mut gas_meter = MoveOSGasMeter::new(
cost_table,
GasScheduleConfig::CLI_DEFAULT_MAX_GAS_AMOUNT,
true,
);
gas_meter.charge_io_write(tx.tx_size()).unwrap();

let mut gas_profiler = new_gas_profiler(tx.clone().action, gas_meter);
Expand Down
4 changes: 4 additions & 0 deletions moveos/moveos-types/src/moveos_std/onchain_features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ impl FeatureStore {
let value = self.entries[byte_index as usize];
byte_index < self.entries.len() as u64 && (value & bit_mask) != 0
}

pub fn has_value_size_gas_feature(&self) -> bool {
self.contains_feature(VALUE_SIZE_GAS_FEATURE)
}
}

impl MoveStructType for FeatureStore {
Expand Down
11 changes: 9 additions & 2 deletions moveos/moveos/src/gas/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,7 @@ pub struct MoveOSGasMeter {
gas_left: InternalGas,
//TODO we do not need to use gas_price in gas meter.
charge: bool,
charge_tired_io_write: bool,

execution_gas_used: Rc<RefCell<InternalGas>>,
storage_gas_used: Rc<RefCell<InternalGas>>,
Expand Down Expand Up @@ -582,7 +583,7 @@ impl MoveOSGasMeter {
///
/// Charge for every operation and fail when there is no more gas to pay for operations.
/// This is the instantiation that must be used when executing a user function.
pub fn new(cost_table: CostTable, budget: u64) -> Self {
pub fn new(cost_table: CostTable, budget: u64, charge_tired_io_write: bool) -> Self {
//assert!(gas_price > 0, "gas price cannot be 0");
//let budget_in_unit = budget / gas_price;
// let gas_left = Self::to_internal_units(budget_in_unit);
Expand All @@ -596,6 +597,7 @@ impl MoveOSGasMeter {
gas_left: InternalGas::from(budget),
cost_table,
charge: true,
charge_tired_io_write,
execution_gas_used: Rc::new(RefCell::new(InternalGas::from(0))),
storage_gas_used: Rc::new(RefCell::new(InternalGas::from(0))),
stack_height_high_water_mark: 0,
Expand All @@ -621,6 +623,7 @@ impl MoveOSGasMeter {
cost_table: ZERO_COST_SCHEDULE.clone(),
gas_left: InternalGas::from(0),
charge: false,
charge_tired_io_write: false,
execution_gas_used: Rc::new(RefCell::new(InternalGas::from(0))),
storage_gas_used: Rc::new(RefCell::new(InternalGas::from(0))),
stack_height_high_water_mark: 0,
Expand Down Expand Up @@ -761,7 +764,11 @@ impl ClassifiedGasMeter for MoveOSGasMeter {
.storage_fee_per_transaction_byte
.into();

let (factor, _) = self.cost_table.io_write_tier(tx_size);
let (factor, _) = if self.charge_tired_io_write {
self.cost_table.io_write_tier(tx_size)
} else {
(1_u64, None)
};

let factor_gas = match tx_gas_parameter.checked_mul(factor) {
None => {
Expand Down
28 changes: 24 additions & 4 deletions moveos/moveos/src/moveos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use moveos_store::config_store::ConfigDBStore;
use moveos_store::event_store::EventDBStore;
use moveos_store::state_store::statedb::StateDBStore;
use moveos_store::transaction_store::TransactionDBStore;
use moveos_store::MoveOSStore;
use moveos_store::{load_feature_store_object, MoveOSStore};
use moveos_types::addresses::MOVEOS_STD_ADDRESS;
use moveos_types::function_return_value::FunctionResult;
use moveos_types::moveos_std::gas_schedule::{GasScheduleConfig, GasScheduleUpdated};
Expand Down Expand Up @@ -235,7 +235,7 @@ impl MoveOS {
pub fn verify(&self, tx: MoveOSTransaction) -> VMResult<VerifiedMoveOSTransaction> {
let MoveOSTransaction { root, ctx, action } = tx;
let cost_table = self.load_cost_table(&root)?;
let mut gas_meter = MoveOSGasMeter::new(cost_table, ctx.max_gas_amount);
let mut gas_meter = MoveOSGasMeter::new(cost_table, ctx.max_gas_amount, false);
gas_meter.set_metering(false);

let resolver = RootObjectResolver::new(root.clone(), &self.db);
Expand Down Expand Up @@ -273,8 +273,16 @@ impl MoveOS {
// So we keep a backup here, and then insert to the TxContext kv store when session respawed.
let system_env = ctx.map.clone();

let feature_resolver = RootObjectResolver::new(root.clone(), &self.db);
let feature_store_opt = load_feature_store_object(&feature_resolver);
let has_io_tired_write_feature = match feature_store_opt {
None => false,
Some(feature_store) => feature_store.has_value_size_gas_feature(),
};

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

let resolver = RootObjectResolver::new(root, &self.db);
Expand Down Expand Up @@ -385,7 +393,19 @@ impl MoveOS {
return FunctionResult::err(e);
}
};
let mut gas_meter = MoveOSGasMeter::new(cost_table, tx_context.max_gas_amount);

let feature_resolver = RootObjectResolver::new(root.clone(), &self.db);
let feature_store_opt = load_feature_store_object(&feature_resolver);
let has_io_tired_write_feature = match feature_store_opt {
None => false,
Some(feature_store) => feature_store.has_value_size_gas_feature(),
};

let mut gas_meter = MoveOSGasMeter::new(
cost_table,
tx_context.max_gas_amount,
has_io_tired_write_feature,
);
gas_meter.set_metering(true);
let resolver = RootObjectResolver::new(root, &self.db);
let mut session = self
Expand Down
8 changes: 4 additions & 4 deletions moveos/moveos/src/vm/unit_tests/vm_arguments_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ fn call_script_with_args_ty_args_signers(
let remote_view = RemoteStore::new();
let ctx = TxContext::random_for_testing_only();
let cost_table = initial_cost_schedule(None);
let mut gas_meter = MoveOSGasMeter::new(cost_table, ctx.max_gas_amount);
let mut gas_meter = MoveOSGasMeter::new(cost_table, ctx.max_gas_amount, false);
gas_meter.set_metering(false);
let mut session = moveos_vm.new_session(&remote_view, ctx, gas_meter);

Expand Down Expand Up @@ -376,7 +376,7 @@ fn call_script_function_with_args_ty_args_signers(
remote_view.add_module(module);
let ctx = TxContext::random_for_testing_only();
let cost_table = initial_cost_schedule(None);
let mut gas_meter = MoveOSGasMeter::new(cost_table, ctx.max_gas_amount);
let mut gas_meter = MoveOSGasMeter::new(cost_table, ctx.max_gas_amount, false);
gas_meter.set_metering(false);
let mut session: crate::vm::moveos_vm::MoveOSSession<'_, '_, RemoteStore, MoveOSGasMeter> =
moveos_vm.new_session(&remote_view, ctx, gas_meter);
Expand Down Expand Up @@ -856,7 +856,7 @@ fn call_missing_item() {
let mut remote_view = RemoteStore::new();
let ctx = TxContext::random_for_testing_only();
let cost_table = initial_cost_schedule(None);
let mut gas_meter = MoveOSGasMeter::new(cost_table, ctx.max_gas_amount);
let mut gas_meter = MoveOSGasMeter::new(cost_table, ctx.max_gas_amount, false);
gas_meter.set_metering(false);
let mut session = moveos_vm.new_session(&remote_view, ctx.clone(), gas_meter);
let func_call = FunctionCall::new(
Expand All @@ -875,7 +875,7 @@ fn call_missing_item() {
// missing function
remote_view.add_module(module);
let cost_table = initial_cost_schedule(None);
let mut gas_meter = MoveOSGasMeter::new(cost_table, ctx.max_gas_amount);
let mut gas_meter = MoveOSGasMeter::new(cost_table, ctx.max_gas_amount, false);
gas_meter.set_metering(false);
let mut session = moveos_vm.new_session(&remote_view, ctx, gas_meter);
let error = session
Expand Down

0 comments on commit 91a6c75

Please sign in to comment.