From cc2a65369ea7acc74f3ae500179b2e0a5a82f55e Mon Sep 17 00:00:00 2001 From: Branislav Kontur Date: Tue, 25 Oct 2022 16:41:10 +0200 Subject: [PATCH] [BridgeHub] Init stuff for Haul/Dispatch xcm blobs --- .../bridge-hubs/bridge-hub-rococo/Cargo.toml | 2 + .../src/bridge_common_config.rs | 142 ++++++++++ .../bridge-hub-rococo/src/bridge_config.rs | 261 ------------------ .../src/bridge_hub_rococo_config.rs | 58 ++++ .../src/bridge_hub_wococo_config.rs | 59 ++++ .../bridge-hubs/bridge-hub-rococo/src/lib.rs | 124 ++++++--- .../bridge-hub-rococo/src/xcm_config.rs | 72 +++-- 7 files changed, 393 insertions(+), 325 deletions(-) create mode 100644 parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/bridge_common_config.rs delete mode 100644 parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/bridge_config.rs create mode 100644 parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/bridge_hub_rococo_config.rs create mode 100644 parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/bridge_hub_wococo_config.rs diff --git a/parachains/runtimes/bridge-hubs/bridge-hub-rococo/Cargo.toml b/parachains/runtimes/bridge-hubs/bridge-hub-rococo/Cargo.toml index 1c7ab7a254..000db6e66b 100644 --- a/parachains/runtimes/bridge-hubs/bridge-hub-rococo/Cargo.toml +++ b/parachains/runtimes/bridge-hubs/bridge-hub-rococo/Cargo.toml @@ -80,6 +80,7 @@ pallet-bridge-messages = { path = "../../../../bridges/modules/messages", defaul pallet-bridge-parachains = { path = "../../../../bridges/modules/parachains", default-features = false } pallet-bridge-relayers = { path = "../../../../bridges/modules/relayers", default-features = false } pallet-shift-session-manager = { path = "../../../../bridges/modules/shift-session-manager", default-features = false } +bridge-runtime-common = { path = "../../../../bridges/bin/runtime-common", default-features = false } [features] default = [ @@ -93,6 +94,7 @@ std = [ "bp-runtime/std", "bp-rococo/std", "bp-wococo/std", + "bridge-runtime-common/std", "codec/std", "log/std", "scale-info/std", diff --git a/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/bridge_common_config.rs b/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/bridge_common_config.rs new file mode 100644 index 0000000000..608193fd14 --- /dev/null +++ b/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/bridge_common_config.rs @@ -0,0 +1,142 @@ +// Copyright 2022 Parity Technologies (UK) Ltd. +// This file is part of Cumulus. + +// Cumulus is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Cumulus is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Cumulus. If not, see . + +use crate::universal_exports::HaulBlob; +use bp_messages::{ + source_chain::MessagesBridge, + target_chain::{DispatchMessage, MessageDispatch}, + LaneId, +}; +use bp_runtime::{messages::MessageDispatchResult, AccountIdOf, BalanceOf, Chain}; +use codec::Encode; +use frame_support::{dispatch::Weight, parameter_types}; +use xcm::latest::prelude::*; + +// TODO:check-parameter - we could possibly use BridgeMessage from xcm:v3 stuff +/// PLain "XCM" payload, which we transfer through bridge +pub type XcmAsPlainPayload = sp_std::prelude::Vec; + +// TODO:check-parameter +parameter_types! { + pub const MaxMessagesToPruneAtOnce: bp_messages::MessageNonce = 8; + pub const MaxRequests: u32 = 64; + pub const HeadersToKeep: u32 = 1024; +} + +/// [`XcmBlobMessageDispatch`] is responsible for dispatching received messages from other BridgeHub +pub struct XcmBlobMessageDispatch { + _marker: + sp_std::marker::PhantomData<(SourceBridgeHubChain, TargetBridgeHubChain, DispatchBlob)>, +} + +impl< + SourceBridgeHubChain: Chain, + TargetBridgeHubChain: Chain, + DispatchBlob: crate::universal_exports::DispatchBlob, + > MessageDispatch, BalanceOf> + for XcmBlobMessageDispatch +{ + type DispatchPayload = XcmAsPlainPayload; + + fn dispatch_weight( + message: &mut DispatchMessage>, + ) -> Weight { + log::error!( + "[XcmBlobMessageDispatch] TODO: change here to XCMv3 dispatch_weight with XcmExecutor" + ); + 0 + } + + fn dispatch( + _relayer_account: &AccountIdOf, + message: DispatchMessage>, + ) -> MessageDispatchResult { + log::warn!("[XcmBlobMessageDispatch] DispatchBlob::dispatch_blob triggering"); + let payload = match message.data.payload { + Ok(payload) => payload, + Err(e) => { + log::error!("[XcmBlobMessageDispatch] payload error: {:?}", e); + return MessageDispatchResult { + dispatch_result: false, + unspent_weight: 0, + dispatch_fee_paid_during_dispatch: false, + } + }, + }; + let dispatch_result = match DispatchBlob::dispatch_blob(payload) { + Ok(_) => true, + Err(e) => { + log::error!( + "[XcmBlobMessageDispatch] DispatchBlob::dispatch_blob failed, error: {:?}", + e + ); + false + }, + }; + MessageDispatchResult { + dispatch_result, + dispatch_fee_paid_during_dispatch: false, + unspent_weight: 0, + } + } +} + +/// [`XcmBlobHauler`] is responsible for sending messages to the bridge "point-to-point link" from one side, +/// where on the other it can be dispatched by [`XcmBlobMessageDispatch`]. +pub trait XcmBlobHauler { + /// Which chain is sending + type SenderChain: Chain; + + /// Runtime message sender adapter. + type MessageSender: MessagesBridge< + super::Origin, + AccountIdOf, + BalanceOf, + XcmAsPlainPayload, + >; + + /// Our location within the Consensus Universe. + fn message_sender_origin() -> InteriorMultiLocation; + + /// Return message lane (as "point-to-point link") used to deliver XCM messages. + fn xcm_lane() -> LaneId; +} + +impl HaulBlob for T { + fn haul_blob(blob: sp_std::prelude::Vec) { + let lane = T::xcm_lane(); + // TODO:check-parameter - fee could be taken from BridgeMessage - or add as optional fo send_message + // TODO:check-parameter - or add here something like PriceForSiblingDelivery + let fee = ::Balance::from(0u8); + + let result = T::MessageSender::send_message( + pallet_xcm::Origin::from(MultiLocation::from(T::message_sender_origin())).into(), + lane, + blob, + fee, + ); + let result = result + .map(|artifacts| { + let hash = (lane, artifacts.nonce).using_encoded(sp_io::hashing::blake2_256); + hash + }) + .map_err(|e| { + e + }); + log::info!(target: "runtime::bridge-hub", "haul_blob result: {:?} on lane: {:?}", result, lane); + result.expect("failed to process: TODO:check-parameter - wait for origin/gav-xcm-v3, there is a comment about handliing errors for HaulBlob"); + } +} diff --git a/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/bridge_config.rs b/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/bridge_config.rs deleted file mode 100644 index 36a9658cf2..0000000000 --- a/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/bridge_config.rs +++ /dev/null @@ -1,261 +0,0 @@ -// Copyright 2022 Parity Technologies (UK) Ltd. -// This file is part of Cumulus. - -// Cumulus is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// Cumulus is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with Cumulus. If not, see . - -use bp_messages::{ - source_chain::{LaneMessageVerifier, TargetHeaderChain}, - target_chain::{DispatchMessage, MessageDispatch, ProvedMessages, SourceHeaderChain}, - InboundLaneData, LaneId, Message, OutboundLaneData, -}; -use bp_polkadot_core::Balance; -use bp_runtime::{messages::MessageDispatchResult, AccountIdOf, BalanceOf, Chain}; -use codec::Decode; -use frame_support::{dispatch::Weight, parameter_types, RuntimeDebug}; - -parameter_types! { - // TODO:check-parameter - pub const BridgeHubRococoMaxMessagesToPruneAtOnce: bp_messages::MessageNonce = 8; - pub const BridgeHubWococoMaxMessagesToPruneAtOnce: bp_messages::MessageNonce = 8; - // TODO:check-parameter - pub const BridgeHubRococoMaxUnrewardedRelayerEntriesAtInboundLane: bp_messages::MessageNonce = - bp_bridge_hub_rococo::MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX; - pub const BridgeHubWococoMaxUnrewardedRelayerEntriesAtInboundLane: bp_messages::MessageNonce = - bp_bridge_hub_wococo::MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX; - // TODO:check-parameter - pub const BridgeHubRococoMaxUnconfirmedMessagesAtInboundLane: bp_messages::MessageNonce = - bp_bridge_hub_rococo::MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX; - pub const BridgeHubWococoMaxUnconfirmedMessagesAtInboundLane: bp_messages::MessageNonce = - bp_bridge_hub_wococo::MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX; - - pub const BridgeHubRococoChainId: bp_runtime::ChainId = bp_runtime::BRIDGE_HUB_ROCOCO_CHAIN_ID; - pub const BridgeHubWococoChainId: bp_runtime::ChainId = bp_runtime::BRIDGE_HUB_WOCOCO_CHAIN_ID; -} - -// TODO:check-parameter - when integration XCMv3 change this to struct -pub type PlainXcmPayload = sp_std::prelude::Vec; - -// TODO:check-parameter - when integration XCMv3 change this to struct -pub type FromBridgeHubRococoMessagePayload = PlainXcmPayload; -pub type FromBridgeHubWococoMessagePayload = PlainXcmPayload; -pub type ToBridgeHubRococoMessagePayload = PlainXcmPayload; -pub type ToBridgeHubWococoMessagePayload = PlainXcmPayload; - -// TODO:check-parameter - when integrating XCMv3 change this to FromBridgedChainMessagePayload -pub struct FromBridgeHubRococoMessageDispatch { - _marker: sp_std::marker::PhantomData<(SourceBridgeHubChain, TargetBridgeHubChain)>, -} -pub struct FromBridgeHubWococoMessageDispatch { - _marker: sp_std::marker::PhantomData<(SourceBridgeHubChain, TargetBridgeHubChain)>, -} - -impl - MessageDispatch, BalanceOf> - for FromBridgeHubRococoMessageDispatch -{ - type DispatchPayload = FromBridgeHubRococoMessagePayload; - - fn dispatch_weight( - message: &mut DispatchMessage>, - ) -> Weight { - log::error!("[FromBridgeHubRococoMessageDispatch] TODO: change here to XCMv3 dispatch_weight with XcmExecutor"); - 0 - } - - fn dispatch( - relayer_account: &AccountIdOf, - message: DispatchMessage>, - ) -> MessageDispatchResult { - log::error!("[FromBridgeHubRococoMessageDispatch] TODO: change here to XCMv3 dispatch with XcmExecutor"); - todo!("TODO: implement XCMv3 dispatch") - } -} - -impl - MessageDispatch, BalanceOf> - for FromBridgeHubWococoMessageDispatch -{ - type DispatchPayload = FromBridgeHubWococoMessagePayload; - - fn dispatch_weight( - message: &mut DispatchMessage>, - ) -> Weight { - log::error!("[FromBridgeHubWococoMessageDispatch] TODO: change here to XCMv3 dispatch_weight with XcmExecutor"); - 0 - } - - fn dispatch( - relayer_account: &AccountIdOf, - message: DispatchMessage>, - ) -> MessageDispatchResult { - log::error!("[FromBridgeHubWococoMessageDispatch] TODO: change here to XCMv3 dispatch with XcmExecutor"); - todo!("TODO: implement XCMv3 dispatch") - } -} - -pub struct ToBridgeHubRococoMessageVerifier { - _marker: sp_std::marker::PhantomData<(Origin, Sender)>, -} -pub struct ToBridgeHubWococoMessageVerifier { - _marker: sp_std::marker::PhantomData<(Origin, Sender)>, -} - -impl - LaneMessageVerifier< - Origin, - ToBridgeHubRococoMessagePayload, - BalanceOf, - > for ToBridgeHubRococoMessageVerifier -{ - type Error = &'static str; - - fn verify_message( - submitter: &Origin, - delivery_and_dispatch_fee: &BalanceOf, - lane: &LaneId, - outbound_data: &OutboundLaneData, - payload: &ToBridgeHubRococoMessagePayload, - ) -> Result<(), Self::Error> { - todo!("TODO: ToBridgeHubRococoMessageVerifier - fix verify_message - at the begining to allow all") - } -} - -impl - LaneMessageVerifier< - Origin, - ToBridgeHubWococoMessagePayload, - BalanceOf, - > for ToBridgeHubWococoMessageVerifier -{ - type Error = &'static str; - - fn verify_message( - submitter: &Origin, - delivery_and_dispatch_fee: &BalanceOf, - lane: &LaneId, - outbound_data: &OutboundLaneData, - payload: &ToBridgeHubWococoMessagePayload, - ) -> Result<(), Self::Error> { - todo!("TODO: ToBridgeHubWococoMessageVerifier - fix verify_message - at the begining to allow all") - } -} - -/// BridgeHubRococo chain from message lane point of view. -#[derive(RuntimeDebug, Clone, Copy)] -pub struct BridgeHubRococoMessagingSupport; -/// BridgeHubWococo chain from message lane point of view. -#[derive(RuntimeDebug, Clone, Copy)] -pub struct BridgeHubWococoMessagingSupport; - -impl SourceHeaderChain - for BridgeHubRococoMessagingSupport -{ - type Error = &'static str; - type MessagesProof = (); - - fn verify_messages_proof( - proof: Self::MessagesProof, - messages_count: u32, - ) -> Result>, Self::Error> { - // TODO: need to add, bridges-runtime-common and refactor out of bin - // messages::target::verify_messages_proof_from_parachain::< - // WithRialtoParachainMessageBridge, - // bp_bridge_hub_rococo::Header, - // crate::Runtime, - // crate::WithRialtoParachainsInstance, - // >(ParaId(bp_rialto_parachain::RIALTO_PARACHAIN_ID), proof, messages_count) - todo!("TODO: fix and implement SourceHeaderChain::verify_messages_proof") - } -} - -impl - TargetHeaderChain< - ToBridgeHubRococoMessagePayload, - crate::AccountId, /* bp_bridge_hub_wococo::AccountId */ - > for BridgeHubRococoMessagingSupport -{ - type Error = &'static str; - type MessagesDeliveryProof = (); - - fn verify_message(payload: &ToBridgeHubRococoMessagePayload) -> Result<(), Self::Error> { - // messages::source::verify_chain_message::(payload) - todo!("TODO: fix implementation: TargetHeaderChain::verify_message") - } - - fn verify_messages_delivery_proof( - proof: Self::MessagesDeliveryProof, - ) -> Result< - (LaneId, InboundLaneData), - Self::Error, - > { - // messages::source::verify_messages_delivery_proof_from_parachain::< - // WithRialtoParachainMessageBridge, - // bp_rialto_parachain::Header, - // Runtime, - // crate::WithRialtoParachainsInstance, - // >(ParaId(bp_rialto_parachain::RIALTO_PARACHAIN_ID), proof) - todo!("TODO: fix implementation: TargetHeaderChain::verify_messages_delivery_proof") - } -} - -impl SourceHeaderChain - for BridgeHubWococoMessagingSupport -{ - type Error = &'static str; - type MessagesProof = (); - - fn verify_messages_proof( - proof: Self::MessagesProof, - messages_count: u32, - ) -> Result>, Self::Error> { - // TODO: need to add, bridges-runtime-common and refactor out of bin - // messages::target::verify_messages_proof_from_parachain::< - // WithMIllauParachainMessageBridge, - // bp_bridge_hub_wococo::Header, - // crate::Runtime, - // crate::WithRialtoParachainsInstance, - // >(ParaId(bp_rialto_parachain::RIALTO_PARACHAIN_ID), proof, messages_count) - todo!("TODO: fix and implement SourceHeaderChain::verify_messages_proof") - } -} - -impl - TargetHeaderChain< - ToBridgeHubWococoMessagePayload, - crate::AccountId, /* bp_bridge_hub_rococo::AccountId */ - > for BridgeHubWococoMessagingSupport -{ - type Error = &'static str; - type MessagesDeliveryProof = (); - - fn verify_message(payload: &ToBridgeHubWococoMessagePayload) -> Result<(), Self::Error> { - // messages::source::verify_chain_message::(payload) - todo!("TODO: fix implementation: TargetHeaderChain::verify_message") - } - - fn verify_messages_delivery_proof( - proof: Self::MessagesDeliveryProof, - ) -> Result< - (LaneId, InboundLaneData), - Self::Error, - > { - // messages::source::verify_messages_delivery_proof_from_parachain::< - // WithRialtoParachainMessageBridge, - // bp_rialto_parachain::Header, - // Runtime, - // crate::WithRialtoParachainsInstance, - // >(ParaId(bp_rialto_parachain::RIALTO_PARACHAIN_ID), proof) - todo!("TODO: fix implementation: TargetHeaderChain::verify_messages_delivery_proof") - } -} diff --git a/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/bridge_hub_rococo_config.rs b/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/bridge_hub_rococo_config.rs new file mode 100644 index 0000000000..a44e9540cc --- /dev/null +++ b/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/bridge_hub_rococo_config.rs @@ -0,0 +1,58 @@ +// Copyright 2022 Parity Technologies (UK) Ltd. +// This file is part of Cumulus. + +// Cumulus is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Cumulus is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Cumulus. If not, see . + +use bp_messages::{source_chain::{LaneMessageVerifier, TargetHeaderChain}, target_chain::{ProvedMessages, SourceHeaderChain}, InboundLaneData, LaneId, Message, OutboundLaneData, MessageKey, MessageData}; +use bp_runtime::{BalanceOf, Chain}; +use frame_support::{parameter_types, RuntimeDebug}; +use xcm::prelude::{InteriorMultiLocation, NetworkId}; +use bp_messages::target_chain::ProvedLaneMessages; +use bridge_runtime_common::messages::target::FromBridgedChainMessagesProof; +use crate::universal_exports::{BridgeBlobDispatcher, HaulBlobExporter}; +use crate::{WithBridgeHubWococoMessagesInstance, XcmAsPlainPayload, XcmBlobHauler, XcmRouter}; +use crate::Runtime; +use crate::ParachainInfo; +use xcm::latest::prelude::*; + +// TODO:check-parameter +parameter_types! { + pub const MaxUnrewardedRelayerEntriesAtInboundLane: bp_messages::MessageNonce = + bp_bridge_hub_rococo::MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX; + pub const MaxUnconfirmedMessagesAtInboundLane: bp_messages::MessageNonce = + bp_bridge_hub_rococo::MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX; + pub const BridgeHubWococoChainId: bp_runtime::ChainId = bp_runtime::BRIDGE_HUB_WOCOCO_CHAIN_ID; + pub BridgeHubRococoUniversalLocation: InteriorMultiLocation = X2(GlobalConsensus(Rococo), Parachain(ParachainInfo::parachain_id().into())); + pub WococoGlobalConsensusNetwork: NetworkId = NetworkId::Wococo; +} + +/// Dispatches received XCM messages from other bridge +pub type OnBridgeHubRococoBlobDispatcher = BridgeBlobDispatcher; + +/// Export XCM messages to be relayed to the otherside +pub type ToBridgeHubWococoHaulBlobExporter = HaulBlobExporter; +pub struct ToBridgeHubWococoXcmBlobHauler; +pub const DEFAULT_XCM_LANE_TO_BRIDGE_HUB_WOCOCO: LaneId = [0, 0, 0, 1]; +impl XcmBlobHauler for ToBridgeHubWococoXcmBlobHauler { + type SenderChain = bp_bridge_hub_rococo::BridgeHubRococo; + type MessageSender = pallet_bridge_messages::Pallet; + + fn message_sender_origin() -> InteriorMultiLocation { + crate::xcm_config::UniversalLocation::get() + } + + fn xcm_lane() -> LaneId { + DEFAULT_XCM_LANE_TO_BRIDGE_HUB_WOCOCO + } +} diff --git a/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/bridge_hub_wococo_config.rs b/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/bridge_hub_wococo_config.rs new file mode 100644 index 0000000000..2a521405db --- /dev/null +++ b/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/bridge_hub_wococo_config.rs @@ -0,0 +1,59 @@ +// Copyright 2022 Parity Technologies (UK) Ltd. +// This file is part of Cumulus. + +// Cumulus is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Cumulus is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Cumulus. If not, see . + +use bp_messages::{source_chain::{LaneMessageVerifier, TargetHeaderChain}, target_chain::{ProvedMessages, SourceHeaderChain}, InboundLaneData, LaneId, Message, OutboundLaneData, MessageKey, MessageData}; +use bp_runtime::{BalanceOf, Chain}; +use frame_support::{parameter_types, RuntimeDebug}; +use xcm::prelude::{InteriorMultiLocation, NetworkId}; +use bp_messages::target_chain::ProvedLaneMessages; +use bridge_runtime_common::messages::target::FromBridgedChainMessagesProof; +use crate::universal_exports::{BridgeBlobDispatcher, HaulBlobExporter}; +use crate::{WithBridgeHubRococoMessagesInstance, XcmAsPlainPayload, XcmBlobHauler, XcmRouter}; +use crate::Runtime; +use crate::ParachainInfo; +use xcm::latest::prelude::*; + +// TODO:check-parameter +parameter_types! { + pub const MaxUnrewardedRelayerEntriesAtInboundLane: bp_messages::MessageNonce = + bp_bridge_hub_wococo::MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX; + pub const MaxUnconfirmedMessagesAtInboundLane: bp_messages::MessageNonce = + bp_bridge_hub_wococo::MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX; + pub const BridgeHubRococoChainId: bp_runtime::ChainId = bp_runtime::BRIDGE_HUB_ROCOCO_CHAIN_ID; + pub BridgeHubWococoUniversalLocation: InteriorMultiLocation = X2(GlobalConsensus(Wococo), Parachain(ParachainInfo::parachain_id().into())); + pub RococoGlobalConsensusNetwork: NetworkId = NetworkId::Rococo; +} + +/// Dispatches received XCM messages from other bridge +pub type OnBridgeHubWococoBlobDispatcher = BridgeBlobDispatcher; + +/// Export XCM messages to be relayed to the otherside +pub type ToBridgeHubRococoHaulBlobExporter = HaulBlobExporter; +pub struct ToBridgeHubRococoXcmBlobHauler; +pub const DEFAULT_XCM_LANE_TO_BRIDGE_HUB_ROCOCO: LaneId = [0, 0, 0, 1]; +impl XcmBlobHauler for ToBridgeHubRococoXcmBlobHauler { + type SenderChain = bp_bridge_hub_wococo::BridgeHubWococo; + type MessageSender = pallet_bridge_messages::Pallet; + + fn message_sender_origin() -> InteriorMultiLocation { + crate::xcm_config::UniversalLocation::get() + } + + fn xcm_lane() -> LaneId { + DEFAULT_XCM_LANE_TO_BRIDGE_HUB_ROCOCO + } +} + diff --git a/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs b/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs index 9f4271d139..97a5d88ec2 100644 --- a/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs +++ b/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/lib.rs @@ -22,10 +22,13 @@ #[cfg(feature = "std")] include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); +pub mod bridge_common_config; +pub mod bridge_hub_rococo_config; +pub mod bridge_hub_wococo_config; mod weights; pub mod xcm_config; -mod bridge_config; +use bridge_common_config::*; use cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases; use smallvec::smallvec; use sp_api::impl_runtime_apis; @@ -59,18 +62,20 @@ pub use sp_consensus_aura::sr25519::AuthorityId as AuraId; pub use sp_runtime::{MultiAddress, Perbill, Permill}; use xcm_config::{XcmConfig, XcmOriginToTransactDispatchOrigin}; -use bp_polkadot_core::parachains::ParaId; use bp_runtime::{HeaderId, HeaderIdProvider}; #[cfg(any(feature = "std", test))] pub use sp_runtime::BuildStorage; -// Polkadot imports use polkadot_runtime_common::{BlockHashCount, SlowAdjustingFeeUpdate}; +use sp_runtime::traits::ConstU32; use weights::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight}; -// XCM Imports +use crate::{ + bridge_hub_rococo_config::OnBridgeHubRococoBlobDispatcher, + bridge_hub_wococo_config::OnBridgeHubWococoBlobDispatcher, xcm_config::XcmRouter, +}; use parachains_common::{AccountId, Signature}; use xcm::latest::prelude::BodyId; use xcm_executor::XcmExecutor; @@ -238,11 +243,18 @@ pub fn native_version() -> NativeVersion { } // TODO:check-parameter - move to bridges/primitives, once rebased and would compile with bp_bridge_hub_xyz dependencies -mod runtime_api { - use super::BlockNumber; - use super::Hash; +pub mod runtime_api { + use super::{BlockNumber, Hash}; bp_runtime::decl_bridge_finality_runtime_apis!(rococo); bp_runtime::decl_bridge_finality_runtime_apis!(wococo); + + use bp_messages::{ + InboundMessageDetails, LaneId, MessageNonce, MessagePayload, OutboundMessageDetails, + }; + use frame_support::{sp_runtime::FixedU128, Parameter}; + use sp_std::prelude::Vec; + bp_runtime::decl_bridge_messages_runtime_apis!(bridge_hub_rococo); + bp_runtime::decl_bridge_messages_runtime_apis!(bridge_hub_wococo); } parameter_types! { @@ -412,11 +424,12 @@ impl cumulus_pallet_xcmp_queue::Config for Runtime { type Event = Event; type XcmExecutor = XcmExecutor; type ChannelInfo = ParachainSystem; - type VersionWrapper = (); + type VersionWrapper = PolkadotXcm; type ExecuteOverweightOrigin = EnsureRoot; type ControllerOrigin = EnsureRoot; type ControllerOriginConverter = XcmOriginToTransactDispatchOrigin; - type WeightInfo = (); + type PriceForSiblingDelivery = (); + type WeightInfo = weights::cumulus_pallet_xcmp_queue::WeightInfo; } impl cumulus_pallet_dmp_queue::Config for Runtime { @@ -485,12 +498,6 @@ impl pallet_sudo::Config for Runtime { } // Add bridge pallets (GPA) -parameter_types! { - // TODO:check-parameter - pub const MaxRequests: u32 = 64; - // TODO:check-parameter - pub const HeadersToKeep: u32 = 1024; -} /// Add granda bridge pallet to track Wococo relay chain on Rococo BridgeHub pub type BridgeGrandpaWococoInstance = pallet_bridge_grandpa::Instance1; @@ -523,9 +530,9 @@ parameter_types! { /// Add parachain bridge pallet to track Wococo bridge hub parachain pub type BridgeParachainWococoInstance = pallet_bridge_parachains::Instance1; impl pallet_bridge_parachains::Config for Runtime { + type Event = Event; // TODO:check-parameter type WeightInfo = (); - type Event = Event; type BridgesGrandpaPalletInstance = BridgeGrandpaWococoInstance; type ParasPalletName = WococoBridgeParachainPalletName; type TrackedParachains = Everything; @@ -544,24 +551,25 @@ impl pallet_bridge_parachains::Config for Runtime type HeadsToKeep = ParachainHeadsToKeep; } -/// Add XCM messages support for BrigdeHubRococo to support Wococo->Rococo XCM messages +/// Add XCM messages support for BrigdeHubRococo to support Rococo->Wococo XCM messages pub type WithBridgeHubWococoMessagesInstance = pallet_bridge_messages::Instance1; impl pallet_bridge_messages::Config for Runtime { type Event = Event; // TODO:check-parameter - copy of MillauWeigth + refactor type WeightInfo = (); - type BridgedChainId = bridge_config::BridgeHubWococoChainId; - // TODO:check-parameter - do we need any conversion rate or what ever? + type BridgedChainId = bridge_hub_rococo_config::BridgeHubWococoChainId; type Parameter = (); - type MaxMessagesToPruneAtOnce = bridge_config::BridgeHubWococoMaxMessagesToPruneAtOnce; - type MaxUnrewardedRelayerEntriesAtInboundLane = bridge_config::BridgeHubWococoMaxUnrewardedRelayerEntriesAtInboundLane; - type MaxUnconfirmedMessagesAtInboundLane = bridge_config::BridgeHubWococoMaxUnconfirmedMessagesAtInboundLane; + type MaxMessagesToPruneAtOnce = MaxMessagesToPruneAtOnce; + type MaxUnrewardedRelayerEntriesAtInboundLane = + bridge_hub_rococo_config::MaxUnrewardedRelayerEntriesAtInboundLane; + type MaxUnconfirmedMessagesAtInboundLane = + bridge_hub_rococo_config::MaxUnconfirmedMessagesAtInboundLane; type MaximalOutboundPayloadSize = (); - type OutboundPayload = bridge_config::ToBridgeHubWococoMessagePayload; - type OutboundMessageFee = crate::Balance; /* bp_bridge_hub_rococo::Balance */ + type OutboundPayload = XcmAsPlainPayload; + type OutboundMessageFee = crate::Balance; /* bp_bridge_hub_rococo::Balance */ - type InboundPayload = bridge_config::FromBridgeHubWococoMessagePayload; + type InboundPayload = XcmAsPlainPayload; type InboundMessageFee = crate::Balance; /* bp_bridge_hub_wococo::Balance */ type InboundRelayer = crate::AccountId; /* bp_bridge_hub_wococo::AccountId */ @@ -573,27 +581,32 @@ impl pallet_bridge_messages::Config for Run type OnDeliveryConfirmed = (); type SourceHeaderChain = bridge_config::BridgeHubWococoMessagingSupport; - type MessageDispatch = bridge_config::FromBridgeHubWococoMessageDispatch; + type MessageDispatch = XcmBlobMessageDispatch< + bp_bridge_hub_wococo::BridgeHubWococo, + bp_bridge_hub_rococo::BridgeHubRococo, + OnBridgeHubRococoBlobDispatcher, + >; } -/// Add XCM messages support for BrigdeHubWococo to support Rococo->Wococo XCM messages +/// Add XCM messages support for BrigdeHubWococo to support Wococo->Rococo XCM messages pub type WithBridgeHubRococoMessagesInstance = pallet_bridge_messages::Instance2; impl pallet_bridge_messages::Config for Runtime { type Event = Event; // TODO:check-parameter - copy of MillauWeigth + refactor type WeightInfo = (); - type BridgedChainId = bridge_config::BridgeHubRococoChainId; - // TODO:check-parameter - do we need any conversion rate or what ever? + type BridgedChainId = bridge_hub_wococo_config::BridgeHubRococoChainId; type Parameter = (); - type MaxMessagesToPruneAtOnce = bridge_config::BridgeHubRococoMaxMessagesToPruneAtOnce; - type MaxUnrewardedRelayerEntriesAtInboundLane = bridge_config::BridgeHubRococoMaxUnrewardedRelayerEntriesAtInboundLane; - type MaxUnconfirmedMessagesAtInboundLane = bridge_config::BridgeHubRococoMaxUnconfirmedMessagesAtInboundLane; + type MaxMessagesToPruneAtOnce = MaxMessagesToPruneAtOnce; + type MaxUnrewardedRelayerEntriesAtInboundLane = + bridge_hub_wococo_config::MaxUnrewardedRelayerEntriesAtInboundLane; + type MaxUnconfirmedMessagesAtInboundLane = + bridge_hub_wococo_config::MaxUnconfirmedMessagesAtInboundLane; type MaximalOutboundPayloadSize = (); - type OutboundPayload = bridge_config::ToBridgeHubRococoMessagePayload; - type OutboundMessageFee = crate::Balance; /* bp_bridge_hub_wococo::Balance */ + type OutboundPayload = XcmAsPlainPayload; + type OutboundMessageFee = crate::Balance; /* bp_bridge_hub_wococo::Balance */ - type InboundPayload = bridge_config::FromBridgeHubRococoMessagePayload; + type InboundPayload = XcmAsPlainPayload; type InboundMessageFee = crate::Balance; /* bp_bridge_hub_rococo::Balance */ type InboundRelayer = crate::AccountId; /* bp_bridge_hub_rococo::AccountId */ @@ -605,7 +618,11 @@ impl pallet_bridge_messages::Config for Run type OnDeliveryConfirmed = (); type SourceHeaderChain = bridge_config::BridgeHubRococoMessagingSupport; - type MessageDispatch = bridge_config::FromBridgeHubRococoMessageDispatch; + type MessageDispatch = XcmBlobMessageDispatch< + bp_bridge_hub_rococo::BridgeHubRococo, + bp_bridge_hub_wococo::BridgeHubWococo, + OnBridgeHubWococoBlobDispatcher, + >; } /// Add shift session manager @@ -796,6 +813,41 @@ impl_runtime_apis! { } } + // This exposed by BridgeHubRococo + impl runtime_api::ToBridgeHubWococoOutboundLaneApi for Runtime { + fn estimate_message_delivery_and_dispatch_fee( + _lane_id: bp_messages::LaneId, + payload: XcmAsPlainPayload, + _conversion_rate: Option, + ) -> Option { + None + } + + fn message_details( + lane: bp_messages::LaneId, + begin: bp_messages::MessageNonce, + end: bp_messages::MessageNonce, + ) -> Vec> { + bridge_runtime_common::messages_api::outbound_message_details::< + Runtime, + WithBridgeHubWococoMessagesInstance, + >(lane, begin, end) + } + } + + // This is exposed by BridgeHubWococo + impl runtime_api::FromBridgeHubRococoInboundLaneApi for Runtime { + fn message_details( + lane: bp_messages::LaneId, + messages: Vec<(bp_messages::MessagePayload, bp_messages::OutboundMessageDetails)>, + ) -> Vec { + bridge_runtime_common::messages_api::inbound_message_details::< + Runtime, + WithBridgeHubRococoMessagesInstance, + >(lane, messages) + } + } + #[cfg(feature = "try-runtime")] impl frame_try_runtime::TryRuntime for Runtime { fn on_runtime_upgrade() -> (Weight, Weight) { diff --git a/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/xcm_config.rs b/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/xcm_config.rs index 84a2aeb60d..c6dead42d5 100644 --- a/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/xcm_config.rs +++ b/parachains/runtimes/bridge-hubs/bridge-hub-rococo/src/xcm_config.rs @@ -15,15 +15,18 @@ // along with Cumulus. If not, see . use super::{ - AccountId, Balance, Balances, Call, Event, Origin, ParachainInfo, ParachainSystem, PolkadotXcm, Runtime, - XcmpQueue, + AccountId, Balance, Balances, Call, Event, Origin, ParachainInfo, ParachainSystem, PolkadotXcm, + Runtime, XcmpQueue, +}; +use crate::{ + bridge_hub_rococo_config::ToBridgeHubWococoHaulBlobExporter, + bridge_hub_wococo_config::ToBridgeHubRococoHaulBlobExporter, }; use frame_support::{ match_types, parameter_types, traits::{Everything, Nothing}, - weights::Weight, + weights::{IdentityFee, Weight}, }; -use frame_support::weights::IdentityFee; use pallet_xcm::XcmPassthrough; use polkadot_parachain::primitives::Sibling; use xcm::latest::prelude::*; @@ -34,7 +37,7 @@ use xcm_builder::{ SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit, UsingComponents, }; -use xcm_executor::XcmExecutor; +use xcm_executor::{traits::ExportXcm, XcmExecutor}; parameter_types! { pub const RelayLocation: MultiLocation = MultiLocation::parent(); @@ -192,6 +195,8 @@ pub type Barrier = ( TakeWeightCredit, AllowTopLevelPaidExecutionFrom, AllowUnpaidExecutionFrom, + // TODO:check-parameter - supporting unpaid execution at first then SovereignPaid + AllowUnpaidExecutionFrom, // ^^^ Parent & its unit plurality gets free execution ); @@ -219,7 +224,7 @@ impl xcm_executor::Config for XcmConfig { type AssetLocker = (); type AssetExchanger = (); type FeeManager = (); - type MessageExporter = (); + type MessageExporter = BridgeHubRococoOrBridgeHubWococoSwitchExporter; type UniversalAliases = Nothing; type CallDispatcher = Call; } @@ -231,32 +236,11 @@ pub type LocalOriginToLocation = SignedToAccountId32, + cumulus_primitives_utility::ParentAsUmp, // ..and XCMP to communicate with the sibling chains. XcmpQueue, ); -// TODO: hacked -// impl pallet_xcm::Config for Runtime { -// type Event = Event; -// type SendXcmOrigin = EnsureXcmOrigin; -// type XcmRouter = XcmRouter; -// type ExecuteXcmOrigin = EnsureXcmOrigin; -// type XcmExecuteFilter = Nothing; -// // ^ Disable dispatchable execute on the XCM pallet. -// // Needs to be `Everything` for local testing. -// type XcmExecutor = XcmExecutor; -// type XcmTeleportFilter = Everything; -// type XcmReserveTransferFilter = Nothing; -// type Weigher = FixedWeightBounds; -// type LocationInverter = LocationInverter; -// type Origin = Origin; -// type Call = Call; -// -// const VERSION_DISCOVERY_QUEUE_SIZE: u32 = 100; -// // ^ Override for AdvertisedXcmVersion default -// type AdvertisedXcmVersion = pallet_xcm::CurrentXcmVersion; -// } impl pallet_xcm::Config for Runtime { type Event = Event; type SendXcmOrigin = EnsureXcmOrigin; @@ -283,3 +267,35 @@ impl cumulus_pallet_xcm::Config for Runtime { type Event = Event; type XcmExecutor = XcmExecutor; } + +/// Hacky switch implementation, because we have just one runtime for Rococo and Wococo BridgeHub, so it means we have just one XcmConfig +pub struct BridgeHubRococoOrBridgeHubWococoSwitchExporter; +impl ExportXcm for BridgeHubRococoOrBridgeHubWococoSwitchExporter { + type Ticket = (NetworkId, (sp_std::prelude::Vec, XcmHash)); + + fn validate( + network: NetworkId, + channel: u32, + destination: &mut Option, + message: &mut Option>, + ) -> SendResult { + match network { + Rococo => + ToBridgeHubRococoHaulBlobExporter::validate(network, channel, destination, message) + .map(|result| ((Rococo, result.0), result.1)), + Wococo => + ToBridgeHubWococoHaulBlobExporter::validate(network, channel, destination, message) + .map(|result| ((Wococo, result.0), result.1)), + _ => unimplemented!("Unsupported network: {:?}", network), + } + } + + fn deliver(ticket: Self::Ticket) -> Result { + let (network, ticket) = ticket; + match network { + Rococo => ToBridgeHubRococoHaulBlobExporter::deliver(ticket), + Wococo => ToBridgeHubWococoHaulBlobExporter::deliver(ticket), + _ => unimplemented!("Unsupported network: {:?}", network), + } + } +}