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

Migrate pallet-session and session-historical to pallet attribute macro #8815

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion frame/babe/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use sp_runtime::{
use frame_system::InitKind;
use frame_support::{
parameter_types,
traits::{KeyOwnerProofSystem, OnInitialize},
traits::{KeyOwnerProofSystem, OnInitialize, GenesisBuild},
};
use sp_io;
use sp_core::{H256, U256, crypto::{IsWrappedBy, KeyTypeId, Pair}};
Expand Down
2 changes: 1 addition & 1 deletion frame/grandpa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ impl<T: Config> OneSessionHandler<T::AccountId> for Module<T>

// if we didn't issue a change, we update the mapping to note that the current
// set corresponds to the latest equivalent session (i.e. now).
let session_index = <pallet_session::Module<T>>::current_index();
let session_index = <pallet_session::Pallet<T>>::current_index();
SetIdSession::insert(current_set_id, &session_index);
}

Expand Down
2 changes: 1 addition & 1 deletion frame/grandpa/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use ::grandpa as finality_grandpa;
use codec::Encode;
use frame_support::{
parameter_types,
traits::{KeyOwnerProofSystem, OnFinalize, OnInitialize},
traits::{KeyOwnerProofSystem, OnFinalize, OnInitialize, GenesisBuild},
};
use pallet_staking::EraIndex;
use sp_core::{crypto::KeyTypeId, H256};
Expand Down
2 changes: 1 addition & 1 deletion frame/session/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ The [Staking pallet](https://docs.rs/pallet-staking/latest/pallet_staking/) uses
use pallet_session as session;

fn validators<T: pallet_session::Config>() -> Vec<<T as pallet_session::Config>::ValidatorId> {
<pallet_session::Module<T>>::validators()
<pallet_session::Pallet<T>>::validators()
}
```

Expand Down
6 changes: 3 additions & 3 deletions frame/session/benchmarking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use frame_support::{
traits::{KeyOwnerProofSystem, OnInitialize},
};
use frame_system::RawOrigin;
use pallet_session::{historical::Module as Historical, Module as Session, *};
use pallet_session::{historical::Pallet as Historical, Pallet as Session, *};
use pallet_staking::{
benchmarking::create_validator_with_nominators, testing_utils::create_validators,
RewardDestination,
Expand All @@ -41,12 +41,12 @@ use sp_runtime::traits::{One, StaticLookup};

const MAX_VALIDATORS: u32 = 1000;

pub struct Pallet<T: Config>(pallet_session::Module<T>);
pub struct Pallet<T: Config>(pallet_session::Pallet<T>);
pub trait Config: pallet_session::Config + pallet_session::historical::Config + pallet_staking::Config {}

impl<T: Config> OnInitialize<T::BlockNumber> for Pallet<T> {
fn on_initialize(n: T::BlockNumber) -> frame_support::weights::Weight {
pallet_session::Module::<T>::on_initialize(n)
pallet_session::Pallet::<T>::on_initialize(n)
}
}

Expand Down
115 changes: 66 additions & 49 deletions frame/session/src/historical/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,55 +32,71 @@ use sp_runtime::KeyTypeId;
use sp_runtime::traits::{Convert, OpaqueKeys};
use sp_session::{MembershipProof, ValidatorCount};
use frame_support::{
decl_module, decl_storage, Parameter, print,
Parameter, print,
traits::{ValidatorSet, ValidatorSetWithIdentification},
};
use sp_trie::{MemoryDB, Trie, TrieMut, Recorder, EMPTY_PREFIX};
use sp_trie::trie_types::{TrieDBMut, TrieDB};
use super::{SessionIndex, Module as SessionModule};
use super::{SessionIndex, Pallet as SessionModule, pallet as pallet_session};
pub use pallet::*;

mod shared;
pub mod offchain;
pub mod onchain;

/// Config necessary for the historical module.
pub trait Config: super::Config {
/// Full identification of the validator.
type FullIdentification: Parameter;

/// A conversion from validator ID to full identification.
///
/// This should contain any references to economic actors associated with the
/// validator, since they may be outdated by the time this is queried from a
/// historical trie.
///
/// It must return the identification for the current session index.
type FullIdentificationOf: Convert<Self::ValidatorId, Option<Self::FullIdentification>>;
}

decl_storage! {
trait Store for Module<T: Config> as Session {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change. The historical pallet will no longer use the Session storage.

This needs a storage migration.

/// Mapping from historical session indices to session-data root hash and validator count.
HistoricalSessions get(fn historical_root):
map hasher(twox_64_concat) SessionIndex => Option<(T::Hash, ValidatorCount)>;
/// The range of historical sessions we store. [first, last)
StoredRange: Option<(SessionIndex, SessionIndex)>;
/// Deprecated.
CachedObsolete:
map hasher(twox_64_concat) SessionIndex
=> Option<Vec<(T::ValidatorId, T::FullIdentification)>>;
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;

#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(_);

/// Config necessary for the historical module.
#[pallet::config]
pub trait Config: pallet_session::Config + frame_system::Config {
/// Full identification of the validator.
type FullIdentification: Parameter;

/// A conversion from validator ID to full identification.
///
/// This should contain any references to economic actors associated with the
/// validator, since they may be outdated by the time this is queried from a
/// historical trie.
///
/// It must return the identification for the current session index.
type FullIdentificationOf: Convert<Self::ValidatorId, Option<Self::FullIdentification>>;
}
}

decl_module! {
pub struct Module<T: Config> for enum Call where origin: T::Origin {}
/// Mapping from historical session indices to session-data root hash and validator count.
#[pallet::storage]
#[pallet::getter(fn historical_root)]
pub type HistoricalSessions<T: Config> =
StorageMap<_, Twox64Concat, SessionIndex, (T::Hash, ValidatorCount)>;

/// The range of historical sessions we store. [first, last)
#[pallet::storage]
pub type StoredRange<T: Config> = StorageValue<_, (SessionIndex, SessionIndex)>;

#[pallet::storage]
pub type CachedObsolete<T: Config> =
StorageMap<_, Twox64Concat, SessionIndex, (T::ValidatorId, T::FullIdentification)>;

#[pallet::hooks]
/// Deprecated.
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}

#[pallet::call]
impl<T: Config> Pallet<T> {}
}

impl<T: Config> Module<T> {
impl<T: Config> Pallet<T> {
/// Prune historical stored session roots up to (but not including)
/// `up_to`.
pub fn prune_up_to(up_to: SessionIndex) {
<Self as Store>::StoredRange::mutate(|range| {
<<Self as Store>::StoredRange>::mutate(|range| {
let (start, end) = match *range {
Some(range) => range,
None => return, // nothing to prune.
Expand All @@ -104,20 +120,20 @@ impl<T: Config> Module<T> {
}
}

impl<T: Config> ValidatorSet<T::AccountId> for Module<T> {
impl<T: Config> ValidatorSet<T::AccountId> for Pallet<T> {
type ValidatorId = T::ValidatorId;
type ValidatorIdOf = T::ValidatorIdOf;

fn session_index() -> sp_staking::SessionIndex {
super::Module::<T>::current_index()
super::Pallet::<T>::current_index()
}

fn validators() -> Vec<Self::ValidatorId> {
super::Module::<T>::validators()
super::Pallet::<T>::validators()
}
}

impl<T: Config> ValidatorSetWithIdentification<T::AccountId> for Module<T> {
impl<T: Config> ValidatorSetWithIdentification<T::AccountId> for Pallet<T> {
type Identification = T::FullIdentification;
type IdentificationOf = T::FullIdentificationOf;
}
Expand All @@ -141,7 +157,7 @@ impl<T: Config, I> crate::SessionManager<T::ValidatorId> for NoteHistoricalRoot<
{
fn new_session(new_index: SessionIndex) -> Option<Vec<T::ValidatorId>> {

StoredRange::mutate(|range| {
StoredRange::<T>::mutate(|range| {
range.get_or_insert_with(|| (new_index, new_index)).1 = new_index + 1;
});

Expand Down Expand Up @@ -180,7 +196,8 @@ impl<T: Config, I> crate::SessionManager<T::ValidatorId> for NoteHistoricalRoot<
}

/// A tuple of the validator's ID and their full identification.
pub type IdentificationTuple<T> = (<T as crate::Config>::ValidatorId, <T as Config>::FullIdentification);
pub type IdentificationTuple<T> = (<T as crate::Config>::ValidatorId,
<T as Config>::FullIdentification);

/// A trie instance for checking and generating proofs.
pub struct ProvingTrie<T: Config> {
Expand Down Expand Up @@ -281,7 +298,7 @@ impl<T: Config> ProvingTrie<T> {
}

impl<T: Config, D: AsRef<[u8]>> frame_support::traits::KeyOwnerProofSystem<(KeyTypeId, D)>
for Module<T>
for Pallet<T>
{
type Proof = MembershipProof;
type IdentificationTuple = IdentificationTuple<T>;
Expand Down Expand Up @@ -346,10 +363,10 @@ pub(crate) mod tests {
NEXT_VALIDATORS, force_new_session,
set_next_validators, Test, System, Session,
};
use frame_support::traits::{KeyOwnerProofSystem, OnInitialize};
use frame_support::traits::{KeyOwnerProofSystem, OnInitialize, GenesisBuild};
use frame_support::BasicExternalities;

type Historical = Module<Test>;
type Historical = Pallet<Test>;

pub(crate) fn new_test_ext() -> sp_io::TestExternalities {
let mut t = frame_system::GenesisConfig::default().build_storage::<Test>().unwrap();
Expand Down Expand Up @@ -412,27 +429,27 @@ pub(crate) mod tests {

}

assert_eq!(StoredRange::get(), Some((0, 100)));
assert_eq!(StoredRange::<Test>::get(), Some((0, 100)));

for i in 0..100 {
assert!(Historical::historical_root(i).is_some())
}

Historical::prune_up_to(10);
assert_eq!(StoredRange::get(), Some((10, 100)));
assert_eq!(StoredRange::<Test>::get(), Some((10, 100)));

Historical::prune_up_to(9);
assert_eq!(StoredRange::get(), Some((10, 100)));
assert_eq!(StoredRange::<Test>::get(), Some((10, 100)));

for i in 10..100 {
assert!(Historical::historical_root(i).is_some())
}

Historical::prune_up_to(99);
assert_eq!(StoredRange::get(), Some((99, 100)));
assert_eq!(StoredRange::<Test>::get(), Some((99, 100)));

Historical::prune_up_to(100);
assert_eq!(StoredRange::get(), None);
assert_eq!(StoredRange::<Test>::get(), None);

for i in 99..199u64 {
set_next_validators(vec![i]);
Expand All @@ -443,14 +460,14 @@ pub(crate) mod tests {

}

assert_eq!(StoredRange::get(), Some((100, 200)));
assert_eq!(StoredRange::<Test>::get(), Some((100, 200)));

for i in 100..200 {
assert!(Historical::historical_root(i).is_some())
}

Historical::prune_up_to(9999);
assert_eq!(StoredRange::get(), None);
assert_eq!(StoredRange::<Test>::get(), None);

for i in 100..200 {
assert!(Historical::historical_root(i).is_none())
Expand Down
6 changes: 3 additions & 3 deletions frame/session/src/historical/offchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,13 @@ pub fn keep_newest<T: Config>(n_to_keep: usize) {

#[cfg(test)]
mod tests {
use super::super::{onchain, Module};
use super::super::{onchain, Pallet};
use super::*;
use crate::mock::{
force_new_session, set_next_validators, Session, System, Test, NEXT_VALIDATORS,
};
use codec::Encode;
use frame_support::traits::{KeyOwnerProofSystem, OnInitialize};
use frame_support::traits::{KeyOwnerProofSystem, OnInitialize, GenesisBuild};
use sp_core::crypto::key_types::DUMMY;
use sp_core::offchain::{
testing::TestOffchainExt,
Expand All @@ -155,7 +155,7 @@ mod tests {
use sp_runtime::testing::UintAuthorityId;
use frame_support::BasicExternalities;

type Historical = Module<Test>;
type Historical = Pallet<Test>;

pub fn new_test_ext() -> sp_io::TestExternalities {
let mut t = frame_system::GenesisConfig::default()
Expand Down
Loading