near_parameters/
cost.rs

1use crate::parameter::Parameter;
2use enum_map::{enum_map, EnumMap};
3use near_account_id::AccountType;
4use near_primitives_core::types::{Balance, Compute, Gas};
5use near_schema_checker_lib::ProtocolSchema;
6use num_rational::Rational32;
7
8/// Costs associated with an object that can only be sent over the network (and executed
9/// by the receiver).
10/// NOTE: `send_sir` or `send_not_sir` fees are usually burned when the item is being created.
11/// And `execution` fee is burned when the item is being executed.
12#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, Hash, PartialEq, Eq)]
13pub struct Fee {
14    /// Fee for sending an object from the sender to itself, guaranteeing that it does not leave
15    /// the shard.
16    pub send_sir: Gas,
17    /// Fee for sending an object potentially across the shards.
18    pub send_not_sir: Gas,
19    /// Fee for executing the object.
20    pub execution: Gas,
21}
22
23impl Fee {
24    #[inline]
25    pub fn send_fee(&self, sir: bool) -> Gas {
26        if sir {
27            self.send_sir
28        } else {
29            self.send_not_sir
30        }
31    }
32
33    pub fn exec_fee(&self) -> Gas {
34        self.execution
35    }
36
37    /// The minimum fee to send and execute.
38    pub fn min_send_and_exec_fee(&self) -> Gas {
39        std::cmp::min(self.send_sir, self.send_not_sir) + self.execution
40    }
41}
42
43#[derive(Debug, Clone, Hash, PartialEq, Eq)]
44pub struct ParameterCost {
45    pub gas: Gas,
46    pub compute: Compute,
47}
48
49#[derive(Debug, Clone, Hash, PartialEq, Eq)]
50pub struct ExtCostsConfig {
51    pub costs: EnumMap<ExtCosts, ParameterCost>,
52}
53
54// We multiply the actual computed costs by the fixed factor to ensure we
55// have certain reserve for further gas price variation.
56const SAFETY_MULTIPLIER: u64 = 3;
57
58impl ExtCostsConfig {
59    pub fn gas_cost(&self, param: ExtCosts) -> Gas {
60        self.costs[param].gas
61    }
62
63    pub fn compute_cost(&self, param: ExtCosts) -> Compute {
64        self.costs[param].compute
65    }
66
67    /// Convenience constructor to use in tests where the exact gas cost does
68    /// not need to correspond to a specific protocol version.
69    pub fn test_with_undercharging_factor(factor: u64) -> ExtCostsConfig {
70        let costs = enum_map! {
71            ExtCosts::base => SAFETY_MULTIPLIER * 88256037,
72            ExtCosts::contract_loading_base => SAFETY_MULTIPLIER * 11815321,
73            ExtCosts::contract_loading_bytes => SAFETY_MULTIPLIER * 72250,
74            ExtCosts::read_memory_base => SAFETY_MULTIPLIER * 869954400,
75            ExtCosts::read_memory_byte => SAFETY_MULTIPLIER * 1267111,
76            ExtCosts::write_memory_base => SAFETY_MULTIPLIER * 934598287,
77            ExtCosts::write_memory_byte => SAFETY_MULTIPLIER * 907924,
78            ExtCosts::read_register_base => SAFETY_MULTIPLIER * 839055062,
79            ExtCosts::read_register_byte => SAFETY_MULTIPLIER * 32854,
80            ExtCosts::write_register_base => SAFETY_MULTIPLIER * 955174162,
81            ExtCosts::write_register_byte => SAFETY_MULTIPLIER * 1267188,
82            ExtCosts::utf8_decoding_base => SAFETY_MULTIPLIER * 1037259687,
83            ExtCosts::utf8_decoding_byte => SAFETY_MULTIPLIER * 97193493,
84            ExtCosts::utf16_decoding_base => SAFETY_MULTIPLIER * 1181104350,
85            ExtCosts::utf16_decoding_byte => SAFETY_MULTIPLIER * 54525831,
86            ExtCosts::sha256_base => SAFETY_MULTIPLIER * 1513656750,
87            ExtCosts::sha256_byte => SAFETY_MULTIPLIER * 8039117,
88            ExtCosts::keccak256_base => SAFETY_MULTIPLIER * 1959830425,
89            ExtCosts::keccak256_byte => SAFETY_MULTIPLIER * 7157035,
90            ExtCosts::keccak512_base => SAFETY_MULTIPLIER * 1937129412,
91            ExtCosts::keccak512_byte => SAFETY_MULTIPLIER * 12216567,
92            ExtCosts::ripemd160_base => SAFETY_MULTIPLIER * 284558362,
93            ExtCosts::ed25519_verify_base => SAFETY_MULTIPLIER * 1513656750,
94            ExtCosts::ed25519_verify_byte => SAFETY_MULTIPLIER * 7157035,
95            ExtCosts::ripemd160_block => SAFETY_MULTIPLIER * 226702528,
96            ExtCosts::ecrecover_base => SAFETY_MULTIPLIER * 1121789875000,
97            ExtCosts::log_base => SAFETY_MULTIPLIER * 1181104350,
98            ExtCosts::log_byte => SAFETY_MULTIPLIER * 4399597,
99            ExtCosts::storage_write_base => SAFETY_MULTIPLIER * 21398912000,
100            ExtCosts::storage_write_key_byte => SAFETY_MULTIPLIER * 23494289,
101            ExtCosts::storage_write_value_byte => SAFETY_MULTIPLIER * 10339513,
102            ExtCosts::storage_write_evicted_byte => SAFETY_MULTIPLIER * 10705769,
103            ExtCosts::storage_read_base => SAFETY_MULTIPLIER * 18785615250,
104            ExtCosts::storage_read_key_byte => SAFETY_MULTIPLIER * 10317511,
105            ExtCosts::storage_read_value_byte => SAFETY_MULTIPLIER * 1870335,
106            ExtCosts::storage_large_read_overhead_base => 0,
107            ExtCosts::storage_large_read_overhead_byte => 0,
108            ExtCosts::storage_remove_base => SAFETY_MULTIPLIER * 17824343500,
109            ExtCosts::storage_remove_key_byte => SAFETY_MULTIPLIER * 12740128,
110            ExtCosts::storage_remove_ret_value_byte => SAFETY_MULTIPLIER * 3843852,
111            ExtCosts::storage_has_key_base => SAFETY_MULTIPLIER * 18013298875,
112            ExtCosts::storage_has_key_byte => SAFETY_MULTIPLIER * 10263615,
113            // Here it should be `SAFETY_MULTIPLIER * 0` for consistency, but then
114            // clippy complains with "this operation will always return zero" warning
115            ExtCosts::storage_iter_create_prefix_base => 0,
116            ExtCosts::storage_iter_create_prefix_byte => 0,
117            ExtCosts::storage_iter_create_range_base => 0,
118            ExtCosts::storage_iter_create_from_byte => 0,
119            ExtCosts::storage_iter_create_to_byte => 0,
120            ExtCosts::storage_iter_next_base => 0,
121            ExtCosts::storage_iter_next_key_byte => 0,
122            ExtCosts::storage_iter_next_value_byte => 0,
123            ExtCosts::touching_trie_node => SAFETY_MULTIPLIER * 5367318642,
124            ExtCosts::read_cached_trie_node => SAFETY_MULTIPLIER * 760_000_000,
125            ExtCosts::promise_and_base => SAFETY_MULTIPLIER * 488337800,
126            ExtCosts::promise_and_per_promise => SAFETY_MULTIPLIER * 1817392,
127            ExtCosts::promise_return => SAFETY_MULTIPLIER * 186717462,
128            ExtCosts::validator_stake_base => SAFETY_MULTIPLIER * 303944908800,
129            ExtCosts::validator_total_stake_base => SAFETY_MULTIPLIER * 303944908800,
130            ExtCosts::alt_bn128_g1_multiexp_base => 713_000_000_000,
131            ExtCosts::alt_bn128_g1_multiexp_element => 320_000_000_000,
132            ExtCosts::alt_bn128_pairing_check_base => 9_686_000_000_000,
133            ExtCosts::alt_bn128_pairing_check_element => 5_102_000_000_000,
134            ExtCosts::alt_bn128_g1_sum_base => 3_000_000_000,
135            ExtCosts::alt_bn128_g1_sum_element => 5_000_000_000,
136            ExtCosts::bls12381_p1_sum_base => SAFETY_MULTIPLIER * 5_500_000_000,
137            ExtCosts::bls12381_p1_sum_element => SAFETY_MULTIPLIER * 2_000_000_000,
138            ExtCosts::bls12381_p2_sum_base => SAFETY_MULTIPLIER * 6_200_000_000,
139            ExtCosts::bls12381_p2_sum_element => SAFETY_MULTIPLIER * 5_000_000_000,
140            ExtCosts::bls12381_g1_multiexp_base => SAFETY_MULTIPLIER * 5_500_000_000,
141            ExtCosts::bls12381_g1_multiexp_element => SAFETY_MULTIPLIER * 310_000_000_000,
142            ExtCosts::bls12381_g2_multiexp_base => SAFETY_MULTIPLIER * 6_200_000_000,
143            ExtCosts::bls12381_g2_multiexp_element => SAFETY_MULTIPLIER * 665_000_000_000,
144            ExtCosts::bls12381_map_fp_to_g1_base => SAFETY_MULTIPLIER * 500_000_000,
145            ExtCosts::bls12381_map_fp_to_g1_element => SAFETY_MULTIPLIER * 84_000_000_000,
146            ExtCosts::bls12381_map_fp2_to_g2_base => SAFETY_MULTIPLIER * 500_000_000,
147            ExtCosts::bls12381_map_fp2_to_g2_element => SAFETY_MULTIPLIER * 300_000_000_000,
148            ExtCosts::bls12381_pairing_base => SAFETY_MULTIPLIER * 710_000_000_000,
149            ExtCosts::bls12381_pairing_element => SAFETY_MULTIPLIER * 710_000_000_000,
150            ExtCosts::bls12381_p1_decompress_base => SAFETY_MULTIPLIER * 500_000_000,
151            ExtCosts::bls12381_p1_decompress_element => SAFETY_MULTIPLIER * 27_000_000_000,
152            ExtCosts::bls12381_p2_decompress_base => SAFETY_MULTIPLIER * 500_000_000,
153            ExtCosts::bls12381_p2_decompress_element => SAFETY_MULTIPLIER * 55_000_000_000,
154            // TODO(yield/resume): replicate fees here after estimation
155            ExtCosts::yield_create_base => 300_000_000_000_000,
156            ExtCosts::yield_create_byte => 300_000_000_000_000,
157            ExtCosts::yield_resume_base => 300_000_000_000_000,
158            ExtCosts::yield_resume_byte => 300_000_000_000_000,
159        }
160        .map(|_, value| ParameterCost { gas: value, compute: value * factor });
161        ExtCostsConfig { costs }
162    }
163
164    /// `test_with_undercharging_factor` with a factor of 1.
165    pub fn test() -> ExtCostsConfig {
166        Self::test_with_undercharging_factor(1)
167    }
168}
169
170/// Strongly-typed representation of the fees for counting.
171///
172/// Do not change the enum discriminants here, they are used for borsh
173/// (de-)serialization.
174#[derive(
175    Copy,
176    Clone,
177    Hash,
178    PartialEq,
179    Eq,
180    Debug,
181    PartialOrd,
182    Ord,
183    strum::Display,
184    strum::EnumIter,
185    enum_map::Enum,
186    ProtocolSchema,
187)]
188#[allow(non_camel_case_types)]
189pub enum ExtCosts {
190    base = 0,
191    contract_loading_base = 1,
192    contract_loading_bytes = 2,
193    read_memory_base = 3,
194    read_memory_byte = 4,
195    write_memory_base = 5,
196    write_memory_byte = 6,
197    read_register_base = 7,
198    read_register_byte = 8,
199    write_register_base = 9,
200    write_register_byte = 10,
201    utf8_decoding_base = 11,
202    utf8_decoding_byte = 12,
203    utf16_decoding_base = 13,
204    utf16_decoding_byte = 14,
205    sha256_base = 15,
206    sha256_byte = 16,
207    keccak256_base = 17,
208    keccak256_byte = 18,
209    keccak512_base = 19,
210    keccak512_byte = 20,
211    ripemd160_base = 21,
212    ripemd160_block = 22,
213    ecrecover_base = 23,
214    log_base = 24,
215    log_byte = 25,
216    storage_write_base = 26,
217    storage_write_key_byte = 27,
218    storage_write_value_byte = 28,
219    storage_write_evicted_byte = 29,
220    storage_read_base = 30,
221    storage_read_key_byte = 31,
222    storage_read_value_byte = 32,
223    storage_remove_base = 33,
224    storage_remove_key_byte = 34,
225    storage_remove_ret_value_byte = 35,
226    storage_has_key_base = 36,
227    storage_has_key_byte = 37,
228    storage_iter_create_prefix_base = 38,
229    storage_iter_create_prefix_byte = 39,
230    storage_iter_create_range_base = 40,
231    storage_iter_create_from_byte = 41,
232    storage_iter_create_to_byte = 42,
233    storage_iter_next_base = 43,
234    storage_iter_next_key_byte = 44,
235    storage_iter_next_value_byte = 45,
236    touching_trie_node = 46,
237    read_cached_trie_node = 47,
238    promise_and_base = 48,
239    promise_and_per_promise = 49,
240    promise_return = 50,
241    validator_stake_base = 51,
242    validator_total_stake_base = 52,
243    alt_bn128_g1_multiexp_base = 53,
244    alt_bn128_g1_multiexp_element = 54,
245    alt_bn128_pairing_check_base = 55,
246    alt_bn128_pairing_check_element = 56,
247    alt_bn128_g1_sum_base = 57,
248    alt_bn128_g1_sum_element = 58,
249    ed25519_verify_base = 59,
250    ed25519_verify_byte = 60,
251    yield_create_base = 61,
252    yield_create_byte = 62,
253    yield_resume_base = 63,
254    yield_resume_byte = 64,
255    bls12381_p1_sum_base = 65,
256    bls12381_p1_sum_element = 66,
257    bls12381_p2_sum_base = 67,
258    bls12381_p2_sum_element = 68,
259    bls12381_g1_multiexp_base = 69,
260    bls12381_g1_multiexp_element = 70,
261    bls12381_g2_multiexp_base = 71,
262    bls12381_g2_multiexp_element = 72,
263    bls12381_map_fp_to_g1_base = 73,
264    bls12381_map_fp_to_g1_element = 74,
265    bls12381_map_fp2_to_g2_base = 75,
266    bls12381_map_fp2_to_g2_element = 76,
267    bls12381_pairing_base = 77,
268    bls12381_pairing_element = 78,
269    bls12381_p1_decompress_base = 79,
270    bls12381_p1_decompress_element = 80,
271    bls12381_p2_decompress_base = 81,
272    bls12381_p2_decompress_element = 82,
273    storage_large_read_overhead_base = 83,
274    storage_large_read_overhead_byte = 84,
275}
276
277// Type of an action, used in fees logic.
278#[derive(
279    Copy,
280    Clone,
281    Hash,
282    PartialEq,
283    Eq,
284    Debug,
285    PartialOrd,
286    Ord,
287    strum::Display,
288    strum::EnumIter,
289    enum_map::Enum,
290    ProtocolSchema,
291)]
292#[allow(non_camel_case_types)]
293pub enum ActionCosts {
294    create_account = 0,
295    delete_account = 1,
296    deploy_contract_base = 2,
297    deploy_contract_byte = 3,
298    function_call_base = 4,
299    function_call_byte = 5,
300    transfer = 6,
301    stake = 7,
302    add_full_access_key = 8,
303    add_function_call_key_base = 9,
304    add_function_call_key_byte = 10,
305    delete_key = 11,
306    new_action_receipt = 12,
307    new_data_receipt_base = 13,
308    new_data_receipt_byte = 14,
309    delegate = 15,
310}
311
312impl ExtCosts {
313    pub fn gas(self, config: &ExtCostsConfig) -> Gas {
314        config.gas_cost(self)
315    }
316
317    pub fn compute(self, config: &ExtCostsConfig) -> Compute {
318        config.compute_cost(self)
319    }
320
321    pub fn param(&self) -> Parameter {
322        match self {
323            ExtCosts::base => Parameter::WasmBase,
324            ExtCosts::contract_loading_base => Parameter::WasmContractLoadingBase,
325            ExtCosts::contract_loading_bytes => Parameter::WasmContractLoadingBytes,
326            ExtCosts::read_memory_base => Parameter::WasmReadMemoryBase,
327            ExtCosts::read_memory_byte => Parameter::WasmReadMemoryByte,
328            ExtCosts::write_memory_base => Parameter::WasmWriteMemoryBase,
329            ExtCosts::write_memory_byte => Parameter::WasmWriteMemoryByte,
330            ExtCosts::read_register_base => Parameter::WasmReadRegisterBase,
331            ExtCosts::read_register_byte => Parameter::WasmReadRegisterByte,
332            ExtCosts::write_register_base => Parameter::WasmWriteRegisterBase,
333            ExtCosts::write_register_byte => Parameter::WasmWriteRegisterByte,
334            ExtCosts::utf8_decoding_base => Parameter::WasmUtf8DecodingBase,
335            ExtCosts::utf8_decoding_byte => Parameter::WasmUtf8DecodingByte,
336            ExtCosts::utf16_decoding_base => Parameter::WasmUtf16DecodingBase,
337            ExtCosts::utf16_decoding_byte => Parameter::WasmUtf16DecodingByte,
338            ExtCosts::sha256_base => Parameter::WasmSha256Base,
339            ExtCosts::sha256_byte => Parameter::WasmSha256Byte,
340            ExtCosts::keccak256_base => Parameter::WasmKeccak256Base,
341            ExtCosts::keccak256_byte => Parameter::WasmKeccak256Byte,
342            ExtCosts::keccak512_base => Parameter::WasmKeccak512Base,
343            ExtCosts::keccak512_byte => Parameter::WasmKeccak512Byte,
344            ExtCosts::ripemd160_base => Parameter::WasmRipemd160Base,
345            ExtCosts::ripemd160_block => Parameter::WasmRipemd160Block,
346            ExtCosts::ecrecover_base => Parameter::WasmEcrecoverBase,
347            ExtCosts::ed25519_verify_base => Parameter::WasmEd25519VerifyBase,
348            ExtCosts::ed25519_verify_byte => Parameter::WasmEd25519VerifyByte,
349            ExtCosts::log_base => Parameter::WasmLogBase,
350            ExtCosts::log_byte => Parameter::WasmLogByte,
351            ExtCosts::storage_write_base => Parameter::WasmStorageWriteBase,
352            ExtCosts::storage_write_key_byte => Parameter::WasmStorageWriteKeyByte,
353            ExtCosts::storage_write_value_byte => Parameter::WasmStorageWriteValueByte,
354            ExtCosts::storage_write_evicted_byte => Parameter::WasmStorageWriteEvictedByte,
355            ExtCosts::storage_read_base => Parameter::WasmStorageReadBase,
356            ExtCosts::storage_read_key_byte => Parameter::WasmStorageReadKeyByte,
357            ExtCosts::storage_read_value_byte => Parameter::WasmStorageReadValueByte,
358            ExtCosts::storage_large_read_overhead_base => {
359                Parameter::WasmStorageLargeReadOverheadBase
360            }
361            ExtCosts::storage_large_read_overhead_byte => {
362                Parameter::WasmStorageLargeReadOverheadByte
363            }
364            ExtCosts::storage_remove_base => Parameter::WasmStorageRemoveBase,
365            ExtCosts::storage_remove_key_byte => Parameter::WasmStorageRemoveKeyByte,
366            ExtCosts::storage_remove_ret_value_byte => Parameter::WasmStorageRemoveRetValueByte,
367            ExtCosts::storage_has_key_base => Parameter::WasmStorageHasKeyBase,
368            ExtCosts::storage_has_key_byte => Parameter::WasmStorageHasKeyByte,
369            ExtCosts::storage_iter_create_prefix_base => Parameter::WasmStorageIterCreatePrefixBase,
370            ExtCosts::storage_iter_create_prefix_byte => Parameter::WasmStorageIterCreatePrefixByte,
371            ExtCosts::storage_iter_create_range_base => Parameter::WasmStorageIterCreateRangeBase,
372            ExtCosts::storage_iter_create_from_byte => Parameter::WasmStorageIterCreateFromByte,
373            ExtCosts::storage_iter_create_to_byte => Parameter::WasmStorageIterCreateToByte,
374            ExtCosts::storage_iter_next_base => Parameter::WasmStorageIterNextBase,
375            ExtCosts::storage_iter_next_key_byte => Parameter::WasmStorageIterNextKeyByte,
376            ExtCosts::storage_iter_next_value_byte => Parameter::WasmStorageIterNextValueByte,
377            ExtCosts::touching_trie_node => Parameter::WasmTouchingTrieNode,
378            ExtCosts::read_cached_trie_node => Parameter::WasmReadCachedTrieNode,
379            ExtCosts::promise_and_base => Parameter::WasmPromiseAndBase,
380            ExtCosts::promise_and_per_promise => Parameter::WasmPromiseAndPerPromise,
381            ExtCosts::promise_return => Parameter::WasmPromiseReturn,
382            ExtCosts::validator_stake_base => Parameter::WasmValidatorStakeBase,
383            ExtCosts::validator_total_stake_base => Parameter::WasmValidatorTotalStakeBase,
384            ExtCosts::alt_bn128_g1_multiexp_base => Parameter::WasmAltBn128G1MultiexpBase,
385            ExtCosts::alt_bn128_g1_multiexp_element => Parameter::WasmAltBn128G1MultiexpElement,
386            ExtCosts::alt_bn128_pairing_check_base => Parameter::WasmAltBn128PairingCheckBase,
387            ExtCosts::alt_bn128_pairing_check_element => Parameter::WasmAltBn128PairingCheckElement,
388            ExtCosts::alt_bn128_g1_sum_base => Parameter::WasmAltBn128G1SumBase,
389            ExtCosts::alt_bn128_g1_sum_element => Parameter::WasmAltBn128G1SumElement,
390            ExtCosts::yield_create_base => Parameter::WasmYieldCreateBase,
391            ExtCosts::yield_create_byte => Parameter::WasmYieldCreateByte,
392            ExtCosts::yield_resume_base => Parameter::WasmYieldResumeBase,
393            ExtCosts::yield_resume_byte => Parameter::WasmYieldResumeByte,
394            ExtCosts::bls12381_p1_sum_base => Parameter::WasmBls12381P1SumBase,
395            ExtCosts::bls12381_p1_sum_element => Parameter::WasmBls12381P1SumElement,
396            ExtCosts::bls12381_p2_sum_base => Parameter::WasmBls12381P2SumBase,
397            ExtCosts::bls12381_p2_sum_element => Parameter::WasmBls12381P2SumElement,
398            ExtCosts::bls12381_g1_multiexp_base => Parameter::WasmBls12381G1MultiexpBase,
399            ExtCosts::bls12381_g1_multiexp_element => Parameter::WasmBls12381G1MultiexpElement,
400            ExtCosts::bls12381_g2_multiexp_base => Parameter::WasmBls12381G2MultiexpBase,
401            ExtCosts::bls12381_g2_multiexp_element => Parameter::WasmBls12381G2MultiexpElement,
402            ExtCosts::bls12381_map_fp_to_g1_base => Parameter::WasmBls12381MapFpToG1Base,
403            ExtCosts::bls12381_map_fp_to_g1_element => Parameter::WasmBls12381MapFpToG1Element,
404            ExtCosts::bls12381_map_fp2_to_g2_base => Parameter::WasmBls12381MapFp2ToG2Base,
405            ExtCosts::bls12381_map_fp2_to_g2_element => Parameter::WasmBls12381MapFp2ToG2Element,
406            ExtCosts::bls12381_pairing_base => Parameter::WasmBls12381PairingBase,
407            ExtCosts::bls12381_pairing_element => Parameter::WasmBls12381PairingElement,
408            ExtCosts::bls12381_p1_decompress_base => Parameter::WasmBls12381P1DecompressBase,
409            ExtCosts::bls12381_p1_decompress_element => Parameter::WasmBls12381P1DecompressElement,
410            ExtCosts::bls12381_p2_decompress_base => Parameter::WasmBls12381P2DecompressBase,
411            ExtCosts::bls12381_p2_decompress_element => Parameter::WasmBls12381P2DecompressElement,
412        }
413    }
414}
415
416#[derive(Debug, Clone, Hash, PartialEq, Eq)]
417pub struct RuntimeFeesConfig {
418    /// Gas fees for sending and executing actions.
419    pub action_fees: EnumMap<ActionCosts, Fee>,
420
421    /// Describes fees for storage.
422    pub storage_usage_config: StorageUsageConfig,
423
424    /// Fraction of the burnt gas to reward to the contract account for execution.
425    pub burnt_gas_reward: Rational32,
426
427    /// Pessimistic gas price inflation ratio.
428    pub pessimistic_gas_price_inflation_ratio: Rational32,
429}
430
431/// Describes cost of storage per block
432#[derive(Debug, Clone, Hash, PartialEq, Eq)]
433pub struct StorageUsageConfig {
434    /// Amount of yN per byte required to have on the account. See
435    /// <https://nomicon.io/Economics/README.html#state-stake> for details.
436    pub storage_amount_per_byte: Balance,
437    /// Number of bytes for an account record, including rounding up for account id.
438    pub num_bytes_account: u64,
439    /// Additional number of bytes for a k/v record
440    pub num_extra_bytes_record: u64,
441}
442
443impl RuntimeFeesConfig {
444    /// Access action fee by `ActionCosts`.
445    pub fn fee(&self, cost: ActionCosts) -> &Fee {
446        &self.action_fees[cost]
447    }
448
449    pub fn test() -> Self {
450        Self {
451            storage_usage_config: StorageUsageConfig::test(),
452            burnt_gas_reward: Rational32::new(3, 10),
453            pessimistic_gas_price_inflation_ratio: Rational32::new(103, 100),
454            action_fees: enum_map::enum_map! {
455                ActionCosts::create_account => Fee {
456                    send_sir: 3_850_000_000_000,
457                    send_not_sir: 3_850_000_000_000,
458                    execution: 3_850_000_000_000,
459                },
460                ActionCosts::delete_account => Fee {
461                    send_sir: 147489000000,
462                    send_not_sir: 147489000000,
463                    execution: 147489000000,
464                },
465                ActionCosts::deploy_contract_base => Fee {
466                    send_sir: 184765750000,
467                    send_not_sir: 184765750000,
468                    execution: 184765750000,
469                },
470                ActionCosts::deploy_contract_byte => Fee {
471                    send_sir: 6812999,
472                    send_not_sir: 6812999,
473                    execution: 6812999,
474                },
475                ActionCosts::function_call_base => Fee {
476                    send_sir: 2319861500000,
477                    send_not_sir: 2319861500000,
478                    execution: 2319861500000,
479                },
480                ActionCosts::function_call_byte => Fee {
481                    send_sir: 2235934,
482                    send_not_sir: 2235934,
483                    execution: 2235934,
484                },
485                ActionCosts::transfer => Fee {
486                    send_sir: 115123062500,
487                    send_not_sir: 115123062500,
488                    execution: 115123062500,
489                },
490                ActionCosts::stake => Fee {
491                    send_sir: 141715687500,
492                    send_not_sir: 141715687500,
493                    execution: 102217625000,
494                },
495                ActionCosts::add_full_access_key => Fee {
496                    send_sir: 101765125000,
497                    send_not_sir: 101765125000,
498                    execution: 101765125000,
499                },
500                ActionCosts::add_function_call_key_base => Fee {
501                    send_sir: 102217625000,
502                    send_not_sir: 102217625000,
503                    execution: 102217625000,
504                },
505                ActionCosts::add_function_call_key_byte => Fee {
506                    send_sir: 1925331,
507                    send_not_sir: 1925331,
508                    execution: 1925331,
509                },
510                ActionCosts::delete_key => Fee {
511                    send_sir: 94946625000,
512                    send_not_sir: 94946625000,
513                    execution: 94946625000,
514                },
515                ActionCosts::new_action_receipt => Fee {
516                    send_sir: 108059500000,
517                    send_not_sir: 108059500000,
518                    execution: 108059500000,
519                },
520                ActionCosts::new_data_receipt_base => Fee {
521                    send_sir: 4697339419375,
522                    send_not_sir: 4697339419375,
523                    execution: 4697339419375,
524                },
525                ActionCosts::new_data_receipt_byte => Fee {
526                    send_sir: 59357464,
527                    send_not_sir: 59357464,
528                    execution: 59357464,
529                },
530                ActionCosts::delegate => Fee {
531                    send_sir: 200_000_000_000,
532                    send_not_sir: 200_000_000_000,
533                    execution: 200_000_000_000,
534                },
535            },
536        }
537    }
538
539    pub fn free() -> Self {
540        Self {
541            action_fees: enum_map::enum_map! {
542                _ => Fee { send_sir: 0, send_not_sir: 0, execution: 0 }
543            },
544            storage_usage_config: StorageUsageConfig::free(),
545            burnt_gas_reward: Rational32::from_integer(0),
546            pessimistic_gas_price_inflation_ratio: Rational32::from_integer(0),
547        }
548    }
549
550    /// The minimum amount of gas required to create and execute a new receipt with a function call
551    /// action.
552    /// This amount is used to determine how many receipts can be created, send and executed for
553    /// some amount of prepaid gas using function calls.
554    pub fn min_receipt_with_function_call_gas(&self) -> Gas {
555        self.fee(ActionCosts::new_action_receipt).min_send_and_exec_fee()
556            + self.fee(ActionCosts::function_call_base).min_send_and_exec_fee()
557    }
558}
559
560impl StorageUsageConfig {
561    pub fn test() -> Self {
562        Self {
563            num_bytes_account: 100,
564            num_extra_bytes_record: 40,
565            storage_amount_per_byte: 909 * 100_000_000_000_000_000,
566        }
567    }
568
569    pub(crate) fn free() -> StorageUsageConfig {
570        Self { num_bytes_account: 0, num_extra_bytes_record: 0, storage_amount_per_byte: 0 }
571    }
572}
573
574/// Helper functions for computing Transfer fees.
575/// In case of implicit account creation they include extra fees for the CreateAccount and
576/// AddFullAccessKey (for NEAR-implicit account only) actions that are implicit.
577/// We can assume that no overflow will happen here.
578pub fn transfer_exec_fee(
579    cfg: &RuntimeFeesConfig,
580    implicit_account_creation_allowed: bool,
581    eth_implicit_accounts_enabled: bool,
582    receiver_account_type: AccountType,
583) -> Gas {
584    let transfer_fee = cfg.fee(ActionCosts::transfer).exec_fee();
585    match (implicit_account_creation_allowed, eth_implicit_accounts_enabled, receiver_account_type)
586    {
587        // Regular transfer to a named account.
588        (_, _, AccountType::NamedAccount) => transfer_fee,
589        // No account will be created, just a regular transfer.
590        (false, _, _) => transfer_fee,
591        // No account will be created, just a regular transfer.
592        (true, false, AccountType::EthImplicitAccount) => transfer_fee,
593        // Extra fee for the CreateAccount.
594        (true, true, AccountType::EthImplicitAccount) => {
595            transfer_fee + cfg.fee(ActionCosts::create_account).exec_fee()
596        }
597        // Extra fees for the CreateAccount and AddFullAccessKey.
598        (true, _, AccountType::NearImplicitAccount) => {
599            transfer_fee
600                + cfg.fee(ActionCosts::create_account).exec_fee()
601                + cfg.fee(ActionCosts::add_full_access_key).exec_fee()
602        }
603    }
604}
605
606pub fn transfer_send_fee(
607    cfg: &RuntimeFeesConfig,
608    sender_is_receiver: bool,
609    implicit_account_creation_allowed: bool,
610    eth_implicit_accounts_enabled: bool,
611    receiver_account_type: AccountType,
612) -> Gas {
613    let transfer_fee = cfg.fee(ActionCosts::transfer).send_fee(sender_is_receiver);
614    match (implicit_account_creation_allowed, eth_implicit_accounts_enabled, receiver_account_type)
615    {
616        // Regular transfer to a named account.
617        (_, _, AccountType::NamedAccount) => transfer_fee,
618        // No account will be created, just a regular transfer.
619        (false, _, _) => transfer_fee,
620        // No account will be created, just a regular transfer.
621        (true, false, AccountType::EthImplicitAccount) => transfer_fee,
622        // Extra fee for the CreateAccount.
623        (true, true, AccountType::EthImplicitAccount) => {
624            transfer_fee + cfg.fee(ActionCosts::create_account).send_fee(sender_is_receiver)
625        }
626        // Extra fees for the CreateAccount and AddFullAccessKey.
627        (true, _, AccountType::NearImplicitAccount) => {
628            transfer_fee
629                + cfg.fee(ActionCosts::create_account).send_fee(sender_is_receiver)
630                + cfg.fee(ActionCosts::add_full_access_key).send_fee(sender_is_receiver)
631        }
632    }
633}