git.delta.rocks / unique-network / refs/commits / 95c1ed5151b9

difftreelog

Scheduler benchmarks fixed. Useless code in pallet was removed.

Dev2022-06-09parent: #c557492.patch.diff
in: master

4 files changed

modifiedMakefilediffbeforeafterboth
--- a/Makefile
+++ b/Makefile
@@ -93,5 +93,9 @@
 bench-structure:
 	make _bench PALLET=structure
 
+.PHONY: bench-scheduler
+bench-scheduler:
+	make _bench PALLET=unq-scheduler	
+
 .PHONY: bench
-bench: bench-evm-migration bench-unique bench-structure bench-fungible bench-refungible bench-nonfungible
+bench: bench-evm-migration bench-unique bench-structure bench-fungible bench-refungible bench-nonfungible bench-scheduler
modifiedpallets/fungible/src/weights.rsdiffbeforeafterboth
--- a/pallets/fungible/src/weights.rs
+++ b/pallets/fungible/src/weights.rs
@@ -3,12 +3,13 @@
 //! Autogenerated weights for pallet_fungible
 //!
 //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
-//! DATE: 2022-03-01, STEPS: `50`, REPEAT: 200, LOW RANGE: `[]`, HIGH RANGE: `[]`
+//! DATE: 2022-06-09, STEPS: `50`, REPEAT: 200, LOW RANGE: `[]`, HIGH RANGE: `[]`
 //! EXECUTION: None, WASM-EXECUTION: Compiled, CHAIN: None, DB CACHE: 1024
 
 // Executed Command:
 // target/release/unique-collator
 // benchmark
+// pallet
 // --pallet
 // pallet-fungible
 // --wasm-execution
@@ -47,16 +48,16 @@
 	// Storage: Fungible TotalSupply (r:1 w:1)
 	// Storage: Fungible Balance (r:1 w:1)
 	fn create_item() -> Weight {
-		(14_407_000 as Weight)
+		(40_678_000 as Weight)
 			.saturating_add(T::DbWeight::get().reads(2 as Weight))
 			.saturating_add(T::DbWeight::get().writes(2 as Weight))
 	}
 	// Storage: Fungible TotalSupply (r:1 w:1)
 	// Storage: Fungible Balance (r:4 w:4)
 	fn create_multiple_items_ex(b: u32, ) -> Weight {
-		(13_030_000 as Weight)
-			// Standard Error: 1_000
-			.saturating_add((3_779_000 as Weight).saturating_mul(b as Weight))
+		(13_036_000 as Weight)
+			// Standard Error: 9_000
+			.saturating_add((15_611_000 as Weight).saturating_mul(b as Weight))
 			.saturating_add(T::DbWeight::get().reads(1 as Weight))
 			.saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(b as Weight)))
 			.saturating_add(T::DbWeight::get().writes(1 as Weight))
@@ -65,28 +66,27 @@
 	// Storage: Fungible TotalSupply (r:1 w:1)
 	// Storage: Fungible Balance (r:1 w:1)
 	fn burn_item() -> Weight {
-		(15_565_000 as Weight)
+		(42_340_000 as Weight)
 			.saturating_add(T::DbWeight::get().reads(2 as Weight))
 			.saturating_add(T::DbWeight::get().writes(2 as Weight))
 	}
-
 	// Storage: Fungible Balance (r:2 w:2)
 	fn transfer() -> Weight {
-		(17_713_000 as Weight)
+		(47_321_000 as Weight)
 			.saturating_add(T::DbWeight::get().reads(2 as Weight))
 			.saturating_add(T::DbWeight::get().writes(2 as Weight))
 	}
 	// Storage: Fungible Balance (r:1 w:0)
 	// Storage: Fungible Allowance (r:0 w:1)
 	fn approve() -> Weight {
-		(14_834_000 as Weight)
+		(38_385_000 as Weight)
 			.saturating_add(T::DbWeight::get().reads(1 as Weight))
 			.saturating_add(T::DbWeight::get().writes(1 as Weight))
 	}
 	// Storage: Fungible Allowance (r:1 w:1)
 	// Storage: Fungible Balance (r:2 w:2)
 	fn transfer_from() -> Weight {
-		(25_189_000 as Weight)
+		(69_001_000 as Weight)
 			.saturating_add(T::DbWeight::get().reads(3 as Weight))
 			.saturating_add(T::DbWeight::get().writes(3 as Weight))
 	}
@@ -94,7 +94,7 @@
 	// Storage: Fungible TotalSupply (r:1 w:1)
 	// Storage: Fungible Balance (r:1 w:1)
 	fn burn_from() -> Weight {
-		(24_065_000 as Weight)
+		(63_996_000 as Weight)
 			.saturating_add(T::DbWeight::get().reads(3 as Weight))
 			.saturating_add(T::DbWeight::get().writes(3 as Weight))
 	}
@@ -105,16 +105,16 @@
 	// Storage: Fungible TotalSupply (r:1 w:1)
 	// Storage: Fungible Balance (r:1 w:1)
 	fn create_item() -> Weight {
-		(14_407_000 as Weight)
+		(40_678_000 as Weight)
 			.saturating_add(RocksDbWeight::get().reads(2 as Weight))
 			.saturating_add(RocksDbWeight::get().writes(2 as Weight))
 	}
 	// Storage: Fungible TotalSupply (r:1 w:1)
 	// Storage: Fungible Balance (r:4 w:4)
 	fn create_multiple_items_ex(b: u32, ) -> Weight {
-		(13_030_000 as Weight)
-			// Standard Error: 1_000
-			.saturating_add((3_779_000 as Weight).saturating_mul(b as Weight))
+		(13_036_000 as Weight)
+			// Standard Error: 9_000
+			.saturating_add((15_611_000 as Weight).saturating_mul(b as Weight))
 			.saturating_add(RocksDbWeight::get().reads(1 as Weight))
 			.saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(b as Weight)))
 			.saturating_add(RocksDbWeight::get().writes(1 as Weight))
@@ -123,28 +123,27 @@
 	// Storage: Fungible TotalSupply (r:1 w:1)
 	// Storage: Fungible Balance (r:1 w:1)
 	fn burn_item() -> Weight {
-		(15_565_000 as Weight)
+		(42_340_000 as Weight)
 			.saturating_add(RocksDbWeight::get().reads(2 as Weight))
 			.saturating_add(RocksDbWeight::get().writes(2 as Weight))
 	}
-
 	// Storage: Fungible Balance (r:2 w:2)
 	fn transfer() -> Weight {
-		(17_713_000 as Weight)
+		(47_321_000 as Weight)
 			.saturating_add(RocksDbWeight::get().reads(2 as Weight))
 			.saturating_add(RocksDbWeight::get().writes(2 as Weight))
 	}
 	// Storage: Fungible Balance (r:1 w:0)
 	// Storage: Fungible Allowance (r:0 w:1)
 	fn approve() -> Weight {
-		(14_834_000 as Weight)
+		(38_385_000 as Weight)
 			.saturating_add(RocksDbWeight::get().reads(1 as Weight))
 			.saturating_add(RocksDbWeight::get().writes(1 as Weight))
 	}
 	// Storage: Fungible Allowance (r:1 w:1)
 	// Storage: Fungible Balance (r:2 w:2)
 	fn transfer_from() -> Weight {
-		(25_189_000 as Weight)
+		(69_001_000 as Weight)
 			.saturating_add(RocksDbWeight::get().reads(3 as Weight))
 			.saturating_add(RocksDbWeight::get().writes(3 as Weight))
 	}
@@ -152,7 +151,7 @@
 	// Storage: Fungible TotalSupply (r:1 w:1)
 	// Storage: Fungible Balance (r:1 w:1)
 	fn burn_from() -> Weight {
-		(24_065_000 as Weight)
+		(63_996_000 as Weight)
 			.saturating_add(RocksDbWeight::get().reads(3 as Weight))
 			.saturating_add(RocksDbWeight::get().writes(3 as Weight))
 	}
modifiedpallets/scheduler/src/benchmarking.rsdiffbeforeafterboth
--- a/pallets/scheduler/src/benchmarking.rs
+++ b/pallets/scheduler/src/benchmarking.rs
@@ -1,23 +1,6 @@
-// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
-// This file is part of Unique Network.
-
-// Unique Network is free software: you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation, either version 3 of the License, or
-// (at your option) any later version.
-
-// Unique Network is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-// GNU General Public License for more details.
-
-// You should have received a copy of the GNU General Public License
-// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
-
-// Original license
 // This file is part of Substrate.
 
-// Copyright (C) 2020-2021 Parity Technologies (UK) Ltd.
+// Copyright (C) 2020-2022 Parity Technologies (UK) Ltd.
 // SPDX-License-Identifier: Apache-2.0
 
 // Licensed under the Apache License, Version 2.0 (the "License");
@@ -33,91 +16,203 @@
 // limitations under the License.
 
 //! Scheduler pallet benchmarking.
-
-#![cfg(feature = "runtime-benchmarks")]
 
 use super::*;
-use sp_std::{vec, prelude::*};
+use frame_benchmarking::benchmarks;
+use frame_support::{
+	ensure,
+	traits::{OnInitialize, PreimageProvider, PreimageRecipient},
+};
 use frame_system::RawOrigin;
-use frame_support::{ensure, traits::OnInitialize};
-use frame_benchmarking::{benchmarks, impl_benchmark_test_suite};
+use sp_runtime::traits::Hash;
+use sp_std::{prelude::*, vec};
 
-use crate::Module as Scheduler;
+use crate::Pallet as Scheduler;
 use frame_system::Pallet as System;
 
 const BLOCK_NUMBER: u32 = 2;
 
-// Add `n` named items to the schedule
-fn fill_schedule<T: Config>(when: T::BlockNumber, n: u32) -> Result<(), &'static str> {
-	// Essentially a no-op call.
-	let call = frame_system::Call::set_storage { items: vec![] };
+/// Add `n` named items to the schedule.
+///
+/// For `resolved`:
+/// - `None`: aborted (hash without preimage)
+/// - `Some(true)`: hash resolves into call if possible, plain call otherwise
+/// - `Some(false)`: plain call
+fn fill_schedule<T: Config>(
+	when: T::BlockNumber,
+	n: u32,
+	periodic: bool,
+	named: bool,
+	resolved: Option<bool>,
+) -> Result<(), &'static str> {
 	for i in 0..n {
 		// Named schedule is strictly heavier than anonymous
-		Scheduler::<T>::do_schedule_named(
-			i.encode(),
-			DispatchTime::At(when),
-			// Add periodicity
-			Some((T::BlockNumber::one(), 100)),
-			// HARD_DEADLINE priority means it gets executed no matter what
-			0,
-			frame_system::RawOrigin::Root.into(),
-			call.clone().into(),
-		)?;
+		let (call, hash) = call_and_hash::<T>(i);
+		let call_or_hash = match resolved {
+			Some(true) => {
+				T::PreimageProvider::note_preimage(call.encode().try_into().unwrap());
+				if T::PreimageProvider::have_preimage(&hash) {
+					CallOrHashOf::<T>::Hash(hash)
+				} else {
+					call.into()
+				}
+			},
+			Some(false) => call.into(),
+			None => CallOrHashOf::<T>::Hash(hash),
+		};
+		let period = match periodic {
+			true => Some(((i + 100).into(), 100)),
+			false => None,
+		};
+		let t = DispatchTime::At(when);
+		let origin = frame_system::RawOrigin::Root.into();
+		if named {
+			Scheduler::<T>::do_schedule_named(i.encode().try_into().unwrap_or([0; MAX_TASK_ID_LENGTH_IN_BYTES as usize]),
+			t, period, 0, origin, call_or_hash)?;
+		} else {
+			Scheduler::<T>::do_schedule(t, period, 0, origin, call_or_hash)?;
+		}
 	}
-	ensure!(
-		Agenda::<T>::get(when).len() == n as usize,
-		"didn't fill schedule"
-	);
+	ensure!(Agenda::<T>::get(when).len() == n as usize, "didn't fill schedule");
 	Ok(())
 }
 
+fn call_and_hash<T: Config>(i: u32) -> (<T as Config>::Call, T::Hash) {
+	// Essentially a no-op call.
+	let call: <T as Config>::Call = frame_system::Call::remark { remark: i.encode() }.into();
+	let hash = T::Hashing::hash_of(&call);
+	(call, hash)
+}
+
+
 benchmarks! {
-	schedule {
-		let s in 0 .. T::MaxScheduledPerBlock::get();
+	on_initialize_periodic_named_resolved {
+		let s in 1 .. T::MaxScheduledPerBlock::get();
+		let when = BLOCK_NUMBER.into();
+		fill_schedule::<T>(when, s, true, true, Some(true))?;
+	}: { Scheduler::<T>::on_initialize(BLOCK_NUMBER.into()); }
+	verify {
+		assert_eq!(System::<T>::event_count(), s * 2);
+		for i in 0..s {
+			assert_eq!(Agenda::<T>::get(when + (i + 100).into()).len(), 1 as usize);
+		}
+	}
+
+	on_initialize_named_resolved {
+		let s in 1 .. T::MaxScheduledPerBlock::get();
+		let when = BLOCK_NUMBER.into();
+		fill_schedule::<T>(when, s, false, true, Some(true))?;
+	}: { Scheduler::<T>::on_initialize(BLOCK_NUMBER.into()); }
+	verify {
+		assert_eq!(System::<T>::event_count(), s * 2);
+		assert!(Agenda::<T>::iter().count() == 0);
+	}
+
+	on_initialize_periodic_resolved {
+		let s in 1 .. T::MaxScheduledPerBlock::get();
+		let when = BLOCK_NUMBER.into();
+		fill_schedule::<T>(when, s, true, false, Some(true))?;
+	}: { Scheduler::<T>::on_initialize(BLOCK_NUMBER.into()); }
+	verify {
+		assert_eq!(System::<T>::event_count(), s * 2);
+		for i in 0..s {
+			assert_eq!(Agenda::<T>::get(when + (i + 100).into()).len(), 1 as usize);
+		}
+	}
+
+	on_initialize_resolved {
+		let s in 1 .. T::MaxScheduledPerBlock::get();
 		let when = BLOCK_NUMBER.into();
-		let periodic = Some((T::BlockNumber::one(), 100));
-		let priority = 0;
-		// Essentially a no-op call.
-		let call = Box::new(frame_system::Call::set_storage { items: vec![] }.into());
+		fill_schedule::<T>(when, s, false, false, Some(true))?;
+	}: { Scheduler::<T>::on_initialize(BLOCK_NUMBER.into()); }
+	verify {
+		assert_eq!(System::<T>::event_count(), s * 2);
+		assert!(Agenda::<T>::iter().count() == 0);
+	}
 
-		fill_schedule::<T>(when, s)?;
-	}: _(RawOrigin::Root, when, periodic, priority, call)
+	on_initialize_named_aborted {
+		let s in 1 .. T::MaxScheduledPerBlock::get();
+		let when = BLOCK_NUMBER.into();
+		fill_schedule::<T>(when, s, false, true, None)?;
+	}: { Scheduler::<T>::on_initialize(BLOCK_NUMBER.into()); }
 	verify {
-		ensure!(
-			Agenda::<T>::get(when).len() == (s + 1) as usize,
-			"didn't add to schedule"
-		);
+		assert_eq!(System::<T>::event_count(), 0);
+		if let Some(delay) = T::NoPreimagePostponement::get() {
+			assert_eq!(Agenda::<T>::get(when + delay).len(), s as usize);
+		} else {
+			assert!(Agenda::<T>::iter().count() == 0);
+		}
+	}
+
+	on_initialize_aborted {
+		let s in 1 .. T::MaxScheduledPerBlock::get();
+		let when = BLOCK_NUMBER.into();
+		fill_schedule::<T>(when, s, false, false, None)?;
+	}: { Scheduler::<T>::on_initialize(BLOCK_NUMBER.into()); }
+	verify {
+		assert_eq!(System::<T>::event_count(), 0);
+		if let Some(delay) = T::NoPreimagePostponement::get() {
+			assert_eq!(Agenda::<T>::get(when + delay).len(), s as usize);
+		} else {
+			assert!(Agenda::<T>::iter().count() == 0);
+		}
+	}
+
+	on_initialize_periodic_named {
+		let s in 1 .. T::MaxScheduledPerBlock::get();
+		let when = BLOCK_NUMBER.into();
+		fill_schedule::<T>(when, s, true, true, Some(false))?;
+	}: { Scheduler::<T>::on_initialize(BLOCK_NUMBER.into()); }
+	verify {
+		assert_eq!(System::<T>::event_count(), s);
+		for i in 0..s {
+			assert_eq!(Agenda::<T>::get(when + (i + 100).into()).len(), 1 as usize);
+		}
+	}
+
+	on_initialize_periodic {
+		let s in 1 .. T::MaxScheduledPerBlock::get();
+		let when = BLOCK_NUMBER.into();
+		fill_schedule::<T>(when, s, true, false, Some(false))?;
+	}: { Scheduler::<T>::on_initialize(when); }
+	verify {
+		assert_eq!(System::<T>::event_count(), s);
+		for i in 0..s {
+			assert_eq!(Agenda::<T>::get(when + (i + 100).into()).len(), 1 as usize);
+		}
 	}
 
-	cancel {
+	on_initialize_named {
 		let s in 1 .. T::MaxScheduledPerBlock::get();
 		let when = BLOCK_NUMBER.into();
+		fill_schedule::<T>(when, s, false, true, Some(false))?;
+	}: { Scheduler::<T>::on_initialize(BLOCK_NUMBER.into()); }
+	verify {
+		assert_eq!(System::<T>::event_count(), s);
+		assert!(Agenda::<T>::iter().count() == 0);
+	}
 
-		fill_schedule::<T>(when, s)?;
-		assert_eq!(Agenda::<T>::get(when).len(), s as usize);
-	}: _(RawOrigin::Root, when, 0)
+	on_initialize {
+		let s in 1 .. T::MaxScheduledPerBlock::get();
+		let when = BLOCK_NUMBER.into();
+		fill_schedule::<T>(when, s, false, false, Some(false))?;
+	}: { Scheduler::<T>::on_initialize(BLOCK_NUMBER.into()); }
 	verify {
-		ensure!(
-			Lookup::<T>::get(0.encode()).is_none(),
-			"didn't remove from lookup"
-		);
-		// Removed schedule is NONE
-		ensure!(
-			Agenda::<T>::get(when)[0].is_none(),
-			"didn't remove from schedule"
-		);
+		assert_eq!(System::<T>::event_count(), s);
+		assert!(Agenda::<T>::iter().count() == 0);
 	}
 
 	schedule_named {
 		let s in 0 .. T::MaxScheduledPerBlock::get();
-		let id = s.encode();
+		let id = s.encode().try_into().unwrap_or([0; MAX_TASK_ID_LENGTH_IN_BYTES as usize]);
 		let when = BLOCK_NUMBER.into();
 		let periodic = Some((T::BlockNumber::one(), 100));
 		let priority = 0;
 		// Essentially a no-op call.
-		let call = Box::new(frame_system::Call::set_storage { items: vec![] }.into());
+		let inner_call = frame_system::Call::set_storage { items: vec![] }.into();
+		let call = Box::new(CallOrHashOf::<T>::Value(inner_call));
 
-		fill_schedule::<T>(when, s)?;
+		fill_schedule::<T>(when, s, true, true, Some(false))?;
 	}: _(RawOrigin::Root, id, when, periodic, priority, call)
 	verify {
 		ensure!(
@@ -129,12 +224,13 @@
 	cancel_named {
 		let s in 1 .. T::MaxScheduledPerBlock::get();
 		let when = BLOCK_NUMBER.into();
+		let id = 0.encode().try_into().unwrap_or([0; MAX_TASK_ID_LENGTH_IN_BYTES as usize]);
 
-		fill_schedule::<T>(when, s)?;
-	}: _(RawOrigin::Root, 0.encode())
+		fill_schedule::<T>(when, s, true, true, Some(false))?;
+	}: _(RawOrigin::Root, id)
 	verify {
 		ensure!(
-			Lookup::<T>::get(0.encode()).is_none(),
+			Lookup::<T>::get(id).is_none(),
 			"didn't remove from lookup"
 		);
 		// Removed schedule is NONE
@@ -144,21 +240,5 @@
 		);
 	}
 
-	// TODO [#7141]: Make this more complex and flexible so it can be used in automation.
-	#[extra]
-	on_initialize {
-		let s in 0 .. T::MaxScheduledPerBlock::get();
-		let when = BLOCK_NUMBER.into();
-		fill_schedule::<T>(when, s)?;
-	}: { Scheduler::<T>::on_initialize(BLOCK_NUMBER.into()); }
-	verify {
-		assert_eq!(System::<T>::event_count(), s);
-		// Next block should have all the schedules again
-		ensure!(
-			Agenda::<T>::get(when + T::BlockNumber::one()).len() == s as usize,
-			"didn't append schedule"
-		);
-	}
+	impl_benchmark_test_suite!(Scheduler, crate::mock::new_test_ext(), crate::mock::Test);
 }
-
-impl_benchmark_test_suite!(Scheduler, crate::tests::new_test_ext(), crate::tests::Test,);
modifiedpallets/scheduler/src/lib.rsdiffbeforeafterboth
60// Ensure we're `no_std` when compiling for Wasm.60// Ensure we're `no_std` when compiling for Wasm.
61#![cfg_attr(not(feature = "std"), no_std)]61#![cfg_attr(not(feature = "std"), no_std)]
6262
63// FIXME
64// #[cfg(feature = "runtime-benchmarks")]63#[cfg(feature = "runtime-benchmarks")]
65// mod benchmarking;64mod benchmarking;
6665
67pub mod weights;66pub mod weights;
6867
640 Ok((when, index))639 Ok((when, index))
641 }640 }
642641
643 fn do_cancel(
644 origin: Option<T::PalletsOrigin>,
645 (when, index): TaskAddress<T::BlockNumber>,
646 ) -> Result<(), DispatchError> {
647 let scheduled = Agenda::<T>::try_mutate(when, |agenda| {
648 agenda.get_mut(index as usize).map_or(
649 Ok(None),
650 |s| -> Result<Option<Scheduled<_, _, _, _>>, DispatchError> {
651 if let (Some(ref o), Some(ref s)) = (origin, s.borrow()) {
652 if matches!(
653 T::OriginPrivilegeCmp::cmp_privilege(o, &s.origin),
654 Some(Ordering::Less) | None
655 ) {
656 return Err(BadOrigin.into());
657 }
658 };
659 Ok(s.take())
660 },
661 )
662 })?;
663 if let Some(s) = scheduled {
664 s.call.ensure_unrequested::<T::PreimageProvider>();
665 if let Some(id) = s.maybe_id {
666 Lookup::<T>::remove(id);
667 }
668 Self::deposit_event(Event::Canceled { when, index });
669 Ok(())
670 } else {
671 Err(Error::<T>::NotFound)?
672 }
673 }
674
675 fn do_reschedule(
676 (when, index): TaskAddress<T::BlockNumber>,
677 new_time: DispatchTime<T::BlockNumber>,
678 ) -> Result<TaskAddress<T::BlockNumber>, DispatchError> {
679 let new_time = Self::resolve_time(new_time)?;
680
681 if new_time == when {
682 return Err(Error::<T>::RescheduleNoChange.into());
683 }
684
685 Agenda::<T>::try_mutate(when, |agenda| -> DispatchResult {
686 let task = agenda.get_mut(index as usize).ok_or(Error::<T>::NotFound)?;
687 let task = task.take().ok_or(Error::<T>::NotFound)?;
688 Agenda::<T>::append(new_time, Some(task));
689 Ok(())
690 })?;
691
692 let new_index = Agenda::<T>::decode_len(new_time).unwrap_or(1) as u32 - 1;
693 Self::deposit_event(Event::Canceled { when, index });
694 Self::deposit_event(Event::Scheduled {
695 when: new_time,
696 index: new_index,
697 });
698
699 Ok((new_time, new_index))
700 }
701
702 fn do_schedule_named(642 fn do_schedule_named(
703 id: ScheduledId,643 id: ScheduledId,
704 when: DispatchTime<T::BlockNumber>,644 when: DispatchTime<T::BlockNumber>,
791 })731 })
792 }732 }
793
794 fn do_reschedule_named(
795 id: ScheduledId,
796 new_time: DispatchTime<T::BlockNumber>,
797 ) -> Result<TaskAddress<T::BlockNumber>, DispatchError> {
798 let new_time = Self::resolve_time(new_time)?;
799
800 Lookup::<T>::try_mutate_exists(
801 id,
802 |lookup| -> Result<TaskAddress<T::BlockNumber>, DispatchError> {
803 let (when, index) = lookup.ok_or(Error::<T>::NotFound)?;
804
805 if new_time == when {
806 return Err(Error::<T>::RescheduleNoChange.into());
807 }
808
809 Agenda::<T>::try_mutate(when, |agenda| -> DispatchResult {
810 let task = agenda.get_mut(index as usize).ok_or(Error::<T>::NotFound)?;
811 let task = task.take().ok_or(Error::<T>::NotFound)?;
812 Agenda::<T>::append(new_time, Some(task));
813
814 Ok(())
815 })?;
816
817 let new_index = Agenda::<T>::decode_len(new_time).unwrap_or(1) as u32 - 1;
818 Self::deposit_event(Event::Canceled { when, index });
819 Self::deposit_event(Event::Scheduled {
820 when: new_time,
821 index: new_index,
822 });
823
824 *lookup = Some((new_time, new_index));
825
826 Ok((new_time, new_index))
827 },
828 )
829 }
830}
831
832impl<T: Config> schedule::v2::Anon<T::BlockNumber, <T as Config>::Call, T::PalletsOrigin>
833 for Pallet<T>
834{
835 type Address = TaskAddress<T::BlockNumber>;
836 type Hash = T::Hash;
837
838 fn schedule(
839 when: DispatchTime<T::BlockNumber>,
840 maybe_periodic: Option<schedule::Period<T::BlockNumber>>,
841 priority: schedule::Priority,
842 origin: T::PalletsOrigin,
843 call: CallOrHashOf<T>,
844 ) -> Result<Self::Address, DispatchError> {
845 Self::do_schedule(when, maybe_periodic, priority, origin, call)
846 }
847
848 fn cancel((when, index): Self::Address) -> Result<(), ()> {
849 Self::do_cancel(None, (when, index)).map_err(|_| ())
850 }
851
852 fn reschedule(
853 address: Self::Address,
854 when: DispatchTime<T::BlockNumber>,
855 ) -> Result<Self::Address, DispatchError> {
856 Self::do_reschedule(address, when)
857 }
858
859 fn next_dispatch_time((when, index): Self::Address) -> Result<T::BlockNumber, ()> {
860 Agenda::<T>::get(when)
861 .get(index as usize)
862 .ok_or(())
863 .map(|_| when)
864 }
865}
866
867impl<T: Config> schedule::v2::Named<T::BlockNumber, <T as Config>::Call, T::PalletsOrigin>
868 for Pallet<T>
869{
870 type Address = TaskAddress<T::BlockNumber>;
871 type Hash = T::Hash;
872
873 fn schedule_named(
874 id: Vec<u8>,
875 when: DispatchTime<T::BlockNumber>,
876 maybe_periodic: Option<schedule::Period<T::BlockNumber>>,
877 priority: schedule::Priority,
878 origin: T::PalletsOrigin,
879 call: CallOrHashOf<T>,
880 ) -> Result<Self::Address, ()> {
881 let inner_id: ScheduledId = id
882 .try_into()
883 .unwrap_or([0; MAX_TASK_ID_LENGTH_IN_BYTES as usize]);
884 Self::do_schedule_named(inner_id, when, maybe_periodic, priority, origin, call)
885 .map_err(|_| ())
886 }
887
888 fn cancel_named(id: Vec<u8>) -> Result<(), ()> {
889 let inner_id: ScheduledId = id
890 .try_into()
891 .unwrap_or([0; MAX_TASK_ID_LENGTH_IN_BYTES as usize]);
892 Self::do_cancel_named(None, inner_id).map_err(|_| ())
893 }
894
895 fn reschedule_named(
896 id: Vec<u8>,
897 when: DispatchTime<T::BlockNumber>,
898 ) -> Result<Self::Address, DispatchError> {
899 let inner_id: ScheduledId = id
900 .try_into()
901 .unwrap_or([0; MAX_TASK_ID_LENGTH_IN_BYTES as usize]);
902 Self::do_reschedule_named(inner_id, when)
903 }
904
905 fn next_dispatch_time(id: Vec<u8>) -> Result<T::BlockNumber, ()> {
906 let inner_id: ScheduledId = id
907 .try_into()
908 .unwrap_or([0; MAX_TASK_ID_LENGTH_IN_BYTES as usize]);
909 Lookup::<T>::get(inner_id)
910 .and_then(|(when, index)| Agenda::<T>::get(when).get(index as usize).map(|_| when))
911 .ok_or(())
912 }
913}
914733