Introduce in-origin filtering (#6318)

* impl filter in origin

* remove IsCallable usage. Breaking: utility::batch(root, calls) no longer bypass BasicCallFilter

* rename BasicCallFilter -> BaseCallFilter

* refactor code

* Apply suggestions from code review

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* remove forgotten temporar comment

* better add suggestion in another PR

* refactor: use Clone instead of mem::replace

* fix tests

* fix tests

* fix tests

* fix benchmarks

* Make root bypass filter in utility::batch

* fix unused imports

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
This commit is contained in:
Guillaume Thiolliere
2020-06-15 17:05:41 +02:00
committed by GitHub
parent 97cac4ce8b
commit c2ad27271b
79 changed files with 536 additions and 302 deletions
+13 -15
View File
@@ -23,7 +23,7 @@ use super::*;
use frame_support::{
assert_ok, assert_noop, impl_outer_origin, parameter_types, impl_outer_dispatch,
weights::Weight, impl_outer_event
weights::Weight, impl_outer_event, dispatch::DispatchError, traits::Filter, storage,
};
use sp_core::H256;
use sp_runtime::{Perbill, traits::{BlakeTwo256, IdentityLookup}, testing::Header};
@@ -59,6 +59,7 @@ parameter_types! {
pub const AvailableBlockRatio: Perbill = Perbill::one();
}
impl frame_system::Trait for Test {
type BaseCallFilter = TestBaseCallFilter;
type Origin = Origin;
type Index = u64;
type BlockNumber = u64;
@@ -98,8 +99,8 @@ parameter_types! {
pub const MultisigDepositFactor: u64 = 1;
pub const MaxSignatories: u16 = 3;
}
pub struct TestIsCallable;
impl Filter<Call> for TestIsCallable {
pub struct TestBaseCallFilter;
impl Filter<Call> for TestBaseCallFilter {
fn filter(c: &Call) -> bool {
match *c {
Call::Balances(_) => true,
@@ -107,17 +108,9 @@ impl Filter<Call> for TestIsCallable {
}
}
}
impl FilterStack<Call> for TestIsCallable {
type Stack = ();
fn push(_: impl Fn(&Call) -> bool + 'static) {}
fn pop() {}
fn take() -> Self::Stack { () }
fn restore(_: Self::Stack) {}
}
impl Trait for Test {
type Event = TestEvent;
type Call = Call;
type IsCallable = TestIsCallable;
}
type System = frame_system::Module<Test>;
type Balances = pallet_balances::Module<Test>;
@@ -171,21 +164,26 @@ fn as_sub_filters() {
Origin::signed(1),
1,
Box::new(Call::System(frame_system::Call::remark(vec![]))),
), Error::<Test>::Uncallable);
), DispatchError::BadOrigin);
});
}
#[test]
fn batch_with_root_works() {
new_test_ext().execute_with(|| {
let k = b"a".to_vec();
let call = Call::System(frame_system::Call::set_storage(vec![(k.clone(), k.clone())]));
assert!(!TestBaseCallFilter::filter(&call));
assert_eq!(Balances::free_balance(1), 10);
assert_eq!(Balances::free_balance(2), 10);
assert_ok!(Utility::batch(Origin::ROOT, vec![
assert_ok!(Utility::batch(Origin::root(), vec![
Call::Balances(BalancesCall::force_transfer(1, 2, 5)),
Call::Balances(BalancesCall::force_transfer(1, 2, 5))
Call::Balances(BalancesCall::force_transfer(1, 2, 5)),
call, // Check filters are correctly bypassed
]));
assert_eq!(Balances::free_balance(1), 0);
assert_eq!(Balances::free_balance(2), 20);
assert_eq!(storage::unhashed::get_raw(&k), Some(k));
});
}
@@ -213,7 +211,7 @@ fn batch_with_signed_filters() {
Call::System(frame_system::Call::remark(vec![]))
]),
);
expect_event(Event::Uncallable(0));
expect_event(Event::BatchInterrupted(0, DispatchError::BadOrigin));
});
}