near_parameters/
view.rs

1use crate::config::{CongestionControlConfig, WitnessConfig};
2use crate::{ActionCosts, ExtCosts, Fee, ParameterCost};
3use near_account_id::AccountId;
4use near_primitives_core::types::Balance;
5use near_primitives_core::types::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    pub storage_amount_per_byte: Balance,
15    /// Costs of different actions that need to be performed when sending and
16    /// processing transaction and receipts.
17    pub transaction_costs: RuntimeFeesConfigView,
18    /// Config of wasm operations.
19    pub wasm_config: VMConfigView,
20    /// Config that defines rules for account creation.
21    pub account_creation_config: AccountCreationConfigView,
22    /// The configuration for congestion control.
23    pub congestion_control_config: CongestionControlConfigView,
24    /// Configuration specific to ChunkStateWitness.
25    pub witness_config: WitnessConfigView,
26}
27
28/// Describes different fees for the runtime
29#[derive(Debug, serde::Serialize, serde::Deserialize, Clone)]
30#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
31pub struct RuntimeFeesConfigView {
32    /// Describes the cost of creating an action receipt, `ActionReceipt`, excluding the actual cost
33    /// of actions.
34    /// - `send` cost is burned when a receipt is created using `promise_create` or
35    ///     `promise_batch_create`
36    /// - `exec` cost is burned when the receipt is being executed.
37    pub action_receipt_creation_config: Fee,
38    /// Describes the cost of creating a data receipt, `DataReceipt`.
39    pub data_receipt_creation_config: DataReceiptCreationConfigView,
40    /// Describes the cost of creating a certain action, `Action`. Includes all variants.
41    pub action_creation_config: ActionCreationConfigView,
42    /// Describes fees for storage.
43    pub storage_usage_config: StorageUsageConfigView,
44
45    /// Fraction of the burnt gas to reward to the contract account for execution.
46    #[cfg_attr(feature = "schemars", schemars(with = "Rational32SchemarsProvider"))]
47    pub burnt_gas_reward: Rational32,
48
49    /// Pessimistic gas price inflation ratio.
50    #[cfg_attr(feature = "schemars", schemars(with = "Rational32SchemarsProvider"))]
51    pub pessimistic_gas_price_inflation_ratio: Rational32,
52}
53
54/// The structure describes configuration for creation of new accounts.
55#[derive(Debug, serde::Serialize, serde::Deserialize, Clone)]
56#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
57pub struct AccountCreationConfigView {
58    /// The minimum length of the top-level account ID that is allowed to be created by any account.
59    pub min_allowed_top_level_account_length: u8,
60    /// The account ID of the account registrar. This account ID allowed to create top-level
61    /// accounts of any valid length.
62    pub registrar_account_id: AccountId,
63}
64
65/// The fees settings for a data receipt creation
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    /// Base gas cost of a linear operation
224    pub linear_op_base_cost: u64,
225    /// Unit gas cost of a linear operation
226    pub linear_op_unit_cost: u64,
227
228    /// See [VMConfig::vm_kind](crate::vm::Config::vm_kind).
229    pub vm_kind: crate::vm::VMKind,
230    /// See [VMConfig::discard_custom_sections](crate::vm::Config::discard_custom_sections).
231    pub discard_custom_sections: bool,
232    /// See [VMConfig::saturating_float_to_int](crate::vm::Config::saturating_float_to_int).
233    pub saturating_float_to_int: bool,
234    /// See [VMConfig::global_contract_host_fns](crate::vm::Config::global_contract_host_fns).
235    pub global_contract_host_fns: bool,
236    /// See [VMConfig::reftypes_bulk_memory](crate::vm::Config::reftypes_bulk_memory).
237    pub reftypes_bulk_memory: bool,
238    /// See [VMConfig::deterministic_account_ids](crate::vm::Config::deterministic_account_ids).
239    pub deterministic_account_ids: bool,
240
241    /// See [VMConfig::storage_get_mode](crate::vm::Config::storage_get_mode).
242    pub storage_get_mode: crate::vm::StorageGetMode,
243    /// See [VMConfig::fix_contract_loading_cost](crate::vm::Config::fix_contract_loading_cost).
244    pub fix_contract_loading_cost: bool,
245    /// See [VMConfig::implicit_account_creation](crate::vm::Config::implicit_account_creation).
246    pub implicit_account_creation: bool,
247    /// See [VMConfig::eth_implicit_accounts](crate::vm::Config::eth_implicit_accounts).
248    pub eth_implicit_accounts: bool,
249
250    /// Describes limits for VM and Runtime.
251    ///
252    /// TODO: Consider changing this to `VMLimitConfigView` to avoid dependency
253    /// on runtime.
254    pub limit_config: crate::vm::LimitConfig,
255}
256
257impl From<crate::vm::Config> for VMConfigView {
258    fn from(config: crate::vm::Config) -> Self {
259        Self {
260            ext_costs: ExtCostsConfigView::from(config.ext_costs),
261            grow_mem_cost: config.grow_mem_cost,
262            regular_op_cost: config.regular_op_cost,
263            linear_op_base_cost: config.linear_op_base_cost,
264            linear_op_unit_cost: config.linear_op_unit_cost,
265            discard_custom_sections: config.discard_custom_sections,
266            limit_config: config.limit_config,
267            storage_get_mode: config.storage_get_mode,
268            fix_contract_loading_cost: config.fix_contract_loading_cost,
269            implicit_account_creation: config.implicit_account_creation,
270            vm_kind: config.vm_kind,
271            eth_implicit_accounts: config.eth_implicit_accounts,
272            saturating_float_to_int: config.saturating_float_to_int,
273            global_contract_host_fns: config.global_contract_host_fns,
274            reftypes_bulk_memory: config.reftypes_bulk_memory,
275            deterministic_account_ids: config.deterministic_account_ids,
276        }
277    }
278}
279
280impl From<VMConfigView> for crate::vm::Config {
281    fn from(view: VMConfigView) -> Self {
282        Self {
283            ext_costs: crate::ExtCostsConfig::from(view.ext_costs),
284            grow_mem_cost: view.grow_mem_cost,
285            regular_op_cost: view.regular_op_cost,
286            linear_op_base_cost: view.linear_op_base_cost,
287            linear_op_unit_cost: view.linear_op_unit_cost,
288            discard_custom_sections: view.discard_custom_sections,
289            limit_config: view.limit_config,
290            storage_get_mode: view.storage_get_mode,
291            fix_contract_loading_cost: view.fix_contract_loading_cost,
292            implicit_account_creation: view.implicit_account_creation,
293            vm_kind: view.vm_kind,
294            eth_implicit_accounts: view.eth_implicit_accounts,
295            saturating_float_to_int: view.saturating_float_to_int,
296            global_contract_host_fns: view.global_contract_host_fns,
297            reftypes_bulk_memory: view.reftypes_bulk_memory,
298            deterministic_account_ids: view.deterministic_account_ids,
299        }
300    }
301}
302
303/// Typed view of ExtCostsConfig to preserve JSON output field names in protocol
304/// config RPC output.
305#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, Hash, PartialEq, Eq)]
306#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
307pub struct ExtCostsConfigView {
308    /// Base cost for calling a host function.
309    pub base: Gas,
310
311    /// Base cost of loading a pre-compiled contract
312    pub contract_loading_base: Gas,
313    /// Cost per byte of loading a pre-compiled contract
314    pub contract_loading_bytes: Gas,
315
316    /// Base cost for guest memory read
317    pub read_memory_base: Gas,
318    /// Cost for guest memory read
319    pub read_memory_byte: Gas,
320
321    /// Base cost for guest memory write
322    pub write_memory_base: Gas,
323
324    /// Cost for guest memory write per byte
325    pub write_memory_byte: Gas,
326
327    /// Base cost for reading from register
328    pub read_register_base: Gas,
329    /// Cost for reading byte from register
330    pub read_register_byte: Gas,
331
332    /// Base cost for writing into register
333    pub write_register_base: Gas,
334    /// Cost for writing byte into register
335    pub write_register_byte: Gas,
336
337    /// Base cost of decoding utf8. It's used for `log_utf8` and `panic_utf8`.
338    pub utf8_decoding_base: Gas,
339    /// Cost per byte of decoding utf8. It's used for `log_utf8` and `panic_utf8`.
340    pub utf8_decoding_byte: Gas,
341
342    /// Base cost of decoding utf16. It's used for `log_utf16`.
343    pub utf16_decoding_base: Gas,
344    /// Cost per byte of decoding utf16. It's used for `log_utf16`.
345    pub utf16_decoding_byte: Gas,
346
347    /// Cost of getting sha256 base
348    pub sha256_base: Gas,
349    /// Cost of getting sha256 per byte
350    pub sha256_byte: Gas,
351
352    /// Cost of getting sha256 base
353    pub keccak256_base: Gas,
354    /// Cost of getting sha256 per byte
355    pub keccak256_byte: Gas,
356
357    /// Cost of getting sha256 base
358    pub keccak512_base: Gas,
359    /// Cost of getting sha256 per byte
360    pub keccak512_byte: Gas,
361
362    /// Cost of getting ripemd160 base
363    pub ripemd160_base: Gas,
364    /// Cost of getting ripemd160 per message block
365    pub ripemd160_block: Gas,
366
367    /// Cost of getting ed25519 base
368    pub ed25519_verify_base: Gas,
369    /// Cost of getting ed25519 per byte
370    pub ed25519_verify_byte: Gas,
371
372    /// Cost of calling ecrecover
373    pub ecrecover_base: Gas,
374
375    /// Cost for calling logging.
376    pub log_base: Gas,
377    /// Cost for logging per byte
378    pub log_byte: Gas,
379
380    // ###############
381    // # Storage API #
382    // ###############
383    /// Storage trie write key base cost
384    pub storage_write_base: Gas,
385    /// Storage trie write key per byte cost
386    pub storage_write_key_byte: Gas,
387    /// Storage trie write value per byte cost
388    pub storage_write_value_byte: Gas,
389    /// Storage trie write cost per byte of evicted value.
390    pub storage_write_evicted_byte: Gas,
391
392    /// Storage trie read key base cost
393    pub storage_read_base: Gas,
394    /// Storage trie read key per byte cost
395    pub storage_read_key_byte: Gas,
396    /// Storage trie read value cost per byte cost
397    pub storage_read_value_byte: Gas,
398
399    /// Storage trie read key overhead base cost, when doing large reads
400    pub storage_large_read_overhead_base: Gas,
401    /// Storage trie read key overhead  per-byte cost, when doing large reads
402    pub storage_large_read_overhead_byte: Gas,
403
404    /// Remove key from trie base cost
405    pub storage_remove_base: Gas,
406    /// Remove key from trie per byte cost
407    pub storage_remove_key_byte: Gas,
408    /// Remove key from trie ret value byte cost
409    pub storage_remove_ret_value_byte: Gas,
410
411    /// Storage trie check for key existence cost base
412    pub storage_has_key_base: Gas,
413    /// Storage trie check for key existence per key byte
414    pub storage_has_key_byte: Gas,
415
416    /// Create trie prefix iterator cost base
417    pub storage_iter_create_prefix_base: Gas,
418    /// Create trie prefix iterator cost per byte.
419    pub storage_iter_create_prefix_byte: Gas,
420
421    /// Create trie range iterator cost base
422    pub storage_iter_create_range_base: Gas,
423    /// Create trie range iterator cost per byte of from key.
424    pub storage_iter_create_from_byte: Gas,
425    /// Create trie range iterator cost per byte of to key.
426    pub storage_iter_create_to_byte: Gas,
427
428    /// Trie iterator per key base cost
429    pub storage_iter_next_base: Gas,
430    /// Trie iterator next key byte cost
431    pub storage_iter_next_key_byte: Gas,
432    /// Trie iterator next key byte cost
433    pub storage_iter_next_value_byte: Gas,
434
435    /// Cost per reading trie node from DB
436    pub touching_trie_node: Gas,
437    /// Cost for reading trie node from memory
438    pub read_cached_trie_node: Gas,
439
440    // ###############
441    // # Promise API #
442    // ###############
443    /// Cost for calling `promise_and`
444    pub promise_and_base: Gas,
445    /// Cost for calling `promise_and` for each promise
446    pub promise_and_per_promise: Gas,
447    /// Cost for calling `promise_return`
448    pub promise_return: Gas,
449
450    // ###############
451    // # Validator API #
452    // ###############
453    /// Cost of calling `validator_stake`.
454    pub validator_stake_base: Gas,
455    /// Cost of calling `validator_total_stake`.
456    pub validator_total_stake_base: Gas,
457
458    // Removed parameters, only here for keeping the output backward-compatible.
459    pub contract_compile_base: Gas,
460    pub contract_compile_bytes: Gas,
461
462    // #############
463    // # Alt BN128 #
464    // #############
465    /// Base cost for multiexp
466    pub alt_bn128_g1_multiexp_base: Gas,
467    /// Per element cost for multiexp
468    pub alt_bn128_g1_multiexp_element: Gas,
469    /// Base cost for sum
470    pub alt_bn128_g1_sum_base: Gas,
471    /// Per element cost for sum
472    pub alt_bn128_g1_sum_element: Gas,
473    /// Base cost for pairing check
474    pub alt_bn128_pairing_check_base: Gas,
475    /// Per element cost for pairing check
476    pub alt_bn128_pairing_check_element: Gas,
477    /// Base cost for creating a yield promise.
478    pub yield_create_base: Gas,
479    /// Per byte cost of arguments and method name.
480    pub yield_create_byte: Gas,
481    /// Base cost for resuming a yield receipt.
482    pub yield_resume_base: Gas,
483    /// Per byte cost of resume payload.
484    pub yield_resume_byte: Gas,
485    pub bls12381_p1_sum_base: Gas,
486    pub bls12381_p1_sum_element: Gas,
487    pub bls12381_p2_sum_base: Gas,
488    pub bls12381_p2_sum_element: Gas,
489    pub bls12381_g1_multiexp_base: Gas,
490    pub bls12381_g1_multiexp_element: Gas,
491    pub bls12381_g2_multiexp_base: Gas,
492    pub bls12381_g2_multiexp_element: Gas,
493    pub bls12381_map_fp_to_g1_base: Gas,
494    pub bls12381_map_fp_to_g1_element: Gas,
495    pub bls12381_map_fp2_to_g2_base: Gas,
496    pub bls12381_map_fp2_to_g2_element: Gas,
497    pub bls12381_pairing_base: Gas,
498    pub bls12381_pairing_element: Gas,
499    pub bls12381_p1_decompress_base: Gas,
500    pub bls12381_p1_decompress_element: Gas,
501    pub bls12381_p2_decompress_base: Gas,
502    pub bls12381_p2_decompress_element: Gas,
503}
504
505impl From<crate::ExtCostsConfig> for ExtCostsConfigView {
506    fn from(config: crate::ExtCostsConfig) -> Self {
507        Self {
508            base: config.gas_cost(ExtCosts::base),
509            contract_loading_base: config.gas_cost(ExtCosts::contract_loading_base),
510            contract_loading_bytes: config.gas_cost(ExtCosts::contract_loading_bytes),
511            read_memory_base: config.gas_cost(ExtCosts::read_memory_base),
512            read_memory_byte: config.gas_cost(ExtCosts::read_memory_byte),
513            write_memory_base: config.gas_cost(ExtCosts::write_memory_base),
514            write_memory_byte: config.gas_cost(ExtCosts::write_memory_byte),
515            read_register_base: config.gas_cost(ExtCosts::read_register_base),
516            read_register_byte: config.gas_cost(ExtCosts::read_register_byte),
517            write_register_base: config.gas_cost(ExtCosts::write_register_base),
518            write_register_byte: config.gas_cost(ExtCosts::write_register_byte),
519            utf8_decoding_base: config.gas_cost(ExtCosts::utf8_decoding_base),
520            utf8_decoding_byte: config.gas_cost(ExtCosts::utf8_decoding_byte),
521            utf16_decoding_base: config.gas_cost(ExtCosts::utf16_decoding_base),
522            utf16_decoding_byte: config.gas_cost(ExtCosts::utf16_decoding_byte),
523            sha256_base: config.gas_cost(ExtCosts::sha256_base),
524            sha256_byte: config.gas_cost(ExtCosts::sha256_byte),
525            keccak256_base: config.gas_cost(ExtCosts::keccak256_base),
526            keccak256_byte: config.gas_cost(ExtCosts::keccak256_byte),
527            keccak512_base: config.gas_cost(ExtCosts::keccak512_base),
528            keccak512_byte: config.gas_cost(ExtCosts::keccak512_byte),
529            ripemd160_base: config.gas_cost(ExtCosts::ripemd160_base),
530            ripemd160_block: config.gas_cost(ExtCosts::ripemd160_block),
531            ed25519_verify_base: config.gas_cost(ExtCosts::ed25519_verify_base),
532            ed25519_verify_byte: config.gas_cost(ExtCosts::ed25519_verify_byte),
533            ecrecover_base: config.gas_cost(ExtCosts::ecrecover_base),
534            log_base: config.gas_cost(ExtCosts::log_base),
535            log_byte: config.gas_cost(ExtCosts::log_byte),
536            storage_write_base: config.gas_cost(ExtCosts::storage_write_base),
537            storage_write_key_byte: config.gas_cost(ExtCosts::storage_write_key_byte),
538            storage_write_value_byte: config.gas_cost(ExtCosts::storage_write_value_byte),
539            storage_write_evicted_byte: config.gas_cost(ExtCosts::storage_write_evicted_byte),
540            storage_read_base: config.gas_cost(ExtCosts::storage_read_base),
541            storage_read_key_byte: config.gas_cost(ExtCosts::storage_read_key_byte),
542            storage_read_value_byte: config.gas_cost(ExtCosts::storage_read_value_byte),
543            storage_large_read_overhead_base: config
544                .gas_cost(ExtCosts::storage_large_read_overhead_base),
545            storage_large_read_overhead_byte: config
546                .gas_cost(ExtCosts::storage_large_read_overhead_byte),
547            storage_remove_base: config.gas_cost(ExtCosts::storage_remove_base),
548            storage_remove_key_byte: config.gas_cost(ExtCosts::storage_remove_key_byte),
549            storage_remove_ret_value_byte: config.gas_cost(ExtCosts::storage_remove_ret_value_byte),
550            storage_has_key_base: config.gas_cost(ExtCosts::storage_has_key_base),
551            storage_has_key_byte: config.gas_cost(ExtCosts::storage_has_key_byte),
552            storage_iter_create_prefix_base: config
553                .gas_cost(ExtCosts::storage_iter_create_prefix_base),
554            storage_iter_create_prefix_byte: config
555                .gas_cost(ExtCosts::storage_iter_create_prefix_byte),
556            storage_iter_create_range_base: config
557                .gas_cost(ExtCosts::storage_iter_create_range_base),
558            storage_iter_create_from_byte: config.gas_cost(ExtCosts::storage_iter_create_from_byte),
559            storage_iter_create_to_byte: config.gas_cost(ExtCosts::storage_iter_create_to_byte),
560            storage_iter_next_base: config.gas_cost(ExtCosts::storage_iter_next_base),
561            storage_iter_next_key_byte: config.gas_cost(ExtCosts::storage_iter_next_key_byte),
562            storage_iter_next_value_byte: config.gas_cost(ExtCosts::storage_iter_next_value_byte),
563            touching_trie_node: config.gas_cost(ExtCosts::touching_trie_node),
564            read_cached_trie_node: config.gas_cost(ExtCosts::read_cached_trie_node),
565            promise_and_base: config.gas_cost(ExtCosts::promise_and_base),
566            promise_and_per_promise: config.gas_cost(ExtCosts::promise_and_per_promise),
567            promise_return: config.gas_cost(ExtCosts::promise_return),
568            validator_stake_base: config.gas_cost(ExtCosts::validator_stake_base),
569            validator_total_stake_base: config.gas_cost(ExtCosts::validator_total_stake_base),
570            alt_bn128_g1_multiexp_base: config.gas_cost(ExtCosts::alt_bn128_g1_multiexp_base),
571            alt_bn128_g1_multiexp_element: config.gas_cost(ExtCosts::alt_bn128_g1_multiexp_element),
572            alt_bn128_g1_sum_base: config.gas_cost(ExtCosts::alt_bn128_g1_sum_base),
573            alt_bn128_g1_sum_element: config.gas_cost(ExtCosts::alt_bn128_g1_sum_element),
574            alt_bn128_pairing_check_base: config.gas_cost(ExtCosts::alt_bn128_pairing_check_base),
575            alt_bn128_pairing_check_element: config
576                .gas_cost(ExtCosts::alt_bn128_pairing_check_element),
577            yield_create_base: config.gas_cost(ExtCosts::yield_create_base),
578            yield_create_byte: config.gas_cost(ExtCosts::yield_create_byte),
579            yield_resume_base: config.gas_cost(ExtCosts::yield_resume_base),
580            yield_resume_byte: config.gas_cost(ExtCosts::yield_resume_byte),
581            bls12381_p1_sum_base: config.gas_cost(ExtCosts::bls12381_p1_sum_base),
582            bls12381_p1_sum_element: config.gas_cost(ExtCosts::bls12381_p1_sum_element),
583            bls12381_p2_sum_base: config.gas_cost(ExtCosts::bls12381_p2_sum_base),
584            bls12381_p2_sum_element: config.gas_cost(ExtCosts::bls12381_p2_sum_element),
585            bls12381_g1_multiexp_base: config.gas_cost(ExtCosts::bls12381_g1_multiexp_base),
586            bls12381_g1_multiexp_element: config.gas_cost(ExtCosts::bls12381_g1_multiexp_element),
587            bls12381_g2_multiexp_base: config.gas_cost(ExtCosts::bls12381_g2_multiexp_base),
588            bls12381_g2_multiexp_element: config.gas_cost(ExtCosts::bls12381_g2_multiexp_element),
589            bls12381_map_fp_to_g1_base: config.gas_cost(ExtCosts::bls12381_map_fp_to_g1_base),
590            bls12381_map_fp_to_g1_element: config.gas_cost(ExtCosts::bls12381_map_fp_to_g1_element),
591            bls12381_map_fp2_to_g2_base: config.gas_cost(ExtCosts::bls12381_map_fp2_to_g2_base),
592            bls12381_map_fp2_to_g2_element: config
593                .gas_cost(ExtCosts::bls12381_map_fp2_to_g2_element),
594            bls12381_pairing_base: config.gas_cost(ExtCosts::bls12381_pairing_base),
595            bls12381_pairing_element: config.gas_cost(ExtCosts::bls12381_pairing_element),
596            bls12381_p1_decompress_base: config.gas_cost(ExtCosts::bls12381_p1_decompress_base),
597            bls12381_p1_decompress_element: config
598                .gas_cost(ExtCosts::bls12381_p1_decompress_element),
599            bls12381_p2_decompress_base: config.gas_cost(ExtCosts::bls12381_p2_decompress_base),
600            bls12381_p2_decompress_element: config
601                .gas_cost(ExtCosts::bls12381_p2_decompress_element),
602            // removed parameters
603            contract_compile_base: Gas::ZERO,
604            contract_compile_bytes: Gas::ZERO,
605        }
606    }
607}
608
609impl From<ExtCostsConfigView> for crate::ExtCostsConfig {
610    fn from(view: ExtCostsConfigView) -> Self {
611        let costs = enum_map::enum_map! {
612                ExtCosts::base => view.base,
613                ExtCosts::contract_loading_base => view.contract_loading_base,
614                ExtCosts::contract_loading_bytes => view.contract_loading_bytes,
615                ExtCosts::read_memory_base => view.read_memory_base,
616                ExtCosts::read_memory_byte => view.read_memory_byte,
617                ExtCosts::write_memory_base => view.write_memory_base,
618                ExtCosts::write_memory_byte => view.write_memory_byte,
619                ExtCosts::read_register_base => view.read_register_base,
620                ExtCosts::read_register_byte => view.read_register_byte,
621                ExtCosts::write_register_base => view.write_register_base,
622                ExtCosts::write_register_byte => view.write_register_byte,
623                ExtCosts::utf8_decoding_base => view.utf8_decoding_base,
624                ExtCosts::utf8_decoding_byte => view.utf8_decoding_byte,
625                ExtCosts::utf16_decoding_base => view.utf16_decoding_base,
626                ExtCosts::utf16_decoding_byte => view.utf16_decoding_byte,
627                ExtCosts::sha256_base => view.sha256_base,
628                ExtCosts::sha256_byte => view.sha256_byte,
629                ExtCosts::keccak256_base => view.keccak256_base,
630                ExtCosts::keccak256_byte => view.keccak256_byte,
631                ExtCosts::keccak512_base => view.keccak512_base,
632                ExtCosts::keccak512_byte => view.keccak512_byte,
633                ExtCosts::ripemd160_base => view.ripemd160_base,
634                ExtCosts::ripemd160_block => view.ripemd160_block,
635                ExtCosts::ed25519_verify_base => view.ed25519_verify_base,
636                ExtCosts::ed25519_verify_byte => view.ed25519_verify_byte,
637                ExtCosts::ecrecover_base => view.ecrecover_base,
638                ExtCosts::log_base => view.log_base,
639                ExtCosts::log_byte => view.log_byte,
640                ExtCosts::storage_write_base => view.storage_write_base,
641                ExtCosts::storage_write_key_byte => view.storage_write_key_byte,
642                ExtCosts::storage_write_value_byte => view.storage_write_value_byte,
643                ExtCosts::storage_write_evicted_byte => view.storage_write_evicted_byte,
644                ExtCosts::storage_read_base => view.storage_read_base,
645                ExtCosts::storage_read_key_byte => view.storage_read_key_byte,
646                ExtCosts::storage_read_value_byte => view.storage_read_value_byte,
647                ExtCosts::storage_large_read_overhead_base => view.storage_large_read_overhead_base,
648                ExtCosts::storage_large_read_overhead_byte => view.storage_large_read_overhead_byte,
649                ExtCosts::storage_remove_base => view.storage_remove_base,
650                ExtCosts::storage_remove_key_byte => view.storage_remove_key_byte,
651                ExtCosts::storage_remove_ret_value_byte => view.storage_remove_ret_value_byte,
652                ExtCosts::storage_has_key_base => view.storage_has_key_base,
653                ExtCosts::storage_has_key_byte => view.storage_has_key_byte,
654                ExtCosts::storage_iter_create_prefix_base => view.storage_iter_create_prefix_base,
655                ExtCosts::storage_iter_create_prefix_byte => view.storage_iter_create_prefix_byte,
656                ExtCosts::storage_iter_create_range_base => view.storage_iter_create_range_base,
657                ExtCosts::storage_iter_create_from_byte => view.storage_iter_create_from_byte,
658                ExtCosts::storage_iter_create_to_byte => view.storage_iter_create_to_byte,
659                ExtCosts::storage_iter_next_base => view.storage_iter_next_base,
660                ExtCosts::storage_iter_next_key_byte => view.storage_iter_next_key_byte,
661                ExtCosts::storage_iter_next_value_byte => view.storage_iter_next_value_byte,
662                ExtCosts::touching_trie_node => view.touching_trie_node,
663                ExtCosts::read_cached_trie_node => view.read_cached_trie_node,
664                ExtCosts::promise_and_base => view.promise_and_base,
665                ExtCosts::promise_and_per_promise => view.promise_and_per_promise,
666                ExtCosts::promise_return => view.promise_return,
667                ExtCosts::validator_stake_base => view.validator_stake_base,
668                ExtCosts::validator_total_stake_base => view.validator_total_stake_base,
669                ExtCosts::alt_bn128_g1_multiexp_base => view.alt_bn128_g1_multiexp_base,
670                ExtCosts::alt_bn128_g1_multiexp_element => view.alt_bn128_g1_multiexp_element,
671                ExtCosts::alt_bn128_g1_sum_base => view.alt_bn128_g1_sum_base,
672                ExtCosts::alt_bn128_g1_sum_element => view.alt_bn128_g1_sum_element,
673                ExtCosts::alt_bn128_pairing_check_base => view.alt_bn128_pairing_check_base,
674                ExtCosts::alt_bn128_pairing_check_element => view.alt_bn128_pairing_check_element,
675                ExtCosts::yield_create_base => view.yield_create_base,
676                ExtCosts::yield_create_byte => view.yield_create_byte,
677                ExtCosts::yield_resume_base => view.yield_resume_base,
678                ExtCosts::yield_resume_byte => view.yield_resume_byte,
679                ExtCosts::bls12381_p1_sum_base => view.bls12381_p1_sum_base,
680                ExtCosts::bls12381_p1_sum_element => view.bls12381_p1_sum_element,
681                ExtCosts::bls12381_p2_sum_base => view.bls12381_p2_sum_base,
682                ExtCosts::bls12381_p2_sum_element => view.bls12381_p2_sum_element,
683                ExtCosts::bls12381_g1_multiexp_base => view.bls12381_g1_multiexp_base,
684                ExtCosts::bls12381_g1_multiexp_element => view.bls12381_g1_multiexp_element,
685                ExtCosts::bls12381_g2_multiexp_base => view.bls12381_g2_multiexp_base,
686                ExtCosts::bls12381_g2_multiexp_element => view.bls12381_g2_multiexp_element,
687                ExtCosts::bls12381_map_fp_to_g1_base => view.bls12381_map_fp_to_g1_base,
688                ExtCosts::bls12381_map_fp_to_g1_element => view.bls12381_map_fp_to_g1_element,
689                ExtCosts::bls12381_map_fp2_to_g2_base => view.bls12381_map_fp2_to_g2_base,
690                ExtCosts::bls12381_map_fp2_to_g2_element => view.bls12381_map_fp2_to_g2_element,
691                ExtCosts::bls12381_pairing_base => view.bls12381_pairing_base,
692                ExtCosts::bls12381_pairing_element => view.bls12381_pairing_element,
693                ExtCosts::bls12381_p1_decompress_base => view.bls12381_p1_decompress_base,
694                ExtCosts::bls12381_p1_decompress_element => view.bls12381_p1_decompress_element,
695                ExtCosts::bls12381_p2_decompress_base => view.bls12381_p2_decompress_base,
696                ExtCosts::bls12381_p2_decompress_element => view.bls12381_p2_decompress_element,
697        }
698        .map(|_, value| ParameterCost { gas: value, compute: value.as_gas() });
699        Self { costs }
700    }
701}
702
703/// Configuration specific to ChunkStateWitness.
704#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, Hash, PartialEq, Eq)]
705#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
706pub struct WitnessConfigView {
707    /// Size limit for storage proof generated while executing receipts in a chunk.
708    /// After this limit is reached we defer execution of any new receipts.
709    pub main_storage_proof_size_soft_limit: u64,
710    /// Maximum size of transactions contained inside ChunkStateWitness.
711    ///
712    /// A witness contains transactions from both the previous chunk and the current one.
713    /// This parameter limits the sum of sizes of transactions from both of those chunks.
714    pub combined_transactions_size_limit: usize,
715    /// Soft size limit of storage proof used to validate new transactions in ChunkStateWitness.
716    pub new_transactions_validation_state_size_soft_limit: u64,
717}
718
719impl From<WitnessConfig> for WitnessConfigView {
720    fn from(config: WitnessConfig) -> Self {
721        Self {
722            main_storage_proof_size_soft_limit: config.main_storage_proof_size_soft_limit,
723            combined_transactions_size_limit: config.combined_transactions_size_limit,
724            new_transactions_validation_state_size_soft_limit: config
725                .new_transactions_validation_state_size_soft_limit,
726        }
727    }
728}
729
730/// The configuration for congestion control. More info about congestion [here](https://near.github.io/nearcore/architecture/how/receipt-congestion.html?highlight=congestion#receipt-congestion)
731#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, PartialEq)]
732#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
733pub struct CongestionControlConfigView {
734    /// How much gas in delayed receipts of a shard is 100% incoming congestion.
735    ///
736    /// See [`CongestionControlConfig`] for more details.
737    pub max_congestion_incoming_gas: Gas,
738
739    /// How much gas in outgoing buffered receipts of a shard is 100% congested.
740    ///
741    /// Outgoing congestion contributes to overall congestion, which reduces how
742    /// much other shards are allowed to forward to this shard.
743    pub max_congestion_outgoing_gas: Gas,
744
745    /// How much memory space of all delayed and buffered receipts in a shard is
746    /// considered 100% congested.
747    ///
748    /// See [`CongestionControlConfig`] for more details.
749    pub max_congestion_memory_consumption: u64,
750
751    /// How many missed chunks in a row in a shard is considered 100% congested.
752    pub max_congestion_missed_chunks: u64,
753
754    /// The maximum amount of gas attached to receipts a shard can forward to
755    /// another shard per chunk.
756    ///
757    /// See [`CongestionControlConfig`] for more details.
758    pub max_outgoing_gas: Gas,
759
760    /// The minimum gas each shard can send to a shard that is not fully congested.
761    ///
762    /// See [`CongestionControlConfig`] for more details.
763    pub min_outgoing_gas: Gas,
764
765    /// How much gas the chosen allowed shard can send to a 100% congested shard.
766    ///
767    /// See [`CongestionControlConfig`] for more details.
768    pub allowed_shard_outgoing_gas: Gas,
769
770    /// The maximum amount of gas in a chunk spent on converting new transactions to
771    /// receipts.
772    ///
773    /// See [`CongestionControlConfig`] for more details.
774    pub max_tx_gas: Gas,
775
776    /// The minimum amount of gas in a chunk spent on converting new transactions
777    /// to receipts, as long as the receiving shard is not congested.
778    ///
779    /// See [`CongestionControlConfig`] for more details.
780    pub min_tx_gas: Gas,
781
782    /// How much congestion a shard can tolerate before it stops all shards from
783    /// accepting new transactions with the receiver set to the congested shard.
784    pub reject_tx_congestion_threshold: f64,
785
786    /// The standard size limit for outgoing receipts aimed at a single shard.
787    /// This limit is pretty small to keep the size of source_receipt_proofs under control.
788    /// It limits the total sum of outgoing receipts, not individual receipts.
789    pub outgoing_receipts_usual_size_limit: u64,
790
791    /// Large size limit for outgoing receipts to a shard, used when it's safe
792    /// to send a lot of receipts without making the state witness too large.
793    /// It limits the total sum of outgoing receipts, not individual receipts.
794    pub outgoing_receipts_big_size_limit: u64,
795}
796
797impl From<CongestionControlConfig> for CongestionControlConfigView {
798    fn from(other: CongestionControlConfig) -> 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
816impl From<CongestionControlConfigView> for CongestionControlConfig {
817    fn from(other: CongestionControlConfigView) -> Self {
818        Self {
819            max_congestion_incoming_gas: other.max_congestion_incoming_gas,
820            max_congestion_outgoing_gas: other.max_congestion_outgoing_gas,
821            max_congestion_memory_consumption: other.max_congestion_memory_consumption,
822            max_congestion_missed_chunks: other.max_congestion_missed_chunks,
823            max_outgoing_gas: other.max_outgoing_gas,
824            min_outgoing_gas: other.min_outgoing_gas,
825            allowed_shard_outgoing_gas: other.allowed_shard_outgoing_gas,
826            max_tx_gas: other.max_tx_gas,
827            min_tx_gas: other.min_tx_gas,
828            reject_tx_congestion_threshold: other.reject_tx_congestion_threshold,
829            outgoing_receipts_usual_size_limit: other.outgoing_receipts_usual_size_limit,
830            outgoing_receipts_big_size_limit: other.outgoing_receipts_big_size_limit,
831        }
832    }
833}
834
835#[cfg(feature = "schemars")]
836pub type Rational32SchemarsProvider = [i32; 2];
837
838#[cfg(test)]
839#[cfg(not(feature = "nightly"))]
840mod tests {
841    /// The JSON representation used in RPC responses must not remove or rename
842    /// fields, only adding fields is allowed or we risk breaking clients.
843    #[test]
844    fn test_runtime_config_view() {
845        use crate::RuntimeConfig;
846        use crate::RuntimeConfigStore;
847        use crate::view::RuntimeConfigView;
848        use near_primitives_core::version::PROTOCOL_VERSION;
849
850        let config_store = RuntimeConfigStore::new(None);
851        let config = config_store.get_config(PROTOCOL_VERSION);
852        let view = RuntimeConfigView::from(RuntimeConfig::clone(config));
853        insta::assert_json_snapshot!(&view, { ".wasm_config.vm_kind" => "<REDACTED>"});
854    }
855}