sui_adapter_v0/
gas_charger.rs

1// Copyright (c) 2021, Facebook, Inc. and its affiliates
2// Copyright (c) Mysten Labs, Inc.
3// SPDX-License-Identifier: Apache-2.0
4
5pub use checked::*;
6
7#[sui_macros::with_checked_arithmetic]
8pub mod checked {
9
10    use crate::sui_types::gas::SuiGasStatusAPI;
11    use crate::temporary_store::TemporaryStore;
12    use sui_protocol_config::ProtocolConfig;
13    use sui_types::gas::{deduct_gas, GasCostSummary, SuiGasStatus};
14    use sui_types::gas_model::gas_predicates::dont_charge_budget_on_storage_oog;
15    use sui_types::{
16        base_types::{ObjectID, ObjectRef},
17        digests::TransactionDigest,
18        error::ExecutionError,
19        gas_model::tables::GasStatus,
20        is_system_package,
21        object::Data,
22        storage::{DeleteKindWithOldVersion, WriteKind},
23    };
24    use tracing::trace;
25
26    /// Tracks all gas operations for a single transaction.
27    /// This is the main entry point for gas accounting.
28    /// All the information about gas is stored in this object.
29    /// The objective here is two-fold:
30    /// 1- Isolate al version info into a single entry point. This file and the other gas
31    ///    related files are the only one that check for gas version.
32    /// 2- Isolate all gas accounting into a single implementation. Gas objects are not
33    ///    passed around, and they are retrieved from this instance.
34    #[derive(Debug)]
35    pub struct GasCharger {
36        tx_digest: TransactionDigest,
37        gas_model_version: u64,
38        gas_coins: Vec<ObjectRef>,
39        // this is the first gas coin in `gas_coins` and the one that all others will
40        // be smashed into. It can be None for system transactions when `gas_coins` is empty.
41        smashed_gas_coin: Option<ObjectID>,
42        gas_status: SuiGasStatus,
43    }
44
45    impl GasCharger {
46        pub fn new(
47            tx_digest: TransactionDigest,
48            gas_coins: Vec<ObjectRef>,
49            gas_status: SuiGasStatus,
50            protocol_config: &ProtocolConfig,
51        ) -> Self {
52            let gas_model_version = protocol_config.gas_model_version();
53            Self {
54                tx_digest,
55                gas_model_version,
56                gas_coins,
57                smashed_gas_coin: None,
58                gas_status,
59            }
60        }
61
62        pub fn new_unmetered(tx_digest: TransactionDigest) -> Self {
63            Self {
64                tx_digest,
65                gas_model_version: 6, // pick any of the latest, it should not matter
66                gas_coins: vec![],
67                smashed_gas_coin: None,
68                gas_status: SuiGasStatus::new_unmetered(),
69            }
70        }
71
72        // TODO: there is only one caller to this function that should not exist otherwise.
73        //       Explore way to remove it.
74        pub(crate) fn gas_coins(&self) -> &[ObjectRef] {
75            &self.gas_coins
76        }
77
78        // Return the logical gas coin for this transactions or None if no gas coin was present
79        // (system transactions).
80        pub fn gas_coin(&self) -> Option<ObjectID> {
81            self.smashed_gas_coin
82        }
83
84        pub fn gas_budget(&self) -> u64 {
85            self.gas_status.gas_budget()
86        }
87
88        pub fn unmetered_storage_rebate(&self) -> u64 {
89            self.gas_status.unmetered_storage_rebate()
90        }
91
92        pub fn no_charges(&self) -> bool {
93            self.gas_status.gas_used() == 0
94                && self.gas_status.storage_rebate() == 0
95                && self.gas_status.storage_gas_units() == 0
96        }
97
98        pub fn is_unmetered(&self) -> bool {
99            self.gas_status.is_unmetered()
100        }
101
102        pub fn move_gas_status(&self) -> &GasStatus {
103            self.gas_status.move_gas_status()
104        }
105
106        pub fn move_gas_status_mut(&mut self) -> &mut GasStatus {
107            self.gas_status.move_gas_status_mut()
108        }
109
110        pub fn into_gas_status(self) -> SuiGasStatus {
111            self.gas_status
112        }
113
114        pub fn summary(&self) -> GasCostSummary {
115            self.gas_status.summary()
116        }
117
118        // This function is called when the transaction is about to be executed.
119        // It will smash all gas coins into a single one and set the logical gas coin
120        // to be the first one in the list.
121        // After this call, `gas_coin` will return it id of the gas coin.
122        // This function panics if errors are found while operation on the gas coins.
123        // Transaction and certificate input checks must have insured that all gas coins
124        // are correct.
125        pub fn smash_gas(&mut self, temporary_store: &mut TemporaryStore<'_>) {
126            let gas_coin_count = self.gas_coins.len();
127            if gas_coin_count == 0 || (gas_coin_count == 1 && self.gas_coins[0].0 == ObjectID::ZERO)
128            {
129                return; // self.smashed_gas_coin is None
130            }
131            // set the first coin to be the transaction only gas coin.
132            // All others will be smashed into this one.
133            let gas_coin_id = self.gas_coins[0].0;
134            self.smashed_gas_coin = Some(gas_coin_id);
135            if gas_coin_count == 1 {
136                return;
137            }
138            // sum the value of all gas coins
139            let new_balance = self
140                .gas_coins
141                .iter()
142                .map(|obj_ref| {
143                    let obj = temporary_store.objects().get(&obj_ref.0).unwrap();
144                    let Data::Move(move_obj) = &obj.data else {
145                        return Err(ExecutionError::invariant_violation(
146                            "Provided non-gas coin object as input for gas!",
147                        ));
148                    };
149                    if !move_obj.type_().is_gas_coin() {
150                        return Err(ExecutionError::invariant_violation(
151                            "Provided non-gas coin object as input for gas!",
152                        ));
153                    }
154                    Ok(move_obj.get_coin_value_unsafe())
155                })
156                .collect::<Result<Vec<u64>, ExecutionError>>()
157                // transaction and certificate input checks must have insured that all gas coins
158                // are valid
159                .unwrap_or_else(|_| {
160                    panic!(
161                        "Invariant violation: non-gas coin object as input for gas in txn {}",
162                        self.tx_digest
163                    )
164                })
165                .iter()
166                .sum();
167            let mut primary_gas_object = temporary_store
168                .objects()
169                .get(&gas_coin_id)
170                // unwrap should be safe because we checked that this exists in `self.objects()` above
171                .unwrap_or_else(|| {
172                    panic!(
173                        "Invariant violation: gas coin not found in store in txn {}",
174                        self.tx_digest
175                    )
176                })
177                .clone();
178            // delete all gas objects except the primary_gas_object
179            for (id, version, _digest) in &self.gas_coins[1..] {
180                debug_assert_ne!(*id, primary_gas_object.id());
181                temporary_store.delete_object(id, DeleteKindWithOldVersion::Normal(*version));
182            }
183            primary_gas_object
184                .data
185                .try_as_move_mut()
186                // unwrap should be safe because we checked that the primary gas object was a coin object above.
187                .unwrap_or_else(|| {
188                    panic!(
189                        "Invariant violation: invalid coin object in txn {}",
190                        self.tx_digest
191                    )
192                })
193                .set_coin_value_unsafe(new_balance);
194            temporary_store.write_object(primary_gas_object, WriteKind::Mutate);
195        }
196
197        //
198        // Gas charging operations
199        //
200
201        pub fn track_storage_mutation(
202            &mut self,
203            object_id: ObjectID,
204            new_size: usize,
205            storage_rebate: u64,
206        ) -> u64 {
207            self.gas_status
208                .track_storage_mutation(object_id, new_size, storage_rebate)
209        }
210
211        pub fn reset_storage_cost_and_rebate(&mut self) {
212            self.gas_status.reset_storage_cost_and_rebate();
213        }
214
215        pub fn charge_publish_package(&mut self, size: usize) -> Result<(), ExecutionError> {
216            self.gas_status.charge_publish_package(size)
217        }
218
219        pub fn charge_input_objects(
220            &mut self,
221            temporary_store: &TemporaryStore<'_>,
222        ) -> Result<(), ExecutionError> {
223            let objects = temporary_store.objects();
224            // TODO: Charge input object count.
225            let _object_count = objects.len();
226            // Charge bytes read
227            let total_size = temporary_store
228                .objects()
229                .iter()
230                // don't charge for loading Sui Framework or Move stdlib
231                .filter(|(id, _)| !is_system_package(**id))
232                .map(|(_, obj)| obj.object_size_for_gas_metering())
233                .sum();
234            self.gas_status.charge_storage_read(total_size)
235        }
236
237        /// Resets any mutations, deletions, and events recorded in the store, as well as any storage costs and
238        /// rebates, then Re-runs gas smashing. Effects on store are now as if we were about to begin execution
239        pub fn reset(&mut self, temporary_store: &mut TemporaryStore<'_>) {
240            temporary_store.drop_writes();
241            self.gas_status.reset_storage_cost_and_rebate();
242            self.smash_gas(temporary_store);
243        }
244
245        /// Entry point for gas charging.
246        /// 1. Compute tx storage gas costs and tx storage rebates, update storage_rebate field of
247        /// mutated objects
248        /// 2. Deduct computation gas costs and storage costs, credit storage rebates.
249        /// The happy path of this function follows (1) + (2) and is fairly simple.
250        /// Most of the complexity is in the unhappy paths:
251        /// - if execution aborted before calling this function, we have to dump all writes +
252        ///   re-smash gas, then charge for storage
253        /// - if we run out of gas while charging for storage, we have to dump all writes +
254        ///   re-smash gas, then charge for storage again
255        pub fn charge_gas<T>(
256            &mut self,
257            temporary_store: &mut TemporaryStore<'_>,
258            execution_result: &mut Result<T, ExecutionError>,
259        ) -> GasCostSummary {
260            // at this point, we have done *all* charging for computation,
261            // but have not yet set the storage rebate or storage gas units
262            debug_assert!(self.gas_status.storage_rebate() == 0);
263            debug_assert!(self.gas_status.storage_gas_units() == 0);
264
265            if self.smashed_gas_coin.is_some() {
266                // bucketize computation cost
267                if let Err(err) = self.gas_status.bucketize_computation(None) {
268                    if execution_result.is_ok() {
269                        *execution_result = Err(err);
270                    }
271                }
272
273                // On error we need to dump writes, deletes, etc before charging storage gas
274                if execution_result.is_err() {
275                    self.reset(temporary_store);
276                }
277            }
278
279            // compute and collect storage charges
280            temporary_store.ensure_gas_and_input_mutated(self);
281            temporary_store.collect_storage_and_rebate(self);
282
283            if self.smashed_gas_coin.is_some() {
284                #[skip_checked_arithmetic]
285                trace!(target: "replay_gas_info", "Gas smashing has occurred for this transaction");
286            }
287
288            // system transactions (None smashed_gas_coin)  do not have gas and so do not charge
289            // for storage, however they track storage values to check for conservation rules
290            if let Some(gas_object_id) = self.smashed_gas_coin {
291                if dont_charge_budget_on_storage_oog(self.gas_model_version) {
292                    self.handle_storage_and_rebate_v2(temporary_store, execution_result)
293                } else {
294                    self.handle_storage_and_rebate_v1(temporary_store, execution_result)
295                }
296
297                let cost_summary = self.gas_status.summary();
298                let gas_used = cost_summary.net_gas_usage();
299
300                let mut gas_object = temporary_store.read_object(&gas_object_id).unwrap().clone();
301                deduct_gas(&mut gas_object, gas_used);
302                #[skip_checked_arithmetic]
303                trace!(gas_used, gas_obj_id =? gas_object.id(), gas_obj_ver =? gas_object.version(), "Updated gas object");
304
305                temporary_store.write_object(gas_object, WriteKind::Mutate);
306                cost_summary
307            } else {
308                GasCostSummary::default()
309            }
310        }
311
312        fn handle_storage_and_rebate_v1<T>(
313            &mut self,
314            temporary_store: &mut TemporaryStore<'_>,
315            execution_result: &mut Result<T, ExecutionError>,
316        ) {
317            if let Err(err) = self.gas_status.charge_storage_and_rebate() {
318                self.reset(temporary_store);
319                self.gas_status.adjust_computation_on_out_of_gas();
320                temporary_store.ensure_gas_and_input_mutated(self);
321                temporary_store.collect_rebate(self);
322                if execution_result.is_ok() {
323                    *execution_result = Err(err);
324                }
325            }
326        }
327
328        fn handle_storage_and_rebate_v2<T>(
329            &mut self,
330            temporary_store: &mut TemporaryStore<'_>,
331            execution_result: &mut Result<T, ExecutionError>,
332        ) {
333            if let Err(err) = self.gas_status.charge_storage_and_rebate() {
334                // we run out of gas charging storage, reset and try charging for storage again.
335                // Input objects are touched and so they have a storage cost
336                self.reset(temporary_store);
337                temporary_store.ensure_gas_and_input_mutated(self);
338                temporary_store.collect_storage_and_rebate(self);
339                if let Err(err) = self.gas_status.charge_storage_and_rebate() {
340                    // we run out of gas attempting to charge for the input objects exclusively,
341                    // deal with this edge case by not charging for storage
342                    self.reset(temporary_store);
343                    self.gas_status.adjust_computation_on_out_of_gas();
344                    temporary_store.ensure_gas_and_input_mutated(self);
345                    temporary_store.collect_rebate(self);
346                    if execution_result.is_ok() {
347                        *execution_result = Err(err);
348                    }
349                } else if execution_result.is_ok() {
350                    *execution_result = Err(err);
351                }
352            }
353        }
354    }
355}