Skip to content

Commit

Permalink
Code review (#1385)
Browse files Browse the repository at this point in the history
  • Loading branch information
aurexav authored Jan 17, 2024
1 parent 6eb64be commit 87531dd
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 68 deletions.
105 changes: 54 additions & 51 deletions runtime/common/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ macro_rules! impl_account_migration_tests {
#[test]
fn validate_evm_account_already_exist() {
let (from, _) = alice();
let to = H160::from_low_u64_be(33).into();
let to = H160::from_low_u64_be(0).into();

ExtBuilder::default().with_balances(vec![(to, RING_AMOUNT)]).build().execute_with(
|| {
Expand All @@ -188,7 +188,7 @@ macro_rules! impl_account_migration_tests {
#[test]
fn validate_invalid_sig() {
let (from, from_pk) = alice();
let to = H160::from_low_u64_be(33).into();
let to = H160::from_low_u64_be(0).into();
let message = darwinia_account_migration::signable_message(b"?", &to);
let sig = from.sign(&message);

Expand Down Expand Up @@ -861,84 +861,87 @@ macro_rules! impl_maintenance_tests {
use sp_core::H160;
use sp_runtime::{traits::Dispatchable, DispatchError};

pub fn full_name(pallet_name: &[u8], call_name: &[u8]) -> RuntimeCallNameOf<Runtime> {
fn full_name(pallet_name: &[u8], call_name: &[u8]) -> RuntimeCallNameOf<Runtime> {
<RuntimeCallNameOf<Runtime>>::from((
pallet_name.to_vec().try_into().unwrap(),
call_name.to_vec().try_into().unwrap(),
))
}

#[test]
fn tx_pause_origins_work_correctly() {
fn tx_pause_origins_should_work() {
ExtBuilder::default().build().execute_with(|| {
assert_ok!(TxPause::pause(
RuntimeOrigin::root(),
full_name(b"Balances", b"transfer")
));

assert_err!(
TxPause::pause(
RuntimeOrigin::signed(H160::default().into()),
full_name(b"Balances", b"transfer")
),
DispatchError::BadOrigin
);
})
});
}

#[test]
fn tx_pause_pause_and_unpause_work_correctly() {
let from = H160::from_low_u64_be(555).into();
let to = H160::from_low_u64_be(333).into();
ExtBuilder::default()
.with_balances(vec![(from, 100), (to, 50)])
.build()
.execute_with(|| {
assert_ok!(TxPause::pause(
RuntimeOrigin::root(),
full_name(b"System", b"remark")
));
assert_err!(
RuntimeCall::System(frame_system::Call::remark {
remark: b"hello world".to_vec()
})
.dispatch(RuntimeOrigin::signed(from)),
frame_system::Error::<Runtime>::CallFiltered
);
fn tx_pause_pause_and_unpause_should_work() {
let from = H160::from_low_u64_be(0).into();
let to = H160::from_low_u64_be(1).into();

assert_ok!(TxPause::unpause(
RuntimeOrigin::root(),
full_name(b"System", b"remark")
));
assert_ok!(RuntimeCall::System(frame_system::Call::remark {
remark: b"hello world".to_vec(),
})
.dispatch(RuntimeOrigin::signed(from)));
})
}

#[test]
fn tx_pause_pause_calls_except_on_whitelist() {
let from = H160::from_low_u64_be(555).into();
ExtBuilder::default().with_balances(vec![(from, 100)]).build().execute_with(|| {
assert_ok!(RuntimeCall::System(frame_system::Call::remark_with_event {
remark: b"hello world".to_vec(),
})
.dispatch(RuntimeOrigin::signed(from)));

assert_ok!(TxPause::pause(
RuntimeOrigin::root(),
full_name(b"Balances", b"transfer")
));
assert_err!(
TxPause::pause(
RuntimeOrigin::root(),
full_name(b"System", b"remark_with_event")
),
pallet_tx_pause::Error::<Runtime>::Unpausable
RuntimeCall::Balances(pallet_balances::Call::transfer {
dest: to,
value: 1,
})
.dispatch(RuntimeOrigin::signed(from)),
<frame_system::Error<Runtime>>::CallFiltered
);

assert_ok!(RuntimeCall::System(frame_system::Call::remark_with_event {
remark: b"hello world".to_vec(),
assert_ok!(TxPause::unpause(
RuntimeOrigin::root(),
full_name(b"Balances", b"transfer")
));
assert_ok!(RuntimeCall::Balances(pallet_balances::Call::transfer {
dest: to,
value: 1,
})
.dispatch(RuntimeOrigin::signed(from)));
})
});
}

#[test]
fn tx_pause_whitelist_should_work() {
ExtBuilder::default().build().execute_with(|| {
let whitelist: &[(&[u8], &[&[u8]])] = &[
(b"System", &[b"*"]),
(b"ParachainSystem", &[b"*"]),
(b"Timestamp", &[b"*"]),
(b"Session", &[b"*"]),
(b"Scheduler", &[b"*"]),
(b"Preimage", &[b"*"]),
(b"TxPause", &[b"*"]),
(b"TechnicalCommittee", &[b"*"]),
(b"ConvictionVoting", &[b"*"]),
(b"Referenda", &[b"*"]),
(b"Whitelist", &[b"*"]),
];

whitelist.iter().for_each(|(p, cs)| {
cs.iter().for_each(|c| {
assert_err!(
TxPause::pause(RuntimeOrigin::root(), full_name(p, c)),
<pallet_tx_pause::Error<Runtime>>::Unpausable
);
});
});
});
}
}
};
Expand Down
14 changes: 14 additions & 0 deletions runtime/crab/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ impl frame_support::traits::OnRuntimeUpgrade for CustomOnRuntimeUpgrade {
}

fn migrate() -> frame_support::weights::Weight {
let _ =
migration::clear_storage_prefix(b"MessageGadget", b"CommitmentContract", &[], None, None);
let _ = migration::clear_storage_prefix(b"EcdsaAuthority", b"Authorities", &[], None, None);
let _ = migration::clear_storage_prefix(b"EcdsaAuthority", b"NextAuthorities", &[], None, None);
let _ = migration::clear_storage_prefix(b"EcdsaAuthority", b"Nonce", &[], None, None);
let _ = migration::clear_storage_prefix(
b"EcdsaAuthority",
b"AuthoritiesChangeToSign",
&[],
None,
None,
);
let _ =
migration::clear_storage_prefix(b"EcdsaAuthority", b"MessageRootToSign", &[], None, None);
let _ = migration::clear_storage_prefix(b"Council", b"Proposals", &[], None, None);
let _ = migration::clear_storage_prefix(b"Council", b"ProposalOf", &[], None, None);
let _ = migration::clear_storage_prefix(b"Council", b"Voting", &[], None, None);
Expand Down
20 changes: 16 additions & 4 deletions runtime/crab/src/pallets/tx_pause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,27 @@
// darwinia
use crate::*;

/// Calls that cannot be paused by the tx-pause pallet.
pub struct TxPauseWhitelistedCalls;
impl frame_support::traits::Contains<pallet_tx_pause::RuntimeCallNameOf<Runtime>>
for TxPauseWhitelistedCalls
{
fn contains(full_name: &pallet_tx_pause::RuntimeCallNameOf<Runtime>) -> bool {
matches!(
(full_name.0.as_slice(), full_name.1.as_slice()),
(b"System", b"remark_with_event")
let pallet = full_name.0.as_slice();

// Pallets that can be paused by the tx-pause pallet.
!matches!(
pallet,
b"Balances"
| b"Assets" | b"Vesting"
| b"Deposit" | b"AccountMigration"
| b"DarwiniaStaking"
| b"Ethereum" | b"EVM"
| b"MessageTransact"
| b"BridgePolkadotGrandpa"
| b"BridgePolkadotParachain"
| b"BridgeDarwiniaMessages"
| b"BridgeDarwiniaDispatch"
| b"DarwiniaFeeMarket"
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/darwinia/src/pallets/governance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl pallet_referenda::Config for Runtime {
type SubmitOrigin = frame_system::EnsureSigned<AccountId>;
type Tally = pallet_conviction_voting::TallyOf<Self>;
type Tracks = TracksInfo;
type UndecidingTimeout = ConstU32<{ 14 * DAYS }>;
type UndecidingTimeout = ConstU32<{ 28 * DAYS }>;
type Votes = pallet_conviction_voting::VotesOf<Self>;
// type WeightInfo = weights::pallet_referenda::WeightInfo<Self>;
type WeightInfo = ();
Expand Down
20 changes: 16 additions & 4 deletions runtime/darwinia/src/pallets/tx_pause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,27 @@
// darwinia
use crate::*;

/// Calls that cannot be paused by the tx-pause pallet.
pub struct TxPauseWhitelistedCalls;
impl frame_support::traits::Contains<pallet_tx_pause::RuntimeCallNameOf<Runtime>>
for TxPauseWhitelistedCalls
{
fn contains(full_name: &pallet_tx_pause::RuntimeCallNameOf<Runtime>) -> bool {
matches!(
(full_name.0.as_slice(), full_name.1.as_slice()),
(b"System", b"remark_with_event")
let pallet = full_name.0.as_slice();

// Pallets that can be paused by the tx-pause pallet.
!matches!(
pallet,
b"Balances"
| b"Assets" | b"Vesting"
| b"Deposit" | b"AccountMigration"
| b"DarwiniaStaking"
| b"Ethereum" | b"EVM"
| b"MessageTransact"
| b"BridgePolkadotGrandpa"
| b"BridgePolkadotParachain"
| b"BridgeDarwiniaMessages"
| b"BridgeDarwiniaDispatch"
| b"DarwiniaFeeMarket"
)
}
}
Expand Down
20 changes: 16 additions & 4 deletions runtime/pangolin/src/pallets/tx_pause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,27 @@
// darwinia
use crate::*;

/// Calls that cannot be paused by the tx-pause pallet.
pub struct TxPauseWhitelistedCalls;
impl frame_support::traits::Contains<pallet_tx_pause::RuntimeCallNameOf<Runtime>>
for TxPauseWhitelistedCalls
{
fn contains(full_name: &pallet_tx_pause::RuntimeCallNameOf<Runtime>) -> bool {
matches!(
(full_name.0.as_slice(), full_name.1.as_slice()),
(b"System", b"remark_with_event")
let pallet = full_name.0.as_slice();

// Pallets that can be paused by the tx-pause pallet.
!matches!(
pallet,
b"Balances"
| b"Assets" | b"Vesting"
| b"Deposit" | b"AccountMigration"
| b"DarwiniaStaking"
| b"Ethereum" | b"EVM"
| b"MessageTransact"
| b"BridgePolkadotGrandpa"
| b"BridgePolkadotParachain"
| b"BridgeDarwiniaMessages"
| b"BridgeDarwiniaDispatch"
| b"DarwiniaFeeMarket"
)
}
}
Expand Down
20 changes: 16 additions & 4 deletions runtime/pangoro/src/pallets/tx_pause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,27 @@
// darwinia
use crate::*;

/// Calls that cannot be paused by the tx-pause pallet.
pub struct TxPauseWhitelistedCalls;
impl frame_support::traits::Contains<pallet_tx_pause::RuntimeCallNameOf<Runtime>>
for TxPauseWhitelistedCalls
{
fn contains(full_name: &pallet_tx_pause::RuntimeCallNameOf<Runtime>) -> bool {
matches!(
(full_name.0.as_slice(), full_name.1.as_slice()),
(b"System", b"remark_with_event")
let pallet = full_name.0.as_slice();

// Pallets that can be paused by the tx-pause pallet.
!matches!(
pallet,
b"Balances"
| b"Assets" | b"Vesting"
| b"Deposit" | b"AccountMigration"
| b"DarwiniaStaking"
| b"Ethereum" | b"EVM"
| b"MessageTransact"
| b"BridgePolkadotGrandpa"
| b"BridgePolkadotParachain"
| b"BridgeDarwiniaMessages"
| b"BridgeDarwiniaDispatch"
| b"DarwiniaFeeMarket"
)
}
}
Expand Down

0 comments on commit 87531dd

Please sign in to comment.