diff --git a/authority/src/lib.rs b/authority/src/lib.rs index 63bb57ff7..d2ca9bc28 100644 --- a/authority/src/lib.rs +++ b/authority/src/lib.rs @@ -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; @@ -201,6 +200,7 @@ pub mod module { #[pallet::origin] pub type Origin = DelayedOrigin, ::PalletsOrigin>; pub(crate) type CallOf = ::RuntimeCall; + pub(crate) type BoundedCallOf = Bounded, ::Hashing>; #[pallet::config] pub trait Config: frame_system::Config { @@ -221,8 +221,12 @@ pub mod module { + GetDispatchInfo; /// The Scheduler. - #[allow(deprecated)] - type Scheduler: ScheduleNamed, ::RuntimeCall, Self::PalletsOrigin>; + type Scheduler: ScheduleNamed< + BlockNumberFor, + ::RuntimeCall, + Self::PalletsOrigin, + Hasher = Self::Hashing, + >; /// The type represent origin that can be dispatched by other origins. type AsOriginId: Parameter + AsOriginId<::RuntimeOrigin, Self::PalletsOrigin>; @@ -337,7 +341,7 @@ pub mod module { when: DispatchTime>, priority: Priority, with_delayed_origin: bool, - call: Box>, + call: Box>, ) -> DispatchResult { T::AuthorityConfig::check_schedule_dispatch(origin.clone(), priority)?; @@ -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::::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::::FailedToSchedule)?; Self::deposit_event(Event::Scheduled { origin: pallets_origin, @@ -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::::FailedToFastTrack)?; + let task_name = (&initial_origin, task_id).using_encoded(blake2_256); + T::Scheduler::reschedule_named(task_name, when).map_err(|_| Error::::FailedToFastTrack)?; Self::deposit_event(Event::FastTracked { origin: *initial_origin, @@ -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::::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::::FailedToDelay)?; let now = frame_system::Pallet::::block_number(); let dispatch_at = now.saturating_add(additional_delay); @@ -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::::FailedToCancel)?; + + let task_name = (&initial_origin, task_id).using_encoded(blake2_256); + T::Scheduler::cancel_named(task_name).map_err(|_| Error::::FailedToCancel)?; Self::deposit_event(Event::Cancelled { origin: *initial_origin, diff --git a/authority/src/tests.rs b/authority/src/tests.rs index bcbd4dc80..c1366c096 100644 --- a/authority/src/tests.rs +++ b/authority/src/tests.rs @@ -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; @@ -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::::FailedToSchedule ); @@ -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 { @@ -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), @@ -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 ); @@ -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 { @@ -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), @@ -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 { @@ -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), @@ -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 { @@ -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), @@ -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 { @@ -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),