Skip to content

Commit 41c7729

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 <[email protected]> * 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 <[email protected]>
1 parent c97372d commit 41c7729

File tree

5 files changed

+27
-25
lines changed

5 files changed

+27
-25
lines changed

fuzzer/src/mock.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ impl Convert<u128, u64> for CurrencyToVoteHandler {
5757
pub struct Test;
5858

5959
impl frame_system::Trait for Test {
60+
type BaseCallFilter = ();
6061
type Origin = Origin;
6162
type DbWeight = ();
6263
type BlockExecutionWeight = ();

fuzzer/src/submit_solution.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ use honggfuzz::fuzz;
2323

2424
use mock::Test;
2525
use pallet_staking::testing_utils::*;
26-
use frame_support::{assert_ok, storage::StorageValue};
26+
use frame_support::{assert_ok, storage::StorageValue, traits::UnfilteredDispatchable};
2727
use frame_system::RawOrigin;
28-
use sp_runtime::{traits::Dispatchable, DispatchError};
28+
use sp_runtime::DispatchError;
2929
use sp_core::offchain::{testing::TestOffchainExt, OffchainExt};
3030
use pallet_staking::{EraElectionStatus, ElectionStatus, Module as Staking, Call as StakingCall};
3131

@@ -159,7 +159,7 @@ fn main() {
159159
match mode {
160160
Mode::WeakerSubmission => {
161161
assert_eq!(
162-
call.dispatch(origin.clone().into()).unwrap_err().error,
162+
call.dispatch_bypass_filter(origin.clone().into()).unwrap_err().error,
163163
DispatchError::Module {
164164
index: 0,
165165
error: 16,
@@ -170,7 +170,7 @@ fn main() {
170170
// NOTE: so exhaustive pattern doesn't work here.. maybe some rust issue?
171171
// or due to `#[repr(u32)]`?
172172
Mode::InitialSubmission | Mode::StrongerSubmission => {
173-
assert_ok!(call.dispatch(origin.into()));
173+
assert_ok!(call.dispatch_bypass_filter(origin.into()));
174174
}
175175
};
176176
})

src/benchmarking.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use super::*;
2121
use crate::Module as Staking;
2222
use testing_utils::*;
2323

24-
use sp_runtime::{traits::{Dispatchable, One}};
24+
use sp_runtime::traits::One;
2525
use frame_system::RawOrigin;
2626
pub use frame_benchmarking::{benchmarks, account};
2727
const SEED: u32 = 0;
@@ -379,12 +379,12 @@ benchmarks! {
379379
let current_era = CurrentEra::get().unwrap();
380380
let mut points_total = 0;
381381
let mut points_individual = Vec::new();
382-
let mut payout_calls = Vec::new();
382+
let mut payout_calls_arg = Vec::new();
383383

384384
for validator in new_validators.iter() {
385385
points_total += 10;
386386
points_individual.push((validator.clone(), 10));
387-
payout_calls.push(Call::<T>::payout_stakers(validator.clone(), current_era))
387+
payout_calls_arg.push((validator.clone(), current_era));
388388
}
389389

390390
// Give Era Points
@@ -401,8 +401,8 @@ benchmarks! {
401401

402402
let caller: T::AccountId = account("caller", 0, SEED);
403403
}: {
404-
for call in payout_calls {
405-
call.dispatch(RawOrigin::Signed(caller.clone()).into())?;
404+
for arg in payout_calls_arg {
405+
<Staking<T>>::payout_stakers(RawOrigin::Signed(caller.clone()).into(), arg.0, arg.1)?;
406406
}
407407
}
408408

src/mock.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ parameter_types! {
200200
pub const AvailableBlockRatio: Perbill = Perbill::one();
201201
}
202202
impl frame_system::Trait for Test {
203+
type BaseCallFilter = ();
203204
type Origin = Origin;
204205
type Index = AccountIndex;
205206
type BlockNumber = BlockNumber;

src/tests.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ fn force_unstake_works() {
4747
// Force unstake needs correct number of slashing spans (for weight calculation)
4848
assert_noop!(Staking::force_unstake(Origin::signed(11), 11, 0), BadOrigin);
4949
// We now force them to unstake
50-
assert_ok!(Staking::force_unstake(Origin::ROOT, 11, 2));
50+
assert_ok!(Staking::force_unstake(Origin::root(), 11, 2));
5151
// No longer bonded.
5252
assert_eq!(Staking::bonded(&11), None);
5353
// Transfer works.
@@ -1477,7 +1477,7 @@ fn on_free_balance_zero_stash_removes_validator() {
14771477
assert_eq!(Balances::total_balance(&11), 0);
14781478

14791479
// Reap the stash
1480-
assert_ok!(Staking::reap_stash(Origin::NONE, 11, 0));
1480+
assert_ok!(Staking::reap_stash(Origin::none(), 11, 0));
14811481

14821482
// Check storage items do not exist
14831483
assert!(!<Ledger<Test>>::contains_key(&10));
@@ -1533,7 +1533,7 @@ fn on_free_balance_zero_stash_removes_nominator() {
15331533
assert_eq!(Balances::total_balance(&11), 0);
15341534

15351535
// Reap the stash
1536-
assert_ok!(Staking::reap_stash(Origin::NONE, 11, 0));
1536+
assert_ok!(Staking::reap_stash(Origin::none(), 11, 0));
15371537

15381538
// Check storage items do not exist
15391539
assert!(!<Ledger<Test>>::contains_key(&10));
@@ -1928,7 +1928,7 @@ fn offence_forces_new_era() {
19281928
#[test]
19291929
fn offence_ensures_new_era_without_clobbering() {
19301930
ExtBuilder::default().build_and_execute(|| {
1931-
assert_ok!(Staking::force_new_era_always(Origin::ROOT));
1931+
assert_ok!(Staking::force_new_era_always(Origin::root()));
19321932
assert_eq!(Staking::force_era(), Forcing::ForceAlways);
19331933

19341934
on_offence_now(
@@ -2302,8 +2302,8 @@ fn garbage_collection_after_slashing() {
23022302
assert_eq!(slashing_spans.iter().count(), 2);
23032303

23042304
// reap_stash respects num_slashing_spans so that weight is accurate
2305-
assert_noop!(Staking::reap_stash(Origin::NONE, 11, 0), Error::<Test>::IncorrectSlashingSpans);
2306-
assert_ok!(Staking::reap_stash(Origin::NONE, 11, 2));
2305+
assert_noop!(Staking::reap_stash(Origin::none(), 11, 0), Error::<Test>::IncorrectSlashingSpans);
2306+
assert_ok!(Staking::reap_stash(Origin::none(), 11, 2));
23072307

23082308
assert!(<Staking as crate::Store>::SlashingSpans::get(&11).is_none());
23092309
assert_eq!(<Staking as crate::Store>::SpanSlash::get(&(11, 0)).amount_slashed(), &0);
@@ -2591,11 +2591,11 @@ fn remove_deferred() {
25912591

25922592
// fails if empty
25932593
assert_noop!(
2594-
Staking::cancel_deferred_slash(Origin::ROOT, 1, vec![]),
2594+
Staking::cancel_deferred_slash(Origin::root(), 1, vec![]),
25952595
Error::<Test>::EmptyTargets
25962596
);
25972597

2598-
assert_ok!(Staking::cancel_deferred_slash(Origin::ROOT, 1, vec![0]));
2598+
assert_ok!(Staking::cancel_deferred_slash(Origin::root(), 1, vec![0]));
25992599

26002600
assert_eq!(Balances::free_balance(11), 1000);
26012601
assert_eq!(Balances::free_balance(101), 2000);
@@ -2692,21 +2692,21 @@ fn remove_multi_deferred() {
26922692

26932693
// fails if list is not sorted
26942694
assert_noop!(
2695-
Staking::cancel_deferred_slash(Origin::ROOT, 1, vec![2, 0, 4]),
2695+
Staking::cancel_deferred_slash(Origin::root(), 1, vec![2, 0, 4]),
26962696
Error::<Test>::NotSortedAndUnique
26972697
);
26982698
// fails if list is not unique
26992699
assert_noop!(
2700-
Staking::cancel_deferred_slash(Origin::ROOT, 1, vec![0, 2, 2]),
2700+
Staking::cancel_deferred_slash(Origin::root(), 1, vec![0, 2, 2]),
27012701
Error::<Test>::NotSortedAndUnique
27022702
);
27032703
// fails if bad index
27042704
assert_noop!(
2705-
Staking::cancel_deferred_slash(Origin::ROOT, 1, vec![1, 2, 3, 4, 5]),
2705+
Staking::cancel_deferred_slash(Origin::root(), 1, vec![1, 2, 3, 4, 5]),
27062706
Error::<Test>::InvalidSlashIndex
27072707
);
27082708

2709-
assert_ok!(Staking::cancel_deferred_slash(Origin::ROOT, 1, vec![0, 2, 4]));
2709+
assert_ok!(Staking::cancel_deferred_slash(Origin::root(), 1, vec![0, 2, 4]));
27102710

27112711
let slashes = <Staking as Store>::UnappliedSlashes::get(&1);
27122712
assert_eq!(slashes.len(), 2);
@@ -4243,16 +4243,16 @@ fn test_max_nominator_rewarded_per_validator_and_cant_steal_someone_else_reward(
42434243
fn set_history_depth_works() {
42444244
ExtBuilder::default().build_and_execute(|| {
42454245
mock::start_era(10);
4246-
Staking::set_history_depth(Origin::ROOT, 20, 0).unwrap();
4246+
Staking::set_history_depth(Origin::root(), 20, 0).unwrap();
42474247
assert!(<Staking as Store>::ErasTotalStake::contains_key(10 - 4));
42484248
assert!(<Staking as Store>::ErasTotalStake::contains_key(10 - 5));
4249-
Staking::set_history_depth(Origin::ROOT, 4, 0).unwrap();
4249+
Staking::set_history_depth(Origin::root(), 4, 0).unwrap();
42504250
assert!(<Staking as Store>::ErasTotalStake::contains_key(10 - 4));
42514251
assert!(!<Staking as Store>::ErasTotalStake::contains_key(10 - 5));
4252-
Staking::set_history_depth(Origin::ROOT, 3, 0).unwrap();
4252+
Staking::set_history_depth(Origin::root(), 3, 0).unwrap();
42534253
assert!(!<Staking as Store>::ErasTotalStake::contains_key(10 - 4));
42544254
assert!(!<Staking as Store>::ErasTotalStake::contains_key(10 - 5));
4255-
Staking::set_history_depth(Origin::ROOT, 8, 0).unwrap();
4255+
Staking::set_history_depth(Origin::root(), 8, 0).unwrap();
42564256
assert!(!<Staking as Store>::ErasTotalStake::contains_key(10 - 4));
42574257
assert!(!<Staking as Store>::ErasTotalStake::contains_key(10 - 5));
42584258
});

0 commit comments

Comments
 (0)