From 0c5977d50bfd1a36f2ce796cea2a83624d767962 Mon Sep 17 00:00:00 2001 From: steelgeek091 Date: Fri, 15 Nov 2024 18:51:33 +0800 Subject: [PATCH 1/3] Tired io write is controlled by feature --- crates/rooch/src/tx_runner.rs | 14 +++++-- .../src/moveos_std/onchain_features.rs | 4 ++ moveos/moveos/src/gas/table.rs | 11 +++++- moveos/moveos/src/moveos.rs | 38 +++++++++++++++++-- .../src/vm/unit_tests/vm_arguments_tests.rs | 8 ++-- 5 files changed, 61 insertions(+), 14 deletions(-) diff --git a/crates/rooch/src/tx_runner.rs b/crates/rooch/src/tx_runner.rs index 7bfdfbbb44..50bc67e320 100644 --- a/crates/rooch/src/tx_runner.rs +++ b/crates/rooch/src/tx_runner.rs @@ -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( @@ -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); diff --git a/moveos/moveos-types/src/moveos_std/onchain_features.rs b/moveos/moveos-types/src/moveos_std/onchain_features.rs index 14d926fac8..d33a536d48 100644 --- a/moveos/moveos-types/src/moveos_std/onchain_features.rs +++ b/moveos/moveos-types/src/moveos_std/onchain_features.rs @@ -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 { diff --git a/moveos/moveos/src/gas/table.rs b/moveos/moveos/src/gas/table.rs index cda17c472b..70ad4f64c9 100644 --- a/moveos/moveos/src/gas/table.rs +++ b/moveos/moveos/src/gas/table.rs @@ -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>, storage_gas_used: Rc>, @@ -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); @@ -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, @@ -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, @@ -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 => { diff --git a/moveos/moveos/src/moveos.rs b/moveos/moveos/src/moveos.rs index c17ebfe27a..b4794ba7c4 100644 --- a/moveos/moveos/src/moveos.rs +++ b/moveos/moveos/src/moveos.rs @@ -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}; @@ -235,7 +235,7 @@ impl MoveOS { pub fn verify(&self, tx: MoveOSTransaction) -> VMResult { 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); @@ -273,8 +273,20 @@ 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 mut has_io_tired_write_feature = match feature_store_opt { + None => false, + Some(feature_store) => feature_store.has_value_size_gas_feature(), + }; + + if is_system_call { + has_io_tired_write_feature = false; + } + 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); @@ -385,7 +397,25 @@ impl MoveOS { return FunctionResult::err(e); } }; - let mut gas_meter = MoveOSGasMeter::new(cost_table, tx_context.max_gas_amount); + + let is_system_call = tx_context.is_system_call(); + + let feature_resolver = RootObjectResolver::new(root.clone(), &self.db); + let feature_store_opt = load_feature_store_object(&feature_resolver); + let mut has_io_tired_write_feature = match feature_store_opt { + None => false, + Some(feature_store) => feature_store.has_value_size_gas_feature(), + }; + + if is_system_call { + has_io_tired_write_feature = false; + } + + 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 diff --git a/moveos/moveos/src/vm/unit_tests/vm_arguments_tests.rs b/moveos/moveos/src/vm/unit_tests/vm_arguments_tests.rs index 0ab2b8aebe..7d4af02da3 100644 --- a/moveos/moveos/src/vm/unit_tests/vm_arguments_tests.rs +++ b/moveos/moveos/src/vm/unit_tests/vm_arguments_tests.rs @@ -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); @@ -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); @@ -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( @@ -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 From 5c9729e083d0c47ecafd77fd1309c6a82412d803 Mon Sep 17 00:00:00 2001 From: steelgeek091 Date: Fri, 15 Nov 2024 19:13:33 +0800 Subject: [PATCH 2/3] remove the if statement --- moveos/moveos/src/moveos.rs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/moveos/moveos/src/moveos.rs b/moveos/moveos/src/moveos.rs index b4794ba7c4..cd6c67b960 100644 --- a/moveos/moveos/src/moveos.rs +++ b/moveos/moveos/src/moveos.rs @@ -280,10 +280,6 @@ impl MoveOS { Some(feature_store) => feature_store.has_value_size_gas_feature(), }; - if is_system_call { - has_io_tired_write_feature = false; - } - let cost_table = self.load_cost_table(&root)?; let mut gas_meter = MoveOSGasMeter::new(cost_table, ctx.max_gas_amount, has_io_tired_write_feature); @@ -407,10 +403,6 @@ impl MoveOS { Some(feature_store) => feature_store.has_value_size_gas_feature(), }; - if is_system_call { - has_io_tired_write_feature = false; - } - let mut gas_meter = MoveOSGasMeter::new( cost_table, tx_context.max_gas_amount, From ca5c211210ea4912ffd7ad86ab182eed1bb3a295 Mon Sep 17 00:00:00 2001 From: steelgeek091 Date: Fri, 15 Nov 2024 19:20:52 +0800 Subject: [PATCH 3/3] cargo clippy --- moveos/moveos/src/moveos.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/moveos/moveos/src/moveos.rs b/moveos/moveos/src/moveos.rs index cd6c67b960..7681211d1f 100644 --- a/moveos/moveos/src/moveos.rs +++ b/moveos/moveos/src/moveos.rs @@ -275,7 +275,7 @@ impl MoveOS { let feature_resolver = RootObjectResolver::new(root.clone(), &self.db); let feature_store_opt = load_feature_store_object(&feature_resolver); - let mut has_io_tired_write_feature = match feature_store_opt { + let has_io_tired_write_feature = match feature_store_opt { None => false, Some(feature_store) => feature_store.has_value_size_gas_feature(), }; @@ -394,11 +394,9 @@ impl MoveOS { } }; - let is_system_call = tx_context.is_system_call(); - let feature_resolver = RootObjectResolver::new(root.clone(), &self.db); let feature_store_opt = load_feature_store_object(&feature_resolver); - let mut has_io_tired_write_feature = match feature_store_opt { + let has_io_tired_write_feature = match feature_store_opt { None => false, Some(feature_store) => feature_store.has_value_size_gas_feature(), };