1
// Copyright (C) Parity Technologies (UK) Ltd.
2
// This file is part of Polkadot.
3

            
4
// Polkadot is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8

            
9
// Polkadot is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13

            
14
// You should have received a copy of the GNU General Public License
15
// along with Polkadot.  If not, see <http://www.gnu.org/licenses/>.
16

            
17
//! Various implementations for `SendXcm`.
18

            
19
use alloc::vec::Vec;
20
use codec::Encode;
21
use core::{marker::PhantomData, result::Result};
22
use frame_system::unique;
23
use xcm::prelude::*;
24
use xcm_executor::{traits::FeeReason, FeesMode};
25

            
26
/// Wrapper router which, if the message does not already end with a `SetTopic` instruction,
27
/// appends one to the message filled with a universally unique ID. This ID is returned from a
28
/// successful `deliver`.
29
///
30
/// If the message does already end with a `SetTopic` instruction, then it is the responsibility
31
/// of the code author to ensure that the ID supplied to `SetTopic` is universally unique. Due to
32
/// this property, consumers of the topic ID must be aware that a user-supplied ID may not be
33
/// unique.
34
///
35
/// This is designed to be at the top-level of any routers, since it will always mutate the
36
/// passed `message` reference into a `None`. Don't try to combine it within a tuple except as the
37
/// last element.
38
pub struct WithUniqueTopic<Inner>(PhantomData<Inner>);
39
impl<Inner: SendXcm> SendXcm for WithUniqueTopic<Inner> {
40
	type Ticket = (Inner::Ticket, [u8; 32]);
41

            
42
6951
	fn validate(
43
6951
		destination: &mut Option<Location>,
44
6951
		message: &mut Option<Xcm<()>>,
45
6951
	) -> SendResult<Self::Ticket> {
46
6951
		let mut message = message.take().ok_or(SendError::MissingArgument)?;
47
6951
		let unique_id = if let Some(SetTopic(id)) = message.last() {
48
24
			*id
49
		} else {
50
6927
			let unique_id = unique(&message);
51
6927
			message.0.push(SetTopic(unique_id));
52
6927
			unique_id
53
		};
54
6951
		let (ticket, assets) = Inner::validate(destination, &mut Some(message))?;
55
1842
		Ok(((ticket, unique_id), assets))
56
6951
	}
57

            
58
1203
	fn deliver(ticket: Self::Ticket) -> Result<XcmHash, SendError> {
59
1203
		let (ticket, unique_id) = ticket;
60
1203
		Inner::deliver(ticket)?;
61
1203
		Ok(unique_id)
62
1203
	}
63
}
64
impl<Inner: InspectMessageQueues> InspectMessageQueues for WithUniqueTopic<Inner> {
65
	fn get_messages() -> Vec<(VersionedLocation, Vec<VersionedXcm<()>>)> {
66
		Inner::get_messages()
67
	}
68
}
69

            
70
pub trait SourceTopic {
71
	fn source_topic(entropy: impl Encode) -> XcmHash;
72
}
73

            
74
impl SourceTopic for () {
75
	fn source_topic(_: impl Encode) -> XcmHash {
76
		[0u8; 32]
77
	}
78
}
79

            
80
/// Wrapper router which, if the message does not already end with a `SetTopic` instruction,
81
/// prepends one to the message filled with an ID from `TopicSource`. This ID is returned from a
82
/// successful `deliver`.
83
///
84
/// This is designed to be at the top-level of any routers, since it will always mutate the
85
/// passed `message` reference into a `None`. Don't try to combine it within a tuple except as the
86
/// last element.
87
pub struct WithTopicSource<Inner, TopicSource>(PhantomData<(Inner, TopicSource)>);
88
impl<Inner: SendXcm, TopicSource: SourceTopic> SendXcm for WithTopicSource<Inner, TopicSource> {
89
	type Ticket = (Inner::Ticket, [u8; 32]);
90

            
91
	fn validate(
92
		destination: &mut Option<Location>,
93
		message: &mut Option<Xcm<()>>,
94
	) -> SendResult<Self::Ticket> {
95
		let mut message = message.take().ok_or(SendError::MissingArgument)?;
96
		let unique_id = if let Some(SetTopic(id)) = message.last() {
97
			*id
98
		} else {
99
			let unique_id = TopicSource::source_topic(&message);
100
			message.0.push(SetTopic(unique_id));
101
			unique_id
102
		};
103
		let (ticket, assets) = Inner::validate(destination, &mut Some(message))
104
			.map_err(|_| SendError::NotApplicable)?;
105
		Ok(((ticket, unique_id), assets))
106
	}
107

            
108
	fn deliver(ticket: Self::Ticket) -> Result<XcmHash, SendError> {
109
		let (ticket, unique_id) = ticket;
110
		Inner::deliver(ticket)?;
111
		Ok(unique_id)
112
	}
113
}
114

            
115
/// Trait for a type which ensures all requirements for successful delivery with XCM transport
116
/// layers.
117
pub trait EnsureDelivery {
118
	/// Prepare all requirements for successful `XcmSender: SendXcm` passing (accounts, balances,
119
	/// channels ...). Returns:
120
	/// - possible `FeesMode` which is expected to be set to executor
121
	/// - possible `Assets` which are expected to be subsume to the Holding Register
122
	fn ensure_successful_delivery(
123
		origin_ref: &Location,
124
		dest: &Location,
125
		fee_reason: FeeReason,
126
	) -> (Option<FeesMode>, Option<Assets>);
127
}
128

            
129
/// Tuple implementation for `EnsureDelivery`.
130
#[impl_trait_for_tuples::impl_for_tuples(30)]
131
impl EnsureDelivery for Tuple {
132
	fn ensure_successful_delivery(
133
		origin_ref: &Location,
134
		dest: &Location,
135
		fee_reason: FeeReason,
136
	) -> (Option<FeesMode>, Option<Assets>) {
137
		for_tuples!( #(
138
			// If the implementation returns something, we're done; if not, let others try.
139
			match Tuple::ensure_successful_delivery(origin_ref, dest, fee_reason.clone()) {
140
				r @ (Some(_), Some(_)) | r @ (Some(_), None) | r @ (None, Some(_)) => return r,
141
				(None, None) => (),
142
			}
143
		)* );
144
		// doing nothing
145
		(None, None)
146
	}
147
}
148

            
149
/// Inspects messages in queues.
150
/// Meant to be used in runtime APIs, not in runtimes.
151
pub trait InspectMessageQueues {
152
	/// Get queued messages and their destinations.
153
	fn get_messages() -> Vec<(VersionedLocation, Vec<VersionedXcm<()>>)>;
154
}
155

            
156
#[impl_trait_for_tuples::impl_for_tuples(30)]
157
impl InspectMessageQueues for Tuple {
158
	fn get_messages() -> Vec<(VersionedLocation, Vec<VersionedXcm<()>>)> {
159
		let mut messages = Vec::new();
160

            
161
		for_tuples!( #(
162
			messages.append(&mut Tuple::get_messages());
163
		)* );
164

            
165
		messages
166
	}
167
}
168

            
169
/// A wrapper router that attempts to *encode* and *decode* passed XCM `message` to ensure that the
170
/// receiving side will be able to decode, at least with the same XCM version.
171
///
172
/// This is designed to be at the top-level of any routers which do the real delivery. While other
173
/// routers can manipulate the `message`, we cannot access the final XCM due to the generic
174
/// `Inner::Ticket`. Therefore, this router aims to validate at least the passed `message`.
175
///
176
/// NOTE: For use in mock runtimes which don't have the DMP/UMP/HRMP XCM validations.
177
pub struct EnsureDecodableXcm<Inner>(core::marker::PhantomData<Inner>);
178
impl<Inner: SendXcm> SendXcm for EnsureDecodableXcm<Inner> {
179
	type Ticket = Inner::Ticket;
180

            
181
	fn validate(
182
		destination: &mut Option<Location>,
183
		message: &mut Option<Xcm<()>>,
184
	) -> SendResult<Self::Ticket> {
185
		if let Some(msg) = message {
186
			let versioned_xcm = VersionedXcm::<()>::from(msg.clone());
187
			if versioned_xcm.validate_xcm_nesting().is_err() {
188
				log::error!(
189
					target: "xcm::validate_xcm_nesting",
190
					"EnsureDecodableXcm validate_xcm_nesting error for \nversioned_xcm: {versioned_xcm:?}\nbased on xcm: {msg:?}"
191
				);
192
				return Err(SendError::Transport("EnsureDecodableXcm validate_xcm_nesting error"))
193
			}
194
		}
195
		Inner::validate(destination, message)
196
	}
197

            
198
	fn deliver(ticket: Self::Ticket) -> Result<XcmHash, SendError> {
199
		Inner::deliver(ticket)
200
	}
201
}