Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 267b45f

Browse files
pepyakindrahnr
authored andcommitted
configuration: refactor configuration initialization (#4569)
Refactor the configuration module's initializer_on_new_session in such a way that it returns the configuration. This would make it inline with other special initialization routines like `shared`'s or `paras`. This will be useful in a following PR that will check consistency of the configuration before setting it.
1 parent 7a08b05 commit 267b45f

File tree

2 files changed

+57
-14
lines changed

2 files changed

+57
-14
lines changed

runtime/parachains/src/configuration.rs

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
10571066
impl<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(|| {

runtime/parachains/src/initializer.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,6 @@ impl<T: Config> Pallet<T> {
221221
all_validators: Vec<ValidatorId>,
222222
queued: Vec<ValidatorId>,
223223
) {
224-
let prev_config = <configuration::Pallet<T>>::config();
225-
226224
let random_seed = {
227225
let mut buf = [0u8; 32];
228226
// TODO: audit usage of randomness API
@@ -233,11 +231,9 @@ impl<T: Config> Pallet<T> {
233231
buf
234232
};
235233

236-
// We can't pass the new config into the thing that determines the new config,
237-
// so we don't pass the `SessionChangeNotification` into this module.
238-
configuration::Pallet::<T>::initializer_on_new_session(&session_index);
239-
240-
let new_config = <configuration::Pallet<T>>::config();
234+
let configuration::SessionChangeOutcome { prev_config, new_config } =
235+
configuration::Pallet::<T>::initializer_on_new_session(&session_index);
236+
let new_config = new_config.unwrap_or_else(|| prev_config.clone());
241237

242238
let validators = shared::Pallet::<T>::initializer_on_new_session(
243239
session_index,

0 commit comments

Comments
 (0)