difftreelog
Scheduler benchmarks fixed. Useless code in pallet was removed.
in: master
4 files changed
Makefilediffbeforeafterboth93bench-structure:93bench-structure:94 make _bench PALLET=structure94 make _bench PALLET=structure9596.PHONY: bench-scheduler97bench-scheduler:98 make _bench PALLET=unq-scheduler 959996.PHONY: bench100.PHONY: bench97bench: bench-evm-migration bench-unique bench-structure bench-fungible bench-refungible bench-nonfungible101bench: bench-evm-migration bench-unique bench-structure bench-fungible bench-refungible bench-nonfungible bench-scheduler98102pallets/fungible/src/weights.rsdiffbeforeafterboth3//! Autogenerated weights for pallet_fungible3//! Autogenerated weights for pallet_fungible4//!4//!5//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev5//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev6//! DATE: 2022-03-01, STEPS: `50`, REPEAT: 200, LOW RANGE: `[]`, HIGH RANGE: `[]`6//! DATE: 2022-06-09, STEPS: `50`, REPEAT: 200, LOW RANGE: `[]`, HIGH RANGE: `[]`7//! EXECUTION: None, WASM-EXECUTION: Compiled, CHAIN: None, DB CACHE: 10247//! EXECUTION: None, WASM-EXECUTION: Compiled, CHAIN: None, DB CACHE: 1024889// Executed Command:9// Executed Command:10// target/release/unique-collator10// target/release/unique-collator11// benchmark11// benchmark12// pallet12// --pallet13// --pallet13// pallet-fungible14// pallet-fungible14// --wasm-execution15// --wasm-execution47 // Storage: Fungible TotalSupply (r:1 w:1)48 // Storage: Fungible TotalSupply (r:1 w:1)48 // Storage: Fungible Balance (r:1 w:1)49 // Storage: Fungible Balance (r:1 w:1)49 fn create_item() -> Weight {50 fn create_item() -> Weight {50 (14_407_000 as Weight)51 (40_678_000 as Weight)51 .saturating_add(T::DbWeight::get().reads(2 as Weight))52 .saturating_add(T::DbWeight::get().reads(2 as Weight))52 .saturating_add(T::DbWeight::get().writes(2 as Weight))53 .saturating_add(T::DbWeight::get().writes(2 as Weight))53 }54 }54 // Storage: Fungible TotalSupply (r:1 w:1)55 // Storage: Fungible TotalSupply (r:1 w:1)55 // Storage: Fungible Balance (r:4 w:4)56 // Storage: Fungible Balance (r:4 w:4)56 fn create_multiple_items_ex(b: u32, ) -> Weight {57 fn create_multiple_items_ex(b: u32, ) -> Weight {57 (13_030_000 as Weight)58 (13_036_000 as Weight)58 // Standard Error: 1_00059 // Standard Error: 9_00059 .saturating_add((3_779_000 as Weight).saturating_mul(b as Weight))60 .saturating_add((15_611_000 as Weight).saturating_mul(b as Weight))60 .saturating_add(T::DbWeight::get().reads(1 as Weight))61 .saturating_add(T::DbWeight::get().reads(1 as Weight))61 .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(b as Weight)))62 .saturating_add(T::DbWeight::get().reads((1 as Weight).saturating_mul(b as Weight)))62 .saturating_add(T::DbWeight::get().writes(1 as Weight))63 .saturating_add(T::DbWeight::get().writes(1 as Weight))65 // Storage: Fungible TotalSupply (r:1 w:1)66 // Storage: Fungible TotalSupply (r:1 w:1)66 // Storage: Fungible Balance (r:1 w:1)67 // Storage: Fungible Balance (r:1 w:1)67 fn burn_item() -> Weight {68 fn burn_item() -> Weight {68 (15_565_000 as Weight)69 (42_340_000 as Weight)69 .saturating_add(T::DbWeight::get().reads(2 as Weight))70 .saturating_add(T::DbWeight::get().reads(2 as Weight))70 .saturating_add(T::DbWeight::get().writes(2 as Weight))71 .saturating_add(T::DbWeight::get().writes(2 as Weight))71 }72 }7273 // Storage: Fungible Balance (r:2 w:2)73 // Storage: Fungible Balance (r:2 w:2)74 fn transfer() -> Weight {74 fn transfer() -> Weight {75 (17_713_000 as Weight)75 (47_321_000 as Weight)76 .saturating_add(T::DbWeight::get().reads(2 as Weight))76 .saturating_add(T::DbWeight::get().reads(2 as Weight))77 .saturating_add(T::DbWeight::get().writes(2 as Weight))77 .saturating_add(T::DbWeight::get().writes(2 as Weight))78 }78 }79 // Storage: Fungible Balance (r:1 w:0)79 // Storage: Fungible Balance (r:1 w:0)80 // Storage: Fungible Allowance (r:0 w:1)80 // Storage: Fungible Allowance (r:0 w:1)81 fn approve() -> Weight {81 fn approve() -> Weight {82 (14_834_000 as Weight)82 (38_385_000 as Weight)83 .saturating_add(T::DbWeight::get().reads(1 as Weight))83 .saturating_add(T::DbWeight::get().reads(1 as Weight))84 .saturating_add(T::DbWeight::get().writes(1 as Weight))84 .saturating_add(T::DbWeight::get().writes(1 as Weight))85 }85 }86 // Storage: Fungible Allowance (r:1 w:1)86 // Storage: Fungible Allowance (r:1 w:1)87 // Storage: Fungible Balance (r:2 w:2)87 // Storage: Fungible Balance (r:2 w:2)88 fn transfer_from() -> Weight {88 fn transfer_from() -> Weight {89 (25_189_000 as Weight)89 (69_001_000 as Weight)90 .saturating_add(T::DbWeight::get().reads(3 as Weight))90 .saturating_add(T::DbWeight::get().reads(3 as Weight))91 .saturating_add(T::DbWeight::get().writes(3 as Weight))91 .saturating_add(T::DbWeight::get().writes(3 as Weight))92 }92 }93 // Storage: Fungible Allowance (r:1 w:1)93 // Storage: Fungible Allowance (r:1 w:1)94 // Storage: Fungible TotalSupply (r:1 w:1)94 // Storage: Fungible TotalSupply (r:1 w:1)95 // Storage: Fungible Balance (r:1 w:1)95 // Storage: Fungible Balance (r:1 w:1)96 fn burn_from() -> Weight {96 fn burn_from() -> Weight {97 (24_065_000 as Weight)97 (63_996_000 as Weight)98 .saturating_add(T::DbWeight::get().reads(3 as Weight))98 .saturating_add(T::DbWeight::get().reads(3 as Weight))99 .saturating_add(T::DbWeight::get().writes(3 as Weight))99 .saturating_add(T::DbWeight::get().writes(3 as Weight))100 }100 }105 // Storage: Fungible TotalSupply (r:1 w:1)105 // Storage: Fungible TotalSupply (r:1 w:1)106 // Storage: Fungible Balance (r:1 w:1)106 // Storage: Fungible Balance (r:1 w:1)107 fn create_item() -> Weight {107 fn create_item() -> Weight {108 (14_407_000 as Weight)108 (40_678_000 as Weight)109 .saturating_add(RocksDbWeight::get().reads(2 as Weight))109 .saturating_add(RocksDbWeight::get().reads(2 as Weight))110 .saturating_add(RocksDbWeight::get().writes(2 as Weight))110 .saturating_add(RocksDbWeight::get().writes(2 as Weight))111 }111 }112 // Storage: Fungible TotalSupply (r:1 w:1)112 // Storage: Fungible TotalSupply (r:1 w:1)113 // Storage: Fungible Balance (r:4 w:4)113 // Storage: Fungible Balance (r:4 w:4)114 fn create_multiple_items_ex(b: u32, ) -> Weight {114 fn create_multiple_items_ex(b: u32, ) -> Weight {115 (13_030_000 as Weight)115 (13_036_000 as Weight)116 // Standard Error: 1_000116 // Standard Error: 9_000117 .saturating_add((3_779_000 as Weight).saturating_mul(b as Weight))117 .saturating_add((15_611_000 as Weight).saturating_mul(b as Weight))118 .saturating_add(RocksDbWeight::get().reads(1 as Weight))118 .saturating_add(RocksDbWeight::get().reads(1 as Weight))119 .saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(b as Weight)))119 .saturating_add(RocksDbWeight::get().reads((1 as Weight).saturating_mul(b as Weight)))120 .saturating_add(RocksDbWeight::get().writes(1 as Weight))120 .saturating_add(RocksDbWeight::get().writes(1 as Weight))123 // Storage: Fungible TotalSupply (r:1 w:1)123 // Storage: Fungible TotalSupply (r:1 w:1)124 // Storage: Fungible Balance (r:1 w:1)124 // Storage: Fungible Balance (r:1 w:1)125 fn burn_item() -> Weight {125 fn burn_item() -> Weight {126 (15_565_000 as Weight)126 (42_340_000 as Weight)127 .saturating_add(RocksDbWeight::get().reads(2 as Weight))127 .saturating_add(RocksDbWeight::get().reads(2 as Weight))128 .saturating_add(RocksDbWeight::get().writes(2 as Weight))128 .saturating_add(RocksDbWeight::get().writes(2 as Weight))129 }129 }130131 // Storage: Fungible Balance (r:2 w:2)130 // Storage: Fungible Balance (r:2 w:2)132 fn transfer() -> Weight {131 fn transfer() -> Weight {133 (17_713_000 as Weight)132 (47_321_000 as Weight)134 .saturating_add(RocksDbWeight::get().reads(2 as Weight))133 .saturating_add(RocksDbWeight::get().reads(2 as Weight))135 .saturating_add(RocksDbWeight::get().writes(2 as Weight))134 .saturating_add(RocksDbWeight::get().writes(2 as Weight))136 }135 }137 // Storage: Fungible Balance (r:1 w:0)136 // Storage: Fungible Balance (r:1 w:0)138 // Storage: Fungible Allowance (r:0 w:1)137 // Storage: Fungible Allowance (r:0 w:1)139 fn approve() -> Weight {138 fn approve() -> Weight {140 (14_834_000 as Weight)139 (38_385_000 as Weight)141 .saturating_add(RocksDbWeight::get().reads(1 as Weight))140 .saturating_add(RocksDbWeight::get().reads(1 as Weight))142 .saturating_add(RocksDbWeight::get().writes(1 as Weight))141 .saturating_add(RocksDbWeight::get().writes(1 as Weight))143 }142 }144 // Storage: Fungible Allowance (r:1 w:1)143 // Storage: Fungible Allowance (r:1 w:1)145 // Storage: Fungible Balance (r:2 w:2)144 // Storage: Fungible Balance (r:2 w:2)146 fn transfer_from() -> Weight {145 fn transfer_from() -> Weight {147 (25_189_000 as Weight)146 (69_001_000 as Weight)148 .saturating_add(RocksDbWeight::get().reads(3 as Weight))147 .saturating_add(RocksDbWeight::get().reads(3 as Weight))149 .saturating_add(RocksDbWeight::get().writes(3 as Weight))148 .saturating_add(RocksDbWeight::get().writes(3 as Weight))150 }149 }151 // Storage: Fungible Allowance (r:1 w:1)150 // Storage: Fungible Allowance (r:1 w:1)152 // Storage: Fungible TotalSupply (r:1 w:1)151 // Storage: Fungible TotalSupply (r:1 w:1)153 // Storage: Fungible Balance (r:1 w:1)152 // Storage: Fungible Balance (r:1 w:1)154 fn burn_from() -> Weight {153 fn burn_from() -> Weight {155 (24_065_000 as Weight)154 (63_996_000 as Weight)156 .saturating_add(RocksDbWeight::get().reads(3 as Weight))155 .saturating_add(RocksDbWeight::get().reads(3 as Weight))157 .saturating_add(RocksDbWeight::get().writes(3 as Weight))156 .saturating_add(RocksDbWeight::get().writes(3 as Weight))158 }157 }pallets/scheduler/src/benchmarking.rsdiffbeforeafterboth1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617// Original license18// This file is part of Substrate.1// This file is part of Substrate.19220// Copyright (C) 2020-2021 Parity Technologies (UK) Ltd.3// Copyright (C) 2020-2022 Parity Technologies (UK) Ltd.21// SPDX-License-Identifier: Apache-2.04// SPDX-License-Identifier: Apache-2.022523// Licensed under the Apache License, Version 2.0 (the "License");6// Licensed under the Apache License, Version 2.0 (the "License");341735//! Scheduler pallet benchmarking.18//! Scheduler pallet benchmarking.3637#![cfg(feature = "runtime-benchmarks")]381939use super::*;20use super::*;21use frame_benchmarking::benchmarks;40use sp_std::{vec, prelude::*};22use frame_support::{23 ensure,24 traits::{OnInitialize, PreimageProvider, PreimageRecipient},25};41use frame_system::RawOrigin;26use frame_system::RawOrigin;42use frame_support::{ensure, traits::OnInitialize};27use sp_runtime::traits::Hash;43use frame_benchmarking::{benchmarks, impl_benchmark_test_suite};28use sp_std::{prelude::*, vec};442945use crate::Module as Scheduler;30use crate::Pallet as Scheduler;46use frame_system::Pallet as System;31use frame_system::Pallet as System;473248const BLOCK_NUMBER: u32 = 2;33const BLOCK_NUMBER: u32 = 2;493450// Add `n` named items to the schedule35/// Add `n` named items to the schedule.36///37/// For `resolved`:38/// - `None`: aborted (hash without preimage)39/// - `Some(true)`: hash resolves into call if possible, plain call otherwise40/// - `Some(false)`: plain call51fn fill_schedule<T: Config>(when: T::BlockNumber, n: u32) -> Result<(), &'static str> {41fn fill_schedule<T: Config>(52 // Essentially a no-op call.42 when: T::BlockNumber,43 n: u32,44 periodic: bool,45 named: bool,46 resolved: Option<bool>,47) -> Result<(), &'static str> {48 for i in 0..n {49 // Named schedule is strictly heavier than anonymous50 let (call, hash) = call_and_hash::<T>(i);53 let call = frame_system::Call::set_storage { items: vec![] };51 let call_or_hash = match resolved {52 Some(true) => {53 T::PreimageProvider::note_preimage(call.encode().try_into().unwrap());54 if T::PreimageProvider::have_preimage(&hash) {55 CallOrHashOf::<T>::Hash(hash)56 } else {57 call.into()58 }59 },60 Some(false) => call.into(),61 None => CallOrHashOf::<T>::Hash(hash),62 };63 let period = match periodic {64 true => Some(((i + 100).into(), 100)),65 false => None,66 };67 let t = DispatchTime::At(when);68 let origin = frame_system::RawOrigin::Root.into();54 for i in 0..n {69 if named {55 // Named schedule is strictly heavier than anonymous56 Scheduler::<T>::do_schedule_named(70 Scheduler::<T>::do_schedule_named(i.encode().try_into().unwrap_or([0; MAX_TASK_ID_LENGTH_IN_BYTES as usize]),57 i.encode(),58 DispatchTime::At(when),59 // Add periodicity60 Some((T::BlockNumber::one(), 100)),61 // HARD_DEADLINE priority means it gets executed no matter what62 0,71 t, period, 0, origin, call_or_hash)?;63 frame_system::RawOrigin::Root.into(),64 call.clone().into(),65 )?;66 }72 } else {73 Scheduler::<T>::do_schedule(t, period, 0, origin, call_or_hash)?;74 }75 }67 ensure!(76 ensure!(Agenda::<T>::get(when).len() == n as usize, "didn't fill schedule");68 Agenda::<T>::get(when).len() == n as usize,69 "didn't fill schedule"70 );71 Ok(())77 Ok(())72}78}7980fn call_and_hash<T: Config>(i: u32) -> (<T as Config>::Call, T::Hash) {81 // Essentially a no-op call.82 let call: <T as Config>::Call = frame_system::Call::remark { remark: i.encode() }.into();83 let hash = T::Hashing::hash_of(&call);84 (call, hash)85}86738774benchmarks! {88benchmarks! {75 schedule {89 on_initialize_periodic_named_resolved {76 let s in 0 .. T::MaxScheduledPerBlock::get();90 let s in 1 .. T::MaxScheduledPerBlock::get();91 let when = BLOCK_NUMBER.into();92 fill_schedule::<T>(when, s, true, true, Some(true))?;93 }: { Scheduler::<T>::on_initialize(BLOCK_NUMBER.into()); }94 verify {95 assert_eq!(System::<T>::event_count(), s * 2);96 for i in 0..s {97 assert_eq!(Agenda::<T>::get(when + (i + 100).into()).len(), 1 as usize);98 }99 }100101 on_initialize_named_resolved {102 let s in 1 .. T::MaxScheduledPerBlock::get();77 let when = BLOCK_NUMBER.into();103 let when = BLOCK_NUMBER.into();104 fill_schedule::<T>(when, s, false, true, Some(true))?;105 }: { Scheduler::<T>::on_initialize(BLOCK_NUMBER.into()); }106 verify {107 assert_eq!(System::<T>::event_count(), s * 2);108 assert!(Agenda::<T>::iter().count() == 0);109 }110111 on_initialize_periodic_resolved {78 let periodic = Some((T::BlockNumber::one(), 100));112 let s in 1 .. T::MaxScheduledPerBlock::get();113 let when = BLOCK_NUMBER.into();114 fill_schedule::<T>(when, s, true, false, Some(true))?;115 }: { Scheduler::<T>::on_initialize(BLOCK_NUMBER.into()); }116 verify {117 assert_eq!(System::<T>::event_count(), s * 2);118 for i in 0..s {119 assert_eq!(Agenda::<T>::get(when + (i + 100).into()).len(), 1 as usize);120 }121 }122123 on_initialize_resolved {124 let s in 1 .. T::MaxScheduledPerBlock::get();79 let priority = 0;125 let when = BLOCK_NUMBER.into();80 // Essentially a no-op call.126 fill_schedule::<T>(when, s, false, false, Some(true))?;127 }: { Scheduler::<T>::on_initialize(BLOCK_NUMBER.into()); }128 verify {129 assert_eq!(System::<T>::event_count(), s * 2);130 assert!(Agenda::<T>::iter().count() == 0);131 }132133 on_initialize_named_aborted {134 let s in 1 .. T::MaxScheduledPerBlock::get();81 let call = Box::new(frame_system::Call::set_storage { items: vec![] }.into());135 let when = BLOCK_NUMBER.into();82136 fill_schedule::<T>(when, s, false, true, None)?;137 }: { Scheduler::<T>::on_initialize(BLOCK_NUMBER.into()); }138 verify {139 assert_eq!(System::<T>::event_count(), 0);140 if let Some(delay) = T::NoPreimagePostponement::get() {141 assert_eq!(Agenda::<T>::get(when + delay).len(), s as usize);142 } else {143 assert!(Agenda::<T>::iter().count() == 0);144 }145 }146147 on_initialize_aborted {148 let s in 1 .. T::MaxScheduledPerBlock::get();149 let when = BLOCK_NUMBER.into();83 fill_schedule::<T>(when, s)?;150 fill_schedule::<T>(when, s, false, false, None)?;84 }: _(RawOrigin::Root, when, periodic, priority, call)151 }: { Scheduler::<T>::on_initialize(BLOCK_NUMBER.into()); }152 verify {153 assert_eq!(System::<T>::event_count(), 0);154 if let Some(delay) = T::NoPreimagePostponement::get() {155 assert_eq!(Agenda::<T>::get(when + delay).len(), s as usize);156 } else {157 assert!(Agenda::<T>::iter().count() == 0);158 }159 }160161 on_initialize_periodic_named {162 let s in 1 .. T::MaxScheduledPerBlock::get();163 let when = BLOCK_NUMBER.into();164 fill_schedule::<T>(when, s, true, true, Some(false))?;165 }: { Scheduler::<T>::on_initialize(BLOCK_NUMBER.into()); }85 verify {166 verify {86 ensure!(167 assert_eq!(System::<T>::event_count(), s);168 for i in 0..s {87 Agenda::<T>::get(when).len() == (s + 1) as usize,169 assert_eq!(Agenda::<T>::get(when + (i + 100).into()).len(), 1 as usize);88 "didn't add to schedule"170 }89 );90 }171 }9117292 cancel {173 on_initialize_periodic {93 let s in 1 .. T::MaxScheduledPerBlock::get();174 let s in 1 .. T::MaxScheduledPerBlock::get();94 let when = BLOCK_NUMBER.into();175 let when = BLOCK_NUMBER.into();9596 fill_schedule::<T>(when, s)?;176 fill_schedule::<T>(when, s, true, false, Some(false))?;177 }: { Scheduler::<T>::on_initialize(when); }178 verify {97 assert_eq!(Agenda::<T>::get(when).len(), s as usize);179 assert_eq!(System::<T>::event_count(), s);180 for i in 0..s {181 assert_eq!(Agenda::<T>::get(when + (i + 100).into()).len(), 1 as usize);182 }183 }184185 on_initialize_named {98 }: _(RawOrigin::Root, when, 0)186 let s in 1 .. T::MaxScheduledPerBlock::get();187 let when = BLOCK_NUMBER.into();188 fill_schedule::<T>(when, s, false, true, Some(false))?;189 }: { Scheduler::<T>::on_initialize(BLOCK_NUMBER.into()); }99 verify {190 verify {100 ensure!(191 assert_eq!(System::<T>::event_count(), s);192 assert!(Agenda::<T>::iter().count() == 0);193 }194195 on_initialize {101 Lookup::<T>::get(0.encode()).is_none(),196 let s in 1 .. T::MaxScheduledPerBlock::get();102 "didn't remove from lookup"197 let when = BLOCK_NUMBER.into();198 fill_schedule::<T>(when, s, false, false, Some(false))?;199 }: { Scheduler::<T>::on_initialize(BLOCK_NUMBER.into()); }200 verify {103 );201 assert_eq!(System::<T>::event_count(), s);104 // Removed schedule is NONE105 ensure!(202 assert!(Agenda::<T>::iter().count() == 0);106 Agenda::<T>::get(when)[0].is_none(),203 }107 "didn't remove from schedule"108 );109 }110204111 schedule_named {205 schedule_named {112 let s in 0 .. T::MaxScheduledPerBlock::get();206 let s in 0 .. T::MaxScheduledPerBlock::get();113 let id = s.encode();207 let id = s.encode().try_into().unwrap_or([0; MAX_TASK_ID_LENGTH_IN_BYTES as usize]);114 let when = BLOCK_NUMBER.into();208 let when = BLOCK_NUMBER.into();115 let periodic = Some((T::BlockNumber::one(), 100));209 let periodic = Some((T::BlockNumber::one(), 100));116 let priority = 0;210 let priority = 0;117 // Essentially a no-op call.211 // Essentially a no-op call.118 let call = Box::new(frame_system::Call::set_storage { items: vec![] }.into());212 let inner_call = frame_system::Call::set_storage { items: vec![] }.into();213 let call = Box::new(CallOrHashOf::<T>::Value(inner_call));119214120 fill_schedule::<T>(when, s)?;215 fill_schedule::<T>(when, s, true, true, Some(false))?;121 }: _(RawOrigin::Root, id, when, periodic, priority, call)216 }: _(RawOrigin::Root, id, when, periodic, priority, call)122 verify {217 verify {123 ensure!(218 ensure!(129 cancel_named {224 cancel_named {130 let s in 1 .. T::MaxScheduledPerBlock::get();225 let s in 1 .. T::MaxScheduledPerBlock::get();131 let when = BLOCK_NUMBER.into();226 let when = BLOCK_NUMBER.into();227 let id = 0.encode().try_into().unwrap_or([0; MAX_TASK_ID_LENGTH_IN_BYTES as usize]);132228133 fill_schedule::<T>(when, s)?;229 fill_schedule::<T>(when, s, true, true, Some(false))?;134 }: _(RawOrigin::Root, 0.encode())230 }: _(RawOrigin::Root, id)135 verify {231 verify {136 ensure!(232 ensure!(137 Lookup::<T>::get(0.encode()).is_none(),233 Lookup::<T>::get(id).is_none(),138 "didn't remove from lookup"234 "didn't remove from lookup"139 );235 );140 // Removed schedule is NONE236 // Removed schedule is NONE144 );240 );145 }241 }146242147 // TODO [#7141]: Make this more complex and flexible so it can be used in automation.148 #[extra]149 on_initialize {150 let s in 0 .. T::MaxScheduledPerBlock::get();151 let when = BLOCK_NUMBER.into();152 fill_schedule::<T>(when, s)?;153 }: { Scheduler::<T>::on_initialize(BLOCK_NUMBER.into()); }243 impl_benchmark_test_suite!(Scheduler, crate::mock::new_test_ext(), crate::mock::Test);154 verify {155 assert_eq!(System::<T>::event_count(), s);156 // Next block should have all the schedules again157 ensure!(158 Agenda::<T>::get(when + T::BlockNumber::one()).len() == s as usize,159 "didn't append schedule"160 );161 }162}244}163164impl_benchmark_test_suite!(Scheduler, crate::tests::new_test_ext(), crate::tests::Test,);165245pallets/scheduler/src/lib.rsdiffbeforeafterboth60// 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)]626263// FIXME64// #[cfg(feature = "runtime-benchmarks")]63#[cfg(feature = "runtime-benchmarks")]65// mod benchmarking;64mod benchmarking;666567pub mod weights;66pub mod weights;6867640 Ok((when, index))639 Ok((when, index))641 }640 }642641643 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) | None655 ) {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 }674675 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)?;680681 if new_time == when {682 return Err(Error::<T>::RescheduleNoChange.into());683 }684685 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 })?;691692 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 });698699 Ok((new_time, new_index))700 }701702 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 }793794 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)?;799800 Lookup::<T>::try_mutate_exists(801 id,802 |lookup| -> Result<TaskAddress<T::BlockNumber>, DispatchError> {803 let (when, index) = lookup.ok_or(Error::<T>::NotFound)?;804805 if new_time == when {806 return Err(Error::<T>::RescheduleNoChange.into());807 }808809 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));813814 Ok(())815 })?;816817 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 });823824 *lookup = Some((new_time, new_index));825826 Ok((new_time, new_index))827 },828 )829 }830}831832impl<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;837838 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 }847848 fn cancel((when, index): Self::Address) -> Result<(), ()> {849 Self::do_cancel(None, (when, index)).map_err(|_| ())850 }851852 fn reschedule(853 address: Self::Address,854 when: DispatchTime<T::BlockNumber>,855 ) -> Result<Self::Address, DispatchError> {856 Self::do_reschedule(address, when)857 }858859 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}866867impl<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;872873 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 = id882 .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 }887888 fn cancel_named(id: Vec<u8>) -> Result<(), ()> {889 let inner_id: ScheduledId = id890 .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 }894895 fn reschedule_named(896 id: Vec<u8>,897 when: DispatchTime<T::BlockNumber>,898 ) -> Result<Self::Address, DispatchError> {899 let inner_id: ScheduledId = id900 .try_into()901 .unwrap_or([0; MAX_TASK_ID_LENGTH_IN_BYTES as usize]);902 Self::do_reschedule_named(inner_id, when)903 }904905 fn next_dispatch_time(id: Vec<u8>) -> Result<T::BlockNumber, ()> {906 let inner_id: ScheduledId = id907 .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