difftreelog
fix require service_task_fetched for Preimages
in: master
2 files changed
pallets/scheduler-v2/src/lib.rsdiffbeforeafterboth--- a/pallets/scheduler-v2/src/lib.rs
+++ b/pallets/scheduler-v2/src/lib.rs
@@ -177,9 +177,23 @@
}
}
+/// Weight Info for the Preimages fetches.
+pub trait SchedulerPreimagesWeightInfo<W: WeightInfo> {
+ /// Get the weight of a task fetches with a given decoded length.
+ fn service_task_fetched(call_length: u32) -> Weight;
+}
+
+impl<W: WeightInfo> SchedulerPreimagesWeightInfo<W> for () {
+ fn service_task_fetched(_call_length: u32) -> Weight {
+ W::service_task_base()
+ }
+}
+
/// A scheduler's interface for managing preimages to hashes
/// and looking up preimages from their hash on-chain.
-pub trait SchedulerPreimages<T: Config>: PreimageRecipient<T::Hash> {
+pub trait SchedulerPreimages<T: Config>:
+ PreimageRecipient<T::Hash> + SchedulerPreimagesWeightInfo<T::WeightInfo>
+{
/// No longer request that the data for decoding the given `call` is available.
fn drop(call: &ScheduledCall<T>);
@@ -200,7 +214,9 @@
) -> Result<(<T as pallet::Config>::RuntimeCall, Option<u32>), DispatchError>;
}
-impl<T: Config, PP: PreimageRecipient<T::Hash>> SchedulerPreimages<T> for PP {
+impl<T: Config, PP: PreimageRecipient<T::Hash> + SchedulerPreimagesWeightInfo<T::WeightInfo>>
+ SchedulerPreimages<T> for PP
+{
fn drop(call: &ScheduledCall<T>) {
match call {
ScheduledCall::Inline(_) => {}
@@ -414,29 +430,25 @@
}
}
-pub(crate) trait MarginalWeightInfo: WeightInfo {
+pub(crate) struct MarginalWeightInfo<T: Config>(sp_std::marker::PhantomData<T>);
+
+impl<T: Config> MarginalWeightInfo<T> {
/// Return the weight of servicing a single task.
fn service_task(maybe_lookup_len: Option<usize>, named: bool, periodic: bool) -> Weight {
- let base = Self::service_task_base();
+ let base = T::WeightInfo::service_task_base();
let mut total = match maybe_lookup_len {
None => base,
- Some(_l) => {
- // TODO uncomment if we will use the Preimages
- // Self::service_task_fetched(l as u32)
- base
- }
+ Some(l) => T::Preimages::service_task_fetched(l as u32),
};
if named {
- total.saturating_accrue(Self::service_task_named().saturating_sub(base));
+ total.saturating_accrue(T::WeightInfo::service_task_named().saturating_sub(base));
}
if periodic {
- total.saturating_accrue(Self::service_task_periodic().saturating_sub(base));
+ total.saturating_accrue(T::WeightInfo::service_task_periodic().saturating_sub(base));
}
total
}
}
-
-impl<T: WeightInfo> MarginalWeightInfo for T {}
#[frame_support::pallet]
pub mod pallet {
@@ -1119,7 +1131,7 @@
None => continue,
Some(t) => t,
};
- let base_weight = T::WeightInfo::service_task(
+ let base_weight = MarginalWeightInfo::<T>::service_task(
task.call.lookup_len().map(|x| x as usize),
task.maybe_id.is_some(),
task.maybe_periodic.is_some(),
@@ -1176,7 +1188,7 @@
}
};
- weight.check_accrue(T::WeightInfo::service_task(
+ weight.check_accrue(MarginalWeightInfo::<T>::service_task(
lookup_len.map(|x| x as usize),
task.maybe_id.is_some(),
task.maybe_periodic.is_some(),
pallets/scheduler-v2/src/tests.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 license:18// This file is part of Substrate.1920// Copyright (C) 2017-2022 Parity Technologies (UK) Ltd.21// SPDX-License-Identifier: Apache-2.02223// Licensed under the Apache License, Version 2.0 (the "License");24// you may not use this file except in compliance with the License.25// You may obtain a copy of the License at26//27// http://www.apache.org/licenses/LICENSE-2.028//29// Unless required by applicable law or agreed to in writing, software30// distributed under the License is distributed on an "AS IS" BASIS,31// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.32// See the License for the specific language governing permissions and33// limitations under the License.3435//! # Scheduler tests.3637use super::*;38use crate::mock::{39 logger, new_test_ext, root, run_to_block, LoggerCall, RuntimeCall, Scheduler, Test, *,40};41use frame_support::{42 assert_noop, assert_ok,43 traits::{Contains, OnInitialize},44 assert_err,45};4647#[test]48fn basic_scheduling_works() {49 new_test_ext().execute_with(|| {50 let call = RuntimeCall::Logger(LoggerCall::log {51 i: 42,52 weight: Weight::from_ref_time(10),53 });54 assert!(!<Test as frame_system::Config>::BaseCallFilter::contains(55 &call56 ));57 assert_ok!(Scheduler::do_schedule(58 DispatchTime::At(4),59 None,60 127,61 root(),62 <ScheduledCall<Test>>::new(call).unwrap(),63 ));64 run_to_block(3);65 assert!(logger::log().is_empty());66 run_to_block(4);67 assert_eq!(logger::log(), vec![(root(), 42u32)]);68 run_to_block(100);69 assert_eq!(logger::log(), vec![(root(), 42u32)]);70 });71}7273#[test]74fn schedule_after_works() {75 new_test_ext().execute_with(|| {76 run_to_block(2);77 let call = RuntimeCall::Logger(LoggerCall::log {78 i: 42,79 weight: Weight::from_ref_time(10),80 });81 assert!(!<Test as frame_system::Config>::BaseCallFilter::contains(82 &call83 ));84 // This will schedule the call 3 blocks after the next block... so block 3 + 3 = 685 assert_ok!(Scheduler::do_schedule(86 DispatchTime::After(3),87 None,88 127,89 root(),90 <ScheduledCall<Test>>::new(call).unwrap(),91 ));92 run_to_block(5);93 assert!(logger::log().is_empty());94 run_to_block(6);95 assert_eq!(logger::log(), vec![(root(), 42u32)]);96 run_to_block(100);97 assert_eq!(logger::log(), vec![(root(), 42u32)]);98 });99}100101#[test]102fn schedule_after_zero_works() {103 new_test_ext().execute_with(|| {104 run_to_block(2);105 let call = RuntimeCall::Logger(LoggerCall::log {106 i: 42,107 weight: Weight::from_ref_time(10),108 });109 assert!(!<Test as frame_system::Config>::BaseCallFilter::contains(110 &call111 ));112 assert_ok!(Scheduler::do_schedule(113 DispatchTime::After(0),114 None,115 127,116 root(),117 <ScheduledCall<Test>>::new(call).unwrap(),118 ));119 // Will trigger on the next block.120 run_to_block(3);121 assert_eq!(logger::log(), vec![(root(), 42u32)]);122 run_to_block(100);123 assert_eq!(logger::log(), vec![(root(), 42u32)]);124 });125}126127#[test]128fn periodic_scheduling_works() {129 new_test_ext().execute_with(|| {130 // at #4, every 3 blocks, 3 times.131 assert_ok!(Scheduler::do_schedule(132 DispatchTime::At(4),133 Some((3, 3)),134 127,135 root(),136 <ScheduledCall<Test>>::new(RuntimeCall::Logger(logger::Call::log {137 i: 42,138 weight: Weight::from_ref_time(10)139 }))140 .unwrap()141 ));142 run_to_block(3);143 assert!(logger::log().is_empty());144 run_to_block(4);145 assert_eq!(logger::log(), vec![(root(), 42u32)]);146 run_to_block(6);147 assert_eq!(logger::log(), vec![(root(), 42u32)]);148 run_to_block(7);149 assert_eq!(logger::log(), vec![(root(), 42u32), (root(), 42u32)]);150 run_to_block(9);151 assert_eq!(logger::log(), vec![(root(), 42u32), (root(), 42u32)]);152 run_to_block(10);153 assert_eq!(154 logger::log(),155 vec![(root(), 42u32), (root(), 42u32), (root(), 42u32)]156 );157 run_to_block(100);158 assert_eq!(159 logger::log(),160 vec![(root(), 42u32), (root(), 42u32), (root(), 42u32)]161 );162 });163}164165#[test]166fn cancel_named_scheduling_works_with_normal_cancel() {167 new_test_ext().execute_with(|| {168 // at #4.169 Scheduler::do_schedule_named(170 [1u8; 32],171 DispatchTime::At(4),172 None,173 127,174 root(),175 <ScheduledCall<Test>>::new(RuntimeCall::Logger(LoggerCall::log {176 i: 69,177 weight: Weight::from_ref_time(10),178 }))179 .unwrap(),180 )181 .unwrap();182 let i = Scheduler::do_schedule(183 DispatchTime::At(4),184 None,185 127,186 root(),187 <ScheduledCall<Test>>::new(RuntimeCall::Logger(LoggerCall::log {188 i: 42,189 weight: Weight::from_ref_time(10),190 }))191 .unwrap(),192 )193 .unwrap();194 run_to_block(3);195 assert!(logger::log().is_empty());196 assert_ok!(Scheduler::do_cancel_named(None, [1u8; 32]));197 assert_ok!(Scheduler::do_cancel(None, i));198 run_to_block(100);199 assert!(logger::log().is_empty());200 });201}202203#[test]204fn cancel_named_periodic_scheduling_works() {205 new_test_ext().execute_with(|| {206 // at #4, every 3 blocks, 3 times.207 Scheduler::do_schedule_named(208 [1u8; 32],209 DispatchTime::At(4),210 Some((3, 3)),211 127,212 root(),213 <ScheduledCall<Test>>::new(RuntimeCall::Logger(LoggerCall::log {214 i: 42,215 weight: Weight::from_ref_time(10),216 }))217 .unwrap(),218 )219 .unwrap();220 // same id results in error.221 assert!(Scheduler::do_schedule_named(222 [1u8; 32],223 DispatchTime::At(4),224 None,225 127,226 root(),227 <ScheduledCall<Test>>::new(RuntimeCall::Logger(LoggerCall::log {228 i: 69,229 weight: Weight::from_ref_time(10)230 }))231 .unwrap(),232 )233 .is_err());234 // different id is ok.235 Scheduler::do_schedule_named(236 [2u8; 32],237 DispatchTime::At(8),238 None,239 127,240 root(),241 <ScheduledCall<Test>>::new(RuntimeCall::Logger(LoggerCall::log {242 i: 69,243 weight: Weight::from_ref_time(10),244 }))245 .unwrap(),246 )247 .unwrap();248 run_to_block(3);249 assert!(logger::log().is_empty());250 run_to_block(4);251 assert_eq!(logger::log(), vec![(root(), 42u32)]);252 run_to_block(6);253 assert_ok!(Scheduler::do_cancel_named(None, [1u8; 32]));254 run_to_block(100);255 assert_eq!(logger::log(), vec![(root(), 42u32), (root(), 69u32)]);256 });257}258259#[test]260fn scheduler_respects_weight_limits() {261 let max_weight: Weight = <Test as Config>::MaximumWeight::get();262 new_test_ext().execute_with(|| {263 let call = RuntimeCall::Logger(LoggerCall::log {264 i: 42,265 weight: max_weight / 3 * 2,266 });267 assert_ok!(Scheduler::do_schedule(268 DispatchTime::At(4),269 None,270 127,271 root(),272 <ScheduledCall<Test>>::new(call).unwrap(),273 ));274 let call = RuntimeCall::Logger(LoggerCall::log {275 i: 69,276 weight: max_weight / 3 * 2,277 });278 assert_ok!(Scheduler::do_schedule(279 DispatchTime::At(4),280 None,281 127,282 root(),283 <ScheduledCall<Test>>::new(call).unwrap(),284 ));285 // 69 and 42 do not fit together286 run_to_block(4);287 assert_eq!(logger::log(), vec![(root(), 42u32)]);288 run_to_block(5);289 assert_eq!(logger::log(), vec![(root(), 42u32), (root(), 69u32)]);290 });291}292293/// Permanently overweight calls are not deleted but also not executed.294#[test]295fn scheduler_does_not_delete_permanently_overweight_call() {296 let max_weight: Weight = <Test as Config>::MaximumWeight::get();297 new_test_ext().execute_with(|| {298 let call = RuntimeCall::Logger(LoggerCall::log {299 i: 42,300 weight: max_weight,301 });302 assert_ok!(Scheduler::do_schedule(303 DispatchTime::At(4),304 None,305 127,306 root(),307 <ScheduledCall<Test>>::new(call).unwrap(),308 ));309 // Never executes.310 run_to_block(100);311 assert_eq!(logger::log(), vec![]);312313 // Assert the `PermanentlyOverweight` event.314 assert_eq!(315 System::events().last().unwrap().event,316 crate::Event::PermanentlyOverweight {317 task: (4, 0),318 id: None319 }320 .into(),321 );322 // The call is still in the agenda.323 assert!(Agenda::<Test>::get(4).agenda[0].is_some());324 });325}326327#[test]328fn scheduler_periodic_tasks_always_find_place() {329 let max_weight: Weight = <Test as Config>::MaximumWeight::get();330 let max_per_block = <Test as Config>::MaxScheduledPerBlock::get();331332 new_test_ext().execute_with(|| {333 let call = RuntimeCall::Logger(LoggerCall::log {334 i: 42,335 weight: (max_weight / 3) * 2,336 });337 let call = <ScheduledCall<Test>>::new(call).unwrap();338339 assert_ok!(Scheduler::do_schedule(340 DispatchTime::At(4),341 Some((4, u32::MAX)),342 127,343 root(),344 call.clone(),345 ));346 // Executes 5 times till block 20.347 run_to_block(20);348 assert_eq!(logger::log().len(), 5);349350 // Block 28 will already be full.351 for _ in 0..max_per_block {352 assert_ok!(Scheduler::do_schedule(353 DispatchTime::At(28),354 None,355 120,356 root(),357 call.clone(),358 ));359 }360361 run_to_block(24);362 assert_eq!(logger::log().len(), 6);363364 // The periodic task should be postponed365 assert_eq!(<Agenda<Test>>::get(29).agenda.len(), 1);366367 run_to_block(27); // will call on_initialize(28)368 assert_eq!(logger::log().len(), 6);369370 run_to_block(28); // will call on_initialize(29)371 assert_eq!(logger::log().len(), 7);372 });373}374375#[test]376fn scheduler_respects_priority_ordering() {377 let max_weight: Weight = <Test as Config>::MaximumWeight::get();378 new_test_ext().execute_with(|| {379 let call = RuntimeCall::Logger(LoggerCall::log {380 i: 42,381 weight: max_weight / 3,382 });383 assert_ok!(Scheduler::do_schedule(384 DispatchTime::At(4),385 None,386 1,387 root(),388 <ScheduledCall<Test>>::new(call).unwrap(),389 ));390 let call = RuntimeCall::Logger(LoggerCall::log {391 i: 69,392 weight: max_weight / 3,393 });394 assert_ok!(Scheduler::do_schedule(395 DispatchTime::At(4),396 None,397 0,398 root(),399 <ScheduledCall<Test>>::new(call).unwrap(),400 ));401 run_to_block(4);402 assert_eq!(logger::log(), vec![(root(), 69u32), (root(), 42u32)]);403 });404}405406#[test]407fn scheduler_respects_priority_ordering_with_soft_deadlines() {408 new_test_ext().execute_with(|| {409 let max_weight: Weight = <Test as Config>::MaximumWeight::get();410 let call = RuntimeCall::Logger(LoggerCall::log {411 i: 42,412 weight: max_weight / 5 * 2,413 });414 assert_ok!(Scheduler::do_schedule(415 DispatchTime::At(4),416 None,417 255,418 root(),419 <ScheduledCall<Test>>::new(call).unwrap(),420 ));421 let call = RuntimeCall::Logger(LoggerCall::log {422 i: 69,423 weight: max_weight / 5 * 2,424 });425 assert_ok!(Scheduler::do_schedule(426 DispatchTime::At(4),427 None,428 127,429 root(),430 <ScheduledCall<Test>>::new(call).unwrap(),431 ));432 let call = RuntimeCall::Logger(LoggerCall::log {433 i: 2600,434 weight: max_weight / 5 * 4,435 });436 assert_ok!(Scheduler::do_schedule(437 DispatchTime::At(4),438 None,439 126,440 root(),441 <ScheduledCall<Test>>::new(call).unwrap(),442 ));443444 // 2600 does not fit with 69 or 42, but has higher priority, so will go through445 run_to_block(4);446 assert_eq!(logger::log(), vec![(root(), 2600u32)]);447 // 69 and 42 fit together448 run_to_block(5);449 assert_eq!(450 logger::log(),451 vec![(root(), 2600u32), (root(), 69u32), (root(), 42u32)]452 );453 });454}455456#[test]457fn on_initialize_weight_is_correct() {458 new_test_ext().execute_with(|| {459 let call_weight = Weight::from_ref_time(25);460461 // Named462 let call = RuntimeCall::Logger(LoggerCall::log {463 i: 3,464 weight: call_weight + Weight::from_ref_time(1),465 });466 assert_ok!(Scheduler::do_schedule_named(467 [1u8; 32],468 DispatchTime::At(3),469 None,470 255,471 root(),472 <ScheduledCall<Test>>::new(call).unwrap(),473 ));474 let call = RuntimeCall::Logger(LoggerCall::log {475 i: 42,476 weight: call_weight + Weight::from_ref_time(2),477 });478 // Anon Periodic479 assert_ok!(Scheduler::do_schedule(480 DispatchTime::At(2),481 Some((1000, 3)),482 128,483 root(),484 <ScheduledCall<Test>>::new(call).unwrap(),485 ));486 let call = RuntimeCall::Logger(LoggerCall::log {487 i: 69,488 weight: call_weight + Weight::from_ref_time(3),489 });490 // Anon491 assert_ok!(Scheduler::do_schedule(492 DispatchTime::At(2),493 None,494 127,495 root(),496 <ScheduledCall<Test>>::new(call).unwrap(),497 ));498 // Named Periodic499 let call = RuntimeCall::Logger(LoggerCall::log {500 i: 2600,501 weight: call_weight + Weight::from_ref_time(4),502 });503 assert_ok!(Scheduler::do_schedule_named(504 [2u8; 32],505 DispatchTime::At(1),506 Some((1000, 3)),507 126,508 root(),509 <ScheduledCall<Test>>::new(call).unwrap(),510 ));511512 // Will include the named periodic only513 assert_eq!(514 Scheduler::on_initialize(1),515 TestWeightInfo::service_agendas_base()516 + TestWeightInfo::service_agenda_base(1)517 + <MarginalWeightInfo<Test>>::service_task(None, true, true)518 + TestWeightInfo::execute_dispatch_unsigned()519 + call_weight + Weight::from_ref_time(4)520 );521 assert_eq!(IncompleteSince::<Test>::get(), None);522 assert_eq!(logger::log(), vec![(root(), 2600u32)]);523524 // Will include anon and anon periodic525 assert_eq!(526 Scheduler::on_initialize(2),527 TestWeightInfo::service_agendas_base()528 + TestWeightInfo::service_agenda_base(2)529 + <MarginalWeightInfo<Test>>::service_task(None, false, true)530 + TestWeightInfo::execute_dispatch_unsigned()531 + call_weight + Weight::from_ref_time(3)532 + <MarginalWeightInfo<Test>>::service_task(None, false, false)533 + TestWeightInfo::execute_dispatch_unsigned()534 + call_weight + Weight::from_ref_time(2)535 );536 assert_eq!(IncompleteSince::<Test>::get(), None);537 assert_eq!(538 logger::log(),539 vec![(root(), 2600u32), (root(), 69u32), (root(), 42u32)]540 );541542 // Will include named only543 assert_eq!(544 Scheduler::on_initialize(3),545 TestWeightInfo::service_agendas_base()546 + TestWeightInfo::service_agenda_base(1)547 + <MarginalWeightInfo<Test>>::service_task(None, true, false)548 + TestWeightInfo::execute_dispatch_unsigned()549 + call_weight + Weight::from_ref_time(1)550 );551 assert_eq!(IncompleteSince::<Test>::get(), None);552 assert_eq!(553 logger::log(),554 vec![555 (root(), 2600u32),556 (root(), 69u32),557 (root(), 42u32),558 (root(), 3u32)559 ]560 );561562 // Will contain none563 let actual_weight = Scheduler::on_initialize(4);564 assert_eq!(565 actual_weight,566 TestWeightInfo::service_agendas_base() + TestWeightInfo::service_agenda_base(0)567 );568 });569}570571#[test]572fn root_calls_works() {573 new_test_ext().execute_with(|| {574 let call = Box::new(RuntimeCall::Logger(LoggerCall::log {575 i: 69,576 weight: Weight::from_ref_time(10),577 }));578 let call2 = Box::new(RuntimeCall::Logger(LoggerCall::log {579 i: 42,580 weight: Weight::from_ref_time(10),581 }));582 assert_ok!(Scheduler::schedule_named(583 RuntimeOrigin::root(),584 [1u8; 32],585 4,586 None,587 Some(127),588 call,589 ));590 assert_ok!(Scheduler::schedule(591 RuntimeOrigin::root(),592 4,593 None,594 Some(127),595 call2596 ));597 run_to_block(3);598 // Scheduled calls are in the agenda.599 assert_eq!(Agenda::<Test>::get(4).agenda.len(), 2);600 assert!(logger::log().is_empty());601 assert_ok!(Scheduler::cancel_named(RuntimeOrigin::root(), [1u8; 32]));602 assert_ok!(Scheduler::cancel(RuntimeOrigin::root(), 4, 1));603 // Scheduled calls are made NONE, so should not effect state604 run_to_block(100);605 assert!(logger::log().is_empty());606 });607}608609#[test]610fn fails_to_schedule_task_in_the_past() {611 new_test_ext().execute_with(|| {612 run_to_block(3);613614 let call1 = Box::new(RuntimeCall::Logger(LoggerCall::log {615 i: 69,616 weight: Weight::from_ref_time(10),617 }));618 let call2 = Box::new(RuntimeCall::Logger(LoggerCall::log {619 i: 42,620 weight: Weight::from_ref_time(10),621 }));622 let call3 = Box::new(RuntimeCall::Logger(LoggerCall::log {623 i: 42,624 weight: Weight::from_ref_time(10),625 }));626627 assert_noop!(628 Scheduler::schedule_named(RuntimeOrigin::root(), [1u8; 32], 2, None, Some(127), call1),629 Error::<Test>::TargetBlockNumberInPast,630 );631632 assert_noop!(633 Scheduler::schedule(RuntimeOrigin::root(), 2, None, Some(127), call2),634 Error::<Test>::TargetBlockNumberInPast,635 );636637 assert_noop!(638 Scheduler::schedule(RuntimeOrigin::root(), 3, None, Some(127), call3),639 Error::<Test>::TargetBlockNumberInPast,640 );641 });642}643644#[test]645fn should_use_origin() {646 new_test_ext().execute_with(|| {647 let call = Box::new(RuntimeCall::Logger(LoggerCall::log {648 i: 69,649 weight: Weight::from_ref_time(10),650 }));651 let call2 = Box::new(RuntimeCall::Logger(LoggerCall::log {652 i: 42,653 weight: Weight::from_ref_time(10),654 }));655 assert_ok!(Scheduler::schedule_named(656 system::RawOrigin::Signed(1).into(),657 [1u8; 32],658 4,659 None,660 None,661 call,662 ));663 assert_ok!(Scheduler::schedule(664 system::RawOrigin::Signed(1).into(),665 4,666 None,667 None,668 call2,669 ));670 run_to_block(3);671 // Scheduled calls are in the agenda.672 assert_eq!(Agenda::<Test>::get(4).agenda.len(), 2);673 assert!(logger::log().is_empty());674 assert_ok!(Scheduler::cancel_named(675 system::RawOrigin::Signed(1).into(),676 [1u8; 32]677 ));678 assert_ok!(Scheduler::cancel(system::RawOrigin::Signed(1).into(), 4, 1));679 // Scheduled calls are made NONE, so should not effect state680 run_to_block(100);681 assert!(logger::log().is_empty());682 });683}684685#[test]686fn should_check_origin() {687 new_test_ext().execute_with(|| {688 let call = Box::new(RuntimeCall::Logger(LoggerCall::log {689 i: 69,690 weight: Weight::from_ref_time(10),691 }));692 let call2 = Box::new(RuntimeCall::Logger(LoggerCall::log {693 i: 42,694 weight: Weight::from_ref_time(10),695 }));696 assert_noop!(697 Scheduler::schedule_named(698 system::RawOrigin::Signed(2).into(),699 [1u8; 32],700 4,701 None,702 None,703 call704 ),705 BadOrigin706 );707 assert_noop!(708 Scheduler::schedule(system::RawOrigin::Signed(2).into(), 4, None, None, call2),709 BadOrigin710 );711 });712}713714#[test]715fn should_check_origin_for_cancel() {716 new_test_ext().execute_with(|| {717 let call = Box::new(RuntimeCall::Logger(LoggerCall::log_without_filter {718 i: 69,719 weight: Weight::from_ref_time(10),720 }));721 let call2 = Box::new(RuntimeCall::Logger(LoggerCall::log_without_filter {722 i: 42,723 weight: Weight::from_ref_time(10),724 }));725 assert_ok!(Scheduler::schedule_named(726 system::RawOrigin::Signed(1).into(),727 [1u8; 32],728 4,729 None,730 None,731 call,732 ));733 assert_ok!(Scheduler::schedule(734 system::RawOrigin::Signed(1).into(),735 4,736 None,737 None,738 call2,739 ));740 run_to_block(3);741 // Scheduled calls are in the agenda.742 assert_eq!(Agenda::<Test>::get(4).agenda.len(), 2);743 assert!(logger::log().is_empty());744 assert_noop!(745 Scheduler::cancel_named(system::RawOrigin::Signed(2).into(), [1u8; 32]),746 BadOrigin747 );748 assert_noop!(749 Scheduler::cancel(system::RawOrigin::Signed(2).into(), 4, 1),750 BadOrigin751 );752 assert_noop!(753 Scheduler::cancel_named(system::RawOrigin::Root.into(), [1u8; 32]),754 BadOrigin755 );756 assert_noop!(757 Scheduler::cancel(system::RawOrigin::Root.into(), 4, 1),758 BadOrigin759 );760 run_to_block(5);761 assert_eq!(762 logger::log(),763 vec![764 (system::RawOrigin::Signed(1).into(), 69u32),765 (system::RawOrigin::Signed(1).into(), 42u32)766 ]767 );768 });769}770771/// Cancelling a call and then scheduling a second call for the same772/// block results in different addresses.773#[test]774fn schedule_does_not_resuse_addr() {775 new_test_ext().execute_with(|| {776 let call = RuntimeCall::Logger(LoggerCall::log {777 i: 42,778 weight: Weight::from_ref_time(10),779 });780781 // Schedule both calls.782 let addr_1 = Scheduler::do_schedule(783 DispatchTime::At(4),784 None,785 127,786 root(),787 <ScheduledCall<Test>>::new(call.clone()).unwrap(),788 )789 .unwrap();790 // Cancel the call.791 assert_ok!(Scheduler::do_cancel(None, addr_1));792 let addr_2 = Scheduler::do_schedule(793 DispatchTime::At(4),794 None,795 127,796 root(),797 <ScheduledCall<Test>>::new(call).unwrap(),798 )799 .unwrap();800801 // Should not re-use the address.802 assert!(addr_1 != addr_2);803 });804}805806#[test]807fn schedule_agenda_overflows() {808 let max: u32 = <Test as Config>::MaxScheduledPerBlock::get();809810 new_test_ext().execute_with(|| {811 let call = RuntimeCall::Logger(LoggerCall::log {812 i: 42,813 weight: Weight::from_ref_time(10),814 });815 let call = <ScheduledCall<Test>>::new(call).unwrap();816817 // Schedule the maximal number allowed per block.818 for _ in 0..max {819 Scheduler::do_schedule(DispatchTime::At(4), None, 127, root(), call.clone()).unwrap();820 }821822 // One more time and it errors.823 assert_noop!(824 Scheduler::do_schedule(DispatchTime::At(4), None, 127, root(), call,),825 <Error<Test>>::AgendaIsExhausted,826 );827828 run_to_block(4);829 // All scheduled calls are executed.830 assert_eq!(logger::log().len() as u32, max);831 });832}833834/// Cancelling and scheduling does not overflow the agenda but fills holes.835#[test]836fn cancel_and_schedule_fills_holes() {837 let max: u32 = <Test as Config>::MaxScheduledPerBlock::get();838 assert!(839 max > 3,840 "This test only makes sense for MaxScheduledPerBlock > 3"841 );842843 new_test_ext().execute_with(|| {844 let call = RuntimeCall::Logger(LoggerCall::log {845 i: 42,846 weight: Weight::from_ref_time(10),847 });848 let call = <ScheduledCall<Test>>::new(call).unwrap();849 let mut addrs = Vec::<_>::default();850851 // Schedule the maximal number allowed per block.852 for _ in 0..max {853 addrs.push(854 Scheduler::do_schedule(DispatchTime::At(4), None, 127, root(), call.clone())855 .unwrap(),856 );857 }858 // Cancel three of them.859 for addr in addrs.into_iter().take(3) {860 Scheduler::do_cancel(None, addr).unwrap();861 }862 // Schedule three new ones.863 for i in 0..3 {864 let (_block, index) =865 Scheduler::do_schedule(DispatchTime::At(4), None, 127, root(), call.clone())866 .unwrap();867 assert_eq!(i, index);868 }869870 run_to_block(4);871 // Maximum number of calls are executed.872 assert_eq!(logger::log().len() as u32, max);873 });874}875876#[test]877fn cannot_schedule_too_big_tasks() {878 new_test_ext().execute_with(|| {879 let call = Box::new(<<Test as Config>::RuntimeCall>::from(SystemCall::remark {880 remark: vec![0; EncodedCall::bound() - 4],881 }));882883 assert_ok!(Scheduler::schedule(884 RuntimeOrigin::root(),885 4,886 None,887 Some(127),888 call889 ));890891 let call = Box::new(<<Test as Config>::RuntimeCall>::from(SystemCall::remark {892 remark: vec![0; EncodedCall::bound() - 3],893 }));894895 assert_err!(896 Scheduler::schedule(RuntimeOrigin::root(), 4, None, Some(127), call),897 <Error<Test>>::TooBigScheduledCall898 );899 });900}