Skip to content

Commit

Permalink
Refactor DenyInstructionsWithXcm with new NestedXcmType
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondkfcheung committed Jan 31, 2025
1 parent 797f868 commit 72f8fe4
Showing 1 changed file with 84 additions and 48 deletions.
132 changes: 84 additions & 48 deletions polkadot/xcm/xcm-builder/src/barriers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,27 @@ impl DenyExecution for DenyReserveTransferToRelayChain {
}
}

enum NestedXcmType<'a, RuntimeCall> {
Local(&'a mut RuntimeCall),
Remote(&'a mut RuntimeCall),
None,
}

impl<'a, RuntimeCall> From<Instruction<RuntimeCall>> for NestedXcmType<'a, RuntimeCall> {
fn from(value: Instruction<RuntimeCall>) -> Self {
match value {
// Local XCMs
ExecuteWithOrigin { mut xcm, .. } | SetAppendix(xcm) | SetErrorHandler(xcm) =>

Check failure on line 557 in polkadot/xcm/xcm-builder/src/barriers.rs

View workflow job for this annotation

GitHub Actions / cargo-check-all-crate-macos

variable `xcm` is bound inconsistently across alternatives separated by `|`
NestedXcmType::Local(&mut xcm),

Check failure on line 558 in polkadot/xcm/xcm-builder/src/barriers.rs

View workflow job for this annotation

GitHub Actions / cargo-check-all-crate-macos

mismatched types
// Remote XCMs
DepositReserveAsset { mut xcm, .. } |
InitiateReserveWithdraw { xcm, .. } |

Check failure on line 561 in polkadot/xcm/xcm-builder/src/barriers.rs

View workflow job for this annotation

GitHub Actions / cargo-check-all-crate-macos

variable `xcm` is bound inconsistently across alternatives separated by `|`
TransferReserveAsset { xcm, .. } => NestedXcmType::Remote(&mut xcm),

Check failure on line 562 in polkadot/xcm/xcm-builder/src/barriers.rs

View workflow job for this annotation

GitHub Actions / cargo-check-all-crate-macos

mismatched types
_ => NestedXcmType::None,
}
}
}

environmental::environmental!(recursion_count: u8);

// TBD:
Expand All @@ -553,6 +574,60 @@ environmental::environmental!(recursion_count: u8);
/// Note: The nested XCM is checked recursively!
pub struct DenyInstructionsWithXcm<Inner>(PhantomData<Inner>);

impl<Inner: DenyExecution> DenyInstructionsWithXcm<Inner> {
fn deny_recursively<RuntimeCall>(
origin: &Location,
nested_xcm: &mut [Instruction<RuntimeCall>],
max_weight: Weight,
properties: &mut Properties,
) -> Result<ControlFlow<()>, ProcessMessageError> {
// Check xcm instructions with `Inner` filter
let _ = Inner::deny_execution(origin, nested_xcm.inner_mut(), max_weight, properties)

Check failure on line 585 in polkadot/xcm/xcm-builder/src/barriers.rs

View workflow job for this annotation

GitHub Actions / cargo-check-all-crate-macos

no method named `inner_mut` found for mutable reference `&mut [staging_xcm::v5::Instruction<RuntimeCall>]` in the current scope
.inspect_err(|e| {
log::warn!(
target: "xcm::barriers",
"DenyInstructionsWithXcm::Inner denied execution, origin: {:?}, nested_xcm: {:?}, error: {:?}",
origin, nested_xcm, e
);
})?;

// Initialize the recursion count only the first time we hit this code in our potential
// recursive execution.
let _ = recursion_count::using_once(&mut 1, || {
recursion_count::with(|count| {
if *count > xcm_executor::RECURSION_LIMIT {
log::error!(
target: "xcm::barriers",
"Recursion limit exceeded, origin: {:?}, nested_xcm: {:?}, count: {count}",
origin, nested_xcm
);

return Err(ProcessMessageError::StackLimitReached);
}

*count = count.saturating_add(1);

Ok(())
})
// This should always return `Some`, but let's play it safe.
.unwrap_or(Ok(()))?;

// Ensure that we always decrement the counter whenever we finish processing the
// `DenyInstructionsWithXcm`.
sp_core::defer! {
recursion_count::with(|count| {
*count = count.saturating_sub(1);
});
}

// Check recursively with `DenyInstructionsWithXcm`
Self::deny_execution(origin, nested_xcm.inner_mut(), max_weight, properties)

Check failure on line 624 in polkadot/xcm/xcm-builder/src/barriers.rs

View workflow job for this annotation

GitHub Actions / cargo-check-all-crate-macos

no method named `inner_mut` found for mutable reference `&mut [staging_xcm::v5::Instruction<RuntimeCall>]` in the current scope
})?;

Ok(ControlFlow::Continue(()))
}
}

impl<Inner: DenyExecution> DenyExecution for DenyInstructionsWithXcm<Inner> {
fn deny_execution<RuntimeCall>(
origin: &Location,
Expand All @@ -562,54 +637,15 @@ impl<Inner: DenyExecution> DenyExecution for DenyInstructionsWithXcm<Inner> {
) -> Result<(), ProcessMessageError> {
instructions.matcher().match_next_inst_while(
|_| true,
|inst| match inst {
SetAppendix(nested_xcm) |
SetErrorHandler(nested_xcm) |
ExecuteWithOrigin { xcm: nested_xcm, .. } => {
// Check xcm instructions with `Inner` filter
let _ = Inner::deny_execution(origin, nested_xcm.inner_mut(), max_weight, properties)
.inspect_err(|e| {
log::warn!(
target: "xcm::barriers",
"DenyInstructionsWithXcm::Inner denied execution, origin: {:?}, nested_xcm: {:?}, error: {:?}",
origin, nested_xcm, e
);
})?;

// Initialize the recursion count only the first time we hit this code in our
// potential recursive execution.
let _ = recursion_count::using_once(&mut 1, || {
recursion_count::with(|count| {
if *count > xcm_executor::RECURSION_LIMIT {
log::error!(
target: "xcm::barriers",
"Recursion limit exceeded, origin: {:?}, nested_xcm: {:?}, count: {count}",
origin, nested_xcm
);

return Err(ProcessMessageError::StackLimitReached);
}
*count = count.saturating_add(1);
Ok(())
})
// This should always return `Some`, but let's play it safe.
.unwrap_or(Ok(()))?;

// Ensure that we always decrement the counter whenever we finish processing
// the `DenyInstructionsWithXcm`.
sp_core::defer! {
recursion_count::with(|count| {
*count = count.saturating_sub(1);
});
}

// Check recursively with `DenyInstructionsWithXcm`
Self::deny_execution(origin, nested_xcm.inner_mut(), max_weight, properties)
})?;

Ok(ControlFlow::Continue(()))
},
_ => Ok(ControlFlow::Continue(())),
|inst| {
let nested_xcm_type: NestedXcmType<RuntimeCall> = inst.into();

Check failure on line 641 in polkadot/xcm/xcm-builder/src/barriers.rs

View workflow job for this annotation

GitHub Actions / cargo-check-all-crate-macos

the trait bound `NestedXcmType<'_, RuntimeCall>: From<&mut staging_xcm::v5::Instruction<RuntimeCall>>` is not satisfied
match nested_xcm_type {
NestedXcmType::Local(nested_xcm) =>
Self::deny_recursively(origin, nested_xcm, max_weight, properties),

Check failure on line 644 in polkadot/xcm/xcm-builder/src/barriers.rs

View workflow job for this annotation

GitHub Actions / cargo-check-all-crate-macos

mismatched types
NestedXcmType::Remote(nested_xcm) =>
Self::deny_recursively(origin, nested_xcm, max_weight, properties),

Check failure on line 646 in polkadot/xcm/xcm-builder/src/barriers.rs

View workflow job for this annotation

GitHub Actions / cargo-check-all-crate-macos

mismatched types
NestedXcmType::None => Ok(ControlFlow::Continue(())),
}
},
)?;

Expand Down

0 comments on commit 72f8fe4

Please sign in to comment.