From e36add3efafc18153ad7a846356375a8e3f9ae4f Mon Sep 17 00:00:00 2001 From: claravanstaden Date: Fri, 3 Nov 2023 13:49:12 +0200 Subject: [PATCH] adds tests --- .../bridge-hub-rococo/tests/tests.rs | 22 +++++ .../bridge-hubs/test-utils/Cargo.toml | 8 ++ .../bridge-hubs/test-utils/src/test_cases.rs | 88 ++++++++++++++++++- 3 files changed, 117 insertions(+), 1 deletion(-) diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/tests/tests.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/tests/tests.rs index 0109e4de4a9a..a84492d3dfba 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/tests/tests.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-rococo/tests/tests.rs @@ -103,6 +103,7 @@ mod bridge_hub_rococo_tests { WithBridgeHubWococoMessageBridge, WithBridgeHubWococoMessagesInstance, DEFAULT_XCM_LANE_TO_BRIDGE_HUB_WOCOCO, }; + use sp_core::H160; bridge_hub_test_utils::test_cases::include_teleports_for_native_asset_works!( Runtime, @@ -296,6 +297,27 @@ mod bridge_hub_rococo_tests { max_expected ); } + + #[test] + pub fn transfer_token_to_ethereum_works() { + bridge_hub_test_utils::test_cases::handle_transfer_token_message::< + Runtime, + XcmConfig, + >( + collator_session_keys(), + bp_bridge_hub_rococo::BRIDGE_HUB_ROCOCO_PARACHAIN_ID, + bp_bridge_hub_rococo::BRIDGE_HUB_ROCOCO_PARACHAIN_ID, + bridge_hub_rococo_runtime::GatewayAddress::get(), + H160::random(), + H160::random(), + Box::new(|runtime_event_encoded: Vec| { + match RuntimeEvent::decode(&mut &runtime_event_encoded[..]) { + Ok(RuntimeEvent::EthereumOutboundQueue(event)) => Some(event), + _ => None, + } + }) + ) + } } mod bridge_hub_wococo_tests { diff --git a/cumulus/parachains/runtimes/bridge-hubs/test-utils/Cargo.toml b/cumulus/parachains/runtimes/bridge-hubs/test-utils/Cargo.toml index 2ad79bb24886..b541f49fb59a 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/test-utils/Cargo.toml +++ b/cumulus/parachains/runtimes/bridge-hubs/test-utils/Cargo.toml @@ -56,6 +56,11 @@ pallet-bridge-messages = { path = "../../../../../bridges/modules/messages", def pallet-bridge-relayers = { path = "../../../../../bridges/modules/relayers", default-features = false } bridge-runtime-common = { path = "../../../../../bridges/bin/runtime-common", default-features = false } +# Ethereum Bridge (Snowbridge) +snowbridge-core = { path = "../../../../../../parachain/primitives/core", default-features = false } +snowbridge-outbound-queue = { path = "../../../../../../parachain/pallets/outbound-queue", default-features = false } +snowbridge-control = { path = "../../../../../../parachain/pallets/control", default-features = false } + [features] default = [ "std" ] std = [ @@ -98,4 +103,7 @@ std = [ "xcm-builder/std", "xcm-executor/std", "xcm/std", + "snowbridge-core/std", + "snowbridge-outbound-queue/std", + "snowbridge-control/std", ] diff --git a/cumulus/parachains/runtimes/bridge-hubs/test-utils/src/test_cases.rs b/cumulus/parachains/runtimes/bridge-hubs/test-utils/src/test_cases.rs index e77af189b4fd..d93f2893c680 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/test-utils/src/test_cases.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/test-utils/src/test_cases.rs @@ -44,7 +44,7 @@ use parachains_runtimes_test_utils::{ mock_open_hrmp_channel, AccountIdOf, BalanceOf, CollatorSessionKeys, ExtBuilder, ValidatorIdOf, XcmReceivedFrom, }; -use sp_core::H256; +use sp_core::{H160, H256}; use sp_keyring::AccountKeyring::*; use sp_runtime::{ traits::{Header as HeaderT, Zero}, @@ -950,6 +950,92 @@ where estimated_fee.into() } +pub fn handle_transfer_token_message< + Runtime, + XcmConfig, +>( + collator_session_key: CollatorSessionKeys, + runtime_para_id: u32, + bridghub_parachain_id: u32, + gateway_proxy_address: H160, + weth_contract_address: H160, + destination_contract: H160, + snowbridge_outbound_queue: Box< + dyn Fn(Vec) -> Option>, + >, +) where + Runtime: frame_system::Config + + pallet_balances::Config + + pallet_session::Config + + pallet_xcm::Config + + parachain_info::Config + + pallet_collator_selection::Config + + cumulus_pallet_dmp_queue::Config + + cumulus_pallet_parachain_system::Config + + snowbridge_outbound_queue::Config, + XcmConfig: xcm_executor::Config, + ValidatorIdOf: From>, +{ + let bridgehub_parachain_location = MultiLocation::new(1, Parachain(bridghub_parachain_id)); + + ExtBuilder::::default() + .with_collators(collator_session_key.collators()) + .with_session_keys(collator_session_key.session_keys()) + .with_para_id(runtime_para_id.into()) + .with_tracing() + .build() + .execute_with(|| { + let assets = vec![MultiAsset { + id: Concrete(MultiLocation { + parents: 0, + interior: X2(AccountKey20{ network: None, key: gateway_proxy_address.into()}, AccountKey20{ network: None, key: weth_contract_address.into() }), + }), + fun: Fungible(1000000000), + }]; + + let inner_xcm = Xcm(vec![ + UnpaidExecution { weight_limit: Unlimited, check_origin: None }, + WithdrawAsset(MultiAssets::from(assets.clone())), + DepositAsset { + assets: MultiAssetFilter::from(assets), + beneficiary: MultiLocation { + parents: 0, + interior: X1(AccountKey20{ network: None, key: destination_contract.into()}), + } + }, + SetTopic([0; 32]) + ]); + + // prepare transfer token message + let xcm = Xcm(vec![ + UnpaidExecution { weight_limit: Unlimited, check_origin: None }, + ExportMessage { + network: Ethereum { chain_id: 15 }, + destination: Here, + xcm: inner_xcm + } + ]); + + // execute XCM + let hash = xcm.using_encoded(sp_io::hashing::blake2_256); + assert_ok!(XcmExecutor::::execute_xcm( + bridgehub_parachain_location, + xcm, + hash, + RuntimeHelper::::xcm_max_weight(XcmReceivedFrom::Sibling), + ) + .ensure_complete()); + + // check events + let mut events = >::events() + .into_iter() + .filter_map(|e| snowbridge_outbound_queue(e.event.encode())); + assert!( + events.any(|e| matches!(e, snowbridge_outbound_queue::Event::MessageQueued { .. })) + ); + }); +} + pub mod test_data { use super::*; use bp_header_chain::justification::GrandpaJustification;