Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add CallFilter into pallet-scheduler #3

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ impl pallet_scheduler::Config for Runtime {
type WeightInfo = pallet_scheduler::weights::SubstrateWeight<Runtime>;
type OriginPrivilegeCmp = EqualPrivilegeOnly;
type Preimages = Preimage;
type CallFilter = Everything;
}

impl pallet_glutton::Config for Runtime {
Expand Down
3 changes: 2 additions & 1 deletion frame/democracy/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate as pallet_democracy;
use frame_support::{
assert_noop, assert_ok, ord_parameter_types, parameter_types,
traits::{
ConstU32, ConstU64, Contains, EqualPrivilegeOnly, GenesisBuild, OnInitialize,
ConstU32, ConstU64, Contains, EqualPrivilegeOnly, Everything, GenesisBuild, OnInitialize,
SortedMembers, StorePreimage,
},
weights::Weight,
Expand Down Expand Up @@ -132,6 +132,7 @@ impl pallet_scheduler::Config for Test {
type WeightInfo = ();
type OriginPrivilegeCmp = EqualPrivilegeOnly;
type Preimages = ();
type CallFilter = Everything;
}

impl pallet_balances::Config for Test {
Expand Down
5 changes: 3 additions & 2 deletions frame/referenda/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::{
assert_ok, ord_parameter_types, parameter_types,
traits::{
ConstU32, ConstU64, Contains, EqualPrivilegeOnly, OnInitialize, OriginTrait, Polling,
SortedMembers,
ConstU32, ConstU64, Contains, EqualPrivilegeOnly, Everything, OnInitialize, OriginTrait,
Polling, SortedMembers,
},
weights::Weight,
};
Expand Down Expand Up @@ -109,6 +109,7 @@ impl pallet_scheduler::Config for Test {
type WeightInfo = ();
type OriginPrivilegeCmp = EqualPrivilegeOnly;
type Preimages = Preimage;
type CallFilter = Everything;
}
impl pallet_balances::Config for Test {
type MaxReserves = ();
Expand Down
21 changes: 18 additions & 3 deletions frame/scheduler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ use frame_support::{
ensure,
traits::{
schedule::{self, DispatchTime, MaybeHashed},
Bounded, CallerTrait, EnsureOrigin, Get, Hash as PreimageHash, IsType, OriginTrait,
PalletInfoAccess, PrivilegeCmp, QueryPreimage, StorageVersion, StorePreimage,
Bounded, CallerTrait, Contains, EnsureOrigin, Get, Hash as PreimageHash, IsType,
OriginTrait, PalletInfoAccess, PrivilegeCmp, QueryPreimage, StorageVersion, StorePreimage,
},
weights::{Weight, WeightMeter},
};
Expand Down Expand Up @@ -165,7 +165,7 @@ impl<T: WeightInfo> MarginalWeightInfo for T {}
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::{dispatch::PostDispatchInfo, pallet_prelude::*};
use frame_support::{dispatch::PostDispatchInfo, pallet_prelude::*, traits::Contains};
use frame_system::pallet_prelude::*;

/// The current storage version.
Expand Down Expand Up @@ -228,6 +228,9 @@ pub mod pallet {

/// The preimage provider with which we look up call hashes to get the call.
type Preimages: QueryPreimage + StorePreimage;

/// Filtering calls.
type CallFilter: Contains<<Self as Config>::RuntimeCall>;
}

#[pallet::storage]
Expand Down Expand Up @@ -311,6 +314,7 @@ pub mod pallet {
) -> DispatchResult {
T::ScheduleOrigin::ensure_origin(origin.clone())?;
let origin = <T as Config>::RuntimeOrigin::from(origin);
Self::check_call_filter(&call)?;
Self::do_schedule(
DispatchTime::At(when),
maybe_periodic,
Expand Down Expand Up @@ -344,6 +348,7 @@ pub mod pallet {
) -> DispatchResult {
T::ScheduleOrigin::ensure_origin(origin.clone())?;
let origin = <T as Config>::RuntimeOrigin::from(origin);
Self::check_call_filter(&call)?;
Self::do_schedule_named(
id,
DispatchTime::At(when),
Expand Down Expand Up @@ -377,6 +382,7 @@ pub mod pallet {
) -> DispatchResult {
T::ScheduleOrigin::ensure_origin(origin.clone())?;
let origin = <T as Config>::RuntimeOrigin::from(origin);
Self::check_call_filter(&call)?;
Self::do_schedule(
DispatchTime::After(after),
maybe_periodic,
Expand All @@ -400,6 +406,7 @@ pub mod pallet {
) -> DispatchResult {
T::ScheduleOrigin::ensure_origin(origin.clone())?;
let origin = <T as Config>::RuntimeOrigin::from(origin);
Self::check_call_filter(&call)?;
Self::do_schedule_named(
id,
DispatchTime::After(after),
Expand Down Expand Up @@ -941,6 +948,14 @@ impl<T: Config> Pallet<T> {
Self::deposit_event(Event::Canceled { when, index });
Self::place_task(new_time, task).map_err(|x| x.0)
}

fn check_call_filter(call: &<T as Config>::RuntimeCall) -> DispatchResult {
if !<T as Config>::CallFilter::contains(call) {
return Err(<frame_system::Error<T>>::CallFiltered.into())
}

Ok(())
}
}

enum ServiceTaskError {
Expand Down
18 changes: 18 additions & 0 deletions frame/scheduler/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ pub mod logger {
});
Ok(())
}

#[pallet::call_index(2)]
#[pallet::weight(*weight)]
pub fn log_not_sheduled(origin: OriginFor<T>, i: u32, weight: Weight) -> DispatchResult {
Self::deposit_event(Event::Logged(i, weight));
Log::mutate(|log| {
log.push((origin.caller().clone(), i));
});
Ok(())
}
}
}

Expand Down Expand Up @@ -117,6 +127,13 @@ impl Contains<RuntimeCall> for BaseFilter {
}
}

pub struct CallFilter;
impl Contains<RuntimeCall> for CallFilter {
fn contains(call: &RuntimeCall) -> bool {
!matches!(call, RuntimeCall::Logger(LoggerCall::log_not_sheduled { .. }))
}
}

parameter_types! {
pub BlockWeights: frame_system::limits::BlockWeights =
frame_system::limits::BlockWeights::simple_max(
Expand Down Expand Up @@ -220,6 +237,7 @@ impl Config for Test {
type WeightInfo = TestWeightInfo;
type OriginPrivilegeCmp = EqualPrivilegeOnly;
type Preimages = Preimage;
type CallFilter = CallFilter;
}

pub type LoggerCall = logger::Call<Test>;
Expand Down
33 changes: 33 additions & 0 deletions frame/scheduler/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,39 @@ fn root_calls_works() {
});
}

#[test]
fn fails_to_schedule_filtered_task() {
new_test_ext().execute_with(|| {
let call = Box::new(RuntimeCall::Logger(LoggerCall::log_not_sheduled {
i: 42,
weight: Weight::from_ref_time(10),
}));
assert_err!(
Scheduler::schedule(RuntimeOrigin::root(), 4, None, 127, call.clone(),),
frame_system::Error::<Test>::CallFiltered
);
assert_err!(
Scheduler::schedule_named(RuntimeOrigin::root(), [1u8; 32], 4, None, 127, call.clone(),),
frame_system::Error::<Test>::CallFiltered
);
assert_err!(
Scheduler::schedule_after(RuntimeOrigin::root(), 4, None, 127, call.clone(),),
frame_system::Error::<Test>::CallFiltered
);
assert_err!(
Scheduler::schedule_named_after(
RuntimeOrigin::root(),
[1u8; 32],
4,
None,
127,
call.clone(),
),
frame_system::Error::<Test>::CallFiltered
);
});
}

#[test]
fn fails_to_schedule_task_in_the_past() {
new_test_ext().execute_with(|| {
Expand Down