|
14 | 14 | // You should have received a copy of the GNU General Public License
|
15 | 15 | // along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
|
16 | 16 |
|
| 17 | +//! Mock implementations to test XCM builder configuration types. |
| 18 | +
|
17 | 19 | use crate::{
|
18 | 20 | barriers::{AllowSubscriptionsFrom, RespectSuspension, TrailingSetTopicAsId},
|
19 | 21 | test_utils::*,
|
@@ -42,7 +44,7 @@ pub use sp_std::{
|
42 | 44 | marker::PhantomData,
|
43 | 45 | };
|
44 | 46 | pub use xcm::latest::{prelude::*, Weight};
|
45 |
| -use xcm_executor::traits::Properties; |
| 47 | +use xcm_executor::traits::{Properties, QueryHandler, QueryResponseStatus}; |
46 | 48 | pub use xcm_executor::{
|
47 | 49 | traits::{
|
48 | 50 | AssetExchange, AssetLock, CheckSuspension, ConvertOrigin, Enact, ExportXcm, FeeManager,
|
@@ -410,6 +412,63 @@ pub fn response(query_id: u64) -> Option<Response> {
|
410 | 412 | })
|
411 | 413 | }
|
412 | 414 |
|
| 415 | +/// Mock implementation of the [`QueryHandler`] trait for creating XCM success queries and expecting |
| 416 | +/// responses. |
| 417 | +pub struct TestQueryHandler<T, BlockNumber>(core::marker::PhantomData<(T, BlockNumber)>); |
| 418 | +impl<T: Config, BlockNumber: sp_runtime::traits::Zero> QueryHandler |
| 419 | + for TestQueryHandler<T, BlockNumber> |
| 420 | +{ |
| 421 | + type QueryId = u64; |
| 422 | + type BlockNumber = BlockNumber; |
| 423 | + type Error = XcmError; |
| 424 | + type UniversalLocation = T::UniversalLocation; |
| 425 | + |
| 426 | + fn new_query( |
| 427 | + responder: impl Into<MultiLocation>, |
| 428 | + _timeout: Self::BlockNumber, |
| 429 | + _match_querier: impl Into<MultiLocation>, |
| 430 | + ) -> Self::QueryId { |
| 431 | + let query_id = 1; |
| 432 | + expect_response(query_id, responder.into()); |
| 433 | + query_id |
| 434 | + } |
| 435 | + |
| 436 | + fn report_outcome( |
| 437 | + message: &mut Xcm<()>, |
| 438 | + responder: impl Into<MultiLocation>, |
| 439 | + timeout: Self::BlockNumber, |
| 440 | + ) -> Result<Self::QueryId, Self::Error> { |
| 441 | + let responder = responder.into(); |
| 442 | + let destination = Self::UniversalLocation::get() |
| 443 | + .invert_target(&responder) |
| 444 | + .map_err(|()| XcmError::LocationNotInvertible)?; |
| 445 | + let query_id = Self::new_query(responder, timeout, Here); |
| 446 | + let response_info = QueryResponseInfo { destination, query_id, max_weight: Weight::zero() }; |
| 447 | + let report_error = Xcm(vec![ReportError(response_info)]); |
| 448 | + message.0.insert(0, SetAppendix(report_error)); |
| 449 | + Ok(query_id) |
| 450 | + } |
| 451 | + |
| 452 | + fn take_response(query_id: Self::QueryId) -> QueryResponseStatus<Self::BlockNumber> { |
| 453 | + QUERIES |
| 454 | + .with(|q| { |
| 455 | + q.borrow().get(&query_id).and_then(|v| match v { |
| 456 | + ResponseSlot::Received(r) => Some(QueryResponseStatus::Ready { |
| 457 | + response: r.clone(), |
| 458 | + at: Self::BlockNumber::zero(), |
| 459 | + }), |
| 460 | + _ => Some(QueryResponseStatus::NotFound), |
| 461 | + }) |
| 462 | + }) |
| 463 | + .unwrap_or(QueryResponseStatus::NotFound) |
| 464 | + } |
| 465 | + |
| 466 | + #[cfg(feature = "runtime-benchmarks")] |
| 467 | + fn expect_response(_id: Self::QueryId, _response: xcm::latest::Response) { |
| 468 | + // Unnecessary since it's only a test implementation |
| 469 | + } |
| 470 | +} |
| 471 | + |
413 | 472 | parameter_types! {
|
414 | 473 | pub static ExecutorUniversalLocation: InteriorMultiLocation
|
415 | 474 | = (ByGenesis([0; 32]), Parachain(42)).into();
|
|
0 commit comments