From 35145d8efd211f3e9bf59ad1781a8583035970c2 Mon Sep 17 00:00:00 2001 From: Mirko Zichichi Date: Wed, 30 Oct 2024 11:23:17 +0100 Subject: [PATCH] chore(iota-types/gas): Remove old gas models (#3766) * chore(iota-types/gas): remove old cost tables * refactor(iota-types/gas): remove dont_charge_budget_on_storage_oog * refactor(iota-types/gas): remove gas_price_too_high * refactor(iota-types/gas): remove charge_input_as_memory * refactor(iota-types/gas): remove use_legacy_abstract_size * refactor(iota-types/gas): remove charge_upgrades * chore(iota-protocol-config): move gas model version from 8 to 1 * fix(iota-types/gas): max_gas_price comparison --------- Co-authored-by: Alexander Sporn --- crates/iota-open-rpc/spec/openrpc.json | 2 +- crates/iota-protocol-config/src/lib.rs | 2 +- ...tocol_config__test__Mainnet_version_1.snap | 2 +- ...tocol_config__test__Testnet_version_1.snap | 2 +- ...iota_protocol_config__test__version_1.snap | 2 +- crates/iota-types/src/gas.rs | 8 +- .../src/gas_model/gas_predicates.rs | 48 +---- crates/iota-types/src/gas_model/tables.rs | 198 +----------------- .../src/account_universe/transfer_gen.rs | 2 +- .../latest/iota-adapter/src/gas_charger.rs | 38 +--- .../v0/iota-adapter/src/gas_charger.rs | 38 +--- 11 files changed, 27 insertions(+), 315 deletions(-) diff --git a/crates/iota-open-rpc/spec/openrpc.json b/crates/iota-open-rpc/spec/openrpc.json index 10d0f8d5f34..cd116254fbe 100644 --- a/crates/iota-open-rpc/spec/openrpc.json +++ b/crates/iota-open-rpc/spec/openrpc.json @@ -1603,7 +1603,7 @@ "u64": "1" }, "gas_model_version": { - "u64": "8" + "u64": "1" }, "gas_rounding_step": { "u64": "1000" diff --git a/crates/iota-protocol-config/src/lib.rs b/crates/iota-protocol-config/src/lib.rs index 8b2f8cd07d7..9a235a3c492 100644 --- a/crates/iota-protocol-config/src/lib.rs +++ b/crates/iota-protocol-config/src/lib.rs @@ -1266,7 +1266,7 @@ impl ProtocolConfig { obj_access_cost_verify_per_byte: Some(200), obj_data_cost_refundable: Some(100), obj_metadata_cost_non_refundable: Some(50), - gas_model_version: Some(8), + gas_model_version: Some(1), storage_rebate_rate: Some(10000), // Change reward slashing rate to 100%. reward_slashing_rate: Some(10000), diff --git a/crates/iota-protocol-config/src/snapshots/iota_protocol_config__test__Mainnet_version_1.snap b/crates/iota-protocol-config/src/snapshots/iota_protocol_config__test__Mainnet_version_1.snap index 40ac4f18320..4895285e864 100644 --- a/crates/iota-protocol-config/src/snapshots/iota_protocol_config__test__Mainnet_version_1.snap +++ b/crates/iota-protocol-config/src/snapshots/iota_protocol_config__test__Mainnet_version_1.snap @@ -84,7 +84,7 @@ obj_access_cost_read_per_byte: 15 obj_access_cost_mutate_per_byte: 40 obj_access_cost_delete_per_byte: 40 obj_access_cost_verify_per_byte: 200 -gas_model_version: 8 +gas_model_version: 1 obj_data_cost_refundable: 100 obj_metadata_cost_non_refundable: 50 storage_rebate_rate: 10000 diff --git a/crates/iota-protocol-config/src/snapshots/iota_protocol_config__test__Testnet_version_1.snap b/crates/iota-protocol-config/src/snapshots/iota_protocol_config__test__Testnet_version_1.snap index 1a37c689667..226eb542a00 100644 --- a/crates/iota-protocol-config/src/snapshots/iota_protocol_config__test__Testnet_version_1.snap +++ b/crates/iota-protocol-config/src/snapshots/iota_protocol_config__test__Testnet_version_1.snap @@ -84,7 +84,7 @@ obj_access_cost_read_per_byte: 15 obj_access_cost_mutate_per_byte: 40 obj_access_cost_delete_per_byte: 40 obj_access_cost_verify_per_byte: 200 -gas_model_version: 8 +gas_model_version: 1 obj_data_cost_refundable: 100 obj_metadata_cost_non_refundable: 50 storage_rebate_rate: 10000 diff --git a/crates/iota-protocol-config/src/snapshots/iota_protocol_config__test__version_1.snap b/crates/iota-protocol-config/src/snapshots/iota_protocol_config__test__version_1.snap index 1863f2f5d1d..c1ca58f0173 100644 --- a/crates/iota-protocol-config/src/snapshots/iota_protocol_config__test__version_1.snap +++ b/crates/iota-protocol-config/src/snapshots/iota_protocol_config__test__version_1.snap @@ -88,7 +88,7 @@ obj_access_cost_read_per_byte: 15 obj_access_cost_mutate_per_byte: 40 obj_access_cost_delete_per_byte: 40 obj_access_cost_verify_per_byte: 200 -gas_model_version: 8 +gas_model_version: 1 obj_data_cost_refundable: 100 obj_metadata_cost_non_refundable: 50 storage_rebate_rate: 10000 diff --git a/crates/iota-types/src/gas.rs b/crates/iota-types/src/gas.rs index 39b7bd9edff..8df712a0797 100644 --- a/crates/iota-types/src/gas.rs +++ b/crates/iota-types/src/gas.rs @@ -19,10 +19,7 @@ pub mod checked { ObjectID, effects::{TransactionEffects, TransactionEffectsAPI}, error::{ExecutionError, IotaResult, UserInputError, UserInputResult}, - gas_model::{ - gas_predicates::gas_price_too_high, gas_v1::IotaGasStatus as IotaGasStatusV1, - tables::GasStatus, - }, + gas_model::{gas_v1::IotaGasStatus as IotaGasStatusV1, tables::GasStatus}, iota_serde::{BigInt, Readable}, object::Object, transaction::ObjectReadResult, @@ -78,8 +75,7 @@ pub mod checked { } .into()); } - if gas_price_too_high(config.gas_model_version()) && gas_price >= config.max_gas_price() - { + if gas_price > config.max_gas_price() { return Err(UserInputError::GasPriceTooHigh { max_gas_price: config.max_gas_price(), } diff --git a/crates/iota-types/src/gas_model/gas_predicates.rs b/crates/iota-types/src/gas_model/gas_predicates.rs index 8117c30cb34..9dbbe50c16a 100644 --- a/crates/iota-types/src/gas_model/gas_predicates.rs +++ b/crates/iota-types/src/gas_model/gas_predicates.rs @@ -6,51 +6,9 @@ // Predicates and utility functions based on gas versions. // -use crate::gas_model::{ - tables::{ - initial_cost_schedule_v1, initial_cost_schedule_v2, initial_cost_schedule_v3, - initial_cost_schedule_v4, initial_cost_schedule_v5, - }, - units_types::CostTable, -}; - -/// If true, do not charge the entire budget on storage OOG -pub fn dont_charge_budget_on_storage_oog(gas_model_version: u64) -> bool { - gas_model_version >= 4 -} - -/// If true, enable the check for gas price too high -pub fn gas_price_too_high(gas_model_version: u64) -> bool { - gas_model_version >= 4 -} - -/// If true, input object bytes are treated as memory allocated in Move and -/// charged according to the bucket they end up in. -pub fn charge_input_as_memory(gas_model_version: u64) -> bool { - gas_model_version == 4 -} - -/// If true, calculate value sizes using the legacy size calculation. -pub fn use_legacy_abstract_size(gas_model_version: u64) -> bool { - gas_model_version <= 7 -} - -// If true, charge differently for package upgrades -pub fn charge_upgrades(gas_model_version: u64) -> bool { - gas_model_version >= 7 -} +use crate::gas_model::{tables::initial_cost_schedule_v1, units_types::CostTable}; // Return the version supported cost table -pub fn cost_table_for_version(gas_model: u64) -> CostTable { - if gas_model <= 3 { - initial_cost_schedule_v1() - } else if gas_model == 4 { - initial_cost_schedule_v2() - } else if gas_model == 5 { - initial_cost_schedule_v3() - } else if gas_model <= 7 { - initial_cost_schedule_v4() - } else { - initial_cost_schedule_v5() - } +pub fn cost_table_for_version(_gas_model: u64) -> CostTable { + initial_cost_schedule_v1() } diff --git a/crates/iota-types/src/gas_model/tables.rs b/crates/iota-types/src/gas_model/tables.rs index c4da744fb7b..749a97c7e64 100644 --- a/crates/iota-types/src/gas_model/tables.rs +++ b/crates/iota-types/src/gas_model/tables.rs @@ -18,7 +18,6 @@ use move_vm_types::{ }; use once_cell::sync::Lazy; -use super::gas_predicates::{charge_input_as_memory, use_legacy_abstract_size}; use crate::gas_model::units_types::{CostTable, Gas, GasCost}; /// VM flat fee @@ -125,7 +124,7 @@ impl GasStatus { /// system code that does not have to charge the user. pub fn new_unmetered() -> Self { Self { - gas_model_version: 4, + gas_model_version: 1, gas_left: InternalGas::new(0), gas_price: 1, initial_budget: InternalGas::new(0), @@ -322,21 +321,12 @@ impl GasStatus { // As more bytes are read throughout the computation the cost per bytes is // increased. pub fn charge_bytes(&mut self, size: usize, cost_per_byte: u64) -> PartialVMResult<()> { - let computation_cost = if charge_input_as_memory(self.gas_model_version) { - self.increase_stack_size(size as u64)?; - self.stack_size_current_tier_mult * size as u64 * cost_per_byte - } else { - size as u64 * cost_per_byte - }; + let computation_cost = size as u64 * cost_per_byte; self.deduct_units(computation_cost) } fn abstract_memory_size(&self, val: impl ValueView) -> AbstractMemorySize { - if use_legacy_abstract_size(self.gas_model_version) { - val.legacy_abstract_memory_size() - } else { - val.abstract_memory_size() - } + val.abstract_memory_size() } pub fn gas_price(&self) -> u64 { @@ -717,186 +707,6 @@ pub fn unit_cost_schedule() -> CostTable { } pub fn initial_cost_schedule_v1() -> CostTable { - let instruction_tiers: BTreeMap = vec![ - (0, 1), - (3000, 2), - (6000, 3), - (8000, 5), - (9000, 9), - (9500, 16), - (10000, 29), - (10500, 50), - ] - .into_iter() - .collect(); - - let stack_height_tiers: BTreeMap = vec![ - (0, 1), - (400, 2), - (800, 3), - (1200, 5), - (1500, 9), - (1800, 16), - (2000, 29), - (2200, 50), - ] - .into_iter() - .collect(); - - let stack_size_tiers: BTreeMap = vec![ - (0, 1), - (2000, 2), - (5000, 3), - (8000, 5), - (10000, 9), - (11000, 16), - (11500, 29), - (11500, 50), - ] - .into_iter() - .collect(); - - CostTable { - instruction_tiers, - stack_size_tiers, - stack_height_tiers, - } -} - -pub fn initial_cost_schedule_v2() -> CostTable { - let instruction_tiers: BTreeMap = vec![ - (0, 1), - (3000, 2), - (6000, 3), - (8000, 5), - (9000, 9), - (9500, 16), - (10000, 29), - (10500, 50), - (12000, 150), - (15000, 250), - ] - .into_iter() - .collect(); - - let stack_height_tiers: BTreeMap = vec![ - (0, 1), - (400, 2), - (800, 3), - (1200, 5), - (1500, 9), - (1800, 16), - (2000, 29), - (2200, 50), - (3000, 150), - (5000, 250), - ] - .into_iter() - .collect(); - - let stack_size_tiers: BTreeMap = vec![ - (0, 1), - (2000, 2), - (5000, 3), - (8000, 5), - (10000, 9), - (11000, 16), - (11500, 29), - (11500, 50), - (15000, 150), - (20000, 250), - ] - .into_iter() - .collect(); - - CostTable { - instruction_tiers, - stack_size_tiers, - stack_height_tiers, - } -} - -pub fn initial_cost_schedule_v3() -> CostTable { - let instruction_tiers: BTreeMap = vec![ - (0, 1), - (3000, 2), - (6000, 3), - (8000, 5), - (9000, 9), - (9500, 16), - (10000, 29), - (10500, 50), - (15000, 100), - ] - .into_iter() - .collect(); - - let stack_height_tiers: BTreeMap = vec![ - (0, 1), - (400, 2), - (800, 3), - (1200, 5), - (1500, 9), - (1800, 16), - (2000, 29), - (2200, 50), - (5000, 100), - ] - .into_iter() - .collect(); - - let stack_size_tiers: BTreeMap = vec![ - (0, 1), - (2000, 2), - (5000, 3), - (8000, 5), - (10000, 9), - (11000, 16), - (11500, 29), - (11500, 50), - (20000, 100), - ] - .into_iter() - .collect(); - - CostTable { - instruction_tiers, - stack_size_tiers, - stack_height_tiers, - } -} - -pub fn initial_cost_schedule_v4() -> CostTable { - let instruction_tiers: BTreeMap = vec![ - (0, 1), - (20_000, 2), - (50_000, 10), - (100_000, 50), - (200_000, 100), - ] - .into_iter() - .collect(); - - let stack_height_tiers: BTreeMap = - vec![(0, 1), (1_000, 2), (10_000, 10)].into_iter().collect(); - - let stack_size_tiers: BTreeMap = vec![ - (0, 1), - (100_000, 2), // ~100K - (500_000, 5), // ~500K - (1_000_000, 100), // ~1M - ] - .into_iter() - .collect(); - - CostTable { - instruction_tiers, - stack_size_tiers, - stack_height_tiers, - } -} - -pub fn initial_cost_schedule_v5() -> CostTable { let instruction_tiers: BTreeMap = vec![ (0, 1), (20_000, 2), @@ -934,7 +744,7 @@ pub fn initial_cost_schedule_v5() -> CostTable { // instead we perform this translation from our gas units and cost schedule to // the one expected by the Move unit tests. pub fn initial_cost_schedule_for_unit_tests() -> move_vm_test_utils::gas_schedule::CostTable { - let table = initial_cost_schedule_v5(); + let table = initial_cost_schedule_v1(); move_vm_test_utils::gas_schedule::CostTable { instruction_tiers: table.instruction_tiers.into_iter().collect(), stack_height_tiers: table.stack_height_tiers.into_iter().collect(), diff --git a/crates/transaction-fuzzer/src/account_universe/transfer_gen.rs b/crates/transaction-fuzzer/src/account_universe/transfer_gen.rs index 255445d50c5..7bdeb9adfdc 100644 --- a/crates/transaction-fuzzer/src/account_universe/transfer_gen.rs +++ b/crates/transaction-fuzzer/src/account_universe/transfer_gen.rs @@ -365,7 +365,7 @@ impl RunInfo { let gas_budget_too_low = p2p.gas < PROTOCOL_CONFIG.base_tx_cost_fixed() * p2p.gas_price; let not_enough_gas = p2p.gas < p2p_success_gas(p2p.gas_price); let gas_price_too_low = p2p.gas_price < rgp; - let gas_price_too_high = p2p.gas_price >= PROTOCOL_CONFIG.max_gas_price(); + let gas_price_too_high = p2p.gas_price > PROTOCOL_CONFIG.max_gas_price(); let gas_price_greater_than_budget = p2p.gas_price > p2p.gas; let gas_units_too_low = p2p.gas_price > 0 && p2p.gas / p2p.gas_price < INSUFFICIENT_GAS_UNITS_THRESHOLD diff --git a/iota-execution/latest/iota-adapter/src/gas_charger.rs b/iota-execution/latest/iota-adapter/src/gas_charger.rs index 1c07ea77de5..d83e38ba39b 100644 --- a/iota-execution/latest/iota-adapter/src/gas_charger.rs +++ b/iota-execution/latest/iota-adapter/src/gas_charger.rs @@ -15,10 +15,7 @@ pub mod checked { digests::TransactionDigest, error::ExecutionError, gas::{GasCostSummary, IotaGasStatus, deduct_gas}, - gas_model::{ - gas_predicates::{charge_upgrades, dont_charge_budget_on_storage_oog}, - tables::GasStatus, - }, + gas_model::tables::GasStatus, is_system_package, object::Data, }; @@ -35,6 +32,7 @@ pub mod checked { /// version. 2- Isolate all gas accounting into a single implementation. /// Gas objects are not passed around, and they are retrieved from /// this instance. + #[allow(dead_code)] #[derive(Debug)] pub struct GasCharger { tx_digest: TransactionDigest, @@ -66,7 +64,7 @@ pub mod checked { pub fn new_unmetered(tx_digest: TransactionDigest) -> Self { Self { tx_digest, - gas_model_version: 6, // pick any of the latest, it should not matter + gas_model_version: 1, // pick any of the latest, it should not matter gas_coins: vec![], smashed_gas_coin: None, gas_status: IotaGasStatus::new_unmetered(), @@ -223,11 +221,7 @@ pub mod checked { } pub fn charge_upgrade_package(&mut self, size: usize) -> Result<(), ExecutionError> { - if charge_upgrades(self.gas_model_version) { - self.gas_status.charge_publish_package(size) - } else { - Ok(()) - } + self.gas_status.charge_publish_package(size) } pub fn charge_input_objects( @@ -325,11 +319,7 @@ pub mod checked { // charge for storage, however they track storage values to check // for conservation rules if let Some(gas_object_id) = self.smashed_gas_coin { - if dont_charge_budget_on_storage_oog(self.gas_model_version) { - self.handle_storage_and_rebate_v2(temporary_store, execution_result) - } else { - self.handle_storage_and_rebate_v1(temporary_store, execution_result) - } + self.handle_storage_and_rebate(temporary_store, execution_result); let cost_summary = self.gas_status.summary(); let gas_used = cost_summary.net_gas_usage(); @@ -346,23 +336,7 @@ pub mod checked { } } - fn handle_storage_and_rebate_v1( - &mut self, - temporary_store: &mut TemporaryStore<'_>, - execution_result: &mut Result, - ) { - if let Err(err) = self.gas_status.charge_storage_and_rebate() { - self.reset(temporary_store); - self.gas_status.adjust_computation_on_out_of_gas(); - temporary_store.ensure_active_inputs_mutated(); - temporary_store.collect_rebate(self); - if execution_result.is_ok() { - *execution_result = Err(err); - } - } - } - - fn handle_storage_and_rebate_v2( + fn handle_storage_and_rebate( &mut self, temporary_store: &mut TemporaryStore<'_>, execution_result: &mut Result, diff --git a/iota-execution/v0/iota-adapter/src/gas_charger.rs b/iota-execution/v0/iota-adapter/src/gas_charger.rs index 1c07ea77de5..d83e38ba39b 100644 --- a/iota-execution/v0/iota-adapter/src/gas_charger.rs +++ b/iota-execution/v0/iota-adapter/src/gas_charger.rs @@ -15,10 +15,7 @@ pub mod checked { digests::TransactionDigest, error::ExecutionError, gas::{GasCostSummary, IotaGasStatus, deduct_gas}, - gas_model::{ - gas_predicates::{charge_upgrades, dont_charge_budget_on_storage_oog}, - tables::GasStatus, - }, + gas_model::tables::GasStatus, is_system_package, object::Data, }; @@ -35,6 +32,7 @@ pub mod checked { /// version. 2- Isolate all gas accounting into a single implementation. /// Gas objects are not passed around, and they are retrieved from /// this instance. + #[allow(dead_code)] #[derive(Debug)] pub struct GasCharger { tx_digest: TransactionDigest, @@ -66,7 +64,7 @@ pub mod checked { pub fn new_unmetered(tx_digest: TransactionDigest) -> Self { Self { tx_digest, - gas_model_version: 6, // pick any of the latest, it should not matter + gas_model_version: 1, // pick any of the latest, it should not matter gas_coins: vec![], smashed_gas_coin: None, gas_status: IotaGasStatus::new_unmetered(), @@ -223,11 +221,7 @@ pub mod checked { } pub fn charge_upgrade_package(&mut self, size: usize) -> Result<(), ExecutionError> { - if charge_upgrades(self.gas_model_version) { - self.gas_status.charge_publish_package(size) - } else { - Ok(()) - } + self.gas_status.charge_publish_package(size) } pub fn charge_input_objects( @@ -325,11 +319,7 @@ pub mod checked { // charge for storage, however they track storage values to check // for conservation rules if let Some(gas_object_id) = self.smashed_gas_coin { - if dont_charge_budget_on_storage_oog(self.gas_model_version) { - self.handle_storage_and_rebate_v2(temporary_store, execution_result) - } else { - self.handle_storage_and_rebate_v1(temporary_store, execution_result) - } + self.handle_storage_and_rebate(temporary_store, execution_result); let cost_summary = self.gas_status.summary(); let gas_used = cost_summary.net_gas_usage(); @@ -346,23 +336,7 @@ pub mod checked { } } - fn handle_storage_and_rebate_v1( - &mut self, - temporary_store: &mut TemporaryStore<'_>, - execution_result: &mut Result, - ) { - if let Err(err) = self.gas_status.charge_storage_and_rebate() { - self.reset(temporary_store); - self.gas_status.adjust_computation_on_out_of_gas(); - temporary_store.ensure_active_inputs_mutated(); - temporary_store.collect_rebate(self); - if execution_result.is_ok() { - *execution_result = Err(err); - } - } - } - - fn handle_storage_and_rebate_v2( + fn handle_storage_and_rebate( &mut self, temporary_store: &mut TemporaryStore<'_>, execution_result: &mut Result,