XCM: Use matcher API in DenyReserveAssetTransferToRelay (#2290)

* Use matcher API in DenyReserveAssetTransferToRelay

* Fixes

* Fixes

* Fixes

* cargo fmt
This commit is contained in:
Keith Yeung
2023-03-10 05:44:43 -08:00
committed by GitHub
parent 39e5da3c3f
commit ce03148b28
2 changed files with 58 additions and 48 deletions
+30 -24
View File
@@ -1,12 +1,12 @@
use crate::impls::AccountIdOf;
use core::marker::PhantomData;
use core::{marker::PhantomData, ops::ControlFlow};
use frame_support::{
log,
traits::{fungibles::Inspect, tokens::BalanceConversion, ContainsPair},
weights::{Weight, WeightToFee, WeightToFeePolynomial},
};
use sp_runtime::traits::Get;
use xcm::latest::prelude::*;
use xcm::{latest::prelude::*, CreateMatcher, MatchXcm};
use xcm_executor::traits::ShouldExecute;
//TODO: move DenyThenTry to polkadot's xcm module.
@@ -42,32 +42,38 @@ impl ShouldExecute for DenyReserveTransferToRelayChain {
_max_weight: Weight,
_weight_credit: &mut Weight,
) -> Result<(), ()> {
if message.iter().any(|inst| {
matches!(
inst,
message.matcher().match_next_inst_while(
|_| true,
|inst| match inst {
InitiateReserveWithdraw {
reserve: MultiLocation { parents: 1, interior: Here },
..
} | DepositReserveAsset { dest: MultiLocation { parents: 1, interior: Here }, .. } |
TransferReserveAsset {
dest: MultiLocation { parents: 1, interior: Here },
..
}
)
}) {
return Err(()) // Deny
}
} |
DepositReserveAsset {
dest: MultiLocation { parents: 1, interior: Here }, ..
} |
TransferReserveAsset {
dest: MultiLocation { parents: 1, interior: Here }, ..
} => {
Err(()) // Deny
},
// An unexpected reserve transfer has arrived from the Relay Chain. Generally,
// `IsReserve` should not allow this, but we just log it here.
ReserveAssetDeposited { .. }
if matches!(origin, MultiLocation { parents: 1, interior: Here }) =>
{
log::warn!(
target: "xcm::barrier",
"Unexpected ReserveAssetDeposited from the Relay Chain",
);
Ok(ControlFlow::Continue(()))
},
_ => Ok(ControlFlow::Continue(())),
},
)?;
// An unexpected reserve transfer has arrived from the Relay Chain. Generally, `IsReserve`
// should not allow this, but we just log it here.
if matches!(origin, MultiLocation { parents: 1, interior: Here }) &&
message.iter().any(|inst| matches!(inst, ReserveAssetDeposited { .. }))
{
log::warn!(
target: "xcm::barriers",
"Unexpected ReserveAssetDeposited from the Relay Chain",
);
}
// Permit everything else
Ok(())
}