Skip to content

Commit

Permalink
Hardcode weight for instructions with MultiAssetFilter params (#822)
Browse files Browse the repository at this point in the history
* Hardcode weight for instructions with  MultiAssetFilter params

Signed-off-by: Georgi Zlatarev <georgi.zlatarev@manta.network>

* TEst for receiver weight

Signed-off-by: Georgi Zlatarev <georgi.zlatarev@manta.network>

* Add sanity weight checks for typical sender and receiver xcm messages

Signed-off-by: Georgi Zlatarev <georgi.zlatarev@manta.network>

* Fix clippy

Signed-off-by: Georgi Zlatarev <georgi.zlatarev@manta.network>

* Fix clippy

Signed-off-by: Georgi Zlatarev <georgi.zlatarev@manta.network>

Signed-off-by: Georgi Zlatarev <georgi.zlatarev@manta.network>
Co-authored-by: Shumo Chu <stechu@users.noreply.github.com>
  • Loading branch information
ghzlatarev and stechu committed Oct 8, 2022
1 parent 0cbd977 commit 378c4a9
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 11 deletions.
2 changes: 1 addition & 1 deletion pallets/manta-pay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ pub mod pallet {
}
}
(
VoidNumberSetInsertionOrder::<T>::contains_key(max_sender_index as u64),
VoidNumberSetInsertionOrder::<T>::contains_key(max_sender_index),
senders,
)
}
Expand Down
27 changes: 22 additions & 5 deletions runtime/calamari/src/weights/xcm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mod pallet_xcm_benchmarks_generic;

use crate::Runtime;
use frame_support::weights::Weight;
use sp_std::cmp;
use xcm::{latest::prelude::*, DoubleEncoded};

use pallet_xcm_benchmarks_fungible::WeightInfo as XcmFungibleWeight;
Expand Down Expand Up @@ -113,15 +114,22 @@ impl<Call> XcmWeightInfo<Call> for CalamariXcmWeight<Call> {
_max_assets: &u32,
_dest: &MultiLocation,
) -> Weight {
assets.weigh_multi_assets(XcmFungibleWeight::<Runtime>::deposit_asset())
// Hardcoded until better understanding how to deal with worst case scenario of holding register
let hardcoded_weight: u64 = 1_000_000_000;
let weight = assets.weigh_multi_assets(XcmFungibleWeight::<Runtime>::deposit_asset());
cmp::min(hardcoded_weight, weight)
}
fn deposit_reserve_asset(
assets: &MultiAssetFilter,
_max_assets: &u32,
_dest: &MultiLocation,
_xcm: &Xcm<()>,
) -> Weight {
assets.weigh_multi_assets(XcmFungibleWeight::<Runtime>::deposit_reserve_asset())
// Hardcoded until better understanding how to deal with worst case scenario of holding register
let hardcoded_weight: u64 = 1_000_000_000;
let weight =
assets.weigh_multi_assets(XcmFungibleWeight::<Runtime>::deposit_reserve_asset());
cmp::min(hardcoded_weight, weight)
}
fn exchange_asset(_give: &MultiAssetFilter, _receive: &MultiAssets) -> Weight {
Weight::MAX
Expand All @@ -131,22 +139,31 @@ impl<Call> XcmWeightInfo<Call> for CalamariXcmWeight<Call> {
_reserve: &MultiLocation,
_xcm: &Xcm<()>,
) -> Weight {
assets.weigh_multi_assets(XcmGeneric::<Runtime>::initiate_reserve_withdraw())
// Hardcoded until better understanding how to deal with worst case scenario of holding register
let hardcoded_weight: u64 = 1_000_000_000;
let weight = assets.weigh_multi_assets(XcmGeneric::<Runtime>::initiate_reserve_withdraw());
cmp::min(hardcoded_weight, weight)
}
fn initiate_teleport(
assets: &MultiAssetFilter,
_dest: &MultiLocation,
_xcm: &Xcm<()>,
) -> Weight {
assets.weigh_multi_assets(XcmFungibleWeight::<Runtime>::initiate_teleport())
// Hardcoded until better understanding how to deal with worst case scenario of holding register
let hardcoded_weight: u64 = 1_000_000_000;
let weight = assets.weigh_multi_assets(XcmFungibleWeight::<Runtime>::initiate_teleport());
cmp::min(hardcoded_weight, weight)
}
fn query_holding(
_query_id: &u64,
_dest: &MultiLocation,
_assets: &MultiAssetFilter,
_max_response_weight: &u64,
) -> Weight {
XcmGeneric::<Runtime>::query_holding()
// Hardcoded until better understanding how to deal with worst case scenario of holding register
let hardcoded_weight: u64 = 1_000_000_000;
let weight = XcmGeneric::<Runtime>::query_holding();
cmp::min(hardcoded_weight, weight)
}
fn buy_execution(_fees: &MultiAsset, _weight_limit: &WeightLimit) -> Weight {
XcmGeneric::<Runtime>::buy_execution()
Expand Down
104 changes: 104 additions & 0 deletions runtime/calamari/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,107 @@ impl orml_xtokens::Config for Runtime {
type MultiLocationsFilter = AssetManager;
type ReserveProvider = AbsoluteReserveProvider;
}

#[test]
fn test_receiver_weight() {
use xcm_executor::traits::WeightBounds;

let dummy_assets = MultiAssets::from(vec![MultiAsset {
id: Concrete(MultiLocation {
parents: 1,
interior: X1(Parachain(1)),
}),
fun: Fungible(10000000000000),
}]);
// format of self_reserve message received from a chain using xTokens
let mut msg = Xcm(vec![
ReserveAssetDeposited(dummy_assets.clone()),
ClearOrigin,
BuyExecution {
fees: MultiAsset {
id: Concrete(MultiLocation {
parents: 1,
interior: X1(Parachain(1)),
}),
fun: Fungible(10000000000000),
},
weight_limit: Limited(3999999999),
},
DepositAsset {
assets: Wild(All),
max_assets: 1,
beneficiary: MultiLocation {
parents: 0,
interior: X1(AccountId32 {
network: Any,
id: [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
],
}),
},
},
]);

let weight = <XcmExecutorConfig as xcm_executor::Config>::Weigher::weight(&mut msg).unwrap();
// 4_000_000_000 is a typical configuration value provided to dApp developers for `dest_weight`
// argument when sending xcm message to Calamari. ie moonbeam, sub=wallet, phala, etc
assert!(weight < 4_000_000_000);

// format of to_reserve message received from a chain using xTokens
msg.0[0] = WithdrawAsset(dummy_assets);
let weight = <XcmExecutorConfig as xcm_executor::Config>::Weigher::weight(&mut msg).unwrap();
assert!(weight < 4_000_000_000);
}

#[test]
fn test_sender_xcm_weight() {
use xcm_executor::traits::WeightBounds;

let dummy_multi_location = MultiLocation {
parents: 1,
interior: X1(Parachain(1)),
};
let dummy_assets = MultiAssets::from(vec![MultiAsset {
id: Concrete(MultiLocation {
parents: 1,
interior: X1(Parachain(1)),
}),
fun: Fungible(10000000000000),
}]);
// format of to_reserve message composed by xTokens
let mut msg = Xcm(vec![
WithdrawAsset(dummy_assets),
InitiateReserveWithdraw {
assets: Wild(All),
reserve: dummy_multi_location.clone(),
xcm: Xcm(vec![
BuyExecution {
fees: MultiAsset {
id: Concrete(dummy_multi_location),
fun: Fungible(10000000000000),
},
weight_limit: Limited(3999999999),
},
DepositAsset {
assets: Wild(All),
max_assets: 1,
beneficiary: MultiLocation {
parents: 0,
interior: X1(AccountId32 {
network: Any,
id: [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
],
}),
},
},
]),
},
]);

let weight = <XcmExecutorConfig as xcm_executor::Config>::Weigher::weight(&mut msg).unwrap();
// 4_000_000_000 is a typical configuration we use on the manta dApps
assert!(weight < 4_000_000_000);
}
27 changes: 22 additions & 5 deletions runtime/dolphin/src/weights/xcm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mod pallet_xcm_benchmarks_generic;

use crate::Runtime;
use frame_support::weights::Weight;
use sp_std::cmp;
use xcm::{latest::prelude::*, DoubleEncoded};

use pallet_xcm_benchmarks_fungible::WeightInfo as XcmFungibleWeight;
Expand Down Expand Up @@ -113,15 +114,22 @@ impl<Call> XcmWeightInfo<Call> for DolphinXcmWeight<Call> {
_max_assets: &u32,
_dest: &MultiLocation,
) -> Weight {
assets.weigh_multi_assets(XcmFungibleWeight::<Runtime>::deposit_asset())
// Hardcoded until better understanding how to deal with worst case scenario of holding register
let hardcoded_weight: u64 = 1_000_000_000;
let weight = assets.weigh_multi_assets(XcmFungibleWeight::<Runtime>::deposit_asset());
cmp::min(hardcoded_weight, weight)
}
fn deposit_reserve_asset(
assets: &MultiAssetFilter,
_max_assets: &u32,
_dest: &MultiLocation,
_xcm: &Xcm<()>,
) -> Weight {
assets.weigh_multi_assets(XcmFungibleWeight::<Runtime>::deposit_reserve_asset())
// Hardcoded until better understanding how to deal with worst case scenario of holding register
let hardcoded_weight: u64 = 1_000_000_000;
let weight =
assets.weigh_multi_assets(XcmFungibleWeight::<Runtime>::deposit_reserve_asset());
cmp::min(hardcoded_weight, weight)
}
fn exchange_asset(_give: &MultiAssetFilter, _receive: &MultiAssets) -> Weight {
Weight::MAX
Expand All @@ -131,22 +139,31 @@ impl<Call> XcmWeightInfo<Call> for DolphinXcmWeight<Call> {
_reserve: &MultiLocation,
_xcm: &Xcm<()>,
) -> Weight {
assets.weigh_multi_assets(XcmGeneric::<Runtime>::initiate_reserve_withdraw())
// Hardcoded until better understanding how to deal with worst case scenario of holding register
let hardcoded_weight: u64 = 1_000_000_000;
let weight = assets.weigh_multi_assets(XcmGeneric::<Runtime>::initiate_reserve_withdraw());
cmp::min(hardcoded_weight, weight)
}
fn initiate_teleport(
assets: &MultiAssetFilter,
_dest: &MultiLocation,
_xcm: &Xcm<()>,
) -> Weight {
assets.weigh_multi_assets(XcmFungibleWeight::<Runtime>::initiate_teleport())
// Hardcoded until better understanding how to deal with worst case scenario of holding register
let hardcoded_weight: u64 = 1_000_000_000;
let weight = assets.weigh_multi_assets(XcmFungibleWeight::<Runtime>::initiate_teleport());
cmp::min(hardcoded_weight, weight)
}
fn query_holding(
_query_id: &u64,
_dest: &MultiLocation,
_assets: &MultiAssetFilter,
_max_response_weight: &u64,
) -> Weight {
XcmGeneric::<Runtime>::query_holding()
// Hardcoded until better understanding how to deal with worst case scenario of holding register
let hardcoded_weight: u64 = 1_000_000_000;
let weight = XcmGeneric::<Runtime>::query_holding();
cmp::min(hardcoded_weight, weight)
}
fn buy_execution(_fees: &MultiAsset, _weight_limit: &WeightLimit) -> Weight {
XcmGeneric::<Runtime>::buy_execution()
Expand Down

0 comments on commit 378c4a9

Please sign in to comment.