From e80f794dffeea3f63c3a792f505a805204069c53 Mon Sep 17 00:00:00 2001 From: akildemir Date: Sat, 18 Nov 2023 10:53:28 +0300 Subject: [PATCH] add gene_session & new_session public functions --- frame/grandpa/src/lib.rs | 49 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/frame/grandpa/src/lib.rs b/frame/grandpa/src/lib.rs index 64f0490f6f6c8..f6fddccda422b 100644 --- a/frame/grandpa/src/lib.rs +++ b/frame/grandpa/src/lib.rs @@ -507,6 +507,55 @@ impl Pallet { } } + /// This function is added later to be used in place of overly specific trait + /// implementation of OneSessionHandler::on_genesis_session. + pub fn genesis_session(authorities: AuthorityList) { + Self::initialize(&authorities); + } + + /// This function is added later to be used in place of overly specific trait + /// implementation of OneSessionHandler::on_new_session. + pub fn new_session(changed: bool, session_index: u32, next_authorities: AuthorityList) { + // Always issue a change if `session` says that the validators have changed. + // Even if their session keys are the same as before, the underlying economic + // identities have changed. + let current_set_id = if changed || >::exists() { + + let res = if let Some((further_wait, median)) = >::take() { + Self::schedule_change(next_authorities, further_wait, Some(median)) + } else { + Self::schedule_change(next_authorities, Zero::zero(), None) + }; + + if res.is_ok() { + let current_set_id = CurrentSetId::::mutate(|s| { + *s += 1; + *s + }); + + let max_set_id_session_entries = T::MaxSetIdSessionEntries::get().max(1); + if current_set_id >= max_set_id_session_entries { + SetIdSession::::remove(current_set_id - max_set_id_session_entries); + } + + current_set_id + } else { + // either the session module signalled that the validators have changed + // or the set was stalled. but since we didn't successfully schedule + // an authority set change we do not increment the set id. + Self::current_set_id() + } + } else { + // nothing's changed, neither economic conditions nor session keys. update the pointer + // of the current set. + Self::current_set_id() + }; + + // update the mapping to note that the current set corresponds to the + // latest equivalent session (i.e. now). + SetIdSession::::insert(current_set_id, &session_index); + } + /// Deposit one of this module's logs. fn deposit_log(log: ConsensusLog>) { let log = DigestItem::Consensus(GRANDPA_ENGINE_ID, log.encode());