sui_types/gas_model/
gas_predicates.rs

1// Copyright (c) 2021, Facebook, Inc. and its affiliates
2// Copyright (c) Mysten Labs, Inc.
3// SPDX-License-Identifier: Apache-2.0
4
5//
6// Predicates and utility functions based on gas versions.
7//
8
9use crate::gas_model::tables::{
10    initial_cost_schedule_v1, initial_cost_schedule_v2, initial_cost_schedule_v3,
11    initial_cost_schedule_v4, initial_cost_schedule_v5,
12};
13use crate::gas_model::units_types::CostTable;
14use sui_protocol_config::ProtocolConfig;
15
16// Threshold after which native functions contribute to virtual instruction count.
17const V2_NATIVE_FUNCTION_CALL_THRESHOLD: u64 = 700;
18
19/// If true, do not charge the entire budget on storage OOG
20pub fn dont_charge_budget_on_storage_oog(gas_model_version: u64) -> bool {
21    gas_model_version >= 4
22}
23
24/// If true, enable the check for gas price too high
25pub fn check_for_gas_price_too_high(gas_model_version: u64) -> bool {
26    gas_model_version >= 4
27}
28
29/// If true, input object bytes are treated as memory allocated in Move and
30/// charged according to the bucket they end up in.
31pub fn charge_input_as_memory(gas_model_version: u64) -> bool {
32    gas_model_version == 4
33}
34
35/// If true, calculate value sizes using the legacy size calculation.
36pub fn use_legacy_abstract_size(gas_model_version: u64) -> bool {
37    gas_model_version <= 7
38}
39
40// If true, use the value of txn_base_cost as a multiplier of transaction gas price
41// to determine the minimum cost of a transaction.
42pub fn txn_base_cost_as_multiplier(protocol_config: &ProtocolConfig) -> bool {
43    protocol_config.txn_base_cost_as_multiplier()
44}
45
46// If true, charge differently for package upgrades
47pub fn charge_upgrades(gas_model_version: u64) -> bool {
48    gas_model_version >= 7
49}
50
51// Return the version supported cost table
52pub fn cost_table_for_version(gas_model: u64) -> CostTable {
53    if gas_model <= 3 {
54        initial_cost_schedule_v1()
55    } else if gas_model == 4 {
56        initial_cost_schedule_v2()
57    } else if gas_model == 5 {
58        initial_cost_schedule_v3()
59    } else if gas_model <= 7 {
60        initial_cost_schedule_v4()
61    } else {
62        initial_cost_schedule_v5()
63    }
64}
65
66// Return if the native function call threshold is exceeded
67pub fn native_function_threshold_exceeded(gas_model_version: u64, num_native_calls: u64) -> bool {
68    if gas_model_version > 8 {
69        num_native_calls > V2_NATIVE_FUNCTION_CALL_THRESHOLD
70    } else {
71        false
72    }
73}