From 851b90ec736962dd7cab5bda0dabfb691e5063e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Andr=C3=A9s=20Dorado=20Su=C3=A1rez?= Date: Tue, 22 Oct 2024 18:28:06 -0500 Subject: [PATCH] fix(system-parachains/bridge-hubs): missing structures used in benchmarks --- .../src/bridge_to_polkadot_config.rs | 165 +++++++++-------- .../bridge-hubs/bridge-hub-kusama/src/lib.rs | 73 ++++++-- .../src/bridge_to_kusama_config.rs | 167 ++++++++++-------- .../bridge-hub-polkadot/src/lib.rs | 72 ++++++-- 4 files changed, 300 insertions(+), 177 deletions(-) diff --git a/system-parachains/bridge-hubs/bridge-hub-kusama/src/bridge_to_polkadot_config.rs b/system-parachains/bridge-hubs/bridge-hub-kusama/src/bridge_to_polkadot_config.rs index 080fb4a49c..944eb91e74 100644 --- a/system-parachains/bridge-hubs/bridge-hub-kusama/src/bridge_to_polkadot_config.rs +++ b/system-parachains/bridge-hubs/bridge-hub-kusama/src/bridge_to_polkadot_config.rs @@ -22,9 +22,12 @@ use crate::{ AccountId, Balance, Balances, BlockNumber, BridgePolkadotMessages, PolkadotXcm, Runtime, RuntimeEvent, RuntimeHoldReason, XcmOverBridgeHubPolkadot, }; -use bp_messages::LegacyLaneId; +use bp_messages::{ + source_chain::FromBridgedChainMessagesDeliveryProof, + target_chain::FromBridgedChainMessagesProof, LegacyLaneId, +}; use bp_parachains::SingleParaStoredHeaderDataBuilder; -use bp_runtime::{Chain, UnderlyingChainProvider}; +use bp_runtime::Chain; use bridge_hub_common::xcm_version::XcmVersionOfDestAndRemoteBridge; use frame_support::{parameter_types, traits::PalletInfoAccess}; use frame_system::{EnsureNever, EnsureRoot}; @@ -36,14 +39,10 @@ use pallet_bridge_relayers::extension::{ use pallet_xcm_bridge_hub::XcmAsPlainPayload; use parachains_common::xcm_config::{AllSiblingSystemParachains, RelayOrOtherSystemParachains}; use polkadot_parachain_primitives::primitives::Sibling; -use sp_runtime::{traits::ConstU32, RuntimeDebug}; +use sp_runtime::traits::ConstU32; use xcm::latest::prelude::*; use xcm_builder::{BridgeBlobDispatcher, ParentIsPreset, SiblingParachainConvertsVia}; -/// Lane identifier, used to connect Kusama Asset Hub and Polkadot Asset Hub. -pub const XCM_LANE_FOR_ASSET_HUB_KUSAMA_TO_ASSET_HUB_POLKADOT: LegacyLaneId = - LegacyLaneId([0, 0, 0, 1]); - // Parameters that may be changed by the governance. parameter_types! { /// Reward that is paid (by the Kusama Asset Hub) to relayers for delivering a single @@ -74,10 +73,6 @@ parameter_types! { /// Interior location (relative to this runtime) of the with-Polkadot messages pallet. pub BridgeKusamaToPolkadotMessagesPalletInstance: InteriorLocation = PalletInstance(::index() as u8).into(); - /// Identifier of the sibling Kusama Asset Hub parachain. - pub AssetHubKusamaParaId: cumulus_primitives_core::ParaId = kusama_runtime_constants::system_parachain::ASSET_HUB_ID.into(); - /// Identifier of the bridged Polkadot Asset Hub parachain. - pub AssetHubPolkadotParaId: cumulus_primitives_core::ParaId = polkadot_runtime_constants::system_parachain::ASSET_HUB_ID.into(); /// Location of the bridged Polkadot Bridge Hub parachain. pub BridgeHubPolkadotLocation: Location = Location { parents: 2, @@ -86,10 +81,28 @@ parameter_types! { Parachain(::PARACHAIN_ID) ].into() }; +} - /// Lane identifier, used to connect Kusama Asset Hub and Polkadot Asset Hub. - pub const AssetHubKusamaToAssetHubPolkadotMessagesLane: LegacyLaneId - = XCM_LANE_FOR_ASSET_HUB_KUSAMA_TO_ASSET_HUB_POLKADOT; +pub type RelayersForLegacyLaneIdsMessagesInstance = (); +/// Allows collect and claim rewards for relayers. +impl pallet_bridge_relayers::Config for Runtime { + type RuntimeEvent = RuntimeEvent; + type Reward = Balance; + type PaymentProcedure = bp_relayers::PayRewardFromAccount< + pallet_balances::Pallet, + AccountId, + Self::LaneId, + >; + type StakeAndSlash = pallet_bridge_relayers::StakeAndSlashNamed< + AccountId, + BlockNumber, + Balances, + RelayerStakeReserveId, + RequiredStakeForStakeAndSlash, + RelayerStakeLease, + >; + type LaneId = LegacyLaneId; + type WeightInfo = weights::pallet_bridge_relayers::WeightInfo; } // Parameters, used by bridge transport code. @@ -112,16 +125,6 @@ parameter_types! { /// Name of the `paras` pallet at Polkadot that tracks all parachain heads. pub const ParachainPalletNameAtPolkadot: &'static str = bp_polkadot::PARAS_PALLET_NAME; - /// Maximal number of entries in the unrewarded relayers vector at the Kusama Bridge Hub. It matches the - /// maximal number of unrewarded relayers that the single confirmation transaction at Polkadot Bridge - /// Hub may process. - pub const MaxUnrewardedRelayerEntriesAtInboundLane: bp_messages::MessageNonce = - bp_bridge_hub_polkadot::MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX; - /// Maximal number of unconfirmed messages at the Kusama Bridge Hub. It matches the maximal number of - /// uncinfirmed messages that the single confirmation transaction at Polkadot Bridge Hub may process. - pub const MaxUnconfirmedMessagesAtInboundLane: bp_messages::MessageNonce = - bp_bridge_hub_polkadot::MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX; - /// Reserve identifier, used by the `pallet_bridge_relayers` to hold funds of registered relayer. pub const RelayerStakeReserveId: [u8; 8] = *b"brdgrlrs"; /// Minimal period of relayer registration. Roughly, it is the 1 hour of real time. @@ -137,6 +140,13 @@ parameter_types! { pub storage BridgeDeposit: Balance = constants::currency::UNITS; } +/// Proof of messages, coming from Polkadot. +pub type FromPolkadotBridgeHubMessagesProof = + FromBridgedChainMessagesProof>; +/// Messages delivery proof for Polkadot Bridge Hub -> Kusama Bridge Hub messages. +pub type ToPolkadotBridgeHubMessagesDeliveryProof = + FromBridgedChainMessagesDeliveryProof>; + /// Dispatches received XCM messages from other bridge pub type FromPolkadotMessageBlobDispatcher = BridgeBlobDispatcher< XcmRouter, @@ -144,6 +154,20 @@ pub type FromPolkadotMessageBlobDispatcher = BridgeBlobDispatcher< BridgeKusamaToPolkadotMessagesPalletInstance, >; +/// Signed extension that refunds relayers that are delivering messages from the Polkadot parachain. +pub type OnBridgeHubPolkadotRefundBridgeHubKusamaMessages = BridgeRelayersSignedExtension< + Runtime, + WithMessagesExtensionConfig< + StrOnBridgeHubPolkadotRefundBridgeHubKusamaMessages, + Runtime, + WithBridgeHubPolkadotMessagesInstance, + RelayersForLegacyLaneIdsMessagesInstance, + PriorityBoostPerMessage, + >, + LaneIdOf, +>; +bp_runtime::generate_static_str_provider!(OnBridgeHubPolkadotRefundBridgeHubKusamaMessages); + /// Add GRANDPA bridge pallet to track Polkadot relay chain. pub type BridgeGrandpaPolkadotInstance = pallet_bridge_grandpa::Instance1; impl pallet_bridge_grandpa::Config for Runtime { @@ -168,27 +192,6 @@ impl pallet_bridge_parachains::Config for Runti type MaxParaHeadDataSize = MaxParaHeadDataSize; } -/// Allows collect and claim rewards for relayers. -impl pallet_bridge_relayers::Config for Runtime { - type RuntimeEvent = RuntimeEvent; - type Reward = Balance; - type PaymentProcedure = bp_relayers::PayRewardFromAccount< - pallet_balances::Pallet, - AccountId, - Self::LaneId, - >; - type StakeAndSlash = pallet_bridge_relayers::StakeAndSlashNamed< - AccountId, - BlockNumber, - Balances, - RelayerStakeReserveId, - RequiredStakeForStakeAndSlash, - RelayerStakeLease, - >; - type LaneId = LegacyLaneId; - type WeightInfo = weights::pallet_bridge_relayers::WeightInfo; -} - /// Add XCM messages support for exchanging messages with BridgeHubPolkadot. pub type WithBridgeHubPolkadotMessagesInstance = pallet_bridge_messages::Instance1; impl pallet_bridge_messages::Config for Runtime { @@ -252,38 +255,52 @@ impl pallet_xcm_bridge_hub::Config for Runtime type BlobDispatcher = FromPolkadotMessageBlobDispatcher; } -/// BridgeHubPolkadot chain from message lane point of view. -#[derive(RuntimeDebug, Clone, Copy)] -pub struct BridgeHubPolkadot; - -impl UnderlyingChainProvider for BridgeHubPolkadot { - type Chain = bp_bridge_hub_polkadot::BridgeHubPolkadot; -} - -/// BridgeHubKusama chain from message lane point of view. -#[derive(RuntimeDebug, Clone, Copy)] -pub struct BridgeHubKusama; +#[cfg(feature = "runtime-benchmarks")] +pub(crate) fn open_bridge_for_benchmarks( + with: pallet_xcm_bridge_hub::LaneIdOf, + sibling_para_id: u32, +) -> InteriorLocation +where + R: pallet_xcm_bridge_hub::Config, + XBHI: 'static, + C: xcm_executor::traits::ConvertLocation< + bp_runtime::AccountIdOf>, + >, +{ + use pallet_xcm_bridge_hub::{Bridge, BridgeId, BridgeState}; + use sp_runtime::traits::Zero; + use sp_std::boxed::Box; + use xcm::VersionedInteriorLocation; + + // insert bridge metadata + let lane_id = with; + let sibling_parachain = Location::new(1, [Parachain(sibling_para_id)]); + let universal_source = [GlobalConsensus(Westend), Parachain(sibling_para_id)].into(); + let universal_destination = [GlobalConsensus(Rococo), Parachain(2075)].into(); + let bridge_id = BridgeId::new(&universal_source, &universal_destination); + + // insert only bridge metadata, because the benchmarks create lanes + pallet_xcm_bridge_hub::Bridges::::insert( + bridge_id, + Bridge { + bridge_origin_relative_location: Box::new(sibling_parachain.clone().into()), + bridge_origin_universal_location: Box::new(VersionedInteriorLocation::from( + universal_source.clone(), + )), + bridge_destination_universal_location: Box::new(VersionedInteriorLocation::from( + universal_destination, + )), + state: BridgeState::Opened, + bridge_owner_account: C::convert_location(&sibling_parachain).expect("valid AccountId"), + deposit: Zero::zero(), + lane_id, + }, + ); + pallet_xcm_bridge_hub::LaneToBridge::::insert(lane_id, bridge_id); -impl UnderlyingChainProvider for BridgeHubKusama { - type Chain = bp_bridge_hub_kusama::BridgeHubKusama; + universal_source } -pub type RelayersForLegacyLaneIdsMessagesInstance = (); - -/// Signed extension that refunds relayers that are delivering messages from the Polkadot parachain. -pub type OnBridgeHubPolkadotRefundBridgeHubKusamaMessages = BridgeRelayersSignedExtension< - Runtime, - WithMessagesExtensionConfig< - StrOnBridgeHubPolkadotRefundBridgeHubKusamaMessages, - Runtime, - WithBridgeHubPolkadotMessagesInstance, - RelayersForLegacyLaneIdsMessagesInstance, - PriorityBoostPerMessage, - >, - LaneIdOf, ->; -bp_runtime::generate_static_str_provider!(OnBridgeHubPolkadotRefundBridgeHubKusamaMessages); - #[cfg(test)] mod tests { use super::*; diff --git a/system-parachains/bridge-hubs/bridge-hub-kusama/src/lib.rs b/system-parachains/bridge-hubs/bridge-hub-kusama/src/lib.rs index 603c54fd5c..c089fe3578 100644 --- a/system-parachains/bridge-hubs/bridge-hub-kusama/src/lib.rs +++ b/system-parachains/bridge-hubs/bridge-hub-kusama/src/lib.rs @@ -28,12 +28,14 @@ pub mod genesis_config_presets; mod weights; pub mod xcm_config; +use bp_messages::LegacyLaneId; use bridge_hub_common::message_queue::{ AggregateMessageOrigin, NarrowOriginToSibling, ParaIdToSibling, }; use cumulus_pallet_parachain_system::RelayNumberMonotonicallyIncreases; use cumulus_primitives_core::ParaId; +use pallet_bridge_messages::LaneIdOf; use sp_api::impl_runtime_apis; use sp_core::{crypto::KeyTypeId, OpaqueMetadata}; use sp_runtime::{ @@ -1105,11 +1107,48 @@ impl_runtime_apis! { ); BenchmarkError::Stop("XcmVersion was not stored!") })?; + + let sibling_parachain_id = Parachain(8765); + let remote_parachain_id = Parachain(5678); + let sibling_parachain_location = Location::new(1, [sibling_parachain_id]); + + // fund SA + use frame_support::traits::fungible::Mutate; + use xcm_executor::traits::ConvertLocation; + frame_support::assert_ok!( + Balances::mint_into( + &xcm_config::LocationToAccountId::convert_location(&sibling_parachain_location).expect("valid AccountId"), + bridge_to_polkadot_config::BridgeDeposit::get() + .saturating_add(ExistentialDeposit::get()) + .saturating_add(UNITS * 5) + ) + ); + + // open bridge + let bridge_destination_universal_location: InteriorLocation = [GlobalConsensus(NetworkId::Polkadot), remote_parachain_id].into(); + let locations = XcmOverBridgeHubPolkadot::bridge_locations( + sibling_parachain_location.clone(), + bridge_destination_universal_location.clone(), + )?; + XcmOverBridgeHubPolkadot::do_open_bridge( + locations, + bp_messages::LegacyLaneId([1, 2, 3, 4]), + true, + ).map_err(|e| { + log::error!( + "Failed to `XcmOverBridgeHubRococo::open_bridge`({:?}, {:?})`, error: {:?}", + sibling_parachain_location, + bridge_destination_universal_location, + e + ); + BenchmarkError::Stop("Bridge was not opened!") + })?; + Ok( ( - bridge_to_polkadot_config::FromAssetHubKusamaToAssetHubPolkadotRoute::get().location, + sibling_parachain_location, NetworkId::Polkadot, - Parachain(bridge_to_polkadot_config::AssetHubPolkadotParaId::get().into()).into() + [remote_parachain_id].into() ) ) } @@ -1133,12 +1172,13 @@ impl_runtime_apis! { impl BridgeRelayersConfig for Runtime { fn prepare_rewards_account( - account_params: bp_relayers::RewardsAccountParams, + account_params: bp_relayers::RewardsAccountParams>, reward: Balance, ) { let rewards_account = bp_relayers::PayRewardFromAccount::< Balances, - AccountId + AccountId, + LegacyLaneId, >::rewards_account(account_params); Self::deposit_account(rewards_account, reward); } @@ -1161,17 +1201,17 @@ impl_runtime_apis! { fn prepare_parachain_heads_proof( parachains: &[bp_polkadot_core::parachains::ParaId], parachain_head_size: u32, - proof_size: bp_runtime::StorageProofSize, + proof_params: bp_runtime::UnverifiedStorageProofParams, ) -> ( - pallet_bridge_parachains::RelayBlockNumber, - pallet_bridge_parachains::RelayBlockHash, + bp_parachains::RelayBlockNumber, + bp_parachains::RelayBlockHash, bp_polkadot_core::parachains::ParaHeadsProof, Vec<(bp_polkadot_core::parachains::ParaId, bp_polkadot_core::parachains::ParaHash)>, ) { prepare_parachain_heads_proof::( parachains, parachain_head_size, - proof_size, + proof_params, ) } } @@ -1202,25 +1242,30 @@ impl_runtime_apis! { } fn prepare_message_proof( - params: MessageProofParams, - ) -> (bridge_to_polkadot_config::FromPolkadotBridgeHubMessagesProof, Weight) { + params: MessageProofParams>, + ) -> (bridge_to_polkadot_config::FromPolkadotBridgeHubMessagesProof, Weight) { use cumulus_primitives_core::XcmpMessageSource; assert!(XcmpQueue::take_outbound_messages(usize::MAX).is_empty()); ParachainSystem::open_outbound_hrmp_channel_for_benchmarks_or_tests(42.into()); prepare_message_proof_from_parachain::< Runtime, bridge_to_polkadot_config::BridgeGrandpaPolkadotInstance, - bridge_to_polkadot_config::WithBridgeHubPolkadotMessageBridge, + bridge_to_polkadot_config::WithBridgeHubPolkadotMessagesInstance, >(params, generate_xcm_builder_bridge_message_sample([GlobalConsensus(Kusama), Parachain(42)].into())) } fn prepare_message_delivery_proof( - params: MessageDeliveryProofParams, - ) -> bridge_to_polkadot_config::ToPolkadotBridgeHubMessagesDeliveryProof { + params: MessageDeliveryProofParams>, + ) -> bridge_to_polkadot_config::ToPolkadotBridgeHubMessagesDeliveryProof { + let _ = bridge_to_polkadot_config::open_bridge_for_benchmarks::< + Runtime, + bridge_to_polkadot_config::XcmOverBridgeHubPolkadotInstance, + xcm_config::LocationToAccountId, + >(params.lane, 42); prepare_message_delivery_proof_from_parachain::< Runtime, bridge_to_polkadot_config::BridgeGrandpaPolkadotInstance, - bridge_to_polkadot_config::WithBridgeHubPolkadotMessageBridge, + bridge_to_polkadot_config::WithBridgeHubPolkadotMessagesInstance, >(params) } diff --git a/system-parachains/bridge-hubs/bridge-hub-polkadot/src/bridge_to_kusama_config.rs b/system-parachains/bridge-hubs/bridge-hub-polkadot/src/bridge_to_kusama_config.rs index 690f5689aa..8c4528f0ca 100644 --- a/system-parachains/bridge-hubs/bridge-hub-polkadot/src/bridge_to_kusama_config.rs +++ b/system-parachains/bridge-hubs/bridge-hub-polkadot/src/bridge_to_kusama_config.rs @@ -23,10 +23,12 @@ use crate::{ RuntimeEvent, RuntimeHoldReason, XcmOverBridgeHubKusama, }; -use bp_messages::LegacyLaneId; +use bp_messages::{ + source_chain::FromBridgedChainMessagesDeliveryProof, + target_chain::FromBridgedChainMessagesProof, LegacyLaneId, +}; use bp_parachains::SingleParaStoredHeaderDataBuilder; -use bp_runtime::{Chain, UnderlyingChainProvider}; - +use bp_runtime::Chain; use bridge_hub_common::xcm_version::XcmVersionOfDestAndRemoteBridge; use frame_support::{parameter_types, traits::PalletInfoAccess}; use frame_system::{EnsureNever, EnsureRoot}; @@ -38,14 +40,10 @@ use pallet_xcm_bridge_hub::XcmAsPlainPayload; use parachains_common::xcm_config::{AllSiblingSystemParachains, RelayOrOtherSystemParachains}; use polkadot_parachain_primitives::primitives::Sibling; use polkadot_runtime_constants as constants; -use sp_runtime::{traits::ConstU32, RuntimeDebug}; +use sp_runtime::traits::ConstU32; use xcm::latest::prelude::*; use xcm_builder::{BridgeBlobDispatcher, ParentIsPreset, SiblingParachainConvertsVia}; -/// Lane identifier, used to connect Polkadot Asset Hub and Kusama Asset Hub. -pub const XCM_LANE_FOR_ASSET_HUB_POLKADOT_TO_ASSET_HUB_KUSAMA: LegacyLaneId = - LegacyLaneId([0, 0, 0, 1]); - // Parameters that may be changed by the governance. parameter_types! { /// Reward that is paid (by the Polkadot Asset Hub) to relayers for delivering a single @@ -76,10 +74,6 @@ parameter_types! { /// Interior location (relative to this runtime) of the with-Kusama messages pallet. pub BridgePolkadotToKusamaMessagesPalletInstance: InteriorLocation = PalletInstance(::index() as u8).into(); - /// Identifier of the sibling Polkadot Asset Hub parachain. - pub AssetHubPolkadotParaId: cumulus_primitives_core::ParaId = polkadot_runtime_constants::system_parachain::ASSET_HUB_ID.into(); - /// Identifier of the bridged Kusama Asset Hub parachain. - pub AssetHubKusamaParaId: cumulus_primitives_core::ParaId = kusama_runtime_constants::system_parachain::ASSET_HUB_ID.into(); /// Location of the bridged Kusama Bridge Hub parachain. pub BridgeHubKusamaLocation: Location = Location { parents: 2, @@ -88,9 +82,28 @@ parameter_types! { Parachain(::PARACHAIN_ID) ].into() }; - /// Lane identifier, used to connect Polkadot Asset Hub and Kusama Asset Hub. - pub const AssetHubPolkadotToAssetHubKusamaMessagesLane: bp_messages::LegacyLaneId - = XCM_LANE_FOR_ASSET_HUB_POLKADOT_TO_ASSET_HUB_KUSAMA; +} + +pub type RelayersForLegacyLaneIdsMessagesInstance = (); +/// Allows collect and claim rewards for relayers. +impl pallet_bridge_relayers::Config for Runtime { + type RuntimeEvent = RuntimeEvent; + type Reward = Balance; + type PaymentProcedure = bp_relayers::PayRewardFromAccount< + pallet_balances::Pallet, + AccountId, + Self::LaneId, + >; + type StakeAndSlash = pallet_bridge_relayers::StakeAndSlashNamed< + AccountId, + BlockNumber, + Balances, + RelayerStakeReserveId, + RequiredStakeForStakeAndSlash, + RelayerStakeLease, + >; + type LaneId = LegacyLaneId; + type WeightInfo = weights::pallet_bridge_relayers::WeightInfo; } // Parameters, used by bridge transport code. @@ -113,16 +126,6 @@ parameter_types! { /// Name of the `paras` pallet at Kusama that tracks all parachain heads. pub const ParachainPalletNameAtKusama: &'static str = bp_kusama::PARAS_PALLET_NAME; - /// Maximal number of entries in the unrewarded relayers vector at the Polkadot Bridge Hub. It matches the - /// maximal number of unrewarded relayers that the single confirmation transaction at Kusama Bridge - /// Hub may process. - pub const MaxUnrewardedRelayerEntriesAtInboundLane: bp_messages::MessageNonce = - bp_bridge_hub_kusama::MAX_UNREWARDED_RELAYERS_IN_CONFIRMATION_TX; - /// Maximal number of unconfirmed messages at the Polkadot Bridge Hub. It matches the maximal number of - /// uncinfirmed messages that the single confirmation transaction at Kusama Bridge Hub may process. - pub const MaxUnconfirmedMessagesAtInboundLane: bp_messages::MessageNonce = - bp_bridge_hub_kusama::MAX_UNCONFIRMED_MESSAGES_IN_CONFIRMATION_TX; - /// Reserve identifier, used by the `pallet_bridge_relayers` to hold funds of registered relayer. pub const RelayerStakeReserveId: [u8; 8] = *b"brdgrlrs"; /// Minimal period of relayer registration. Roughly, it is the 1 hour of real time. @@ -138,6 +141,13 @@ parameter_types! { pub storage BridgeDeposit: Balance = 10 * constants::currency::UNITS; } +/// Proof of messages, coming from Kusama. +pub type FromKusamaBridgeHubMessagesProof = + FromBridgedChainMessagesProof>; +/// Messages delivery proof for Kusama Bridge Hub -> Polkadot Bridge Hub messages. +pub type ToKusamaBridgeHubMessagesDeliveryProof = + FromBridgedChainMessagesDeliveryProof>; + /// Dispatches received XCM messages from other bridge pub type FromKusamaMessageBlobDispatcher = BridgeBlobDispatcher< XcmRouter, @@ -145,6 +155,20 @@ pub type FromKusamaMessageBlobDispatcher = BridgeBlobDispatcher< BridgePolkadotToKusamaMessagesPalletInstance, >; +/// Signed extension that refunds relayers that are delivering messages from the Kusama parachain. +pub type OnBridgeHubPolkadotRefundBridgeHubKusamaMessages = BridgeRelayersSignedExtension< + Runtime, + WithMessagesExtensionConfig< + StrOnBridgeHubPolkadotRefundBridgeHubKusamaMessages, + Runtime, + WithBridgeHubKusamaMessagesInstance, + RelayersForLegacyLaneIdsMessagesInstance, + PriorityBoostPerMessage, + >, + LaneIdOf, +>; +bp_runtime::generate_static_str_provider!(OnBridgeHubPolkadotRefundBridgeHubKusamaMessages); + /// Add GRANDPA bridge pallet to track Kusama relay chain. pub type BridgeGrandpaKusamaInstance = pallet_bridge_grandpa::Instance1; impl pallet_bridge_grandpa::Config for Runtime { @@ -169,27 +193,6 @@ impl pallet_bridge_parachains::Config for Runtime type MaxParaHeadDataSize = MaxParaHeadDataSize; } -/// Allows collect and claim rewards for relayers. -impl pallet_bridge_relayers::Config for Runtime { - type RuntimeEvent = RuntimeEvent; - type Reward = Balance; - type PaymentProcedure = bp_relayers::PayRewardFromAccount< - pallet_balances::Pallet, - AccountId, - Self::LaneId, - >; - type StakeAndSlash = pallet_bridge_relayers::StakeAndSlashNamed< - AccountId, - BlockNumber, - Balances, - RelayerStakeReserveId, - RequiredStakeForStakeAndSlash, - RelayerStakeLease, - >; - type LaneId = LegacyLaneId; - type WeightInfo = weights::pallet_bridge_relayers::WeightInfo; -} - /// Add XCM messages support for exchanging messages with BridgeHubKusama. pub type WithBridgeHubKusamaMessagesInstance = pallet_bridge_messages::Instance1; impl pallet_bridge_messages::Config for Runtime { @@ -251,38 +254,52 @@ impl pallet_xcm_bridge_hub::Config for Runtime { type BlobDispatcher = FromKusamaMessageBlobDispatcher; } -/// BridgeHubKusama chain from message lane point of view. -#[derive(RuntimeDebug, Clone, Copy)] -pub struct BridgeHubKusama; - -impl UnderlyingChainProvider for BridgeHubKusama { - type Chain = bp_bridge_hub_kusama::BridgeHubKusama; -} - -/// BridgeHubPolkadot chain from message lane point of view. -#[derive(RuntimeDebug, Clone, Copy)] -pub struct BridgeHubPolkadot; +#[cfg(feature = "runtime-benchmarks")] +pub(crate) fn open_bridge_for_benchmarks( + with: pallet_xcm_bridge_hub::LaneIdOf, + sibling_para_id: u32, +) -> InteriorLocation +where + R: pallet_xcm_bridge_hub::Config, + XBHI: 'static, + C: xcm_executor::traits::ConvertLocation< + bp_runtime::AccountIdOf>, + >, +{ + use pallet_xcm_bridge_hub::{Bridge, BridgeId, BridgeState}; + use sp_runtime::traits::Zero; + use sp_std::boxed::Box; + use xcm::VersionedInteriorLocation; + + // insert bridge metadata + let lane_id = with; + let sibling_parachain = Location::new(1, [Parachain(sibling_para_id)]); + let universal_source = [GlobalConsensus(Westend), Parachain(sibling_para_id)].into(); + let universal_destination = [GlobalConsensus(Rococo), Parachain(2075)].into(); + let bridge_id = BridgeId::new(&universal_source, &universal_destination); + + // insert only bridge metadata, because the benchmarks create lanes + pallet_xcm_bridge_hub::Bridges::::insert( + bridge_id, + Bridge { + bridge_origin_relative_location: Box::new(sibling_parachain.clone().into()), + bridge_origin_universal_location: Box::new(VersionedInteriorLocation::from( + universal_source.clone(), + )), + bridge_destination_universal_location: Box::new(VersionedInteriorLocation::from( + universal_destination, + )), + state: BridgeState::Opened, + bridge_owner_account: C::convert_location(&sibling_parachain).expect("valid AccountId"), + deposit: Zero::zero(), + lane_id, + }, + ); + pallet_xcm_bridge_hub::LaneToBridge::::insert(lane_id, bridge_id); -impl UnderlyingChainProvider for BridgeHubPolkadot { - type Chain = bp_bridge_hub_polkadot::BridgeHubPolkadot; + universal_source } -pub type RelayersForLegacyLaneIdsMessagesInstance = (); - -/// Signed extension that refunds relayers that are delivering messages from the Kusama parachain. -pub type OnBridgeHubPolkadotRefundBridgeHubKusamaMessages = BridgeRelayersSignedExtension< - Runtime, - WithMessagesExtensionConfig< - StrOnBridgeHubPolkadotRefundBridgeHubKusamaMessages, - Runtime, - WithBridgeHubKusamaMessagesInstance, - RelayersForLegacyLaneIdsMessagesInstance, - PriorityBoostPerMessage, - >, - LaneIdOf, ->; -bp_runtime::generate_static_str_provider!(OnBridgeHubPolkadotRefundBridgeHubKusamaMessages); - #[cfg(test)] mod tests { use super::*; diff --git a/system-parachains/bridge-hubs/bridge-hub-polkadot/src/lib.rs b/system-parachains/bridge-hubs/bridge-hub-polkadot/src/lib.rs index 1a69d9da30..01150fe8ed 100644 --- a/system-parachains/bridge-hubs/bridge-hub-polkadot/src/lib.rs +++ b/system-parachains/bridge-hubs/bridge-hub-polkadot/src/lib.rs @@ -34,6 +34,7 @@ use bridge_hub_common::message_queue::{ }; use cumulus_pallet_parachain_system::RelayNumberMonotonicallyIncreases; use cumulus_primitives_core::ParaId; +use pallet_bridge_messages::LaneIdOf; use snowbridge_core::{ outbound::{Command, Fee}, AgentId, PricingParameters, @@ -1123,11 +1124,48 @@ impl_runtime_apis! { ); BenchmarkError::Stop("XcmVersion was not stored!") })?; + + let sibling_parachain_id = Parachain(5678); + let remote_parachain_id = Parachain(8765); + let sibling_parachain_location = Location::new(1, [sibling_parachain_id]); + + // fund SA + use frame_support::traits::fungible::Mutate; + use xcm_executor::traits::ConvertLocation; + frame_support::assert_ok!( + Balances::mint_into( + &xcm_config::LocationToAccountId::convert_location(&sibling_parachain_location).expect("valid AccountId"), + bridge_to_kusama_config::BridgeDeposit::get() + .saturating_add(ExistentialDeposit::get()) + .saturating_add(UNITS * 5) + ) + ); + + // open bridge + let bridge_destination_universal_location: InteriorLocation = [GlobalConsensus(NetworkId::Kusama), remote_parachain_id].into(); + let locations = XcmOverBridgeHubKusama::bridge_locations( + sibling_parachain_location.clone(), + bridge_destination_universal_location.clone(), + )?; + XcmOverBridgeHubKusama::do_open_bridge( + locations, + bp_messages::LegacyLaneId([1, 2, 3, 4]), + true, + ).map_err(|e| { + log::error!( + "Failed to `XcmOverBridgeHubRococo::open_bridge`({:?}, {:?})`, error: {:?}", + sibling_parachain_location, + bridge_destination_universal_location, + e + ); + BenchmarkError::Stop("Bridge was not opened!") + })?; + Ok( ( - bridge_to_kusama_config::FromAssetHubPolkadotToAssetHubKusamaRoute::get().location, + sibling_parachain_location, NetworkId::Kusama, - Parachain(bridge_to_kusama_config::AssetHubKusamaParaId::get().into()).into() + [remote_parachain_id].into() ) ) } @@ -1151,12 +1189,13 @@ impl_runtime_apis! { impl BridgeRelayersConfig for Runtime { fn prepare_rewards_account( - account_params: bp_relayers::RewardsAccountParams, + account_params: bp_relayers::RewardsAccountParams>, reward: Balance, ) { let rewards_account = bp_relayers::PayRewardFromAccount::< Balances, - AccountId + AccountId, + LaneIdOf >::rewards_account(account_params); Self::deposit_account(rewards_account, reward); } @@ -1179,17 +1218,17 @@ impl_runtime_apis! { fn prepare_parachain_heads_proof( parachains: &[bp_polkadot_core::parachains::ParaId], parachain_head_size: u32, - proof_size: bp_runtime::StorageProofSize, + proof_params: bp_runtime::UnverifiedStorageProofParams, ) -> ( - pallet_bridge_parachains::RelayBlockNumber, - pallet_bridge_parachains::RelayBlockHash, + bp_parachains::RelayBlockNumber, + bp_parachains::RelayBlockHash, bp_polkadot_core::parachains::ParaHeadsProof, Vec<(bp_polkadot_core::parachains::ParaId, bp_polkadot_core::parachains::ParaHash)>, ) { prepare_parachain_heads_proof::( parachains, parachain_head_size, - proof_size, + proof_params, ) } } @@ -1220,25 +1259,30 @@ impl_runtime_apis! { } fn prepare_message_proof( - params: MessageProofParams, - ) -> (bridge_to_kusama_config::FromKusamaBridgeHubMessagesProof, Weight) { + params: MessageProofParams>, + ) -> (bridge_to_kusama_config::FromKusamaBridgeHubMessagesProof, Weight) { use cumulus_primitives_core::XcmpMessageSource; assert!(XcmpQueue::take_outbound_messages(usize::MAX).is_empty()); ParachainSystem::open_outbound_hrmp_channel_for_benchmarks_or_tests(42.into()); prepare_message_proof_from_parachain::< Runtime, bridge_to_kusama_config::BridgeGrandpaKusamaInstance, - bridge_to_kusama_config::WithBridgeHubKusamaMessageBridge, + bridge_to_kusama_config::WithBridgeHubKusamaMessagesInstance, >(params, generate_xcm_builder_bridge_message_sample([GlobalConsensus(Polkadot), Parachain(42)].into())) } fn prepare_message_delivery_proof( - params: MessageDeliveryProofParams, - ) -> bridge_to_kusama_config::ToKusamaBridgeHubMessagesDeliveryProof { + params: MessageDeliveryProofParams>, + ) -> bridge_to_kusama_config::ToKusamaBridgeHubMessagesDeliveryProof { + let _ = bridge_to_kusama_config::open_bridge_for_benchmarks::< + Runtime, + bridge_to_kusama_config::XcmOverBridgeHubKusamaInstance, + xcm_config::LocationToAccountId, + >(params.lane, 42); prepare_message_delivery_proof_from_parachain::< Runtime, bridge_to_kusama_config::BridgeGrandpaKusamaInstance, - bridge_to_kusama_config::WithBridgeHubKusamaMessageBridge, + bridge_to_kusama_config::WithBridgeHubKusamaMessagesInstance, >(params) }