Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Test for calculating base delivery fee on BridgeHubs
Browse files Browse the repository at this point in the history
  • Loading branch information
bkontur committed Aug 14, 2023
1 parent ebc0ed1 commit a701215
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,12 @@ pub fn complex_relay_extrinsic_works() {
construct_and_apply_extrinsic,
);
}

#[test]
pub fn can_calculate_weight_for_paid_export_message_with_reserve_transfer() {
bridge_hub_test_utils::test_cases::can_calculate_weight_for_paid_export_message_with_reserve_transfer::<
Runtime,
XcmConfig,
WeightToFee,
>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,12 @@ pub fn complex_relay_extrinsic_works() {
construct_and_apply_extrinsic,
);
}

#[test]
pub fn can_calculate_weight_for_paid_export_message_with_reserve_transfer() {
bridge_hub_test_utils::test_cases::can_calculate_weight_for_paid_export_message_with_reserve_transfer::<
Runtime,
XcmConfig,
WeightToFee,
>();
}
111 changes: 109 additions & 2 deletions parachains/runtimes/bridge-hubs/test-utils/src/test_cases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,13 @@ use parachains_runtimes_test_utils::{
};
use sp_core::H256;
use sp_keyring::AccountKeyring::*;
use sp_runtime::{traits::Header as HeaderT, AccountId32};
use sp_runtime::{
traits::{Header as HeaderT, Zero},
AccountId32,
};
use xcm::latest::prelude::*;
use xcm_builder::DispatchBlobError;
use xcm_executor::XcmExecutor;
use xcm_executor::{traits::WeightBounds, XcmExecutor};

// Re-export test_case from assets
pub use asset_test_utils::include_teleports_for_native_asset_works;
Expand Down Expand Up @@ -132,6 +135,7 @@ pub fn handle_export_message_from_system_parachain_to_outbound_queue_works<
>,
export_message_instruction: fn() -> Instruction<XcmConfig::RuntimeCall>,
expected_lane_id: LaneId,
// TODO: add paid or unpaid
) where
Runtime: frame_system::Config
+ pallet_balances::Config
Expand Down Expand Up @@ -757,6 +761,109 @@ pub fn complex_relay_extrinsic_works<Runtime, XcmConfig, HrmpChannelOpener, GPI,
})
}

pub fn can_calculate_weight_for_paid_export_message_with_reserve_transfer<
Runtime,
XcmConfig,
WeightToFee,
>()
where
Runtime: frame_system::Config + pallet_balances::Config,
XcmConfig: xcm_executor::Config,
WeightToFee: frame_support::weights::WeightToFee<Balance = BalanceOf<Runtime>>,
<WeightToFee as frame_support::weights::WeightToFee>::Balance: From<u128> + Into<u128>,
{
// data here are not relevant for weighing
let mut xcm = Xcm(vec![
WithdrawAsset(MultiAssets::from(vec![MultiAsset {
id: Concrete(MultiLocation { parents: 1, interior: Here }),
fun: Fungible(34333299),
}])),
BuyExecution {
fees: MultiAsset {
id: Concrete(MultiLocation { parents: 1, interior: Here }),
fun: Fungible(34333299),
},
weight_limit: Unlimited,
},
ExportMessage {
network: Polkadot,
destination: X1(Parachain(1000)),
xcm: Xcm(vec![
ReserveAssetDeposited(MultiAssets::from(vec![MultiAsset {
id: Concrete(MultiLocation {
parents: 2,
interior: X1(GlobalConsensus(Kusama)),
}),
fun: Fungible(1000000000000),
}])),
ClearOrigin,
BuyExecution {
fees: MultiAsset {
id: Concrete(MultiLocation {
parents: 2,
interior: X1(GlobalConsensus(Kusama)),
}),
fun: Fungible(1000000000000),
},
weight_limit: Unlimited,
},
DepositAsset {
assets: Wild(AllCounted(1)),
beneficiary: MultiLocation {
parents: 0,
interior: X1(xcm::latest::prelude::AccountId32 {
network: None,
id: [
212, 53, 147, 199, 21, 253, 211, 28, 97, 20, 26, 189, 4, 169, 159,
214, 130, 44, 133, 88, 133, 76, 205, 227, 154, 86, 132, 231, 165,
109, 162, 125,
],
}),
},
},
SetTopic([
116, 82, 194, 132, 171, 114, 217, 165, 23, 37, 161, 177, 165, 179, 247, 114,
137, 101, 147, 70, 28, 157, 168, 32, 154, 63, 74, 228, 152, 180, 5, 63,
]),
]),
},
RefundSurplus,
DepositAsset {
assets: Wild(All),
beneficiary: MultiLocation { parents: 1, interior: X1(Parachain(1000)) },
},
SetTopic([
36, 224, 250, 165, 82, 195, 67, 110, 160, 170, 140, 87, 217, 62, 201, 164, 42, 98, 219,
157, 124, 105, 248, 25, 131, 218, 199, 36, 109, 173, 100, 122,
]),
]);

// get weight
let weight = XcmConfig::Weigher::weight(&mut xcm);
assert_ok!(weight);
let weight = weight.unwrap();
// check if sane
let max_expected = Runtime::BlockWeights::get().max_block / 10;
assert!(
weight.all_lte(max_expected),
"calculated weight: {:?}, max_expected: {:?}",
weight,
max_expected
);

// check fee, should not be 0
let estimated_fee = WeightToFee::weight_to_fee(&weight);
assert!(estimated_fee > BalanceOf::<Runtime>::zero());

frame_support::sp_tracing::try_init_simple();
log::error!(
target: "bridges::estimate",
"Estimate fee: {:?} for `ExportMessage` for runtime: {:?}",
estimated_fee,
Runtime::Version::get(),
)
}

pub mod test_data {
use super::*;
use bp_header_chain::justification::GrandpaJustification;
Expand Down

0 comments on commit a701215

Please sign in to comment.