Skip to content

Commit

Permalink
Refactor haul/dispatch xcm stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
bkontur committed Jan 23, 2023
1 parent 8e24c8c commit 1a1a4f5
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 25 deletions.
8 changes: 3 additions & 5 deletions pallets/parachain-system/src/validate_block/implementation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,16 @@ use cumulus_primitives_core::{
};
use cumulus_primitives_parachain_inherent::ParachainInherentData;

use polkadot_parachain::primitives::{
HeadData, RelayChainBlockNumber, ValidationParams, ValidationResult,
};
use polkadot_parachain::primitives::{HeadData, RelayChainBlockNumber, ValidationResult};

use codec::{Decode, Encode};
use codec::Encode;

use frame_support::traits::{ExecuteBlock, ExtrinsicCall, Get, IsSubType};
use sp_core::storage::{ChildInfo, StateVersion};
use sp_externalities::{set_and_run_with_externalities, Externalities};
use sp_io::KillStorageResult;
use sp_runtime::traits::{Block as BlockT, Extrinsic, HashFor, Header as HeaderT};
use sp_std::{mem, prelude::*};
use sp_std::prelude::*;
use sp_trie::MemoryDB;

type TrieBackend<B> = sp_state_machine::TrieBackend<MemoryDB<HashFor<B>>, HashFor<B>>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ use bp_runtime::{messages::MessageDispatchResult, AccountIdOf, Chain};
use codec::{Decode, Encode};
use frame_support::{dispatch::Weight, CloneNoBound, EqNoBound, PartialEqNoBound};
use scale_info::TypeInfo;
use xcm::latest::prelude::*;
use xcm_builder::{DispatchBlob, DispatchBlobError, HaulBlob, HaulBlobError};

/// PLain "XCM" payload, which we transfer through bridge
pub type XcmAsPlainPayload = sp_std::prelude::Vec<u8>;

/// Message dispatch result type for single message
#[derive(CloneNoBound, EqNoBound, PartialEqNoBound, Encode, Decode, Debug, TypeInfo)]
pub enum XcmBlobMessageDispatchResult {
InvalidPayload,
Expand Down Expand Up @@ -62,11 +62,6 @@ impl<SourceBridgeHubChain: Chain, TargetBridgeHubChain: Chain, BlobDispatcher: D
_relayer_account: &AccountIdOf<SourceBridgeHubChain>,
message: DispatchMessage<Self::DispatchPayload>,
) -> MessageDispatchResult<Self::DispatchLevelResult> {
log::warn!(
target: crate::LOG_TARGET,
"[XcmBlobMessageDispatch] DispatchBlob::dispatch_blob triggering - message_nonce: {:?}",
message.key.nonce
);
let payload = match message.data.payload {
Ok(payload) => payload,
Err(e) => {
Expand Down Expand Up @@ -125,29 +120,42 @@ impl<SourceBridgeHubChain: Chain, TargetBridgeHubChain: Chain, BlobDispatcher: D
/// where on the other it can be dispatched by [`XcmBlobMessageDispatch`].
pub trait XcmBlobHauler {
/// Runtime message sender adapter.
type MessageSender: MessagesBridge<super::RuntimeOrigin, XcmAsPlainPayload>;
type MessageSender: MessagesBridge<Self::MessageSenderOrigin, XcmAsPlainPayload>;

/// Runtime message sender origin, which is used by MessageSender.
type MessageSenderOrigin;
/// Our location within the Consensus Universe.
fn message_sender_origin() -> InteriorMultiLocation;
fn message_sender_origin() -> Self::MessageSenderOrigin;

/// Return message lane (as "point-to-point link") used to deliver XCM messages.
fn xcm_lane() -> LaneId;
}

pub struct XcmBlobHaulerAdapter<XcmBlobHauler>(sp_std::marker::PhantomData<XcmBlobHauler>);
impl<H: XcmBlobHauler> HaulBlob for XcmBlobHaulerAdapter<H> {
impl<HaulerOrigin, H: XcmBlobHauler<MessageSenderOrigin = HaulerOrigin>> HaulBlob
for XcmBlobHaulerAdapter<H>
{
fn haul_blob(blob: sp_std::prelude::Vec<u8>) -> Result<(), HaulBlobError> {
let lane = H::xcm_lane();
let result = H::MessageSender::send_message(
pallet_xcm::Origin::from(MultiLocation::from(H::message_sender_origin())).into(),
lane,
blob,
);
let result = H::MessageSender::send_message(H::message_sender_origin(), lane, blob);
let result = result.map(|artifacts| {
let hash = (lane, artifacts.nonce).using_encoded(sp_io::hashing::blake2_256);
hash
});
log::info!(target: crate::LOG_TARGET, "haul_blob result: {:?} on lane: {:?}", result, lane);
match &result {
Ok(result) => log::info!(
target: crate::LOG_TARGET,
"haul_blob result - ok: {:?} on lane: {:?}",
result,
lane
),
Err(error) => log::error!(
target: crate::LOG_TARGET,
"haul_blob result - error: {:?} on lane: {:?}",
error,
lane
),
};
result.map(|_| ()).map_err(|_| HaulBlobError::Transport("MessageSenderError"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,11 @@ impl XcmBlobHauler for ToBridgeHubWococoXcmBlobHauler {
type MessageSender =
pallet_bridge_messages::Pallet<Runtime, WithBridgeHubWococoMessagesInstance>;

fn message_sender_origin() -> InteriorMultiLocation {
crate::xcm_config::UniversalLocation::get()
type MessageSenderOrigin = super::RuntimeOrigin;

fn message_sender_origin() -> Self::MessageSenderOrigin {
pallet_xcm::Origin::from(MultiLocation::new(1, crate::xcm_config::UniversalLocation::get()))
.into()
}

fn xcm_lane() -> LaneId {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,11 @@ impl XcmBlobHauler for ToBridgeHubRococoXcmBlobHauler {
type MessageSender =
pallet_bridge_messages::Pallet<Runtime, WithBridgeHubRococoMessagesInstance>;

fn message_sender_origin() -> InteriorMultiLocation {
crate::xcm_config::UniversalLocation::get()
type MessageSenderOrigin = super::RuntimeOrigin;

fn message_sender_origin() -> super::RuntimeOrigin {
pallet_xcm::Origin::from(MultiLocation::new(1, crate::xcm_config::UniversalLocation::get()))
.into()
}

fn xcm_lane() -> LaneId {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ use polkadot_runtime_common::impls::ToAuthor;
parameter_types! {
pub const RelayLocation: MultiLocation = MultiLocation::parent();
pub RelayChainOrigin: RuntimeOrigin = cumulus_pallet_xcm::Origin::Relay.into();
pub UniversalLocation: InteriorMultiLocation =
pub UniversalLocation: InteriorMultiLocation =
X2(GlobalConsensus(RelayNetwork::get()), Parachain(ParachainInfo::parachain_id().into()));
pub const MaxInstructions: u32 = 100;
pub const MaxAssetsIntoHolding: u32 = 64;
Expand Down

0 comments on commit 1a1a4f5

Please sign in to comment.