Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! Various implementations for `SendXcm`.
use frame_system::unique;
use parity_scale_codec::Encode;
use sp_std::{marker::PhantomData, result::Result};
use xcm::prelude::*;
/// Wrapper router which, if the message does not already end with a `SetTopic` instruction,
/// appends one to the message filled with a universally unique ID. This ID is returned from a
/// successful `deliver`.
///
/// This is designed to be at the top-level of any routers, since it will always mutate the
/// passed `message` reference into a `None`. Don't try to combine it within a tuple except as the
/// last element.
pub struct WithUniqueTopic<Inner>(PhantomData<Inner>);
impl<Inner: SendXcm> SendXcm for WithUniqueTopic<Inner> {
type Ticket = (Inner::Ticket, [u8; 32]);
fn validate(
destination: &mut Option<MultiLocation>,
message: &mut Option<Xcm<()>>,
) -> SendResult<Self::Ticket> {
let mut message = message.take().ok_or(SendError::MissingArgument)?;
let unique_id = if let Some(SetTopic(id)) = message.last() {
*id
} else {
let unique_id = unique(&message);
message.0.push(SetTopic(unique_id));
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
unique_id
};
let (ticket, assets) = Inner::validate(destination, &mut Some(message))
.map_err(|_| SendError::NotApplicable)?;
Ok(((ticket, unique_id), assets))
}
fn deliver(ticket: Self::Ticket) -> Result<XcmHash, SendError> {
let (ticket, unique_id) = ticket;
Inner::deliver(ticket)?;
Ok(unique_id)
}
}
pub trait SourceTopic {
fn source_topic(entropy: impl Encode) -> XcmHash;
}
impl SourceTopic for () {
fn source_topic(_: impl Encode) -> XcmHash {
[0u8; 32]
}
}
/// Wrapper router which, if the message does not already end with a `SetTopic` instruction,
/// prepends one to the message filled with an ID from `TopicSource`. This ID is returned from a
/// successful `deliver`.
///
/// This is designed to be at the top-level of any routers, since it will always mutate the
/// passed `message` reference into a `None`. Don't try to combine it within a tuple except as the
/// last element.
pub struct WithTopicSource<Inner, TopicSource>(PhantomData<(Inner, TopicSource)>);
impl<Inner: SendXcm, TopicSource: SourceTopic> SendXcm for WithTopicSource<Inner, TopicSource> {
type Ticket = (Inner::Ticket, [u8; 32]);
fn validate(
destination: &mut Option<MultiLocation>,
message: &mut Option<Xcm<()>>,
) -> SendResult<Self::Ticket> {
let mut message = message.take().ok_or(SendError::MissingArgument)?;
let unique_id = if let Some(SetTopic(id)) = message.last() {
*id
} else {
let unique_id = TopicSource::source_topic(&message);
message.0.push(SetTopic(unique_id));
unique_id
};
let (ticket, assets) = Inner::validate(destination, &mut Some(message))
.map_err(|_| SendError::NotApplicable)?;
Ok(((ticket, unique_id), assets))
}
fn deliver(ticket: Self::Ticket) -> Result<XcmHash, SendError> {
let (ticket, unique_id) = ticket;
Inner::deliver(ticket)?;
Ok(unique_id)
}
}