Skip to content

Commit 544ad7c

Browse files
gui1117kianenigma
andauthored
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>
1 parent 5bf8b77 commit 544ad7c

File tree

2 files changed

+37
-35
lines changed

2 files changed

+37
-35
lines changed

src/lib.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ use sp_runtime::{DispatchResult, traits::{Dispatchable, Zero}};
4141
use sp_runtime::traits::Member;
4242
use frame_support::{
4343
decl_module, decl_event, decl_error, decl_storage, Parameter, ensure, traits::{
44-
Get, ReservableCurrency, Currency, Filter, FilterStack, FilterStackGuard,
45-
ClearFilterGuard, InstanceFilter
44+
Get, ReservableCurrency, Currency, InstanceFilter,
45+
OriginTrait, IsType,
4646
}, weights::{GetDispatchInfo, constants::{WEIGHT_PER_MICROS, WEIGHT_PER_NANOS}},
4747
dispatch::{PostDispatchInfo, IsSubType},
4848
};
@@ -60,14 +60,12 @@ pub trait Trait: frame_system::Trait {
6060

6161
/// The overarching call type.
6262
type Call: Parameter + Dispatchable<Origin=Self::Origin, PostInfo=PostDispatchInfo>
63-
+ GetDispatchInfo + From<frame_system::Call<Self>> + IsSubType<Module<Self>, Self>;
63+
+ GetDispatchInfo + From<frame_system::Call<Self>> + IsSubType<Module<Self>, Self>
64+
+ IsType<<Self as frame_system::Trait>::Call>;
6465

6566
/// The currency mechanism.
6667
type Currency: ReservableCurrency<Self::AccountId>;
6768

68-
/// Is a given call compatible with the proxying subsystem?
69-
type IsCallable: FilterStack<<Self as Trait>::Call>;
70-
7169
/// A kind of proxy; specified with the proxy and passed in to the `IsProxyable` fitler.
7270
/// The instance filter determines whether a given call may be proxied under this type.
7371
type ProxyType: Parameter + Member + Ord + PartialOrd + InstanceFilter<<Self as Trait>::Call>
@@ -105,8 +103,6 @@ decl_error! {
105103
NotFound,
106104
/// Sender is not a proxy of the account to be proxied.
107105
NotProxy,
108-
/// A call with a `false` `IsCallable` filter was attempted.
109-
Uncallable,
110106
/// A call which is incompatible with the proxy type's filter was attempted.
111107
Unproxyable,
112108
/// Account is already a proxy.
@@ -171,19 +167,17 @@ decl_module! {
171167
.find(|x| &x.0 == &who && force_proxy_type.as_ref().map_or(true, |y| &x.1 == y))
172168
.ok_or(Error::<T>::NotProxy)?;
173169

174-
// We're now executing as a freshly authenticated new account, so the previous call
175-
// restrictions no longer apply.
176-
let _clear_guard = ClearFilterGuard::<T::IsCallable, <T as Trait>::Call>::new();
177-
let _filter_guard = FilterStackGuard::<T::IsCallable, <T as Trait>::Call>::new(
178-
move |c| match c.is_sub_type() {
170+
// This is a freshly authenticated new account, the origin restrictions doesn't apply.
171+
let mut origin: T::Origin = frame_system::RawOrigin::Signed(real).into();
172+
origin.add_filter(move |c: &<T as frame_system::Trait>::Call| {
173+
let c = <T as Trait>::Call::from_ref(c);
174+
match c.is_sub_type() {
179175
Some(Call::add_proxy(_, ref pt)) | Some(Call::remove_proxy(_, ref pt))
180176
if !proxy_type.is_superset(&pt) => false,
181-
_ => proxy_type.filter(&c)
177+
_ => proxy_type.filter(c)
182178
}
183-
);
184-
ensure!(T::IsCallable::filter(&call), Error::<T>::Uncallable);
185-
186-
let e = call.dispatch(frame_system::RawOrigin::Signed(real).into());
179+
});
180+
let e = call.dispatch(origin);
187181
Self::deposit_event(RawEvent::ProxyExecuted(e.map(|_| ()).map_err(|e| e.error)));
188182
}
189183

src/tests.rs

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use super::*;
2323

2424
use frame_support::{
2525
assert_ok, assert_noop, impl_outer_origin, parameter_types, impl_outer_dispatch,
26-
impl_filter_stack, weights::Weight, impl_outer_event, RuntimeDebug, dispatch::DispatchError
26+
weights::Weight, impl_outer_event, RuntimeDebug, dispatch::DispatchError, traits::Filter,
2727
};
2828
use codec::{Encode, Decode};
2929
use sp_core::H256;
@@ -62,6 +62,7 @@ parameter_types! {
6262
pub const AvailableBlockRatio: Perbill = Perbill::one();
6363
}
6464
impl frame_system::Trait for Test {
65+
type BaseCallFilter = BaseFilter;
6566
type Origin = Origin;
6667
type Index = u64;
6768
type BlockNumber = u64;
@@ -99,15 +100,12 @@ impl pallet_balances::Trait for Test {
99100
impl pallet_utility::Trait for Test {
100101
type Event = TestEvent;
101102
type Call = Call;
102-
type IsCallable = IsCallable;
103103
}
104104
parameter_types! {
105105
pub const ProxyDepositBase: u64 = 1;
106106
pub const ProxyDepositFactor: u64 = 1;
107107
pub const MaxProxies: u16 = 4;
108108
}
109-
pub struct IsCallable;
110-
impl_filter_stack!(crate::tests::IsCallable, crate::tests::BaseFilter, crate::tests::Call, is_callable);
111109
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Encode, Decode, RuntimeDebug)]
112110
pub enum ProxyType {
113111
Any,
@@ -142,7 +140,6 @@ impl Trait for Test {
142140
type Event = TestEvent;
143141
type Call = Call;
144142
type Currency = Balances;
145-
type IsCallable = IsCallable;
146143
type ProxyType = ProxyType;
147144
type ProxyDepositBase = ProxyDepositBase;
148145
type ProxyDepositFactor = ProxyDepositFactor;
@@ -158,7 +155,6 @@ use frame_system::Call as SystemCall;
158155
use pallet_balances::Call as BalancesCall;
159156
use pallet_balances::Error as BalancesError;
160157
use pallet_utility::Call as UtilityCall;
161-
use pallet_utility::Error as UtilityError;
162158
use pallet_utility::Event as UtilityEvent;
163159
use super::Call as ProxyCall;
164160

@@ -201,7 +197,8 @@ fn filtering_works() {
201197
expect_event(RawEvent::ProxyExecuted(Ok(())));
202198
assert_ok!(Proxy::proxy(Origin::signed(3), 1, None, call.clone()));
203199
expect_event(RawEvent::ProxyExecuted(Ok(())));
204-
assert_noop!(Proxy::proxy(Origin::signed(4), 1, None, call.clone()), Error::<Test>::Uncallable);
200+
assert_ok!(Proxy::proxy(Origin::signed(4), 1, None, call.clone()));
201+
expect_event(RawEvent::ProxyExecuted(Err(DispatchError::BadOrigin)));
205202

206203
let sub_id = Utility::sub_account_id(1, 0);
207204
Balances::mutate_account(&sub_id, |a| a.free = 1000);
@@ -210,32 +207,41 @@ fn filtering_works() {
210207
let call = Box::new(Call::Utility(UtilityCall::as_sub(0, inner.clone())));
211208
assert_ok!(Proxy::proxy(Origin::signed(2), 1, None, call.clone()));
212209
expect_event(RawEvent::ProxyExecuted(Ok(())));
213-
assert_noop!(Proxy::proxy(Origin::signed(3), 1, None, call.clone()), Error::<Test>::Uncallable);
210+
assert_ok!(Proxy::proxy(Origin::signed(3), 1, None, call.clone()));
211+
expect_event(RawEvent::ProxyExecuted(Err(DispatchError::BadOrigin)));
214212
assert_ok!(Proxy::proxy(Origin::signed(4), 1, None, call.clone()));
215213
expect_event(RawEvent::ProxyExecuted(Ok(())));
216214

217215
let call = Box::new(Call::Utility(UtilityCall::as_limited_sub(0, inner.clone())));
218216
assert_ok!(Proxy::proxy(Origin::signed(2), 1, None, call.clone()));
219217
expect_event(RawEvent::ProxyExecuted(Ok(())));
220-
assert_noop!(Proxy::proxy(Origin::signed(3), 1, None, call.clone()), Error::<Test>::Uncallable);
218+
assert_ok!(Proxy::proxy(Origin::signed(3), 1, None, call.clone()));
219+
expect_event(RawEvent::ProxyExecuted(Err(DispatchError::BadOrigin)));
221220
assert_ok!(Proxy::proxy(Origin::signed(4), 1, None, call.clone()));
222-
let de = DispatchError::from(UtilityError::<Test>::Uncallable).stripped();
223-
expect_event(RawEvent::ProxyExecuted(Err(de)));
221+
expect_event(RawEvent::ProxyExecuted(Err(DispatchError::BadOrigin)));
224222

225223
let call = Box::new(Call::Utility(UtilityCall::batch(vec![*inner])));
226224
assert_ok!(Proxy::proxy(Origin::signed(2), 1, None, call.clone()));
227225
expect_events(vec![UtilityEvent::BatchCompleted.into(), RawEvent::ProxyExecuted(Ok(())).into()]);
228-
assert_noop!(Proxy::proxy(Origin::signed(3), 1, None, call.clone()), Error::<Test>::Uncallable);
226+
assert_ok!(Proxy::proxy(Origin::signed(3), 1, None, call.clone()));
227+
expect_event(RawEvent::ProxyExecuted(Err(DispatchError::BadOrigin)));
229228
assert_ok!(Proxy::proxy(Origin::signed(4), 1, None, call.clone()));
230-
expect_events(vec![UtilityEvent::Uncallable(0).into(), RawEvent::ProxyExecuted(Ok(())).into()]);
229+
expect_events(vec![
230+
UtilityEvent::BatchInterrupted(0, DispatchError::BadOrigin).into(),
231+
RawEvent::ProxyExecuted(Ok(())).into(),
232+
]);
231233

232234
let inner = Box::new(Call::Proxy(ProxyCall::add_proxy(5, ProxyType::Any)));
233235
let call = Box::new(Call::Utility(UtilityCall::batch(vec![*inner])));
234236
assert_ok!(Proxy::proxy(Origin::signed(2), 1, None, call.clone()));
235237
expect_events(vec![UtilityEvent::BatchCompleted.into(), RawEvent::ProxyExecuted(Ok(())).into()]);
236-
assert_noop!(Proxy::proxy(Origin::signed(3), 1, None, call.clone()), Error::<Test>::Uncallable);
238+
assert_ok!(Proxy::proxy(Origin::signed(3), 1, None, call.clone()));
239+
expect_event(RawEvent::ProxyExecuted(Err(DispatchError::BadOrigin)));
237240
assert_ok!(Proxy::proxy(Origin::signed(4), 1, None, call.clone()));
238-
expect_events(vec![UtilityEvent::Uncallable(0).into(), RawEvent::ProxyExecuted(Ok(())).into()]);
241+
expect_events(vec![
242+
UtilityEvent::BatchInterrupted(0, DispatchError::BadOrigin).into(),
243+
RawEvent::ProxyExecuted(Ok(())).into(),
244+
]);
239245
});
240246
}
241247

@@ -293,10 +299,12 @@ fn proxying_works() {
293299
assert_eq!(Balances::free_balance(6), 1);
294300

295301
let call = Box::new(Call::System(SystemCall::set_code(vec![])));
296-
assert_noop!(Proxy::proxy(Origin::signed(3), 1, None, call.clone()), Error::<Test>::Uncallable);
302+
assert_ok!(Proxy::proxy(Origin::signed(3), 1, None, call.clone()));
303+
expect_event(RawEvent::ProxyExecuted(Err(DispatchError::BadOrigin)));
297304

298305
let call = Box::new(Call::Balances(BalancesCall::transfer_keep_alive(6, 1)));
299-
assert_noop!(Proxy::proxy(Origin::signed(2), 1, None, call.clone()), Error::<Test>::Uncallable);
306+
assert_ok!(Call::Proxy(super::Call::proxy(1, None, call.clone())).dispatch(Origin::signed(2)));
307+
expect_event(RawEvent::ProxyExecuted(Err(DispatchError::BadOrigin)));
300308
assert_ok!(Proxy::proxy(Origin::signed(3), 1, None, call.clone()));
301309
expect_event(RawEvent::ProxyExecuted(Ok(())));
302310
assert_eq!(Balances::free_balance(6), 2);

0 commit comments

Comments
 (0)