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