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
44
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// 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/>.

use crate::Xcm;
use core::result;
use frame_support::{
	dispatch::fmt::Debug,
	pallet_prelude::{Get, TypeInfo},
};
use parity_scale_codec::{FullCodec, MaxEncodedLen};
use sp_arithmetic::traits::Zero;
use xcm::latest::{
	Error as XcmError, InteriorMultiLocation, MultiLocation, QueryId, Response,
	Result as XcmResult, Weight, XcmContext,
};

/// Define what needs to be done upon receiving a query response.
pub trait OnResponse {
	/// Returns `true` if we are expecting a response from `origin` for query `query_id` that was
	/// queried by `querier`.
	fn expecting_response(
		origin: &MultiLocation,
		query_id: u64,
		querier: Option<&MultiLocation>,
	) -> bool;
	/// Handler for receiving a `response` from `origin` relating to `query_id` initiated by
	/// `querier`.
	fn on_response(
		origin: &MultiLocation,
		query_id: u64,
		querier: Option<&MultiLocation>,
		response: Response,
		max_weight: Weight,
		context: &XcmContext,
	) -> Weight;
}
impl OnResponse for () {
	fn expecting_response(
		_origin: &MultiLocation,
		_query_id: u64,
		_querier: Option<&MultiLocation>,
	) -> bool {
		false
	}
	fn on_response(
		_origin: &MultiLocation,
		_query_id: u64,
		_querier: Option<&MultiLocation>,
		_response: Response,
		_max_weight: Weight,
		_context: &XcmContext,
	) -> Weight {
		Weight::zero()
	}
}

/// Trait for a type which handles notifying a destination of XCM version changes.
pub trait VersionChangeNotifier {
	/// Start notifying `location` should the XCM version of this chain change.
	///
	/// When it does, this type should ensure a `QueryResponse` message is sent with the given
	/// `query_id` & `max_weight` and with a `response` of `Response::Version`. This should happen
	/// until/unless `stop` is called with the correct `query_id`.
	///
	/// If the `location` has an ongoing notification and when this function is called, then an
	/// error should be returned.
	fn start(
		location: &MultiLocation,
		query_id: QueryId,
		max_weight: Weight,
		context: &XcmContext,
	) -> XcmResult;

	/// Stop notifying `location` should the XCM change. Returns an error if there is no existing
	/// notification set up.
	fn stop(location: &MultiLocation, context: &XcmContext) -> XcmResult;

	/// Return true if a location is subscribed to XCM version changes.
	fn is_subscribed(location: &MultiLocation) -> bool;
}

impl VersionChangeNotifier for () {
	fn start(_: &MultiLocation, _: QueryId, _: Weight, _: &XcmContext) -> XcmResult {
		Err(XcmError::Unimplemented)
	}
	fn stop(_: &MultiLocation, _: &XcmContext) -> XcmResult {
		Err(XcmError::Unimplemented)
	}
	fn is_subscribed(_: &MultiLocation) -> bool {
		false
	}
}

/// The possible state of an XCM query response.
#[derive(Debug, PartialEq, Eq)]
pub enum QueryResponseStatus<BlockNumber> {
	/// The response has arrived, and includes the inner Response and the block number it arrived
	/// at.
	Ready { response: Response, at: BlockNumber },
	/// The response has not yet arrived, the XCM might still be executing or the response might be
	/// in transit.
	Pending { timeout: BlockNumber },
	/// No response with the given `QueryId` was found, or the response was already queried and
	/// removed from local storage.
	NotFound,
	/// Got an unexpected XCM version.
	UnexpectedVersion,
}

/// Provides methods to expect responses from XCMs and query their status.
pub trait QueryHandler {
	type QueryId: From<u64>
		+ FullCodec
		+ MaxEncodedLen
		+ TypeInfo
		+ Clone
		+ Eq
		+ PartialEq
		+ Debug
		+ Copy;
	type BlockNumber: Zero;
	type Error;
	type UniversalLocation: Get<InteriorMultiLocation>;

	/// Attempt to create a new query ID and register it as a query that is yet to respond.
	fn new_query(
		responder: impl Into<MultiLocation>,
		timeout: Self::BlockNumber,
		match_querier: impl Into<MultiLocation>,
	) -> QueryId;

	/// Consume `message` and return another which is equivalent to it except that it reports
	/// back the outcome.
	///
	/// - `message`: The message whose outcome should be reported.
	/// - `responder`: The origin from which a response should be expected.
	/// - `timeout`: The block number after which it is permissible to return `NotFound` from
	///   `take_response`.
	///
	/// `report_outcome` may return an error if the `responder` is not invertible.
	///
	/// It is assumed that the querier of the response will be `Here`.
	/// The response can be queried with `take_response`.
	fn report_outcome(
		message: &mut Xcm<()>,
		responder: impl Into<MultiLocation>,
		timeout: Self::BlockNumber,
	) -> result::Result<Self::QueryId, Self::Error>;

	/// Attempt to remove and return the response of query with ID `query_id`.
	fn take_response(id: Self::QueryId) -> QueryResponseStatus<Self::BlockNumber>;

	/// Makes sure to expect a response with the given id.
	#[cfg(feature = "runtime-benchmarks")]
	fn expect_response(id: Self::QueryId, response: Response);
}