Skip to main content

iota_types/gas_model/
gas_v1.rs

1// Copyright (c) 2021, Facebook, Inc. and its affiliates
2// Copyright (c) Mysten Labs, Inc.
3// Modifications Copyright (c) 2024 IOTA Stiftung
4// SPDX-License-Identifier: Apache-2.0
5
6pub use checked::*;
7
8#[iota_macros::with_checked_arithmetic]
9mod checked {
10    use iota_protocol_config::*;
11    use iota_sdk_types::gas::GasCostSummary;
12    use move_core_types::vm_status::StatusCode;
13
14    use crate::{
15        ObjectId,
16        error::{ExecutionError, ExecutionErrorKind, UserInputError, UserInputResult},
17        gas::{self, IotaGasStatusAPI},
18        gas_model::{
19            gas_predicates::cost_table_for_version,
20            tables::{GasStatus, ZERO_COST_SCHEDULE},
21            units_types::CostTable,
22        },
23        transaction::ObjectReadResult,
24    };
25
26    /// A bucket defines a range of units that will be priced the same.
27    /// After execution a call to `GasStatus::bucketize` will round the
28    /// computation cost to `cost` for the bucket ([`min`, `max`]) the gas
29    /// used falls into.
30    #[expect(dead_code)]
31    pub(crate) struct ComputationBucket {
32        min: u64,
33        max: u64,
34        cost: u64,
35    }
36
37    impl ComputationBucket {
38        fn new(min: u64, max: u64, cost: u64) -> Self {
39            ComputationBucket { min, max, cost }
40        }
41
42        fn simple(min: u64, max: u64) -> Self {
43            Self::new(min, max, max)
44        }
45    }
46
47    fn get_bucket_cost(table: &[ComputationBucket], computation_cost: u64) -> u64 {
48        for bucket in table {
49            if bucket.max >= computation_cost {
50                return bucket.cost;
51            }
52        }
53        match table.last() {
54            // maybe not a literal here could be better?
55            None => 5_000_000,
56            Some(bucket) => bucket.cost,
57        }
58    }
59
60    // define the bucket table for computation charging
61    // If versioning defines multiple functions and
62    fn computation_bucket(max_bucket_cost: u64) -> Vec<ComputationBucket> {
63        assert!(max_bucket_cost >= 5_000_000);
64        vec![
65            ComputationBucket::simple(0, 1_000),
66            ComputationBucket::simple(1_000, 5_000),
67            ComputationBucket::simple(5_000, 10_000),
68            ComputationBucket::simple(10_000, 20_000),
69            ComputationBucket::simple(20_000, 50_000),
70            ComputationBucket::simple(50_000, 200_000),
71            ComputationBucket::simple(200_000, 1_000_000),
72            ComputationBucket::simple(1_000_000, max_bucket_cost),
73        ]
74    }
75
76    /// Portion of the storage rebate that gets passed on to the transaction
77    /// sender. The remainder will be burned, then re-minted + added to the
78    /// storage fund at the next epoch change
79    fn sender_rebate(storage_rebate: u64, storage_rebate_rate: u64) -> u64 {
80        // we round storage rebate such that `>= x.5` goes to x+1 (rounds up) and
81        // `< x.5` goes to x (truncates). We replicate `f32/64::round()`
82        const BASIS_POINTS: u128 = 10000;
83        (((storage_rebate as u128 * storage_rebate_rate as u128)
84        + (BASIS_POINTS / 2)) // integer rounding adds half of the BASIS_POINTS (denominator)
85        / BASIS_POINTS) as u64
86    }
87
88    /// A list of constant costs of various operations in IOTA.
89    pub struct IotaCostTable {
90        /// A flat fee charged for every transaction. This is also the minimum
91        /// amount of gas charged for a transaction.
92        pub(crate) min_transaction_cost: u64,
93        /// Maximum allowable budget for a transaction.
94        pub(crate) max_gas_budget: u64,
95        /// Computation cost per byte charged for package publish. This cost is
96        /// primarily determined by the cost to verify and link a
97        /// package. Note that this does not include the cost of writing
98        /// the package to the store.
99        package_publish_per_byte_cost: u64,
100        /// Per byte cost to read objects from the store. This is computation
101        /// cost instead of storage cost because it does not change the
102        /// amount of data stored on the db.
103        object_read_per_byte_cost: u64,
104        /// Unit cost of a byte in the storage. This will be used both for
105        /// charging for new storage as well as rebating for deleting
106        /// storage. That is, we expect users to get full refund on the
107        /// object storage when it's deleted.
108        storage_per_byte_cost: u64,
109        /// Execution cost table to be used.
110        pub execution_cost_table: CostTable,
111        /// Computation buckets to cost transaction in price groups
112        computation_bucket: Vec<ComputationBucket>,
113    }
114
115    impl std::fmt::Debug for IotaCostTable {
116        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
117            // TODO: dump the fields.
118            write!(f, "IotaCostTable(...)")
119        }
120    }
121
122    impl IotaCostTable {
123        pub(crate) fn new(c: &ProtocolConfig, gas_price: u64) -> Self {
124            // gas_price here is the Reference Gas Price, however we may decide
125            // to change it to be the price passed in the transaction
126            let min_transaction_cost = c.base_tx_cost_fixed() * gas_price;
127            Self {
128                min_transaction_cost,
129                max_gas_budget: c.max_tx_gas(),
130                package_publish_per_byte_cost: c.package_publish_cost_per_byte(),
131                object_read_per_byte_cost: c.obj_access_cost_read_per_byte(),
132                storage_per_byte_cost: c.obj_data_cost_refundable(),
133                execution_cost_table: cost_table_for_version(c.gas_model_version()),
134                computation_bucket: computation_bucket(c.max_gas_computation_bucket()),
135            }
136        }
137
138        pub(crate) fn unmetered() -> Self {
139            Self {
140                min_transaction_cost: 0,
141                max_gas_budget: u64::MAX,
142                package_publish_per_byte_cost: 0,
143                object_read_per_byte_cost: 0,
144                storage_per_byte_cost: 0,
145                execution_cost_table: ZERO_COST_SCHEDULE.clone(),
146                // should not matter
147                computation_bucket: computation_bucket(5_000_000),
148            }
149        }
150    }
151
152    #[derive(Debug)]
153    pub struct PerObjectStorage {
154        /// storage_cost is the total storage gas to charge. This is computed
155        /// at the end of execution while determining storage charges.
156        /// It tracks `storage_bytes * obj_data_cost_refundable` as
157        /// described in `storage_gas_price`
158        /// It has been multiplied by the storage gas price. This is the new
159        /// storage rebate.
160        pub storage_cost: u64,
161        /// storage_rebate is the storage rebate (in IOTA) for in this object.
162        /// This is computed at the end of execution while determining storage
163        /// charges. The value is in IOTA.
164        pub storage_rebate: u64,
165        /// The object size post-transaction in bytes
166        pub new_size: u64,
167    }
168
169    #[derive(Debug)]
170    pub struct IotaGasStatus {
171        /// GasStatus as used by the VM, that is all the VM sees
172        pub gas_status: GasStatus,
173        /// Cost table contains a set of constant/config for the gas
174        /// model/charging
175        cost_table: IotaCostTable,
176        /// Gas budget for this gas status instance.
177        /// Typically the gas budget as defined in the
178        /// `TransactionData::GasData`
179        gas_budget: u64,
180        /// Computation cost after execution. This is the result of the gas used
181        /// by the `GasStatus` properly bucketized.
182        /// Starts at 0 and it is assigned in `bucketize_computation`.
183        computation_cost: u64,
184        /// Whether to charge or go unmetered
185        charge: bool,
186        /// Gas price for computation.
187        /// This is a multiplier on the final charge as related to the RGP
188        /// (reference gas price). Checked at signing: `gas_price >=
189        /// reference_gas_price` and then conceptually
190        /// `final_computation_cost = total_computation_cost * gas_price /
191        /// reference_gas_price`
192        gas_price: u64,
193        // Reference gas price as defined in protocol config.
194        // If `protocol_defined_base_fee' is enabled, this is a mandatory base fee paid to the
195        // protocol.
196        reference_gas_price: u64,
197        /// Gas price for storage. This is a multiplier on the final charge
198        /// as related to the storage gas price defined in the system
199        /// (`ProtocolConfig::storage_gas_price`).
200        /// Conceptually, given a constant `obj_data_cost_refundable`
201        /// (defined in `ProtocolConfig::obj_data_cost_refundable`)
202        /// `total_storage_cost = storage_bytes * obj_data_cost_refundable`
203        /// `final_storage_cost = total_storage_cost * storage_gas_price`
204        storage_gas_price: u64,
205        /// Per Object Storage Cost and Storage Rebate, used to get accumulated
206        /// values at the end of execution to determine storage charges
207        /// and rebates.
208        per_object_storage: Vec<(ObjectId, PerObjectStorage)>,
209        // storage rebate rate as defined in the ProtocolConfig
210        rebate_rate: u64,
211        /// Amount of storage rebate accumulated when we are running in
212        /// unmetered mode (i.e. system transaction). This allows us to
213        /// track how much storage rebate we need to retain in system
214        /// transactions.
215        unmetered_storage_rebate: u64,
216        /// Rounding value to round up gas charges.
217        gas_rounding_step: Option<u64>,
218        /// Flag to indicate whether the protocol-defined base fee is enabled,
219        /// in which case the reference gas price is burned.
220        protocol_defined_base_fee: bool,
221    }
222
223    impl IotaGasStatus {
224        fn new(
225            move_gas_status: GasStatus,
226            gas_budget: u64,
227            charge: bool,
228            gas_price: u64,
229            reference_gas_price: u64,
230            storage_gas_price: u64,
231            rebate_rate: u64,
232            gas_rounding_step: Option<u64>,
233            cost_table: IotaCostTable,
234            protocol_defined_base_fee: bool,
235        ) -> IotaGasStatus {
236            let gas_rounding_step = gas_rounding_step.map(|val| val.max(1));
237            IotaGasStatus {
238                gas_status: move_gas_status,
239                gas_budget,
240                charge,
241                computation_cost: 0,
242                gas_price,
243                reference_gas_price,
244                storage_gas_price,
245                per_object_storage: Vec::new(),
246                rebate_rate,
247                unmetered_storage_rebate: 0,
248                gas_rounding_step,
249                cost_table,
250                protocol_defined_base_fee,
251            }
252        }
253
254        pub(crate) fn new_with_budget(
255            gas_budget: u64,
256            gas_price: u64,
257            reference_gas_price: u64,
258            config: &ProtocolConfig,
259        ) -> IotaGasStatus {
260            let storage_gas_price = config.storage_gas_price();
261            let computation_budget = computation_budget(gas_budget, gas_price, config);
262            let iota_cost_table = IotaCostTable::new(config, gas_price);
263            let gas_rounding_step = config.gas_rounding_step_as_option();
264            Self::new(
265                GasStatus::new(
266                    iota_cost_table.execution_cost_table.clone(),
267                    computation_budget,
268                    gas_price,
269                    config.gas_model_version(),
270                ),
271                gas_budget,
272                true,
273                gas_price,
274                reference_gas_price,
275                storage_gas_price,
276                config.storage_rebate_rate(),
277                gas_rounding_step,
278                iota_cost_table,
279                config.protocol_defined_base_fee(),
280            )
281        }
282
283        pub fn new_unmetered() -> IotaGasStatus {
284            Self::new(
285                GasStatus::new_unmetered(),
286                0,
287                false,
288                0,
289                0,
290                0,
291                0,
292                None,
293                IotaCostTable::unmetered(),
294                false,
295            )
296        }
297
298        pub fn reference_gas_price(&self) -> u64 {
299            self.reference_gas_price
300        }
301
302        // Check whether gas arguments are legit:
303        // 1. Gas object has an address owner.
304        // 2. Gas budget is between min and max budget allowed
305        // 3. Gas balance (all gas coins together) is bigger or equal to budget
306        pub(crate) fn check_gas_balance(
307            &self,
308            gas_objs: &[&ObjectReadResult],
309            gas_budget: u64,
310        ) -> UserInputResult {
311            // 1. All gas objects have an address owner
312            for gas_object in gas_objs {
313                // if as_object() returns None, it means the object has been deleted (and
314                // therefore must be a shared object).
315                if let Some(obj) = gas_object.as_object() {
316                    if !obj.is_address_owned() {
317                        return Err(UserInputError::GasObjectNotOwnedObject { owner: obj.owner });
318                    }
319                } else {
320                    // This case should never happen (because gas can't be a shared object), but we
321                    // handle this case for future-proofing
322                    return Err(UserInputError::MissingGasPayment);
323                }
324            }
325
326            // 2. Gas budget is between min and max budget allowed
327            if gas_budget > self.cost_table.max_gas_budget {
328                return Err(UserInputError::GasBudgetTooHigh {
329                    gas_budget,
330                    max_budget: self.cost_table.max_gas_budget,
331                });
332            }
333            if gas_budget < self.cost_table.min_transaction_cost {
334                return Err(UserInputError::GasBudgetTooLow {
335                    gas_budget,
336                    min_budget: self.cost_table.min_transaction_cost,
337                });
338            }
339
340            // 3. Gas balance (all gas coins together) is bigger or equal to budget
341            let mut gas_balance = 0u128;
342            for gas_obj in gas_objs {
343                // expect is safe because we already checked that all gas objects have an
344                // address owner
345                gas_balance +=
346                    gas::get_gas_balance(gas_obj.as_object().expect("object must be owned"))?
347                        as u128;
348            }
349            if gas_balance < gas_budget as u128 {
350                Err(UserInputError::GasBalanceTooLow {
351                    gas_balance,
352                    needed_gas_amount: gas_budget as u128,
353                })
354            } else {
355                Ok(())
356            }
357        }
358
359        fn storage_cost(&self) -> u64 {
360            self.storage_gas_units()
361        }
362
363        pub fn per_object_storage(&self) -> &Vec<(ObjectId, PerObjectStorage)> {
364            &self.per_object_storage
365        }
366    }
367
368    impl IotaGasStatusAPI for IotaGasStatus {
369        fn is_unmetered(&self) -> bool {
370            !self.charge
371        }
372
373        fn move_gas_status(&self) -> &GasStatus {
374            &self.gas_status
375        }
376
377        fn move_gas_status_mut(&mut self) -> &mut GasStatus {
378            &mut self.gas_status
379        }
380
381        fn bucketize_computation(&mut self) -> Result<(), ExecutionError> {
382            let mut computation_units = self.gas_status.gas_used_pre_gas_price();
383            if let Some(gas_rounding) = self.gas_rounding_step {
384                if gas_rounding > 0
385                    && (computation_units == 0 || computation_units % gas_rounding > 0)
386                {
387                    computation_units = ((computation_units / gas_rounding) + 1) * gas_rounding;
388                }
389            } else {
390                // use the max value of the bucket that the computation_units falls into.
391                computation_units =
392                    get_bucket_cost(&self.cost_table.computation_bucket, computation_units);
393            };
394            let computation_cost = computation_units * self.gas_price;
395            if self.gas_budget <= computation_cost {
396                self.computation_cost = self.gas_budget;
397                Err(ExecutionErrorKind::InsufficientGas.into())
398            } else {
399                self.computation_cost = computation_cost;
400                Ok(())
401            }
402        }
403
404        /// Returns the final (computation cost, storage cost, storage rebate)
405        /// of the gas meter. We use initial budget, combined with
406        /// remaining gas and storage cost to derive computation cost.
407        fn summary(&self) -> GasCostSummary {
408            // compute computation cost burned and storage rebate, both rebate and non
409            // refundable fee
410            let computation_cost_burned = if self.protocol_defined_base_fee {
411                // when protocol_defined_base_fee is enabled, the computation cost burned is
412                // computed as follows:
413                // computation_cost_burned = computation_units * reference_gas_price.
414                // = (computation_cost / gas_price) * reference_gas_price
415                self.computation_cost * self.reference_gas_price / self.gas_price
416            } else {
417                // when protocol_defined_base_fee is disabled, the entire computation cost is
418                // burned.
419                self.computation_cost
420            };
421            let storage_rebate = self.storage_rebate();
422            let sender_rebate = sender_rebate(storage_rebate, self.rebate_rate);
423            assert!(sender_rebate <= storage_rebate);
424            let non_refundable_storage_fee = storage_rebate - sender_rebate;
425            GasCostSummary {
426                computation_cost: self.computation_cost,
427                computation_cost_burned,
428                storage_cost: self.storage_cost(),
429                storage_rebate: sender_rebate,
430                non_refundable_storage_fee,
431            }
432        }
433
434        fn gas_budget(&self) -> u64 {
435            self.gas_budget
436        }
437
438        fn gas_price(&self) -> u64 {
439            self.gas_price
440        }
441
442        fn reference_gas_price(&self) -> u64 {
443            self.reference_gas_price
444        }
445
446        fn storage_gas_units(&self) -> u64 {
447            self.per_object_storage
448                .iter()
449                .map(|(_, per_object)| per_object.storage_cost)
450                .sum()
451        }
452
453        fn storage_rebate(&self) -> u64 {
454            self.per_object_storage
455                .iter()
456                .map(|(_, per_object)| per_object.storage_rebate)
457                .sum()
458        }
459
460        fn unmetered_storage_rebate(&self) -> u64 {
461            self.unmetered_storage_rebate
462        }
463
464        fn gas_used(&self) -> u64 {
465            self.gas_status.gas_used_pre_gas_price()
466        }
467
468        fn reset_storage_cost_and_rebate(&mut self) {
469            self.per_object_storage = Vec::new();
470            self.unmetered_storage_rebate = 0;
471        }
472
473        fn charge_storage_read(&mut self, size: usize) -> Result<(), ExecutionError> {
474            self.gas_status
475                .charge_bytes(size, self.cost_table.object_read_per_byte_cost)
476                .map_err(|e| {
477                    debug_assert_eq!(e.major_status(), StatusCode::OUT_OF_GAS);
478                    ExecutionErrorKind::InsufficientGas.into()
479                })
480        }
481
482        fn charge_publish_package(&mut self, size: usize) -> Result<(), ExecutionError> {
483            self.gas_status
484                .charge_bytes(size, self.cost_table.package_publish_per_byte_cost)
485                .map_err(|e| {
486                    debug_assert_eq!(e.major_status(), StatusCode::OUT_OF_GAS);
487                    ExecutionErrorKind::InsufficientGas.into()
488                })
489        }
490
491        /// Update `storage_rebate` and `storage_gas_units` for each object in
492        /// the transaction. There is no charge in this function.
493        /// Charges will all be applied together at the end
494        /// (`track_storage_mutation`).
495        /// Return the new storage rebate (cost of object storage) according to
496        /// `new_size`.
497        fn track_storage_mutation(
498            &mut self,
499            object_id: ObjectId,
500            new_size: usize,
501            storage_rebate: u64,
502        ) -> u64 {
503            if self.is_unmetered() {
504                self.unmetered_storage_rebate += storage_rebate;
505                return 0;
506            }
507
508            // compute and track cost (based on size)
509            let new_size = new_size as u64;
510            let storage_cost =
511                new_size * self.cost_table.storage_per_byte_cost * self.storage_gas_price;
512            // track rebate
513
514            self.per_object_storage.push((
515                object_id,
516                PerObjectStorage {
517                    storage_cost,
518                    storage_rebate,
519                    new_size,
520                },
521            ));
522            // return the new object rebate (object storage cost)
523            storage_cost
524        }
525
526        fn charge_storage_and_rebate(&mut self) -> Result<(), ExecutionError> {
527            let storage_rebate = self.storage_rebate();
528            let storage_cost = self.storage_cost();
529            let sender_rebate = sender_rebate(storage_rebate, self.rebate_rate);
530            assert!(sender_rebate <= storage_rebate);
531            if sender_rebate >= storage_cost {
532                // there is more rebate than cost, when deducting gas we are adding
533                // to whatever is the current amount charged so we are `Ok`
534                Ok(())
535            } else {
536                let gas_left = self.gas_budget - self.computation_cost;
537                // we have to charge for storage and may go out of gas, check
538                if gas_left < storage_cost - sender_rebate {
539                    // Running out of gas would cause the temporary store to reset
540                    // and zero storage and rebate.
541                    // The remaining_gas will be 0 and we will charge all in computation
542                    Err(ExecutionErrorKind::InsufficientGas.into())
543                } else {
544                    Ok(())
545                }
546            }
547        }
548
549        fn adjust_computation_on_out_of_gas(&mut self) {
550            self.per_object_storage = Vec::new();
551            self.computation_cost = self.gas_budget;
552        }
553    }
554
555    pub fn computation_budget(gas_budget: u64, gas_price: u64, config: &ProtocolConfig) -> u64 {
556        let max_computation_budget = config.max_gas_computation_bucket() * gas_price;
557
558        if gas_budget > max_computation_budget {
559            max_computation_budget
560        } else {
561            gas_budget
562        }
563    }
564}