staging_xcm/v4/traits.rs
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//! Cross-Consensus Message format data structures.
18
19pub use crate::v3::{Error, Result, SendError, XcmHash};
20use codec::{Decode, Encode};
21use core::result;
22use scale_info::TypeInfo;
23
24pub use sp_weights::Weight;
25
26use super::*;
27
28/// Outcome of an XCM execution.
29#[derive(Clone, Encode, Decode, Eq, PartialEq, Debug, TypeInfo)]
30pub enum Outcome {
31 /// Execution completed successfully; given weight was used.
32 Complete { used: Weight },
33 /// Execution started, but did not complete successfully due to the given error; given weight
34 /// was used.
35 Incomplete { used: Weight, error: Error },
36 /// Execution did not start due to the given error.
37 Error { error: Error },
38}
39
40impl Outcome {
41 pub fn ensure_complete(self) -> Result {
42 match self {
43 Outcome::Complete { .. } => Ok(()),
44 Outcome::Incomplete { error, .. } => Err(error),
45 Outcome::Error { error, .. } => Err(error),
46 }
47 }
48 pub fn ensure_execution(self) -> result::Result<Weight, Error> {
49 match self {
50 Outcome::Complete { used, .. } => Ok(used),
51 Outcome::Incomplete { used, .. } => Ok(used),
52 Outcome::Error { error, .. } => Err(error),
53 }
54 }
55 /// How much weight was used by the XCM execution attempt.
56 pub fn weight_used(&self) -> Weight {
57 match self {
58 Outcome::Complete { used, .. } => *used,
59 Outcome::Incomplete { used, .. } => *used,
60 Outcome::Error { .. } => Weight::zero(),
61 }
62 }
63}
64
65impl From<Error> for Outcome {
66 fn from(error: Error) -> Self {
67 Self::Error { error }
68 }
69}
70
71pub trait PreparedMessage {
72 fn weight_of(&self) -> Weight;
73}
74
75/// Type of XCM message executor.
76pub trait ExecuteXcm<Call> {
77 type Prepared: PreparedMessage;
78 fn prepare(message: Xcm<Call>) -> result::Result<Self::Prepared, Xcm<Call>>;
79 fn execute(
80 origin: impl Into<Location>,
81 pre: Self::Prepared,
82 id: &mut XcmHash,
83 weight_credit: Weight,
84 ) -> Outcome;
85 fn prepare_and_execute(
86 origin: impl Into<Location>,
87 message: Xcm<Call>,
88 id: &mut XcmHash,
89 weight_limit: Weight,
90 weight_credit: Weight,
91 ) -> Outcome {
92 let Ok(pre) = Self::prepare(message) else {
93 return Outcome::Error { error: Error::WeightNotComputable };
94 };
95 let xcm_weight = pre.weight_of();
96 if xcm_weight.any_gt(weight_limit) {
97 return Outcome::Error { error: Error::WeightLimitReached(xcm_weight) };
98 }
99 Self::execute(origin, pre, id, weight_credit)
100 }
101
102 /// Deduct some `fees` to the sovereign account of the given `location` and place them as per
103 /// the convention for fees.
104 fn charge_fees(location: impl Into<Location>, fees: Assets) -> Result;
105}
106
107pub enum Weightless {}
108impl PreparedMessage for Weightless {
109 fn weight_of(&self) -> Weight {
110 unreachable!()
111 }
112}
113
114impl<C> ExecuteXcm<C> for () {
115 type Prepared = Weightless;
116 fn prepare(message: Xcm<C>) -> result::Result<Self::Prepared, Xcm<C>> {
117 Err(message)
118 }
119 fn execute(_: impl Into<Location>, _: Self::Prepared, _: &mut XcmHash, _: Weight) -> Outcome {
120 unreachable!()
121 }
122 fn charge_fees(_location: impl Into<Location>, _fees: Assets) -> Result {
123 Err(Error::Unimplemented)
124 }
125}
126
127pub trait Reanchorable: Sized {
128 /// Type to return in case of an error.
129 type Error: Debug;
130
131 /// Mutate `self` so that it represents the same location from the point of view of `target`.
132 /// The context of `self` is provided as `context`.
133 ///
134 /// Does not modify `self` in case of overflow.
135 fn reanchor(
136 &mut self,
137 target: &Location,
138 context: &InteriorLocation,
139 ) -> core::result::Result<(), ()>;
140
141 /// Consume `self` and return a new value representing the same location from the point of view
142 /// of `target`. The context of `self` is provided as `context`.
143 ///
144 /// Returns the original `self` in case of overflow.
145 fn reanchored(
146 self,
147 target: &Location,
148 context: &InteriorLocation,
149 ) -> core::result::Result<Self, Self::Error>;
150}
151
152/// Result value when attempting to send an XCM message.
153pub type SendResult<T> = result::Result<(T, Assets), SendError>;
154
155/// Utility for sending an XCM message to a given location.
156///
157/// These can be amalgamated in tuples to form sophisticated routing systems. In tuple format, each
158/// router might return `NotApplicable` to pass the execution to the next sender item. Note that
159/// each `NotApplicable` might alter the destination and the XCM message for to the next router.
160///
161/// # Example
162/// ```rust
163/// # use codec::Encode;
164/// # use staging_xcm::v4::{prelude::*, Weight};
165/// # use staging_xcm::VersionedXcm;
166/// # use std::convert::Infallible;
167///
168/// /// A sender that only passes the message through and does nothing.
169/// struct Sender1;
170/// impl SendXcm for Sender1 {
171/// type Ticket = Infallible;
172/// fn validate(_: &mut Option<Location>, _: &mut Option<Xcm<()>>) -> SendResult<Infallible> {
173/// Err(SendError::NotApplicable)
174/// }
175/// fn deliver(_: Infallible) -> Result<XcmHash, SendError> {
176/// unreachable!()
177/// }
178/// }
179///
180/// /// A sender that accepts a message that has two junctions, otherwise stops the routing.
181/// struct Sender2;
182/// impl SendXcm for Sender2 {
183/// type Ticket = ();
184/// fn validate(destination: &mut Option<Location>, message: &mut Option<Xcm<()>>) -> SendResult<()> {
185/// match destination.as_ref().ok_or(SendError::MissingArgument)?.unpack() {
186/// (0, [j1, j2]) => Ok(((), Assets::new())),
187/// _ => Err(SendError::Unroutable),
188/// }
189/// }
190/// fn deliver(_: ()) -> Result<XcmHash, SendError> {
191/// Ok([0; 32])
192/// }
193/// }
194///
195/// /// A sender that accepts a message from a parent, passing through otherwise.
196/// struct Sender3;
197/// impl SendXcm for Sender3 {
198/// type Ticket = ();
199/// fn validate(destination: &mut Option<Location>, message: &mut Option<Xcm<()>>) -> SendResult<()> {
200/// match destination.as_ref().ok_or(SendError::MissingArgument)?.unpack() {
201/// (1, []) => Ok(((), Assets::new())),
202/// _ => Err(SendError::NotApplicable),
203/// }
204/// }
205/// fn deliver(_: ()) -> Result<XcmHash, SendError> {
206/// Ok([0; 32])
207/// }
208/// }
209///
210/// // A call to send via XCM. We don't really care about this.
211/// # fn main() {
212/// let call: Vec<u8> = ().encode();
213/// let message = Xcm(vec![Instruction::Transact {
214/// origin_kind: OriginKind::Superuser,
215/// require_weight_at_most: Weight::zero(),
216/// call: call.into(),
217/// }]);
218/// let message_hash = message.using_encoded(sp_io::hashing::blake2_256);
219///
220/// // Sender2 will block this.
221/// assert!(send_xcm::<(Sender1, Sender2, Sender3)>(Parent.into(), message.clone()).is_err());
222///
223/// // Sender3 will catch this.
224/// assert!(send_xcm::<(Sender1, Sender3)>(Parent.into(), message.clone()).is_ok());
225/// # }
226/// ```
227pub trait SendXcm {
228 /// Intermediate value which connects the two phases of the send operation.
229 type Ticket;
230
231 /// Check whether the given `_message` is deliverable to the given `_destination` and if so
232 /// determine the cost which will be paid by this chain to do so, returning a `Validated` token
233 /// which can be used to enact delivery.
234 ///
235 /// The `destination` and `message` must be `Some` (or else an error will be returned) and they
236 /// may only be consumed if the `Err` is not `NotApplicable`.
237 ///
238 /// If it is not a destination which can be reached with this type but possibly could by others,
239 /// then this *MUST* return `NotApplicable`. Any other error will cause the tuple
240 /// implementation to exit early without trying other type fields.
241 fn validate(
242 destination: &mut Option<Location>,
243 message: &mut Option<Xcm<()>>,
244 ) -> SendResult<Self::Ticket>;
245
246 /// Actually carry out the delivery operation for a previously validated message sending.
247 fn deliver(ticket: Self::Ticket) -> result::Result<XcmHash, SendError>;
248}
249
250#[impl_trait_for_tuples::impl_for_tuples(30)]
251impl SendXcm for Tuple {
252 for_tuples! { type Ticket = (#( Option<Tuple::Ticket> ),* ); }
253
254 fn validate(
255 destination: &mut Option<Location>,
256 message: &mut Option<Xcm<()>>,
257 ) -> SendResult<Self::Ticket> {
258 let mut maybe_cost: Option<Assets> = None;
259 let one_ticket: Self::Ticket = (for_tuples! { #(
260 if maybe_cost.is_some() {
261 None
262 } else {
263 match Tuple::validate(destination, message) {
264 Err(SendError::NotApplicable) => None,
265 Err(e) => { return Err(e) },
266 Ok((v, c)) => {
267 maybe_cost = Some(c);
268 Some(v)
269 },
270 }
271 }
272 ),* });
273 if let Some(cost) = maybe_cost {
274 Ok((one_ticket, cost))
275 } else {
276 Err(SendError::NotApplicable)
277 }
278 }
279
280 fn deliver(one_ticket: Self::Ticket) -> result::Result<XcmHash, SendError> {
281 for_tuples!( #(
282 if let Some(validated) = one_ticket.Tuple {
283 return Tuple::deliver(validated);
284 }
285 )* );
286 Err(SendError::Unroutable)
287 }
288}
289
290/// Convenience function for using a `SendXcm` implementation. Just interprets the `dest` and wraps
291/// both in `Some` before passing them as mutable references into `T::send_xcm`.
292pub fn validate_send<T: SendXcm>(dest: Location, msg: Xcm<()>) -> SendResult<T::Ticket> {
293 T::validate(&mut Some(dest), &mut Some(msg))
294}
295
296/// Convenience function for using a `SendXcm` implementation. Just interprets the `dest` and wraps
297/// both in `Some` before passing them as mutable references into `T::send_xcm`.
298///
299/// Returns either `Ok` with the price of the delivery, or `Err` with the reason why the message
300/// could not be sent.
301///
302/// Generally you'll want to validate and get the price first to ensure that the sender can pay it
303/// before actually doing the delivery.
304pub fn send_xcm<T: SendXcm>(
305 dest: Location,
306 msg: Xcm<()>,
307) -> result::Result<(XcmHash, Assets), SendError> {
308 let (ticket, price) = T::validate(&mut Some(dest), &mut Some(msg))?;
309 let hash = T::deliver(ticket)?;
310 Ok((hash, price))
311}