Skip to content

Commit

Permalink
XCM remove extra QueryId types from traits (paritytech#3763)
Browse files Browse the repository at this point in the history
We do not need to make these traits generic over QueryId type, we can
just use the QueryId alias everywhere
  • Loading branch information
pgherveou authored and dharjeezy committed Mar 24, 2024
1 parent 47d9f7b commit 3c7d6b0
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 37 deletions.
11 changes: 5 additions & 6 deletions polkadot/xcm/pallet-xcm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ pub mod pallet {
origin: OriginFor<T>,
timeout: BlockNumberFor<T>,
match_querier: VersionedLocation,
) -> Result<Self::QueryId, DispatchError> {
) -> Result<QueryId, DispatchError> {
let responder = <T as Config>::ExecuteXcmOrigin::ensure_origin(origin)?;
let query_id = <Self as QueryHandler>::new_query(
responder,
Expand Down Expand Up @@ -1478,7 +1478,6 @@ impl<T: Config> sp_std::fmt::Debug for FeesHandling<T> {
}

impl<T: Config> QueryHandler for Pallet<T> {
type QueryId = u64;
type BlockNumber = BlockNumberFor<T>;
type Error = XcmError;
type UniversalLocation = T::UniversalLocation;
Expand All @@ -1488,7 +1487,7 @@ impl<T: Config> QueryHandler for Pallet<T> {
responder: impl Into<Location>,
timeout: BlockNumberFor<T>,
match_querier: impl Into<Location>,
) -> Self::QueryId {
) -> QueryId {
Self::do_new_query(responder, None, timeout, match_querier)
}

Expand All @@ -1498,7 +1497,7 @@ impl<T: Config> QueryHandler for Pallet<T> {
message: &mut Xcm<()>,
responder: impl Into<Location>,
timeout: Self::BlockNumber,
) -> Result<Self::QueryId, Self::Error> {
) -> Result<QueryId, Self::Error> {
let responder = responder.into();
let destination = Self::UniversalLocation::get()
.invert_target(&responder)
Expand All @@ -1511,7 +1510,7 @@ impl<T: Config> QueryHandler for Pallet<T> {
}

/// Removes response when ready and emits [Event::ResponseTaken] event.
fn take_response(query_id: Self::QueryId) -> QueryResponseStatus<Self::BlockNumber> {
fn take_response(query_id: QueryId) -> QueryResponseStatus<Self::BlockNumber> {
match Queries::<T>::get(query_id) {
Some(QueryStatus::Ready { response, at }) => match response.try_into() {
Ok(response) => {
Expand All @@ -1528,7 +1527,7 @@ impl<T: Config> QueryHandler for Pallet<T> {
}

#[cfg(feature = "runtime-benchmarks")]
fn expect_response(id: Self::QueryId, response: Response) {
fn expect_response(id: QueryId, response: Response) {
let response = response.into();
Queries::<T>::insert(
id,
Expand Down
4 changes: 2 additions & 2 deletions polkadot/xcm/xcm-builder/src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ pub trait QueryController<Origin, Timeout>: QueryHandler {
origin: Origin,
timeout: Timeout,
match_querier: VersionedLocation,
) -> Result<Self::QueryId, DispatchError>;
) -> Result<QueryId, DispatchError>;
}

impl<Origin, RuntimeCall> ExecuteController<Origin, RuntimeCall> for () {
Expand Down Expand Up @@ -186,7 +186,7 @@ impl<Origin, Timeout> QueryController<Origin, Timeout> for () {
_origin: Origin,
_timeout: Timeout,
_match_querier: VersionedLocation,
) -> Result<Self::QueryId, DispatchError> {
) -> Result<QueryId, DispatchError> {
Ok(Default::default())
}
}
2 changes: 1 addition & 1 deletion polkadot/xcm/xcm-builder/src/pay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl<
type Beneficiary = Beneficiary;
type AssetKind = AssetKind;
type Balance = u128;
type Id = Querier::QueryId;
type Id = QueryId;
type Error = xcm::latest::Error;

fn pay(
Expand Down
11 changes: 5 additions & 6 deletions polkadot/xcm/xcm-builder/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub use sp_std::{
collections::{btree_map::BTreeMap, btree_set::BTreeSet},
fmt::Debug,
};
pub use xcm::latest::{prelude::*, Weight};
pub use xcm::latest::{prelude::*, QueryId, Weight};
use xcm_executor::traits::{Properties, QueryHandler, QueryResponseStatus};
pub use xcm_executor::{
traits::{
Expand Down Expand Up @@ -414,7 +414,6 @@ pub struct TestQueryHandler<T, BlockNumber>(core::marker::PhantomData<(T, BlockN
impl<T: Config, BlockNumber: sp_runtime::traits::Zero + Encode> QueryHandler
for TestQueryHandler<T, BlockNumber>
{
type QueryId = u64;
type BlockNumber = BlockNumber;
type Error = XcmError;
type UniversalLocation = T::UniversalLocation;
Expand All @@ -423,7 +422,7 @@ impl<T: Config, BlockNumber: sp_runtime::traits::Zero + Encode> QueryHandler
responder: impl Into<Location>,
_timeout: Self::BlockNumber,
_match_querier: impl Into<Location>,
) -> Self::QueryId {
) -> QueryId {
let query_id = 1;
expect_response(query_id, responder.into());
query_id
Expand All @@ -433,7 +432,7 @@ impl<T: Config, BlockNumber: sp_runtime::traits::Zero + Encode> QueryHandler
message: &mut Xcm<()>,
responder: impl Into<Location>,
timeout: Self::BlockNumber,
) -> Result<Self::QueryId, Self::Error> {
) -> Result<QueryId, Self::Error> {
let responder = responder.into();
let destination = Self::UniversalLocation::get()
.invert_target(&responder)
Expand All @@ -445,7 +444,7 @@ impl<T: Config, BlockNumber: sp_runtime::traits::Zero + Encode> QueryHandler
Ok(query_id)
}

fn take_response(query_id: Self::QueryId) -> QueryResponseStatus<Self::BlockNumber> {
fn take_response(query_id: QueryId) -> QueryResponseStatus<Self::BlockNumber> {
QUERIES
.with(|q| {
q.borrow().get(&query_id).and_then(|v| match v {
Expand All @@ -460,7 +459,7 @@ impl<T: Config, BlockNumber: sp_runtime::traits::Zero + Encode> QueryHandler
}

#[cfg(feature = "runtime-benchmarks")]
fn expect_response(_id: Self::QueryId, _response: xcm::latest::Response) {
fn expect_response(_id: QueryId, _response: xcm::latest::Response) {
// Unnecessary since it's only a test implementation
}
}
Expand Down
31 changes: 9 additions & 22 deletions polkadot/xcm/xcm-executor/src/traits/on_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@

use crate::{Junctions::Here, Xcm};
use core::result;
use frame_support::{
pallet_prelude::{Get, TypeInfo},
parameter_types,
};
use parity_scale_codec::{Decode, Encode, FullCodec, MaxEncodedLen};
use frame_support::{pallet_prelude::Get, parameter_types};
use parity_scale_codec::{Decode, Encode};
use sp_arithmetic::traits::Zero;
use sp_std::fmt::Debug;
use xcm::latest::{
Expand Down Expand Up @@ -115,15 +112,6 @@ pub enum QueryResponseStatus<BlockNumber> {

/// 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 + Encode;
type Error;
type UniversalLocation: Get<InteriorLocation>;
Expand Down Expand Up @@ -151,14 +139,14 @@ pub trait QueryHandler {
message: &mut Xcm<()>,
responder: impl Into<Location>,
timeout: Self::BlockNumber,
) -> result::Result<Self::QueryId, Self::Error>;
) -> result::Result<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>;
fn take_response(id: 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);
fn expect_response(id: QueryId, response: Response);
}

parameter_types! {
Expand All @@ -168,28 +156,27 @@ parameter_types! {
impl QueryHandler for () {
type BlockNumber = u64;
type Error = ();
type QueryId = u64;
type UniversalLocation = UniversalLocation;

fn take_response(_query_id: Self::QueryId) -> QueryResponseStatus<Self::BlockNumber> {
fn take_response(_query_id: QueryId) -> QueryResponseStatus<Self::BlockNumber> {
QueryResponseStatus::NotFound
}
fn new_query(
_responder: impl Into<Location>,
_timeout: Self::BlockNumber,
_match_querier: impl Into<Location>,
) -> Self::QueryId {
) -> QueryId {
0u64
}

fn report_outcome(
_message: &mut Xcm<()>,
_responder: impl Into<Location>,
_timeout: Self::BlockNumber,
) -> Result<Self::QueryId, Self::Error> {
) -> Result<QueryId, Self::Error> {
Err(())
}

#[cfg(feature = "runtime-benchmarks")]
fn expect_response(_id: Self::QueryId, _response: crate::Response) {}
fn expect_response(_id: QueryId, _response: crate::Response) {}
}

0 comments on commit 3c7d6b0

Please sign in to comment.