From 50f5d5bc9dac257eee4a7dbf4a50813f8e401a45 Mon Sep 17 00:00:00 2001 From: Branislav Kontur Date: Thu, 25 Jan 2024 14:49:54 +0100 Subject: [PATCH] [bridges/xcm] adapt https://github.com/paritytech/polkadot-sdk/pull/2481 (XCM version check) + https://github.com/paritytech/polkadot-sdk/pull/2692 --- Cargo.lock | 1 - .../asset-hubs/asset-hub-kusama/src/lib.rs | 20 ++++- .../asset-hub-kusama/tests/tests.rs | 12 ++- .../asset-hubs/asset-hub-polkadot/src/lib.rs | 20 ++++- .../asset-hub-polkadot/tests/tests.rs | 12 ++- .../src/bridge_to_polkadot_config.rs | 26 +++++- .../bridge-hubs/bridge-hub-kusama/src/lib.rs | 15 ++++ .../bridge-hub-kusama/tests/tests.rs | 82 ++++++++----------- .../src/bridge_to_kusama_config.rs | 25 +++++- .../bridge-hub-polkadot/src/lib.rs | 15 ++++ .../bridge-hub-polkadot/tests/tests.rs | 78 ++++++++---------- 11 files changed, 195 insertions(+), 111 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0df9de34f3..18f542ddf0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3380,7 +3380,6 @@ dependencies = [ "staging-xcm-builder 4.0.0", "staging-xcm-executor 4.0.0", "substrate-wasm-builder 14.0.0", - "system-parachains-constants", ] [[package]] diff --git a/system-parachains/asset-hubs/asset-hub-kusama/src/lib.rs b/system-parachains/asset-hubs/asset-hub-kusama/src/lib.rs index fbb81301d5..f1eb53f198 100644 --- a/system-parachains/asset-hubs/asset-hub-kusama/src/lib.rs +++ b/system-parachains/asset-hubs/asset-hub-kusama/src/lib.rs @@ -881,6 +881,7 @@ impl pallet_xcm_bridge_hub_router::Config for Runti type UniversalLocation = xcm_config::UniversalLocation; type BridgedNetworkId = xcm_config::bridging::to_polkadot::PolkadotNetwork; type Bridges = xcm_config::bridging::NetworkExportTable; + type DestinationVersion = PolkadotXcm; #[cfg(not(feature = "runtime-benchmarks"))] type BridgeHubOrigin = EnsureXcm>; @@ -1535,11 +1536,26 @@ impl_runtime_apis! { ); } - fn ensure_bridged_target_destination() -> MultiLocation { + fn ensure_bridged_target_destination() -> Result { ParachainSystem::open_outbound_hrmp_channel_for_benchmarks_or_tests( xcm_config::bridging::SiblingBridgeHubParaId::get().into() ); - xcm_config::bridging::to_polkadot::AssetHubPolkadot::get() + let bridged_asset_hub = xcm_config::bridging::to_polkadot::AssetHubPolkadot::get(); + let _ = PolkadotXcm::force_xcm_version( + RuntimeOrigin::root(), + Box::new(bridged_asset_hub), + XCM_VERSION, + ).map_err(|e| { + log::error!( + "Failed to dispatch `force_xcm_version({:?}, {:?}, {:?})`, error: {:?}", + RuntimeOrigin::root(), + bridged_asset_hub, + XCM_VERSION, + e + ); + BenchmarkError::Stop("XcmVersion was not stored!") + })?; + Ok(bridged_asset_hub) } } diff --git a/system-parachains/asset-hubs/asset-hub-kusama/tests/tests.rs b/system-parachains/asset-hubs/asset-hub-kusama/tests/tests.rs index 603e3ea8ca..1d2b5daa0d 100644 --- a/system-parachains/asset-hubs/asset-hub-kusama/tests/tests.rs +++ b/system-parachains/asset-hubs/asset-hub-kusama/tests/tests.rs @@ -25,9 +25,9 @@ use asset_hub_kusama_runtime::{ TrustBackedAssetsPalletLocation, XcmConfig, }, AllPalletsWithoutSystem, AssetDeposit, Assets, Balances, ExistentialDeposit, ForeignAssets, - ForeignAssetsInstance, MetadataDepositBase, MetadataDepositPerByte, ParachainSystem, Runtime, - RuntimeCall, RuntimeEvent, SessionKeys, ToPolkadotXcmRouterInstance, TrustBackedAssetsInstance, - XcmpQueue, + ForeignAssetsInstance, MetadataDepositBase, MetadataDepositPerByte, ParachainSystem, + PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, SessionKeys, + ToPolkadotXcmRouterInstance, TrustBackedAssetsInstance, XcmpQueue, }; use asset_test_utils::{ test_cases_over_bridge::TestBridgingConfig, CollatorSessionKey, CollatorSessionKeys, ExtBuilder, @@ -657,6 +657,12 @@ asset_test_utils::include_create_and_manage_foreign_assets_for_local_consensus_p ); fn bridging_to_asset_hub_polkadot() -> TestBridgingConfig { + let _ = PolkadotXcm::force_xcm_version( + RuntimeOrigin::root(), + Box::new(bridging::to_polkadot::AssetHubPolkadot::get()), + XCM_VERSION, + ) + .expect("version saved!"); TestBridgingConfig { bridged_network: bridging::to_polkadot::PolkadotNetwork::get(), local_bridge_hub_para_id: bridging::SiblingBridgeHubParaId::get(), diff --git a/system-parachains/asset-hubs/asset-hub-polkadot/src/lib.rs b/system-parachains/asset-hubs/asset-hub-polkadot/src/lib.rs index a400336467..7871e3b4bd 100644 --- a/system-parachains/asset-hubs/asset-hub-polkadot/src/lib.rs +++ b/system-parachains/asset-hubs/asset-hub-polkadot/src/lib.rs @@ -792,6 +792,7 @@ impl pallet_xcm_bridge_hub_router::Config for Runtime type UniversalLocation = xcm_config::UniversalLocation; type BridgedNetworkId = xcm_config::bridging::to_kusama::KusamaNetwork; type Bridges = xcm_config::bridging::NetworkExportTable; + type DestinationVersion = PolkadotXcm; #[cfg(not(feature = "runtime-benchmarks"))] type BridgeHubOrigin = EnsureXcm>; @@ -1420,11 +1421,26 @@ impl_runtime_apis! { ); } - fn ensure_bridged_target_destination() -> MultiLocation { + fn ensure_bridged_target_destination() -> Result { ParachainSystem::open_outbound_hrmp_channel_for_benchmarks_or_tests( xcm_config::bridging::SiblingBridgeHubParaId::get().into() ); - xcm_config::bridging::to_kusama::AssetHubKusama::get() + let bridged_asset_hub = xcm_config::bridging::to_kusama::AssetHubKusama::get(); + let _ = PolkadotXcm::force_xcm_version( + RuntimeOrigin::root(), + Box::new(bridged_asset_hub), + XCM_VERSION, + ).map_err(|e| { + log::error!( + "Failed to dispatch `force_xcm_version({:?}, {:?}, {:?})`, error: {:?}", + RuntimeOrigin::root(), + bridged_asset_hub, + XCM_VERSION, + e + ); + BenchmarkError::Stop("XcmVersion was not stored!") + })?; + Ok(bridged_asset_hub) } } diff --git a/system-parachains/asset-hubs/asset-hub-polkadot/tests/tests.rs b/system-parachains/asset-hubs/asset-hub-polkadot/tests/tests.rs index b9bb25e243..5b36c1ed18 100644 --- a/system-parachains/asset-hubs/asset-hub-polkadot/tests/tests.rs +++ b/system-parachains/asset-hubs/asset-hub-polkadot/tests/tests.rs @@ -25,9 +25,9 @@ use asset_hub_polkadot_runtime::{ TrustBackedAssetsPalletLocation, XcmConfig, }, AllPalletsWithoutSystem, AssetDeposit, Assets, Balances, ExistentialDeposit, ForeignAssets, - ForeignAssetsInstance, MetadataDepositBase, MetadataDepositPerByte, ParachainSystem, Runtime, - RuntimeCall, RuntimeEvent, SessionKeys, ToKusamaXcmRouterInstance, TrustBackedAssetsInstance, - XcmpQueue, + ForeignAssetsInstance, MetadataDepositBase, MetadataDepositPerByte, ParachainSystem, + PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, SessionKeys, + ToKusamaXcmRouterInstance, TrustBackedAssetsInstance, XcmpQueue, }; use asset_test_utils::{ test_cases_over_bridge::TestBridgingConfig, CollatorSessionKey, CollatorSessionKeys, ExtBuilder, @@ -683,6 +683,12 @@ asset_test_utils::include_create_and_manage_foreign_assets_for_local_consensus_p ); fn bridging_to_asset_hub_kusama() -> TestBridgingConfig { + let _ = PolkadotXcm::force_xcm_version( + RuntimeOrigin::root(), + Box::new(bridging::to_kusama::AssetHubKusama::get()), + XCM_VERSION, + ) + .expect("version saved!"); TestBridgingConfig { bridged_network: bridging::to_kusama::KusamaNetwork::get(), local_bridge_hub_para_id: bridging::SiblingBridgeHubParaId::get(), 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 4d2cab6726..e8a72e6658 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 @@ -19,8 +19,8 @@ use crate::{ weights, xcm_config::{UniversalLocation, XcmRouter}, - AccountId, Balance, Balances, BlockNumber, BridgePolkadotMessages, Runtime, RuntimeEvent, - RuntimeOrigin, XcmOverBridgeHubPolkadot, + AccountId, Balance, Balances, BlockNumber, BridgePolkadotMessages, PolkadotXcm, Runtime, + RuntimeEvent, RuntimeOrigin, XcmOverBridgeHubPolkadot, }; use bp_messages::LaneId; use bp_parachains::SingleParaStoredHeaderDataBuilder; @@ -33,7 +33,7 @@ use bridge_runtime_common::{ }, messages_xcm_extension::{ SenderAndLane, XcmAsPlainPayload, XcmBlobHauler, XcmBlobHaulerAdapter, - XcmBlobMessageDispatch, + XcmBlobMessageDispatch, XcmVersionOfDestAndRemoteBridge, }, refund_relayer_extension::{ ActualFeeRefund, RefundBridgedParachainMessages, RefundSignedExtensionAdapter, @@ -75,6 +75,11 @@ parameter_types! { parameter_types! { /// Polkadot Network identifier. pub PolkadotGlobalConsensusNetwork: NetworkId = NetworkId::Polkadot; + /// Polkadot Network as `Location`. + pub PolkadotGlobalConsensusNetworkLocation: MultiLocation = MultiLocation { + parents: 2, + interior: X1(GlobalConsensus(PolkadotGlobalConsensusNetwork::get())) + }; /// Interior location (relative to this runtime) of the with-Polkadot messages pallet. pub BridgeKusamaToPolkadotMessagesPalletInstance: InteriorMultiLocation = X1( PalletInstance(::index() as u8), @@ -84,6 +89,14 @@ parameter_types! { 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: MultiLocation = MultiLocation { + parents: 2, + interior: X2( + GlobalConsensus(PolkadotGlobalConsensusNetwork::get()), + Parachain(::PARACHAIN_ID) + ) + }; /// A route (XCM location and bridge lane) that the Kusama Asset Hub -> Polkadot Asset Hub /// message is following. @@ -257,9 +270,11 @@ impl XcmBlobHauler for ToBridgeHubPolkadotXcmBlobHauler { pub type XcmOverBridgeHubPolkadotInstance = pallet_xcm_bridge_hub::Instance1; impl pallet_xcm_bridge_hub::Config for Runtime { type UniversalLocation = UniversalLocation; - type BridgedNetworkId = PolkadotGlobalConsensusNetwork; + type BridgedNetwork = PolkadotGlobalConsensusNetworkLocation; type BridgeMessagesPalletInstance = WithBridgeHubPolkadotMessagesInstance; type MessageExportPrice = (); + type DestinationVersion = + XcmVersionOfDestAndRemoteBridge; type Lanes = ActiveLanes; type LanesSupport = ToBridgeHubPolkadotXcmBlobHauler; } @@ -418,5 +433,8 @@ mod tests { bp_bridge_hub_kusama::WITH_BRIDGE_KUSAMA_TO_POLKADOT_MESSAGES_PALLET_INDEX )) ); + + assert!(BridgeHubPolkadotLocation::get() + .starts_with(&PolkadotGlobalConsensusNetworkLocation::get())); } } 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 590ff75f17..c100836d92 100644 --- a/system-parachains/bridge-hubs/bridge-hub-kusama/src/lib.rs +++ b/system-parachains/bridge-hubs/bridge-hub-kusama/src/lib.rs @@ -940,6 +940,21 @@ impl_runtime_apis! { fn export_message_origin_and_destination( ) -> Result<(MultiLocation, NetworkId, InteriorMultiLocation), BenchmarkError> { + // save XCM version for remote bridge hub + let _ = PolkadotXcm::force_xcm_version( + RuntimeOrigin::root(), + Box::new(bridge_to_polkadot_config::BridgeHubPolkadotLocation::get()), + XCM_VERSION, + ).map_err(|e| { + log::error!( + "Failed to dispatch `force_xcm_version({:?}, {:?}, {:?})`, error: {:?}", + RuntimeOrigin::root(), + bridge_to_polkadot_config::BridgeHubPolkadotLocation::get(), + XCM_VERSION, + e + ); + BenchmarkError::Stop("XcmVersion was not stored!") + })?; Ok( ( bridge_to_polkadot_config::FromAssetHubKusamaToAssetHubPolkadotRoute::get().location, diff --git a/system-parachains/bridge-hubs/bridge-hub-kusama/tests/tests.rs b/system-parachains/bridge-hubs/bridge-hub-kusama/tests/tests.rs index b9a0d64516..bf614b2096 100644 --- a/system-parachains/bridge-hubs/bridge-hub-kusama/tests/tests.rs +++ b/system-parachains/bridge-hubs/bridge-hub-kusama/tests/tests.rs @@ -18,25 +18,26 @@ use bp_polkadot_core::Signature; use bridge_hub_kusama_runtime::{ bridge_to_polkadot_config::{ AssetHubPolkadotParaId, BridgeGrandpaPolkadotInstance, BridgeHubPolkadotChainId, - BridgeParachainPolkadotInstance, DeliveryRewardInBalance, PolkadotGlobalConsensusNetwork, - RefundBridgeHubPolkadotMessages, RequiredStakeForStakeAndSlash, - WithBridgeHubPolkadotMessageBridge, WithBridgeHubPolkadotMessagesInstance, - XCM_LANE_FOR_ASSET_HUB_KUSAMA_TO_ASSET_HUB_POLKADOT, + BridgeHubPolkadotLocation, BridgeParachainPolkadotInstance, DeliveryRewardInBalance, + PolkadotGlobalConsensusNetwork, RefundBridgeHubPolkadotMessages, + RequiredStakeForStakeAndSlash, WithBridgeHubPolkadotMessageBridge, + WithBridgeHubPolkadotMessagesInstance, XCM_LANE_FOR_ASSET_HUB_KUSAMA_TO_ASSET_HUB_POLKADOT, }, xcm_config::{KsmRelayLocation, RelayNetwork, XcmConfig}, AllPalletsWithoutSystem, BridgeRejectObsoleteHeadersAndMessages, Executive, ExistentialDeposit, - ParachainSystem, PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, SessionKeys, SignedExtra, - UncheckedExtrinsic, + ParachainSystem, PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, SessionKeys, + SignedExtra, UncheckedExtrinsic, }; +use bridge_hub_test_utils::test_cases::from_parachain; use codec::{Decode, Encode}; -use frame_support::parameter_types; -use frame_system::pallet_prelude::HeaderFor; -use parachains_common::{kusama::fee::WeightToFee, AccountId, AuraId, Balance}; +use frame_support::{parameter_types, traits::ConstU8}; +use parachains_common::{AccountId, AuraId, Balance}; use sp_keyring::AccountKeyring::Alice; use sp_runtime::{ generic::{Era, SignedPayload}, AccountId32, }; +use system_parachains_constants::kusama::fee::WeightToFee; use xcm::latest::prelude::*; const ALICE: [u8; 32] = [1u8; 32]; @@ -44,6 +45,16 @@ const ALICE: [u8; 32] = [1u8; 32]; // Para id of sibling chain used in tests. pub const SIBLING_PARACHAIN_ID: u32 = 1000; +// Runtime from tests PoV +type RuntimeTestsAdapter = from_parachain::WithRemoteParachainHelperAdapter< + Runtime, + AllPalletsWithoutSystem, + BridgeGrandpaPolkadotInstance, + BridgeParachainPolkadotInstance, + WithBridgeHubPolkadotMessagesInstance, + WithBridgeHubPolkadotMessageBridge, +>; + parameter_types! { pub CheckingAccount: AccountId = PolkadotXcm::check_account(); } @@ -52,13 +63,16 @@ fn construct_extrinsic( sender: sp_keyring::AccountKeyring, call: RuntimeCall, ) -> UncheckedExtrinsic { + let account_id = AccountId32::from(sender.public()); let extra: SignedExtra = ( frame_system::CheckNonZeroSender::::new(), frame_system::CheckSpecVersion::::new(), frame_system::CheckTxVersion::::new(), frame_system::CheckGenesis::::new(), frame_system::CheckEra::::from(Era::immortal()), - frame_system::CheckNonce::::from(0), + frame_system::CheckNonce::::from( + frame_system::Pallet::::account(&account_id).nonce, + ), frame_system::CheckWeight::::new(), pallet_transaction_payment::ChargeTransactionPayment::::from(0), BridgeRejectObsoleteHeadersAndMessages, @@ -68,7 +82,7 @@ fn construct_extrinsic( let signature = payload.using_encoded(|e| sender.sign(e)); UncheckedExtrinsic::new_signed( call, - AccountId32::from(sender.public()).into(), + account_id.into(), Signature::Sr25519(signature.clone()), extra, ) @@ -76,18 +90,13 @@ fn construct_extrinsic( fn construct_and_apply_extrinsic( relayer_at_target: sp_keyring::AccountKeyring, - batch: pallet_utility::Call, + call: RuntimeCall, ) -> sp_runtime::DispatchOutcome { - let batch_call = RuntimeCall::Utility(batch); - let xt = construct_extrinsic(relayer_at_target, batch_call); + let xt = construct_extrinsic(relayer_at_target, call); let r = Executive::apply_extrinsic(xt); r.unwrap() } -fn executive_init_block(header: &HeaderFor) { - Executive::initialize_block(header) -} - fn collator_session_keys() -> bridge_hub_test_utils::CollatorSessionKeys { bridge_hub_test_utils::CollatorSessionKeys::new( AccountId::from(Alice), @@ -130,11 +139,7 @@ fn initialize_bridge_by_governance_works() { bridge_hub_test_utils::test_cases::initialize_bridge_by_governance_works::< Runtime, BridgeGrandpaPolkadotInstance, - >( - collator_session_keys(), - bp_bridge_hub_kusama::BRIDGE_HUB_KUSAMA_PARACHAIN_ID, - Box::new(|call| RuntimeCall::BridgePolkadotGrandpa(call).encode()), - ) + >(collator_session_keys(), bp_bridge_hub_kusama::BRIDGE_HUB_KUSAMA_PARACHAIN_ID) } #[test] @@ -188,7 +193,7 @@ fn handle_export_message_from_system_parachain_add_to_outbound_queue_works() { Some((KsmRelayLocation::get(), ExistentialDeposit::get()).into()), // value should be >= than value generated by `can_calculate_weight_for_paid_export_message_with_reserve_transfer` Some((KsmRelayLocation::get(), bp_bridge_hub_kusama::BridgeHubKusamaBaseXcmFeeInKsms::get()).into()), - || (), + || PolkadotXcm::force_xcm_version(RuntimeOrigin::root(), Box::new(BridgeHubPolkadotLocation::get()), XCM_VERSION).expect("version saved!"), ) } @@ -202,6 +207,7 @@ fn message_dispatch_routing_works() { WithBridgeHubPolkadotMessagesInstance, RelayNetwork, PolkadotGlobalConsensusNetwork, + ConstU8<2>, >( collator_session_keys(), bp_bridge_hub_kusama::BRIDGE_HUB_KUSAMA_PARACHAIN_ID, @@ -225,38 +231,22 @@ fn message_dispatch_routing_works() { #[test] fn relayed_incoming_message_works() { - bridge_hub_test_utils::test_cases::relayed_incoming_message_works::< - Runtime, - AllPalletsWithoutSystem, - XcmConfig, - ParachainSystem, - BridgeGrandpaPolkadotInstance, - BridgeParachainPolkadotInstance, - WithBridgeHubPolkadotMessagesInstance, - WithBridgeHubPolkadotMessageBridge, - >( + from_parachain::relayed_incoming_message_works::( collator_session_keys(), bp_bridge_hub_kusama::BRIDGE_HUB_KUSAMA_PARACHAIN_ID, bp_bridge_hub_polkadot::BRIDGE_HUB_POLKADOT_PARACHAIN_ID, + BridgeHubPolkadotChainId::get(), SIBLING_PARACHAIN_ID, Kusama, XCM_LANE_FOR_ASSET_HUB_KUSAMA_TO_ASSET_HUB_POLKADOT, || (), + construct_and_apply_extrinsic, ) } #[test] pub fn complex_relay_extrinsic_works() { - bridge_hub_test_utils::test_cases::complex_relay_extrinsic_works::< - Runtime, - AllPalletsWithoutSystem, - XcmConfig, - ParachainSystem, - BridgeGrandpaPolkadotInstance, - BridgeParachainPolkadotInstance, - WithBridgeHubPolkadotMessagesInstance, - WithBridgeHubPolkadotMessageBridge, - >( + from_parachain::complex_relay_extrinsic_works::( collator_session_keys(), bp_bridge_hub_kusama::BRIDGE_HUB_KUSAMA_PARACHAIN_ID, bp_bridge_hub_polkadot::BRIDGE_HUB_POLKADOT_PARACHAIN_ID, @@ -264,10 +254,8 @@ pub fn complex_relay_extrinsic_works() { BridgeHubPolkadotChainId::get(), Kusama, XCM_LANE_FOR_ASSET_HUB_KUSAMA_TO_ASSET_HUB_POLKADOT, - ExistentialDeposit::get(), - executive_init_block, - construct_and_apply_extrinsic, || (), + construct_and_apply_extrinsic, ); } 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 3f43e4c4c3..7463658756 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 @@ -19,8 +19,8 @@ use crate::{ weights, xcm_config::{UniversalLocation, XcmRouter}, - AccountId, Balance, Balances, BlockNumber, BridgeKusamaMessages, Runtime, RuntimeEvent, - RuntimeOrigin, XcmOverBridgeHubKusama, + AccountId, Balance, Balances, BlockNumber, BridgeKusamaMessages, PolkadotXcm, Runtime, + RuntimeEvent, RuntimeOrigin, XcmOverBridgeHubKusama, }; use bp_messages::LaneId; use bp_parachains::SingleParaStoredHeaderDataBuilder; @@ -33,7 +33,7 @@ use bridge_runtime_common::{ }, messages_xcm_extension::{ SenderAndLane, XcmAsPlainPayload, XcmBlobHauler, XcmBlobHaulerAdapter, - XcmBlobMessageDispatch, + XcmBlobMessageDispatch, XcmVersionOfDestAndRemoteBridge, }, refund_relayer_extension::{ ActualFeeRefund, RefundBridgedParachainMessages, RefundSignedExtensionAdapter, @@ -75,6 +75,11 @@ parameter_types! { parameter_types! { /// Kusama Network identifier. pub KusamaGlobalConsensusNetwork: NetworkId = NetworkId::Kusama; + /// Kusama Network as `Location`. + pub KusamaGlobalConsensusNetworkLocation: MultiLocation = MultiLocation { + parents: 2, + interior: X1(GlobalConsensus(KusamaGlobalConsensusNetwork::get())) + }; /// Interior location (relative to this runtime) of the with-Kusama messages pallet. pub BridgePolkadotToKusamaMessagesPalletInstance: InteriorMultiLocation = X1( PalletInstance(::index() as u8), @@ -84,6 +89,14 @@ parameter_types! { 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: MultiLocation = MultiLocation { + parents: 2, + interior: X2( + GlobalConsensus(KusamaGlobalConsensusNetwork::get()), + Parachain(::PARACHAIN_ID) + ) + }; /// A route (XCM location and bridge lane) that the Polkadot Asset Hub -> Kusama Asset Hub /// message is following. @@ -257,9 +270,10 @@ impl XcmBlobHauler for ToBridgeHubKusamaXcmBlobHauler { pub type XcmOverBridgeHubKusamaInstance = pallet_xcm_bridge_hub::Instance1; impl pallet_xcm_bridge_hub::Config for Runtime { type UniversalLocation = UniversalLocation; - type BridgedNetworkId = KusamaGlobalConsensusNetwork; + type BridgedNetwork = KusamaGlobalConsensusNetworkLocation; type BridgeMessagesPalletInstance = WithBridgeHubKusamaMessagesInstance; type MessageExportPrice = (); + type DestinationVersion = XcmVersionOfDestAndRemoteBridge; type Lanes = ActiveLanes; type LanesSupport = ToBridgeHubKusamaXcmBlobHauler; } @@ -414,5 +428,8 @@ mod tests { bp_bridge_hub_polkadot::WITH_BRIDGE_POLKADOT_TO_KUSAMA_MESSAGES_PALLET_INDEX )) ); + + assert!(BridgeHubKusamaLocation::get() + .starts_with(&KusamaGlobalConsensusNetworkLocation::get())); } } 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 3fe54ef14e..872073f261 100644 --- a/system-parachains/bridge-hubs/bridge-hub-polkadot/src/lib.rs +++ b/system-parachains/bridge-hubs/bridge-hub-polkadot/src/lib.rs @@ -940,6 +940,21 @@ impl_runtime_apis! { fn export_message_origin_and_destination( ) -> Result<(MultiLocation, NetworkId, InteriorMultiLocation), BenchmarkError> { + // save XCM version for remote bridge hub + let _ = PolkadotXcm::force_xcm_version( + RuntimeOrigin::root(), + Box::new(bridge_to_kusama_config::BridgeHubKusamaLocation::get()), + XCM_VERSION, + ).map_err(|e| { + log::error!( + "Failed to dispatch `force_xcm_version({:?}, {:?}, {:?})`, error: {:?}", + RuntimeOrigin::root(), + bridge_to_kusama_config::BridgeHubKusamaLocation::get(), + XCM_VERSION, + e + ); + BenchmarkError::Stop("XcmVersion was not stored!") + })?; Ok( ( bridge_to_kusama_config::FromAssetHubPolkadotToAssetHubKusamaRoute::get().location, diff --git a/system-parachains/bridge-hubs/bridge-hub-polkadot/tests/tests.rs b/system-parachains/bridge-hubs/bridge-hub-polkadot/tests/tests.rs index ba7a5c2d87..c3a3517318 100644 --- a/system-parachains/bridge-hubs/bridge-hub-polkadot/tests/tests.rs +++ b/system-parachains/bridge-hubs/bridge-hub-polkadot/tests/tests.rs @@ -18,25 +18,26 @@ use bp_polkadot_core::Signature; use bridge_hub_polkadot_runtime::{ bridge_to_kusama_config::{ AssetHubKusamaParaId, BridgeGrandpaKusamaInstance, BridgeHubKusamaChainId, - BridgeParachainKusamaInstance, DeliveryRewardInBalance, KusamaGlobalConsensusNetwork, - RefundBridgeHubKusamaMessages, RequiredStakeForStakeAndSlash, + BridgeHubKusamaLocation, BridgeParachainKusamaInstance, DeliveryRewardInBalance, + KusamaGlobalConsensusNetwork, RefundBridgeHubKusamaMessages, RequiredStakeForStakeAndSlash, WithBridgeHubKusamaMessageBridge, WithBridgeHubKusamaMessagesInstance, XCM_LANE_FOR_ASSET_HUB_POLKADOT_TO_ASSET_HUB_KUSAMA, }, xcm_config::{DotRelayLocation, RelayNetwork, XcmConfig}, AllPalletsWithoutSystem, BridgeRejectObsoleteHeadersAndMessages, Executive, ExistentialDeposit, - ParachainSystem, PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, SessionKeys, SignedExtra, - UncheckedExtrinsic, + ParachainSystem, PolkadotXcm, Runtime, RuntimeCall, RuntimeEvent, RuntimeOrigin, SessionKeys, + SignedExtra, UncheckedExtrinsic, }; +use bridge_hub_test_utils::test_cases::from_parachain; use codec::{Decode, Encode}; -use frame_support::parameter_types; -use frame_system::pallet_prelude::HeaderFor; -use parachains_common::{polkadot::fee::WeightToFee, AccountId, AuraId, Balance}; +use frame_support::{parameter_types, traits::ConstU8}; +use parachains_common::{AccountId, AuraId, Balance}; use sp_keyring::AccountKeyring::Alice; use sp_runtime::{ generic::{Era, SignedPayload}, AccountId32, }; +use system_parachains_constants::polkadot::fee::WeightToFee; use xcm::latest::prelude::*; const ALICE: [u8; 32] = [1u8; 32]; @@ -44,6 +45,16 @@ const ALICE: [u8; 32] = [1u8; 32]; // Para id of sibling chain used in tests. pub const SIBLING_PARACHAIN_ID: u32 = 1000; +// Runtime from tests PoV +type RuntimeTestsAdapter = from_parachain::WithRemoteParachainHelperAdapter< + Runtime, + AllPalletsWithoutSystem, + BridgeGrandpaKusamaInstance, + BridgeParachainKusamaInstance, + WithBridgeHubKusamaMessagesInstance, + WithBridgeHubKusamaMessageBridge, +>; + parameter_types! { pub CheckingAccount: AccountId = PolkadotXcm::check_account(); } @@ -52,13 +63,16 @@ fn construct_extrinsic( sender: sp_keyring::AccountKeyring, call: RuntimeCall, ) -> UncheckedExtrinsic { + let account_id = AccountId32::from(sender.public()); let extra: SignedExtra = ( frame_system::CheckNonZeroSender::::new(), frame_system::CheckSpecVersion::::new(), frame_system::CheckTxVersion::::new(), frame_system::CheckGenesis::::new(), frame_system::CheckEra::::from(Era::immortal()), - frame_system::CheckNonce::::from(0), + frame_system::CheckNonce::::from( + frame_system::Pallet::::account(&account_id).nonce, + ), frame_system::CheckWeight::::new(), pallet_transaction_payment::ChargeTransactionPayment::::from(0), BridgeRejectObsoleteHeadersAndMessages, @@ -68,7 +82,7 @@ fn construct_extrinsic( let signature = payload.using_encoded(|e| sender.sign(e)); UncheckedExtrinsic::new_signed( call, - AccountId32::from(sender.public()).into(), + account_id.into(), Signature::Sr25519(signature.clone()), extra, ) @@ -76,18 +90,13 @@ fn construct_extrinsic( fn construct_and_apply_extrinsic( relayer_at_target: sp_keyring::AccountKeyring, - batch: pallet_utility::Call, + call: RuntimeCall, ) -> sp_runtime::DispatchOutcome { - let batch_call = RuntimeCall::Utility(batch); - let xt = construct_extrinsic(relayer_at_target, batch_call); + let xt = construct_extrinsic(relayer_at_target, call); let r = Executive::apply_extrinsic(xt); r.unwrap() } -fn executive_init_block(header: &HeaderFor) { - Executive::initialize_block(header) -} - fn collator_session_keys() -> bridge_hub_test_utils::CollatorSessionKeys { bridge_hub_test_utils::CollatorSessionKeys::new( AccountId::from(Alice), @@ -130,11 +139,7 @@ fn initialize_bridge_by_governance_works() { bridge_hub_test_utils::test_cases::initialize_bridge_by_governance_works::< Runtime, BridgeGrandpaKusamaInstance, - >( - collator_session_keys(), - bp_bridge_hub_polkadot::BRIDGE_HUB_POLKADOT_PARACHAIN_ID, - Box::new(|call| RuntimeCall::BridgeKusamaGrandpa(call).encode()), - ) + >(collator_session_keys(), bp_bridge_hub_polkadot::BRIDGE_HUB_POLKADOT_PARACHAIN_ID) } #[test] @@ -188,7 +193,7 @@ fn handle_export_message_from_system_parachain_add_to_outbound_queue_works() { Some((DotRelayLocation::get(), ExistentialDeposit::get()).into()), // value should be >= than value generated by `can_calculate_weight_for_paid_export_message_with_reserve_transfer` Some((DotRelayLocation::get(), bp_bridge_hub_polkadot::BridgeHubPolkadotBaseXcmFeeInDots::get()).into()), - || (), + || PolkadotXcm::force_xcm_version(RuntimeOrigin::root(), Box::new(BridgeHubKusamaLocation::get()), XCM_VERSION).expect("version saved!"), ) } @@ -202,6 +207,7 @@ fn message_dispatch_routing_works() { WithBridgeHubKusamaMessagesInstance, RelayNetwork, KusamaGlobalConsensusNetwork, + ConstU8<2>, >( collator_session_keys(), bp_bridge_hub_polkadot::BRIDGE_HUB_POLKADOT_PARACHAIN_ID, @@ -225,38 +231,22 @@ fn message_dispatch_routing_works() { #[test] fn relayed_incoming_message_works() { - bridge_hub_test_utils::test_cases::relayed_incoming_message_works::< - Runtime, - AllPalletsWithoutSystem, - XcmConfig, - ParachainSystem, - BridgeGrandpaKusamaInstance, - BridgeParachainKusamaInstance, - WithBridgeHubKusamaMessagesInstance, - WithBridgeHubKusamaMessageBridge, - >( + from_parachain::relayed_incoming_message_works::( collator_session_keys(), bp_bridge_hub_polkadot::BRIDGE_HUB_POLKADOT_PARACHAIN_ID, bp_bridge_hub_kusama::BRIDGE_HUB_KUSAMA_PARACHAIN_ID, + BridgeHubKusamaChainId::get(), SIBLING_PARACHAIN_ID, Polkadot, XCM_LANE_FOR_ASSET_HUB_POLKADOT_TO_ASSET_HUB_KUSAMA, || (), + construct_and_apply_extrinsic, ) } #[test] pub fn complex_relay_extrinsic_works() { - bridge_hub_test_utils::test_cases::complex_relay_extrinsic_works::< - Runtime, - AllPalletsWithoutSystem, - XcmConfig, - ParachainSystem, - BridgeGrandpaKusamaInstance, - BridgeParachainKusamaInstance, - WithBridgeHubKusamaMessagesInstance, - WithBridgeHubKusamaMessageBridge, - >( + from_parachain::complex_relay_extrinsic_works::( collator_session_keys(), bp_bridge_hub_polkadot::BRIDGE_HUB_POLKADOT_PARACHAIN_ID, bp_bridge_hub_kusama::BRIDGE_HUB_KUSAMA_PARACHAIN_ID, @@ -264,10 +254,8 @@ pub fn complex_relay_extrinsic_works() { BridgeHubKusamaChainId::get(), Polkadot, XCM_LANE_FOR_ASSET_HUB_POLKADOT_TO_ASSET_HUB_KUSAMA, - ExistentialDeposit::get(), - executive_init_block, - construct_and_apply_extrinsic, || (), + construct_and_apply_extrinsic, ); }