Skip to content

Commit c612b47

Browse files
committed
Pallet session wrapper draft
1 parent 9d45e9a commit c612b47

File tree

7 files changed

+95
-29
lines changed

7 files changed

+95
-29
lines changed

demo/node/src/staging.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,22 @@ pub fn staging_genesis(
130130
},
131131
transaction_payment: Default::default(),
132132
session: SessionConfig {
133-
initial_validators: initial_authorities
133+
keys: initial_authorities
134134
.iter()
135135
.map(|authority_keys| {
136-
(authority_keys.cross_chain.clone().into(), authority_keys.session.clone())
136+
(
137+
authority_keys.cross_chain.clone().into(),
138+
authority_keys.cross_chain.clone().into(),
139+
authority_keys.session.clone(),
140+
)
137141
})
138142
.collect(),
143+
non_authority_keys: Default::default(),
139144
},
140145
sidechain: SidechainConfig {
141146
genesis_utxo: sp_sidechain::read_genesis_utxo_from_env_with_defaults()?,
142147
..Default::default()
143148
},
144-
pallet_session: Default::default(),
145149
session_committee_management: SessionCommitteeManagementConfig {
146150
initial_authorities: initial_authorities
147151
.into_iter()

demo/node/src/template_chain_spec.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ pub fn chain_spec() -> Result<ChainSpec, envy::Error> {
2727
transaction_payment: Default::default(),
2828
session: SessionConfig {
2929
// Initial validators are meant to be updated in the chain spec file, so it is empty here.
30-
initial_validators: vec![],
30+
keys: vec![],
31+
non_authority_keys: Default::default(),
3132
},
3233
sidechain: SidechainConfig {
3334
genesis_utxo: sp_sidechain::read_genesis_utxo_from_env_with_defaults()?,
3435
..Default::default()
3536
},
36-
pallet_session: Default::default(),
3737
session_committee_management: SessionCommitteeManagementConfig {
3838
// Same as SessionConfig
3939
initial_authorities: vec![],

demo/node/src/testnet.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,19 +181,23 @@ pub fn testnet_genesis(
181181
},
182182
transaction_payment: Default::default(),
183183
session: SessionConfig {
184-
initial_validators: initial_authorities
184+
keys: initial_authorities
185185
.iter()
186186
.map(|authority_keys| {
187-
(authority_keys.cross_chain.clone().into(), authority_keys.session.clone())
187+
(
188+
authority_keys.cross_chain.clone().into(),
189+
authority_keys.cross_chain.clone().into(),
190+
authority_keys.session.clone(),
191+
)
188192
})
189193
.collect(),
194+
non_authority_keys: Default::default(),
190195
},
191196
sidechain: SidechainConfig {
192197
genesis_utxo: sp_sidechain::read_genesis_utxo_from_env_with_defaults()?,
193198
slots_per_epoch: SlotsPerEpoch::read_from_env()?,
194199
..Default::default()
195200
},
196-
pallet_session: Default::default(),
197201
session_committee_management: SessionCommitteeManagementConfig {
198202
initial_authorities: initial_authorities
199203
.into_iter()

demo/runtime/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pallet-grandpa = { workspace = true }
3232
pallet-partner-chains-session = { workspace = true, features = [
3333
"pallet-session-compat",
3434
] }
35-
pallet-session = { workspace = true }
35+
pallet-session = { workspace = true, features = ["historical"] }
3636
pallet-sudo = { workspace = true }
3737
frame-system = { workspace = true }
3838
frame-try-runtime = { workspace = true, optional = true }

demo/runtime/src/genesis_config_presets.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ fn testnet_genesis(
5858
transaction_payment: Default::default(),
5959
sidechain: Default::default(),
6060
session_committee_management: Default::default(),
61-
pallet_session: Default::default(),
6261
session: Default::default(),
6362
native_token_management: Default::default(),
6463
governed_map: GovernedMapConfig {

demo/runtime/src/lib.rs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ use sp_core::ByteArray;
4949
use sp_core::{OpaqueMetadata, crypto::KeyTypeId};
5050
use sp_governed_map::MainChainScriptsV1;
5151
use sp_inherents::InherentIdentifier;
52+
use sp_runtime::traits::ConvertInto;
5253
use sp_runtime::{
5354
ApplyExtrinsicResult, MultiSignature, Perbill, generic, impl_opaque_keys,
5455
traits::{
@@ -310,7 +311,24 @@ impl pallet_aura::Config for Runtime {
310311
type SlotDuration = ConstU64<SLOT_DURATION>;
311312
}
312313

313-
pallet_partner_chains_session::impl_pallet_session_config!(Runtime);
314+
impl pallet_session::Config for Runtime {
315+
type RuntimeEvent = RuntimeEvent;
316+
type ValidatorId = AccountId;
317+
type ValidatorIdOf = ConvertInto;
318+
type ShouldEndSession = ValidatorManagementSessionManager<Runtime>;
319+
type NextSessionRotation = ();
320+
type SessionManager = ValidatorManagementSessionManager<Runtime>;
321+
type SessionHandler = <SessionKeys as OpaqueKeys>::KeyTypeIdProviders;
322+
type Keys = SessionKeys;
323+
type DisablingStrategy = pallet_session::disabling::UpToLimitWithReEnablingDisablingStrategy;
324+
325+
type WeightInfo = pallet_session::weights::SubstrateWeight<Runtime>;
326+
}
327+
328+
// impl pallet_session::historical::Config for Runtime {
329+
// type FullIdentification = ();
330+
// type FullIdentificationOf = ();
331+
// }
314332

315333
impl pallet_grandpa::Config for Runtime {
316334
type RuntimeEvent = RuntimeEvent;
@@ -374,16 +392,6 @@ impl pallet_sudo::Config for Runtime {
374392
type WeightInfo = pallet_sudo::weights::SubstrateWeight<Runtime>;
375393
}
376394

377-
impl pallet_partner_chains_session::Config for Runtime {
378-
type RuntimeEvent = RuntimeEvent;
379-
type ValidatorId = <Self as frame_system::Config>::AccountId;
380-
type ShouldEndSession = ValidatorManagementSessionManager<Runtime>;
381-
type NextSessionRotation = ();
382-
type SessionManager = ValidatorManagementSessionManager<Runtime>;
383-
type SessionHandler = <opaque::SessionKeys as OpaqueKeys>::KeyTypeIdProviders;
384-
type Keys = opaque::SessionKeys;
385-
}
386-
387395
parameter_types! {
388396
pub const MaxValidators: u32 = 32;
389397
}
@@ -657,13 +665,8 @@ construct_runtime!(
657665
BlockProducerMetadata: pallet_block_producer_metadata,
658666
BlockProductionLog: pallet_block_production_log,
659667
BlockParticipation: pallet_block_participation,
660-
// pallet_grandpa reads pallet_session::pallet::CurrentIndex storage.
661-
// Only stub implementation of pallet_session should be wired.
662-
// Partner Chains session_manager ValidatorManagementSessionManager writes to pallet_session::pallet::CurrentIndex.
663-
// ValidatorManagementSessionManager is wired in by pallet_partner_chains_session.
664-
PalletSession: pallet_session,
665-
// The order matters!! pallet_partner_chains_session needs to come last for correct initialization order
666-
Session: pallet_partner_chains_session,
668+
Session: pallet_session,
669+
// Historical: pallet_session::historical,
667670
NativeTokenManagement: pallet_native_token_management,
668671
GovernedMap: pallet_governed_map,
669672
TestHelperPallet: crate::test_helper_pallet,
@@ -859,7 +862,6 @@ impl_runtime_apis! {
859862
}
860863
}
861864

862-
863865
impl frame_system_rpc_runtime_api::AccountNonceApi<Block, AccountId, Nonce> for Runtime {
864866
fn account_nonce(account: AccountId) -> Nonce {
865867
System::account_nonce(account)

toolkit/committee-selection/pallet/src/session_manager.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,47 @@ impl<T: crate::Config + pallet_session::Config>
100100
}
101101
}
102102

103+
impl<T: crate::Config + pallet_session::Config> pallet_session::SessionManager<T::AccountId>
104+
for ValidatorManagementSessionManager<T>
105+
{
106+
fn new_session_genesis(_new_index: SessionIndex) -> Option<Vec<T::AccountId>> {
107+
Some(
108+
crate::Pallet::<T>::current_committee_storage()
109+
.committee
110+
.into_iter()
111+
.map(|member| member.authority_id().into())
112+
.collect::<Vec<_>>(),
113+
)
114+
}
115+
116+
// Instead of Some((*).expect) we could just use (*). However, we rather panic in presence of
117+
// important programming errors.
118+
fn new_session(new_index: SessionIndex) -> Option<Vec<T::AccountId>> {
119+
info!("New session {new_index}");
120+
pallet_session::pallet::CurrentIndex::<T>::put(new_index);
121+
Some(
122+
crate::Pallet::<T>::rotate_committee_to_next_epoch()
123+
.expect(
124+
"Session should never end without current epoch validators defined. \
125+
Check ShouldEndSession implementation or if it is used before starting new session",
126+
)
127+
.into_iter()
128+
.map(|member| member.authority_id().into())
129+
.collect(),
130+
)
131+
}
132+
133+
fn end_session(end_index: SessionIndex) {
134+
info!("End session {end_index}");
135+
}
136+
137+
// Session is expected to be at least 1 block behind sidechain epoch.
138+
fn start_session(start_index: SessionIndex) {
139+
let epoch_number = T::current_epoch_number();
140+
info!("Start session {start_index}, epoch {epoch_number}");
141+
}
142+
}
143+
103144
/// This implementation tries to end each session in the first block of each sidechain epoch in which
104145
/// the committee for the epoch is defined.
105146
impl<T, ScEpochNumber> pallet_partner_chains_session::ShouldEndSession<BlockNumberFor<T>>
@@ -116,6 +157,22 @@ where
116157
}
117158
}
118159

160+
/// This implementation tries to end each session in the first block of each sidechain epoch in which
161+
/// the committee for the epoch is defined.
162+
impl<T, ScEpochNumber> pallet_session::ShouldEndSession<BlockNumberFor<T>>
163+
for ValidatorManagementSessionManager<T>
164+
where
165+
T: crate::Config<ScEpochNumber = ScEpochNumber>,
166+
ScEpochNumber: Clone + PartialOrd,
167+
{
168+
fn should_end_session(_n: BlockNumberFor<T>) -> bool {
169+
let current_epoch_number = T::current_epoch_number();
170+
171+
current_epoch_number > crate::Pallet::<T>::current_committee_storage().epoch
172+
&& crate::Pallet::<T>::next_committee().is_some()
173+
}
174+
}
175+
119176
#[cfg(test)]
120177
mod tests {
121178
use crate::mock::mock_pallet::CurrentEpoch;

0 commit comments

Comments
 (0)