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

Dispatchable XCMs should translate to v0 #4062

Merged
merged 3 commits into from
Oct 12, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion runtime/kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1323,7 +1323,7 @@ parameter_types! {
/// individual routers.
pub type XcmRouter = (
// Only one router so far - use DMP to communicate with child parachains.
xcm_sender::ChildParachainRouter<Runtime, xcm::AlwaysRelease>,
xcm_sender::ChildParachainRouter<Runtime, XcmPallet>,
);

parameter_types! {
Expand Down
2 changes: 1 addition & 1 deletion runtime/rococo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ parameter_types! {
/// individual routers.
pub type XcmRouter = (
// Only one router so far - use DMP to communicate with child parachains.
xcm_sender::ChildParachainRouter<Runtime, xcm::AlwaysRelease>,
xcm_sender::ChildParachainRouter<Runtime, XcmPallet>,
);

parameter_types! {
Expand Down
43 changes: 27 additions & 16 deletions xcm/pallet-xcm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,16 +526,21 @@ pub mod pallet {
.map_err(|_| Error::<T>::CannotReanchor)?;
let max_assets = assets.len() as u32;
let assets = assets.into();
let mut remote_message = Xcm(vec![
BuyExecution { fees, weight_limit: Limited(0) },
DepositAsset { assets: Wild(All), max_assets, beneficiary },
Comment on lines +530 to +531
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I should put this in the README for XCMv2, highlighting that it's very important for Weigher implementations to give proper weights for BuyExecution and DepositAsset.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah - as long as chains are on v1/v0, they must use the same (or lower) weights as Kusama.

]);
// use local weight for remote message and hope for the best.
let remote_weight = T::Weigher::weight(&mut remote_message)
.map_err(|()| Error::<T>::UnweighableMessage)?;
if let Some(BuyExecution { weight_limit: Limited(ref mut limit), .. }) =
remote_message.0.get_mut(0)
{
*limit = remote_weight;
}
let mut message = Xcm(vec![
WithdrawAsset(assets),
InitiateTeleport {
assets: Wild(All),
dest,
xcm: Xcm(vec![
BuyExecution { fees, weight_limit: Unlimited },
DepositAsset { assets: Wild(All), max_assets, beneficiary },
]),
},
InitiateTeleport { assets: Wild(All), dest, xcm: remote_message.into() },
]);
let weight =
T::Weigher::weight(&mut message).map_err(|()| Error::<T>::UnweighableMessage)?;
Expand Down Expand Up @@ -597,14 +602,20 @@ pub mod pallet {
.map_err(|_| Error::<T>::CannotReanchor)?;
let max_assets = assets.len() as u32;
let assets = assets.into();
let mut message = Xcm(vec![TransferReserveAsset {
assets,
dest,
xcm: Xcm(vec![
BuyExecution { fees, weight_limit: Unlimited },
DepositAsset { assets: Wild(All), max_assets, beneficiary },
]),
}]);
let mut remote_message = Xcm(vec![
BuyExecution { fees, weight_limit: Limited(0) },
DepositAsset { assets: Wild(All), max_assets, beneficiary },
]);
// use local weight for remote message and hope for the best.
let remote_weight = T::Weigher::weight(&mut remote_message)
.map_err(|()| Error::<T>::UnweighableMessage)?;
if let Some(BuyExecution { weight_limit: Limited(ref mut limit), .. }) =
remote_message.0.get_mut(0)
{
*limit = remote_weight;
}
let mut message =
Xcm(vec![TransferReserveAsset { assets, dest, xcm: remote_message.into() }]);
let weight =
T::Weigher::weight(&mut message).map_err(|()| Error::<T>::UnweighableMessage)?;
let outcome =
Expand Down
5 changes: 5 additions & 0 deletions xcm/pallet-xcm/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,11 @@ pub(crate) fn buy_execution<C>(fees: impl Into<MultiAsset>) -> Instruction<C> {
BuyExecution { fees: fees.into(), weight_limit: Unlimited }
}

pub(crate) fn buy_limited_execution<C>(fees: impl Into<MultiAsset>, weight: u64) -> Instruction<C> {
use xcm::latest::prelude::*;
BuyExecution { fees: fees.into(), weight_limit: Limited(weight) }
}

pub(crate) fn new_test_ext_with_balances(
balances: Vec<(AccountId, Balance)>,
) -> sp_io::TestExternalities {
Expand Down
21 changes: 19 additions & 2 deletions xcm/pallet-xcm/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,29 @@ fn teleport_assets_works() {
new_test_ext_with_balances(balances).execute_with(|| {
let weight = 2 * BaseXcmWeight::get();
assert_eq!(Balances::total_balance(&ALICE), INITIAL_BALANCE);
let dest: MultiLocation = AccountId32 { network: Any, id: BOB.into() }.into();
assert_ok!(XcmPallet::teleport_assets(
Origin::signed(ALICE),
Box::new(RelayLocation::get().into()),
Box::new(AccountId32 { network: Any, id: BOB.into() }.into().into()),
Box::new(dest.clone().into()),
Box::new((Here, SEND_AMOUNT).into()),
0,
));
assert_eq!(Balances::total_balance(&ALICE), INITIAL_BALANCE - SEND_AMOUNT);
assert_eq!(
sent_xcm(),
vec![(
RelayLocation::get().into(),
Xcm(vec![
ReceiveTeleportedAsset((Here, SEND_AMOUNT).into()),
ClearOrigin,
buy_limited_execution((Here, SEND_AMOUNT), 2000),
DepositAsset { assets: All.into(), max_assets: 1, beneficiary: dest },
]),
)]
);
let versioned_sent = VersionedXcm::from(sent_xcm().into_iter().next().unwrap().1);
let _check_v0_ok: xcm::v0::Xcm<()> = versioned_sent.try_into().unwrap();
assert_eq!(
last_event(),
Event::XcmPallet(crate::Event::Attempted(Outcome::Complete(weight)))
Expand Down Expand Up @@ -265,11 +280,13 @@ fn reserve_transfer_assets_works() {
Xcm(vec![
ReserveAssetDeposited((Parent, SEND_AMOUNT).into()),
ClearOrigin,
buy_execution((Parent, SEND_AMOUNT)),
buy_limited_execution((Parent, SEND_AMOUNT), 2000),
DepositAsset { assets: All.into(), max_assets: 1, beneficiary: dest },
]),
)]
);
let versioned_sent = VersionedXcm::from(sent_xcm().into_iter().next().unwrap().1);
let _check_v0_ok: xcm::v0::Xcm<()> = versioned_sent.try_into().unwrap();
assert_eq!(
last_event(),
Event::XcmPallet(crate::Event::Attempted(Outcome::Complete(weight)))
Expand Down