Skip to content

Commit

Permalink
chore(iota-types/gas): Remove old gas models (#3766)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
miker83z and alexsporn authored Oct 30, 2024
1 parent e13b151 commit 35145d8
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 315 deletions.
2 changes: 1 addition & 1 deletion crates/iota-open-rpc/spec/openrpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -1603,7 +1603,7 @@
"u64": "1"
},
"gas_model_version": {
"u64": "8"
"u64": "1"
},
"gas_rounding_step": {
"u64": "1000"
Expand Down
2 changes: 1 addition & 1 deletion crates/iota-protocol-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 2 additions & 6 deletions crates/iota-types/src/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
}
Expand Down
48 changes: 3 additions & 45 deletions crates/iota-types/src/gas_model/gas_predicates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
198 changes: 4 additions & 194 deletions crates/iota-types/src/gas_model/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -717,186 +707,6 @@ pub fn unit_cost_schedule() -> CostTable {
}

pub fn initial_cost_schedule_v1() -> CostTable {
let instruction_tiers: BTreeMap<u64, u64> = 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<u64, u64> = 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<u64, u64> = 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<u64, u64> = 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<u64, u64> = 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<u64, u64> = 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<u64, u64> = 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<u64, u64> = 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<u64, u64> = 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<u64, u64> = vec![
(0, 1),
(20_000, 2),
(50_000, 10),
(100_000, 50),
(200_000, 100),
]
.into_iter()
.collect();

let stack_height_tiers: BTreeMap<u64, u64> =
vec![(0, 1), (1_000, 2), (10_000, 10)].into_iter().collect();

let stack_size_tiers: BTreeMap<u64, u64> = 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<u64, u64> = vec![
(0, 1),
(20_000, 2),
Expand Down Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 35145d8

Please sign in to comment.