configuration.rs 42.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Copyright 2020 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.

// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.

//! Configuration manager for the Polkadot runtime parachains logic.
//!
//! Configuration can change only at session boundaries and is buffered until then.

Shawn Tabrizi's avatar
Shawn Tabrizi committed
21
use crate::shared;
22
use frame_support::{pallet_prelude::*, weights::constants::WEIGHT_PER_MILLIS};
Shawn Tabrizi's avatar
Shawn Tabrizi committed
23
24
use frame_system::pallet_prelude::*;
use parity_scale_codec::{Decode, Encode};
25
use primitives::v1::{Balance, SessionIndex, MAX_CODE_SIZE, MAX_HEAD_DATA_SIZE, MAX_POV_SIZE};
26
use sp_runtime::traits::Zero;
Shawn Tabrizi's avatar
Shawn Tabrizi committed
27
use sp_std::prelude::*;
28

29
30
31
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;

32
33
pub use pallet::*;

34
35
36
37
pub mod migration;

const LOG_TARGET: &str = "runtime::configuration";

38
/// All configuration of the runtime with respect to parachains and parathreads.
39
#[derive(Clone, Encode, Decode, PartialEq, sp_core::RuntimeDebug, scale_info::TypeInfo)]
40
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
asynchronous rob's avatar
asynchronous rob committed
41
pub struct HostConfiguration<BlockNumber> {
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
	// NOTE: This structure is used by parachains via merkle proofs. Therefore, this struct requires
	// special treatment.
	//
	// A parachain requested this struct can only depend on the subset of this struct. Specifically,
	// only a first few fields can be depended upon. These fields cannot be changed without
	// corresponding migration of the parachains.
	/**
	 * The parameters that are required for the parachains.
	 */

	/// The maximum validation code size, in bytes.
	pub max_code_size: u32,
	/// The maximum head-data size, in bytes.
	pub max_head_data_size: u32,
	/// Total number of individual messages allowed in the parachain -> relay-chain message queue.
	pub max_upward_queue_count: u32,
	/// Total size of messages allowed in the parachain -> relay-chain message queue before which
	/// no further messages may be added to it. If it exceeds this then the queue may contain only
	/// a single message.
	pub max_upward_queue_size: u32,
	/// The maximum size of an upward message that can be sent by a candidate.
	///
	/// This parameter affects the size upper bound of the `CandidateCommitments`.
	pub max_upward_message_size: u32,
	/// The maximum number of messages that a candidate can contain.
	///
	/// This parameter affects the size upper bound of the `CandidateCommitments`.
	pub max_upward_message_num_per_candidate: u32,
	/// The maximum number of outbound HRMP messages can be sent by a candidate.
	///
	/// This parameter affects the upper bound of size of `CandidateCommitments`.
	pub hrmp_max_message_num_per_candidate: u32,
74
75
76
77
	/// The minimum frequency at which parachains can update their validation code.
	pub validation_upgrade_frequency: BlockNumber,
	/// The delay, in blocks, before a validation upgrade is applied.
	pub validation_upgrade_delay: BlockNumber,
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95

	/**
	 * The parameters that are not essential, but still may be of interest for parachains.
	 */

	/// The maximum POV block size, in bytes.
	pub max_pov_size: u32,
	/// The maximum size of a message that can be put in a downward message queue.
	///
	/// Since we require receiving at least one DMP message the obvious upper bound of the size is
	/// the PoV size. Of course, there is a lot of other different things that a parachain may
	/// decide to do with its PoV so this value in practice will be picked as a fraction of the PoV
	/// size.
	pub max_downward_message_size: u32,
	/// The amount of weight we wish to devote to the processing the dispatchable upward messages
	/// stage.
	///
	/// NOTE that this is a soft limit and could be exceeded.
96
	pub ump_service_total_weight: Weight,
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
	/// The maximum number of outbound HRMP channels a parachain is allowed to open.
	pub hrmp_max_parachain_outbound_channels: u32,
	/// The maximum number of outbound HRMP channels a parathread is allowed to open.
	pub hrmp_max_parathread_outbound_channels: u32,
	/// The deposit that the sender should provide for opening an HRMP channel.
	pub hrmp_sender_deposit: Balance,
	/// The deposit that the recipient should provide for accepting opening an HRMP channel.
	pub hrmp_recipient_deposit: Balance,
	/// The maximum number of messages allowed in an HRMP channel at once.
	pub hrmp_channel_max_capacity: u32,
	/// The maximum total size of messages in bytes allowed in an HRMP channel at once.
	pub hrmp_channel_max_total_size: u32,
	/// The maximum number of inbound HRMP channels a parachain is allowed to accept.
	pub hrmp_max_parachain_inbound_channels: u32,
	/// The maximum number of inbound HRMP channels a parathread is allowed to accept.
	pub hrmp_max_parathread_inbound_channels: u32,
	/// The maximum size of a message that could ever be put into an HRMP channel.
	///
	/// This parameter affects the upper bound of size of `CandidateCommitments`.
	pub hrmp_channel_max_message_size: u32,

	/**
	 * Parameters that will unlikely be needed by parachains.
	 */

122
123
124
	/// How long to keep code on-chain, in blocks. This should be sufficiently long that disputes
	/// have concluded.
	pub code_retention_period: BlockNumber,
125
126
127
128
	/// The amount of execution cores to dedicate to parathread execution.
	pub parathread_cores: u32,
	/// The number of retries that a parathread author has to submit their block.
	pub parathread_retries: u32,
129
130
131
	/// How often parachain groups should be rotated across parachains.
	///
	/// Must be non-zero.
132
	pub group_rotation_frequency: BlockNumber,
133
134
	/// The availability period, in blocks, for parachains. This is the amount of blocks
	/// after inclusion that validators have to make the block available and signal its availability to
135
136
137
	/// the chain.
	///
	/// Must be at least 1.
138
139
	pub chain_availability_period: BlockNumber,
	/// The availability period, in blocks, for parathreads. Same as the `chain_availability_period`,
140
141
142
	/// but a differing timeout due to differing requirements.
	///
	/// Must be at least 1.
143
144
145
	pub thread_availability_period: BlockNumber,
	/// The amount of blocks ahead to schedule parachains and parathreads.
	pub scheduling_lookahead: u32,
146
147
148
	/// The maximum number of validators to have per core.
	///
	/// `None` means no maximum.
149
	pub max_validators_per_core: Option<u32>,
Denis_P's avatar
Denis_P committed
150
	/// The maximum number of validators to use for parachain consensus, period.
151
152
153
	///
	/// `None` means no maximum.
	pub max_validators: Option<u32>,
154
155
	/// The amount of sessions to keep for disputes.
	pub dispute_period: SessionIndex,
156
157
158
159
160
161
	/// How long after dispute conclusion to accept statements.
	pub dispute_post_conclusion_acceptance_period: BlockNumber,
	/// The maximum number of dispute spam slots
	pub dispute_max_spam_slots: u32,
	/// How long it takes for a dispute to conclude by time-out, if no supermajority is reached.
	pub dispute_conclusion_by_time_out_period: BlockNumber,
162
163
	/// The amount of consensus slots that must pass between submitting an assignment and
	/// submitting an approval vote before a validator is considered a no-show.
164
	///
165
166
167
168
169
170
171
172
173
	/// Must be at least 1.
	pub no_show_slots: u32,
	/// The number of delay tranches in total.
	pub n_delay_tranches: u32,
	/// The width of the zeroth delay tranche for approval assignments. This many delay tranches
	/// beyond 0 are all consolidated to form a wide 0 tranche.
	pub zeroth_delay_tranche_width: u32,
	/// The number of validators needed to approve a block.
	pub needed_approvals: u32,
Denis_P's avatar
Denis_P committed
174
	/// The number of samples to do of the `RelayVRFModulo` approval assignment criterion.
175
	pub relay_vrf_modulo_samples: u32,
176
177
178
	/// The maximum amount of weight any individual upward message may consume. Messages above this
	/// weight go into the overweight queue and may only be serviced explicitly.
	pub ump_max_individual_weight: Weight,
179
180
}

181
182
183
184
185
186
187
188
189
impl<BlockNumber: Default + From<u32>> Default for HostConfiguration<BlockNumber> {
	fn default() -> Self {
		Self {
			group_rotation_frequency: 1u32.into(),
			chain_availability_period: 1u32.into(),
			thread_availability_period: 1u32.into(),
			no_show_slots: 1u32.into(),
			validation_upgrade_frequency: Default::default(),
			validation_upgrade_delay: Default::default(),
190
			code_retention_period: Default::default(),
191
192
193
194
195
196
197
			max_code_size: Default::default(),
			max_pov_size: Default::default(),
			max_head_data_size: Default::default(),
			parathread_cores: Default::default(),
			parathread_retries: Default::default(),
			scheduling_lookahead: Default::default(),
			max_validators_per_core: Default::default(),
198
			max_validators: None,
199
200
201
202
			dispute_period: 6,
			dispute_post_conclusion_acceptance_period: 100.into(),
			dispute_max_spam_slots: 2,
			dispute_conclusion_by_time_out_period: 200.into(),
203
204
205
206
207
208
209
			n_delay_tranches: Default::default(),
			zeroth_delay_tranche_width: Default::default(),
			needed_approvals: Default::default(),
			relay_vrf_modulo_samples: Default::default(),
			max_upward_queue_count: Default::default(),
			max_upward_queue_size: Default::default(),
			max_downward_message_size: Default::default(),
210
			ump_service_total_weight: Default::default(),
211
212
213
214
215
216
217
218
219
220
221
222
			max_upward_message_size: Default::default(),
			max_upward_message_num_per_candidate: Default::default(),
			hrmp_sender_deposit: Default::default(),
			hrmp_recipient_deposit: Default::default(),
			hrmp_channel_max_capacity: Default::default(),
			hrmp_channel_max_total_size: Default::default(),
			hrmp_max_parachain_inbound_channels: Default::default(),
			hrmp_max_parathread_inbound_channels: Default::default(),
			hrmp_channel_max_message_size: Default::default(),
			hrmp_max_parachain_outbound_channels: Default::default(),
			hrmp_max_parathread_outbound_channels: Default::default(),
			hrmp_max_message_num_per_candidate: Default::default(),
223
			ump_max_individual_weight: 20 * WEIGHT_PER_MILLIS,
224
225
226
227
228
229
230
231
232
233
		}
	}
}

impl<BlockNumber: Zero> HostConfiguration<BlockNumber> {
	/// Checks that this instance is consistent with the requirements on each individual member.
	///
	/// # Panic
	///
	/// This function panics if any member is not set properly.
234
	pub fn check_consistency(&self) {
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
		if self.group_rotation_frequency.is_zero() {
			panic!("`group_rotation_frequency` must be non-zero!")
		}

		if self.chain_availability_period.is_zero() {
			panic!("`chain_availability_period` must be at least 1!")
		}

		if self.thread_availability_period.is_zero() {
			panic!("`thread_availability_period` must be at least 1!")
		}

		if self.no_show_slots.is_zero() {
			panic!("`no_show_slots` must be at least 1!")
		}
250
251
252
253

		if self.max_code_size > MAX_CODE_SIZE {
			panic!(
				"`max_code_size` ({}) is bigger than allowed by the client ({})",
Shawn Tabrizi's avatar
Shawn Tabrizi committed
254
				self.max_code_size, MAX_CODE_SIZE,
255
256
257
			)
		}

258
259
260
261
262
263
264
		if self.max_head_data_size > MAX_HEAD_DATA_SIZE {
			panic!(
				"`max_head_data_size` ({}) is bigger than allowed by the client ({})",
				self.max_head_data_size, MAX_HEAD_DATA_SIZE
			)
		}

265
266
267
		if self.max_pov_size > MAX_POV_SIZE {
			panic!("`max_pov_size` is bigger than allowed by the client")
		}
268
269
270
	}
}

271
272
273
274
275
276
277
278
279
pub trait WeightInfo {
	fn set_config_with_block_number() -> Weight;
	fn set_config_with_u32() -> Weight;
	fn set_config_with_option_u32() -> Weight;
	fn set_config_with_weight() -> Weight;
	fn set_config_with_balance() -> Weight;
	fn set_hrmp_open_request_ttl() -> Weight;
}

280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
pub struct TestWeightInfo;
impl WeightInfo for TestWeightInfo {
	fn set_config_with_block_number() -> Weight {
		Weight::MAX
	}
	fn set_config_with_u32() -> Weight {
		Weight::MAX
	}
	fn set_config_with_option_u32() -> Weight {
		Weight::MAX
	}
	fn set_config_with_weight() -> Weight {
		Weight::MAX
	}
	fn set_config_with_balance() -> Weight {
		Weight::MAX
	}
	fn set_hrmp_open_request_ttl() -> Weight {
		Weight::MAX
	}
}

302
303
304
#[frame_support::pallet]
pub mod pallet {
	use super::*;
305

306
307
	#[pallet::pallet]
	#[pallet::generate_store(pub(super) trait Store)]
308
	#[pallet::storage_version(migration::STORAGE_VERSION)]
309
	pub struct Pallet<T>(_);
310

311
	#[pallet::config]
312
313
314
315
	pub trait Config: frame_system::Config + shared::Config {
		/// Weight information for extrinsics in this pallet.
		type WeightInfo: WeightInfo;
	}
316
317
318

	#[pallet::error]
	pub enum Error<T> {
319
320
321
		/// The new value for a configuration parameter is invalid.
		InvalidNewValue,
	}
322

323
324
325
	/// The active configuration for the current session.
	#[pallet::storage]
	#[pallet::getter(fn config)]
Shawn Tabrizi's avatar
Shawn Tabrizi committed
326
327
	pub(crate) type ActiveConfig<T: Config> =
		StorageValue<_, HostConfiguration<T::BlockNumber>, ValueQuery>;
328
329
330

	/// Pending configuration (if any) for the next session.
	#[pallet::storage]
Shawn Tabrizi's avatar
Shawn Tabrizi committed
331
332
	pub(crate) type PendingConfig<T: Config> =
		StorageMap<_, Twox64Concat, SessionIndex, HostConfiguration<T::BlockNumber>>;
333
334
335

	#[pallet::genesis_config]
	pub struct GenesisConfig<T: Config> {
Shawn Tabrizi's avatar
Shawn Tabrizi committed
336
		pub config: HostConfiguration<T::BlockNumber>,
337
338
339
340
341
	}

	#[cfg(feature = "std")]
	impl<T: Config> Default for GenesisConfig<T> {
		fn default() -> Self {
Shawn Tabrizi's avatar
Shawn Tabrizi committed
342
			GenesisConfig { config: Default::default() }
343
344
345
346
347
348
349
350
351
352
		}
	}

	#[pallet::genesis_build]
	impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
		fn build(&self) {
			self.config.check_consistency();
			ActiveConfig::<T>::put(&self.config);
		}
	}
353

354
355
	#[pallet::call]
	impl<T: Config> Pallet<T> {
356
		/// Set the validation upgrade frequency.
357
358
359
360
		#[pallet::weight((
			T::WeightInfo::set_config_with_block_number(),
			DispatchClass::Operational,
		))]
Shawn Tabrizi's avatar
Shawn Tabrizi committed
361
362
363
364
		pub fn set_validation_upgrade_frequency(
			origin: OriginFor<T>,
			new: T::BlockNumber,
		) -> DispatchResult {
365
366
367
368
369
370
371
372
			ensure_root(origin)?;
			Self::update_config_member(|config| {
				sp_std::mem::replace(&mut config.validation_upgrade_frequency, new) != new
			});
			Ok(())
		}

		/// Set the validation upgrade delay.
373
374
375
376
		#[pallet::weight((
			T::WeightInfo::set_config_with_block_number(),
			DispatchClass::Operational,
		))]
Shawn Tabrizi's avatar
Shawn Tabrizi committed
377
378
379
380
		pub fn set_validation_upgrade_delay(
			origin: OriginFor<T>,
			new: T::BlockNumber,
		) -> DispatchResult {
381
382
383
384
385
386
387
388
			ensure_root(origin)?;
			Self::update_config_member(|config| {
				sp_std::mem::replace(&mut config.validation_upgrade_delay, new) != new
			});
			Ok(())
		}

		/// Set the acceptance period for an included candidate.
389
390
391
392
		#[pallet::weight((
			T::WeightInfo::set_config_with_block_number(),
			DispatchClass::Operational,
		))]
Shawn Tabrizi's avatar
Shawn Tabrizi committed
393
394
395
396
		pub fn set_code_retention_period(
			origin: OriginFor<T>,
			new: T::BlockNumber,
		) -> DispatchResult {
397
398
			ensure_root(origin)?;
			Self::update_config_member(|config| {
399
				sp_std::mem::replace(&mut config.code_retention_period, new) != new
400
401
402
403
404
			});
			Ok(())
		}

		/// Set the max validation code size for incoming upgrades.
405
406
407
408
		#[pallet::weight((
			T::WeightInfo::set_config_with_u32(),
			DispatchClass::Operational,
		))]
409
		pub fn set_max_code_size(origin: OriginFor<T>, new: u32) -> DispatchResult {
410
			ensure_root(origin)?;
411
			ensure!(new <= MAX_CODE_SIZE, Error::<T>::InvalidNewValue);
412
413
414
415
416
417
			Self::update_config_member(|config| {
				sp_std::mem::replace(&mut config.max_code_size, new) != new
			});
			Ok(())
		}

418
		/// Set the max POV block size for incoming upgrades.
419
420
421
422
		#[pallet::weight((
			T::WeightInfo::set_config_with_u32(),
			DispatchClass::Operational,
		))]
423
		pub fn set_max_pov_size(origin: OriginFor<T>, new: u32) -> DispatchResult {
424
			ensure_root(origin)?;
425
			ensure!(new <= MAX_POV_SIZE, Error::<T>::InvalidNewValue);
426
427
428
429
430
431
			Self::update_config_member(|config| {
				sp_std::mem::replace(&mut config.max_pov_size, new) != new
			});
			Ok(())
		}

432
		/// Set the max head data size for paras.
433
434
435
436
		#[pallet::weight((
			T::WeightInfo::set_config_with_u32(),
			DispatchClass::Operational,
		))]
437
		pub fn set_max_head_data_size(origin: OriginFor<T>, new: u32) -> DispatchResult {
438
			ensure_root(origin)?;
439
			ensure!(new <= MAX_HEAD_DATA_SIZE, Error::<T>::InvalidNewValue);
440
441
442
443
444
445
446
			Self::update_config_member(|config| {
				sp_std::mem::replace(&mut config.max_head_data_size, new) != new
			});
			Ok(())
		}

		/// Set the number of parathread execution cores.
447
448
449
450
		#[pallet::weight((
			T::WeightInfo::set_config_with_u32(),
			DispatchClass::Operational,
		))]
451
		pub fn set_parathread_cores(origin: OriginFor<T>, new: u32) -> DispatchResult {
452
453
454
455
456
457
458
459
			ensure_root(origin)?;
			Self::update_config_member(|config| {
				sp_std::mem::replace(&mut config.parathread_cores, new) != new
			});
			Ok(())
		}

		/// Set the number of retries for a particular parathread.
460
461
462
463
		#[pallet::weight((
			T::WeightInfo::set_config_with_u32(),
			DispatchClass::Operational,
		))]
464
		pub fn set_parathread_retries(origin: OriginFor<T>, new: u32) -> DispatchResult {
465
466
467
468
469
470
471
472
			ensure_root(origin)?;
			Self::update_config_member(|config| {
				sp_std::mem::replace(&mut config.parathread_retries, new) != new
			});
			Ok(())
		}

		/// Set the parachain validator-group rotation frequency
473
474
475
476
		#[pallet::weight((
			T::WeightInfo::set_config_with_block_number(),
			DispatchClass::Operational,
		))]
Shawn Tabrizi's avatar
Shawn Tabrizi committed
477
478
479
480
		pub fn set_group_rotation_frequency(
			origin: OriginFor<T>,
			new: T::BlockNumber,
		) -> DispatchResult {
481
			ensure_root(origin)?;
482
483
484

			ensure!(!new.is_zero(), Error::<T>::InvalidNewValue);

485
			Self::update_config_member(|config| {
486
				sp_std::mem::replace(&mut config.group_rotation_frequency, new) != new
487
488
489
490
491
			});
			Ok(())
		}

		/// Set the availability period for parachains.
492
493
494
495
		#[pallet::weight((
			T::WeightInfo::set_config_with_block_number(),
			DispatchClass::Operational,
		))]
Shawn Tabrizi's avatar
Shawn Tabrizi committed
496
497
498
499
		pub fn set_chain_availability_period(
			origin: OriginFor<T>,
			new: T::BlockNumber,
		) -> DispatchResult {
500
			ensure_root(origin)?;
501
502
503

			ensure!(!new.is_zero(), Error::<T>::InvalidNewValue);

504
505
506
507
508
509
510
			Self::update_config_member(|config| {
				sp_std::mem::replace(&mut config.chain_availability_period, new) != new
			});
			Ok(())
		}

		/// Set the availability period for parathreads.
511
512
513
514
		#[pallet::weight((
			T::WeightInfo::set_config_with_block_number(),
			DispatchClass::Operational,
		))]
Shawn Tabrizi's avatar
Shawn Tabrizi committed
515
516
517
518
		pub fn set_thread_availability_period(
			origin: OriginFor<T>,
			new: T::BlockNumber,
		) -> DispatchResult {
519
			ensure_root(origin)?;
520
521
522

			ensure!(!new.is_zero(), Error::<T>::InvalidNewValue);

523
524
525
526
527
528
529
			Self::update_config_member(|config| {
				sp_std::mem::replace(&mut config.thread_availability_period, new) != new
			});
			Ok(())
		}

		/// Set the scheduling lookahead, in expected number of blocks at peak throughput.
530
531
532
533
		#[pallet::weight((
			T::WeightInfo::set_config_with_u32(),
			DispatchClass::Operational,
		))]
534
		pub fn set_scheduling_lookahead(origin: OriginFor<T>, new: u32) -> DispatchResult {
535
536
537
538
539
540
			ensure_root(origin)?;
			Self::update_config_member(|config| {
				sp_std::mem::replace(&mut config.scheduling_lookahead, new) != new
			});
			Ok(())
		}
541

542
		/// Set the maximum number of validators to assign to any core.
543
544
545
546
		#[pallet::weight((
			T::WeightInfo::set_config_with_option_u32(),
			DispatchClass::Operational,
		))]
Shawn Tabrizi's avatar
Shawn Tabrizi committed
547
548
549
550
		pub fn set_max_validators_per_core(
			origin: OriginFor<T>,
			new: Option<u32>,
		) -> DispatchResult {
551
552
553
554
555
556
557
			ensure_root(origin)?;
			Self::update_config_member(|config| {
				sp_std::mem::replace(&mut config.max_validators_per_core, new) != new
			});
			Ok(())
		}

558
		/// Set the maximum number of validators to use in parachain consensus.
559
560
561
562
		#[pallet::weight((
			T::WeightInfo::set_config_with_option_u32(),
			DispatchClass::Operational,
		))]
563
		pub fn set_max_validators(origin: OriginFor<T>, new: Option<u32>) -> DispatchResult {
564
565
566
567
568
569
570
			ensure_root(origin)?;
			Self::update_config_member(|config| {
				sp_std::mem::replace(&mut config.max_validators, new) != new
			});
			Ok(())
		}

571
		/// Set the dispute period, in number of sessions to keep for disputes.
572
573
574
575
		#[pallet::weight((
			T::WeightInfo::set_config_with_u32(),
			DispatchClass::Operational,
		))]
576
		pub fn set_dispute_period(origin: OriginFor<T>, new: SessionIndex) -> DispatchResult {
577
578
579
580
581
582
583
			ensure_root(origin)?;
			Self::update_config_member(|config| {
				sp_std::mem::replace(&mut config.dispute_period, new) != new
			});
			Ok(())
		}

584
		/// Set the dispute post conclusion acceptance period.
585
586
587
588
		#[pallet::weight((
			T::WeightInfo::set_config_with_block_number(),
			DispatchClass::Operational,
		))]
589
		pub fn set_dispute_post_conclusion_acceptance_period(
590
			origin: OriginFor<T>,
591
592
593
594
			new: T::BlockNumber,
		) -> DispatchResult {
			ensure_root(origin)?;
			Self::update_config_member(|config| {
Shawn Tabrizi's avatar
Shawn Tabrizi committed
595
596
				sp_std::mem::replace(&mut config.dispute_post_conclusion_acceptance_period, new) !=
					new
597
598
599
600
601
			});
			Ok(())
		}

		/// Set the maximum number of dispute spam slots.
602
603
604
605
		#[pallet::weight((
			T::WeightInfo::set_config_with_u32(),
			DispatchClass::Operational,
		))]
606
		pub fn set_dispute_max_spam_slots(origin: OriginFor<T>, new: u32) -> DispatchResult {
607
608
609
610
611
612
613
614
			ensure_root(origin)?;
			Self::update_config_member(|config| {
				sp_std::mem::replace(&mut config.dispute_max_spam_slots, new) != new
			});
			Ok(())
		}

		/// Set the dispute conclusion by time out period.
615
616
617
618
		#[pallet::weight((
			T::WeightInfo::set_config_with_block_number(),
			DispatchClass::Operational,
		))]
Shawn Tabrizi's avatar
Shawn Tabrizi committed
619
620
621
622
		pub fn set_dispute_conclusion_by_time_out_period(
			origin: OriginFor<T>,
			new: T::BlockNumber,
		) -> DispatchResult {
623
624
625
626
627
628
629
			ensure_root(origin)?;
			Self::update_config_member(|config| {
				sp_std::mem::replace(&mut config.dispute_conclusion_by_time_out_period, new) != new
			});
			Ok(())
		}

630
631
		/// Set the no show slots, in number of number of consensus slots.
		/// Must be at least 1.
632
633
634
635
		#[pallet::weight((
			T::WeightInfo::set_config_with_u32(),
			DispatchClass::Operational,
		))]
636
		pub fn set_no_show_slots(origin: OriginFor<T>, new: u32) -> DispatchResult {
637
			ensure_root(origin)?;
638
639
640

			ensure!(!new.is_zero(), Error::<T>::InvalidNewValue);

641
642
643
644
645
646
647
			Self::update_config_member(|config| {
				sp_std::mem::replace(&mut config.no_show_slots, new) != new
			});
			Ok(())
		}

		/// Set the total number of delay tranches.
648
649
650
651
		#[pallet::weight((
			T::WeightInfo::set_config_with_u32(),
			DispatchClass::Operational,
		))]
652
		pub fn set_n_delay_tranches(origin: OriginFor<T>, new: u32) -> DispatchResult {
653
654
655
656
657
658
659
660
			ensure_root(origin)?;
			Self::update_config_member(|config| {
				sp_std::mem::replace(&mut config.n_delay_tranches, new) != new
			});
			Ok(())
		}

		/// Set the zeroth delay tranche width.
661
662
663
664
		#[pallet::weight((
			T::WeightInfo::set_config_with_u32(),
			DispatchClass::Operational,
		))]
665
		pub fn set_zeroth_delay_tranche_width(origin: OriginFor<T>, new: u32) -> DispatchResult {
666
667
668
669
670
671
672
673
			ensure_root(origin)?;
			Self::update_config_member(|config| {
				sp_std::mem::replace(&mut config.zeroth_delay_tranche_width, new) != new
			});
			Ok(())
		}

		/// Set the number of validators needed to approve a block.
674
675
676
677
		#[pallet::weight((
			T::WeightInfo::set_config_with_u32(),
			DispatchClass::Operational,
		))]
678
		pub fn set_needed_approvals(origin: OriginFor<T>, new: u32) -> DispatchResult {
679
680
681
682
683
684
685
			ensure_root(origin)?;
			Self::update_config_member(|config| {
				sp_std::mem::replace(&mut config.needed_approvals, new) != new
			});
			Ok(())
		}

Denis_P's avatar
Denis_P committed
686
		/// Set the number of samples to do of the `RelayVRFModulo` approval assignment criterion.
687
688
689
690
		#[pallet::weight((
			T::WeightInfo::set_config_with_u32(),
			DispatchClass::Operational,
		))]
691
		pub fn set_relay_vrf_modulo_samples(origin: OriginFor<T>, new: u32) -> DispatchResult {
692
693
694
695
696
697
698
			ensure_root(origin)?;
			Self::update_config_member(|config| {
				sp_std::mem::replace(&mut config.relay_vrf_modulo_samples, new) != new
			});
			Ok(())
		}

699
		/// Sets the maximum items that can present in a upward dispatch queue at once.
700
701
702
703
		#[pallet::weight((
			T::WeightInfo::set_config_with_u32(),
			DispatchClass::Operational,
		))]
704
		pub fn set_max_upward_queue_count(origin: OriginFor<T>, new: u32) -> DispatchResult {
705
706
707
708
709
710
711
712
			ensure_root(origin)?;
			Self::update_config_member(|config| {
				sp_std::mem::replace(&mut config.max_upward_queue_count, new) != new
			});
			Ok(())
		}

		/// Sets the maximum total size of items that can present in a upward dispatch queue at once.
713
714
715
716
		#[pallet::weight((
			T::WeightInfo::set_config_with_u32(),
			DispatchClass::Operational,
		))]
717
		pub fn set_max_upward_queue_size(origin: OriginFor<T>, new: u32) -> DispatchResult {
718
719
720
721
722
723
724
			ensure_root(origin)?;
			Self::update_config_member(|config| {
				sp_std::mem::replace(&mut config.max_upward_queue_size, new) != new
			});
			Ok(())
		}

725
		/// Set the critical downward message size.
726
727
728
729
		#[pallet::weight((
			T::WeightInfo::set_config_with_u32(),
			DispatchClass::Operational,
		))]
730
		pub fn set_max_downward_message_size(origin: OriginFor<T>, new: u32) -> DispatchResult {
731
732
733
734
735
736
			ensure_root(origin)?;
			Self::update_config_member(|config| {
				sp_std::mem::replace(&mut config.max_downward_message_size, new) != new
			});
			Ok(())
		}
737
738

		/// Sets the soft limit for the phase of dispatching dispatchable upward messages.
739
740
741
742
		#[pallet::weight((
			T::WeightInfo::set_config_with_weight(),
			DispatchClass::Operational,
		))]
743
		pub fn set_ump_service_total_weight(origin: OriginFor<T>, new: Weight) -> DispatchResult {
744
745
			ensure_root(origin)?;
			Self::update_config_member(|config| {
746
				sp_std::mem::replace(&mut config.ump_service_total_weight, new) != new
747
748
749
750
751
			});
			Ok(())
		}

		/// Sets the maximum size of an upward message that can be sent by a candidate.
752
753
754
755
		#[pallet::weight((
			T::WeightInfo::set_config_with_u32(),
			DispatchClass::Operational,
		))]
756
		pub fn set_max_upward_message_size(origin: OriginFor<T>, new: u32) -> DispatchResult {
757
758
759
760
761
762
763
764
			ensure_root(origin)?;
			Self::update_config_member(|config| {
				sp_std::mem::replace(&mut config.max_upward_message_size, new) != new
			});
			Ok(())
		}

		/// Sets the maximum number of messages that a candidate can contain.
765
766
767
768
		#[pallet::weight((
			T::WeightInfo::set_config_with_u32(),
			DispatchClass::Operational,
		))]
Shawn Tabrizi's avatar
Shawn Tabrizi committed
769
770
771
772
		pub fn set_max_upward_message_num_per_candidate(
			origin: OriginFor<T>,
			new: u32,
		) -> DispatchResult {
773
774
775
776
777
778
			ensure_root(origin)?;
			Self::update_config_member(|config| {
				sp_std::mem::replace(&mut config.max_upward_message_num_per_candidate, new) != new
			});
			Ok(())
		}
Sergey Pepyakin's avatar
Sergey Pepyakin committed
779
780

		/// Sets the number of sessions after which an HRMP open channel request expires.
781
782
783
784
		#[pallet::weight((
			T::WeightInfo::set_hrmp_open_request_ttl(),
			DispatchClass::Operational,
		))]
785
786
787
788
		// Deprecated, but is not marked as such, because that would trigger warnings coming from
		// the macro.
		pub fn set_hrmp_open_request_ttl(_origin: OriginFor<T>, _new: u32) -> DispatchResult {
			Err("this doesn't have any effect".into())
Sergey Pepyakin's avatar
Sergey Pepyakin committed
789
790
791
		}

		/// Sets the amount of funds that the sender should provide for opening an HRMP channel.
792
793
794
795
		#[pallet::weight((
			T::WeightInfo::set_config_with_balance(),
			DispatchClass::Operational,
		))]
796
		pub fn set_hrmp_sender_deposit(origin: OriginFor<T>, new: Balance) -> DispatchResult {
Sergey Pepyakin's avatar
Sergey Pepyakin committed
797
798
799
800
801
802
803
804
805
			ensure_root(origin)?;
			Self::update_config_member(|config| {
				sp_std::mem::replace(&mut config.hrmp_sender_deposit, new) != new
			});
			Ok(())
		}

		/// Sets the amount of funds that the recipient should provide for accepting opening an HRMP
		/// channel.
806
807
808
809
		#[pallet::weight((
			T::WeightInfo::set_config_with_balance(),
			DispatchClass::Operational,
		))]
810
		pub fn set_hrmp_recipient_deposit(origin: OriginFor<T>, new: Balance) -> DispatchResult {
Sergey Pepyakin's avatar
Sergey Pepyakin committed
811
812
813
814
815
816
817
818
			ensure_root(origin)?;
			Self::update_config_member(|config| {
				sp_std::mem::replace(&mut config.hrmp_recipient_deposit, new) != new
			});
			Ok(())
		}

		/// Sets the maximum number of messages allowed in an HRMP channel at once.
819
820
821
822
		#[pallet::weight((
			T::WeightInfo::set_config_with_u32(),
			DispatchClass::Operational,
		))]
823
		pub fn set_hrmp_channel_max_capacity(origin: OriginFor<T>, new: u32) -> DispatchResult {
Sergey Pepyakin's avatar
Sergey Pepyakin committed
824
825
826
827
828
829
830
831
			ensure_root(origin)?;
			Self::update_config_member(|config| {
				sp_std::mem::replace(&mut config.hrmp_channel_max_capacity, new) != new
			});
			Ok(())
		}

		/// Sets the maximum total size of messages in bytes allowed in an HRMP channel at once.
832
833
834
835
		#[pallet::weight((
			T::WeightInfo::set_config_with_u32(),
			DispatchClass::Operational,
		))]
836
		pub fn set_hrmp_channel_max_total_size(origin: OriginFor<T>, new: u32) -> DispatchResult {
Sergey Pepyakin's avatar
Sergey Pepyakin committed
837
838
839
840
841
842
843
844
			ensure_root(origin)?;
			Self::update_config_member(|config| {
				sp_std::mem::replace(&mut config.hrmp_channel_max_total_size, new) != new
			});
			Ok(())
		}

		/// Sets the maximum number of inbound HRMP channels a parachain is allowed to accept.
845
846
847
848
		#[pallet::weight((
			T::WeightInfo::set_config_with_u32(),
			DispatchClass::Operational,
		))]
Shawn Tabrizi's avatar
Shawn Tabrizi committed
849
850
851
852
		pub fn set_hrmp_max_parachain_inbound_channels(
			origin: OriginFor<T>,
			new: u32,
		) -> DispatchResult {
Sergey Pepyakin's avatar
Sergey Pepyakin committed
853
854
855
856
857
858
859
860
			ensure_root(origin)?;
			Self::update_config_member(|config| {
				sp_std::mem::replace(&mut config.hrmp_max_parachain_inbound_channels, new) != new
			});
			Ok(())
		}

		/// Sets the maximum number of inbound HRMP channels a parathread is allowed to accept.
861
862
863
864
		#[pallet::weight((
			T::WeightInfo::set_config_with_u32(),
			DispatchClass::Operational,
		))]
Shawn Tabrizi's avatar
Shawn Tabrizi committed
865
866
867
868
		pub fn set_hrmp_max_parathread_inbound_channels(
			origin: OriginFor<T>,
			new: u32,
		) -> DispatchResult {
Sergey Pepyakin's avatar
Sergey Pepyakin committed
869
870
871
872
873
874
875
876
			ensure_root(origin)?;
			Self::update_config_member(|config| {
				sp_std::mem::replace(&mut config.hrmp_max_parathread_inbound_channels, new) != new
			});
			Ok(())
		}

		/// Sets the maximum size of a message that could ever be put into an HRMP channel.
877
878
879
880
		#[pallet::weight((
			T::WeightInfo::set_config_with_u32(),
			DispatchClass::Operational,
		))]
881
		pub fn set_hrmp_channel_max_message_size(origin: OriginFor<T>, new: u32) -> DispatchResult {
Sergey Pepyakin's avatar
Sergey Pepyakin committed
882
883
884
885
886
887
888
889
			ensure_root(origin)?;
			Self::update_config_member(|config| {
				sp_std::mem::replace(&mut config.hrmp_channel_max_message_size, new) != new
			});
			Ok(())
		}

		/// Sets the maximum number of outbound HRMP channels a parachain is allowed to open.
890
891
892
893
		#[pallet::weight((
			T::WeightInfo::set_config_with_u32(),
			DispatchClass::Operational,
		))]
Shawn Tabrizi's avatar
Shawn Tabrizi committed
894
895
896
897
		pub fn set_hrmp_max_parachain_outbound_channels(
			origin: OriginFor<T>,
			new: u32,
		) -> DispatchResult {
Sergey Pepyakin's avatar
Sergey Pepyakin committed
898
899
900
901
902
903
904
905
			ensure_root(origin)?;
			Self::update_config_member(|config| {
				sp_std::mem::replace(&mut config.hrmp_max_parachain_outbound_channels, new) != new
			});
			Ok(())
		}

		/// Sets the maximum number of outbound HRMP channels a parathread is allowed to open.
906
907
908
909
		#[pallet::weight((
			T::WeightInfo::set_config_with_u32(),
			DispatchClass::Operational,
		))]
Shawn Tabrizi's avatar
Shawn Tabrizi committed
910
911
912
913
		pub fn set_hrmp_max_parathread_outbound_channels(
			origin: OriginFor<T>,
			new: u32,
		) -> DispatchResult {
Sergey Pepyakin's avatar
Sergey Pepyakin committed
914
915
916
917
918
919
920
921
			ensure_root(origin)?;
			Self::update_config_member(|config| {
				sp_std::mem::replace(&mut config.hrmp_max_parathread_outbound_channels, new) != new
			});
			Ok(())
		}

		/// Sets the maximum number of outbound HRMP messages can be sent by a candidate.
922
923
924
925
		#[pallet::weight((
			T::WeightInfo::set_config_with_u32(),
			DispatchClass::Operational,
		))]
Shawn Tabrizi's avatar
Shawn Tabrizi committed
926
927
928
929
		pub fn set_hrmp_max_message_num_per_candidate(
			origin: OriginFor<T>,
			new: u32,
		) -> DispatchResult {
Sergey Pepyakin's avatar
Sergey Pepyakin committed
930
931
932
933
934
935
			ensure_root(origin)?;
			Self::update_config_member(|config| {
				sp_std::mem::replace(&mut config.hrmp_max_message_num_per_candidate, new) != new
			});
			Ok(())
		}
936
937

		/// Sets the maximum amount of weight any individual upward message may consume.
938
939
940
941
		#[pallet::weight((
			T::WeightInfo::set_config_with_weight(),
			DispatchClass::Operational,
		))]
942
943
944
945
946
947
948
		pub fn set_ump_max_individual_weight(origin: OriginFor<T>, new: Weight) -> DispatchResult {
			ensure_root(origin)?;
			Self::update_config_member(|config| {
				sp_std::mem::replace(&mut config.ump_max_individual_weight, new) != new
			});
			Ok(())
		}
949
	}
950
951
952

	#[pallet::hooks]
	impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
953
954
955
956
		fn on_runtime_upgrade() -> Weight {
			migration::migrate_to_latest::<T>()
		}

957
958
959
960
961
962
963
964
965
		fn integrity_test() {
			assert_eq!(
				&ActiveConfig::<T>::hashed_key(),
				primitives::v1::well_known_keys::ACTIVE_CONFIG,
				"`well_known_keys::ACTIVE_CONFIG` doesn't match key of `ActiveConfig`! Make sure that the name of the\
				 configuration pallet is `Configuration` in the runtime!",
			);
		}
	}
966
967
}

968
impl<T: Config> Pallet<T> {
969
970
971
972
973
974
	/// Called by the initializer to initialize the configuration module.
	pub(crate) fn initializer_initialize(_now: T::BlockNumber) -> Weight {
		0
	}

	/// Called by the initializer to finalize the configuration module.
Shawn Tabrizi's avatar
Shawn Tabrizi committed
975
	pub(crate) fn initializer_finalize() {}
976
977

	/// Called by the initializer to note that a new session has started.
Shawn Tabrizi's avatar
Shawn Tabrizi committed
978
	pub(crate) fn initializer_on_new_session(session_index: &SessionIndex) {
979
		if let Some(pending) = <Self as Store>::PendingConfig::take(session_index) {
980
			<Self as Store>::ActiveConfig::set(pending);
981
982
983
		}
	}

984
985
	/// Return the session index that should be used for any future scheduled changes.
	fn scheduled_session() -> SessionIndex {
986
		shared::Pallet::<T>::scheduled_session()
987
988
	}

989
990
991
992
993
994
995
	/// Forcibly set the active config. This should be used with extreme care, and typically
	/// only when enabling parachains runtime modules for the first time on a chain which has
	/// been running without them.
	pub fn force_set_active_config(config: HostConfiguration<T::BlockNumber>) {
		<Self as Store>::ActiveConfig::set(config);
	}

996
997
998
999
1000
	// NOTE: Explicitly tell rustc not to inline this because otherwise heuristics note the incoming
	// closure making it's attractive to inline. However, in this case, we will end up with lots of
	// duplicated code (making this function to show up in the top of heaviest functions) only for
	// the sake of essentially avoiding an indirect call. Doesn't worth it.
	#[inline(never)]
For faster browsing, not all history is shown. View entire blame