Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Change send_xcm MultiLocation argument to be generic
  • Loading branch information
KiChjang committed Aug 11, 2021
commit e03f549357b5c7d829de66d50111014796a4d21c
3 changes: 2 additions & 1 deletion runtime/common/src/xcm_sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ pub struct ChildParachainRouter<T, W>(PhantomData<(T, W)>);
impl<T: configuration::Config + dmp::Config, W: xcm::WrapVersion> SendXcm
for ChildParachainRouter<T, W>
{
fn send_xcm(dest: MultiLocation, msg: Xcm) -> Result {
fn send_xcm(dest: impl Into<MultiLocation>, msg: Xcm) -> Result {
let dest = dest.into();
match dest {
MultiLocation { parents: 0, interior: Junctions::X1(Junction::Parachain(id)) } => {
// Downward message passing.
Expand Down
2 changes: 1 addition & 1 deletion runtime/test-runtime/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MultiLocation>, _msg: Xcm<()>) -> XcmResult {
Ok(())
}
}
Expand Down
7 changes: 4 additions & 3 deletions xcm/pallet-xcm/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MultiLocation>, 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<MultiLocation>, msg: Xcm) -> XcmResult {
let dest = dest.into();
if dest.len() == 8 {
Err(XcmError::Undefined)
} else {
Expand Down
12 changes: 6 additions & 6 deletions xcm/src/v1/multilocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,24 +325,24 @@ impl From<Parent> 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<ParentThen> 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<Ancestor> 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<AncestorThen> for MultiLocation {
fn from(x: AncestorThen) -> Self {
MultiLocation { parents: x.0, interior: x.1 }
fn from(AncestorThen(parents, interior): AncestorThen) -> Self {
MultiLocation { parents, interior }
}
}

Expand Down
16 changes: 9 additions & 7 deletions xcm/src/v1/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,15 +194,16 @@ impl<C> ExecuteXcm<C> 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<MultiLocation>, 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<MultiLocation>, message: Xcm<()>) -> Result {
/// let destination = destination.into();
/// if matches!(destination.interior(), Junctions::X2(j1, j2))
/// && destination.parent_count() == 0
/// {
Expand All @@ -216,7 +217,8 @@ impl<C> ExecuteXcm<C> 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<MultiLocation>, message: Xcm<()>) -> Result {
/// let destination = destination.into();
/// if matches!(destination.interior(), Junctions::Here)
/// && destination.parent_count() == 1
/// {
Expand Down Expand Up @@ -252,19 +254,19 @@ 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<MultiLocation>, 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<MultiLocation>, 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) {
Err(Error::CannotReachDestination(d, m)) => (d, m),
o @ _ => return o,
};
)* );
Err(Error::CannotReachDestination(destination, message))
Err(Error::CannotReachDestination(destination.into(), message))
}
}
4 changes: 2 additions & 2 deletions xcm/xcm-builder/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MultiLocation>, msg: opaque::Xcm) -> XcmResult {
SENT_XCM.with(|q| q.borrow_mut().push((dest.into(), msg)));
Ok(())
}
}
Expand Down
6 changes: 4 additions & 2 deletions xcm/xcm-simulator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,10 @@ macro_rules! decl_test_network {
pub struct ParachainXcmRouter<T>($crate::PhantomData<T>);

impl<T: $crate::Get<$crate::ParaId>> $crate::SendXcm for ParachainXcmRouter<T> {
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);
Expand Down Expand Up @@ -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 => {
Expand Down