From 30d0cf4242758c5558e9fd83c6c2c507692f2d52 Mon Sep 17 00:00:00 2001 From: claravanstaden Date: Mon, 9 Dec 2024 19:35:09 +0200 Subject: [PATCH] finish integration tests --- .../primitives/router/src/inbound/v2.rs | 24 +++++++-- .../src/tests/snowbridge.rs | 10 ++-- .../src/tests/snowbridge_v2.rs | 50 +++++++++---------- .../src/bridge_to_ethereum_config.rs | 2 + 4 files changed, 53 insertions(+), 33 deletions(-) diff --git a/bridges/snowbridge/primitives/router/src/inbound/v2.rs b/bridges/snowbridge/primitives/router/src/inbound/v2.rs index 0757869d3045..23b51eb4a5f7 100644 --- a/bridges/snowbridge/primitives/router/src/inbound/v2.rs +++ b/bridges/snowbridge/primitives/router/src/inbound/v2.rs @@ -55,8 +55,10 @@ pub enum Asset { /// Reason why a message conversion failed. #[derive(Copy, Clone, TypeInfo, PalletError, Encode, Decode, RuntimeDebug)] pub enum ConvertMessageError { - /// Invalid foreign ERC20 token ID + /// Invalid foreign ERC-20 token ID InvalidAsset, + /// Cannot reachor a foreign ERC-20 asset location. + CannotReanchor, } pub trait ConvertMessage { @@ -69,12 +71,16 @@ pub struct MessageToXcm< ConvertAssetId, WethAddress, GatewayProxyAddress, + EthereumUniversalLocation, + GlobalAssetHubLocation, > where EthereumNetwork: Get, InboundQueuePalletInstance: Get, ConvertAssetId: MaybeEquivalence, WethAddress: Get, GatewayProxyAddress: Get, + EthereumUniversalLocation: Get, + GlobalAssetHubLocation: Get, { _phantom: PhantomData<( EthereumNetwork, @@ -82,6 +88,8 @@ pub struct MessageToXcm< ConvertAssetId, WethAddress, GatewayProxyAddress, + EthereumUniversalLocation, + GlobalAssetHubLocation, )>, } @@ -91,6 +99,8 @@ impl< ConvertAssetId, WethAddress, GatewayProxyAddress, + EthereumUniversalLocation, + GlobalAssetHubLocation, > ConvertMessage for MessageToXcm< EthereumNetwork, @@ -98,6 +108,8 @@ impl< ConvertAssetId, WethAddress, GatewayProxyAddress, + EthereumUniversalLocation, + GlobalAssetHubLocation, > where EthereumNetwork: Get, @@ -105,6 +117,8 @@ where ConvertAssetId: MaybeEquivalence, WethAddress: Get, GatewayProxyAddress: Get, + EthereumUniversalLocation: Get, + GlobalAssetHubLocation: Get, { fn convert( message: Message, @@ -172,9 +186,13 @@ where reserve_assets.push(asset); }, Asset::ForeignTokenERC20 { token_id, value } => { - let asset_id = ConvertAssetId::convert(&token_id) + let asset_loc = ConvertAssetId::convert(&token_id) .ok_or(ConvertMessageError::InvalidAsset)?; - let asset: XcmAsset = (asset_id, *value).into(); + let mut reanchored_asset_loc = asset_loc.clone(); + reanchored_asset_loc + .reanchor(&GlobalAssetHubLocation::get(), &EthereumUniversalLocation::get()) + .map_err(|_| ConvertMessageError::CannotReanchor)?; + let asset: XcmAsset = (reanchored_asset_loc, *value).into(); withdraw_assets.push(asset); }, } diff --git a/cumulus/parachains/integration-tests/emulated/tests/bridges/bridge-hub-westend/src/tests/snowbridge.rs b/cumulus/parachains/integration-tests/emulated/tests/bridges/bridge-hub-westend/src/tests/snowbridge.rs index a55aa9f9c353..48eeb07a7c6a 100644 --- a/cumulus/parachains/integration-tests/emulated/tests/bridges/bridge-hub-westend/src/tests/snowbridge.rs +++ b/cumulus/parachains/integration-tests/emulated/tests/bridges/bridge-hub-westend/src/tests/snowbridge.rs @@ -19,6 +19,7 @@ use codec::{Decode, Encode}; use emulated_integration_tests_common::RESERVABLE_ASSET_ID; use frame_support::pallet_prelude::TypeInfo; use hex_literal::hex; +use penpal_emulated_chain::PARA_ID_B; use rococo_westend_system_emulated_network::asset_hub_westend_emulated_chain::genesis::AssetHubWestendAssetOwner; use snowbridge_core::{outbound::OperatingMode, AssetMetadata, TokenIdOf}; use snowbridge_router_primitives::inbound::{ @@ -28,8 +29,6 @@ use snowbridge_router_primitives::inbound::{ use sp_core::H256; use testnet_parachains_constants::westend::snowbridge::EthereumNetwork; use xcm_executor::traits::ConvertLocation; -use penpal_emulated_chain::PARA_ID_B; -use penpal_emulated_chain::penpal_runtime; const INITIAL_FUND: u128 = 5_000_000_000_000; pub const CHAIN_ID: u64 = 11155111; @@ -38,7 +37,7 @@ const ETHEREUM_DESTINATION_ADDRESS: [u8; 20] = hex!("44a57ee2f2FCcb85FDa2B0B18EB const XCM_FEE: u128 = 100_000_000_000; const TOKEN_AMOUNT: u128 = 100_000_000_000; -#[derive(Encode, Decode, Debug, PartialEq, Eq, Clone, TypeInfo)] +#[derive(Encode, Decode, Debug, PartialEq, Eq, Clone, TypeIfmtnfo)] pub enum ControlCall { #[codec(index = 3)] CreateAgent, @@ -289,7 +288,6 @@ fn send_weth_asset_from_asset_hub_to_ethereum() { }); } - /// Tests sending a token to a 3rd party parachain, called PenPal. The token reserve is /// still located on AssetHub. #[test] @@ -336,7 +334,9 @@ fn send_token_from_ethereum_to_penpal() { 1000, )); - assert!(::ForeignAssets::asset_exists(weth_asset_location.clone())); + assert!(::ForeignAssets::asset_exists( + weth_asset_location.clone() + )); }); AssetHubWestend::execute_with(|| { diff --git a/cumulus/parachains/integration-tests/emulated/tests/bridges/bridge-hub-westend/src/tests/snowbridge_v2.rs b/cumulus/parachains/integration-tests/emulated/tests/bridges/bridge-hub-westend/src/tests/snowbridge_v2.rs index 5ee51cda9295..6bc41f212b6e 100644 --- a/cumulus/parachains/integration-tests/emulated/tests/bridges/bridge-hub-westend/src/tests/snowbridge_v2.rs +++ b/cumulus/parachains/integration-tests/emulated/tests/bridges/bridge-hub-westend/src/tests/snowbridge_v2.rs @@ -19,19 +19,20 @@ use bridge_hub_westend_runtime::{ EthereumInboundQueueV2, }; use codec::Encode; +use emulated_integration_tests_common::RESERVABLE_ASSET_ID; use hex_literal::hex; +use penpal_emulated_chain::PARA_ID_B; +use snowbridge_core::{AssetMetadata, TokenIdOf}; use snowbridge_router_primitives::inbound::{ - v2::{Asset::NativeTokenERC20, Message}, + v2::{ + Asset::{ForeignTokenERC20, NativeTokenERC20}, + Message, + }, EthereumLocationsConverterFor, }; use sp_core::{H160, H256}; use sp_runtime::MultiAddress; -use emulated_integration_tests_common::RESERVABLE_ASSET_ID; -use penpal_emulated_chain::PARA_ID_B; -use snowbridge_core::AssetMetadata; use xcm_executor::traits::ConvertLocation; -use snowbridge_core::TokenIdOf; -use snowbridge_router_primitives::inbound::v2::Asset::ForeignTokenERC20; const TOKEN_AMOUNT: u128 = 100_000_000_000; /// Calculates the XCM prologue fee for sending an XCM to AH. @@ -537,14 +538,16 @@ fn send_token_to_penpal_v2() { // Pay fees on Penpal. PayFees { asset: weth_fee_penpal }, // Deposit assets to beneficiary. - DepositAsset { assets: Wild(AllOf { - id: AssetId(token_location.clone()), - fun: WildFungibility::Fungible, - }), - beneficiary: beneficiary.clone(), }, + DepositAsset { + assets: Wild(AllOf { + id: AssetId(token_location.clone()), + fun: WildFungibility::Fungible, + }), + beneficiary: beneficiary.clone(), + }, SetTopic(H256::random().into()), ] - .into(), + .into(), }, ]; let xcm: Xcm<()> = instructions.into(); @@ -611,6 +614,8 @@ fn send_foreign_erc20_token_back_to_polkadot() { let asset_id: Location = [PalletInstance(ASSETS_PALLET_ID), GeneralIndex(RESERVABLE_ASSET_ID.into())].into(); + register_foreign_asset(weth_location()); + let asset_id_in_bh: Location = Location::new( 1, [ @@ -627,8 +632,8 @@ fn send_foreign_erc20_token_back_to_polkadot() { Parachain(AssetHubWestend::para_id().into()), ], ) - .appended_with(asset_id.clone().interior) - .unwrap(); + .appended_with(asset_id.clone().interior) + .unwrap(); let ethereum_destination = Location::new(2, [GlobalConsensus(Ethereum { chain_id: CHAIN_ID })]); @@ -653,36 +658,33 @@ fn send_foreign_erc20_token_back_to_polkadot() { .into(); AssetHubWestend::fund_accounts(vec![(ethereum_sovereign.clone(), INITIAL_FUND)]); + // Mint the asset into the bridge sovereign account, to mimic locked funds AssetHubWestend::mint_asset( ::RuntimeOrigin::signed(AssetHubWestendAssetOwner::get()), RESERVABLE_ASSET_ID, - AssetHubWestendSender::get(), + ethereum_sovereign.clone(), TOKEN_AMOUNT, ); let token_id = TokenIdOf::convert_location(&asset_id_after_reanchored).unwrap(); - let asset: Asset = (asset_id_after_reanchored, TOKEN_AMOUNT).into(); let assets = vec![ // to pay fees - NativeTokenERC20 { token_id: WETH.into(), value: 2_000_000_000_000u128 }, + NativeTokenERC20 { token_id: WETH.into(), value: 3_000_000_000_000u128 }, // the token being transferred ForeignTokenERC20 { token_id: token_id.into(), value: TOKEN_AMOUNT }, ]; BridgeHubWestend::execute_with(|| { type RuntimeEvent = ::RuntimeEvent; - let instructions = vec![ - WithdrawAsset(asset.clone().into()), - DepositAsset { assets: Wild(AllCounted(2)), beneficiary }, - ]; + let instructions = vec![DepositAsset { assets: Wild(AllCounted(2)), beneficiary }]; let xcm: Xcm<()> = instructions.into(); let versioned_message_xcm = VersionedXcm::V5(xcm); let origin = EthereumGatewayAddress::get(); let message = Message { origin, - fee: 1_500_000_000_000u128, + fee: 3_500_000_000_000u128, assets, xcm: versioned_message_xcm.encode(), claimer: Some(claimer_bytes), @@ -943,7 +945,6 @@ pub(crate) fn set_up_weth_and_dot_pool(asset: v5::Location) { }); } - pub(crate) fn set_up_weth_and_dot_pool_on_penpal(asset: v5::Location) { let wnd: v5::Location = v5::Parent.into(); let penpal_location = BridgeHubWestend::sibling_location_of(PenpalB::para_id()); @@ -956,8 +957,7 @@ pub(crate) fn set_up_weth_and_dot_pool_on_penpal(asset: v5::Location) { type RuntimeEvent = ::RuntimeEvent; let signed_owner = ::RuntimeOrigin::signed(owner.clone()); - let signed_bh_sovereign = - ::RuntimeOrigin::signed(bh_sovereign.clone()); + let signed_bh_sovereign = ::RuntimeOrigin::signed(bh_sovereign.clone()); assert_ok!(::ForeignAssets::mint( signed_bh_sovereign.clone(), diff --git a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/src/bridge_to_ethereum_config.rs b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/src/bridge_to_ethereum_config.rs index b46291f09e9e..b05fa357fb98 100644 --- a/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/src/bridge_to_ethereum_config.rs +++ b/cumulus/parachains/runtimes/bridge-hubs/bridge-hub-westend/src/bridge_to_ethereum_config.rs @@ -125,6 +125,8 @@ impl snowbridge_pallet_inbound_queue_v2::Config for Runtime { EthereumSystem, WethAddress, EthereumGatewayAddress, + EthereumUniversalLocation, + AssetHubFromEthereum, >; }