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

Commit

Permalink
Migrate pallet-session to the new pallet macro (#9796)
Browse files Browse the repository at this point in the history
* Migrate pallet-session to the new pallet macro

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* Remove old macros

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* Fix

Signed-off-by: koushiro <koushiro.cqx@gmail.com>
  • Loading branch information
koushiro committed Sep 28, 2021
1 parent aa3ea04 commit 03c2c2f
Show file tree
Hide file tree
Showing 15 changed files with 285 additions and 237 deletions.
3 changes: 2 additions & 1 deletion frame/beefy-mmr/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ use std::vec;

use beefy_primitives::mmr::MmrLeafVersion;
use frame_support::{
construct_runtime, parameter_types, sp_io::TestExternalities, BasicExternalities,
construct_runtime, parameter_types, sp_io::TestExternalities, traits::GenesisBuild,
BasicExternalities,
};
use sp_core::{Hasher, H256};
use sp_runtime::{
Expand Down
3 changes: 2 additions & 1 deletion frame/beefy/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
use std::vec;

use frame_support::{
construct_runtime, parameter_types, sp_io::TestExternalities, BasicExternalities,
construct_runtime, parameter_types, sp_io::TestExternalities, traits::GenesisBuild,
BasicExternalities,
};
use sp_core::H256;
use sp_runtime::{
Expand Down
24 changes: 13 additions & 11 deletions frame/session/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,40 @@ readme = "README.md"
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = [
"derive",
] }
log = { version = "0.4.0", default-features = false }
impl-trait-for-tuples = "0.2.1"

codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] }
scale-info = { version = "1.0", default-features = false, features = ["derive"] }
sp-core = { version = "4.0.0-dev", default-features = false, path = "../../primitives/core" }

sp-std = { version = "4.0.0-dev", default-features = false, path = "../../primitives/std" }
sp-core = { version = "4.0.0-dev", default-features = false, path = "../../primitives/core" }
sp-io = { version = "4.0.0-dev", default-features = false, path = "../../primitives/io" }
sp-runtime = { version = "4.0.0-dev", default-features = false, path = "../../primitives/runtime" }
sp-session = { version = "4.0.0-dev", default-features = false, path = "../../primitives/session" }
sp-staking = { version = "4.0.0-dev", default-features = false, path = "../../primitives/staking" }
sp-trie = { version = "4.0.0-dev", default-features = false, path = "../../primitives/trie", optional = true }

frame-support = { version = "4.0.0-dev", default-features = false, path = "../support" }
frame-system = { version = "4.0.0-dev", default-features = false, path = "../system" }
pallet-timestamp = { version = "4.0.0-dev", default-features = false, path = "../timestamp" }
sp-trie = { version = "4.0.0-dev", optional = true, default-features = false, path = "../../primitives/trie" }
log = { version = "0.4.0", default-features = false }
impl-trait-for-tuples = "0.2.1"

[features]
default = ["std", "historical"]
historical = ["sp-trie"]
std = [
"log/std",
"codec/std",
"scale-info/std",
"sp-std/std",
"sp-io/std",
"frame-support/std",
"sp-core/std",
"sp-io/std",
"sp-runtime/std",
"sp-session/std",
"sp-staking/std",
"pallet-timestamp/std",
"sp-trie/std",
"log/std",
"frame-support/std",
"frame-system/std",
"pallet-timestamp/std",
]
try-runtime = ["frame-support/try-runtime"]
8 changes: 4 additions & 4 deletions frame/session/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Session Module
# Session Pallet

The Session module allows validators to manage their session keys, provides a function for changing
the session length, and handles session rotation.

- [`session::Trait`](https://docs.rs/pallet-session/latest/pallet_session/trait.Config.html)
- [`Call`](https://docs.rs/pallet-session/latest/pallet_session/enum.Call.html)
- [`Module`](https://docs.rs/pallet-session/latest/pallet_session/struct.Module.html)
- [`Pallet`](https://docs.rs/pallet-session/latest/pallet_session/struct.Pallet.html)

## Overview

Expand Down Expand Up @@ -72,11 +72,11 @@ 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()
}
```

## Related Modules
## Related Pallets

- [Staking](https://docs.rs/pallet-staking/latest/pallet_staking/)

Expand Down
28 changes: 13 additions & 15 deletions frame/session/benchmarking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,37 @@ readme = "README.md"
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
sp-session = { version = "4.0.0-dev", default-features = false, path = "../../../primitives/session" }
sp-runtime = { version = "4.0.0-dev", default-features = false, path = "../../../primitives/runtime" }
rand = { version = "0.7.2", default-features = false }

sp-std = { version = "4.0.0-dev", default-features = false, path = "../../../primitives/std" }
frame-system = { version = "4.0.0-dev", default-features = false, path = "../../system" }
sp-runtime = { version = "4.0.0-dev", default-features = false, path = "../../../primitives/runtime" }
sp-session = { version = "4.0.0-dev", default-features = false, path = "../../../primitives/session" }

frame-benchmarking = { version = "4.0.0-dev", default-features = false, path = "../../benchmarking" }
frame-support = { version = "4.0.0-dev", default-features = false, path = "../../support" }
pallet-staking = { version = "4.0.0-dev", default-features = false, features = [
"runtime-benchmarks",
], path = "../../staking" }
frame-system = { version = "4.0.0-dev", default-features = false, path = "../../system" }
pallet-session = { version = "4.0.0-dev", default-features = false, path = "../../session" }
rand = { version = "0.7.2", default-features = false }
pallet-staking = { version = "4.0.0-dev", default-features = false, features = ["runtime-benchmarks"], path = "../../staking" }

[dev-dependencies]
codec = { package = "parity-scale-codec", version = "2.0.0", features = [
"derive",
] }
codec = { package = "parity-scale-codec", version = "2.0.0", features = ["derive"] }
scale-info = "1.0"
sp-core = { version = "4.0.0-dev", path = "../../../primitives/core" }
pallet-staking-reward-curve = { version = "4.0.0-dev", path = "../../staking/reward-curve" }
sp-io = { version = "4.0.0-dev", path = "../../../primitives/io" }
pallet-timestamp = { version = "4.0.0-dev", path = "../../timestamp" }
pallet-balances = { version = "4.0.0-dev", path = "../../balances" }
pallet-timestamp = { version = "4.0.0-dev", path = "../../timestamp" }
pallet-staking-reward-curve = { version = "4.0.0-dev", path = "../../staking/reward-curve" }
frame-election-provider-support = { version = "4.0.0-dev", path = "../../election-provider-support" }

[features]
default = ["std"]
std = [
"sp-std/std",
"sp-session/std",
"sp-runtime/std",
"frame-system/std",
"sp-session/std",
"frame-benchmarking/std",
"frame-support/std",
"pallet-staking/std",
"frame-system/std",
"pallet-session/std",
"pallet-staking/std",
]
6 changes: 3 additions & 3 deletions frame/session/benchmarking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,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::Module as Historical, Pallet as Session, *};
use pallet_staking::{
benchmarking::create_validator_with_nominators, testing_utils::create_validators,
RewardDestination,
Expand All @@ -39,15 +39,15 @@ 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
12 changes: 7 additions & 5 deletions frame/session/src/historical/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
//! These roots and proofs of inclusion can be generated at any time during the current session.
//! Afterwards, the proofs can be fed to a consensus module when reporting misbehavior.

use super::{Module as SessionModule, SessionIndex};
use super::{Pallet as SessionModule, SessionIndex};
use codec::{Decode, Encode};
use frame_support::{
decl_module, decl_storage, print,
Expand Down Expand Up @@ -114,11 +114,11 @@ impl<T: Config> ValidatorSet<T::AccountId> for Module<T> {
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()
}
}

Expand Down Expand Up @@ -366,11 +366,13 @@ pub(crate) mod tests {
use crate::mock::{
force_new_session, set_next_validators, Session, System, Test, NEXT_VALIDATORS,
};

use sp_runtime::{key_types::DUMMY, testing::UintAuthorityId};

use frame_support::{
traits::{KeyOwnerProofSystem, OnInitialize},
traits::{GenesisBuild, KeyOwnerProofSystem, OnInitialize},
BasicExternalities,
};
use sp_runtime::{key_types::DUMMY, testing::UintAuthorityId};

type Historical = Module<Test>;

Expand Down
29 changes: 13 additions & 16 deletions frame/session/src/historical/offchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,11 @@ use sp_runtime::{
KeyTypeId,
};
use sp_session::MembershipProof;

use super::{
super::{Pallet as SessionModule, SessionIndex},
Config, IdentificationTuple, ProvingTrie,
};

use super::shared;
use sp_std::prelude::*;

use super::{shared, Config, IdentificationTuple, ProvingTrie};
use crate::{Pallet as SessionModule, SessionIndex};

/// A set of validators, which was used for a fixed session index.
struct ValidatorSet<T: Config> {
validator_set: Vec<IdentificationTuple<T>>,
Expand Down Expand Up @@ -142,23 +138,24 @@ pub fn keep_newest<T: Config>(n_to_keep: usize) {

#[cfg(test)]
mod tests {
use super::{
super::{onchain, Module},
*,
};
use crate::mock::{
force_new_session, set_next_validators, Session, System, Test, NEXT_VALIDATORS,
use super::*;
use crate::{
historical::{onchain, Module},
mock::{force_new_session, set_next_validators, Session, System, Test, NEXT_VALIDATORS},
};

use codec::Encode;
use frame_support::traits::{KeyOwnerProofSystem, OnInitialize};
use sp_core::{
crypto::key_types::DUMMY,
offchain::{testing::TestOffchainExt, OffchainDbExt, OffchainWorkerExt, StorageKind},
};

use frame_support::BasicExternalities;
use sp_runtime::testing::UintAuthorityId;

use frame_support::{
traits::{GenesisBuild, KeyOwnerProofSystem, OnInitialize},
BasicExternalities,
};

type Historical = Module<Test>;

pub fn new_test_ext() -> sp_io::TestExternalities {
Expand Down
10 changes: 3 additions & 7 deletions frame/session/src/historical/onchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,11 @@

use codec::Encode;
use sp_runtime::traits::Convert;

use super::{
super::{Config as SessionConfig, Pallet as SessionModule, SessionIndex},
Config as HistoricalConfig,
};

use super::shared;
use sp_std::prelude::*;

use super::{shared, Config as HistoricalConfig};
use crate::{Config as SessionConfig, Pallet as SessionModule, SessionIndex};

/// Store the validator-set associated to the `session_index` to the off-chain database.
///
/// Further processing is then done [`off-chain side`](super::offchain).
Expand Down
2 changes: 1 addition & 1 deletion frame/session/src/historical/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
//! Shared logic between on-chain and off-chain components used for slashing using an off-chain
//! worker.

use super::SessionIndex;
use codec::Encode;
use sp_staking::SessionIndex;
use sp_std::prelude::*;

pub(super) const PREFIX: &[u8] = b"session_historical";
Expand Down
Loading

0 comments on commit 03c2c2f

Please sign in to comment.