This repository was archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
reschedule #6860
Merged
Merged
reschedule #6860
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
7d4ca0d
reschedule
xlc 2781617
add tests
xlc 7ae59df
Apply suggestions from code review
xlc 45d0e33
add test for reschedule named perodic
xlc 803e3c4
update test
xlc 2dc3aa0
handle the case when reschedule does not change time
xlc 49de2c0
Merge remote-tracking branch 'origin/master' into reschedule
xlc 4f98a44
Merge remote-tracking branch 'origin/master' into reschedule
xlc fef2aaf
Merge branch 'master' into pr/6860
shawntabrizi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -190,10 +190,12 @@ decl_error! { | |
| pub enum Error for Module<T: Trait> { | ||
| /// Failed to schedule a call | ||
| FailedToSchedule, | ||
| /// Failed to cancel a scheduled call | ||
| FailedToCancel, | ||
| /// Cannot find the scheduled call. | ||
| NotFound, | ||
| /// Given target block number is in the past. | ||
| TargetBlockNumberInPast, | ||
| /// Reschedule failed because it does not change scheduled time. | ||
| RescheduleNoChange, | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -438,13 +440,7 @@ impl<T: Trait> Module<T> { | |
| } | ||
| } | ||
|
|
||
| fn do_schedule( | ||
| when: DispatchTime<T::BlockNumber>, | ||
| maybe_periodic: Option<schedule::Period<T::BlockNumber>>, | ||
| priority: schedule::Priority, | ||
| origin: T::PalletsOrigin, | ||
| call: <T as Trait>::Call | ||
| ) -> Result<TaskAddress<T::BlockNumber>, DispatchError> { | ||
| fn resolve_time(when: DispatchTime<T::BlockNumber>) -> Result<T::BlockNumber, DispatchError> { | ||
| let now = frame_system::Module::<T>::block_number(); | ||
|
|
||
| let when = match when { | ||
|
|
@@ -456,6 +452,18 @@ impl<T: Trait> Module<T> { | |
| return Err(Error::<T>::TargetBlockNumberInPast.into()) | ||
| } | ||
|
|
||
| Ok(when) | ||
| } | ||
|
|
||
| fn do_schedule( | ||
| when: DispatchTime<T::BlockNumber>, | ||
| maybe_periodic: Option<schedule::Period<T::BlockNumber>>, | ||
| priority: schedule::Priority, | ||
| origin: T::PalletsOrigin, | ||
| call: <T as Trait>::Call | ||
| ) -> Result<TaskAddress<T::BlockNumber>, DispatchError> { | ||
| let when = Self::resolve_time(when)?; | ||
|
|
||
| // sanitize maybe_periodic | ||
| let maybe_periodic = maybe_periodic | ||
| .filter(|p| p.1 > 1 && !p.0.is_zero()) | ||
|
|
@@ -496,10 +504,34 @@ impl<T: Trait> Module<T> { | |
| Self::deposit_event(RawEvent::Canceled(when, index)); | ||
| Ok(()) | ||
| } else { | ||
| Err(Error::<T>::FailedToCancel)? | ||
| Err(Error::<T>::NotFound)? | ||
| } | ||
| } | ||
|
|
||
| fn do_reschedule( | ||
| (when, index): TaskAddress<T::BlockNumber>, | ||
| new_time: DispatchTime<T::BlockNumber>, | ||
| ) -> Result<TaskAddress<T::BlockNumber>, DispatchError> { | ||
| let new_time = Self::resolve_time(new_time)?; | ||
|
|
||
| if new_time == when { | ||
| return Err(Error::<T>::RescheduleNoChange.into()); | ||
| } | ||
|
|
||
| Agenda::<T>::try_mutate(when, |agenda| -> DispatchResult { | ||
| let task = agenda.get_mut(index as usize).ok_or(Error::<T>::NotFound)?; | ||
| let task = task.take().ok_or(Error::<T>::NotFound)?; | ||
| Agenda::<T>::append(new_time, Some(task)); | ||
| Ok(()) | ||
| })?; | ||
|
|
||
| let new_index = Agenda::<T>::decode_len(new_time).unwrap_or(1) as u32 - 1; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Its more efficient to get the length from the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| Self::deposit_event(RawEvent::Canceled(when, index)); | ||
| Self::deposit_event(RawEvent::Scheduled(new_time, new_index)); | ||
|
|
||
| Ok((new_time, new_index)) | ||
| } | ||
|
|
||
| fn do_schedule_named( | ||
| id: Vec<u8>, | ||
| when: DispatchTime<T::BlockNumber>, | ||
|
|
@@ -513,16 +545,7 @@ impl<T: Trait> Module<T> { | |
| return Err(Error::<T>::FailedToSchedule)? | ||
| } | ||
|
|
||
| let now = frame_system::Module::<T>::block_number(); | ||
|
|
||
| let when = match when { | ||
| DispatchTime::At(x) => x, | ||
| DispatchTime::After(x) => now.saturating_add(x) | ||
| }; | ||
|
|
||
| if when <= now { | ||
| return Err(Error::<T>::TargetBlockNumberInPast.into()) | ||
| } | ||
| let when = Self::resolve_time(when)?; | ||
|
|
||
| // sanitize maybe_periodic | ||
| let maybe_periodic = maybe_periodic | ||
|
|
@@ -560,10 +583,41 @@ impl<T: Trait> Module<T> { | |
| Self::deposit_event(RawEvent::Canceled(when, index)); | ||
| Ok(()) | ||
| } else { | ||
| Err(Error::<T>::FailedToCancel)? | ||
| Err(Error::<T>::NotFound)? | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| fn do_reschedule_named( | ||
| id: Vec<u8>, | ||
| new_time: DispatchTime<T::BlockNumber>, | ||
| ) -> Result<TaskAddress<T::BlockNumber>, DispatchError> { | ||
| let new_time = Self::resolve_time(new_time)?; | ||
|
|
||
| Lookup::<T>::try_mutate_exists(id, |lookup| -> Result<TaskAddress<T::BlockNumber>, DispatchError> { | ||
| let (when, index) = lookup.ok_or(Error::<T>::NotFound)?; | ||
|
|
||
| if new_time == when { | ||
| return Err(Error::<T>::RescheduleNoChange.into()); | ||
| } | ||
|
|
||
| Agenda::<T>::try_mutate(when, |agenda| -> DispatchResult { | ||
| let task = agenda.get_mut(index as usize).ok_or(Error::<T>::NotFound)?; | ||
| let task = task.take().ok_or(Error::<T>::NotFound)?; | ||
| Agenda::<T>::append(new_time, Some(task)); | ||
|
|
||
| Ok(()) | ||
| })?; | ||
|
|
||
| let new_index = Agenda::<T>::decode_len(new_time).unwrap_or(1) as u32 - 1; | ||
| Self::deposit_event(RawEvent::Canceled(when, index)); | ||
| Self::deposit_event(RawEvent::Scheduled(new_time, new_index)); | ||
|
|
||
| *lookup = Some((new_time, new_index)); | ||
|
|
||
| Ok((new_time, new_index)) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| impl<T: Trait> schedule::Anon<T::BlockNumber, <T as Trait>::Call, T::PalletsOrigin> for Module<T> { | ||
|
|
@@ -582,6 +636,17 @@ impl<T: Trait> schedule::Anon<T::BlockNumber, <T as Trait>::Call, T::PalletsOrig | |
| fn cancel((when, index): Self::Address) -> Result<(), ()> { | ||
| Self::do_cancel(None, (when, index)).map_err(|_| ()) | ||
| } | ||
|
|
||
| fn reschedule( | ||
| address: Self::Address, | ||
| when: DispatchTime<T::BlockNumber>, | ||
| ) -> Result<Self::Address, DispatchError> { | ||
| Self::do_reschedule(address, when) | ||
| } | ||
|
|
||
| fn next_dispatch_time((when, index): Self::Address) -> Result<T::BlockNumber, ()> { | ||
| Agenda::<T>::get(when).get(index as usize).ok_or(()).map(|_| when) | ||
| } | ||
| } | ||
|
|
||
| impl<T: Trait> schedule::Named<T::BlockNumber, <T as Trait>::Call, T::PalletsOrigin> for Module<T> { | ||
|
|
@@ -601,6 +666,17 @@ impl<T: Trait> schedule::Named<T::BlockNumber, <T as Trait>::Call, T::PalletsOri | |
| fn cancel_named(id: Vec<u8>) -> Result<(), ()> { | ||
| Self::do_cancel_named(None, id).map_err(|_| ()) | ||
| } | ||
|
|
||
| fn reschedule_named( | ||
| id: Vec<u8>, | ||
| when: DispatchTime<T::BlockNumber>, | ||
| ) -> Result<Self::Address, DispatchError> { | ||
| Self::do_reschedule_named(id, when) | ||
| } | ||
|
|
||
| fn next_dispatch_time(id: Vec<u8>) -> Result<T::BlockNumber, ()> { | ||
| Lookup::<T>::get(id).and_then(|(when, index)| Agenda::<T>::get(when).get(index as usize).map(|_| when)).ok_or(()) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
|
|
@@ -824,6 +900,95 @@ mod tests { | |
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn reschedule_works() { | ||
| new_test_ext().execute_with(|| { | ||
| let call = Call::Logger(logger::Call::log(42, 1000)); | ||
| assert!(!<Test as frame_system::Trait>::BaseCallFilter::filter(&call)); | ||
| assert_eq!(Scheduler::do_schedule(DispatchTime::At(4), None, 127, root(), call).unwrap(), (4, 0)); | ||
|
|
||
| run_to_block(3); | ||
| assert!(logger::log().is_empty()); | ||
|
|
||
| assert_eq!(Scheduler::do_reschedule((4, 0), DispatchTime::At(6)).unwrap(), (6, 0)); | ||
|
|
||
| assert_noop!(Scheduler::do_reschedule((6, 0), DispatchTime::At(6)), Error::<Test>::RescheduleNoChange); | ||
|
|
||
| run_to_block(4); | ||
| assert!(logger::log().is_empty()); | ||
|
|
||
| run_to_block(6); | ||
| assert_eq!(logger::log(), vec![(root(), 42u32)]); | ||
|
|
||
| run_to_block(100); | ||
| assert_eq!(logger::log(), vec![(root(), 42u32)]); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn reschedule_named_works() { | ||
xlc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| new_test_ext().execute_with(|| { | ||
| let call = Call::Logger(logger::Call::log(42, 1000)); | ||
| assert!(!<Test as frame_system::Trait>::BaseCallFilter::filter(&call)); | ||
| assert_eq!(Scheduler::do_schedule_named( | ||
| 1u32.encode(), DispatchTime::At(4), None, 127, root(), call | ||
| ).unwrap(), (4, 0)); | ||
|
|
||
| run_to_block(3); | ||
| assert!(logger::log().is_empty()); | ||
|
|
||
| assert_eq!(Scheduler::do_reschedule_named(1u32.encode(), DispatchTime::At(6)).unwrap(), (6, 0)); | ||
|
|
||
| assert_noop!(Scheduler::do_reschedule_named(1u32.encode(), DispatchTime::At(6)), Error::<Test>::RescheduleNoChange); | ||
|
|
||
| run_to_block(4); | ||
| assert!(logger::log().is_empty()); | ||
|
|
||
| run_to_block(6); | ||
| assert_eq!(logger::log(), vec![(root(), 42u32)]); | ||
|
|
||
| run_to_block(100); | ||
| assert_eq!(logger::log(), vec![(root(), 42u32)]); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn reschedule_named_perodic_works() { | ||
| new_test_ext().execute_with(|| { | ||
| let call = Call::Logger(logger::Call::log(42, 1000)); | ||
| assert!(!<Test as frame_system::Trait>::BaseCallFilter::filter(&call)); | ||
| assert_eq!(Scheduler::do_schedule_named( | ||
| 1u32.encode(), DispatchTime::At(4), Some((3, 3)), 127, root(), call | ||
| ).unwrap(), (4, 0)); | ||
|
|
||
| run_to_block(3); | ||
| assert!(logger::log().is_empty()); | ||
|
|
||
| assert_eq!(Scheduler::do_reschedule_named(1u32.encode(), DispatchTime::At(5)).unwrap(), (5, 0)); | ||
| assert_eq!(Scheduler::do_reschedule_named(1u32.encode(), DispatchTime::At(6)).unwrap(), (6, 0)); | ||
|
|
||
| run_to_block(5); | ||
| assert!(logger::log().is_empty()); | ||
|
|
||
| run_to_block(6); | ||
| assert_eq!(logger::log(), vec![(root(), 42u32)]); | ||
|
|
||
| assert_eq!(Scheduler::do_reschedule_named(1u32.encode(), DispatchTime::At(10)).unwrap(), (10, 0)); | ||
|
|
||
| run_to_block(9); | ||
| assert_eq!(logger::log(), vec![(root(), 42u32)]); | ||
|
|
||
| run_to_block(10); | ||
| assert_eq!(logger::log(), vec![(root(), 42u32), (root(), 42u32)]); | ||
|
|
||
| run_to_block(13); | ||
| assert_eq!(logger::log(), vec![(root(), 42u32), (root(), 42u32), (root(), 42u32)]); | ||
|
|
||
| run_to_block(100); | ||
| assert_eq!(logger::log(), vec![(root(), 42u32), (root(), 42u32), (root(), 42u32)]); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn cancel_named_scheduling_works_with_normal_cancel() { | ||
| new_test_ext().execute_with(|| { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.