@@ -1054,6 +1054,15 @@ pub mod pallet {
10541054 }
10551055}
10561056
1057+ /// A struct that holds the configuration that was active before the session change and optionally
1058+ /// a configuration that became active after the session change.
1059+ pub struct SessionChangeOutcome < BlockNumber > {
1060+ /// Previously active configuration.
1061+ pub prev_config : HostConfiguration < BlockNumber > ,
1062+ /// If new configuration was applied during the session change, this is the new configuration.
1063+ pub new_config : Option < HostConfiguration < BlockNumber > > ,
1064+ }
1065+
10571066impl < T : Config > Pallet < T > {
10581067 /// Called by the initializer to initialize the configuration module.
10591068 pub ( crate ) fn initializer_initialize ( _now : T :: BlockNumber ) -> Weight {
@@ -1064,13 +1073,22 @@ impl<T: Config> Pallet<T> {
10641073 pub ( crate ) fn initializer_finalize ( ) { }
10651074
10661075 /// Called by the initializer to note that a new session has started.
1067- pub ( crate ) fn initializer_on_new_session ( session_index : & SessionIndex ) {
1076+ ///
1077+ /// Returns the configuration that was actual before the session change and the configuration
1078+ /// that became active after the session change. If there were no scheduled changes, both will
1079+ /// be the same.
1080+ pub ( crate ) fn initializer_on_new_session (
1081+ session_index : & SessionIndex ,
1082+ ) -> SessionChangeOutcome < T :: BlockNumber > {
10681083 let pending_configs = <PendingConfigs < T > >:: get ( ) ;
1084+ let prev_config = <Self as Store >:: ActiveConfig :: get ( ) ;
1085+
1086+ // No pending configuration changes, so we're done.
10691087 if pending_configs. is_empty ( ) {
1070- return
1088+ return SessionChangeOutcome { prev_config , new_config : None }
10711089 }
10721090
1073- let ( past_and_present, future) = pending_configs
1091+ let ( mut past_and_present, future) = pending_configs
10741092 . into_iter ( )
10751093 . partition :: < Vec < _ > , _ > ( |& ( apply_at_session, _) | apply_at_session <= * session_index) ;
10761094
@@ -1082,11 +1100,16 @@ impl<T: Config> Pallet<T> {
10821100 "Skipping applying configuration changes scheduled sessions in the past" ,
10831101 ) ;
10841102 }
1085- if let Some ( ( _, pending) ) = past_and_present. last ( ) {
1086- <Self as Store >:: ActiveConfig :: put ( pending) ;
1103+
1104+ let new_config = past_and_present. pop ( ) . map ( |( _, config) | config) ;
1105+ if let Some ( ref new_config) = new_config {
1106+ // Apply the new configuration.
1107+ <Self as Store >:: ActiveConfig :: put ( new_config) ;
10871108 }
10881109
10891110 <PendingConfigs < T > >:: put ( future) ;
1111+
1112+ SessionChangeOutcome { prev_config, new_config }
10901113 }
10911114
10921115 /// Return the session index that should be used for any future scheduled changes.
@@ -1167,9 +1190,14 @@ mod tests {
11671190
11681191 use frame_support:: assert_ok;
11691192
1170- fn on_new_session ( session_index : SessionIndex ) {
1193+ fn on_new_session (
1194+ session_index : SessionIndex ,
1195+ ) -> ( HostConfiguration < u32 > , HostConfiguration < u32 > ) {
11711196 ParasShared :: set_session_index ( session_index) ;
1172- Configuration :: initializer_on_new_session ( & session_index) ;
1197+ let SessionChangeOutcome { prev_config, new_config } =
1198+ Configuration :: initializer_on_new_session ( & session_index) ;
1199+ let new_config = new_config. unwrap_or_else ( || prev_config. clone ( ) ) ;
1200+ ( prev_config, new_config)
11731201 }
11741202
11751203 #[ test]
@@ -1182,6 +1210,25 @@ mod tests {
11821210 } ) ;
11831211 }
11841212
1213+ #[ test]
1214+ fn initializer_on_new_session ( ) {
1215+ new_test_ext ( Default :: default ( ) ) . execute_with ( || {
1216+ let ( prev_config, new_config) = on_new_session ( 1 ) ;
1217+ assert_eq ! ( prev_config, new_config) ;
1218+ assert_ok ! ( Configuration :: set_validation_upgrade_delay( Origin :: root( ) , 100 ) ) ;
1219+
1220+ let ( prev_config, new_config) = on_new_session ( 2 ) ;
1221+ assert_eq ! ( prev_config, new_config) ;
1222+
1223+ let ( prev_config, new_config) = on_new_session ( 3 ) ;
1224+ assert_eq ! ( prev_config, HostConfiguration :: default ( ) ) ;
1225+ assert_eq ! (
1226+ new_config,
1227+ HostConfiguration { validation_upgrade_delay: 100 , ..prev_config }
1228+ ) ;
1229+ } ) ;
1230+ }
1231+
11851232 #[ test]
11861233 fn config_changes_after_2_session_boundary ( ) {
11871234 new_test_ext ( Default :: default ( ) ) . execute_with ( || {
0 commit comments