Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
CallDispatcher trait
Browse files Browse the repository at this point in the history
  • Loading branch information
nanocryk committed Jun 10, 2022
1 parent ae53f58 commit b69cd77
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 6 deletions.
2 changes: 2 additions & 0 deletions runtime/kusama/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use xcm_builder::{
SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit,
UsingComponents, WeightInfoBounds,
};
use xcm_executor::traits::JustDispatch;

parameter_types! {
/// The location of the KSM token, from the context of this chain. Since this token is native to this
Expand Down Expand Up @@ -161,6 +162,7 @@ impl xcm_executor::Config for XcmConfig {
// No bridges yet...
type MessageExporter = ();
type UniversalAliases = Nothing;
type CallDispatcher = JustDispatch;
}

parameter_types! {
Expand Down
2 changes: 2 additions & 0 deletions runtime/polkadot/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use xcm_builder::{
IsConcrete, SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation,
TakeWeightCredit, UsingComponents,
};
use xcm_executor::traits::JustDispatch;

parameter_types! {
/// The location of the DOT token, from the context of this chain. Since this token is native to this
Expand Down Expand Up @@ -158,6 +159,7 @@ impl xcm_executor::Config for XcmConfig {
// No bridges yet...
type MessageExporter = ();
type UniversalAliases = Nothing;
type CallDispatcher = JustDispatch;
}

parameter_types! {
Expand Down
3 changes: 2 additions & 1 deletion runtime/rococo/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use xcm_builder::{
CurrencyAdapter as XcmCurrencyAdapter, FixedWeightBounds, IsConcrete,
SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, UsingComponents,
};
use xcm_executor::XcmExecutor;
use xcm_executor::{traits::JustDispatch, XcmExecutor};

parameter_types! {
pub const TokenLocation: MultiLocation = Here.into_location();
Expand Down Expand Up @@ -144,6 +144,7 @@ impl xcm_executor::Config for XcmConfig {
type FeeManager = ();
type MessageExporter = ();
type UniversalAliases = Nothing;
type CallDispatcher = JustDispatch;
}

parameter_types! {
Expand Down
3 changes: 2 additions & 1 deletion runtime/westend/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use xcm_builder::{
SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit,
UsingComponents, WeightInfoBounds,
};
use xcm_executor::XcmExecutor;
use xcm_executor::{traits::JustDispatch, XcmExecutor};

parameter_types! {
pub const TokenLocation: MultiLocation = Here.into_location();
Expand Down Expand Up @@ -123,6 +123,7 @@ impl xcm_executor::Config for XcmConfig {
type FeeManager = ();
type MessageExporter = ();
type UniversalAliases = Nothing;
type CallDispatcher = JustDispatch;
}

/// Type to convert an `Origin` type value into a `MultiLocation` value which represents an interior location
Expand Down
7 changes: 6 additions & 1 deletion xcm/xcm-executor/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.

use crate::traits::{
AssetExchange, AssetLock, ClaimAssets, ConvertOrigin, DropAssets, ExportXcm, FeeManager,
CallDispatcher, AssetExchange, AssetLock, ClaimAssets, ConvertOrigin, DropAssets, ExportXcm, FeeManager,
OnResponse, ShouldExecute, TransactAsset, VersionChangeNotifier, WeightBounds, WeightTrader,
};
use frame_support::{
Expand Down Expand Up @@ -94,4 +94,9 @@ pub trait Config {
/// The origin locations and specific universal junctions to which they are allowed to elevate
/// themselves.
type UniversalAliases: Contains<(MultiLocation, Junction)>;

/// How calls are dispatched from XCM.
/// Allows to modify the origin and/or the call based on the other.
/// To simply do `call.dispatch(origin)`, use `JustDispatch`.
type CallDispatcher: CallDispatcher<Self::Call>;
}
4 changes: 2 additions & 2 deletions xcm/xcm-executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use xcm::latest::prelude::*;

pub mod traits;
use traits::{
validate_export, AssetExchange, AssetLock, ClaimAssets, ConvertOrigin, DropAssets, Enact,
CallDispatcher, validate_export, AssetExchange, AssetLock, ClaimAssets, ConvertOrigin, DropAssets, Enact,
ExportXcm, FeeManager, FeeReason, OnResponse, ShouldExecute, TransactAsset,
VersionChangeNotifier, WeightBounds, WeightTrader,
};
Expand Down Expand Up @@ -512,7 +512,7 @@ impl<Config: config::Config> XcmExecutor<Config> {
.map_err(|_| XcmError::BadOrigin)?;
let weight = message_call.get_dispatch_info().weight;
ensure!(weight <= require_weight_at_most, XcmError::MaxWeightInvalid);
let maybe_actual_weight = match message_call.dispatch(dispatch_origin) {
let maybe_actual_weight = match Config::CallDispatcher::dispatch(message_call, dispatch_origin) {
Ok(post_info) => {
self.transact_status = MaybeErrorCode::Success;
post_info.actual_weight
Expand Down
22 changes: 22 additions & 0 deletions xcm/xcm-executor/src/traits/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.

use parity_scale_codec::{Decode, Encode};
use sp_runtime::{traits::Dispatchable, DispatchErrorWithPostInfo};
use sp_std::{borrow::Borrow, prelude::*, result::Result};
use xcm::latest::prelude::*;

Expand Down Expand Up @@ -203,3 +204,24 @@ impl<O> ConvertOrigin<O> for Tuple {
Err(origin)
}
}

/// Defines how a call is dispatched with given origin.
/// Allows to customize call dispatch, such as adapting the origin based on the call
/// or modifying the call.
pub trait CallDispatcher<Call: Dispatchable> {
fn dispatch(
call: Call,
origin: Call::Origin,
) -> Result<Call::PostInfo, DispatchErrorWithPostInfo<Call::PostInfo>>;
}

/// Just call `call.dispatch(origin)`.
pub struct JustDispatch;
impl<Call: Dispatchable> CallDispatcher<Call> for JustDispatch {
fn dispatch(
call: Call,
origin: Call::Origin,
) -> Result<Call::PostInfo, DispatchErrorWithPostInfo<Call::PostInfo>> {
call.dispatch(origin)
}
}
2 changes: 1 addition & 1 deletion xcm/xcm-executor/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//! Various traits used in configuring the executor.
mod conversion;
pub use conversion::{Convert, ConvertOrigin, Decoded, Encoded, Identity, JustTry};
pub use conversion::{CallDispatcher, JustDispatch, Convert, ConvertOrigin, Decoded, Encoded, Identity, JustTry};
mod drop_assets;
pub use drop_assets::{ClaimAssets, DropAssets};
mod asset_lock;
Expand Down

0 comments on commit b69cd77

Please sign in to comment.