Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
adds tests
Browse files Browse the repository at this point in the history
  • Loading branch information
claravanstaden authored and claravanstaden committed Nov 3, 2023
1 parent 989fb0f commit e36add3
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<u8>| {
match RuntimeEvent::decode(&mut &runtime_event_encoded[..]) {
Ok(RuntimeEvent::EthereumOutboundQueue(event)) => Some(event),
_ => None,
}
})
)
}
}

mod bridge_hub_wococo_tests {
Expand Down
8 changes: 8 additions & 0 deletions cumulus/parachains/runtimes/bridge-hubs/test-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -98,4 +103,7 @@ std = [
"xcm-builder/std",
"xcm-executor/std",
"xcm/std",
"snowbridge-core/std",
"snowbridge-outbound-queue/std",
"snowbridge-control/std",
]
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -950,6 +950,92 @@ where
estimated_fee.into()
}

pub fn handle_transfer_token_message<
Runtime,
XcmConfig,
>(
collator_session_key: CollatorSessionKeys<Runtime>,
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<u8>) -> Option<snowbridge_outbound_queue::Event<Runtime>>,
>,
) 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<Runtime>: From<AccountIdOf<Runtime>>,
{
let bridgehub_parachain_location = MultiLocation::new(1, Parachain(bridghub_parachain_id));

ExtBuilder::<Runtime>::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::<XcmConfig>::execute_xcm(
bridgehub_parachain_location,
xcm,
hash,
RuntimeHelper::<Runtime>::xcm_max_weight(XcmReceivedFrom::Sibling),
)
.ensure_complete());

// check events
let mut events = <frame_system::Pallet<Runtime>>::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;
Expand Down

0 comments on commit e36add3

Please sign in to comment.