1use crate::{ActionCosts, ExtCosts, Fee, ParameterCost};
2use num_rational::Rational32;
3use unc_account_id::AccountId;
4use unc_primitives_core::serialize::dec_format;
5use unc_primitives_core::types::{Balance, Gas};
6
7#[derive(Debug, serde::Serialize, serde::Deserialize, Clone)]
9pub struct RuntimeConfigView {
10 #[serde(with = "dec_format")]
13 pub storage_amount_per_byte: Balance,
14 pub transaction_costs: RuntimeFeesConfigView,
17 pub wasm_config: VMConfigView,
19 pub account_creation_config: AccountCreationConfigView,
21}
22
23#[derive(Debug, serde::Serialize, serde::Deserialize, Clone)]
24pub struct RuntimeFeesConfigView {
25 pub action_receipt_creation_config: Fee,
31 pub data_receipt_creation_config: DataReceiptCreationConfigView,
33 pub action_creation_config: ActionCreationConfigView,
35 pub storage_usage_config: StorageUsageConfigView,
37
38 pub burnt_gas_reward: Rational32,
40
41 pub pessimistic_gas_price_inflation_ratio: Rational32,
43}
44
45#[derive(Debug, serde::Serialize, serde::Deserialize, Clone)]
47pub struct AccountCreationConfigView {
48 pub min_allowed_top_level_account_length: u8,
50 pub registrar_account_id: AccountId,
53}
54
55#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, Hash, PartialEq, Eq)]
56pub struct DataReceiptCreationConfigView {
57 pub base_cost: Fee,
65 pub cost_per_byte: Fee,
70}
71
72#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, Hash, PartialEq, Eq)]
74pub struct ActionCreationConfigView {
75 pub create_account_cost: Fee,
77
78 pub deploy_contract_cost: Fee,
80 pub deploy_contract_cost_per_byte: Fee,
82
83 pub function_call_cost: Fee,
85 pub function_call_cost_per_byte: Fee,
87
88 pub transfer_cost: Fee,
90
91 pub pledge_cost: Fee,
93
94 pub add_key_cost: AccessKeyCreationConfigView,
96
97 pub delete_key_cost: Fee,
99
100 pub delete_account_cost: Fee,
102
103 pub delegate_cost: Fee,
107}
108
109#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, Hash, PartialEq, Eq)]
111pub struct AccessKeyCreationConfigView {
112 pub full_access_cost: Fee,
114 pub function_call_cost: Fee,
116 pub function_call_cost_per_byte: Fee,
118}
119
120#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, Hash, PartialEq, Eq)]
122pub struct StorageUsageConfigView {
123 pub num_bytes_account: u64,
125 pub num_extra_bytes_record: u64,
127}
128
129impl From<crate::RuntimeConfig> for RuntimeConfigView {
130 fn from(config: crate::RuntimeConfig) -> Self {
131 Self {
132 storage_amount_per_byte: config.storage_amount_per_byte(),
133 transaction_costs: RuntimeFeesConfigView {
134 action_receipt_creation_config: config
135 .fees
136 .fee(ActionCosts::new_action_receipt)
137 .clone(),
138 data_receipt_creation_config: DataReceiptCreationConfigView {
139 base_cost: config.fees.fee(ActionCosts::new_data_receipt_base).clone(),
140 cost_per_byte: config.fees.fee(ActionCosts::new_data_receipt_byte).clone(),
141 },
142 action_creation_config: ActionCreationConfigView {
143 create_account_cost: config.fees.fee(ActionCosts::create_account).clone(),
144 deploy_contract_cost: config
145 .fees
146 .fee(ActionCosts::deploy_contract_base)
147 .clone(),
148 deploy_contract_cost_per_byte: config
149 .fees
150 .fee(ActionCosts::deploy_contract_byte)
151 .clone(),
152 function_call_cost: config.fees.fee(ActionCosts::function_call_base).clone(),
153 function_call_cost_per_byte: config
154 .fees
155 .fee(ActionCosts::function_call_byte)
156 .clone(),
157 transfer_cost: config.fees.fee(ActionCosts::transfer).clone(),
158 pledge_cost: config.fees.fee(ActionCosts::pledge).clone(),
159 add_key_cost: AccessKeyCreationConfigView {
160 full_access_cost: config.fees.fee(ActionCosts::add_full_access_key).clone(),
161 function_call_cost: config
162 .fees
163 .fee(ActionCosts::add_function_call_key_base)
164 .clone(),
165 function_call_cost_per_byte: config
166 .fees
167 .fee(ActionCosts::add_function_call_key_byte)
168 .clone(),
169 },
170 delete_key_cost: config.fees.fee(ActionCosts::delete_key).clone(),
171 delete_account_cost: config.fees.fee(ActionCosts::delete_account).clone(),
172 delegate_cost: config.fees.fee(ActionCosts::delegate).clone(),
173 },
174 storage_usage_config: StorageUsageConfigView {
175 num_bytes_account: config.fees.storage_usage_config.num_bytes_account,
176 num_extra_bytes_record: config.fees.storage_usage_config.num_extra_bytes_record,
177 },
178 burnt_gas_reward: config.fees.burnt_gas_reward,
179 pessimistic_gas_price_inflation_ratio: config
180 .fees
181 .pessimistic_gas_price_inflation_ratio,
182 },
183 wasm_config: VMConfigView::from(config.wasm_config),
184 account_creation_config: AccountCreationConfigView {
185 min_allowed_top_level_account_length: config
186 .account_creation_config
187 .min_allowed_top_level_account_length,
188 registrar_account_id: config.account_creation_config.registrar_account_id,
189 },
190 }
191 }
192}
193
194#[derive(Clone, Debug, Hash, serde::Serialize, serde::Deserialize, PartialEq, Eq)]
195pub struct VMConfigView {
196 pub ext_costs: ExtCostsConfigView,
198
199 pub grow_mem_cost: u32,
201 pub regular_op_cost: u32,
203
204 pub vm_kind: crate::vm::VMKind,
206 pub disable_9393_fix: bool,
208 pub storage_get_mode: crate::vm::StorageGetMode,
210 pub fix_contract_loading_cost: bool,
212 pub implicit_account_creation: bool,
214 pub math_extension: bool,
216 pub ed25519_verify: bool,
218 pub alt_bn128: bool,
220 pub function_call_weight: bool,
222 pub eth_accounts: bool,
224
225 pub limit_config: crate::vm::LimitConfig,
230}
231
232impl From<crate::vm::Config> for VMConfigView {
233 fn from(config: crate::vm::Config) -> Self {
234 Self {
235 ext_costs: ExtCostsConfigView::from(config.ext_costs),
236 grow_mem_cost: config.grow_mem_cost,
237 regular_op_cost: config.regular_op_cost,
238 disable_9393_fix: config.disable_9393_fix,
239 limit_config: config.limit_config,
240 storage_get_mode: config.storage_get_mode,
241 fix_contract_loading_cost: config.fix_contract_loading_cost,
242 implicit_account_creation: config.implicit_account_creation,
243 math_extension: config.math_extension,
244 ed25519_verify: config.ed25519_verify,
245 alt_bn128: config.alt_bn128,
246 function_call_weight: config.function_call_weight,
247 vm_kind: config.vm_kind,
248 eth_accounts: config.eth_accounts,
249 }
250 }
251}
252
253impl From<VMConfigView> for crate::vm::Config {
254 fn from(view: VMConfigView) -> Self {
255 Self {
256 ext_costs: crate::ExtCostsConfig::from(view.ext_costs),
257 grow_mem_cost: view.grow_mem_cost,
258 regular_op_cost: view.regular_op_cost,
259 disable_9393_fix: view.disable_9393_fix,
260 limit_config: view.limit_config,
261 storage_get_mode: view.storage_get_mode,
262 fix_contract_loading_cost: view.fix_contract_loading_cost,
263 implicit_account_creation: view.implicit_account_creation,
264 math_extension: view.math_extension,
265 ed25519_verify: view.ed25519_verify,
266 alt_bn128: view.alt_bn128,
267 function_call_weight: view.function_call_weight,
268 vm_kind: view.vm_kind,
269 eth_accounts: view.eth_accounts,
270 }
271 }
272}
273
274#[derive(Debug, serde::Serialize, serde::Deserialize, Clone, Hash, PartialEq, Eq)]
277pub struct ExtCostsConfigView {
278 pub base: Gas,
280
281 pub contract_loading_base: Gas,
283 pub contract_loading_bytes: Gas,
285
286 pub read_memory_base: Gas,
288 pub read_memory_byte: Gas,
290
291 pub write_memory_base: Gas,
293 pub write_memory_byte: Gas,
295
296 pub read_register_base: Gas,
298 pub read_register_byte: Gas,
300
301 pub write_register_base: Gas,
303 pub write_register_byte: Gas,
305
306 pub utf8_decoding_base: Gas,
308 pub utf8_decoding_byte: Gas,
310
311 pub utf16_decoding_base: Gas,
313 pub utf16_decoding_byte: Gas,
315
316 pub sha256_base: Gas,
318 pub sha256_byte: Gas,
320
321 pub keccak256_base: Gas,
323 pub keccak256_byte: Gas,
325
326 pub keccak512_base: Gas,
328 pub keccak512_byte: Gas,
330
331 pub ripemd160_base: Gas,
333 pub ripemd160_block: Gas,
335
336 pub ed25519_verify_base: Gas,
338 pub ed25519_verify_byte: Gas,
340
341 pub ecrecover_base: Gas,
343
344 pub log_base: Gas,
346 pub log_byte: Gas,
348
349 pub storage_write_base: Gas,
354 pub storage_write_key_byte: Gas,
356 pub storage_write_value_byte: Gas,
358 pub storage_write_evicted_byte: Gas,
360
361 pub storage_read_base: Gas,
363 pub storage_read_key_byte: Gas,
365 pub storage_read_value_byte: Gas,
367
368 pub storage_remove_base: Gas,
370 pub storage_remove_key_byte: Gas,
372 pub storage_remove_ret_value_byte: Gas,
374
375 pub storage_has_key_base: Gas,
377 pub storage_has_key_byte: Gas,
379
380 pub storage_iter_create_prefix_base: Gas,
382 pub storage_iter_create_prefix_byte: Gas,
384
385 pub storage_iter_create_range_base: Gas,
387 pub storage_iter_create_from_byte: Gas,
389 pub storage_iter_create_to_byte: Gas,
391
392 pub storage_iter_next_base: Gas,
394 pub storage_iter_next_key_byte: Gas,
396 pub storage_iter_next_value_byte: Gas,
398
399 pub touching_trie_node: Gas,
401 pub read_cached_trie_node: Gas,
403
404 pub promise_and_base: Gas,
409 pub promise_and_per_promise: Gas,
411 pub promise_return: Gas,
413
414 pub validator_pledge_base: Gas,
419 pub validator_power_base: Gas,
420 pub validator_total_pledge_base: Gas,
422 pub validator_total_power_base: Gas,
423
424 pub contract_compile_base: Gas,
426 pub contract_compile_bytes: Gas,
427
428 pub alt_bn128_g1_multiexp_base: Gas,
433 pub alt_bn128_g1_multiexp_element: Gas,
435 pub alt_bn128_g1_sum_base: Gas,
437 pub alt_bn128_g1_sum_element: Gas,
439 pub alt_bn128_pairing_check_base: Gas,
441 pub alt_bn128_pairing_check_element: Gas,
443}
444
445impl From<crate::ExtCostsConfig> for ExtCostsConfigView {
446 fn from(config: crate::ExtCostsConfig) -> Self {
447 Self {
448 base: config.gas_cost(ExtCosts::base),
449 contract_loading_base: config.gas_cost(ExtCosts::contract_loading_base),
450 contract_loading_bytes: config.gas_cost(ExtCosts::contract_loading_bytes),
451 read_memory_base: config.gas_cost(ExtCosts::read_memory_base),
452 read_memory_byte: config.gas_cost(ExtCosts::read_memory_byte),
453 write_memory_base: config.gas_cost(ExtCosts::write_memory_base),
454 write_memory_byte: config.gas_cost(ExtCosts::write_memory_byte),
455 read_register_base: config.gas_cost(ExtCosts::read_register_base),
456 read_register_byte: config.gas_cost(ExtCosts::read_register_byte),
457 write_register_base: config.gas_cost(ExtCosts::write_register_base),
458 write_register_byte: config.gas_cost(ExtCosts::write_register_byte),
459 utf8_decoding_base: config.gas_cost(ExtCosts::utf8_decoding_base),
460 utf8_decoding_byte: config.gas_cost(ExtCosts::utf8_decoding_byte),
461 utf16_decoding_base: config.gas_cost(ExtCosts::utf16_decoding_base),
462 utf16_decoding_byte: config.gas_cost(ExtCosts::utf16_decoding_byte),
463 sha256_base: config.gas_cost(ExtCosts::sha256_base),
464 sha256_byte: config.gas_cost(ExtCosts::sha256_byte),
465 keccak256_base: config.gas_cost(ExtCosts::keccak256_base),
466 keccak256_byte: config.gas_cost(ExtCosts::keccak256_byte),
467 keccak512_base: config.gas_cost(ExtCosts::keccak512_base),
468 keccak512_byte: config.gas_cost(ExtCosts::keccak512_byte),
469 ripemd160_base: config.gas_cost(ExtCosts::ripemd160_base),
470 ripemd160_block: config.gas_cost(ExtCosts::ripemd160_block),
471 ed25519_verify_base: config.gas_cost(ExtCosts::ed25519_verify_base),
472 ed25519_verify_byte: config.gas_cost(ExtCosts::ed25519_verify_byte),
473 ecrecover_base: config.gas_cost(ExtCosts::ecrecover_base),
474 log_base: config.gas_cost(ExtCosts::log_base),
475 log_byte: config.gas_cost(ExtCosts::log_byte),
476 storage_write_base: config.gas_cost(ExtCosts::storage_write_base),
477 storage_write_key_byte: config.gas_cost(ExtCosts::storage_write_key_byte),
478 storage_write_value_byte: config.gas_cost(ExtCosts::storage_write_value_byte),
479 storage_write_evicted_byte: config.gas_cost(ExtCosts::storage_write_evicted_byte),
480 storage_read_base: config.gas_cost(ExtCosts::storage_read_base),
481 storage_read_key_byte: config.gas_cost(ExtCosts::storage_read_key_byte),
482 storage_read_value_byte: config.gas_cost(ExtCosts::storage_read_value_byte),
483 storage_remove_base: config.gas_cost(ExtCosts::storage_remove_base),
484 storage_remove_key_byte: config.gas_cost(ExtCosts::storage_remove_key_byte),
485 storage_remove_ret_value_byte: config.gas_cost(ExtCosts::storage_remove_ret_value_byte),
486 storage_has_key_base: config.gas_cost(ExtCosts::storage_has_key_base),
487 storage_has_key_byte: config.gas_cost(ExtCosts::storage_has_key_byte),
488 storage_iter_create_prefix_base: config
489 .gas_cost(ExtCosts::storage_iter_create_prefix_base),
490 storage_iter_create_prefix_byte: config
491 .gas_cost(ExtCosts::storage_iter_create_prefix_byte),
492 storage_iter_create_range_base: config
493 .gas_cost(ExtCosts::storage_iter_create_range_base),
494 storage_iter_create_from_byte: config.gas_cost(ExtCosts::storage_iter_create_from_byte),
495 storage_iter_create_to_byte: config.gas_cost(ExtCosts::storage_iter_create_to_byte),
496 storage_iter_next_base: config.gas_cost(ExtCosts::storage_iter_next_base),
497 storage_iter_next_key_byte: config.gas_cost(ExtCosts::storage_iter_next_key_byte),
498 storage_iter_next_value_byte: config.gas_cost(ExtCosts::storage_iter_next_value_byte),
499 touching_trie_node: config.gas_cost(ExtCosts::touching_trie_node),
500 read_cached_trie_node: config.gas_cost(ExtCosts::read_cached_trie_node),
501 promise_and_base: config.gas_cost(ExtCosts::promise_and_base),
502 promise_and_per_promise: config.gas_cost(ExtCosts::promise_and_per_promise),
503 promise_return: config.gas_cost(ExtCosts::promise_return),
504 validator_pledge_base: config.gas_cost(ExtCosts::validator_pledge_base),
505 validator_total_pledge_base: config.gas_cost(ExtCosts::validator_total_pledge_base),
506 validator_power_base: config.gas_cost(ExtCosts::validator_power_base),
507 validator_total_power_base: config.gas_cost(ExtCosts::validator_total_power_base),
508 alt_bn128_g1_multiexp_base: config.gas_cost(ExtCosts::alt_bn128_g1_multiexp_base),
509 alt_bn128_g1_multiexp_element: config.gas_cost(ExtCosts::alt_bn128_g1_multiexp_element),
510 alt_bn128_g1_sum_base: config.gas_cost(ExtCosts::alt_bn128_g1_sum_base),
511 alt_bn128_g1_sum_element: config.gas_cost(ExtCosts::alt_bn128_g1_sum_element),
512 alt_bn128_pairing_check_base: config.gas_cost(ExtCosts::alt_bn128_pairing_check_base),
513 alt_bn128_pairing_check_element: config
514 .gas_cost(ExtCosts::alt_bn128_pairing_check_element),
515 contract_compile_base: 0,
517 contract_compile_bytes: 0,
518 }
519 }
520}
521
522impl From<ExtCostsConfigView> for crate::ExtCostsConfig {
523 fn from(view: ExtCostsConfigView) -> Self {
524 let costs = enum_map::enum_map! {
525 ExtCosts::base => view.base,
526 ExtCosts::contract_loading_base => view.contract_loading_base,
527 ExtCosts::contract_loading_bytes => view.contract_loading_bytes,
528 ExtCosts::read_memory_base => view.read_memory_base,
529 ExtCosts::read_memory_byte => view.read_memory_byte,
530 ExtCosts::write_memory_base => view.write_memory_base,
531 ExtCosts::write_memory_byte => view.write_memory_byte,
532 ExtCosts::read_register_base => view.read_register_base,
533 ExtCosts::read_register_byte => view.read_register_byte,
534 ExtCosts::write_register_base => view.write_register_base,
535 ExtCosts::write_register_byte => view.write_register_byte,
536 ExtCosts::utf8_decoding_base => view.utf8_decoding_base,
537 ExtCosts::utf8_decoding_byte => view.utf8_decoding_byte,
538 ExtCosts::utf16_decoding_base => view.utf16_decoding_base,
539 ExtCosts::utf16_decoding_byte => view.utf16_decoding_byte,
540 ExtCosts::sha256_base => view.sha256_base,
541 ExtCosts::sha256_byte => view.sha256_byte,
542 ExtCosts::keccak256_base => view.keccak256_base,
543 ExtCosts::keccak256_byte => view.keccak256_byte,
544 ExtCosts::keccak512_base => view.keccak512_base,
545 ExtCosts::keccak512_byte => view.keccak512_byte,
546 ExtCosts::ripemd160_base => view.ripemd160_base,
547 ExtCosts::ripemd160_block => view.ripemd160_block,
548 ExtCosts::ed25519_verify_base => view.ed25519_verify_base,
549 ExtCosts::ed25519_verify_byte => view.ed25519_verify_byte,
550 ExtCosts::ecrecover_base => view.ecrecover_base,
551 ExtCosts::log_base => view.log_base,
552 ExtCosts::log_byte => view.log_byte,
553 ExtCosts::storage_write_base => view.storage_write_base,
554 ExtCosts::storage_write_key_byte => view.storage_write_key_byte,
555 ExtCosts::storage_write_value_byte => view.storage_write_value_byte,
556 ExtCosts::storage_write_evicted_byte => view.storage_write_evicted_byte,
557 ExtCosts::storage_read_base => view.storage_read_base,
558 ExtCosts::storage_read_key_byte => view.storage_read_key_byte,
559 ExtCosts::storage_read_value_byte => view.storage_read_value_byte,
560 ExtCosts::storage_remove_base => view.storage_remove_base,
561 ExtCosts::storage_remove_key_byte => view.storage_remove_key_byte,
562 ExtCosts::storage_remove_ret_value_byte => view.storage_remove_ret_value_byte,
563 ExtCosts::storage_has_key_base => view.storage_has_key_base,
564 ExtCosts::storage_has_key_byte => view.storage_has_key_byte,
565 ExtCosts::storage_iter_create_prefix_base => view.storage_iter_create_prefix_base,
566 ExtCosts::storage_iter_create_prefix_byte => view.storage_iter_create_prefix_byte,
567 ExtCosts::storage_iter_create_range_base => view.storage_iter_create_range_base,
568 ExtCosts::storage_iter_create_from_byte => view.storage_iter_create_from_byte,
569 ExtCosts::storage_iter_create_to_byte => view.storage_iter_create_to_byte,
570 ExtCosts::storage_iter_next_base => view.storage_iter_next_base,
571 ExtCosts::storage_iter_next_key_byte => view.storage_iter_next_key_byte,
572 ExtCosts::storage_iter_next_value_byte => view.storage_iter_next_value_byte,
573 ExtCosts::touching_trie_node => view.touching_trie_node,
574 ExtCosts::read_cached_trie_node => view.read_cached_trie_node,
575 ExtCosts::promise_and_base => view.promise_and_base,
576 ExtCosts::promise_and_per_promise => view.promise_and_per_promise,
577 ExtCosts::promise_return => view.promise_return,
578 ExtCosts::validator_pledge_base => view.validator_pledge_base,
579 ExtCosts::validator_total_pledge_base => view.validator_total_pledge_base,
580 ExtCosts::validator_power_base => view.validator_power_base,
581 ExtCosts::validator_total_power_base => view.validator_total_power_base,
582 ExtCosts::alt_bn128_g1_multiexp_base => view.alt_bn128_g1_multiexp_base,
583 ExtCosts::alt_bn128_g1_multiexp_element => view.alt_bn128_g1_multiexp_element,
584 ExtCosts::alt_bn128_g1_sum_base => view.alt_bn128_g1_sum_base,
585 ExtCosts::alt_bn128_g1_sum_element => view.alt_bn128_g1_sum_element,
586 ExtCosts::alt_bn128_pairing_check_base => view.alt_bn128_pairing_check_base,
587 ExtCosts::alt_bn128_pairing_check_element => view.alt_bn128_pairing_check_element,
588 }
589 .map(|_, value| ParameterCost { gas: value, compute: value });
590 Self { costs }
591 }
592}
593
594#[cfg(test)]
595mod tests {
596 #[test]
599 #[cfg_attr(feature = "nightly", ignore)]
600 fn test_runtime_config_view() {
601 use crate::view::RuntimeConfigView;
602 use crate::RuntimeConfig;
603 use crate::RuntimeConfigStore;
604 use unc_primitives_core::version::PROTOCOL_VERSION;
605
606 let config_store = RuntimeConfigStore::new(None);
607 let config = config_store.get_config(PROTOCOL_VERSION);
608 let view = RuntimeConfigView::from(RuntimeConfig::clone(config));
609 insta::assert_json_snapshot!(&view, { ".wasm_config.vm_kind" => "<REDACTED>"});
610 }
611}