initializer.rs 14.2 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 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/>.

//! This module is responsible for maintaining a consistent initialization order for all other
//! parachains modules. It's also responsible for finalization and session change notifications.
//!
//! This module can throw fatal errors if session-change notifications are received after initialization.

22
23
use crate::{
	configuration::{self, HostConfiguration},
asynchronous rob's avatar
asynchronous rob committed
24
	disputes::DisputesHandler,
Shawn Tabrizi's avatar
Shawn Tabrizi committed
25
	dmp, hrmp, inclusion, paras, scheduler, session_info, shared, ump,
26
};
27
28
29
30
31
use frame_support::{
	traits::{OneSessionHandler, Randomness},
	weights::Weight,
};
use frame_system::limits::BlockWeights;
Shawn Tabrizi's avatar
Shawn Tabrizi committed
32
33
use parity_scale_codec::{Decode, Encode};
use primitives::v1::{BlockNumber, ConsensusLog, SessionIndex, ValidatorId};
34
use scale_info::TypeInfo;
Shawn Tabrizi's avatar
Shawn Tabrizi committed
35
use sp_std::prelude::*;
asynchronous rob's avatar
asynchronous rob committed
36

37
38
39
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;

40
41
pub use pallet::*;

asynchronous rob's avatar
asynchronous rob committed
42
/// Information about a session change that has just occurred.
43
#[derive(Clone)]
asynchronous rob's avatar
asynchronous rob committed
44
45
46
pub struct SessionChangeNotification<BlockNumber> {
	/// The new validators in the session.
	pub validators: Vec<ValidatorId>,
Denis_P's avatar
Denis_P committed
47
	/// The queued validators for the following session.
asynchronous rob's avatar
asynchronous rob committed
48
49
50
51
52
53
54
	pub queued: Vec<ValidatorId>,
	/// The configuration before handling the session change
	pub prev_config: HostConfiguration<BlockNumber>,
	/// The configuration after handling the session change.
	pub new_config: HostConfiguration<BlockNumber>,
	/// A secure random seed for the session, gathered from BABE.
	pub random_seed: [u8; 32],
asynchronous rob's avatar
asynchronous rob committed
55
	/// New session index.
56
	pub session_index: SessionIndex,
asynchronous rob's avatar
asynchronous rob committed
57
}
58

59
60
61
62
63
64
65
66
67
68
69
70
71
impl<BlockNumber: Default + From<u32>> Default for SessionChangeNotification<BlockNumber> {
	fn default() -> Self {
		Self {
			validators: Vec::new(),
			queued: Vec::new(),
			prev_config: HostConfiguration::default(),
			new_config: HostConfiguration::default(),
			random_seed: Default::default(),
			session_index: Default::default(),
		}
	}
}

72
#[derive(Encode, Decode, TypeInfo)]
73
struct BufferedSessionChange {
74
75
	validators: Vec<ValidatorId>,
	queued: Vec<ValidatorId>,
76
	session_index: SessionIndex,
77
78
}

79
80
81
82
83
84
85
86
87
88
pub trait WeightInfo {
	fn force_approve(d: u32) -> Weight;
}

impl WeightInfo for () {
	fn force_approve(_: u32) -> Weight {
		BlockWeights::default().max_block
	}
}

89
90
#[frame_support::pallet]
pub mod pallet {
Shawn Tabrizi's avatar
Shawn Tabrizi committed
91
	use super::*;
92
93
	use frame_support::pallet_prelude::*;
	use frame_system::pallet_prelude::*;
94

95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
	#[pallet::pallet]
	#[pallet::generate_store(pub(super) trait Store)]
	pub struct Pallet<T>(_);

	#[pallet::config]
	pub trait Config:
		frame_system::Config
		+ configuration::Config
		+ shared::Config
		+ paras::Config
		+ scheduler::Config
		+ inclusion::Config
		+ session_info::Config
		+ dmp::Config
		+ ump::Config
		+ hrmp::Config
	{
		/// A randomness beacon.
		type Randomness: Randomness<Self::Hash, Self::BlockNumber>;
		/// An origin which is allowed to force updates to parachains.
		type ForceOrigin: EnsureOrigin<<Self as frame_system::Config>::Origin>;
116
117
		/// Weight information for extrinsics in this pallet.
		type WeightInfo: WeightInfo;
118
119
	}

120
121
	/// Whether the parachains modules have been initialized within this block.
	///
Denis_P's avatar
Denis_P committed
122
	/// Semantically a `bool`, but this guarantees it should never hit the trie,
123
124
	/// as this is cleared in `on_finalize` and Frame optimizes `None` values to be empty values.
	///
Denis_P's avatar
Denis_P committed
125
	/// As a `bool`, `set(false)` and `remove()` both lead to the next `get()` being false, but one of
126
127
128
129
130
131
132
133
134
135
136
137
138
	/// them writes to the trie and one does not. This confusion makes `Option<()>` more suitable for
	/// the semantics of this variable.
	#[pallet::storage]
	pub(super) type HasInitialized<T: Config> = StorageValue<_, ()>;

	/// Buffered session changes along with the block number at which they should be applied.
	///
	/// Typically this will be empty or one element long. Apart from that this item never hits
	/// the storage.
	///
	/// However this is a `Vec` regardless to handle various edge cases that may occur at runtime
	/// upgrade boundaries or if governance intervenes.
	#[pallet::storage]
Shawn Tabrizi's avatar
Shawn Tabrizi committed
139
140
	pub(super) type BufferedSessionChanges<T: Config> =
		StorageValue<_, Vec<BufferedSessionChange>, ValueQuery>;
141
142
143

	#[pallet::hooks]
	impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
144
145
146
147
148
149
		fn on_initialize(now: T::BlockNumber) -> Weight {
			// The other modules are initialized in this order:
			// - Configuration
			// - Paras
			// - Scheduler
			// - Inclusion
150
			// - SessionInfo
asynchronous rob's avatar
asynchronous rob committed
151
			// - Disputes
152
153
154
			// - DMP
			// - UMP
			// - HRMP
155
			let total_weight = configuration::Pallet::<T>::initializer_initialize(now) +
156
				shared::Pallet::<T>::initializer_initialize(now) +
ferrell-code's avatar
ferrell-code committed
157
				paras::Pallet::<T>::initializer_initialize(now) +
158
				scheduler::Pallet::<T>::initializer_initialize(now) +
159
				inclusion::Pallet::<T>::initializer_initialize(now) +
160
				session_info::Pallet::<T>::initializer_initialize(now) +
asynchronous rob's avatar
asynchronous rob committed
161
				T::DisputesHandler::initializer_initialize(now) +
162
				dmp::Pallet::<T>::initializer_initialize(now) +
163
				ump::Pallet::<T>::initializer_initialize(now) +
164
				hrmp::Pallet::<T>::initializer_initialize(now);
165

166
			HasInitialized::<T>::set(Some(()));
167
168
169
170

			total_weight
		}

171
		fn on_finalize(_: T::BlockNumber) {
asynchronous rob's avatar
asynchronous rob committed
172
			// reverse initialization order.
173
			hrmp::Pallet::<T>::initializer_finalize();
174
			ump::Pallet::<T>::initializer_finalize();
175
			dmp::Pallet::<T>::initializer_finalize();
asynchronous rob's avatar
asynchronous rob committed
176
			T::DisputesHandler::initializer_finalize();
177
			session_info::Pallet::<T>::initializer_finalize();
178
			inclusion::Pallet::<T>::initializer_finalize();
179
			scheduler::Pallet::<T>::initializer_finalize();
ferrell-code's avatar
ferrell-code committed
180
			paras::Pallet::<T>::initializer_finalize();
181
			shared::Pallet::<T>::initializer_finalize();
182
			configuration::Pallet::<T>::initializer_finalize();
183
184
185
186
187

			// Apply buffered session changes as the last thing. This way the runtime APIs and the
			// next block will observe the next session.
			//
			// Note that we only apply the last session as all others lasted less than a block (weirdly).
Shawn Tabrizi's avatar
Shawn Tabrizi committed
188
189
			if let Some(BufferedSessionChange { session_index, validators, queued }) =
				BufferedSessionChanges::<T>::take().pop()
190
191
192
193
			{
				Self::apply_new_session(session_index, validators, queued);
			}

194
			HasInitialized::<T>::take();
195
		}
196
	}
197

198
199
	#[pallet::call]
	impl<T: Config> Pallet<T> {
200
201
202
		/// Issue a signal to the consensus engine to forcibly act as though all parachain
		/// blocks in all relay chain blocks up to and including the given number in the current
		/// chain are valid and should be finalized.
203
204
205
206
207
208
		#[pallet::weight((
			<T as Config>::WeightInfo::force_approve(
				frame_system::Pallet::<T>::digest().logs.len() as u32,
			),
			DispatchClass::Operational,
		))]
209
		pub fn force_approve(origin: OriginFor<T>, up_to: BlockNumber) -> DispatchResult {
210
211
212
			T::ForceOrigin::ensure_origin(origin)?;

			frame_system::Pallet::<T>::deposit_log(ConsensusLog::ForceApprove(up_to).into());
213
			Ok(())
214
		}
215
216
217
	}
}

218
impl<T: Config> Pallet<T> {
219
	fn apply_new_session(
220
		session_index: SessionIndex,
221
		all_validators: Vec<ValidatorId>,
222
223
		queued: Vec<ValidatorId>,
	) {
224
		let prev_config = <configuration::Pallet<T>>::config();
asynchronous rob's avatar
asynchronous rob committed
225
226
227

		let random_seed = {
			let mut buf = [0u8; 32];
228
229
230
			// TODO: audit usage of randomness API
			// https://github.com/paritytech/polkadot/issues/2601
			let (random_hash, _) = T::Randomness::random(&b"paras"[..]);
asynchronous rob's avatar
asynchronous rob committed
231
232
233
234
235
236
237
			let len = sp_std::cmp::min(32, random_hash.as_ref().len());
			buf[..len].copy_from_slice(&random_hash.as_ref()[..len]);
			buf
		};

		// We can't pass the new config into the thing that determines the new config,
		// so we don't pass the `SessionChangeNotification` into this module.
238
		configuration::Pallet::<T>::initializer_on_new_session(&session_index);
asynchronous rob's avatar
asynchronous rob committed
239

240
		let new_config = <configuration::Pallet<T>>::config();
asynchronous rob's avatar
asynchronous rob committed
241

242
		let validators = shared::Pallet::<T>::initializer_on_new_session(
243
244
245
246
247
248
			session_index,
			random_seed.clone(),
			&new_config,
			all_validators,
		);

asynchronous rob's avatar
asynchronous rob committed
249
250
251
252
253
254
		let notification = SessionChangeNotification {
			validators,
			queued,
			prev_config,
			new_config,
			random_seed,
asynchronous rob's avatar
asynchronous rob committed
255
			session_index,
asynchronous rob's avatar
asynchronous rob committed
256
257
		};

ferrell-code's avatar
ferrell-code committed
258
		let outgoing_paras = paras::Pallet::<T>::initializer_on_new_session(&notification);
259
		scheduler::Pallet::<T>::initializer_on_new_session(&notification);
260
		inclusion::Pallet::<T>::initializer_on_new_session(&notification);
261
		session_info::Pallet::<T>::initializer_on_new_session(&notification);
asynchronous rob's avatar
asynchronous rob committed
262
		T::DisputesHandler::initializer_on_new_session(&notification);
263
		dmp::Pallet::<T>::initializer_on_new_session(&notification, &outgoing_paras);
264
		ump::Pallet::<T>::initializer_on_new_session(&notification, &outgoing_paras);
265
		hrmp::Pallet::<T>::initializer_on_new_session(&notification, &outgoing_paras);
266
	}
267
268

	/// Should be called when a new session occurs. Buffers the session notification to be applied
269
	/// at the end of the block. If `queued` is `None`, the `validators` are considered queued.
270
271
	fn on_new_session<'a, I: 'a>(
		_changed: bool,
272
		session_index: SessionIndex,
273
274
		validators: I,
		queued: Option<I>,
Shawn Tabrizi's avatar
Shawn Tabrizi committed
275
276
	) where
		I: Iterator<Item = (&'a T::AccountId, ValidatorId)>,
277
278
279
280
281
282
283
284
	{
		let validators: Vec<_> = validators.map(|(_, v)| v).collect();
		let queued: Vec<_> = if let Some(queued) = queued {
			queued.map(|(_, v)| v).collect()
		} else {
			validators.clone()
		};

285
286
287
288
		if session_index == 0 {
			// Genesis session should be immediately enacted.
			Self::apply_new_session(0, validators, queued);
		} else {
Shawn Tabrizi's avatar
Shawn Tabrizi committed
289
290
291
			BufferedSessionChanges::<T>::mutate(|v| {
				v.push(BufferedSessionChange { validators, queued, session_index })
			});
292
		}
293
	}
asynchronous rob's avatar
asynchronous rob committed
294
295
296
297
298
299
300
301
302

	// Allow to trigger on_new_session in tests, this is needed as long as pallet_session is not
	// implemented in mock.
	#[cfg(test)]
	pub(crate) fn test_trigger_on_new_session<'a, I: 'a>(
		changed: bool,
		session_index: SessionIndex,
		validators: I,
		queued: Option<I>,
Shawn Tabrizi's avatar
Shawn Tabrizi committed
303
304
	) where
		I: Iterator<Item = (&'a T::AccountId, ValidatorId)>,
asynchronous rob's avatar
asynchronous rob committed
305
306
307
	{
		Self::on_new_session(changed, session_index, validators, queued)
	}
308
309
}

310
impl<T: Config> sp_runtime::BoundToRuntimeAppPublic for Pallet<T> {
311
312
313
	type Public = ValidatorId;
}

314
impl<T: pallet_session::Config + Config> OneSessionHandler<T::AccountId> for Pallet<T> {
315
316
	type Key = ValidatorId;

317
	fn on_genesis_session<'a, I: 'a>(validators: I)
Shawn Tabrizi's avatar
Shawn Tabrizi committed
318
319
	where
		I: Iterator<Item = (&'a T::AccountId, Self::Key)>,
320
	{
321
		<Pallet<T>>::on_new_session(false, 0, validators, None);
322
323
324
	}

	fn on_new_session<'a, I: 'a>(changed: bool, validators: I, queued: I)
Shawn Tabrizi's avatar
Shawn Tabrizi committed
325
326
	where
		I: Iterator<Item = (&'a T::AccountId, Self::Key)>,
327
	{
328
329
		let session_index = <pallet_session::Pallet<T>>::current_index();
		<Pallet<T>>::on_new_session(changed, session_index, validators, Some(queued));
330
331
	}

332
	fn on_disabled(_i: u32) {}
333
334
335
336
337
}

#[cfg(test)]
mod tests {
	use super::*;
338
	use crate::mock::{
Shawn Tabrizi's avatar
Shawn Tabrizi committed
339
340
		new_test_ext, Configuration, Dmp, Initializer, MockGenesisConfig, Paras, SessionInfo,
		System,
341
	};
Shawn Tabrizi's avatar
Shawn Tabrizi committed
342
	use primitives::v1::Id as ParaId;
343

344
345
346
347
	use frame_support::{
		assert_ok,
		traits::{OnFinalize, OnInitialize},
	};
348

349
350
351
352
353
354
355
356
357
358
	#[test]
	fn session_0_is_instantly_applied() {
		new_test_ext(Default::default()).execute_with(|| {
			Initializer::on_new_session(
				false,
				0,
				Vec::new().into_iter(),
				Some(Vec::new().into_iter()),
			);

359
			let v = <Initializer as Store>::BufferedSessionChanges::get();
360
361
362
363
364
365
366
			assert!(v.is_empty());

			assert_eq!(SessionInfo::earliest_stored_session(), 0);
			assert!(SessionInfo::session_info(0).is_some());
		});
	}

367
	#[test]
368
369
370
371
372
373
374
375
376
377
378
379
	fn session_change_before_initialize_is_still_buffered_after() {
		new_test_ext(Default::default()).execute_with(|| {
			Initializer::on_new_session(
				false,
				1,
				Vec::new().into_iter(),
				Some(Vec::new().into_iter()),
			);

			let now = System::block_number();
			Initializer::on_initialize(now);

380
			let v = <Initializer as Store>::BufferedSessionChanges::get();
381
382
383
384
385
			assert_eq!(v.len(), 1);
		});
	}

	#[test]
386
	fn session_change_applied_on_finalize() {
387
388
		new_test_ext(Default::default()).execute_with(|| {
			Initializer::on_initialize(1);
asynchronous rob's avatar
asynchronous rob committed
389
390
391
392
393
394
			Initializer::on_new_session(
				false,
				1,
				Vec::new().into_iter(),
				Some(Vec::new().into_iter()),
			);
395

396
			Initializer::on_finalize(1);
397

398
			assert!(<Initializer as Store>::BufferedSessionChanges::get().is_empty());
399
400
401
402
403
404
405
406
		});
	}

	#[test]
	fn sets_flag_on_initialize() {
		new_test_ext(Default::default()).execute_with(|| {
			Initializer::on_initialize(1);

407
			assert!(<Initializer as Store>::HasInitialized::get().is_some());
408
409
410
411
412
413
414
415
416
		})
	}

	#[test]
	fn clears_flag_on_finalize() {
		new_test_ext(Default::default()).execute_with(|| {
			Initializer::on_initialize(1);
			Initializer::on_finalize(1);

417
			assert!(<Initializer as Store>::HasInitialized::get().is_none());
418
419
		})
	}
420
421
422
423
424
425
426
427
428
429
430
431
432

	#[test]
	fn scheduled_cleanup_performed() {
		let a = ParaId::from(1312);
		let b = ParaId::from(228);
		let c = ParaId::from(123);

		let mock_genesis = crate::paras::ParaGenesisArgs {
			parachain: true,
			genesis_head: Default::default(),
			validation_code: Default::default(),
		};

Shawn Tabrizi's avatar
Shawn Tabrizi committed
433
434
435
436
		new_test_ext(MockGenesisConfig {
			configuration: crate::configuration::GenesisConfig {
				config: crate::configuration::HostConfiguration {
					max_downward_message_size: 1024,
437
438
					..Default::default()
				},
Shawn Tabrizi's avatar
Shawn Tabrizi committed
439
440
441
442
443
444
445
			},
			paras: crate::paras::GenesisConfig {
				paras: vec![
					(a, mock_genesis.clone()),
					(b, mock_genesis.clone()),
					(c, mock_genesis.clone()),
				],
446
				..Default::default()
Shawn Tabrizi's avatar
Shawn Tabrizi committed
447
448
449
450
			},
			..Default::default()
		})
		.execute_with(|| {
451
452
453
454
455
			// enqueue downward messages to A, B and C.
			assert_ok!(Dmp::queue_downward_message(&Configuration::config(), a, vec![1, 2, 3]));
			assert_ok!(Dmp::queue_downward_message(&Configuration::config(), b, vec![4, 5, 6]));
			assert_ok!(Dmp::queue_downward_message(&Configuration::config(), c, vec![7, 8, 9]));

456
457
			assert_ok!(Paras::schedule_para_cleanup(a));
			assert_ok!(Paras::schedule_para_cleanup(b));
458

459
460
			// Apply session 2 in the future
			Initializer::apply_new_session(2, vec![], vec![]);
461
462
463
464
465
466

			assert!(Dmp::dmq_contents(a).is_empty());
			assert!(Dmp::dmq_contents(b).is_empty());
			assert!(!Dmp::dmq_contents(c).is_empty());
		});
	}
467
}