pallet_runtime_configs/
lib.rs

1//! Pallet for tweaking the runtime configs for multiple network.
2
3#![cfg_attr(not(feature = "std"), no_std)]
4
5#[cfg(feature = "runtime-benchmarks")]
6mod benchmarking;
7pub mod weights;
8
9pub use pallet::*;
10
11#[frame_support::pallet]
12mod pallet {
13    use crate::weights::WeightInfo;
14    use frame_support::pallet_prelude::*;
15    use frame_system::pallet_prelude::*;
16    use sp_runtime::traits::Zero;
17    use subspace_runtime_primitives::CouncilDemocracyConfigParams;
18
19    #[pallet::pallet]
20    pub struct Pallet<T>(_);
21
22    /// Whether to enable calls in pallet-domains.
23    #[pallet::storage]
24    #[pallet::getter(fn enable_domains)]
25    pub type EnableDomains<T> = StorageValue<_, bool, ValueQuery>;
26
27    /// Whether to enable dynamic cost of storage.
28    #[pallet::storage]
29    #[pallet::getter(fn enable_dynamic_cost_of_storage)]
30    pub type EnableDynamicCostOfStorage<T> = StorageValue<_, bool, ValueQuery>;
31
32    /// Whether to enable balances transfers.
33    #[pallet::storage]
34    #[pallet::getter(fn enable_balance_transfers)]
35    pub type EnableBalanceTransfers<T> = StorageValue<_, bool, ValueQuery>;
36
37    #[pallet::storage]
38    pub type ConfirmationDepthK<T: Config> = StorageValue<_, BlockNumberFor<T>, ValueQuery>;
39
40    #[pallet::storage]
41    pub type CouncilDemocracyConfig<T: Config> =
42        StorageValue<_, CouncilDemocracyConfigParams<BlockNumberFor<T>>, ValueQuery>;
43
44    #[pallet::config]
45    pub trait Config: frame_system::Config {
46        /// Weight information for extrinsics in this pallet.
47        type WeightInfo: WeightInfo;
48    }
49
50    #[pallet::genesis_config]
51    pub struct GenesisConfig<T: Config> {
52        /// Whether to enable domains
53        pub enable_domains: bool,
54        /// Whether to enable dynamic cost of storage (if `false` cost per byte is equal to 1)
55        pub enable_dynamic_cost_of_storage: bool,
56        /// Whether to enable balance transfers
57        pub enable_balance_transfers: bool,
58        /// Confirmation depth k to use in the archiving process
59        pub confirmation_depth_k: BlockNumberFor<T>,
60        /// Council and democracy config params.
61        pub council_democracy_config_params: CouncilDemocracyConfigParams<BlockNumberFor<T>>,
62    }
63
64    impl<T: Config> Default for GenesisConfig<T> {
65        #[inline]
66        fn default() -> Self {
67            Self {
68                enable_domains: false,
69                enable_dynamic_cost_of_storage: false,
70                enable_balance_transfers: false,
71                confirmation_depth_k: BlockNumberFor::<T>::from(100u32),
72                council_democracy_config_params:
73                    CouncilDemocracyConfigParams::<BlockNumberFor<T>>::default(),
74            }
75        }
76    }
77
78    #[pallet::genesis_build]
79    impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
80        fn build(&self) {
81            let Self {
82                enable_domains,
83                enable_dynamic_cost_of_storage,
84                enable_balance_transfers,
85                confirmation_depth_k,
86                council_democracy_config_params,
87            } = self;
88
89            assert!(
90                !confirmation_depth_k.is_zero(),
91                "ConfirmationDepthK can not be zero"
92            );
93
94            <EnableDomains<T>>::put(enable_domains);
95            <EnableDynamicCostOfStorage<T>>::put(enable_dynamic_cost_of_storage);
96            <EnableBalanceTransfers<T>>::put(enable_balance_transfers);
97            <ConfirmationDepthK<T>>::put(confirmation_depth_k);
98            CouncilDemocracyConfig::<T>::put(council_democracy_config_params);
99        }
100    }
101
102    #[pallet::call]
103    impl<T: Config> Pallet<T> {
104        /// Change enable domains state.
105        #[pallet::call_index(0)]
106        #[pallet::weight(< T as Config >::WeightInfo::set_enable_domains())]
107        pub fn set_enable_domains(origin: OriginFor<T>, enable_domains: bool) -> DispatchResult {
108            ensure_root(origin)?;
109
110            EnableDomains::<T>::put(enable_domains);
111
112            Ok(())
113        }
114
115        /// Enable or disable dynamic cost of storage.
116        #[pallet::call_index(1)]
117        #[pallet::weight(< T as Config >::WeightInfo::set_enable_dynamic_cost_of_storage())]
118        pub fn set_enable_dynamic_cost_of_storage(
119            origin: OriginFor<T>,
120            enable_dynamic_cost_of_storage: bool,
121        ) -> DispatchResult {
122            ensure_root(origin)?;
123
124            EnableDynamicCostOfStorage::<T>::put(enable_dynamic_cost_of_storage);
125
126            Ok(())
127        }
128
129        /// Enable or disable balance transfers for all users.
130        #[pallet::call_index(2)]
131        #[pallet::weight(< T as Config >::WeightInfo::set_enable_balance_transfers())]
132        pub fn set_enable_balance_transfers(
133            origin: OriginFor<T>,
134            enable_balance_transfers: bool,
135        ) -> DispatchResult {
136            ensure_root(origin)?;
137
138            EnableBalanceTransfers::<T>::put(enable_balance_transfers);
139
140            Ok(())
141        }
142    }
143}