Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Move executor params to SessionInfo: primitives and runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
s0me0ne-unkn0wn committed Dec 4, 2022
1 parent 2439ae6 commit 1988355
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 57 deletions.
4 changes: 2 additions & 2 deletions primitives/src/runtime_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ sp_api::decl_runtime_apis! {
#[api_version(3)]
fn disputes() -> Vec<(v2::SessionIndex, v2::CandidateHash, v2::DisputeState<v2::BlockNumber>)>;

/// Returns execution environment parameter set for the session.
/// Get the session info for the given session, if stored.
#[api_version(3)]
fn session_executor_params(session_index: sp_staking::SessionIndex) -> Option<vstaging::ExecutorParams>;
fn session_info_staging(index: sp_staking::SessionIndex) -> Option<vstaging::SessionInfo>;
}
}
125 changes: 125 additions & 0 deletions primitives/src/vstaging/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,128 @@ pub use executor_params::{
ExecInstantiationStrategy, ExecutionEnvironment, ExecutorParam, ExecutorParams,
ExecutorParamsHash,
};

use crate::v2::{
self, AssignmentId, AuthorityDiscoveryId, GroupIndex, IndexedVec, SessionIndex, ValidatorId,
ValidatorIndex,
};
use parity_scale_codec::{Decode, Encode};
#[cfg(feature = "std")]
use parity_util_mem::MallocSizeOf;
use primitives::RuntimeDebug;
use scale_info::TypeInfo;
use sp_std::vec::Vec;

/// Information about validator sets of a session.
#[derive(Clone, Encode, Decode, RuntimeDebug, TypeInfo)]
#[cfg_attr(feature = "std", derive(PartialEq, MallocSizeOf))]
pub struct SessionInfo {
/****** New in vstaging *******/
/// Executor parameter set for the session.
pub executor_params: ExecutorParams,

/****** New in v2 *******/
/// All the validators actively participating in parachain consensus.
/// Indices are into the broader validator set.
pub active_validator_indices: Vec<ValidatorIndex>,
/// A secure random seed for the session, gathered from BABE.
pub random_seed: [u8; 32],
/// The amount of sessions to keep for disputes.
pub dispute_period: SessionIndex,

/****** Old fields ******/
/// Validators in canonical ordering.
///
/// NOTE: There might be more authorities in the current session, than `validators` participating
/// in parachain consensus. See
/// [`max_validators`](https://github.com/paritytech/polkadot/blob/a52dca2be7840b23c19c153cf7e110b1e3e475f8/runtime/parachains/src/configuration.rs#L148).
///
/// `SessionInfo::validators` will be limited to to `max_validators` when set.
pub validators: IndexedVec<ValidatorIndex, ValidatorId>,
/// Validators' authority discovery keys for the session in canonical ordering.
///
/// NOTE: The first `validators.len()` entries will match the corresponding validators in
/// `validators`, afterwards any remaining authorities can be found. This is any authorities not
/// participating in parachain consensus - see
/// [`max_validators`](https://github.com/paritytech/polkadot/blob/a52dca2be7840b23c19c153cf7e110b1e3e475f8/runtime/parachains/src/configuration.rs#L148)
#[cfg_attr(feature = "std", ignore_malloc_size_of = "outside type")]
pub discovery_keys: Vec<AuthorityDiscoveryId>,
/// The assignment keys for validators.
///
/// NOTE: There might be more authorities in the current session, than validators participating
/// in parachain consensus. See
/// [`max_validators`](https://github.com/paritytech/polkadot/blob/a52dca2be7840b23c19c153cf7e110b1e3e475f8/runtime/parachains/src/configuration.rs#L148).
///
/// Therefore:
/// ```ignore
/// assignment_keys.len() == validators.len() && validators.len() <= discovery_keys.len()
/// ```
pub assignment_keys: Vec<AssignmentId>,
/// Validators in shuffled ordering - these are the validator groups as produced
/// by the `Scheduler` module for the session and are typically referred to by
/// `GroupIndex`.
pub validator_groups: IndexedVec<GroupIndex, Vec<ValidatorIndex>>,
/// The number of availability cores used by the protocol during this session.
pub n_cores: u32,
/// The zeroth delay tranche width.
pub zeroth_delay_tranche_width: u32,
/// The number of samples we do of `relay_vrf_modulo`.
pub relay_vrf_modulo_samples: u32,
/// The number of delay tranches in total.
pub n_delay_tranches: u32,
/// How many slots (BABE / SASSAFRAS) must pass before an assignment is considered a
/// no-show.
pub no_show_slots: u32,
/// The number of validators needed to approve a block.
pub needed_approvals: u32,
}

// Structure downgrade for backward compatibility
impl From<SessionInfo> for v2::SessionInfo {
fn from(new: SessionInfo) -> Self {
Self {
active_validator_indices: new.active_validator_indices,
random_seed: new.random_seed,
dispute_period: new.dispute_period,
validators: new.validators,
discovery_keys: new.discovery_keys,
assignment_keys: new.assignment_keys,
validator_groups: new.validator_groups,
n_cores: new.n_cores,
zeroth_delay_tranche_width: new.zeroth_delay_tranche_width,
relay_vrf_modulo_samples: new.relay_vrf_modulo_samples,
n_delay_tranches: new.n_delay_tranches,
no_show_slots: new.no_show_slots,
needed_approvals: new.needed_approvals,
}
}
}

// Structure upgrade for storage translation
impl From<v2::SessionInfo> for SessionInfo {
fn from(old: v2::SessionInfo) -> Self {
Self {
executor_params: ExecutorParams::default(),
active_validator_indices: old.active_validator_indices,
random_seed: old.random_seed,
dispute_period: old.dispute_period,
validators: old.validators,
discovery_keys: old.discovery_keys,
assignment_keys: old.assignment_keys,
validator_groups: old.validator_groups,
n_cores: old.n_cores,
zeroth_delay_tranche_width: old.zeroth_delay_tranche_width,
relay_vrf_modulo_samples: old.relay_vrf_modulo_samples,
n_delay_tranches: old.n_delay_tranches,
no_show_slots: old.no_show_slots,
needed_approvals: old.needed_approvals,
}
}
}

// Old V1 session support
impl From<v2::OldV1SessionInfo> for SessionInfo {
fn from(old: v2::OldV1SessionInfo) -> Self {
v2::SessionInfo::from(old).into()
}
}
10 changes: 5 additions & 5 deletions runtime/parachains/src/runtime_api_impl/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ use crate::{
session_info, shared,
};
use primitives::v2::{
AuthorityDiscoveryId, CandidateEvent, CommittedCandidateReceipt, CoreIndex, CoreOccupied,
self, AuthorityDiscoveryId, CandidateEvent, CommittedCandidateReceipt, CoreIndex, CoreOccupied,
CoreState, GroupIndex, GroupRotationInfo, Hash, Id as ParaId, InboundDownwardMessage,
InboundHrmpMessage, OccupiedCore, OccupiedCoreAssumption, PersistedValidationData,
PvfCheckStatement, ScheduledCore, ScrapedOnChainVotes, SessionIndex, SessionInfo,
ValidationCode, ValidationCodeHash, ValidatorId, ValidatorIndex, ValidatorSignature,
PvfCheckStatement, ScheduledCore, ScrapedOnChainVotes, SessionIndex, ValidationCode,
ValidationCodeHash, ValidatorId, ValidatorIndex, ValidatorSignature,
};
use sp_runtime::traits::One;
use sp_std::{collections::btree_map::BTreeMap, prelude::*};
Expand Down Expand Up @@ -343,8 +343,8 @@ where
}

/// Get the session info for the given session, if stored.
pub fn session_info<T: session_info::Config>(index: SessionIndex) -> Option<SessionInfo> {
<session_info::Pallet<T>>::session_info(index)
pub fn session_info<T: session_info::Config>(index: SessionIndex) -> Option<v2::SessionInfo> {
<session_info::Pallet<T>>::session_info(index).map(|s| s.into())
}

/// Implementation for the `dmq_contents` function of the runtime API.
Expand Down
12 changes: 6 additions & 6 deletions runtime/parachains/src/runtime_api_impl/vstaging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use crate::{disputes, session_info};
use primitives::{
v2::{CandidateHash, DisputeState, SessionIndex},
vstaging::ExecutorParams,
vstaging,
};
use sp_std::prelude::*;

Expand All @@ -29,9 +29,9 @@ pub fn get_session_disputes<T: disputes::Config>(
<disputes::Pallet<T>>::disputes()
}

/// Get session executor parameter set
pub fn session_executor_params<T: session_info::Config>(
session_index: SessionIndex,
) -> Option<ExecutorParams> {
<session_info::Pallet<T>>::session_executor_params(session_index)
/// Get the session info for the given session, if stored.
pub fn session_info_staging<T: session_info::Config>(
index: SessionIndex,
) -> Option<vstaging::SessionInfo> {
<session_info::Pallet<T>>::session_info(index)
}
16 changes: 3 additions & 13 deletions runtime/parachains/src/session_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ use frame_support::{
traits::{OneSessionHandler, ValidatorSet, ValidatorSetWithIdentification},
};
use primitives::{
v2::{AssignmentId, AuthorityDiscoveryId, SessionIndex, SessionInfo},
vstaging::{ExecutionEnvironment, ExecutorParam, ExecutorParams},
v2::{AssignmentId, AuthorityDiscoveryId, SessionIndex},
vstaging::{ExecutionEnvironment, ExecutorParam, ExecutorParams, SessionInfo},
};
use sp_std::vec::Vec;

Expand Down Expand Up @@ -111,12 +111,6 @@ pub mod pallet {
#[pallet::getter(fn account_keys)]
pub(crate) type AccountKeys<T: Config> =
StorageMap<_, Identity, SessionIndex, Vec<AccountId<T>>>;

/// Executor parameter set for a given session index
#[pallet::storage]
#[pallet::getter(fn session_executor_params)]
pub(crate) type SessionExecutorParams<T: Config> =
StorageMap<_, Identity, SessionIndex, ExecutorParams>;
}

/// An abstraction for the authority discovery pallet
Expand Down Expand Up @@ -168,7 +162,6 @@ impl<T: Config> Pallet<T> {
// Idx will be missing for a few sessions after the runtime upgrade.
// But it shouldn'be be a problem.
AccountKeys::<T>::remove(&idx);
SessionExecutorParams::<T>::remove(&idx);
}
// update `EarliestStoredSession` based on `config.dispute_period`
EarliestStoredSession::<T>::set(new_earliest_stored_session);
Expand All @@ -185,6 +178,7 @@ impl<T: Config> Pallet<T> {

// create a new entry in `Sessions` with information about the current session
let new_session_info = SessionInfo {
executor_params: ExecutorParams::from(&EXECUTOR_PARAMS[..]),
validators, // these are from the notification and are thus already correct.
discovery_keys: take_active_subset_and_inactive(&active_set, &discovery_keys),
assignment_keys: take_active_subset(&active_set, &assignment_keys),
Expand All @@ -200,10 +194,6 @@ impl<T: Config> Pallet<T> {
dispute_period,
};
Sessions::<T>::insert(&new_session_index, &new_session_info);
SessionExecutorParams::<T>::insert(
&new_session_index,
ExecutorParams::from(&EXECUTOR_PARAMS[..]),
);
}

/// Called by the initializer to initialize the session info pallet.
Expand Down
38 changes: 22 additions & 16 deletions runtime/parachains/src/session_info/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,40 +23,48 @@ pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(2);

pub mod v2 {
use super::STORAGE_VERSION;
use crate::{session_info, session_info::Pallet, shared};
use crate::{
session_info,
session_info::{Pallet, Store},
};
use frame_support::{
pallet_prelude::Weight,
traits::{OnRuntimeUpgrade, StorageVersion},
};
use frame_system::Config;
use primitives::vstaging::ExecutorParams;
use sp_core::Get;

#[cfg(feature = "try-runtime")]
use primitives::vstaging::ExecutorParams;

#[cfg(feature = "try-runtime")]
use crate::session_info::Vec;

#[cfg(feature = "try-runtime")]
use crate::shared;

/// The log target.
const TARGET: &'static str = "runtime::session_info::migration::v2";

pub struct MigrateToV2<T>(sp_std::marker::PhantomData<T>);
impl<T: Config + session_info::pallet::Config> OnRuntimeUpgrade for MigrateToV2<T> {
fn on_runtime_upgrade() -> Weight {
// Bootstrap session executor params with the default ones if no parameters for the
// current session are in storage. `ExecutorParams::default()` is supposed to generate
// EXACTLY the same set of parameters the previous implementation used in a hard-coded
// form. This supposed to only run once, when upgrading from pre-parametrized executor
// code.
let db_weight = T::DbWeight::get();
let mut weight = db_weight.reads(1);
if StorageVersion::get::<Pallet<T>>() == 1 {
log::info!(target: TARGET, "Upgrading storage v1 -> v2");
let session_index = <shared::Pallet<T>>::session_index();
session_info::pallet::SessionExecutorParams::<T>::insert(
&session_index,
ExecutorParams::default(),
let mut vs = 0;

<Pallet<T> as Store>::Sessions::translate_values(
|old: primitives::v2::SessionInfo| {
vs += 1;
Some(primitives::vstaging::SessionInfo::from(old))
},
);
weight += db_weight.reads_writes(vs, vs);

STORAGE_VERSION.put::<Pallet<T>>();
weight += db_weight.reads(1) + db_weight.writes(2);
weight += db_weight.writes(1);
} else {
log::warn!(target: TARGET, "Can only upgrade from version 1");
}
Expand All @@ -67,8 +75,6 @@ pub mod v2 {
fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
log::info!(target: TARGET, "Performing pre-upgrade checks");
assert_eq!(StorageVersion::get::<Pallet<T>>(), 1);
let session_index = <shared::Pallet<T>>::session_index();
assert!(Pallet::<T>::session_executor_params(session_index).is_none());
Ok(Default::default())
}

Expand All @@ -77,8 +83,8 @@ pub mod v2 {
log::info!(target: TARGET, "Performing post-upgrade checks");
assert_eq!(StorageVersion::get::<Pallet<T>>(), 2);
let session_index = <shared::Pallet<T>>::session_index();
let executor_params = Pallet::<T>::session_executor_params(session_index);
assert_eq!(executor_params, Some(ExecutorParams::default()));
let session_info = Pallet::<T>::session_info(session_index);
assert_eq!(session_info.unwrap().executor_params, ExecutorParams::default());
Ok(())
}
}
Expand Down
14 changes: 7 additions & 7 deletions runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use primitives::{
v2::{
AccountId, AccountIndex, Balance, BlockNumber, CandidateEvent, CandidateHash,
self, AccountId, AccountIndex, Balance, BlockNumber, CandidateEvent, CandidateHash,
CommittedCandidateReceipt, CoreState, DisputeState, GroupRotationInfo, Hash, Id as ParaId,
InboundDownwardMessage, InboundHrmpMessage, Moment, Nonce, OccupiedCoreAssumption,
PersistedValidationData, ScrapedOnChainVotes, SessionInfo, Signature, ValidationCode,
PersistedValidationData, ScrapedOnChainVotes, Signature, ValidationCode,
ValidationCodeHash, ValidatorId, ValidatorIndex,
},
vstaging::ExecutorParams,
vstaging,
};
use runtime_common::{
assigned_slots, auctions, claims, crowdloan, impl_runtime_weights, impls::ToAuthor,
Expand Down Expand Up @@ -1666,12 +1666,12 @@ sp_api::impl_runtime_apis! {
})
}

fn session_executor_params(session_index: SessionIndex) -> Option<ExecutorParams> {
parachains_runtime_api_impl_staging::session_executor_params::<Runtime>(session_index)
fn session_info(index: SessionIndex) -> Option<v2::SessionInfo> {
parachains_runtime_api_impl::session_info::<Runtime>(index)
}

fn session_info(index: SessionIndex) -> Option<SessionInfo> {
parachains_runtime_api_impl::session_info::<Runtime>(index)
fn session_info_staging(index: SessionIndex) -> Option<vstaging::SessionInfo> {
parachains_runtime_api_impl_staging::session_info_staging::<Runtime>(index)
}

fn dmq_contents(recipient: ParaId) -> Vec<InboundDownwardMessage<BlockNumber>> {
Expand Down
16 changes: 8 additions & 8 deletions runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ use pallet_transaction_payment::{CurrencyAdapter, FeeDetails, RuntimeDispatchInf
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use primitives::{
v2::{
AccountId, AccountIndex, Balance, BlockNumber, CandidateEvent, CandidateHash,
self, AccountId, AccountIndex, Balance, BlockNumber, CandidateEvent, CandidateHash,
CommittedCandidateReceipt, CoreState, DisputeState, GroupRotationInfo, Hash, Id as ParaId,
InboundDownwardMessage, InboundHrmpMessage, Moment, Nonce, OccupiedCoreAssumption,
PersistedValidationData, PvfCheckStatement, ScrapedOnChainVotes, SessionInfo, Signature,
ValidationCode, ValidationCodeHash, ValidatorId, ValidatorIndex, ValidatorSignature,
PersistedValidationData, PvfCheckStatement, ScrapedOnChainVotes, Signature, ValidationCode,
ValidationCodeHash, ValidatorId, ValidatorIndex, ValidatorSignature,
},
vstaging::ExecutorParams,
vstaging,
};
use runtime_common::{
assigned_slots, auctions, crowdloan, elections::OnChainAccuracy, impl_runtime_weights,
Expand Down Expand Up @@ -1403,12 +1403,12 @@ sp_api::impl_runtime_apis! {
})
}

fn session_executor_params(session_index: SessionIndex) -> Option<ExecutorParams> {
parachains_runtime_api_impl_staging::session_executor_params::<Runtime>(session_index)
fn session_info(index: SessionIndex) -> Option<v2::SessionInfo> {
parachains_runtime_api_impl::session_info::<Runtime>(index)
}

fn session_info(index: SessionIndex) -> Option<SessionInfo> {
parachains_runtime_api_impl::session_info::<Runtime>(index)
fn session_info_staging(index: SessionIndex) -> Option<vstaging::SessionInfo> {
parachains_runtime_api_impl_staging::session_info_staging::<Runtime>(index)
}

fn dmq_contents(recipient: ParaId) -> Vec<InboundDownwardMessage<BlockNumber>> {
Expand Down

0 comments on commit 1988355

Please sign in to comment.