Skip to content

Commit

Permalink
use schedule traits v3 instead of v1 (#1001)
Browse files Browse the repository at this point in the history
  • Loading branch information
zjb0807 committed May 14, 2024
1 parent 764922f commit f2fc5ee
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 43 deletions.
50 changes: 22 additions & 28 deletions authority/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,21 @@ use frame_support::{
pallet_prelude::*,
traits::{
schedule::{DispatchTime, Priority},
EitherOfDiverse, EnsureOrigin, Get, IsType, OriginTrait,
Bounded, EitherOfDiverse, EnsureOrigin, Get, IsType, OriginTrait,
},
};
use frame_system::{pallet_prelude::*, EnsureRoot, EnsureSigned};
use parity_scale_codec::MaxEncodedLen;
use scale_info::TypeInfo;
use sp_core::defer;
use sp_io::hashing::blake2_256;
use sp_runtime::{
traits::{CheckedSub, Dispatchable, Hash, Saturating},
ArithmeticError, DispatchError, DispatchResult, Either, RuntimeDebug,
};
use sp_std::prelude::*;

// Todo: Switch to current v3 api: https://github.com/open-web3-stack/open-runtime-module-library/issues/995
#[allow(deprecated)]
use frame_support::traits::schedule::v1::Named as ScheduleNamed;
use frame_support::traits::schedule::v3::Named as ScheduleNamed;

mod mock;
mod tests;
Expand Down Expand Up @@ -201,6 +200,7 @@ pub mod module {
#[pallet::origin]
pub type Origin<T> = DelayedOrigin<BlockNumberFor<T>, <T as Config>::PalletsOrigin>;
pub(crate) type CallOf<T> = <T as Config>::RuntimeCall;
pub(crate) type BoundedCallOf<T> = Bounded<CallOf<T>, <T as frame_system::Config>::Hashing>;

#[pallet::config]
pub trait Config: frame_system::Config {
Expand All @@ -221,8 +221,12 @@ pub mod module {
+ GetDispatchInfo;

/// The Scheduler.
#[allow(deprecated)]
type Scheduler: ScheduleNamed<BlockNumberFor<Self>, <Self as Config>::RuntimeCall, Self::PalletsOrigin>;
type Scheduler: ScheduleNamed<
BlockNumberFor<Self>,
<Self as Config>::RuntimeCall,
Self::PalletsOrigin,
Hasher = Self::Hashing,
>;

/// The type represent origin that can be dispatched by other origins.
type AsOriginId: Parameter + AsOriginId<<Self as frame_system::Config>::RuntimeOrigin, Self::PalletsOrigin>;
Expand Down Expand Up @@ -337,7 +341,7 @@ pub mod module {
when: DispatchTime<BlockNumberFor<T>>,
priority: Priority,
with_delayed_origin: bool,
call: Box<CallOf<T>>,
call: Box<BoundedCallOf<T>>,
) -> DispatchResult {
T::AuthorityConfig::check_schedule_dispatch(origin.clone(), priority)?;

Expand All @@ -364,16 +368,9 @@ pub mod module {
};
let pallets_origin = schedule_origin.caller().clone();

#[allow(deprecated)]
T::Scheduler::schedule_named(
Encode::encode(&(&pallets_origin, id)),
when,
None,
priority,
pallets_origin.clone(),
*call,
)
.map_err(|_| Error::<T>::FailedToSchedule)?;
let task_name = (&pallets_origin, id).using_encoded(blake2_256);
T::Scheduler::schedule_named(task_name, when, None, priority, pallets_origin.clone(), *call)
.map_err(|_| Error::<T>::FailedToSchedule)?;

Self::deposit_event(Event::Scheduled {
origin: pallets_origin,
Expand Down Expand Up @@ -402,9 +399,8 @@ pub mod module {
};

T::AuthorityConfig::check_fast_track_schedule(origin, &initial_origin, new_delay)?;
#[allow(deprecated)]
T::Scheduler::reschedule_named((&initial_origin, task_id).encode(), when)
.map_err(|_| Error::<T>::FailedToFastTrack)?;
let task_name = (&initial_origin, task_id).using_encoded(blake2_256);
T::Scheduler::reschedule_named(task_name, when).map_err(|_| Error::<T>::FailedToFastTrack)?;

Self::deposit_event(Event::FastTracked {
origin: *initial_origin,
Expand All @@ -425,12 +421,9 @@ pub mod module {
) -> DispatchResult {
T::AuthorityConfig::check_delay_schedule(origin, &initial_origin)?;

#[allow(deprecated)]
T::Scheduler::reschedule_named(
(&initial_origin, task_id).encode(),
DispatchTime::After(additional_delay),
)
.map_err(|_| Error::<T>::FailedToDelay)?;
let task_name = (&initial_origin, task_id).using_encoded(blake2_256);
T::Scheduler::reschedule_named(task_name, DispatchTime::After(additional_delay))
.map_err(|_| Error::<T>::FailedToDelay)?;

let now = frame_system::Pallet::<T>::block_number();
let dispatch_at = now.saturating_add(additional_delay);
Expand All @@ -452,8 +445,9 @@ pub mod module {
task_id: ScheduleTaskIndex,
) -> DispatchResult {
T::AuthorityConfig::check_cancel_schedule(origin, &initial_origin)?;
#[allow(deprecated)]
T::Scheduler::cancel_named((&initial_origin, task_id).encode()).map_err(|_| Error::<T>::FailedToCancel)?;

let task_name = (&initial_origin, task_id).using_encoded(blake2_256);
T::Scheduler::cancel_named(task_name).map_err(|_| Error::<T>::FailedToCancel)?;

Self::deposit_event(Event::Cancelled {
origin: *initial_origin,
Expand Down
30 changes: 15 additions & 15 deletions authority/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ use super::*;
use frame_support::{
assert_noop, assert_ok,
dispatch::DispatchErrorWithPostInfo,
traits::{schedule::DispatchTime, OriginTrait},
traits::{schedule::DispatchTime, OriginTrait, StorePreimage},
};
use frame_system::RawOrigin;
use mock::{
authority, run_to_block, Authority, BlockNumber, ExtBuilder, MockAsOriginId, OriginCaller, Runtime, RuntimeCall,
RuntimeOrigin, System,
authority, run_to_block, Authority, BlockNumber, ExtBuilder, MockAsOriginId, OriginCaller, Preimage, Runtime,
RuntimeCall, RuntimeOrigin, System,
};
use parity_scale_codec::MaxEncodedLen;
use sp_io::hashing::blake2_256;
Expand Down Expand Up @@ -74,7 +74,7 @@ fn schedule_dispatch_at_work() {
DispatchTime::At(1),
0,
true,
Box::new(call.clone())
Box::new(Preimage::bound(call.clone()).unwrap())
),
Error::<Runtime>::FailedToSchedule
);
Expand All @@ -84,7 +84,7 @@ fn schedule_dispatch_at_work() {
DispatchTime::At(2),
0,
true,
Box::new(call.clone())
Box::new(Preimage::bound(call.clone()).unwrap())
));
System::assert_last_event(mock::RuntimeEvent::Authority(Event::Scheduled {
origin: OriginCaller::Authority(DelayedOrigin {
Expand All @@ -109,7 +109,7 @@ fn schedule_dispatch_at_work() {
DispatchTime::At(3),
0,
false,
Box::new(call)
Box::new(Preimage::bound(call).unwrap())
));
System::assert_last_event(mock::RuntimeEvent::Authority(Event::Scheduled {
origin: OriginCaller::system(RawOrigin::Root),
Expand Down Expand Up @@ -144,7 +144,7 @@ fn schedule_dispatch_after_work() {
DispatchTime::At(0),
0,
true,
Box::new(call.clone())
Box::new(Preimage::bound(call.clone()).unwrap())
),
ArithmeticError::Overflow
);
Expand All @@ -154,7 +154,7 @@ fn schedule_dispatch_after_work() {
DispatchTime::After(0),
0,
true,
Box::new(call.clone())
Box::new(Preimage::bound(call.clone()).unwrap())
));
System::assert_last_event(mock::RuntimeEvent::Authority(Event::Scheduled {
origin: OriginCaller::Authority(DelayedOrigin {
Expand All @@ -179,7 +179,7 @@ fn schedule_dispatch_after_work() {
DispatchTime::After(0),
0,
false,
Box::new(call)
Box::new(Preimage::bound(call).unwrap())
));
System::assert_last_event(mock::RuntimeEvent::Authority(Event::Scheduled {
origin: OriginCaller::system(RawOrigin::Root),
Expand Down Expand Up @@ -214,7 +214,7 @@ fn fast_track_scheduled_dispatch_work() {
DispatchTime::At(2),
0,
true,
Box::new(call.clone())
Box::new(Preimage::bound(call.clone()).unwrap())
));
System::assert_last_event(mock::RuntimeEvent::Authority(Event::Scheduled {
origin: OriginCaller::Authority(DelayedOrigin {
Expand Down Expand Up @@ -255,7 +255,7 @@ fn fast_track_scheduled_dispatch_work() {
DispatchTime::At(2),
0,
false,
Box::new(call)
Box::new(Preimage::bound(call.clone()).unwrap())
));
System::assert_last_event(mock::RuntimeEvent::Authority(Event::Scheduled {
origin: OriginCaller::system(RawOrigin::Root),
Expand Down Expand Up @@ -293,7 +293,7 @@ fn delay_scheduled_dispatch_work() {
DispatchTime::At(2),
0,
true,
Box::new(call.clone())
Box::new(Preimage::bound(call.clone()).unwrap())
));
System::assert_last_event(mock::RuntimeEvent::Authority(Event::Scheduled {
origin: OriginCaller::Authority(DelayedOrigin {
Expand Down Expand Up @@ -334,7 +334,7 @@ fn delay_scheduled_dispatch_work() {
DispatchTime::At(2),
0,
false,
Box::new(call)
Box::new(Preimage::bound(call).unwrap())
));
System::assert_last_event(mock::RuntimeEvent::Authority(Event::Scheduled {
origin: OriginCaller::system(RawOrigin::Root),
Expand Down Expand Up @@ -371,7 +371,7 @@ fn cancel_scheduled_dispatch_work() {
DispatchTime::At(2),
0,
true,
Box::new(call.clone())
Box::new(Preimage::bound(call.clone()).unwrap())
));
System::assert_last_event(mock::RuntimeEvent::Authority(Event::Scheduled {
origin: OriginCaller::Authority(DelayedOrigin {
Expand Down Expand Up @@ -410,7 +410,7 @@ fn cancel_scheduled_dispatch_work() {
DispatchTime::At(2),
0,
false,
Box::new(call)
Box::new(Preimage::bound(call).unwrap())
));
System::assert_last_event(mock::RuntimeEvent::Authority(Event::Scheduled {
origin: OriginCaller::system(RawOrigin::Root),
Expand Down

0 comments on commit f2fc5ee

Please sign in to comment.