Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
tests for proxy and utility for tx-pause, fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
nuke-web3 committed Oct 12, 2022
1 parent adb3ab8 commit 0538ecd
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 21 deletions.
10 changes: 4 additions & 6 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,10 @@ pub struct UnfilterableCallNames;
/// Make Balances::transfer_keep_alive unfilterable, accept all others.
impl Contains<FullNameOf<Runtime>> for UnfilterableCallNames {
fn contains(full_name: &FullNameOf<Runtime>) -> bool {
let unpausables: Vec<FullNameOf<Runtime>> = vec![
(
b"Balances".to_vec().try_into().unwrap(),
Some(b"transfer_keep_alive".to_vec().try_into().unwrap()),
),
];
let unpausables: Vec<FullNameOf<Runtime>> = vec![(
b"Balances".to_vec().try_into().unwrap(),
Some(b"transfer_keep_alive".to_vec().try_into().unwrap()),
)];

for unpausable_call in unpausables {
let (pallet_name, maybe_call_name) = full_name;
Expand Down
6 changes: 3 additions & 3 deletions frame/safe-mode/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ pub struct UnfilterableCalls;
impl Contains<RuntimeCall> for UnfilterableCalls {
fn contains(call: &RuntimeCall) -> bool {
match call {
RuntimeCall::Balances(_) => false,
RuntimeCall::Balances(_) => false,
_ => true,
}
}
Expand Down Expand Up @@ -211,7 +211,7 @@ impl ForceExtendOrigin {
}
}

impl<O: Into<Result<RawOrigin<u64>, O>> + From<RawOrigin<u64>> + std::fmt::Debug> EnsureOrigin<O>
impl<O: Into<Result<RawOrigin<u64>, O>> + From<RawOrigin<u64>>> EnsureOrigin<O>
for ForceActivateOrigin
{
type Success = u64;
Expand All @@ -234,7 +234,7 @@ impl<O: Into<Result<RawOrigin<u64>, O>> + From<RawOrigin<u64>> + std::fmt::Debug
}
}

impl<O: Into<Result<RawOrigin<u64>, O>> + From<RawOrigin<u64>> + std::fmt::Debug> EnsureOrigin<O>
impl<O: Into<Result<RawOrigin<u64>, O>> + From<RawOrigin<u64>>> EnsureOrigin<O>
for ForceExtendOrigin
{
type Success = u64;
Expand Down
17 changes: 10 additions & 7 deletions frame/safe-mode/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,16 +134,16 @@ fn can_filter_balance_calls_when_activated() {
});
}


#[test]
fn can_filter_balance_in_batch_when_activated() {
new_test_ext().execute_with(|| {
let batch_call = RuntimeCall::Utility(pallet_utility::Call::batch { calls: vec![call_transfer()]});
let batch_call =
RuntimeCall::Utility(pallet_utility::Call::batch { calls: vec![call_transfer()] });

assert_ok!(batch_call.clone().dispatch(Origin::signed(0)));

assert_ok!(SafeMode::activate(Origin::signed(0)));

assert_ok!(batch_call.clone().dispatch(Origin::signed(0)));
System::assert_last_event(
pallet_utility::Event::BatchInterrupted {
Expand All @@ -161,13 +161,16 @@ fn can_filter_balance_in_proxy_when_activated() {
assert_ok!(Proxy::add_proxy(Origin::signed(1), 2, ProxyType::JustTransfer, 0));

assert_ok!(Proxy::proxy(Origin::signed(2), 1, None, Box::new(call_transfer())));
System::assert_last_event( pallet_proxy::Event::ProxyExecuted { result: Ok(()) }.into() );
System::assert_last_event(pallet_proxy::Event::ProxyExecuted { result: Ok(()) }.into());

assert_ok!(SafeMode::force_activate(ForceActivateOrigin::Weak.signed()));

assert_ok!(Proxy::proxy(Origin::signed(2), 1, None, Box::new(call_transfer())));
System::assert_last_event(
pallet_proxy::Event::ProxyExecuted { result: DispatchError::from(frame_system::Error::<Test>::CallFiltered).into() }.into(),
pallet_proxy::Event::ProxyExecuted {
result: DispatchError::from(frame_system::Error::<Test>::CallFiltered).into(),
}
.into(),
);
});
}
Expand Down Expand Up @@ -494,4 +497,4 @@ fn fails_when_explicit_origin_required() {

fn call_transfer() -> RuntimeCall {
RuntimeCall::Balances(pallet_balances::Call::transfer { dest: 1, value: 1 })
}
}
44 changes: 44 additions & 0 deletions frame/tx-pause/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,50 @@ fn can_unpause_specific_call() {
});
}

#[test]
fn can_filter_balance_in_batch_when_paused() {
new_test_ext().execute_with(|| {
let batch_call =
RuntimeCall::Utility(pallet_utility::Call::batch { calls: vec![call_transfer(1, 1)] });

assert_ok!(TxPause::pause_call(
Origin::signed(mock::PauseOrigin::get()),
name(b"Balances"),
name(b"transfer"),
));

assert_ok!(batch_call.clone().dispatch(Origin::signed(0)));
System::assert_last_event(
pallet_utility::Event::BatchInterrupted {
index: 0,
error: frame_system::Error::<Test>::CallFiltered.into(),
}
.into(),
);
});
}

#[test]
fn can_filter_balance_in_proxy_when_paused() {
new_test_ext().execute_with(|| {
assert_ok!(TxPause::pause_call(
Origin::signed(mock::PauseOrigin::get()),
name(b"Balances"),
name(b"transfer"),
));

assert_ok!(Proxy::add_proxy(Origin::signed(1), 2, ProxyType::JustTransfer, 0));

assert_ok!(Proxy::proxy(Origin::signed(2), 1, None, Box::new(call_transfer(1, 1))));
System::assert_last_event(
pallet_proxy::Event::ProxyExecuted {
result: DispatchError::from(frame_system::Error::<Test>::CallFiltered).into(),
}
.into(),
);
});
}

// GENERAL FAIL/NEGATIVE TESTS ---------------------

#[test]
Expand Down
10 changes: 5 additions & 5 deletions frame/tx-pause/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
//! Autogenerated weights for pallet_tx_pause
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2022-09-19, STEPS: `1`, REPEAT: 1, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! DATE: 2022-10-11, STEPS: `1`, REPEAT: 1, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! HOSTNAME: `pop-os`, CPU: `12th Gen Intel(R) Core(TM) i7-12700H`
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024

Expand Down Expand Up @@ -64,13 +64,13 @@ pub struct SubstrateWeight<T>(PhantomData<T>);
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
// Storage: TxPause PausedCalls (r:1 w:1)
fn pause_call() -> Weight {
Weight::from_ref_time(55_270_000 as u64)
Weight::from_ref_time(38_905_000 as u64)
.saturating_add(T::DbWeight::get().reads(1 as u64))
.saturating_add(T::DbWeight::get().writes(1 as u64))
}
// Storage: TxPause PausedCalls (r:1 w:1)
fn unpause_call() -> Weight {
Weight::from_ref_time(55_290_000 as u64)
Weight::from_ref_time(32_892_000 as u64)
.saturating_add(T::DbWeight::get().reads(1 as u64))
.saturating_add(T::DbWeight::get().writes(1 as u64))
}
Expand All @@ -80,13 +80,13 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
impl WeightInfo for () {
// Storage: TxPause PausedCalls (r:1 w:1)
fn pause_call() -> Weight {
Weight::from_ref_time(55_270_000 as u64)
Weight::from_ref_time(38_905_000 as u64)
.saturating_add(RocksDbWeight::get().reads(1 as u64))
.saturating_add(RocksDbWeight::get().writes(1 as u64))
}
// Storage: TxPause PausedCalls (r:1 w:1)
fn unpause_call() -> Weight {
Weight::from_ref_time(55_290_000 as u64)
Weight::from_ref_time(32_892_000 as u64)
.saturating_add(RocksDbWeight::get().reads(1 as u64))
.saturating_add(RocksDbWeight::get().writes(1 as u64))
}
Expand Down

0 comments on commit 0538ecd

Please sign in to comment.