Skip to content
This repository has been 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 6 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/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
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
114 changes: 65 additions & 49 deletions frame/session/src/historical/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,55 +32,70 @@ 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]
stanly-johnson marked this conversation as resolved.
Show resolved Hide resolved
pub type CachedObsolete<T: Config> =
StorageMap<_, Twox64Concat, SessionIndex, (T::ValidatorId, T::FullIdentification)>;

#[pallet::hooks]
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 +119,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 +156,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 +195,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 +297,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 @@ -347,9 +363,9 @@ pub(crate) mod tests {
set_next_validators, Test, System, Session,
};
use frame_support::traits::{KeyOwnerProofSystem, OnInitialize};
use frame_support::BasicExternalities;
use frame_support::{BasicExternalities, traits::GenesisBuild};
stanly-johnson marked this conversation as resolved.
Show resolved Hide resolved

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 +428,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 +459,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,7 +137,7 @@ 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,
Expand All @@ -153,9 +153,9 @@ mod tests {
};

use sp_runtime::testing::UintAuthorityId;
use frame_support::BasicExternalities;
use frame_support::{BasicExternalities, traits::GenesisBuild};
stanly-johnson marked this conversation as resolved.
Show resolved Hide resolved

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