Skip to content

Commit

Permalink
test(xcm): add test for conversions between v4 and v5
Browse files Browse the repository at this point in the history
  • Loading branch information
franciscoaguirre committed Nov 29, 2024
1 parent 8917c80 commit b3967c7
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
2 changes: 2 additions & 0 deletions cumulus/parachains/pallets/ping/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ pub mod pallet {
})
.encode()
.into(),
fallback_max_weight: None,
}]),
) {
Ok((hash, cost)) => {
Expand Down Expand Up @@ -214,6 +215,7 @@ pub mod pallet {
})
.encode()
.into(),
fallback_max_weight: None,
}]),
) {
Ok((hash, cost)) =>
Expand Down
46 changes: 44 additions & 2 deletions polkadot/xcm/src/v5/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1353,8 +1353,8 @@ impl<Call> TryFrom<OldInstruction<Call>> for Instruction<Call> {
HrmpChannelAccepted { recipient } => Self::HrmpChannelAccepted { recipient },
HrmpChannelClosing { initiator, sender, recipient } =>
Self::HrmpChannelClosing { initiator, sender, recipient },
Transact { origin_kind, require_weight_at_most: _, call } =>
Self::Transact { origin_kind, call: call.into(), fallback_max_weight: None },
Transact { origin_kind, require_weight_at_most, call } =>
Self::Transact { origin_kind, call: call.into(), fallback_max_weight: Some(require_weight_at_most) },
ReportError(response_info) => Self::ReportError(QueryResponseInfo {
query_id: response_info.query_id,
destination: response_info.destination.try_into().map_err(|_| ())?,
Expand Down Expand Up @@ -1587,6 +1587,48 @@ mod tests {
assert_eq!(new_xcm, xcm);
}

#[test]
fn transact_roundtrip_works() {
// We can convert as long as there's a fallback.
let xcm = Xcm::<()>(vec![
WithdrawAsset((Here, 1u128).into()),
Transact {
origin_kind: OriginKind::SovereignAccount,
call: vec![].into(),
fallback_max_weight: Some(Weight::from_parts(1_000_000, 1_024)),
},
]);
let old_xcm = OldXcm::<()>(vec![
OldInstruction::WithdrawAsset((OldHere, 1u128).into()),
OldInstruction::Transact {
origin_kind: OriginKind::SovereignAccount,
call: vec![].into(),
require_weight_at_most: Weight::from_parts(1_000_000, 1_024),
},
]);
assert_eq!(old_xcm, OldXcm::<()>::try_from(xcm.clone()).unwrap());
let new_xcm: Xcm<()> = old_xcm.try_into().unwrap();
assert_eq!(new_xcm, xcm);

// If we have no fallback the resulting message won't know the weight.
let xcm_without_fallback = Xcm::<()>(vec![
WithdrawAsset((Here, 1u128).into()),
Transact {
origin_kind: OriginKind::SovereignAccount,
call: vec![].into(),
fallback_max_weight: None,
},
]);
let old_xcm = OldXcm::<()>(vec![
OldInstruction::WithdrawAsset((OldHere, 1u128).into()),
OldInstruction::Transact {
origin_kind: OriginKind::SovereignAccount,
call: vec![].into(),
require_weight_at_most: Weight::MAX,
},
]);
}

#[test]
fn decoding_respects_limit() {
let max_xcm = Xcm::<()>(vec![ClearOrigin; MAX_INSTRUCTIONS_TO_DECODE as usize]);
Expand Down
4 changes: 4 additions & 0 deletions polkadot/xcm/xcm-simulator/example/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ fn dmp() {
Xcm(vec![Transact {
origin_kind: OriginKind::SovereignAccount,
call: remark.encode().into(),
fallback_max_weight: None,
}]),
));
});
Expand Down Expand Up @@ -74,6 +75,7 @@ fn ump() {
Xcm(vec![Transact {
origin_kind: OriginKind::SovereignAccount,
call: remark.encode().into(),
fallback_max_weight: None,
}]),
));
});
Expand Down Expand Up @@ -101,6 +103,7 @@ fn xcmp() {
Xcm(vec![Transact {
origin_kind: OriginKind::SovereignAccount,
call: remark.encode().into(),
fallback_max_weight: None,
}]),
));
});
Expand Down Expand Up @@ -388,6 +391,7 @@ fn reserve_asset_class_create_and_reserve_transfer() {
)
.encode()
.into(),
fallback_max_weight: None,
}]);
// Send creation.
assert_ok!(RelayChainPalletXcm::send_xcm(Here, Parachain(1), message));
Expand Down

0 comments on commit b3967c7

Please sign in to comment.