mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-30 11:41:02 +00:00
runtime/session_info: avoid heavy loop when introduced on a live chain (#2099)
* session_info: a heavy loop test * session_info: fix a typo * session_info: fix heavy loop * session_info: crank the iterations all the way up
This commit is contained in:
@@ -101,9 +101,12 @@ impl<T: Config> Module<T> {
|
|||||||
// update `EarliestStoredSession` based on `config.dispute_period`
|
// update `EarliestStoredSession` based on `config.dispute_period`
|
||||||
EarliestStoredSession::set(new_earliest_stored_session);
|
EarliestStoredSession::set(new_earliest_stored_session);
|
||||||
// remove all entries from `Sessions` from the previous value up to the new value
|
// remove all entries from `Sessions` from the previous value up to the new value
|
||||||
|
// avoid a potentially heavy loop when introduced on a live chain
|
||||||
|
if old_earliest_stored_session != 0 || Sessions::get(0).is_some() {
|
||||||
for idx in old_earliest_stored_session..new_earliest_stored_session {
|
for idx in old_earliest_stored_session..new_earliest_stored_session {
|
||||||
Sessions::remove(&idx);
|
Sessions::remove(&idx);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// create a new entry in `Sessions` with information about the current session
|
// create a new entry in `Sessions` with information about the current session
|
||||||
let new_session_info = SessionInfo {
|
let new_session_info = SessionInfo {
|
||||||
validators,
|
validators,
|
||||||
@@ -216,7 +219,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn session_pruning_is_based_on_dispute_deriod() {
|
fn session_pruning_is_based_on_dispute_period() {
|
||||||
new_test_ext(genesis_config()).execute_with(|| {
|
new_test_ext(genesis_config()).execute_with(|| {
|
||||||
run_to_block(100, session_changes);
|
run_to_block(100, session_changes);
|
||||||
assert_eq!(EarliestStoredSession::get(), 9);
|
assert_eq!(EarliestStoredSession::get(), 9);
|
||||||
@@ -253,4 +256,26 @@ mod tests {
|
|||||||
assert_eq!(session.needed_approvals, 42);
|
assert_eq!(session.needed_approvals, 42);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn session_pruning_avoids_heavy_loop() {
|
||||||
|
new_test_ext(genesis_config()).execute_with(|| {
|
||||||
|
let start = 1_000_000_000;
|
||||||
|
System::on_initialize(start);
|
||||||
|
System::set_block_number(start);
|
||||||
|
|
||||||
|
if let Some(notification) = new_session_every_block(start) {
|
||||||
|
Configuration::initializer_on_new_session(¬ification.validators, ¬ification.queued);
|
||||||
|
SessionInfo::initializer_on_new_session(¬ification);
|
||||||
|
}
|
||||||
|
|
||||||
|
Configuration::initializer_initialize(start);
|
||||||
|
SessionInfo::initializer_initialize(start);
|
||||||
|
|
||||||
|
assert_eq!(EarliestStoredSession::get(), start - 1);
|
||||||
|
|
||||||
|
run_to_block(start + 1, new_session_every_block);
|
||||||
|
assert_eq!(EarliestStoredSession::get(), start);
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user