near_parameters/
view.rs

1use crate::config::{CongestionControlConfig, WitnessConfig};
2use crate::{ActionCosts, ExtCosts, Fee, ParameterCost};
3use near_account_id::AccountId;
4use near_primitives_core::serialize::dec_format;
5use near_primitives_core::types::{Balance, Gas};
6use num_rational::Rational32;
7
8/// View that preserves JSON format of the runtime config.
9#[derive(Debug, serde::Serialize, serde::Deserialize, Clone)]
10#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
11pub struct RuntimeConfigView {
12    /// Amount of yN per byte required to have on the account.  See
13    /// <https://nomicon.io/Economics/Economic#state-stake> for details.
14    #[serde(with = "dec_format")]
15    #[cfg_attr(feature = "schemars", schemars(with = "String"))]
16    pub storage_amount_per_byte: Balance,
17    /// Costs of different actions that need to be performed when sending and
18    /// processing transaction and receipts.
19    pub transaction_costs: RuntimeFeesConfigView,
20    /// Config of wasm operations.
21    pub wasm_config: VMConfigView,
22    /// Config that defines rules for account creation.
23    pub account_creation_config: AccountCreationConfigView,
24    /// The configuration for congestion control.
25    pub congestion_control_config: CongestionControlConfigView,
26    /// Configuration specific to ChunkStateWitness.
27    pub witness_config: WitnessConfigView,
28}
29
30#[derive(Debug, serde::Serialize, serde::Deserialize, Clone)]
31#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
32pub struct RuntimeFeesConfigView {
33    /// Describes the cost of creating an action receipt, `ActionReceipt`, excluding the actual cost
34    /// of actions.
35    /// - `send` cost is burned when a receipt is created using `promise_create` or
36    ///     `promise_batch_create`
37    /// - `exec` cost is burned when the receipt is being executed.
38    pub action_receipt_creation_config: Fee,
39    /// Describes the cost of creating a data receipt, `DataReceipt`.
40    pub data_receipt_creation_config: DataReceiptCreationConfigView,
41    /// Describes the cost of creating a certain action, `Action`. Includes all variants.
42    pub action_creation_config: ActionCreationConfigView,
43    /// Describes fees for storage.
44    pub storage_usage_config: StorageUsageConfigView,
45
46    /// Fraction of the burnt gas to reward to the contract account for execution.
47    #[cfg_attr(feature = "schemars", schemars(with = "Rational32SchemarsProvider"))]
48    pub burnt_gas_reward: Rational32,
49
50    /// Pessimistic gas price inflation ratio.
51    #[cfg_attr(feature = "schemars", schemars(with = "Rational32SchemarsProvider"))]
52    pub pessimistic_gas_price_inflation_ratio: Rational32,
53}
54
55/// The structure describes configuration for creation of new accounts.
56#[derive(Debug, serde::Serialize, serde::Deserialize, Clone)]
57#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
58pub struct AccountCreationConfigView {
59    /// The minimum length of the top-level account ID that is allowed to be created by any account.
60    pub min_allowed_top_level_account_length: u8,
61    /// The account ID of the account registrar. This account ID allowed to create top-level
62    /// accounts of any valid length.
63    pub registrar_account_id: AccountId,
64}
65
66#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, Hash, PartialEq, Eq)]
67#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
68pub struct DataReceiptCreationConfigView {
69    /// Base cost of creating a data receipt.
70    /// Both `send` and `exec` costs are burned when a new receipt has input dependencies. The gas
71    /// is charged for each input dependency. The dependencies are specified when a receipt is
72    /// created using `promise_then` and `promise_batch_then`.
73    /// NOTE: Any receipt with output dependencies will produce data receipts. Even if it fails.
74    /// Even if the last action is not a function call (in case of success it will return empty
75    /// value).
76    pub base_cost: Fee,
77    /// Additional cost per byte sent.
78    /// Both `send` and `exec` costs are burned when a function call finishes execution and returns
79    /// `N` bytes of data to every output dependency. For each output dependency the cost is
80    /// `(send(sir) + exec()) * N`.
81    pub cost_per_byte: Fee,
82}
83
84/// Describes the cost of creating a specific action, `Action`. Includes all variants.
85#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, Hash, PartialEq, Eq)]
86#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
87pub struct ActionCreationConfigView {
88    /// Base cost of creating an account.
89    pub create_account_cost: Fee,
90
91    /// Base cost of deploying a contract.
92    pub deploy_contract_cost: Fee,
93    /// Cost per byte of deploying a contract.
94    pub deploy_contract_cost_per_byte: Fee,
95
96    /// Base cost of calling a function.
97    pub function_call_cost: Fee,
98    /// Cost per byte of method name and arguments of calling a function.
99    pub function_call_cost_per_byte: Fee,
100
101    /// Base cost of making a transfer.
102    pub transfer_cost: Fee,
103
104    /// Base cost of staking.
105    pub stake_cost: Fee,
106
107    /// Base cost of adding a key.
108    pub add_key_cost: AccessKeyCreationConfigView,
109
110    /// Base cost of deleting a key.
111    pub delete_key_cost: Fee,
112
113    /// Base cost of deleting an account.
114    pub delete_account_cost: Fee,
115
116    /// Base cost for processing a delegate action.
117    ///
118    /// This is on top of the costs for the actions inside the delegate action.
119    pub delegate_cost: Fee,
120}
121
122/// Describes the cost of creating an access key.
123#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, Hash, PartialEq, Eq)]
124#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
125pub struct AccessKeyCreationConfigView {
126    /// Base cost of creating a full access access-key.
127    pub full_access_cost: Fee,
128    /// Base cost of creating an access-key restricted to specific functions.
129    pub function_call_cost: Fee,
130    /// Cost per byte of method_names of creating a restricted access-key.
131    pub function_call_cost_per_byte: Fee,
132}
133
134/// Describes cost of storage per block
135#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, Hash, PartialEq, Eq)]
136#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
137pub struct StorageUsageConfigView {
138    /// Number of bytes for an account record, including rounding up for account id.
139    pub num_bytes_account: u64,
140    /// Additional number of bytes for a k/v record
141    pub num_extra_bytes_record: u64,
142}
143
144impl From<crate::RuntimeConfig> for RuntimeConfigView {
145    fn from(config: crate::RuntimeConfig) -> Self {
146        Self {
147            storage_amount_per_byte: config.storage_amount_per_byte(),
148            transaction_costs: RuntimeFeesConfigView {
149                action_receipt_creation_config: config
150                    .fees
151                    .fee(ActionCosts::new_action_receipt)
152                    .clone(),
153                data_receipt_creation_config: DataReceiptCreationConfigView {
154                    base_cost: config.fees.fee(ActionCosts::new_data_receipt_base).clone(),
155                    cost_per_byte: config.fees.fee(ActionCosts::new_data_receipt_byte).clone(),
156                },
157                action_creation_config: ActionCreationConfigView {
158                    create_account_cost: config.fees.fee(ActionCosts::create_account).clone(),
159                    deploy_contract_cost: config
160                        .fees
161                        .fee(ActionCosts::deploy_contract_base)
162                        .clone(),
163                    deploy_contract_cost_per_byte: config
164                        .fees
165                        .fee(ActionCosts::deploy_contract_byte)
166                        .clone(),
167                    function_call_cost: config.fees.fee(ActionCosts::function_call_base).clone(),
168                    function_call_cost_per_byte: config
169                        .fees
170                        .fee(ActionCosts::function_call_byte)
171                        .clone(),
172                    transfer_cost: config.fees.fee(ActionCosts::transfer).clone(),
173                    stake_cost: config.fees.fee(ActionCosts::stake).clone(),
174                    add_key_cost: AccessKeyCreationConfigView {
175                        full_access_cost: config.fees.fee(ActionCosts::add_full_access_key).clone(),
176                        function_call_cost: config
177                            .fees
178                            .fee(ActionCosts::add_function_call_key_base)
179                            .clone(),
180                        function_call_cost_per_byte: config
181                            .fees
182                            .fee(ActionCosts::add_function_call_key_byte)
183                            .clone(),
184                    },
185                    delete_key_cost: config.fees.fee(ActionCosts::delete_key).clone(),
186                    delete_account_cost: config.fees.fee(ActionCosts::delete_account).clone(),
187                    delegate_cost: config.fees.fee(ActionCosts::delegate).clone(),
188                },
189                storage_usage_config: StorageUsageConfigView {
190                    num_bytes_account: config.fees.storage_usage_config.num_bytes_account,
191                    num_extra_bytes_record: config.fees.storage_usage_config.num_extra_bytes_record,
192                },
193                burnt_gas_reward: config.fees.burnt_gas_reward,
194                pessimistic_gas_price_inflation_ratio: config
195                    .fees
196                    .pessimistic_gas_price_inflation_ratio,
197            },
198            wasm_config: VMConfigView::from(crate::vm::Config::clone(&config.wasm_config)),
199            account_creation_config: AccountCreationConfigView {
200                min_allowed_top_level_account_length: config
201                    .account_creation_config
202                    .min_allowed_top_level_account_length,
203                registrar_account_id: config.account_creation_config.registrar_account_id,
204            },
205            congestion_control_config: CongestionControlConfigView::from(
206                config.congestion_control_config,
207            ),
208            witness_config: WitnessConfigView::from(config.witness_config),
209        }
210    }
211}
212
213#[derive(Clone, Debug, Hash, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
214#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
215pub struct VMConfigView {
216    /// Costs for runtime externals
217    pub ext_costs: ExtCostsConfigView,
218
219    /// Gas cost of a growing memory by single page.
220    pub grow_mem_cost: u32,
221    /// Gas cost of a regular operation.
222    pub regular_op_cost: u32,
223
224    /// See [VMConfig::vm_kind](crate::vm::Config::vm_kind).
225    pub vm_kind: crate::vm::VMKind,
226    /// See [VMConfig::discard_custom_sections](crate::vm::Config::discard_custom_sections).
227    pub discard_custom_sections: bool,
228    /// See [VMConfig::saturating_float_to_int](crate::vm::Config::saturating_float_to_int).
229    pub saturating_float_to_int: bool,
230    /// See [VMConfig::global_contract_host_fns](crate::vm::Config::global_contract_host_fns).
231    pub global_contract_host_fns: bool,
232
233    /// See [VMConfig::storage_get_mode](crate::vm::Config::storage_get_mode).
234    pub storage_get_mode: crate::vm::StorageGetMode,
235    /// See [VMConfig::fix_contract_loading_cost](crate::vm::Config::fix_contract_loading_cost).
236    pub fix_contract_loading_cost: bool,
237    /// See [VMConfig::implicit_account_creation](crate::vm::Config::implicit_account_creation).
238    pub implicit_account_creation: bool,
239    /// See [VMConfig::eth_implicit_accounts](crate::vm::Config::eth_implicit_accounts).
240    pub eth_implicit_accounts: bool,
241
242    /// Describes limits for VM and Runtime.
243    ///
244    /// TODO: Consider changing this to `VMLimitConfigView` to avoid dependency
245    /// on runtime.
246    pub limit_config: crate::vm::LimitConfig,
247}
248
249impl From<crate::vm::Config> for VMConfigView {
250    fn from(config: crate::vm::Config) -> Self {
251        Self {
252            ext_costs: ExtCostsConfigView::from(config.ext_costs),
253            grow_mem_cost: config.grow_mem_cost,
254            regular_op_cost: config.regular_op_cost,
255            discard_custom_sections: config.discard_custom_sections,
256            limit_config: config.limit_config,
257            storage_get_mode: config.storage_get_mode,
258            fix_contract_loading_cost: config.fix_contract_loading_cost,
259            implicit_account_creation: config.implicit_account_creation,
260            vm_kind: config.vm_kind,
261            eth_implicit_accounts: config.eth_implicit_accounts,
262            saturating_float_to_int: config.saturating_float_to_int,
263            global_contract_host_fns: config.global_contract_host_fns,
264        }
265    }
266}
267
268impl From<VMConfigView> for crate::vm::Config {
269    fn from(view: VMConfigView) -> Self {
270        Self {
271            ext_costs: crate::ExtCostsConfig::from(view.ext_costs),
272            grow_mem_cost: view.grow_mem_cost,
273            regular_op_cost: view.regular_op_cost,
274            discard_custom_sections: view.discard_custom_sections,
275            limit_config: view.limit_config,
276            storage_get_mode: view.storage_get_mode,
277            fix_contract_loading_cost: view.fix_contract_loading_cost,
278            implicit_account_creation: view.implicit_account_creation,
279            vm_kind: view.vm_kind,
280            eth_implicit_accounts: view.eth_implicit_accounts,
281            saturating_float_to_int: view.saturating_float_to_int,
282            global_contract_host_fns: view.global_contract_host_fns,
283        }
284    }
285}
286
287/// Typed view of ExtCostsConfig to preserve JSON output field names in protocol
288/// config RPC output.
289#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, Hash, PartialEq, Eq)]
290#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
291pub struct ExtCostsConfigView {
292    /// Base cost for calling a host function.
293    pub base: Gas,
294
295    /// Base cost of loading a pre-compiled contract
296    pub contract_loading_base: Gas,
297    /// Cost per byte of loading a pre-compiled contract
298    pub contract_loading_bytes: Gas,
299
300    /// Base cost for guest memory read
301    pub read_memory_base: Gas,
302    /// Cost for guest memory read
303    pub read_memory_byte: Gas,
304
305    /// Base cost for guest memory write
306    pub write_memory_base: Gas,
307    /// Cost for guest memory write per byte
308    pub write_memory_byte: Gas,
309
310    /// Base cost for reading from register
311    pub read_register_base: Gas,
312    /// Cost for reading byte from register
313    pub read_register_byte: Gas,
314
315    /// Base cost for writing into register
316    pub write_register_base: Gas,
317    /// Cost for writing byte into register
318    pub write_register_byte: Gas,
319
320    /// Base cost of decoding utf8. It's used for `log_utf8` and `panic_utf8`.
321    pub utf8_decoding_base: Gas,
322    /// Cost per byte of decoding utf8. It's used for `log_utf8` and `panic_utf8`.
323    pub utf8_decoding_byte: Gas,
324
325    /// Base cost of decoding utf16. It's used for `log_utf16`.
326    pub utf16_decoding_base: Gas,
327    /// Cost per byte of decoding utf16. It's used for `log_utf16`.
328    pub utf16_decoding_byte: Gas,
329
330    /// Cost of getting sha256 base
331    pub sha256_base: Gas,
332    /// Cost of getting sha256 per byte
333    pub sha256_byte: Gas,
334
335    /// Cost of getting sha256 base
336    pub keccak256_base: Gas,
337    /// Cost of getting sha256 per byte
338    pub keccak256_byte: Gas,
339
340    /// Cost of getting sha256 base
341    pub keccak512_base: Gas,
342    /// Cost of getting sha256 per byte
343    pub keccak512_byte: Gas,
344
345    /// Cost of getting ripemd160 base
346    pub ripemd160_base: Gas,
347    /// Cost of getting ripemd160 per message block
348    pub ripemd160_block: Gas,
349
350    /// Cost of getting ed25519 base
351    pub ed25519_verify_base: Gas,
352    /// Cost of getting ed25519 per byte
353    pub ed25519_verify_byte: Gas,
354
355    /// Cost of calling ecrecover
356    pub ecrecover_base: Gas,
357
358    /// Cost for calling logging.
359    pub log_base: Gas,
360    /// Cost for logging per byte
361    pub log_byte: Gas,
362
363    // ###############
364    // # Storage API #
365    // ###############
366    /// Storage trie write key base cost
367    pub storage_write_base: Gas,
368    /// Storage trie write key per byte cost
369    pub storage_write_key_byte: Gas,
370    /// Storage trie write value per byte cost
371    pub storage_write_value_byte: Gas,
372    /// Storage trie write cost per byte of evicted value.
373    pub storage_write_evicted_byte: Gas,
374
375    /// Storage trie read key base cost
376    pub storage_read_base: Gas,
377    /// Storage trie read key per byte cost
378    pub storage_read_key_byte: Gas,
379    /// Storage trie read value cost per byte cost
380    pub storage_read_value_byte: Gas,
381
382    /// Storage trie read key overhead base cost, when doing large reads
383    pub storage_large_read_overhead_base: Gas,
384    /// Storage trie read key overhead  per-byte cost, when doing large reads
385    pub storage_large_read_overhead_byte: Gas,
386
387    /// Remove key from trie base cost
388    pub storage_remove_base: Gas,
389    /// Remove key from trie per byte cost
390    pub storage_remove_key_byte: Gas,
391    /// Remove key from trie ret value byte cost
392    pub storage_remove_ret_value_byte: Gas,
393
394    /// Storage trie check for key existence cost base
395    pub storage_has_key_base: Gas,
396    /// Storage trie check for key existence per key byte
397    pub storage_has_key_byte: Gas,
398
399    /// Create trie prefix iterator cost base
400    pub storage_iter_create_prefix_base: Gas,
401    /// Create trie prefix iterator cost per byte.
402    pub storage_iter_create_prefix_byte: Gas,
403
404    /// Create trie range iterator cost base
405    pub storage_iter_create_range_base: Gas,
406    /// Create trie range iterator cost per byte of from key.
407    pub storage_iter_create_from_byte: Gas,
408    /// Create trie range iterator cost per byte of to key.
409    pub storage_iter_create_to_byte: Gas,
410
411    /// Trie iterator per key base cost
412    pub storage_iter_next_base: Gas,
413    /// Trie iterator next key byte cost
414    pub storage_iter_next_key_byte: Gas,
415    /// Trie iterator next key byte cost
416    pub storage_iter_next_value_byte: Gas,
417
418    /// Cost per reading trie node from DB
419    pub touching_trie_node: Gas,
420    /// Cost for reading trie node from memory
421    pub read_cached_trie_node: Gas,
422
423    // ###############
424    // # Promise API #
425    // ###############
426    /// Cost for calling `promise_and`
427    pub promise_and_base: Gas,
428    /// Cost for calling `promise_and` for each promise
429    pub promise_and_per_promise: Gas,
430    /// Cost for calling `promise_return`
431    pub promise_return: Gas,
432
433    // ###############
434    // # Validator API #
435    // ###############
436    /// Cost of calling `validator_stake`.
437    pub validator_stake_base: Gas,
438    /// Cost of calling `validator_total_stake`.
439    pub validator_total_stake_base: Gas,
440
441    // Removed parameters, only here for keeping the output backward-compatible.
442    pub contract_compile_base: Gas,
443    pub contract_compile_bytes: Gas,
444
445    // #############
446    // # Alt BN128 #
447    // #############
448    /// Base cost for multiexp
449    pub alt_bn128_g1_multiexp_base: Gas,
450    /// Per element cost for multiexp
451    pub alt_bn128_g1_multiexp_element: Gas,
452    /// Base cost for sum
453    pub alt_bn128_g1_sum_base: Gas,
454    /// Per element cost for sum
455    pub alt_bn128_g1_sum_element: Gas,
456    /// Base cost for pairing check
457    pub alt_bn128_pairing_check_base: Gas,
458    /// Per element cost for pairing check
459    pub alt_bn128_pairing_check_element: Gas,
460    /// Base cost for creating a yield promise.
461    pub yield_create_base: Gas,
462    /// Per byte cost of arguments and method name.
463    pub yield_create_byte: Gas,
464    /// Base cost for resuming a yield receipt.
465    pub yield_resume_base: Gas,
466    /// Per byte cost of resume payload.
467    pub yield_resume_byte: Gas,
468    pub bls12381_p1_sum_base: Gas,
469    pub bls12381_p1_sum_element: Gas,
470    pub bls12381_p2_sum_base: Gas,
471    pub bls12381_p2_sum_element: Gas,
472    pub bls12381_g1_multiexp_base: Gas,
473    pub bls12381_g1_multiexp_element: Gas,
474    pub bls12381_g2_multiexp_base: Gas,
475    pub bls12381_g2_multiexp_element: Gas,
476    pub bls12381_map_fp_to_g1_base: Gas,
477    pub bls12381_map_fp_to_g1_element: Gas,
478    pub bls12381_map_fp2_to_g2_base: Gas,
479    pub bls12381_map_fp2_to_g2_element: Gas,
480    pub bls12381_pairing_base: Gas,
481    pub bls12381_pairing_element: Gas,
482    pub bls12381_p1_decompress_base: Gas,
483    pub bls12381_p1_decompress_element: Gas,
484    pub bls12381_p2_decompress_base: Gas,
485    pub bls12381_p2_decompress_element: Gas,
486}
487
488impl From<crate::ExtCostsConfig> for ExtCostsConfigView {
489    fn from(config: crate::ExtCostsConfig) -> Self {
490        Self {
491            base: config.gas_cost(ExtCosts::base),
492            contract_loading_base: config.gas_cost(ExtCosts::contract_loading_base),
493            contract_loading_bytes: config.gas_cost(ExtCosts::contract_loading_bytes),
494            read_memory_base: config.gas_cost(ExtCosts::read_memory_base),
495            read_memory_byte: config.gas_cost(ExtCosts::read_memory_byte),
496            write_memory_base: config.gas_cost(ExtCosts::write_memory_base),
497            write_memory_byte: config.gas_cost(ExtCosts::write_memory_byte),
498            read_register_base: config.gas_cost(ExtCosts::read_register_base),
499            read_register_byte: config.gas_cost(ExtCosts::read_register_byte),
500            write_register_base: config.gas_cost(ExtCosts::write_register_base),
501            write_register_byte: config.gas_cost(ExtCosts::write_register_byte),
502            utf8_decoding_base: config.gas_cost(ExtCosts::utf8_decoding_base),
503            utf8_decoding_byte: config.gas_cost(ExtCosts::utf8_decoding_byte),
504            utf16_decoding_base: config.gas_cost(ExtCosts::utf16_decoding_base),
505            utf16_decoding_byte: config.gas_cost(ExtCosts::utf16_decoding_byte),
506            sha256_base: config.gas_cost(ExtCosts::sha256_base),
507            sha256_byte: config.gas_cost(ExtCosts::sha256_byte),
508            keccak256_base: config.gas_cost(ExtCosts::keccak256_base),
509            keccak256_byte: config.gas_cost(ExtCosts::keccak256_byte),
510            keccak512_base: config.gas_cost(ExtCosts::keccak512_base),
511            keccak512_byte: config.gas_cost(ExtCosts::keccak512_byte),
512            ripemd160_base: config.gas_cost(ExtCosts::ripemd160_base),
513            ripemd160_block: config.gas_cost(ExtCosts::ripemd160_block),
514            ed25519_verify_base: config.gas_cost(ExtCosts::ed25519_verify_base),
515            ed25519_verify_byte: config.gas_cost(ExtCosts::ed25519_verify_byte),
516            ecrecover_base: config.gas_cost(ExtCosts::ecrecover_base),
517            log_base: config.gas_cost(ExtCosts::log_base),
518            log_byte: config.gas_cost(ExtCosts::log_byte),
519            storage_write_base: config.gas_cost(ExtCosts::storage_write_base),
520            storage_write_key_byte: config.gas_cost(ExtCosts::storage_write_key_byte),
521            storage_write_value_byte: config.gas_cost(ExtCosts::storage_write_value_byte),
522            storage_write_evicted_byte: config.gas_cost(ExtCosts::storage_write_evicted_byte),
523            storage_read_base: config.gas_cost(ExtCosts::storage_read_base),
524            storage_read_key_byte: config.gas_cost(ExtCosts::storage_read_key_byte),
525            storage_read_value_byte: config.gas_cost(ExtCosts::storage_read_value_byte),
526            storage_large_read_overhead_base: config
527                .gas_cost(ExtCosts::storage_large_read_overhead_base),
528            storage_large_read_overhead_byte: config
529                .gas_cost(ExtCosts::storage_large_read_overhead_byte),
530            storage_remove_base: config.gas_cost(ExtCosts::storage_remove_base),
531            storage_remove_key_byte: config.gas_cost(ExtCosts::storage_remove_key_byte),
532            storage_remove_ret_value_byte: config.gas_cost(ExtCosts::storage_remove_ret_value_byte),
533            storage_has_key_base: config.gas_cost(ExtCosts::storage_has_key_base),
534            storage_has_key_byte: config.gas_cost(ExtCosts::storage_has_key_byte),
535            storage_iter_create_prefix_base: config
536                .gas_cost(ExtCosts::storage_iter_create_prefix_base),
537            storage_iter_create_prefix_byte: config
538                .gas_cost(ExtCosts::storage_iter_create_prefix_byte),
539            storage_iter_create_range_base: config
540                .gas_cost(ExtCosts::storage_iter_create_range_base),
541            storage_iter_create_from_byte: config.gas_cost(ExtCosts::storage_iter_create_from_byte),
542            storage_iter_create_to_byte: config.gas_cost(ExtCosts::storage_iter_create_to_byte),
543            storage_iter_next_base: config.gas_cost(ExtCosts::storage_iter_next_base),
544            storage_iter_next_key_byte: config.gas_cost(ExtCosts::storage_iter_next_key_byte),
545            storage_iter_next_value_byte: config.gas_cost(ExtCosts::storage_iter_next_value_byte),
546            touching_trie_node: config.gas_cost(ExtCosts::touching_trie_node),
547            read_cached_trie_node: config.gas_cost(ExtCosts::read_cached_trie_node),
548            promise_and_base: config.gas_cost(ExtCosts::promise_and_base),
549            promise_and_per_promise: config.gas_cost(ExtCosts::promise_and_per_promise),
550            promise_return: config.gas_cost(ExtCosts::promise_return),
551            validator_stake_base: config.gas_cost(ExtCosts::validator_stake_base),
552            validator_total_stake_base: config.gas_cost(ExtCosts::validator_total_stake_base),
553            alt_bn128_g1_multiexp_base: config.gas_cost(ExtCosts::alt_bn128_g1_multiexp_base),
554            alt_bn128_g1_multiexp_element: config.gas_cost(ExtCosts::alt_bn128_g1_multiexp_element),
555            alt_bn128_g1_sum_base: config.gas_cost(ExtCosts::alt_bn128_g1_sum_base),
556            alt_bn128_g1_sum_element: config.gas_cost(ExtCosts::alt_bn128_g1_sum_element),
557            alt_bn128_pairing_check_base: config.gas_cost(ExtCosts::alt_bn128_pairing_check_base),
558            alt_bn128_pairing_check_element: config
559                .gas_cost(ExtCosts::alt_bn128_pairing_check_element),
560            yield_create_base: config.gas_cost(ExtCosts::yield_create_base),
561            yield_create_byte: config.gas_cost(ExtCosts::yield_create_byte),
562            yield_resume_base: config.gas_cost(ExtCosts::yield_resume_base),
563            yield_resume_byte: config.gas_cost(ExtCosts::yield_resume_byte),
564            bls12381_p1_sum_base: config.gas_cost(ExtCosts::bls12381_p1_sum_base),
565            bls12381_p1_sum_element: config.gas_cost(ExtCosts::bls12381_p1_sum_element),
566            bls12381_p2_sum_base: config.gas_cost(ExtCosts::bls12381_p2_sum_base),
567            bls12381_p2_sum_element: config.gas_cost(ExtCosts::bls12381_p2_sum_element),
568            bls12381_g1_multiexp_base: config.gas_cost(ExtCosts::bls12381_g1_multiexp_base),
569            bls12381_g1_multiexp_element: config.gas_cost(ExtCosts::bls12381_g1_multiexp_element),
570            bls12381_g2_multiexp_base: config.gas_cost(ExtCosts::bls12381_g2_multiexp_base),
571            bls12381_g2_multiexp_element: config.gas_cost(ExtCosts::bls12381_g2_multiexp_element),
572            bls12381_map_fp_to_g1_base: config.gas_cost(ExtCosts::bls12381_map_fp_to_g1_base),
573            bls12381_map_fp_to_g1_element: config.gas_cost(ExtCosts::bls12381_map_fp_to_g1_element),
574            bls12381_map_fp2_to_g2_base: config.gas_cost(ExtCosts::bls12381_map_fp2_to_g2_base),
575            bls12381_map_fp2_to_g2_element: config
576                .gas_cost(ExtCosts::bls12381_map_fp2_to_g2_element),
577            bls12381_pairing_base: config.gas_cost(ExtCosts::bls12381_pairing_base),
578            bls12381_pairing_element: config.gas_cost(ExtCosts::bls12381_pairing_element),
579            bls12381_p1_decompress_base: config.gas_cost(ExtCosts::bls12381_p1_decompress_base),
580            bls12381_p1_decompress_element: config
581                .gas_cost(ExtCosts::bls12381_p1_decompress_element),
582            bls12381_p2_decompress_base: config.gas_cost(ExtCosts::bls12381_p2_decompress_base),
583            bls12381_p2_decompress_element: config
584                .gas_cost(ExtCosts::bls12381_p2_decompress_element),
585            // removed parameters
586            contract_compile_base: 0,
587            contract_compile_bytes: 0,
588        }
589    }
590}
591
592impl From<ExtCostsConfigView> for crate::ExtCostsConfig {
593    fn from(view: ExtCostsConfigView) -> Self {
594        let costs = enum_map::enum_map! {
595                ExtCosts::base => view.base,
596                ExtCosts::contract_loading_base => view.contract_loading_base,
597                ExtCosts::contract_loading_bytes => view.contract_loading_bytes,
598                ExtCosts::read_memory_base => view.read_memory_base,
599                ExtCosts::read_memory_byte => view.read_memory_byte,
600                ExtCosts::write_memory_base => view.write_memory_base,
601                ExtCosts::write_memory_byte => view.write_memory_byte,
602                ExtCosts::read_register_base => view.read_register_base,
603                ExtCosts::read_register_byte => view.read_register_byte,
604                ExtCosts::write_register_base => view.write_register_base,
605                ExtCosts::write_register_byte => view.write_register_byte,
606                ExtCosts::utf8_decoding_base => view.utf8_decoding_base,
607                ExtCosts::utf8_decoding_byte => view.utf8_decoding_byte,
608                ExtCosts::utf16_decoding_base => view.utf16_decoding_base,
609                ExtCosts::utf16_decoding_byte => view.utf16_decoding_byte,
610                ExtCosts::sha256_base => view.sha256_base,
611                ExtCosts::sha256_byte => view.sha256_byte,
612                ExtCosts::keccak256_base => view.keccak256_base,
613                ExtCosts::keccak256_byte => view.keccak256_byte,
614                ExtCosts::keccak512_base => view.keccak512_base,
615                ExtCosts::keccak512_byte => view.keccak512_byte,
616                ExtCosts::ripemd160_base => view.ripemd160_base,
617                ExtCosts::ripemd160_block => view.ripemd160_block,
618                ExtCosts::ed25519_verify_base => view.ed25519_verify_base,
619                ExtCosts::ed25519_verify_byte => view.ed25519_verify_byte,
620                ExtCosts::ecrecover_base => view.ecrecover_base,
621                ExtCosts::log_base => view.log_base,
622                ExtCosts::log_byte => view.log_byte,
623                ExtCosts::storage_write_base => view.storage_write_base,
624                ExtCosts::storage_write_key_byte => view.storage_write_key_byte,
625                ExtCosts::storage_write_value_byte => view.storage_write_value_byte,
626                ExtCosts::storage_write_evicted_byte => view.storage_write_evicted_byte,
627                ExtCosts::storage_read_base => view.storage_read_base,
628                ExtCosts::storage_read_key_byte => view.storage_read_key_byte,
629                ExtCosts::storage_read_value_byte => view.storage_read_value_byte,
630                ExtCosts::storage_large_read_overhead_base => view.storage_large_read_overhead_base,
631                ExtCosts::storage_large_read_overhead_byte => view.storage_large_read_overhead_byte,
632                ExtCosts::storage_remove_base => view.storage_remove_base,
633                ExtCosts::storage_remove_key_byte => view.storage_remove_key_byte,
634                ExtCosts::storage_remove_ret_value_byte => view.storage_remove_ret_value_byte,
635                ExtCosts::storage_has_key_base => view.storage_has_key_base,
636                ExtCosts::storage_has_key_byte => view.storage_has_key_byte,
637                ExtCosts::storage_iter_create_prefix_base => view.storage_iter_create_prefix_base,
638                ExtCosts::storage_iter_create_prefix_byte => view.storage_iter_create_prefix_byte,
639                ExtCosts::storage_iter_create_range_base => view.storage_iter_create_range_base,
640                ExtCosts::storage_iter_create_from_byte => view.storage_iter_create_from_byte,
641                ExtCosts::storage_iter_create_to_byte => view.storage_iter_create_to_byte,
642                ExtCosts::storage_iter_next_base => view.storage_iter_next_base,
643                ExtCosts::storage_iter_next_key_byte => view.storage_iter_next_key_byte,
644                ExtCosts::storage_iter_next_value_byte => view.storage_iter_next_value_byte,
645                ExtCosts::touching_trie_node => view.touching_trie_node,
646                ExtCosts::read_cached_trie_node => view.read_cached_trie_node,
647                ExtCosts::promise_and_base => view.promise_and_base,
648                ExtCosts::promise_and_per_promise => view.promise_and_per_promise,
649                ExtCosts::promise_return => view.promise_return,
650                ExtCosts::validator_stake_base => view.validator_stake_base,
651                ExtCosts::validator_total_stake_base => view.validator_total_stake_base,
652                ExtCosts::alt_bn128_g1_multiexp_base => view.alt_bn128_g1_multiexp_base,
653                ExtCosts::alt_bn128_g1_multiexp_element => view.alt_bn128_g1_multiexp_element,
654                ExtCosts::alt_bn128_g1_sum_base => view.alt_bn128_g1_sum_base,
655                ExtCosts::alt_bn128_g1_sum_element => view.alt_bn128_g1_sum_element,
656                ExtCosts::alt_bn128_pairing_check_base => view.alt_bn128_pairing_check_base,
657                ExtCosts::alt_bn128_pairing_check_element => view.alt_bn128_pairing_check_element,
658                ExtCosts::yield_create_base => view.yield_create_base,
659                ExtCosts::yield_create_byte => view.yield_create_byte,
660                ExtCosts::yield_resume_base => view.yield_resume_base,
661                ExtCosts::yield_resume_byte => view.yield_resume_byte,
662                ExtCosts::bls12381_p1_sum_base => view.bls12381_p1_sum_base,
663                ExtCosts::bls12381_p1_sum_element => view.bls12381_p1_sum_element,
664                ExtCosts::bls12381_p2_sum_base => view.bls12381_p2_sum_base,
665                ExtCosts::bls12381_p2_sum_element => view.bls12381_p2_sum_element,
666                ExtCosts::bls12381_g1_multiexp_base => view.bls12381_g1_multiexp_base,
667                ExtCosts::bls12381_g1_multiexp_element => view.bls12381_g1_multiexp_element,
668                ExtCosts::bls12381_g2_multiexp_base => view.bls12381_g2_multiexp_base,
669                ExtCosts::bls12381_g2_multiexp_element => view.bls12381_g2_multiexp_element,
670                ExtCosts::bls12381_map_fp_to_g1_base => view.bls12381_map_fp_to_g1_base,
671                ExtCosts::bls12381_map_fp_to_g1_element => view.bls12381_map_fp_to_g1_element,
672                ExtCosts::bls12381_map_fp2_to_g2_base => view.bls12381_map_fp2_to_g2_base,
673                ExtCosts::bls12381_map_fp2_to_g2_element => view.bls12381_map_fp2_to_g2_element,
674                ExtCosts::bls12381_pairing_base => view.bls12381_pairing_base,
675                ExtCosts::bls12381_pairing_element => view.bls12381_pairing_element,
676                ExtCosts::bls12381_p1_decompress_base => view.bls12381_p1_decompress_base,
677                ExtCosts::bls12381_p1_decompress_element => view.bls12381_p1_decompress_element,
678                ExtCosts::bls12381_p2_decompress_base => view.bls12381_p2_decompress_base,
679                ExtCosts::bls12381_p2_decompress_element => view.bls12381_p2_decompress_element,
680        }
681        .map(|_, value| ParameterCost { gas: value, compute: value });
682        Self { costs }
683    }
684}
685
686/// Configuration specific to ChunkStateWitness.
687#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, Hash, PartialEq, Eq)]
688#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
689pub struct WitnessConfigView {
690    /// Size limit for storage proof generated while executing receipts in a chunk.
691    /// After this limit is reached we defer execution of any new receipts.
692    pub main_storage_proof_size_soft_limit: usize,
693    // Maximum size of transactions contained inside ChunkStateWitness.
694    /// A witness contains transactions from both the previous chunk and the current one.
695    /// This parameter limits the sum of sizes of transactions from both of those chunks.
696    pub combined_transactions_size_limit: usize,
697    /// Soft size limit of storage proof used to validate new transactions in ChunkStateWitness.
698    pub new_transactions_validation_state_size_soft_limit: usize,
699}
700
701impl From<WitnessConfig> for WitnessConfigView {
702    fn from(config: WitnessConfig) -> Self {
703        Self {
704            main_storage_proof_size_soft_limit: config.main_storage_proof_size_soft_limit,
705            combined_transactions_size_limit: config.combined_transactions_size_limit,
706            new_transactions_validation_state_size_soft_limit: config
707                .new_transactions_validation_state_size_soft_limit,
708        }
709    }
710}
711
712#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, PartialEq)]
713#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
714pub struct CongestionControlConfigView {
715    /// How much gas in delayed receipts of a shard is 100% incoming congestion.
716    ///
717    /// See [`CongestionControlConfig`] for more details.
718    pub max_congestion_incoming_gas: Gas,
719
720    /// How much gas in outgoing buffered receipts of a shard is 100% congested.
721    ///
722    /// Outgoing congestion contributes to overall congestion, which reduces how
723    /// much other shards are allowed to forward to this shard.
724    pub max_congestion_outgoing_gas: Gas,
725
726    /// How much memory space of all delayed and buffered receipts in a shard is
727    /// considered 100% congested.
728    ///
729    /// See [`CongestionControlConfig`] for more details.
730    pub max_congestion_memory_consumption: u64,
731
732    /// How many missed chunks in a row in a shard is considered 100% congested.
733    pub max_congestion_missed_chunks: u64,
734
735    /// The maximum amount of gas attached to receipts a shard can forward to
736    /// another shard per chunk.
737    ///
738    /// See [`CongestionControlConfig`] for more details.
739    pub max_outgoing_gas: Gas,
740
741    /// The minimum gas each shard can send to a shard that is not fully congested.
742    ///
743    /// See [`CongestionControlConfig`] for more details.
744    pub min_outgoing_gas: Gas,
745
746    /// How much gas the chosen allowed shard can send to a 100% congested shard.
747    ///
748    /// See [`CongestionControlConfig`] for more details.
749    pub allowed_shard_outgoing_gas: Gas,
750
751    /// The maximum amount of gas in a chunk spent on converting new transactions to
752    /// receipts.
753    ///
754    /// See [`CongestionControlConfig`] for more details.
755    pub max_tx_gas: Gas,
756
757    /// The minimum amount of gas in a chunk spent on converting new transactions
758    /// to receipts, as long as the receiving shard is not congested.
759    ///
760    /// See [`CongestionControlConfig`] for more details.
761    pub min_tx_gas: Gas,
762
763    /// How much congestion a shard can tolerate before it stops all shards from
764    /// accepting new transactions with the receiver set to the congested shard.
765    pub reject_tx_congestion_threshold: f64,
766
767    /// The standard size limit for outgoing receipts aimed at a single shard.
768    /// This limit is pretty small to keep the size of source_receipt_proofs under control.
769    /// It limits the total sum of outgoing receipts, not individual receipts.
770    pub outgoing_receipts_usual_size_limit: u64,
771
772    /// Large size limit for outgoing receipts to a shard, used when it's safe
773    /// to send a lot of receipts without making the state witness too large.
774    /// It limits the total sum of outgoing receipts, not individual receipts.
775    pub outgoing_receipts_big_size_limit: u64,
776}
777
778impl From<CongestionControlConfig> for CongestionControlConfigView {
779    fn from(other: CongestionControlConfig) -> Self {
780        Self {
781            max_congestion_incoming_gas: other.max_congestion_incoming_gas,
782            max_congestion_outgoing_gas: other.max_congestion_outgoing_gas,
783            max_congestion_memory_consumption: other.max_congestion_memory_consumption,
784            max_congestion_missed_chunks: other.max_congestion_missed_chunks,
785            max_outgoing_gas: other.max_outgoing_gas,
786            min_outgoing_gas: other.min_outgoing_gas,
787            allowed_shard_outgoing_gas: other.allowed_shard_outgoing_gas,
788            max_tx_gas: other.max_tx_gas,
789            min_tx_gas: other.min_tx_gas,
790            reject_tx_congestion_threshold: other.reject_tx_congestion_threshold,
791            outgoing_receipts_usual_size_limit: other.outgoing_receipts_usual_size_limit,
792            outgoing_receipts_big_size_limit: other.outgoing_receipts_big_size_limit,
793        }
794    }
795}
796
797impl From<CongestionControlConfigView> for CongestionControlConfig {
798    fn from(other: CongestionControlConfigView) -> Self {
799        Self {
800            max_congestion_incoming_gas: other.max_congestion_incoming_gas,
801            max_congestion_outgoing_gas: other.max_congestion_outgoing_gas,
802            max_congestion_memory_consumption: other.max_congestion_memory_consumption,
803            max_congestion_missed_chunks: other.max_congestion_missed_chunks,
804            max_outgoing_gas: other.max_outgoing_gas,
805            min_outgoing_gas: other.min_outgoing_gas,
806            allowed_shard_outgoing_gas: other.allowed_shard_outgoing_gas,
807            max_tx_gas: other.max_tx_gas,
808            min_tx_gas: other.min_tx_gas,
809            reject_tx_congestion_threshold: other.reject_tx_congestion_threshold,
810            outgoing_receipts_usual_size_limit: other.outgoing_receipts_usual_size_limit,
811            outgoing_receipts_big_size_limit: other.outgoing_receipts_big_size_limit,
812        }
813    }
814}
815
816#[cfg(feature = "schemars")]
817pub type Rational32SchemarsProvider = [i32; 2];
818
819#[cfg(test)]
820#[cfg(not(feature = "nightly"))]
821mod tests {
822    /// The JSON representation used in RPC responses must not remove or rename
823    /// fields, only adding fields is allowed or we risk breaking clients.
824    #[test]
825    fn test_runtime_config_view() {
826        use crate::RuntimeConfig;
827        use crate::RuntimeConfigStore;
828        use crate::view::RuntimeConfigView;
829        use near_primitives_core::version::PROTOCOL_VERSION;
830
831        let config_store = RuntimeConfigStore::new(None);
832        let config = config_store.get_config(PROTOCOL_VERSION);
833        let view = RuntimeConfigView::from(RuntimeConfig::clone(config));
834        insta::assert_json_snapshot!(&view, { ".wasm_config.vm_kind" => "<REDACTED>"});
835    }
836}