pub trait SessionManager<ValidatorId> {
    fn new_session(new_index: SessionIndex) -> Option<Vec<ValidatorId>>;
    fn end_session(end_index: SessionIndex);
    fn start_session(start_index: SessionIndex);

    fn new_session_genesis(new_index: SessionIndex) -> Option<Vec<ValidatorId>> { ... }
}
Expand description

A trait for managing creation of new validator set.

Required Methods§

Plan a new session, and optionally provide the new validator set.

Even if the validator-set is the same as before, if any underlying economic conditions have changed (i.e. stake-weights), the new validator set must be returned. This is necessary for consensus engines making use of the session pallet to issue a validator-set change so misbehavior can be provably associated with the new economic conditions as opposed to the old. The returned validator set, if any, will not be applied until new_index. new_index is strictly greater than from previous call.

The first session start at index 0.

new_session(session) is guaranteed to be called before end_session(session-1). In other words, a new session must always be planned before an ongoing one can be finished.

End the session.

Because the session pallet can queue validator set the ending session can be lower than the last new session index.

Start an already planned session.

The session start to be used for validation.

Provided Methods§

Same as new_session, but it this should only be called at genesis.

The session manager might decide to treat this in a different way. Default impl is simply using new_session.

Examples found in repository?
src/lib.rs (line 462)
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
		fn build(&self) {
			if T::SessionHandler::KEY_TYPE_IDS.len() != T::Keys::key_ids().len() {
				panic!("Number of keys in session handler and session keys does not match");
			}

			T::SessionHandler::KEY_TYPE_IDS
				.iter()
				.zip(T::Keys::key_ids())
				.enumerate()
				.for_each(|(i, (sk, kk))| {
					if sk != kk {
						panic!(
							"Session handler and session key expect different key type at index: {}",
							i,
						);
					}
				});

			for (account, val, keys) in self.keys.iter().cloned() {
				<Pallet<T>>::inner_set_keys(&val, keys)
					.expect("genesis config must not contain duplicates; qed");
				if frame_system::Pallet::<T>::inc_consumers_without_limit(&account).is_err() {
					// This will leak a provider reference, however it only happens once (at
					// genesis) so it's really not a big deal and we assume that the user wants to
					// do this since it's the only way a non-endowed account can contain a session
					// key.
					frame_system::Pallet::<T>::inc_providers(&account);
				}
			}

			let initial_validators_0 =
				T::SessionManager::new_session_genesis(0).unwrap_or_else(|| {
					frame_support::print(
						"No initial validator provided by `SessionManager`, use \
						session config keys to generate initial validator set.",
					);
					self.keys.iter().map(|x| x.1.clone()).collect()
				});
			assert!(
				!initial_validators_0.is_empty(),
				"Empty validator set for session 0 in genesis block!"
			);

			let initial_validators_1 = T::SessionManager::new_session_genesis(1)
				.unwrap_or_else(|| initial_validators_0.clone());
			assert!(
				!initial_validators_1.is_empty(),
				"Empty validator set for session 1 in genesis block!"
			);

			let queued_keys: Vec<_> = initial_validators_1
				.iter()
				.cloned()
				.map(|v| {
					(
						v.clone(),
						Pallet::<T>::load_keys(&v).expect("Validator in session 1 missing keys!"),
					)
				})
				.collect();

			// Tell everyone about the genesis session keys
			T::SessionHandler::on_genesis_session::<T::Keys>(&queued_keys);

			Validators::<T>::put(initial_validators_0);
			<QueuedKeys<T>>::put(queued_keys);

			T::SessionManager::start_session(0);
		}

Implementations on Foreign Types§

Implementors§