mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 18:41:05 +00:00
Support xcm local execution in xcm-handler. (#357)
* Support xcm local execution in xcm handler. * Add docs.
This commit is contained in:
Generated
+1
@@ -1304,6 +1304,7 @@ dependencies = [
|
|||||||
"parity-scale-codec",
|
"parity-scale-codec",
|
||||||
"sp-std",
|
"sp-std",
|
||||||
"xcm",
|
"xcm",
|
||||||
|
"xcm-executor",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ frame-system = { git = "https://github.com/paritytech/substrate", default-featur
|
|||||||
|
|
||||||
# Polkadot Dependencies
|
# Polkadot Dependencies
|
||||||
xcm = { git = "https://github.com/paritytech/polkadot", default-features = false, branch = "master" }
|
xcm = { git = "https://github.com/paritytech/polkadot", default-features = false, branch = "master" }
|
||||||
|
xcm-executor = { git = "https://github.com/paritytech/polkadot", default-features = false, branch = "master" }
|
||||||
|
|
||||||
# Cumulus Dependencies
|
# Cumulus Dependencies
|
||||||
cumulus-primitives-core = { path = "../../primitives/core", default-features = false }
|
cumulus-primitives-core = { path = "../../primitives/core", default-features = false }
|
||||||
@@ -29,5 +30,6 @@ std = [
|
|||||||
"frame-support/std",
|
"frame-support/std",
|
||||||
"frame-system/std",
|
"frame-system/std",
|
||||||
"cumulus-primitives-core/std",
|
"cumulus-primitives-core/std",
|
||||||
"xcm/std"
|
"xcm/std",
|
||||||
|
"xcm-executor/std",
|
||||||
]
|
]
|
||||||
|
|||||||
@@ -27,12 +27,13 @@ use cumulus_primitives_core::{
|
|||||||
DownwardMessageHandler, HrmpMessageHandler, HrmpMessageSender, InboundDownwardMessage,
|
DownwardMessageHandler, HrmpMessageHandler, HrmpMessageSender, InboundDownwardMessage,
|
||||||
InboundHrmpMessage, OutboundHrmpMessage, ParaId, UpwardMessageSender,
|
InboundHrmpMessage, OutboundHrmpMessage, ParaId, UpwardMessageSender,
|
||||||
};
|
};
|
||||||
use frame_support::{decl_error, decl_event, decl_module, sp_runtime::traits::Hash, traits::EnsureOrigin};
|
use frame_support::{decl_error, decl_event, decl_module, dispatch::DispatchResult, sp_runtime::traits::Hash, traits::EnsureOrigin};
|
||||||
use sp_std::convert::{TryFrom, TryInto};
|
use sp_std::convert::{TryFrom, TryInto};
|
||||||
use xcm::{
|
use xcm::{
|
||||||
v0::{Error as XcmError, ExecuteXcm, Junction, MultiLocation, SendXcm, Xcm},
|
v0::{Error as XcmError, ExecuteXcm, Junction, MultiLocation, SendXcm, Xcm},
|
||||||
VersionedXcm,
|
VersionedXcm,
|
||||||
};
|
};
|
||||||
|
use xcm_executor::traits::LocationConversion;
|
||||||
|
|
||||||
pub trait Config: frame_system::Config {
|
pub trait Config: frame_system::Config {
|
||||||
type Event: From<Event<Self>> + Into<<Self as frame_system::Config>::Event>;
|
type Event: From<Event<Self>> + Into<<Self as frame_system::Config>::Event>;
|
||||||
@@ -45,6 +46,9 @@ pub trait Config: frame_system::Config {
|
|||||||
/// Required origin for sending XCM messages. Typically Root or parachain
|
/// Required origin for sending XCM messages. Typically Root or parachain
|
||||||
/// council majority.
|
/// council majority.
|
||||||
type SendXcmOrigin: EnsureOrigin<Self::Origin>;
|
type SendXcmOrigin: EnsureOrigin<Self::Origin>;
|
||||||
|
/// Utility for converting from the signed origin (of type `Self::AccountId`) into a sensible
|
||||||
|
/// `MultiLocation` ready for passing to the XCM interpreter.
|
||||||
|
type AccountIdConverter: LocationConversion<Self::AccountId>;
|
||||||
}
|
}
|
||||||
|
|
||||||
decl_event! {
|
decl_event! {
|
||||||
@@ -68,6 +72,8 @@ decl_error! {
|
|||||||
pub enum Error for Module<T: Config> {
|
pub enum Error for Module<T: Config> {
|
||||||
/// Failed to send XCM message.
|
/// Failed to send XCM message.
|
||||||
FailedToSend,
|
FailedToSend,
|
||||||
|
/// Bad XCM origin.
|
||||||
|
BadXcmOrigin,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,6 +107,21 @@ decl_module! {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T: Config> Module<T> {
|
||||||
|
/// Execute an XCM message locally. Returns `DispatchError` if failed.
|
||||||
|
pub fn execute_xcm(origin: T::AccountId, xcm: Xcm) -> DispatchResult {
|
||||||
|
let xcm_origin = T::AccountIdConverter::try_into_location(origin)
|
||||||
|
.map_err(|_| Error::<T>::BadXcmOrigin)?;
|
||||||
|
let hash = T::Hashing::hash(&xcm.encode());
|
||||||
|
let event = match T::XcmExecutor::execute_xcm(xcm_origin, xcm) {
|
||||||
|
Ok(_) => Event::<T>::Success(hash),
|
||||||
|
Err(e) => Event::<T>::Fail(hash, e),
|
||||||
|
};
|
||||||
|
Self::deposit_event(event);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: Config> DownwardMessageHandler for Module<T> {
|
impl<T: Config> DownwardMessageHandler for Module<T> {
|
||||||
fn handle_downward_message(msg: InboundDownwardMessage) {
|
fn handle_downward_message(msg: InboundDownwardMessage) {
|
||||||
let hash = msg.using_encoded(T::Hashing::hash);
|
let hash = msg.using_encoded(T::Hashing::hash);
|
||||||
|
|||||||
@@ -290,6 +290,7 @@ impl cumulus_pallet_xcm_handler::Config for Runtime {
|
|||||||
type UpwardMessageSender = ParachainSystem;
|
type UpwardMessageSender = ParachainSystem;
|
||||||
type HrmpMessageSender = ParachainSystem;
|
type HrmpMessageSender = ParachainSystem;
|
||||||
type SendXcmOrigin = EnsureRoot<AccountId>;
|
type SendXcmOrigin = EnsureRoot<AccountId>;
|
||||||
|
type AccountIdConverter = LocationConverter;
|
||||||
}
|
}
|
||||||
|
|
||||||
construct_runtime! {
|
construct_runtime! {
|
||||||
|
|||||||
Reference in New Issue
Block a user