Skip to content

Commit 664e3bb

Browse files
committed
feat: Draft of pallet_session_keys_registration
1 parent 30f6e5e commit 664e3bb

File tree

8 files changed

+318
-3
lines changed

8 files changed

+318
-3
lines changed

Cargo.lock

Lines changed: 82 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ members = [
5757
"toolkit/governed-map/primitives",
5858
"toolkit/governed-map/pallet",
5959
"toolkit/committee-selection/selection-simulator",
60+
"toolkit/session_keys_registration",
6061
]
6162
resolver = "2"
6263

@@ -198,6 +199,7 @@ pallet-aura = { default-features = false, git = "https://github.com/paritytech/p
198199
pallet-balances = { default-features = false, git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2506" }
199200
pallet-grandpa = { default-features = false, git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2506" }
200201
pallet-session = { default-features = false, git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2506" }
202+
pallet-staking = { default-features = false, git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2506" }
201203
pallet-sudo = { default-features = false, git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2506" }
202204
pallet-timestamp = { default-features = false, git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2506" }
203205
pallet-transaction-payment = { default-features = false, git = "https://github.com/paritytech/polkadot-sdk.git", tag = "polkadot-stable2506" }
@@ -330,5 +332,8 @@ partner-chains-mock-data-sources = { path = "toolkit/data-sources/mock", default
330332
sp-governed-map = { path = "toolkit/governed-map/primitives", default-features = false }
331333
pallet-governed-map = { path = "toolkit/governed-map/pallet", default-features = false }
332334

335+
# Session Keys Registration
336+
pallet-session-keys-registration = { path = "toolkit/session_keys_registration", default-features = false }
337+
333338
# demo node
334339
partner-chains-demo-runtime = { path = "demo/runtime" }

demo/runtime/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ pallet-governed-map = { workspace = true }
9393
sp-governed-map = { workspace = true }
9494
sp-block-producer-fees = { workspace = true }
9595
pallet-block-producer-fees = { workspace = true }
96+
pallet-session-keys-registration = { workspace = true }
9697

9798
[dev-dependencies]
9899
sp-io = { workspace = true }
@@ -139,6 +140,7 @@ std = [
139140
"pallet-transaction-payment/std",
140141
"pallet-address-associations/std",
141142
"pallet-block-producer-metadata/std",
143+
"pallet-session-keys-registration/std",
142144
"sp-block-producer-metadata/std",
143145
"sp-api/std",
144146
"sp-block-builder/std",

demo/runtime/src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,10 @@ impl pallet_governed_map::Config for Runtime {
681681
type BenchmarkHelper = ();
682682
}
683683

684+
impl pallet_session_keys_registration::Config for Runtime {
685+
type PalletsOrigin = OriginCaller;
686+
}
687+
684688
impl crate::test_helper_pallet::Config for Runtime {}
685689

686690
// Create the runtime by composing the FRAME pallets that were previously configured.
@@ -702,7 +706,8 @@ construct_runtime!(
702706
BlockProducerMetadata: pallet_block_producer_metadata,
703707
BlockProductionLog: pallet_block_production_log,
704708
BlockParticipation: pallet_block_participation,
705-
Session: pallet_session,
709+
Session: pallet_session exclude_parts { Call },
710+
SessionKeysRegistration: pallet_session_keys_registration,
706711
// Historical: pallet_session::historical,
707712
NativeTokenManagement: pallet_native_token_management,
708713
GovernedMap: pallet_governed_map,
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
[package]
2+
name = "pallet-session-keys-registration"
3+
authors.workspace = true
4+
edition.workspace = true
5+
homepage.workspace = true
6+
repository.workspace = true
7+
version.workspace = true
8+
license = "Apache-2.0"
9+
description = "Pallet for registering session keys for the partner chains"
10+
11+
[lints]
12+
workspace = true
13+
14+
[dependencies]
15+
frame-support = { workspace = true, default-features = false }
16+
frame-system = { workspace = true, default-features = false }
17+
parity-scale-codec = { workspace = true }
18+
scale-info = { workspace = true, default-features = false, features = [
19+
"derive",
20+
] }
21+
sp-runtime = { workspace = true, default-features = false }
22+
pallet-session = { workspace = true }
23+
24+
[dev-dependencies]
25+
pallet-staking = { workspace = true }
26+
sp-core = { workspace = true }
27+
sp-io = { workspace = true }
28+
sp-staking = { workspace = true }
29+
sp-tracing = { workspace = true }
30+
31+
[features]
32+
default = ["std"]
33+
std = [
34+
"frame-support/std",
35+
"frame-system/std",
36+
"parity-scale-codec/std",
37+
"scale-info/std",
38+
"sp-runtime/std",
39+
"pallet-session/std",
40+
"pallet-staking/std",
41+
]
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#![cfg_attr(not(feature = "std"), no_std)]
2+
3+
#[cfg(test)]
4+
mod mock;
5+
6+
#[cfg(test)]
7+
mod tests;
8+
9+
pub use pallet::*;
10+
11+
#[frame_support::pallet]
12+
pub mod pallet {
13+
use frame_support::{dispatch::DispatchResultWithPostInfo, pallet_prelude::*};
14+
use scale_info::prelude::boxed::Box;
15+
use sp_runtime::Vec;
16+
17+
#[pallet::pallet]
18+
pub struct Pallet<T>(_);
19+
20+
#[pallet::config]
21+
pub trait Config: frame_system::Config + pallet_session::Config {
22+
/// The caller origin, overarching type of all pallets origins.
23+
type PalletsOrigin: Parameter +
24+
Into<<Self as frame_system::Config>::RuntimeOrigin> +
25+
IsType<<<Self as frame_system::Config>::RuntimeOrigin as frame_support::traits::OriginTrait>::PalletsOrigin>;
26+
}
27+
28+
impl<T: Config> Pallet<T> {
29+
/// Invokes the `pallet_session::Call::set_keys` function to set the session keys.
30+
///
31+
/// Allows the caller to set the session keys for the next session for particular user.
32+
///
33+
/// ## Complexity
34+
/// - `O(1)`. Actual cost depends on the number of length of `T::Keys::key_ids()` which is
35+
/// fixed.
36+
pub fn set_keys(
37+
as_origin: Box<T::PalletsOrigin>,
38+
keys: <T as pallet_session::Config>::Keys,
39+
proof: Vec<u8>,
40+
) -> DispatchResultWithPostInfo {
41+
let call = pallet_session::Call::<T>::set_keys { keys, proof };
42+
43+
use frame_support::traits::UnfilteredDispatchable;
44+
call.dispatch_bypass_filter((*as_origin).into())
45+
}
46+
}
47+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
use crate as pallet_session_keys_registration;
2+
3+
use frame_support::derive_impl;
4+
use frame_support::traits::{OnFinalize, OnInitialize};
5+
use sp_core::parameter_types;
6+
use sp_runtime::key_types::DUMMY;
7+
use sp_runtime::testing::UintAuthorityId;
8+
use sp_runtime::{BuildStorage, KeyTypeId};
9+
use sp_staking::SessionIndex;
10+
11+
type Block = frame_system::mocking::MockBlock<Test>;
12+
13+
sp_runtime::impl_opaque_keys! {
14+
pub struct SessionKeys {
15+
pub foo: UintAuthorityId,
16+
}
17+
}
18+
19+
frame_support::construct_runtime!(
20+
pub enum Test
21+
{
22+
System: frame_system,
23+
Session: pallet_session,
24+
SessionKeysRegistration: pallet_session_keys_registration,
25+
}
26+
);
27+
28+
type AccountId = u64;
29+
30+
pub struct TestSessionHandler;
31+
impl pallet_session::SessionHandler<AccountId> for TestSessionHandler {
32+
const KEY_TYPE_IDS: &'static [KeyTypeId] = &[DUMMY];
33+
34+
fn on_genesis_session<Ks: sp_runtime::traits::OpaqueKeys>(_validators: &[(AccountId, Ks)]) {}
35+
36+
fn on_new_session<Ks: sp_runtime::traits::OpaqueKeys>(
37+
_: bool,
38+
_: &[(AccountId, Ks)],
39+
_: &[(AccountId, Ks)],
40+
) {
41+
}
42+
43+
fn on_disabled(_: u32) {}
44+
}
45+
46+
#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
47+
impl frame_system::Config for Test {
48+
type Block = Block;
49+
}
50+
51+
parameter_types! {
52+
pub const Period: u64 = 1;
53+
pub const Offset: u64 = 0;
54+
}
55+
56+
impl pallet_session::Config for Test {
57+
type RuntimeEvent = RuntimeEvent;
58+
type ValidatorId = u64;
59+
type ValidatorIdOf = sp_runtime::traits::ConvertInto;
60+
type ShouldEndSession = pallet_session::PeriodicSessions<Period, Offset>;
61+
type NextSessionRotation = pallet_session::PeriodicSessions<Period, Offset>;
62+
type SessionManager = ();
63+
type SessionHandler = TestSessionHandler;
64+
type Keys = SessionKeys;
65+
type DisablingStrategy = ();
66+
type WeightInfo = ();
67+
}
68+
69+
impl pallet_session_keys_registration::Config for Test {
70+
type PalletsOrigin = OriginCaller;
71+
}
72+
73+
pub fn new_test_ext() -> sp_io::TestExternalities {
74+
sp_tracing::try_init_simple();
75+
let t = frame_system::GenesisConfig::<Test>::default().build_storage().unwrap();
76+
sp_io::TestExternalities::new(t)
77+
}
78+
79+
pub fn start_session(session_index: SessionIndex) {
80+
for i in Session::current_index()..session_index {
81+
System::on_finalize(System::block_number());
82+
Session::on_finalize(System::block_number());
83+
84+
let parent_hash = if System::block_number() > 1 {
85+
let hdr = System::finalize();
86+
hdr.hash()
87+
} else {
88+
System::parent_hash()
89+
};
90+
91+
System::reset_events();
92+
System::initialize(&(i as u64 + 1), &parent_hash, &Default::default());
93+
System::set_block_number((i + 1).into());
94+
95+
System::on_initialize(System::block_number());
96+
Session::on_initialize(System::block_number());
97+
}
98+
99+
assert_eq!(Session::current_index(), session_index);
100+
}

0 commit comments

Comments
 (0)