unc_parameters/
config.rs

1//! Settings of the parameters of the runtime.
2use crate::config_store::INITIAL_TESTNET_CONFIG;
3use crate::cost::RuntimeFeesConfig;
4use crate::parameter_table::ParameterTable;
5use unc_account_id::AccountId;
6use unc_primitives_core::types::Balance;
7use unc_primitives_core::version::PROTOCOL_VERSION;
8
9use super::parameter_table::InvalidConfigError;
10
11/// The structure that holds the parameters of the runtime, mostly economics.
12#[derive(Debug, Clone, PartialEq, Eq)]
13pub struct RuntimeConfig {
14    /// Action gas costs, storage fees, and economic constants around them.
15    ///
16    /// This contains parameters that are required by the WASM runtime and the
17    /// transaction runtime.
18    pub fees: RuntimeFeesConfig,
19    /// Config of wasm operations, also includes wasm gas costs.
20    ///
21    /// This contains all the configuration parameters that are only required by
22    /// the WASM runtime.
23    pub wasm_config: crate::vm::Config,
24    /// Config that defines rules for account creation.
25    pub account_creation_config: AccountCreationConfig,
26}
27
28impl RuntimeConfig {
29    pub(crate) fn new(params: &ParameterTable) -> Result<Self, InvalidConfigError> {
30        RuntimeConfig::try_from(params)
31    }
32
33    pub fn initial_testnet_config() -> RuntimeConfig {
34        INITIAL_TESTNET_CONFIG
35            .parse()
36            .and_then(|params| RuntimeConfig::new(&params))
37            .expect("Failed parsing initial testnet config")
38    }
39
40    pub fn test() -> Self {
41        let config_store = super::config_store::RuntimeConfigStore::new(None);
42        let wasm_config =
43            crate::vm::Config::clone(&config_store.get_config(PROTOCOL_VERSION).wasm_config);
44        RuntimeConfig {
45            fees: RuntimeFeesConfig::test(),
46            wasm_config,
47            account_creation_config: AccountCreationConfig::default(),
48        }
49    }
50
51    pub fn free() -> Self {
52        let config_store = super::config_store::RuntimeConfigStore::new(None);
53        let mut wasm_config =
54            crate::vm::Config::clone(&config_store.get_config(PROTOCOL_VERSION).wasm_config);
55        wasm_config.make_free();
56        Self {
57            fees: RuntimeFeesConfig::free(),
58            wasm_config,
59            account_creation_config: AccountCreationConfig::default(),
60        }
61    }
62
63    pub fn storage_amount_per_byte(&self) -> Balance {
64        self.fees.storage_usage_config.storage_amount_per_byte
65    }
66}
67
68/// The structure describes configuration for creation of new accounts.
69#[derive(Debug, Clone, PartialEq, Eq)]
70pub struct AccountCreationConfig {
71    /// The minimum length of the top-level account ID that is allowed to be created by any account.
72    pub min_allowed_top_level_account_length: u8,
73    /// The account ID of the account registrar. This account ID allowed to create top-level
74    /// accounts of any valid length.
75    pub registrar_account_id: AccountId,
76}
77
78impl Default for AccountCreationConfig {
79    fn default() -> Self {
80        Self {
81            min_allowed_top_level_account_length: 0,
82            registrar_account_id: "registrar".parse().unwrap(),
83        }
84    }
85}