runtime/session_info: keep track of stash keys (#5473)

This commit is contained in:
Andronik
2022-05-12 11:36:57 +02:00
committed by GitHub
parent eb2448e1be
commit 15e7536a4e
7 changed files with 91 additions and 8 deletions
+3 -1
View File
@@ -1256,7 +1256,9 @@ impl parachains_configuration::Config for Runtime {
impl parachains_shared::Config for Runtime {}
impl parachains_session_info::Config for Runtime {}
impl parachains_session_info::Config for Runtime {
type ValidatorSet = Historical;
}
impl parachains_inclusion::Config for Runtime {
type Event = Event;
+36 -2
View File
@@ -25,7 +25,7 @@ use crate::{
use frame_support::{
parameter_types,
traits::{GenesisBuild, KeyOwnerProofSystem},
traits::{GenesisBuild, KeyOwnerProofSystem, ValidatorSet, ValidatorSetWithIdentification},
weights::Weight,
};
use frame_support_test::TestRandomness;
@@ -301,7 +301,41 @@ impl crate::paras_inherent::Config for Test {
type WeightInfo = crate::paras_inherent::TestWeightInfo;
}
impl crate::session_info::Config for Test {}
pub struct MockValidatorSet;
impl ValidatorSet<AccountId> for MockValidatorSet {
type ValidatorId = AccountId;
type ValidatorIdOf = ValidatorIdOf;
fn session_index() -> SessionIndex {
0
}
fn validators() -> Vec<Self::ValidatorId> {
Vec::new()
}
}
impl ValidatorSetWithIdentification<AccountId> for MockValidatorSet {
type Identification = ();
type IdentificationOf = FoolIdentificationOf;
}
pub struct FoolIdentificationOf;
impl sp_runtime::traits::Convert<AccountId, Option<()>> for FoolIdentificationOf {
fn convert(_: AccountId) -> Option<()> {
Some(())
}
}
pub struct ValidatorIdOf;
impl sp_runtime::traits::Convert<AccountId, Option<AccountId>> for ValidatorIdOf {
fn convert(a: AccountId) -> Option<AccountId> {
Some(a)
}
}
impl crate::session_info::Config for Test {
type ValidatorSet = MockValidatorSet;
}
thread_local! {
pub static DISCOVERY_AUTHORITIES: RefCell<Vec<AuthorityDiscoveryId>> = RefCell::new(Vec::new());
@@ -23,7 +23,10 @@ use crate::{
configuration, paras, scheduler, shared,
util::{take_active_subset, take_active_subset_and_inactive},
};
use frame_support::{pallet_prelude::*, traits::OneSessionHandler};
use frame_support::{
pallet_prelude::*,
traits::{OneSessionHandler, ValidatorSet, ValidatorSetWithIdentification},
};
use primitives::v2::{AssignmentId, AuthorityDiscoveryId, SessionIndex, SessionInfo};
use sp_std::vec::Vec;
@@ -34,6 +37,20 @@ pub mod migration;
#[cfg(test)]
mod tests;
/// A type for representing the validator account id in a session.
pub type AccountId<T> = <<T as Config>::ValidatorSet as ValidatorSet<
<T as frame_system::Config>::AccountId,
>>::ValidatorId;
/// A tuple of `(AccountId, Identification)` where `Identification`
/// is the full identification of `AccountId`.
pub type IdentificationTuple<T> = (
AccountId<T>,
<<T as Config>::ValidatorSet as ValidatorSetWithIdentification<
<T as frame_system::Config>::AccountId,
>>::Identification,
);
#[frame_support::pallet]
pub mod pallet {
use super::*;
@@ -53,6 +70,10 @@ pub mod pallet {
+ scheduler::Config
+ AuthorityDiscoveryConfig
{
/// A type for retrieving `AccountId`s of the validators in the current session.
/// These are stash keys of the validators.
/// It's used for rewards and slashing. `Identification` is only needed for slashing.
type ValidatorSet: ValidatorSetWithIdentification<Self::AccountId>;
}
/// Assignment keys for the current session.
@@ -73,6 +94,14 @@ pub mod pallet {
#[pallet::storage]
#[pallet::getter(fn session_info)]
pub(crate) type Sessions<T: Config> = StorageMap<_, Identity, SessionIndex, SessionInfo>;
/// The validator account keys of the validators actively participating in parachain consensus.
// We do not store this in `SessionInfo` to avoid leaking the `AccountId` type to the client,
// which would complicate the migration process if we are to change it in the future.
#[pallet::storage]
#[pallet::getter(fn account_keys)]
pub(crate) type AccountKeys<T: Config> =
StorageMap<_, Identity, SessionIndex, Vec<AccountId<T>>>;
}
/// An abstraction for the authority discovery pallet
@@ -121,6 +150,9 @@ impl<T: Config> Pallet<T> {
if old_earliest_stored_session != 0 || Sessions::<T>::get(0).is_some() {
for idx in old_earliest_stored_session..new_earliest_stored_session {
Sessions::<T>::remove(&idx);
// Idx will be missing for a few sessions after the runtime upgrade.
// But it shouldn'be be a problem.
AccountKeys::<T>::remove(&idx);
}
// update `EarliestStoredSession` based on `config.dispute_period`
EarliestStoredSession::<T>::set(new_earliest_stored_session);
@@ -128,6 +160,13 @@ impl<T: Config> Pallet<T> {
// just introduced on a live chain
EarliestStoredSession::<T>::set(new_session_index);
}
// The validator set is guaranteed to be of the current session
// because we delay `on_new_session` till the end of the block.
let account_ids = T::ValidatorSet::validators();
let active_account_ids = take_active_subset(&active_set, &account_ids);
AccountKeys::<T>::insert(&new_session_index, &active_account_ids);
// create a new entry in `Sessions` with information about the current session
let new_session_info = SessionInfo {
validators, // these are from the notification and are thus already correct.
+3 -1
View File
@@ -1238,7 +1238,9 @@ impl parachains_configuration::Config for Runtime {
impl parachains_shared::Config for Runtime {}
impl parachains_session_info::Config for Runtime {}
impl parachains_session_info::Config for Runtime {
type ValidatorSet = Historical;
}
impl parachains_inclusion::Config for Runtime {
type Event = Event;
+3 -1
View File
@@ -603,7 +603,9 @@ impl parachains_paras::Config for Runtime {
type NextSessionRotation = Babe;
}
impl parachains_session_info::Config for Runtime {}
impl parachains_session_info::Config for Runtime {
type ValidatorSet = Historical;
}
parameter_types! {
pub const FirstMessageFactorPercent: u64 = 100;
+3 -1
View File
@@ -499,7 +499,9 @@ impl parachains_initializer::Config for Runtime {
type WeightInfo = ();
}
impl parachains_session_info::Config for Runtime {}
impl parachains_session_info::Config for Runtime {
type ValidatorSet = Historical;
}
parameter_types! {
pub const ParasUnsignedPriority: TransactionPriority = TransactionPriority::max_value();
+3 -1
View File
@@ -852,7 +852,9 @@ impl parachains_configuration::Config for Runtime {
impl parachains_shared::Config for Runtime {}
impl parachains_session_info::Config for Runtime {}
impl parachains_session_info::Config for Runtime {
type ValidatorSet = Historical;
}
impl parachains_inclusion::Config for Runtime {
type Event = Event;