mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-31 21:31:02 +00:00
Send messages using xcm pallet (#1518)
* send messages using xcm pallet * XcmBridge && XcmBridgeAdapter + (untested) config in RialtoParachain * impl encode_send_xcm for the rest * remove duplicate code * some fixes * cleanup * some more tests * cleanup * cleanup * send Rialto -> Millau messages using bridge-messages pallet * fmt * some clippy fixes * more clippy
This commit is contained in:
committed by
Bastian Köcher
parent
b21790b769
commit
c131a5e3c8
@@ -26,10 +26,13 @@
|
||||
#[cfg(feature = "std")]
|
||||
include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
|
||||
|
||||
use crate::millau_messages::{ToMillauMessagePayload, WithMillauMessageBridge};
|
||||
use crate::millau_messages::{
|
||||
ToMillauMessagePayload, WithMillauMessageBridge, DEFAULT_XCM_LANE_TO_MILLAU,
|
||||
};
|
||||
|
||||
use bridge_runtime_common::messages::{
|
||||
source::estimate_message_dispatch_and_delivery_fee, MessageBridge,
|
||||
source::{estimate_message_dispatch_and_delivery_fee, XcmBridge, XcmBridgeAdapter},
|
||||
MessageBridge,
|
||||
};
|
||||
use sp_api::impl_runtime_apis;
|
||||
use sp_core::{crypto::KeyTypeId, OpaqueMetadata};
|
||||
@@ -71,6 +74,7 @@ pub use bp_rialto_parachain::{
|
||||
|
||||
pub use pallet_bridge_grandpa::Call as BridgeGrandpaCall;
|
||||
pub use pallet_bridge_messages::Call as MessagesCall;
|
||||
pub use pallet_xcm::Call as XcmCall;
|
||||
|
||||
// Polkadot & XCM imports
|
||||
use pallet_xcm::XcmPassthrough;
|
||||
@@ -302,6 +306,10 @@ parameter_types! {
|
||||
pub const RelayNetwork: NetworkId = NetworkId::Polkadot;
|
||||
pub RelayOrigin: Origin = cumulus_pallet_xcm::Origin::Relay.into();
|
||||
pub UniversalLocation: InteriorMultiLocation = X1(Parachain(ParachainInfo::parachain_id().into()));
|
||||
/// The Millau network ID, associated with Kusama.
|
||||
pub const MillauNetwork: NetworkId = Kusama;
|
||||
/// The RialtoParachain network ID, associated with Westend.
|
||||
pub const ThisNetwork: NetworkId = Westend;
|
||||
}
|
||||
|
||||
/// Type for specifying how a `MultiLocation` can be converted into an `AccountId`. This is used
|
||||
@@ -417,12 +425,36 @@ pub type LocalOriginToLocation = SignedToAccountId32<Origin, AccountId, RelayNet
|
||||
/// The means for routing XCM messages which are not for local execution into the right message
|
||||
/// queues.
|
||||
pub type XcmRouter = (
|
||||
// Two routers - use UMP to communicate with the relay chain:
|
||||
cumulus_primitives_utility::ParentAsUmp<ParachainSystem, (), ()>,
|
||||
// ..and XCMP to communicate with the sibling chains.
|
||||
XcmpQueue,
|
||||
// Bridge is used to communicate with other relay chain (Millau).
|
||||
XcmBridgeAdapter<ToMillauBridge>,
|
||||
);
|
||||
|
||||
/// With-Millau bridge.
|
||||
pub struct ToMillauBridge;
|
||||
|
||||
impl XcmBridge for ToMillauBridge {
|
||||
type MessageBridge = WithMillauMessageBridge;
|
||||
type MessageSender = pallet_bridge_messages::Pallet<Runtime, WithMillauMessagesInstance>;
|
||||
|
||||
fn universal_location() -> InteriorMultiLocation {
|
||||
UniversalLocation::get()
|
||||
}
|
||||
|
||||
fn verify_destination(dest: &MultiLocation) -> bool {
|
||||
matches!(*dest, MultiLocation { parents: 1, interior: X1(GlobalConsensus(r)) } if r == MillauNetwork::get())
|
||||
}
|
||||
|
||||
fn build_destination() -> MultiLocation {
|
||||
let dest: InteriorMultiLocation = MillauNetwork::get().into();
|
||||
let here = UniversalLocation::get();
|
||||
dest.relative_to(&here)
|
||||
}
|
||||
|
||||
fn xcm_lane() -> bp_messages::LaneId {
|
||||
DEFAULT_XCM_LANE_TO_MILLAU
|
||||
}
|
||||
}
|
||||
|
||||
impl pallet_xcm::Config for Runtime {
|
||||
type Event = Event;
|
||||
type SendXcmOrigin = EnsureXcmOrigin<Origin, LocalOriginToLocation>;
|
||||
@@ -800,3 +832,72 @@ cumulus_pallet_parachain_system::register_validate_block!(
|
||||
BlockExecutor = cumulus_pallet_aura_ext::BlockExecutor::<Runtime, Executive>,
|
||||
CheckInherents = CheckInherents,
|
||||
);
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use bp_messages::{
|
||||
target_chain::{DispatchMessage, DispatchMessageData, MessageDispatch},
|
||||
MessageKey,
|
||||
};
|
||||
use bp_runtime::messages::MessageDispatchResult;
|
||||
use bridge_runtime_common::messages::target::FromBridgedChainMessageDispatch;
|
||||
use codec::Encode;
|
||||
|
||||
fn new_test_ext() -> sp_io::TestExternalities {
|
||||
sp_io::TestExternalities::new(
|
||||
frame_system::GenesisConfig::default().build_storage::<Runtime>().unwrap(),
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn xcm_messages_to_millau_are_sent() {
|
||||
new_test_ext().execute_with(|| {
|
||||
// the encoded message (origin ++ xcm) is 0x010109020419A8
|
||||
let dest = (Parent, X1(GlobalConsensus(MillauNetwork::get())));
|
||||
let xcm: Xcm<()> = vec![Instruction::Trap(42)].into();
|
||||
|
||||
let send_result = send_xcm::<XcmRouter>(dest.into(), xcm);
|
||||
let expected_fee = MultiAssets::from((Here, Fungibility::Fungible(4_345_002_552_u128)));
|
||||
let expected_hash =
|
||||
([0u8, 0u8, 0u8, 0u8], 1u64).using_encoded(sp_io::hashing::blake2_256);
|
||||
assert_eq!(send_result, Ok((expected_hash, expected_fee)),);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn xcm_messages_from_millau_are_dispatched() {
|
||||
type XcmExecutor = xcm_executor::XcmExecutor<XcmConfig>;
|
||||
type MessageDispatcher = FromBridgedChainMessageDispatch<
|
||||
WithMillauMessageBridge,
|
||||
XcmExecutor,
|
||||
XcmWeigher,
|
||||
frame_support::traits::ConstU64<BASE_XCM_WEIGHT>,
|
||||
>;
|
||||
|
||||
new_test_ext().execute_with(|| {
|
||||
let location: MultiLocation =
|
||||
(Parent, X1(GlobalConsensus(MillauNetwork::get()))).into();
|
||||
let xcm: Xcm<Call> = vec![Instruction::Trap(42)].into();
|
||||
|
||||
let mut incoming_message = DispatchMessage {
|
||||
key: MessageKey { lane_id: [0, 0, 0, 0], nonce: 1 },
|
||||
data: DispatchMessageData { payload: Ok((location, xcm).into()), fee: 0 },
|
||||
};
|
||||
|
||||
let dispatch_weight = MessageDispatcher::dispatch_weight(&mut incoming_message);
|
||||
assert_eq!(dispatch_weight, 1_000_000_000);
|
||||
|
||||
let dispatch_result =
|
||||
MessageDispatcher::dispatch(&AccountId::from([0u8; 32]), incoming_message);
|
||||
assert_eq!(
|
||||
dispatch_result,
|
||||
MessageDispatchResult {
|
||||
dispatch_result: true,
|
||||
unspent_weight: 0,
|
||||
dispatch_fee_paid_during_dispatch: false,
|
||||
}
|
||||
);
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user