Scheduler: remove empty agenda on cancel (#12989)

* Scheduler: remove empty agenda on cancel

* use iter any

* fix benches

* remove trailing None

* Add CleanupAgendas migration

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* fix ci

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Count non-empty agendas in migration

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
This commit is contained in:
Muharem Ismailov
2022-12-21 16:25:46 +01:00
committed by GitHub
parent 9cdb920462
commit 4dc62d7e71
4 changed files with 353 additions and 81 deletions
+20
View File
@@ -752,6 +752,22 @@ impl<T: Config> Pallet<T> {
Ok(index)
}
/// Remove trailing `None` items of an agenda at `when`. If all items are `None` remove the
/// agenda record entirely.
fn cleanup_agenda(when: T::BlockNumber) {
let mut agenda = Agenda::<T>::get(when);
match agenda.iter().rposition(|i| i.is_some()) {
Some(i) if agenda.len() > i + 1 => {
agenda.truncate(i + 1);
Agenda::<T>::insert(when, agenda);
},
Some(_) => {},
None => {
Agenda::<T>::remove(when);
},
}
}
fn do_schedule(
when: DispatchTime<T::BlockNumber>,
maybe_periodic: Option<schedule::Period<T::BlockNumber>>,
@@ -802,6 +818,7 @@ impl<T: Config> Pallet<T> {
if let Some(id) = s.maybe_id {
Lookup::<T>::remove(id);
}
Self::cleanup_agenda(when);
Self::deposit_event(Event::Canceled { when, index });
Ok(())
} else {
@@ -824,6 +841,7 @@ impl<T: Config> Pallet<T> {
ensure!(!matches!(task, Some(Scheduled { maybe_id: Some(_), .. })), Error::<T>::Named);
task.take().ok_or(Error::<T>::NotFound)
})?;
Self::cleanup_agenda(when);
Self::deposit_event(Event::Canceled { when, index });
Self::place_task(new_time, task).map_err(|x| x.0)
@@ -880,6 +898,7 @@ impl<T: Config> Pallet<T> {
}
Ok(())
})?;
Self::cleanup_agenda(when);
Self::deposit_event(Event::Canceled { when, index });
Ok(())
} else {
@@ -905,6 +924,7 @@ impl<T: Config> Pallet<T> {
let task = agenda.get_mut(index as usize).ok_or(Error::<T>::NotFound)?;
task.take().ok_or(Error::<T>::NotFound)
})?;
Self::cleanup_agenda(when);
Self::deposit_event(Event::Canceled { when, index });
Self::place_task(new_time, task).map_err(|x| x.0)
}