From e03f549357b5c7d829de66d50111014796a4d21c Mon Sep 17 00:00:00 2001 From: Keith Yeung Date: Wed, 11 Aug 2021 15:14:21 -0700 Subject: [PATCH 01/14] Change send_xcm MultiLocation argument to be generic --- runtime/common/src/xcm_sender.rs | 3 ++- runtime/test-runtime/src/xcm_config.rs | 2 +- xcm/pallet-xcm/src/mock.rs | 7 ++++--- xcm/src/v1/multilocation.rs | 12 ++++++------ xcm/src/v1/traits.rs | 16 +++++++++------- xcm/xcm-builder/src/mock.rs | 4 ++-- xcm/xcm-simulator/src/lib.rs | 6 ++++-- 7 files changed, 28 insertions(+), 22 deletions(-) diff --git a/runtime/common/src/xcm_sender.rs b/runtime/common/src/xcm_sender.rs index 602f45bbadf6..9d969d7e7eee 100644 --- a/runtime/common/src/xcm_sender.rs +++ b/runtime/common/src/xcm_sender.rs @@ -27,7 +27,8 @@ pub struct ChildParachainRouter(PhantomData<(T, W)>); impl SendXcm for ChildParachainRouter { - fn send_xcm(dest: MultiLocation, msg: Xcm) -> Result { + fn send_xcm(dest: impl Into, msg: Xcm) -> Result { + let dest = dest.into(); match dest { MultiLocation { parents: 0, interior: Junctions::X1(Junction::Parachain(id)) } => { // Downward message passing. diff --git a/runtime/test-runtime/src/xcm_config.rs b/runtime/test-runtime/src/xcm_config.rs index 7748d55e1875..dcf549f8cde1 100644 --- a/runtime/test-runtime/src/xcm_config.rs +++ b/runtime/test-runtime/src/xcm_config.rs @@ -38,7 +38,7 @@ pub type LocalOriginToLocation = ( pub struct DoNothingRouter; impl SendXcm for DoNothingRouter { - fn send_xcm(_dest: MultiLocation, _msg: Xcm<()>) -> XcmResult { + fn send_xcm(_dest: impl Into, _msg: Xcm<()>) -> XcmResult { Ok(()) } } diff --git a/xcm/pallet-xcm/src/mock.rs b/xcm/pallet-xcm/src/mock.rs index 4dd6537cc1d7..de7a3db5967a 100644 --- a/xcm/pallet-xcm/src/mock.rs +++ b/xcm/pallet-xcm/src/mock.rs @@ -62,15 +62,16 @@ pub fn sent_xcm() -> Vec<(MultiLocation, Xcm)> { /// Sender that never returns error, always sends pub struct TestSendXcm; impl SendXcm for TestSendXcm { - fn send_xcm(dest: MultiLocation, msg: Xcm) -> XcmResult { - SENT_XCM.with(|q| q.borrow_mut().push((dest, msg))); + fn send_xcm(dest: impl Into, msg: Xcm) -> XcmResult { + SENT_XCM.with(|q| q.borrow_mut().push((dest.into(), msg))); Ok(()) } } /// Sender that returns error if `X8` junction and stops routing pub struct TestSendXcmErrX8; impl SendXcm for TestSendXcmErrX8 { - fn send_xcm(dest: MultiLocation, msg: Xcm) -> XcmResult { + fn send_xcm(dest: impl Into, msg: Xcm) -> XcmResult { + let dest = dest.into(); if dest.len() == 8 { Err(XcmError::Undefined) } else { diff --git a/xcm/src/v1/multilocation.rs b/xcm/src/v1/multilocation.rs index f1546b509924..120a1a4b5cc5 100644 --- a/xcm/src/v1/multilocation.rs +++ b/xcm/src/v1/multilocation.rs @@ -325,24 +325,24 @@ impl From for MultiLocation { /// A tuple struct which can be converted into a `MultiLocation` of `parents` value 1 with the inner interior. pub struct ParentThen(Junctions); impl From for MultiLocation { - fn from(x: ParentThen) -> Self { - MultiLocation { parents: 1, interior: x.0 } + fn from(ParentThen(interior): ParentThen) -> Self { + MultiLocation { parents: 1, interior } } } /// A unit struct which can be converted into a `MultiLocation` of the inner `parents` value. pub struct Ancestor(u8); impl From for MultiLocation { - fn from(x: Ancestor) -> Self { - MultiLocation { parents: x.0, interior: Junctions::Here } + fn from(Ancestor(parents): Ancestor) -> Self { + MultiLocation { parents, interior: Junctions::Here } } } /// A unit struct which can be converted into a `MultiLocation` of the inner `parents` value and the inner interior. pub struct AncestorThen(u8, Junctions); impl From for MultiLocation { - fn from(x: AncestorThen) -> Self { - MultiLocation { parents: x.0, interior: x.1 } + fn from(AncestorThen(parents, interior): AncestorThen) -> Self { + MultiLocation { parents, interior } } } diff --git a/xcm/src/v1/traits.rs b/xcm/src/v1/traits.rs index 419c6ec43416..756168bca6f8 100644 --- a/xcm/src/v1/traits.rs +++ b/xcm/src/v1/traits.rs @@ -194,15 +194,16 @@ impl ExecuteXcm for () { /// /// A sender that only passes the message through and does nothing. /// struct Sender1; /// impl SendXcm for Sender1 { -/// fn send_xcm(destination: MultiLocation, message: Xcm<()>) -> Result { -/// return Err(Error::CannotReachDestination(destination, message)) +/// fn send_xcm(destination: impl Into, message: Xcm<()>) -> Result { +/// return Err(Error::CannotReachDestination(destination.into(), message)) /// } /// } /// /// /// A sender that accepts a message that has an X2 junction, otherwise stops the routing. /// struct Sender2; /// impl SendXcm for Sender2 { -/// fn send_xcm(destination: MultiLocation, message: Xcm<()>) -> Result { +/// fn send_xcm(destination: impl Into, message: Xcm<()>) -> Result { +/// let destination = destination.into(); /// if matches!(destination.interior(), Junctions::X2(j1, j2)) /// && destination.parent_count() == 0 /// { @@ -216,7 +217,8 @@ impl ExecuteXcm for () { /// /// A sender that accepts a message from an X1 parent junction, passing through otherwise. /// struct Sender3; /// impl SendXcm for Sender3 { -/// fn send_xcm(destination: MultiLocation, message: Xcm<()>) -> Result { +/// fn send_xcm(destination: impl Into, message: Xcm<()>) -> Result { +/// let destination = destination.into(); /// if matches!(destination.interior(), Junctions::Here) /// && destination.parent_count() == 1 /// { @@ -252,12 +254,12 @@ pub trait SendXcm { /// If it is not a destination which can be reached with this type but possibly could by others, then it *MUST* /// return `CannotReachDestination`. Any other error will cause the tuple implementation to exit early without /// trying other type fields. - fn send_xcm(destination: MultiLocation, message: Xcm<()>) -> Result; + fn send_xcm(destination: impl Into, message: Xcm<()>) -> Result; } #[impl_trait_for_tuples::impl_for_tuples(30)] impl SendXcm for Tuple { - fn send_xcm(destination: MultiLocation, message: Xcm<()>) -> Result { + fn send_xcm(destination: impl Into, message: Xcm<()>) -> Result { for_tuples!( #( // we shadow `destination` and `message` in each expansion for the next one. let (destination, message) = match Tuple::send_xcm(destination, message) { @@ -265,6 +267,6 @@ impl SendXcm for Tuple { o @ _ => return o, }; )* ); - Err(Error::CannotReachDestination(destination, message)) + Err(Error::CannotReachDestination(destination.into(), message)) } } diff --git a/xcm/xcm-builder/src/mock.rs b/xcm/xcm-builder/src/mock.rs index 7c1e93a2b9dd..ccfe1653d599 100644 --- a/xcm/xcm-builder/src/mock.rs +++ b/xcm/xcm-builder/src/mock.rs @@ -107,8 +107,8 @@ pub fn sent_xcm() -> Vec<(MultiLocation, opaque::Xcm)> { } pub struct TestSendXcm; impl SendXcm for TestSendXcm { - fn send_xcm(dest: MultiLocation, msg: opaque::Xcm) -> XcmResult { - SENT_XCM.with(|q| q.borrow_mut().push((dest, msg))); + fn send_xcm(dest: impl Into, msg: opaque::Xcm) -> XcmResult { + SENT_XCM.with(|q| q.borrow_mut().push((dest.into(), msg))); Ok(()) } } diff --git a/xcm/xcm-simulator/src/lib.rs b/xcm/xcm-simulator/src/lib.rs index aed9635cff7f..4584f9516086 100644 --- a/xcm/xcm-simulator/src/lib.rs +++ b/xcm/xcm-simulator/src/lib.rs @@ -192,9 +192,10 @@ macro_rules! decl_test_network { pub struct ParachainXcmRouter($crate::PhantomData); impl> $crate::SendXcm for ParachainXcmRouter { - fn send_xcm(destination: $crate::MultiLocation, message: $crate::Xcm<()>) -> $crate::XcmResult { + fn send_xcm(destination: impl Into<$crate::MultiLocation>, message: $crate::Xcm<()>) -> $crate::XcmResult { use $crate::{UmpSink, XcmpMessageHandlerT}; + let destination = destination.into(); match destination.interior() { $crate::Junctions::Here if destination.parent_count() == 1 => { let encoded = $crate::encode_xcm(message, $crate::MessageKind::Ump); @@ -223,9 +224,10 @@ macro_rules! decl_test_network { /// XCM router for relay chain. pub struct RelayChainXcmRouter; impl $crate::SendXcm for RelayChainXcmRouter { - fn send_xcm(destination: $crate::MultiLocation, message: $crate::Xcm<()>) -> $crate::XcmResult { + fn send_xcm(destination: impl Into<$crate::MultiLocation>, message: $crate::Xcm<()>) -> $crate::XcmResult { use $crate::DmpMessageHandlerT; + let destination = destination.into(); match destination.interior() { $( $crate::X1($crate::Parachain(id)) if *id == $para_id && destination.parent_count() == 0 => { From e098f939b866e389f1b48274f937b3b62856b3e8 Mon Sep 17 00:00:00 2001 From: Keith Yeung Date: Wed, 11 Aug 2021 15:28:20 -0700 Subject: [PATCH 02/14] Change pallet_xcm::send_xcm MultiLocation and Junctions argument to be generic --- xcm/pallet-xcm/src/lib.rs | 8 +++++--- xcm/xcm-simulator/example/src/lib.rs | 6 +++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/xcm/pallet-xcm/src/lib.rs b/xcm/pallet-xcm/src/lib.rs index 9021dc73cbc9..c0ae3acd52b2 100644 --- a/xcm/pallet-xcm/src/lib.rs +++ b/xcm/pallet-xcm/src/lib.rs @@ -122,7 +122,7 @@ pub mod pallet { message: Box>, ) -> DispatchResult { let origin_location = T::SendXcmOrigin::ensure_origin(origin)?; - let interior = + let interior: Junctions = origin_location.clone().try_into().map_err(|_| Error::::InvalidOrigin)?; Self::send_xcm(interior, *dest.clone(), *message.clone()).map_err(|e| match e { XcmError::CannotReachDestination(..) => Error::::Unreachable, @@ -304,10 +304,12 @@ pub mod pallet { /// Relay an XCM `message` from a given `interior` location in this context to a given `dest` /// location. A null `dest` is not handled. pub fn send_xcm( - interior: Junctions, - dest: MultiLocation, + interior: impl Into, + dest: impl Into, message: Xcm<()>, ) -> Result<(), XcmError> { + let interior = interior.into(); + let dest = dest.into(); let message = if let Junctions::Here = interior { message } else { diff --git a/xcm/xcm-simulator/example/src/lib.rs b/xcm/xcm-simulator/example/src/lib.rs index 2649d46991ce..ee5a3831c298 100644 --- a/xcm/xcm-simulator/example/src/lib.rs +++ b/xcm/xcm-simulator/example/src/lib.rs @@ -112,7 +112,7 @@ mod tests { Relay::execute_with(|| { assert_ok!(RelayChainPalletXcm::send_xcm( Here, - Parachain(1).into(), + Parachain(1), Transact { origin_type: OriginKind::SovereignAccount, require_weight_at_most: INITIAL_BALANCE as u64, @@ -139,7 +139,7 @@ mod tests { ParaA::execute_with(|| { assert_ok!(ParachainPalletXcm::send_xcm( Here, - Parent.into(), + Parent, Transact { origin_type: OriginKind::SovereignAccount, require_weight_at_most: INITIAL_BALANCE as u64, @@ -166,7 +166,7 @@ mod tests { ParaA::execute_with(|| { assert_ok!(ParachainPalletXcm::send_xcm( Here, - MultiLocation::new(1, X1(Parachain(2))), + (1, Parachain(2)), Transact { origin_type: OriginKind::SovereignAccount, require_weight_at_most: INITIAL_BALANCE as u64, From 2f0dca9000315f0ff7ea12d730a861943e671a95 Mon Sep 17 00:00:00 2001 From: Keith Yeung Date: Wed, 11 Aug 2021 15:48:52 -0700 Subject: [PATCH 03/14] Change convert_origin MultiLocation argument to be generic --- xcm/pallet-xcm/src/lib.rs | 3 ++- xcm/xcm-builder/src/mock.rs | 4 +-- xcm/xcm-builder/src/origin_conversion.rs | 33 ++++++++++++----------- xcm/xcm-executor/src/traits/conversion.rs | 19 ++++++------- 4 files changed, 32 insertions(+), 27 deletions(-) diff --git a/xcm/pallet-xcm/src/lib.rs b/xcm/pallet-xcm/src/lib.rs index c0ae3acd52b2..a2b59d50bbd3 100644 --- a/xcm/pallet-xcm/src/lib.rs +++ b/xcm/pallet-xcm/src/lib.rs @@ -397,7 +397,8 @@ where /// this crate's `Origin::Xcm` value. pub struct XcmPassthrough(PhantomData); impl> ConvertOrigin for XcmPassthrough { - fn convert_origin(origin: MultiLocation, kind: OriginKind) -> Result { + fn convert_origin(origin: impl Into, kind: OriginKind) -> Result { + let origin = origin.into(); match kind { OriginKind::Xcm => Ok(crate::Origin::Xcm(origin).into()), _ => Err(origin), diff --git a/xcm/xcm-builder/src/mock.rs b/xcm/xcm-builder/src/mock.rs index ccfe1653d599..3310bf6f7099 100644 --- a/xcm/xcm-builder/src/mock.rs +++ b/xcm/xcm-builder/src/mock.rs @@ -162,11 +162,11 @@ pub fn to_account(l: MultiLocation) -> Result { pub struct TestOriginConverter; impl ConvertOrigin for TestOriginConverter { fn convert_origin( - origin: MultiLocation, + origin: impl Into, kind: OriginKind, ) -> Result { use OriginKind::*; - match (kind, origin) { + match (kind, origin.into()) { (Superuser, _) => Ok(TestOrigin::Root), (SovereignAccount, l) => Ok(TestOrigin::Signed(to_account(l)?)), (Native, MultiLocation { parents: 0, interior: X1(Parachain(id)) }) => diff --git a/xcm/xcm-builder/src/origin_conversion.rs b/xcm/xcm-builder/src/origin_conversion.rs index b1800de2c6c8..52486ed8c3e3 100644 --- a/xcm/xcm-builder/src/origin_conversion.rs +++ b/xcm/xcm-builder/src/origin_conversion.rs @@ -32,7 +32,8 @@ impl, Origin: Origi where Origin::AccountId: Clone, { - fn convert_origin(origin: MultiLocation, kind: OriginKind) -> Result { + fn convert_origin(origin: impl Into, kind: OriginKind) -> Result { + let origin = origin.into(); if let OriginKind::SovereignAccount = kind { let location = LocationConverter::convert(origin)?; Ok(Origin::signed(location).into()) @@ -44,7 +45,8 @@ where pub struct ParentAsSuperuser(PhantomData); impl ConvertOrigin for ParentAsSuperuser { - fn convert_origin(origin: MultiLocation, kind: OriginKind) -> Result { + fn convert_origin(origin: impl Into, kind: OriginKind) -> Result { + let origin = origin.into(); if kind == OriginKind::Superuser && origin.contains_parents_only(1) { Ok(Origin::root()) } else { @@ -57,8 +59,8 @@ pub struct ChildSystemParachainAsSuperuser(PhantomData<(ParaId, impl, Origin: OriginTrait> ConvertOrigin for ChildSystemParachainAsSuperuser { - fn convert_origin(origin: MultiLocation, kind: OriginKind) -> Result { - match (kind, origin) { + fn convert_origin(origin: impl Into, kind: OriginKind) -> Result { + match (kind, origin.into()) { ( OriginKind::Superuser, MultiLocation { parents: 0, interior: X1(Junction::Parachain(id)) }, @@ -72,8 +74,8 @@ pub struct SiblingSystemParachainAsSuperuser(PhantomData<(ParaId impl, Origin: OriginTrait> ConvertOrigin for SiblingSystemParachainAsSuperuser { - fn convert_origin(origin: MultiLocation, kind: OriginKind) -> Result { - match (kind, origin) { + fn convert_origin(origin: impl Into, kind: OriginKind) -> Result { + match (kind, origin.into()) { ( OriginKind::Superuser, MultiLocation { parents: 1, interior: X1(Junction::Parachain(id)) }, @@ -87,8 +89,8 @@ pub struct ChildParachainAsNative(PhantomData<(Parachai impl, Origin: From> ConvertOrigin for ChildParachainAsNative { - fn convert_origin(origin: MultiLocation, kind: OriginKind) -> Result { - match (kind, origin) { + fn convert_origin(origin: impl Into, kind: OriginKind) -> Result { + match (kind, origin.into()) { ( OriginKind::Native, MultiLocation { parents: 0, interior: X1(Junction::Parachain(id)) }, @@ -104,8 +106,8 @@ pub struct SiblingParachainAsNative( impl, Origin: From> ConvertOrigin for SiblingParachainAsNative { - fn convert_origin(origin: MultiLocation, kind: OriginKind) -> Result { - match (kind, origin) { + fn convert_origin(origin: impl Into, kind: OriginKind) -> Result { + match (kind, origin.into()) { ( OriginKind::Native, MultiLocation { parents: 1, interior: X1(Junction::Parachain(id)) }, @@ -120,7 +122,8 @@ pub struct RelayChainAsNative(PhantomData<(RelayOrigin, Ori impl, Origin> ConvertOrigin for RelayChainAsNative { - fn convert_origin(origin: MultiLocation, kind: OriginKind) -> Result { + fn convert_origin(origin: impl Into, kind: OriginKind) -> Result { + let origin = origin.into(); if kind == OriginKind::Native && origin.contains_parents_only(1) { Ok(RelayOrigin::get()) } else { @@ -135,8 +138,8 @@ impl, Origin: OriginTrait> ConvertOrigin where Origin::AccountId: From<[u8; 32]>, { - fn convert_origin(origin: MultiLocation, kind: OriginKind) -> Result { - match (kind, origin) { + fn convert_origin(origin: impl Into, kind: OriginKind) -> Result { + match (kind, origin.into()) { ( OriginKind::Native, MultiLocation { parents: 0, interior: X1(Junction::AccountId32 { id, network }) }, @@ -153,8 +156,8 @@ impl, Origin: OriginTrait> ConvertOrigin where Origin::AccountId: From<[u8; 20]>, { - fn convert_origin(origin: MultiLocation, kind: OriginKind) -> Result { - match (kind, origin) { + fn convert_origin(origin: impl Into, kind: OriginKind) -> Result { + match (kind, origin.into()) { ( OriginKind::Native, MultiLocation { parents: 0, interior: X1(Junction::AccountKey20 { key, network }) }, diff --git a/xcm/xcm-executor/src/traits/conversion.rs b/xcm/xcm-executor/src/traits/conversion.rs index edfa1a96029f..f82e9574bfbc 100644 --- a/xcm/xcm-executor/src/traits/conversion.rs +++ b/xcm/xcm-executor/src/traits/conversion.rs @@ -144,9 +144,9 @@ impl Convert, T> for Decoded { /// // A convertor that will bump the para id and pass it to the next one. /// struct BumpParaId; /// impl ConvertOrigin for BumpParaId { -/// fn convert_origin(origin: MultiLocation, _: OriginKind) -> Result { -/// match origin.interior() { -/// Junctions::X1(Junction::Parachain(id)) if origin.parent_count() == 0 => { +/// fn convert_origin(origin: impl Into, _: OriginKind) -> Result { +/// match origin.into() { +/// MultiLocation { parents: 0, interior: Junctions::X1(Junction::Parachain(id)) } => { /// Err(Junctions::X1(Junction::Parachain(id + 1)).into()) /// } /// _ => unreachable!() @@ -156,12 +156,12 @@ impl Convert, T> for Decoded { /// /// struct AcceptPara7; /// impl ConvertOrigin for AcceptPara7 { -/// fn convert_origin(origin: MultiLocation, _: OriginKind) -> Result { -/// match origin.interior() { -/// Junctions::X1(Junction::Parachain(id)) if id == &7 && origin.parent_count() == 0 => { +/// fn convert_origin(origin: impl Into, _: OriginKind) -> Result { +/// match origin.into() { +/// MultiLocation { parents: 0, interior: Junctions::X1(Junction::Parachain(id)) } if id == 7 => { /// Ok(7) /// } -/// _ => Err(origin) +/// o => Err(o) /// } /// } /// } @@ -175,18 +175,19 @@ impl Convert, T> for Decoded { /// ``` pub trait ConvertOrigin { /// Attempt to convert `origin` to the generic `Origin` whilst consuming it. - fn convert_origin(origin: MultiLocation, kind: OriginKind) -> Result; + fn convert_origin(origin: impl Into, kind: OriginKind) -> Result; } #[impl_trait_for_tuples::impl_for_tuples(30)] impl ConvertOrigin for Tuple { - fn convert_origin(origin: MultiLocation, kind: OriginKind) -> Result { + fn convert_origin(origin: impl Into, kind: OriginKind) -> Result { for_tuples!( #( let origin = match Tuple::convert_origin(origin, kind) { Err(o) => o, r => return r }; )* ); + let origin = origin.into(); log::trace!( target: "xcm::convert_origin", "could not convert: origin: {:?}, kind: {:?}", From e6c920321938400d3a97b09199220eb5e164c5e6 Mon Sep 17 00:00:00 2001 From: Keith Yeung Date: Wed, 11 Aug 2021 15:54:04 -0700 Subject: [PATCH 04/14] Change OnResponse MultiLocation arguments to be generic --- xcm/xcm-builder/src/mock.rs | 2 +- xcm/xcm-executor/src/traits/on_response.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/xcm/xcm-builder/src/mock.rs b/xcm/xcm-builder/src/mock.rs index 3310bf6f7099..4d76539e233e 100644 --- a/xcm/xcm-builder/src/mock.rs +++ b/xcm/xcm-builder/src/mock.rs @@ -222,7 +222,7 @@ impl OnResponse for TestResponseHandler { }) } fn on_response( - _origin: MultiLocation, + _origin: impl Into, query_id: u64, response: xcm::latest::Response, ) -> Weight { diff --git a/xcm/xcm-executor/src/traits/on_response.rs b/xcm/xcm-executor/src/traits/on_response.rs index 8af238e7cef2..1eb3fd4f09c0 100644 --- a/xcm/xcm-executor/src/traits/on_response.rs +++ b/xcm/xcm-executor/src/traits/on_response.rs @@ -22,13 +22,13 @@ pub trait OnResponse { /// Returns `true` if we are expecting a response from `origin` for query `query_id`. fn expecting_response(origin: &MultiLocation, query_id: u64) -> bool; /// Handler for receiving a `response` from `origin` relating to `query_id`. - fn on_response(origin: MultiLocation, query_id: u64, response: Response) -> Weight; + fn on_response(origin: impl Into, query_id: u64, response: Response) -> Weight; } impl OnResponse for () { fn expecting_response(_origin: &MultiLocation, _query_id: u64) -> bool { false } - fn on_response(_origin: MultiLocation, _query_id: u64, _response: Response) -> Weight { + fn on_response(_origin: impl Into, _query_id: u64, _response: Response) -> Weight { 0 } } From 85dcf645575d16cb7b186e83d41dd2e0f2b66712 Mon Sep 17 00:00:00 2001 From: Keith Yeung Date: Wed, 11 Aug 2021 16:01:50 -0700 Subject: [PATCH 05/14] Change UniversalWeigher MultiLocation argumente to be generic --- xcm/xcm-executor/src/traits/weight.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xcm/xcm-executor/src/traits/weight.rs b/xcm/xcm-executor/src/traits/weight.rs index 8c9e6ec6366d..5650f5f3f802 100644 --- a/xcm/xcm-executor/src/traits/weight.rs +++ b/xcm/xcm-executor/src/traits/weight.rs @@ -54,7 +54,7 @@ pub trait WeightBounds { /// message. pub trait UniversalWeigher { /// Get the upper limit of weight required for `dest` to execute `message`. - fn weigh(dest: MultiLocation, message: Xcm<()>) -> Result; + fn weigh(dest: impl Into, message: Xcm<()>) -> Result; } /// Charge for weight in order to execute XCM. From 74635177dee3ec8e1635187367b67803eb97a021 Mon Sep 17 00:00:00 2001 From: Keith Yeung Date: Wed, 11 Aug 2021 16:07:57 -0700 Subject: [PATCH 06/14] Change ExecuteXcm MultiLocation argument to be generic --- xcm/src/v1/traits.rs | 7 ++++--- xcm/xcm-executor/src/lib.rs | 3 ++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/xcm/src/v1/traits.rs b/xcm/src/v1/traits.rs index 756168bca6f8..a87cc868f3a9 100644 --- a/xcm/src/v1/traits.rs +++ b/xcm/src/v1/traits.rs @@ -145,7 +145,8 @@ pub trait ExecuteXcm { /// Execute some XCM `message` from `origin` using no more than `weight_limit` weight. The weight limit is /// a basic hard-limit and the implementation may place further restrictions or requirements on weight and /// other aspects. - fn execute_xcm(origin: MultiLocation, message: Xcm, weight_limit: Weight) -> Outcome { + fn execute_xcm(origin: impl Into, message: Xcm, weight_limit: Weight) -> Outcome { + let origin = origin.into(); log::debug!( target: "xcm::execute_xcm", "origin: {:?}, message: {:?}, weight_limit: {:?}", @@ -161,7 +162,7 @@ pub trait ExecuteXcm { /// Some amount of `weight_credit` may be provided which, depending on the implementation, may allow /// execution without associated payment. fn execute_xcm_in_credit( - origin: MultiLocation, + origin: impl Into, message: Xcm, weight_limit: Weight, weight_credit: Weight, @@ -170,7 +171,7 @@ pub trait ExecuteXcm { impl ExecuteXcm for () { fn execute_xcm_in_credit( - _origin: MultiLocation, + _origin: impl Into, _message: Xcm, _weight_limit: Weight, _weight_credit: Weight, diff --git a/xcm/xcm-executor/src/lib.rs b/xcm/xcm-executor/src/lib.rs index 3306c303e261..749805c45b89 100644 --- a/xcm/xcm-executor/src/lib.rs +++ b/xcm/xcm-executor/src/lib.rs @@ -46,11 +46,12 @@ pub const MAX_RECURSION_LIMIT: u32 = 8; impl ExecuteXcm for XcmExecutor { fn execute_xcm_in_credit( - origin: MultiLocation, + origin: impl Into, message: Xcm, weight_limit: Weight, mut weight_credit: Weight, ) -> Outcome { + let origin = origin.into(); log::trace!( target: "xcm::execute_xcm_in_credit", "origin: {:?}, message: {:?}, weight_limit: {:?}, weight_credit: {:?}", From 15f8695b5fd932780d80f75ccae9b09e624847ad Mon Sep 17 00:00:00 2001 From: Keith Yeung Date: Wed, 11 Aug 2021 16:35:39 -0700 Subject: [PATCH 07/14] Remove usages of into for the MultiLocation argument in execute_xcm --- runtime/parachains/src/ump.rs | 5 ++--- xcm/xcm-builder/src/tests.rs | 23 +++++++++------------- xcm/xcm-simulator/example/src/parachain.rs | 4 ++-- 3 files changed, 13 insertions(+), 19 deletions(-) diff --git a/runtime/parachains/src/ump.rs b/runtime/parachains/src/ump.rs index 40f1a5a3acda..81a7e74d2ea4 100644 --- a/runtime/parachains/src/ump.rs +++ b/runtime/parachains/src/ump.rs @@ -106,9 +106,8 @@ impl, C: Config> UmpSink for XcmSi Ok(0) }, Ok(Ok(xcm_message)) => { - let xcm_junction: Junction = Junction::Parachain(origin.into()); - let xcm_location: MultiLocation = xcm_junction.into(); - let outcome = XcmExecutor::execute_xcm(xcm_location, xcm_message, max_weight); + let xcm_junction = Junction::Parachain(origin.into()); + let outcome = XcmExecutor::execute_xcm(xcm_junction, xcm_message, max_weight); match outcome { Outcome::Error(XcmError::WeightLimitReached(required)) => Err((id, required)), outcome => { diff --git a/xcm/xcm-builder/src/tests.rs b/xcm/xcm-builder/src/tests.rs index 5ed3d3c49600..20ecd06ea7bf 100644 --- a/xcm/xcm-builder/src/tests.rs +++ b/xcm/xcm-builder/src/tests.rs @@ -196,7 +196,6 @@ fn paying_reserve_deposit_should_work() { add_reserve(Parent.into(), (Parent, WildFungible).into()); WeightPrice::set((Parent.into(), 1_000_000_000_000)); - let origin = Parent.into(); let fees = (Parent, 30).into(); let message = Xcm::::ReserveAssetDeposited { assets: (Parent, 100).into(), @@ -217,7 +216,7 @@ fn paying_reserve_deposit_should_work() { ], }; let weight_limit = 50; - let r = XcmExecutor::::execute_xcm(origin, message, weight_limit); + let r = XcmExecutor::::execute_xcm(Parent, message, weight_limit); assert_eq!(r, Outcome::Complete(30)); assert_eq!(assets(3000), vec![(Parent, 70).into()]); } @@ -230,7 +229,7 @@ fn transfer_should_work() { add_asset(1001, (Here, 1000).into()); // They want to transfer 100 of them to their sibling parachain #2 let r = XcmExecutor::::execute_xcm( - Parachain(1).into(), + Parachain(1), Xcm::TransferAsset { assets: (Here, 100).into(), beneficiary: X1(AccountIndex64 { index: 3, network: Any }).into(), @@ -254,7 +253,7 @@ fn reserve_transfer_should_work() { // They want to transfer 100 of our native asset from sovereign account of parachain #1 into #2 // and let them know to hand it to account #3. let r = XcmExecutor::::execute_xcm( - Parachain(1).into(), + Parachain(1), Xcm::TransferReserveAsset { assets: (Here, 100).into(), dest: Parachain(2).into(), @@ -289,14 +288,13 @@ fn reserve_transfer_should_work() { fn transacting_should_work() { AllowUnpaidFrom::set(vec![Parent.into()]); - let origin = Parent.into(); let message = Xcm::::Transact { origin_type: OriginKind::Native, require_weight_at_most: 50, call: TestCall::Any(50, None).encode().into(), }; let weight_limit = 60; - let r = XcmExecutor::::execute_xcm(origin, message, weight_limit); + let r = XcmExecutor::::execute_xcm(Parent, message, weight_limit); assert_eq!(r, Outcome::Complete(60)); } @@ -304,14 +302,13 @@ fn transacting_should_work() { fn transacting_should_respect_max_weight_requirement() { AllowUnpaidFrom::set(vec![Parent.into()]); - let origin = Parent.into(); let message = Xcm::::Transact { origin_type: OriginKind::Native, require_weight_at_most: 40, call: TestCall::Any(50, None).encode().into(), }; let weight_limit = 60; - let r = XcmExecutor::::execute_xcm(origin, message, weight_limit); + let r = XcmExecutor::::execute_xcm(Parent, message, weight_limit); assert_eq!(r, Outcome::Incomplete(60, XcmError::TooMuchWeightRequired)); } @@ -319,14 +316,13 @@ fn transacting_should_respect_max_weight_requirement() { fn transacting_should_refund_weight() { AllowUnpaidFrom::set(vec![Parent.into()]); - let origin = Parent.into(); let message = Xcm::::Transact { origin_type: OriginKind::Native, require_weight_at_most: 50, call: TestCall::Any(50, Some(30)).encode().into(), }; let weight_limit = 60; - let r = XcmExecutor::::execute_xcm(origin, message, weight_limit); + let r = XcmExecutor::::execute_xcm(Parent, message, weight_limit); assert_eq!(r, Outcome::Complete(40)); } @@ -371,20 +367,19 @@ fn paid_transacting_should_refund_payment_for_unused_weight() { #[test] fn prepaid_result_of_query_should_get_free_execution() { let query_id = 33; - let origin: MultiLocation = Parent.into(); // We put this in manually here, but normally this would be done at the point of crafting the message. - expect_response(query_id, origin.clone()); + expect_response(query_id, Parent.into()); let the_response = Response::Assets((Parent, 100).into()); let message = Xcm::::QueryResponse { query_id, response: the_response.clone() }; let weight_limit = 10; // First time the response gets through since we're expecting it... - let r = XcmExecutor::::execute_xcm(origin.clone(), message.clone(), weight_limit); + let r = XcmExecutor::::execute_xcm(Parent, message.clone(), weight_limit); assert_eq!(r, Outcome::Complete(10)); assert_eq!(response(query_id).unwrap(), the_response); // Second time it doesn't, since we're not. - let r = XcmExecutor::::execute_xcm(origin.clone(), message.clone(), weight_limit); + let r = XcmExecutor::::execute_xcm(Parent, message.clone(), weight_limit); assert_eq!(r, Outcome::Incomplete(10, XcmError::Barrier)); } diff --git a/xcm/xcm-simulator/example/src/parachain.rs b/xcm/xcm-simulator/example/src/parachain.rs index 79c2f6e2947d..b442b2ea94a9 100644 --- a/xcm/xcm-simulator/example/src/parachain.rs +++ b/xcm/xcm-simulator/example/src/parachain.rs @@ -210,7 +210,7 @@ pub mod mock_msg_queue { let hash = Encode::using_encoded(&xcm, T::Hashing::hash); let (result, event) = match Xcm::::try_from(xcm) { Ok(xcm) => { - let location = MultiLocation::new(1, X1(Parachain(sender.into()))); + let location = (1, Parachain(sender.into())); match T::XcmExecutor::execute_xcm(location, xcm, max_weight) { Outcome::Error(e) => (Err(e.clone()), Event::Fail(Some(hash), e)), Outcome::Complete(w) => (Ok(w), Event::Success(Some(hash))), @@ -266,7 +266,7 @@ pub mod mock_msg_queue { Self::deposit_event(Event::UnsupportedVersion(id)); }, Ok(Ok(x)) => { - let outcome = T::XcmExecutor::execute_xcm(Parent.into(), x, limit); + let outcome = T::XcmExecutor::execute_xcm(Parent, x, limit); Self::deposit_event(Event::ExecutedDownward(id, outcome)); }, } From eba99cadb0940f0c2a1a7051e465cbf7191cb202 Mon Sep 17 00:00:00 2001 From: Keith Yeung Date: Wed, 11 Aug 2021 16:42:20 -0700 Subject: [PATCH 08/14] Make use of generic MultiLocation arguments in rustdocs --- xcm/src/v1/traits.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/xcm/src/v1/traits.rs b/xcm/src/v1/traits.rs index a87cc868f3a9..d17598eb7f71 100644 --- a/xcm/src/v1/traits.rs +++ b/xcm/src/v1/traits.rs @@ -234,17 +234,16 @@ impl ExecuteXcm for () { /// # fn main() { /// let call: Vec = ().encode(); /// let message = Xcm::Transact { origin_type: OriginKind::Superuser, require_weight_at_most: 0, call: call.into() }; -/// let destination: MultiLocation = Parent.into(); /// /// assert!( /// // Sender2 will block this. -/// <(Sender1, Sender2, Sender3) as SendXcm>::send_xcm(destination.clone(), message.clone()) +/// <(Sender1, Sender2, Sender3) as SendXcm>::send_xcm(Parent, message.clone()) /// .is_err() /// ); /// /// assert!( /// // Sender3 will catch this. -/// <(Sender1, Sender3) as SendXcm>::send_xcm(destination.clone(), message.clone()) +/// <(Sender1, Sender3) as SendXcm>::send_xcm(Parent, message.clone()) /// .is_ok() /// ); /// # } From 3db5c7d072bbdf9c6cbfbb146976e0c66a2ce154 Mon Sep 17 00:00:00 2001 From: Keith Yeung Date: Wed, 11 Aug 2021 17:02:19 -0700 Subject: [PATCH 09/14] Cargo fmt --- xcm/pallet-xcm/src/lib.rs | 5 ++- xcm/src/v1/traits.rs | 6 ++- xcm/xcm-builder/src/origin_conversion.rs | 45 +++++++++++++++++----- xcm/xcm-executor/src/traits/conversion.rs | 10 ++++- xcm/xcm-executor/src/traits/on_response.rs | 6 ++- 5 files changed, 58 insertions(+), 14 deletions(-) diff --git a/xcm/pallet-xcm/src/lib.rs b/xcm/pallet-xcm/src/lib.rs index a2b59d50bbd3..2cb428b0d6ea 100644 --- a/xcm/pallet-xcm/src/lib.rs +++ b/xcm/pallet-xcm/src/lib.rs @@ -397,7 +397,10 @@ where /// this crate's `Origin::Xcm` value. pub struct XcmPassthrough(PhantomData); impl> ConvertOrigin for XcmPassthrough { - fn convert_origin(origin: impl Into, kind: OriginKind) -> Result { + fn convert_origin( + origin: impl Into, + kind: OriginKind, + ) -> Result { let origin = origin.into(); match kind { OriginKind::Xcm => Ok(crate::Origin::Xcm(origin).into()), diff --git a/xcm/src/v1/traits.rs b/xcm/src/v1/traits.rs index d17598eb7f71..e055a53e3349 100644 --- a/xcm/src/v1/traits.rs +++ b/xcm/src/v1/traits.rs @@ -145,7 +145,11 @@ pub trait ExecuteXcm { /// Execute some XCM `message` from `origin` using no more than `weight_limit` weight. The weight limit is /// a basic hard-limit and the implementation may place further restrictions or requirements on weight and /// other aspects. - fn execute_xcm(origin: impl Into, message: Xcm, weight_limit: Weight) -> Outcome { + fn execute_xcm( + origin: impl Into, + message: Xcm, + weight_limit: Weight, + ) -> Outcome { let origin = origin.into(); log::debug!( target: "xcm::execute_xcm", diff --git a/xcm/xcm-builder/src/origin_conversion.rs b/xcm/xcm-builder/src/origin_conversion.rs index 52486ed8c3e3..9f33347e378d 100644 --- a/xcm/xcm-builder/src/origin_conversion.rs +++ b/xcm/xcm-builder/src/origin_conversion.rs @@ -32,7 +32,10 @@ impl, Origin: Origi where Origin::AccountId: Clone, { - fn convert_origin(origin: impl Into, kind: OriginKind) -> Result { + fn convert_origin( + origin: impl Into, + kind: OriginKind, + ) -> Result { let origin = origin.into(); if let OriginKind::SovereignAccount = kind { let location = LocationConverter::convert(origin)?; @@ -45,7 +48,10 @@ where pub struct ParentAsSuperuser(PhantomData); impl ConvertOrigin for ParentAsSuperuser { - fn convert_origin(origin: impl Into, kind: OriginKind) -> Result { + fn convert_origin( + origin: impl Into, + kind: OriginKind, + ) -> Result { let origin = origin.into(); if kind == OriginKind::Superuser && origin.contains_parents_only(1) { Ok(Origin::root()) @@ -59,7 +65,10 @@ pub struct ChildSystemParachainAsSuperuser(PhantomData<(ParaId, impl, Origin: OriginTrait> ConvertOrigin for ChildSystemParachainAsSuperuser { - fn convert_origin(origin: impl Into, kind: OriginKind) -> Result { + fn convert_origin( + origin: impl Into, + kind: OriginKind, + ) -> Result { match (kind, origin.into()) { ( OriginKind::Superuser, @@ -74,7 +83,10 @@ pub struct SiblingSystemParachainAsSuperuser(PhantomData<(ParaId impl, Origin: OriginTrait> ConvertOrigin for SiblingSystemParachainAsSuperuser { - fn convert_origin(origin: impl Into, kind: OriginKind) -> Result { + fn convert_origin( + origin: impl Into, + kind: OriginKind, + ) -> Result { match (kind, origin.into()) { ( OriginKind::Superuser, @@ -89,7 +101,10 @@ pub struct ChildParachainAsNative(PhantomData<(Parachai impl, Origin: From> ConvertOrigin for ChildParachainAsNative { - fn convert_origin(origin: impl Into, kind: OriginKind) -> Result { + fn convert_origin( + origin: impl Into, + kind: OriginKind, + ) -> Result { match (kind, origin.into()) { ( OriginKind::Native, @@ -106,7 +121,10 @@ pub struct SiblingParachainAsNative( impl, Origin: From> ConvertOrigin for SiblingParachainAsNative { - fn convert_origin(origin: impl Into, kind: OriginKind) -> Result { + fn convert_origin( + origin: impl Into, + kind: OriginKind, + ) -> Result { match (kind, origin.into()) { ( OriginKind::Native, @@ -122,7 +140,10 @@ pub struct RelayChainAsNative(PhantomData<(RelayOrigin, Ori impl, Origin> ConvertOrigin for RelayChainAsNative { - fn convert_origin(origin: impl Into, kind: OriginKind) -> Result { + fn convert_origin( + origin: impl Into, + kind: OriginKind, + ) -> Result { let origin = origin.into(); if kind == OriginKind::Native && origin.contains_parents_only(1) { Ok(RelayOrigin::get()) @@ -138,7 +159,10 @@ impl, Origin: OriginTrait> ConvertOrigin where Origin::AccountId: From<[u8; 32]>, { - fn convert_origin(origin: impl Into, kind: OriginKind) -> Result { + fn convert_origin( + origin: impl Into, + kind: OriginKind, + ) -> Result { match (kind, origin.into()) { ( OriginKind::Native, @@ -156,7 +180,10 @@ impl, Origin: OriginTrait> ConvertOrigin where Origin::AccountId: From<[u8; 20]>, { - fn convert_origin(origin: impl Into, kind: OriginKind) -> Result { + fn convert_origin( + origin: impl Into, + kind: OriginKind, + ) -> Result { match (kind, origin.into()) { ( OriginKind::Native, diff --git a/xcm/xcm-executor/src/traits/conversion.rs b/xcm/xcm-executor/src/traits/conversion.rs index f82e9574bfbc..5a15df71698f 100644 --- a/xcm/xcm-executor/src/traits/conversion.rs +++ b/xcm/xcm-executor/src/traits/conversion.rs @@ -175,12 +175,18 @@ impl Convert, T> for Decoded { /// ``` pub trait ConvertOrigin { /// Attempt to convert `origin` to the generic `Origin` whilst consuming it. - fn convert_origin(origin: impl Into, kind: OriginKind) -> Result; + fn convert_origin( + origin: impl Into, + kind: OriginKind, + ) -> Result; } #[impl_trait_for_tuples::impl_for_tuples(30)] impl ConvertOrigin for Tuple { - fn convert_origin(origin: impl Into, kind: OriginKind) -> Result { + fn convert_origin( + origin: impl Into, + kind: OriginKind, + ) -> Result { for_tuples!( #( let origin = match Tuple::convert_origin(origin, kind) { Err(o) => o, diff --git a/xcm/xcm-executor/src/traits/on_response.rs b/xcm/xcm-executor/src/traits/on_response.rs index 1eb3fd4f09c0..8d0d23188b2d 100644 --- a/xcm/xcm-executor/src/traits/on_response.rs +++ b/xcm/xcm-executor/src/traits/on_response.rs @@ -28,7 +28,11 @@ impl OnResponse for () { fn expecting_response(_origin: &MultiLocation, _query_id: u64) -> bool { false } - fn on_response(_origin: impl Into, _query_id: u64, _response: Response) -> Weight { + fn on_response( + _origin: impl Into, + _query_id: u64, + _response: Response, + ) -> Weight { 0 } } From bdd1febc5ad1fe2c24f8ba97fecd5d379779e8e3 Mon Sep 17 00:00:00 2001 From: Keith Yeung Date: Wed, 11 Aug 2021 17:03:33 -0700 Subject: [PATCH 10/14] Remove unused import in tests --- runtime/parachains/src/ump.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/parachains/src/ump.rs b/runtime/parachains/src/ump.rs index 81a7e74d2ea4..60e8d8e9b54f 100644 --- a/runtime/parachains/src/ump.rs +++ b/runtime/parachains/src/ump.rs @@ -86,7 +86,7 @@ impl, C: Config> UmpSink for XcmSi ) -> Result { use parity_scale_codec::DecodeLimit; use xcm::{ - latest::{Error as XcmError, Junction, MultiLocation, Xcm}, + latest::{Error as XcmError, Junction, Xcm}, VersionedXcm, }; From 9474846be6b79273e590b10e36b56f3645e15f5a Mon Sep 17 00:00:00 2001 From: Keith Yeung Date: Thu, 26 Aug 2021 23:31:41 -0700 Subject: [PATCH 11/14] Resolve conflicts --- xcm/xcm-simulator/src/lib.rs | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/xcm/xcm-simulator/src/lib.rs b/xcm/xcm-simulator/src/lib.rs index a6b3e6d19999..5e563e153dba 100644 --- a/xcm/xcm-simulator/src/lib.rs +++ b/xcm/xcm-simulator/src/lib.rs @@ -296,11 +296,7 @@ macro_rules! decl_test_network { pub struct ParachainXcmRouter($crate::PhantomData); impl> $crate::SendXcm for ParachainXcmRouter { -<<<<<<< HEAD - fn send_xcm(destination: impl Into<$crate::MultiLocation>, message: $crate::Xcm<()>) -> $crate::XcmResult { -======= - fn send_xcm(destination: $crate::MultiLocation, message: $crate::Xcm<()>) -> $crate::SendResult { ->>>>>>> master + fn send_xcm(destination: impl Into<$crate::MultiLocation>, message: $crate::Xcm<()>) -> $crate::SendResult { use $crate::{UmpSink, XcmpMessageHandlerT}; let destination = destination.into(); @@ -325,11 +321,7 @@ macro_rules! decl_test_network { /// XCM router for relay chain. pub struct RelayChainXcmRouter; impl $crate::SendXcm for RelayChainXcmRouter { -<<<<<<< HEAD - fn send_xcm(destination: impl Into<$crate::MultiLocation>, message: $crate::Xcm<()>) -> $crate::XcmResult { -======= - fn send_xcm(destination: $crate::MultiLocation, message: $crate::Xcm<()>) -> $crate::SendResult { ->>>>>>> master + fn send_xcm(destination: impl Into<$crate::MultiLocation>, message: $crate::Xcm<()>) -> $crate::SendResult { use $crate::DmpMessageHandlerT; let destination = destination.into(); From 44cc69fedf3489d092cf2df3920927b4bae954f2 Mon Sep 17 00:00:00 2001 From: Keith Yeung Date: Thu, 26 Aug 2021 23:32:42 -0700 Subject: [PATCH 12/14] cargo fmt --- xcm/src/v2/traits.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/xcm/src/v2/traits.rs b/xcm/src/v2/traits.rs index 67571ff9217d..740f388bbdef 100644 --- a/xcm/src/v2/traits.rs +++ b/xcm/src/v2/traits.rs @@ -159,7 +159,11 @@ pub trait ExecuteXcm { /// Execute some XCM `message` from `origin` using no more than `weight_limit` weight. The weight limit is /// a basic hard-limit and the implementation may place further restrictions or requirements on weight and /// other aspects. - fn execute_xcm(origin: impl Into, message: Xcm, weight_limit: Weight) -> Outcome { + fn execute_xcm( + origin: impl Into, + message: Xcm, + weight_limit: Weight, + ) -> Outcome { let origin = origin.into(); log::debug!( target: "xcm::execute_xcm", From f5a23dc10f71a085804a5739130ba5a8e7b0e3c5 Mon Sep 17 00:00:00 2001 From: Keith Yeung Date: Mon, 30 Aug 2021 13:35:59 -0700 Subject: [PATCH 13/14] Appease spellcheck --- xcm/xcm-executor/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xcm/xcm-executor/src/lib.rs b/xcm/xcm-executor/src/lib.rs index 922891aa5b46..3e5ed168ab9e 100644 --- a/xcm/xcm-executor/src/lib.rs +++ b/xcm/xcm-executor/src/lib.rs @@ -45,7 +45,7 @@ pub struct XcmExecutor { holding: Assets, origin: Option, trader: Config::Trader, - /// The most recent error result and instruction index into the fragment in which it occured, + /// The most recent error result and instruction index into the fragment in which it occurred, /// if any. error: Option<(u32, XcmError)>, /// The surplus weight, defined as the amount by which `max_weight` is From dbcde1b59208df5907da1d426719f02b450523de Mon Sep 17 00:00:00 2001 From: Keith Yeung Date: Wed, 29 Sep 2021 15:44:44 -0700 Subject: [PATCH 14/14] impl Into in more places --- xcm/pallet-xcm/src/lib.rs | 32 +++++++++++++++++++++----------- xcm/xcm-executor/src/lib.rs | 3 ++- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/xcm/pallet-xcm/src/lib.rs b/xcm/pallet-xcm/src/lib.rs index 1f9969918d16..6bff243feb08 100644 --- a/xcm/pallet-xcm/src/lib.rs +++ b/xcm/pallet-xcm/src/lib.rs @@ -426,7 +426,7 @@ pub mod pallet { weight_used += T::DbWeight::get().read + T::DbWeight::get().write; q.sort_by_key(|i| i.1); while let Some((versioned_dest, _)) = q.pop() { - if let Ok(dest) = versioned_dest.try_into() { + if let Ok(dest) = MultiLocation::try_from(versioned_dest) { if Self::request_version_notify(dest).is_ok() { // TODO: correct weights. weight_used += T::DbWeight::get().read + T::DbWeight::get().write; @@ -688,7 +688,8 @@ pub mod pallet { location: Box, ) -> DispatchResult { ensure_root(origin)?; - let location = (*location).try_into().map_err(|()| Error::::BadLocation)?; + let location: MultiLocation = + (*location).try_into().map_err(|()| Error::::BadLocation)?; Self::request_version_notify(location).map_err(|e| { match e { XcmError::InvalidLocation => Error::::AlreadySubscribed, @@ -710,7 +711,8 @@ pub mod pallet { location: Box, ) -> DispatchResult { ensure_root(origin)?; - let location = (*location).try_into().map_err(|()| Error::::BadLocation)?; + let location: MultiLocation = + (*location).try_into().map_err(|()| Error::::BadLocation)?; Self::unrequest_version_notify(location).map_err(|e| { match e { XcmError::InvalidLocation => Error::::NoSubscription, @@ -867,7 +869,8 @@ pub mod pallet { } /// Request that `dest` informs us of its version. - pub fn request_version_notify(dest: MultiLocation) -> XcmResult { + pub fn request_version_notify(dest: impl Into) -> XcmResult { + let dest = dest.into(); let versioned_dest = VersionedMultiLocation::from(dest.clone()); let already = VersionNotifiers::::contains_key(XCM_VERSION, &versioned_dest); ensure!(!already, XcmError::InvalidLocation); @@ -887,7 +890,8 @@ pub mod pallet { } /// Request that `dest` ceases informing us of its version. - pub fn unrequest_version_notify(dest: MultiLocation) -> XcmResult { + pub fn unrequest_version_notify(dest: impl Into) -> XcmResult { + let dest = dest.into(); let versioned_dest = LatestVersionedMultiLocation(&dest); let query_id = VersionNotifiers::::take(XCM_VERSION, versioned_dest) .ok_or(XcmError::InvalidLocation)?; @@ -918,7 +922,7 @@ pub mod pallet { } fn do_new_query( - responder: MultiLocation, + responder: impl Into, maybe_notify: Option<(u8, u8)>, timeout: T::BlockNumber, ) -> u64 { @@ -927,7 +931,11 @@ pub mod pallet { q.saturating_inc(); Queries::::insert( r, - QueryStatus::Pending { responder: responder.into(), maybe_notify, timeout }, + QueryStatus::Pending { + responder: responder.into().into(), + maybe_notify, + timeout, + }, ); r }) @@ -947,9 +955,10 @@ pub mod pallet { /// value. pub fn report_outcome( message: &mut Xcm<()>, - responder: MultiLocation, + responder: impl Into, timeout: T::BlockNumber, ) -> Result { + let responder = responder.into(); let dest = T::LocationInverter::invert_location(&responder) .map_err(|()| XcmError::MultiLocationNotInvertible)?; let query_id = Self::new_query(responder, timeout); @@ -980,10 +989,11 @@ pub mod pallet { /// may be put in the overweight queue and need to be manually executed. pub fn report_outcome_notify( message: &mut Xcm<()>, - responder: MultiLocation, + responder: impl Into, notify: impl Into<::Call>, timeout: T::BlockNumber, ) -> Result<(), XcmError> { + let responder = responder.into(); let dest = T::LocationInverter::invert_location(&responder) .map_err(|()| XcmError::MultiLocationNotInvertible)?; let notify: ::Call = notify.into(); @@ -995,14 +1005,14 @@ pub mod pallet { } /// Attempt to create a new query ID and register it as a query that is yet to respond. - pub fn new_query(responder: MultiLocation, timeout: T::BlockNumber) -> u64 { + pub fn new_query(responder: impl Into, timeout: T::BlockNumber) -> u64 { Self::do_new_query(responder, None, timeout) } /// Attempt to create a new query ID and register it as a query that is yet to respond, and /// which will call a dispatchable when a response happens. pub fn new_notify_query( - responder: MultiLocation, + responder: impl Into, notify: impl Into<::Call>, timeout: T::BlockNumber, ) -> u64 { diff --git a/xcm/xcm-executor/src/lib.rs b/xcm/xcm-executor/src/lib.rs index f714ad121870..f46963f6fbcd 100644 --- a/xcm/xcm-executor/src/lib.rs +++ b/xcm/xcm-executor/src/lib.rs @@ -150,7 +150,8 @@ impl From for frame_benchmarking::BenchmarkError { } impl XcmExecutor { - pub fn new(origin: MultiLocation) -> Self { + pub fn new(origin: impl Into) -> Self { + let origin = origin.into(); Self { holding: Assets::new(), origin: Some(origin.clone()),