Skip to content

Commit

Permalink
Merge pull request #291 from peaqnetwork/feat/1207512574682420_xcm_fi…
Browse files Browse the repository at this point in the history
…lter

Add safe xcm filter
  • Loading branch information
sfffaaa authored Oct 2, 2024
2 parents 4644060 + 1dcd368 commit 0c7a87c
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 16 deletions.
4 changes: 2 additions & 2 deletions pallets/inflation-manager/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ fn check_fund_enough_token_after_delayed_tge_less() {
fn set_delayed_tge_fail() {
ExternalityBuilder::default().build().execute_with(|| {
assert_noop!(
InflationManager::set_delayed_tge(RuntimeOrigin::signed(1).into(), 1, 100),
InflationManager::set_delayed_tge(RuntimeOrigin::signed(1), 1, 100),
BadOrigin
);
})
Expand Down Expand Up @@ -380,7 +380,7 @@ fn recaluclation_change_fail() {
Error::<TestRuntime>::WrongBlockSetting
);
assert_noop!(
InflationManager::set_recalculation_time(RuntimeOrigin::signed(1).into(), 5000),
InflationManager::set_recalculation_time(RuntimeOrigin::signed(1), 5000),
BadOrigin
);
})
Expand Down
16 changes: 5 additions & 11 deletions pallets/parachain-staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3397,7 +3397,7 @@ fn check_claim_block_normal_wo_delegator() {
(3, origin_balance),
(4, origin_balance),
])
.with_collators(vec![(1, 1 * stake), (2, 2 * stake), (3, 3 * stake), (4, 4 * stake)])
.with_collators(vec![(1, stake), (2, 2 * stake), (3, 3 * stake), (4, 4 * stake)])
.build()
.execute_with(|| {
let authors: Vec<Option<AccountId>> = vec![
Expand Down Expand Up @@ -3479,7 +3479,7 @@ fn check_claim_block_normal_wi_delegator() {
(9, origin_balance),
(10, origin_balance),
])
.with_collators(vec![(1, 1 * stake), (2, 2 * stake), (3, 3 * stake), (4, 4 * stake)])
.with_collators(vec![(1, stake), (2, 2 * stake), (3, 3 * stake), (4, 4 * stake)])
.with_delegators(vec![
(5, 1, 5 * stake),
(6, 1, 6 * stake),
Expand Down Expand Up @@ -3665,17 +3665,11 @@ fn collator_reward_per_session_with_delegator() {
let rewards = StakePallet::get_delgators_reward_per_session(&state, 10, 50000, 1000);
assert_eq!(
rewards[0],
Reward {
owner: 2,
amount: Perquintill::from_rational(10 as u64 * 600, 50000) * 1000
}
Reward { owner: 2, amount: Perquintill::from_rational(10_u64 * 600, 50000) * 1000 }
);
assert_eq!(
rewards[1],
Reward {
owner: 3,
amount: Perquintill::from_rational(10 as u64 * 400, 50000) * 1000
}
Reward { owner: 3, amount: Perquintill::from_rational(10_u64 * 400, 50000) * 1000 }
);
});
}
Expand All @@ -3696,7 +3690,7 @@ fn check_total_collator_staking_num() {
roll_to(4, authors.clone());

let (_weight, balance) = StakePallet::get_total_collator_staking_num();
assert_eq!(balance, 2 * (500 + 600 + 400) + 1 * (100 + 200));
assert_eq!(balance, 2 * (500 + 600 + 400) + (100 + 200));
});
}

Expand Down
54 changes: 53 additions & 1 deletion runtime/krest/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,58 @@ pub type XcmOriginToTransactDispatchOrigin = (
XcmPassthrough<RuntimeOrigin>,
);

/// A call filter for the XCM Transact instruction. This is a temporary measure until we properly
/// account for proof size weights.
pub struct SafeCallFilter;
impl SafeCallFilter {
// 1. RuntimeCall::EVM(..) & RuntimeCall::Ethereum(..) have to be prohibited since we cannot
// measure PoV size properly
// 2. RuntimeCall::Contracts(..) can be allowed, but it hasn't been tested properly yet.

/// Checks whether the base (non-composite) call is allowed to be executed via `Transact` XCM
/// instruction.
pub fn allow_base_call(call: &RuntimeCall) -> bool {
matches!(
call,
RuntimeCall::System(..) |
RuntimeCall::Balances(..) |
RuntimeCall::Vesting(..) |
RuntimeCall::Assets(..) |
RuntimeCall::PolkadotXcm(..) |
RuntimeCall::Session(..) |
RuntimeCall::Multisig(
pallet_multisig::Call::approve_as_multi { .. } |
pallet_multisig::Call::cancel_as_multi { .. },
)
)
}
/// Checks whether composite call is allowed to be executed via `Transact` XCM instruction.
///
/// Each composite call's subcalls are checked against base call filter. No nesting of composite
/// calls is allowed.
pub fn allow_composite_call(call: &RuntimeCall) -> bool {
match call {
RuntimeCall::Utility(pallet_utility::Call::batch { calls, .. }) =>
calls.iter().all(Self::allow_base_call),
RuntimeCall::Utility(pallet_utility::Call::batch_all { calls, .. }) =>
calls.iter().all(Self::allow_base_call),
RuntimeCall::Utility(pallet_utility::Call::as_derivative { call, .. }) =>
Self::allow_base_call(call),
RuntimeCall::Multisig(pallet_multisig::Call::as_multi_threshold_1 { call, .. }) =>
Self::allow_base_call(call),
RuntimeCall::Multisig(pallet_multisig::Call::as_multi { call, .. }) =>
Self::allow_base_call(call),
_ => false,
}
}
}

impl Contains<RuntimeCall> for SafeCallFilter {
fn contains(call: &RuntimeCall) -> bool {
Self::allow_base_call(call) || Self::allow_composite_call(call)
}
}

parameter_types! {
// One XCM operation is 1_000_000_000 weight - almost certainly a conservative estimate.
pub const UnitWeightCost: Weight = Weight::from_parts(1_000_000_000, 1024);
Expand Down Expand Up @@ -282,7 +334,7 @@ impl xcm_executor::Config for XcmConfig {
>;
type MessageExporter = ();
type UniversalAliases = Nothing;
type SafeCallFilter = Everything;
type SafeCallFilter = SafeCallFilter;
type Aliasers = Nothing;

type TransactionalProcessor = FrameTransactionalProcessor;
Expand Down
54 changes: 53 additions & 1 deletion runtime/peaq-dev/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,58 @@ pub type XcmOriginToTransactDispatchOrigin = (
XcmPassthrough<RuntimeOrigin>,
);

/// A call filter for the XCM Transact instruction. This is a temporary measure until we properly
/// account for proof size weights.
pub struct SafeCallFilter;
impl SafeCallFilter {
// 1. RuntimeCall::EVM(..) & RuntimeCall::Ethereum(..) have to be prohibited since we cannot
// measure PoV size properly
// 2. RuntimeCall::Contracts(..) can be allowed, but it hasn't been tested properly yet.

/// Checks whether the base (non-composite) call is allowed to be executed via `Transact` XCM
/// instruction.
pub fn allow_base_call(call: &RuntimeCall) -> bool {
matches!(
call,
RuntimeCall::System(..) |
RuntimeCall::Balances(..) |
RuntimeCall::Vesting(..) |
RuntimeCall::Assets(..) |
RuntimeCall::PolkadotXcm(..) |
RuntimeCall::Session(..) |
RuntimeCall::Multisig(
pallet_multisig::Call::approve_as_multi { .. } |
pallet_multisig::Call::cancel_as_multi { .. },
)
)
}
/// Checks whether composite call is allowed to be executed via `Transact` XCM instruction.
///
/// Each composite call's subcalls are checked against base call filter. No nesting of composite
/// calls is allowed.
pub fn allow_composite_call(call: &RuntimeCall) -> bool {
match call {
RuntimeCall::Utility(pallet_utility::Call::batch { calls, .. }) =>
calls.iter().all(Self::allow_base_call),
RuntimeCall::Utility(pallet_utility::Call::batch_all { calls, .. }) =>
calls.iter().all(Self::allow_base_call),
RuntimeCall::Utility(pallet_utility::Call::as_derivative { call, .. }) =>
Self::allow_base_call(call),
RuntimeCall::Multisig(pallet_multisig::Call::as_multi_threshold_1 { call, .. }) =>
Self::allow_base_call(call),
RuntimeCall::Multisig(pallet_multisig::Call::as_multi { call, .. }) =>
Self::allow_base_call(call),
_ => false,
}
}
}

impl Contains<RuntimeCall> for SafeCallFilter {
fn contains(call: &RuntimeCall) -> bool {
Self::allow_base_call(call) || Self::allow_composite_call(call)
}
}

parameter_types! {
// One XCM operation is 1_000_000_000 weight - almost certainly a conservative estimate.
pub const UnitWeightCost: Weight = Weight::from_parts(1_000_000_000, 1024);
Expand Down Expand Up @@ -282,7 +334,7 @@ impl xcm_executor::Config for XcmConfig {
>;
type MessageExporter = ();
type UniversalAliases = Nothing;
type SafeCallFilter = Everything;
type SafeCallFilter = SafeCallFilter;
type Aliasers = Nothing;

type TransactionalProcessor = FrameTransactionalProcessor;
Expand Down
54 changes: 53 additions & 1 deletion runtime/peaq/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,58 @@ pub type XcmOriginToTransactDispatchOrigin = (
XcmPassthrough<RuntimeOrigin>,
);

/// A call filter for the XCM Transact instruction. This is a temporary measure until we properly
/// account for proof size weights.
pub struct SafeCallFilter;
impl SafeCallFilter {
// 1. RuntimeCall::EVM(..) & RuntimeCall::Ethereum(..) have to be prohibited since we cannot
// measure PoV size properly
// 2. RuntimeCall::Contracts(..) can be allowed, but it hasn't been tested properly yet.

/// Checks whether the base (non-composite) call is allowed to be executed via `Transact` XCM
/// instruction.
pub fn allow_base_call(call: &RuntimeCall) -> bool {
matches!(
call,
RuntimeCall::System(..) |
RuntimeCall::Balances(..) |
RuntimeCall::Vesting(..) |
RuntimeCall::Assets(..) |
RuntimeCall::PolkadotXcm(..) |
RuntimeCall::Session(..) |
RuntimeCall::Multisig(
pallet_multisig::Call::approve_as_multi { .. } |
pallet_multisig::Call::cancel_as_multi { .. },
)
)
}
/// Checks whether composite call is allowed to be executed via `Transact` XCM instruction.
///
/// Each composite call's subcalls are checked against base call filter. No nesting of composite
/// calls is allowed.
pub fn allow_composite_call(call: &RuntimeCall) -> bool {
match call {
RuntimeCall::Utility(pallet_utility::Call::batch { calls, .. }) =>
calls.iter().all(Self::allow_base_call),
RuntimeCall::Utility(pallet_utility::Call::batch_all { calls, .. }) =>
calls.iter().all(Self::allow_base_call),
RuntimeCall::Utility(pallet_utility::Call::as_derivative { call, .. }) =>
Self::allow_base_call(call),
RuntimeCall::Multisig(pallet_multisig::Call::as_multi_threshold_1 { call, .. }) =>
Self::allow_base_call(call),
RuntimeCall::Multisig(pallet_multisig::Call::as_multi { call, .. }) =>
Self::allow_base_call(call),
_ => false,
}
}
}

impl Contains<RuntimeCall> for SafeCallFilter {
fn contains(call: &RuntimeCall) -> bool {
Self::allow_base_call(call) || Self::allow_composite_call(call)
}
}

parameter_types! {
// One XCM operation is 1_000_000_000 weight - almost certainly a conservative estimate.
pub const UnitWeightCost: Weight = Weight::from_parts(1_000_000_000, 1024);
Expand Down Expand Up @@ -282,7 +334,7 @@ impl xcm_executor::Config for XcmConfig {
>;
type MessageExporter = ();
type UniversalAliases = Nothing;
type SafeCallFilter = Everything;
type SafeCallFilter = SafeCallFilter;
type Aliasers = Nothing;

type TransactionalProcessor = FrameTransactionalProcessor;
Expand Down

0 comments on commit 0c7a87c

Please sign in to comment.