Add Call Filter That Prevents Nested batch_all (#9009)

* add filter preventing nested `batch_all`

* more tests

* fix test

* cargo run --release --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_utility --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/utility/src/weights.rs --template=./.maintain/frame-weight-template.hbs

Co-authored-by: Parity Bot <admin@parity.io>
This commit is contained in:
Shawn Tabrizi
2021-06-03 09:05:02 -04:00
committed by GitHub
parent 94717b93db
commit f585bf1c1e
3 changed files with 71 additions and 26 deletions
+41
View File
@@ -519,3 +519,44 @@ fn batch_all_handles_weight_refund() {
);
});
}
#[test]
fn batch_all_does_not_nest() {
new_test_ext().execute_with(|| {
let batch_all = Call::Utility(
UtilityCall::batch_all(
vec![
Call::Balances(BalancesCall::transfer(2, 1)),
Call::Balances(BalancesCall::transfer(2, 1)),
Call::Balances(BalancesCall::transfer(2, 1)),
]
)
);
let info = batch_all.get_dispatch_info();
assert_eq!(Balances::free_balance(1), 10);
assert_eq!(Balances::free_balance(2), 10);
// A nested batch_all call will not pass the filter, and fail with `BadOrigin`.
assert_noop!(
Utility::batch_all(Origin::signed(1), vec![batch_all.clone()]),
DispatchErrorWithPostInfo {
post_info: PostDispatchInfo {
actual_weight: Some(<Test as Config>::WeightInfo::batch_all(1) + info.weight),
pays_fee: Pays::Yes
},
error: DispatchError::BadOrigin,
}
);
// And for those who want to get a little fancy, we check that the filter persists across
// other kinds of dispatch wrapping functions... in this case `batch_all(batch(batch_all(..)))`
let batch_nested = Call::Utility(UtilityCall::batch(vec![batch_all]));
// Batch will end with `Ok`, but does not actually execute as we can see from the event
// and balances.
assert_ok!(Utility::batch_all(Origin::signed(1), vec![batch_nested]));
System::assert_has_event(utility::Event::BatchInterrupted(0, DispatchError::BadOrigin).into());
assert_eq!(Balances::free_balance(1), 10);
assert_eq!(Balances::free_balance(2), 10);
});
}