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
9use core::marker::PhantomData;
10use frame_system::pallet_prelude::BlockNumberFor;
11pub use pallet::*;
12use sp_runtime::traits::Get;
13use subspace_runtime_primitives::GenesisConfigParams;
14pub use weights::WeightInfo;
15
16const DEFAULT_GENESIS_PARAMS: GenesisConfigParams = GenesisConfigParams::production_params();
17
18pub struct DefaultDomainBlockPruning<T>(PhantomData<T>);
19impl<T: Config> Get<BlockNumberFor<T>> for DefaultDomainBlockPruning<T> {
20    fn get() -> BlockNumberFor<T> {
21        BlockNumberFor::<T>::from(DEFAULT_GENESIS_PARAMS.domain_block_pruning_depth)
22    }
23}
24
25pub struct DefaultDomainStakingWithdrawalPeriod<T>(PhantomData<T>);
26impl<T: Config> Get<BlockNumberFor<T>> for DefaultDomainStakingWithdrawalPeriod<T> {
27    fn get() -> BlockNumberFor<T> {
28        BlockNumberFor::<T>::from(DEFAULT_GENESIS_PARAMS.staking_withdrawal_period)
29    }
30}
31
32#[frame_support::pallet]
33mod pallet {
34    use crate::weights::WeightInfo;
35    use crate::{
36        DEFAULT_GENESIS_PARAMS, DefaultDomainBlockPruning, DefaultDomainStakingWithdrawalPeriod,
37    };
38    use frame_support::pallet_prelude::*;
39    use frame_system::pallet_prelude::*;
40    use sp_runtime::traits::Zero;
41    use subspace_runtime_primitives::CouncilDemocracyConfigParams;
42
43    #[pallet::pallet]
44    pub struct Pallet<T>(_);
45
46    /// Whether to enable calls in pallet-domains.
47    #[pallet::storage]
48    #[pallet::getter(fn enable_domains)]
49    pub type EnableDomains<T> = StorageValue<_, bool, ValueQuery>;
50
51    /// Whether to enable dynamic cost of storage.
52    #[pallet::storage]
53    #[pallet::getter(fn enable_dynamic_cost_of_storage)]
54    pub type EnableDynamicCostOfStorage<T> = StorageValue<_, bool, ValueQuery>;
55
56    /// Whether to enable balances transfers.
57    #[pallet::storage]
58    #[pallet::getter(fn enable_balance_transfers)]
59    pub type EnableBalanceTransfers<T> = StorageValue<_, bool, ValueQuery>;
60
61    #[pallet::storage]
62    pub type ConfirmationDepthK<T: Config> = StorageValue<_, BlockNumberFor<T>, ValueQuery>;
63
64    #[pallet::storage]
65    pub type CouncilDemocracyConfig<T: Config> =
66        StorageValue<_, CouncilDemocracyConfigParams<BlockNumberFor<T>>, ValueQuery>;
67
68    /// Domains block pruning depth.
69    #[pallet::storage]
70    pub type DomainBlockPruningDepth<T: Config> =
71        StorageValue<_, BlockNumberFor<T>, ValueQuery, DefaultDomainBlockPruning<T>>;
72
73    /// Domain nominator's staking withdrawal period
74    #[pallet::storage]
75    pub type StakingWithdrawalPeriod<T: Config> =
76        StorageValue<_, BlockNumberFor<T>, ValueQuery, DefaultDomainStakingWithdrawalPeriod<T>>;
77
78    #[pallet::config]
79    pub trait Config: frame_system::Config {
80        /// Weight information for extrinsics in this pallet.
81        type WeightInfo: WeightInfo;
82    }
83
84    #[pallet::genesis_config]
85    pub struct GenesisConfig<T: Config> {
86        /// Whether to enable domains
87        pub enable_domains: bool,
88        /// Whether to enable dynamic cost of storage (if `false` cost per byte is equal to 1)
89        pub enable_dynamic_cost_of_storage: bool,
90        /// Whether to enable balance transfers
91        pub enable_balance_transfers: bool,
92        /// Confirmation depth k to use in the archiving process
93        pub confirmation_depth_k: BlockNumberFor<T>,
94        /// Council and democracy config params.
95        pub council_democracy_config_params: CouncilDemocracyConfigParams<BlockNumberFor<T>>,
96        /// Domain block pruning depth.
97        pub domain_block_pruning_depth: BlockNumberFor<T>,
98        /// Domain nominator's staking withdrawal period.
99        pub staking_withdrawal_period: BlockNumberFor<T>,
100    }
101
102    impl<T: Config> Default for GenesisConfig<T> {
103        #[inline]
104        fn default() -> Self {
105            Self {
106                enable_domains: false,
107                enable_dynamic_cost_of_storage: false,
108                enable_balance_transfers: false,
109                confirmation_depth_k: BlockNumberFor::<T>::from(
110                    DEFAULT_GENESIS_PARAMS.confirmation_depth_k,
111                ),
112                council_democracy_config_params:
113                    CouncilDemocracyConfigParams::<BlockNumberFor<T>>::default(),
114                domain_block_pruning_depth: BlockNumberFor::<T>::from(
115                    DEFAULT_GENESIS_PARAMS.domain_block_pruning_depth,
116                ),
117                staking_withdrawal_period: BlockNumberFor::<T>::from(
118                    DEFAULT_GENESIS_PARAMS.staking_withdrawal_period,
119                ),
120            }
121        }
122    }
123
124    #[pallet::genesis_build]
125    impl<T: Config> BuildGenesisConfig for GenesisConfig<T> {
126        fn build(&self) {
127            let Self {
128                enable_domains,
129                enable_dynamic_cost_of_storage,
130                enable_balance_transfers,
131                confirmation_depth_k,
132                council_democracy_config_params,
133                domain_block_pruning_depth,
134                staking_withdrawal_period,
135            } = self;
136
137            assert!(
138                !confirmation_depth_k.is_zero(),
139                "ConfirmationDepthK can not be zero"
140            );
141
142            assert!(
143                staking_withdrawal_period >= domain_block_pruning_depth,
144                "Stake Withdrawal locking period must be >= Block tree pruning depth"
145            );
146
147            <EnableDomains<T>>::put(enable_domains);
148            <EnableDynamicCostOfStorage<T>>::put(enable_dynamic_cost_of_storage);
149            <EnableBalanceTransfers<T>>::put(enable_balance_transfers);
150            <ConfirmationDepthK<T>>::put(confirmation_depth_k);
151            CouncilDemocracyConfig::<T>::put(council_democracy_config_params);
152            DomainBlockPruningDepth::<T>::put(domain_block_pruning_depth);
153            StakingWithdrawalPeriod::<T>::put(staking_withdrawal_period);
154        }
155    }
156
157    #[pallet::call]
158    impl<T: Config> Pallet<T> {
159        /// Change enable domains state.
160        #[pallet::call_index(0)]
161        #[pallet::weight(< T as Config >::WeightInfo::set_enable_domains())]
162        pub fn set_enable_domains(origin: OriginFor<T>, enable_domains: bool) -> DispatchResult {
163            ensure_root(origin)?;
164
165            EnableDomains::<T>::put(enable_domains);
166
167            Ok(())
168        }
169
170        /// Enable or disable dynamic cost of storage.
171        #[pallet::call_index(1)]
172        #[pallet::weight(< T as Config >::WeightInfo::set_enable_dynamic_cost_of_storage())]
173        pub fn set_enable_dynamic_cost_of_storage(
174            origin: OriginFor<T>,
175            enable_dynamic_cost_of_storage: bool,
176        ) -> DispatchResult {
177            ensure_root(origin)?;
178
179            EnableDynamicCostOfStorage::<T>::put(enable_dynamic_cost_of_storage);
180
181            Ok(())
182        }
183
184        /// Enable or disable balance transfers for all users.
185        #[pallet::call_index(2)]
186        #[pallet::weight(< T as Config >::WeightInfo::set_enable_balance_transfers())]
187        pub fn set_enable_balance_transfers(
188            origin: OriginFor<T>,
189            enable_balance_transfers: bool,
190        ) -> DispatchResult {
191            ensure_root(origin)?;
192
193            EnableBalanceTransfers::<T>::put(enable_balance_transfers);
194
195            Ok(())
196        }
197    }
198}