Skip to main content

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