Skip to content

Commit a4603d4

Browse files
committed
Pallet session wrapper draft
1 parent d6bc2d2 commit a4603d4

File tree

7 files changed

+95
-28
lines changed

7 files changed

+95
-28
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 & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ use sp_core::ByteArray;
4646
use sp_core::{OpaqueMetadata, crypto::KeyTypeId};
4747
use sp_governed_map::MainChainScriptsV1;
4848
use sp_inherents::InherentIdentifier;
49+
use sp_runtime::traits::ConvertInto;
4950
use sp_runtime::{
5051
ApplyExtrinsicResult, MultiSignature, Perbill, generic, impl_opaque_keys,
5152
traits::{
@@ -309,7 +310,24 @@ impl pallet_aura::Config for Runtime {
309310
type SlotDuration = ConstU64<SLOT_DURATION>;
310311
}
311312

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

314332
impl pallet_grandpa::Config for Runtime {
315333
type RuntimeEvent = RuntimeEvent;
@@ -373,15 +391,6 @@ impl pallet_sudo::Config for Runtime {
373391
type WeightInfo = pallet_sudo::weights::SubstrateWeight<Runtime>;
374392
}
375393

376-
impl pallet_partner_chains_session::Config for Runtime {
377-
type ValidatorId = <Self as frame_system::Config>::AccountId;
378-
type ShouldEndSession = ValidatorManagementSessionManager<Runtime>;
379-
type NextSessionRotation = ();
380-
type SessionManager = ValidatorManagementSessionManager<Runtime>;
381-
type SessionHandler = <opaque::SessionKeys as OpaqueKeys>::KeyTypeIdProviders;
382-
type Keys = opaque::SessionKeys;
383-
}
384-
385394
parameter_types! {
386395
pub const MaxValidators: u32 = 1024;
387396
}
@@ -693,13 +702,8 @@ construct_runtime!(
693702
BlockProducerMetadata: pallet_block_producer_metadata,
694703
BlockProductionLog: pallet_block_production_log,
695704
BlockParticipation: pallet_block_participation,
696-
// pallet_grandpa reads pallet_session::pallet::CurrentIndex storage.
697-
// Only stub implementation of pallet_session should be wired.
698-
// Partner Chains session_manager ValidatorManagementSessionManager writes to pallet_session::pallet::CurrentIndex.
699-
// ValidatorManagementSessionManager is wired in by pallet_partner_chains_session.
700-
PalletSession: pallet_session,
701-
// The order matters!! pallet_partner_chains_session needs to come last for correct initialization order
702-
Session: pallet_partner_chains_session,
705+
Session: pallet_session,
706+
// Historical: pallet_session::historical,
703707
NativeTokenManagement: pallet_native_token_management,
704708
GovernedMap: pallet_governed_map,
705709
TestHelperPallet: crate::test_helper_pallet,
@@ -895,7 +899,6 @@ impl_runtime_apis! {
895899
}
896900
}
897901

898-
899902
impl frame_system_rpc_runtime_api::AccountNonceApi<Block, AccountId, Nonce> for Runtime {
900903
fn account_nonce(account: AccountId) -> Nonce {
901904
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
@@ -98,6 +98,47 @@ impl<T: crate::Config + pallet_session::Config>
9898
}
9999
}
100100

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

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

0 commit comments

Comments
 (0)