Skip to content
Merged
Changes from all 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
107 changes: 0 additions & 107 deletions bin/runtime-common/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1015,113 +1015,6 @@ pub mod target {
}
}

pub use xcm_copy::*;

// copy of private types from xcm-builder/src/universal_exports.rs
pub mod xcm_copy {
use codec::{Decode, Encode};
use frame_support::{ensure, traits::Get};
use sp_std::{convert::TryInto, marker::PhantomData, prelude::*};
use xcm::prelude::*;
use xcm_executor::traits::ExportXcm;

pub trait DispatchBlob {
/// Dispatches an incoming blob and returns the unexpectable weight consumed by the
/// dispatch.
fn dispatch_blob(blob: Vec<u8>) -> Result<(), DispatchBlobError>;
}

pub trait HaulBlob {
/// Sends a blob over some point-to-point link. This will generally be implemented by a
/// bridge.
fn haul_blob(blob: Vec<u8>);
}

#[derive(Clone, Encode, Decode)]
pub struct BridgeMessage {
/// The message destination as a *Universal Location*. This means it begins with a
/// `GlobalConsensus` junction describing the network under which global consensus happens.
/// If this does not match our global consensus then it's a fatal error.
universal_dest: VersionedInteriorMultiLocation,
message: VersionedXcm<()>,
}

pub enum DispatchBlobError {
Unbridgable,
InvalidEncoding,
UnsupportedLocationVersion,
UnsupportedXcmVersion,
RoutingError,
NonUniversalDestination,
WrongGlobal,
}

pub struct BridgeBlobDispatcher<Router, OurPlace>(PhantomData<(Router, OurPlace)>);
impl<Router: SendXcm, OurPlace: Get<InteriorMultiLocation>> DispatchBlob
for BridgeBlobDispatcher<Router, OurPlace>
{
fn dispatch_blob(blob: Vec<u8>) -> Result<(), DispatchBlobError> {
let our_universal = OurPlace::get();
let our_global =
our_universal.global_consensus().map_err(|()| DispatchBlobError::Unbridgable)?;
let BridgeMessage { universal_dest, message } =
Decode::decode(&mut &blob[..]).map_err(|_| DispatchBlobError::InvalidEncoding)?;
let universal_dest: InteriorMultiLocation = universal_dest
.try_into()
.map_err(|_| DispatchBlobError::UnsupportedLocationVersion)?;
// `universal_dest` is the desired destination within the universe: first we need to
// check we're in the right global consensus.
let intended_global = universal_dest
.global_consensus()
.map_err(|()| DispatchBlobError::NonUniversalDestination)?;
ensure!(intended_global == our_global, DispatchBlobError::WrongGlobal);
let dest = universal_dest.relative_to(&our_universal);
let message: Xcm<()> =
message.try_into().map_err(|_| DispatchBlobError::UnsupportedXcmVersion)?;
send_xcm::<Router>(dest, message).map_err(|_| DispatchBlobError::RoutingError)?;
Ok(())
}
}

pub struct HaulBlobExporter<Bridge, BridgedNetwork, Price>(
PhantomData<(Bridge, BridgedNetwork, Price)>,
);
impl<Bridge: HaulBlob, BridgedNetwork: Get<NetworkId>, Price: Get<MultiAssets>> ExportXcm
for HaulBlobExporter<Bridge, BridgedNetwork, Price>
{
type Ticket = (Vec<u8>, XcmHash);

fn validate(
network: NetworkId,
_channel: u32,
_universal_source: &mut Option<InteriorMultiLocation>,
destination: &mut Option<InteriorMultiLocation>,
message: &mut Option<Xcm<()>>,
) -> Result<((Vec<u8>, XcmHash), MultiAssets), SendError> {
let bridged_network = BridgedNetwork::get();
ensure!(network == bridged_network, SendError::NotApplicable);
// We don't/can't use the `channel` for this adapter.
let dest = destination.take().ok_or(SendError::MissingArgument)?;
let universal_dest = match dest.pushed_front_with(GlobalConsensus(bridged_network)) {
Ok(d) => d.into(),
Err((dest, _)) => {
*destination = Some(dest);
return Err(SendError::NotApplicable)
},
};
let message = VersionedXcm::from(message.take().ok_or(SendError::MissingArgument)?);
let hash = message.using_encoded(sp_io::hashing::blake2_256);
let blob = BridgeMessage { universal_dest, message }.encode();
Ok(((blob, hash), Price::get()))
}

fn deliver((blob, hash): (Vec<u8>, XcmHash)) -> Result<XcmHash, SendError> {
Bridge::haul_blob(blob);
Ok(hash)
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down