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

use sp_std::prelude::*;
use frame_support::weights::Weight;
24
use primitives::v1::{ValidatorId, SessionIndex};
25
use frame_support::{
26
	decl_storage, decl_module, decl_error, traits::{OneSessionHandler, Randomness},
27
};
28
use parity_scale_codec::{Encode, Decode};
29
30
use crate::{
	configuration::{self, HostConfiguration},
31
	shared, paras, scheduler, inclusion, session_info, dmp, ump, hrmp,
32
};
asynchronous rob's avatar
asynchronous rob committed
33
34

/// Information about a session change that has just occurred.
35
#[derive(Clone)]
asynchronous rob's avatar
asynchronous rob committed
36
37
38
39
40
41
42
43
44
45
46
pub struct SessionChangeNotification<BlockNumber> {
	/// The new validators in the session.
	pub validators: Vec<ValidatorId>,
	/// The qeueud validators for the following session.
	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
47
	/// New session index.
48
	pub session_index: SessionIndex,
asynchronous rob's avatar
asynchronous rob committed
49
}
50

51
52
53
54
55
56
57
58
59
60
61
62
63
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(),
		}
	}
}

64
#[derive(Encode, Decode)]
65
struct BufferedSessionChange {
66
67
	validators: Vec<ValidatorId>,
	queued: Vec<ValidatorId>,
68
	session_index: SessionIndex,
69
70
}

71
72
73
pub trait Config:
	frame_system::Config
	+ configuration::Config
74
	+ shared::Config
75
76
77
78
79
80
81
	+ paras::Config
	+ scheduler::Config
	+ inclusion::Config
	+ session_info::Config
	+ dmp::Config
	+ ump::Config
	+ hrmp::Config
asynchronous rob's avatar
asynchronous rob committed
82
{
asynchronous rob's avatar
asynchronous rob committed
83
	/// A randomness beacon.
84
	type Randomness: Randomness<Self::Hash, Self::BlockNumber>;
asynchronous rob's avatar
asynchronous rob committed
85
}
86
87

decl_storage! {
88
	trait Store for Module<T: Config> as Initializer {
89
90
91
92
93
94
95
96
97
		/// Whether the parachains modules have been initialized within this block.
		///
		/// Semantically a bool, but this guarantees it should never hit the trie,
		/// as this is cleared in `on_finalize` and Frame optimizes `None` values to be empty values.
		///
		/// As a bool, `set(false)` and `remove()` both lead to the next `get()` being false, but one of
		/// them writes to the trie and one does not. This confusion makes `Option<()>` more suitable for
		/// the semantics of this variable.
		HasInitialized: Option<()>;
98
99
		/// Buffered session changes along with the block number at which they should be applied.
		///
100
101
		/// Typically this will be empty or one element long. Apart from that this item never hits
		/// the storage.
102
103
104
		///
		/// However this is a `Vec` regardless to handle various edge cases that may occur at runtime
		/// upgrade boundaries or if governance intervenes.
105
		BufferedSessionChanges: Vec<BufferedSessionChange>;
106
107
108
109
	}
}

decl_error! {
110
	pub enum Error for Module<T: Config> { }
111
112
113
114
}

decl_module! {
	/// The initializer module.
115
	pub struct Module<T: Config> for enum Call where origin: <T as frame_system::Config>::Origin {
116
117
118
119
120
121
122
123
		type Error = Error<T>;

		fn on_initialize(now: T::BlockNumber) -> Weight {
			// The other modules are initialized in this order:
			// - Configuration
			// - Paras
			// - Scheduler
			// - Inclusion
124
			// - SessionInfo
125
			// - Validity
126
127
128
			// - DMP
			// - UMP
			// - HRMP
129
			let total_weight = configuration::Module::<T>::initializer_initialize(now) +
130
				shared::Module::<T>::initializer_initialize(now) +
asynchronous rob's avatar
asynchronous rob committed
131
				paras::Module::<T>::initializer_initialize(now) +
asynchronous rob's avatar
asynchronous rob committed
132
				scheduler::Module::<T>::initializer_initialize(now) +
133
				inclusion::Module::<T>::initializer_initialize(now) +
134
				session_info::Module::<T>::initializer_initialize(now) +
135
136
137
				dmp::Module::<T>::initializer_initialize(now) +
				ump::Module::<T>::initializer_initialize(now) +
				hrmp::Module::<T>::initializer_initialize(now);
138
139
140
141
142
143
144

			HasInitialized::set(Some(()));

			total_weight
		}

		fn on_finalize() {
asynchronous rob's avatar
asynchronous rob committed
145
			// reverse initialization order.
146
147
148
			hrmp::Module::<T>::initializer_finalize();
			ump::Module::<T>::initializer_finalize();
			dmp::Module::<T>::initializer_finalize();
149
			session_info::Module::<T>::initializer_finalize();
asynchronous rob's avatar
asynchronous rob committed
150
			inclusion::Module::<T>::initializer_finalize();
asynchronous rob's avatar
asynchronous rob committed
151
			scheduler::Module::<T>::initializer_finalize();
152
			paras::Module::<T>::initializer_finalize();
153
			shared::Module::<T>::initializer_finalize();
154
			configuration::Module::<T>::initializer_finalize();
155
156
157
158
159
160
161
162
163
164
165
166
167
168

			// 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).
			if let Some(BufferedSessionChange {
				session_index,
				validators,
				queued,
			}) = BufferedSessionChanges::take().pop()
			{
				Self::apply_new_session(session_index, validators, queued);
			}

169
170
171
172
173
			HasInitialized::take();
		}
	}
}

174
impl<T: Config> Module<T> {
175
	fn apply_new_session(
176
		session_index: SessionIndex,
177
		all_validators: Vec<ValidatorId>,
178
179
		queued: Vec<ValidatorId>,
	) {
asynchronous rob's avatar
asynchronous rob committed
180
181
182
183
		let prev_config = <configuration::Module<T>>::config();

		let random_seed = {
			let mut buf = [0u8; 32];
184
185
186
			// 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
187
188
189
190
191
192
193
			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.
194
		configuration::Module::<T>::initializer_on_new_session(&session_index);
asynchronous rob's avatar
asynchronous rob committed
195
196
197

		let new_config = <configuration::Module<T>>::config();

198
199
200
201
202
203
204
		let validators = shared::Module::<T>::initializer_on_new_session(
			session_index,
			random_seed.clone(),
			&new_config,
			all_validators,
		);

asynchronous rob's avatar
asynchronous rob committed
205
206
207
208
209
210
		let notification = SessionChangeNotification {
			validators,
			queued,
			prev_config,
			new_config,
			random_seed,
asynchronous rob's avatar
asynchronous rob committed
211
			session_index,
asynchronous rob's avatar
asynchronous rob committed
212
213
		};

214
		let outgoing_paras = paras::Module::<T>::initializer_on_new_session(&notification);
asynchronous rob's avatar
asynchronous rob committed
215
		scheduler::Module::<T>::initializer_on_new_session(&notification);
asynchronous rob's avatar
asynchronous rob committed
216
		inclusion::Module::<T>::initializer_on_new_session(&notification);
217
		session_info::Module::<T>::initializer_on_new_session(&notification);
218
219
220
		dmp::Module::<T>::initializer_on_new_session(&notification, &outgoing_paras);
		ump::Module::<T>::initializer_on_new_session(&notification, &outgoing_paras);
		hrmp::Module::<T>::initializer_on_new_session(&notification, &outgoing_paras);
221
	}
222
223

	/// Should be called when a new session occurs. Buffers the session notification to be applied
224
	/// at the end of the block. If `queued` is `None`, the `validators` are considered queued.
225
226
	fn on_new_session<'a, I: 'a>(
		_changed: bool,
227
		session_index: SessionIndex,
228
229
230
231
232
233
234
235
236
237
238
239
		validators: I,
		queued: Option<I>,
	)
		where I: Iterator<Item=(&'a T::AccountId, ValidatorId)>
	{
		let validators: Vec<_> = validators.map(|(_, v)| v).collect();
		let queued: Vec<_> = if let Some(queued) = queued {
			queued.map(|(_, v)| v).collect()
		} else {
			validators.clone()
		};

240
241
242
243
244
245
246
247
248
249
250
		if session_index == 0 {
			// Genesis session should be immediately enacted.
			Self::apply_new_session(0, validators, queued);
		} else {
			BufferedSessionChanges::mutate(|v| v.push(BufferedSessionChange {
				validators,
				queued,
				session_index,
			}));
		}

251
	}
252
253
}

254
impl<T: Config> sp_runtime::BoundToRuntimeAppPublic for Module<T> {
255
256
257
	type Public = ValidatorId;
}

258
impl<T: pallet_session::Config + Config> OneSessionHandler<T::AccountId> for Module<T> {
259
260
	type Key = ValidatorId;

261
	fn on_genesis_session<'a, I: 'a>(validators: I)
262
263
		where I: Iterator<Item=(&'a T::AccountId, Self::Key)>
	{
264
		<Module<T>>::on_new_session(false, 0, validators, None);
265
266
267
268
269
	}

	fn on_new_session<'a, I: 'a>(changed: bool, validators: I, queued: I)
		where I: Iterator<Item=(&'a T::AccountId, Self::Key)>
	{
270
		let session_index = <pallet_session::Module<T>>::current_index();
asynchronous rob's avatar
asynchronous rob committed
271
		<Module<T>>::on_new_session(changed, session_index, validators, Some(queued));
272
273
274
275
276
277
278
279
	}

	fn on_disabled(_i: usize) { }
}

#[cfg(test)]
mod tests {
	use super::*;
280
281
282
	use primitives::v1::{Id as ParaId};
	use crate::mock::{
		new_test_ext,
283
		Initializer, System, Dmp, Paras, Configuration, SessionInfo, MockGenesisConfig,
284
	};
285

286
287
288
289
	use frame_support::{
		assert_ok,
		traits::{OnFinalize, OnInitialize},
	};
290

291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
	#[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()),
			);

			let v = <BufferedSessionChanges>::get();
			assert!(v.is_empty());

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

309
	#[test]
310
311
312
313
314
315
316
317
318
319
320
321
	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);

322
			let v = <BufferedSessionChanges>::get();
323
324
325
326
327
			assert_eq!(v.len(), 1);
		});
	}

	#[test]
328
	fn session_change_applied_on_finalize() {
329
330
		new_test_ext(Default::default()).execute_with(|| {
			Initializer::on_initialize(1);
asynchronous rob's avatar
asynchronous rob committed
331
332
333
334
335
336
			Initializer::on_new_session(
				false,
				1,
				Vec::new().into_iter(),
				Some(Vec::new().into_iter()),
			);
337

338
			Initializer::on_finalize(1);
339

340
			assert!(<BufferedSessionChanges>::get().is_empty());
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
		});
	}

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

			assert!(HasInitialized::get().is_some());
		})
	}

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

			assert!(HasInitialized::get().is_none());
		})
	}
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399

	#[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(),
		};

		new_test_ext(
			MockGenesisConfig {
				configuration: crate::configuration::GenesisConfig {
					config: crate::configuration::HostConfiguration {
						max_downward_message_size: 1024,
						..Default::default()
					},
				},
				paras: crate::paras::GenesisConfig {
					paras: vec![
						(a, mock_genesis.clone()),
						(b, mock_genesis.clone()),
						(c, mock_genesis.clone()),
					],
					..Default::default()
				},
				..Default::default()
			}
		).execute_with(|| {

			// 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]));

400
401
			assert_ok!(Paras::schedule_para_cleanup(a));
			assert_ok!(Paras::schedule_para_cleanup(b));
402

403
404
			// Apply session 2 in the future
			Initializer::apply_new_session(2, vec![], vec![]);
405
406
407
408
409
410

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