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

Use XCM benchmarks from pallet_xcm_benchmarks #185

Merged
merged 5 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions .github/templates/setup-worker/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ runs:
- name: Install Rust nightly
shell: bash
run: |
rustup toolchain install nightly-2023-01-01
rustup toolchain install nightly-2023-01-01 --profile minimal --component rustfmt
rustup default nightly-2023-01-01
rustup target add wasm32-unknown-unknown
rustup component add rustfmt
rustup target add wasm32-unknown-unknown
4 changes: 2 additions & 2 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jobs:
uses: jlumbroso/free-disk-space@main
with:
android: true
dotnet: false
dotnet: true
haskell: false
large-packages: false
swap-storage: false
Expand All @@ -86,4 +86,4 @@ jobs:

- name: Run tests
run: |
cargo test --features runtime-benchmarks --jobs 2
cargo test --features runtime-benchmarks --jobs 1
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion runtime/trappist/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ xcm = { git = "https://github.com/paritytech/polkadot", default-features = false
xcm-builder = { git = "https://github.com/paritytech/polkadot", default-features = false, branch = "release-v0.9.37" }
xcm-executor = { git = "https://github.com/paritytech/polkadot", default-features = false, branch = "release-v0.9.37" }
xcm-primitives = { path = "../../primitives/xcm", default-features = false }
pallet-xcm-benchmarks = { git = "https://github.com/paritytech/polkadot", default-features = false, optional = true , branch = "release-v0.9.37" }

# External Pallets
pallet-dex = { version = "0.0.1", git = "https://github.com/paritytech/substrate-dex.git", default-features = false, branch = "polkadot-v0.9.37" }
Expand Down Expand Up @@ -201,7 +202,8 @@ runtime-benchmarks = [
"pallet-utility/runtime-benchmarks",
"pallet-xcm/runtime-benchmarks",
"cumulus-pallet-session-benchmarking/runtime-benchmarks",
"cumulus-pallet-xcmp-queue/runtime-benchmarks"
"cumulus-pallet-xcmp-queue/runtime-benchmarks",
"pallet-xcm-benchmarks/runtime-benchmarks",
]
try-runtime = [
"frame-try-runtime",
Expand Down
108 changes: 105 additions & 3 deletions runtime/trappist/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,10 @@ mod benches {
[pallet_scheduler, Scheduler]
[pallet_utility, Utility]
[cumulus_pallet_xcmp_queue, XcmpQueue]
// XCM
// NOTE: Make sure you point to the individual modules below.
[pallet_xcm_benchmarks::fungible, XcmBalances]
[pallet_xcm_benchmarks::generic, XcmGeneric]
);
}

Expand Down Expand Up @@ -976,6 +980,12 @@ impl_runtime_apis! {
use frame_system_benchmarking::Pallet as SystemBench;
use cumulus_pallet_session_benchmarking::Pallet as SessionBench;

// This is defined once again in dispatch_benchmark, because list_benchmarks!
// and add_benchmarks! are macros exported by define_benchmarks! macros and those types
// are referenced in that call.
type XcmBalances = pallet_xcm_benchmarks::fungible::Pallet::<Runtime>;
type XcmGeneric = pallet_xcm_benchmarks::generic::Pallet::<Runtime>;

let mut list = Vec::<BenchmarkList>::new();
list_benchmarks!(list, extra);

Expand All @@ -986,22 +996,114 @@ impl_runtime_apis! {
fn dispatch_benchmark(
config: frame_benchmarking::BenchmarkConfig
) -> Result<Vec<frame_benchmarking::BenchmarkBatch>, sp_runtime::RuntimeString> {
use frame_benchmarking::{Benchmarking, BenchmarkBatch, TrackedStorageKey};
use frame_benchmarking::{Benchmarking, BenchmarkBatch, TrackedStorageKey, BenchmarkError};

use frame_system_benchmarking::Pallet as SystemBench;
impl frame_system_benchmarking::Config for Runtime {}

use cumulus_pallet_session_benchmarking::Pallet as SessionBench;
impl cumulus_pallet_session_benchmarking::Config for Runtime {}

use xcm::latest::prelude::*;
use xcm_config::RelayLocation;
use pallet_xcm_benchmarks::asset_instance_from;

impl pallet_xcm_benchmarks::Config for Runtime {
type XcmConfig = xcm_config::XcmConfig;
type AccountIdConverter = xcm_config::LocationToAccountId;
fn valid_destination() -> Result<MultiLocation, BenchmarkError> {
Ok(RelayLocation::get())
}
fn worst_case_holding() -> MultiAssets {
// A mix of fungible, non-fungible, and concrete assets.
const HOLDING_FUNGIBLES: u32 = 100;
const HOLDING_NON_FUNGIBLES: u32 = 100;
let fungibles_amount: u128 = 100;
let mut assets = (0..HOLDING_FUNGIBLES)
.map(|i| {
MultiAsset {
id: Concrete(GeneralIndex(i as u128).into()),
fun: Fungible(fungibles_amount * i as u128),
}
.into()
})
.chain(core::iter::once(MultiAsset { id: Concrete(Here.into()), fun: Fungible(u128::MAX) }))
.chain((0..HOLDING_NON_FUNGIBLES).map(|i| MultiAsset {
id: Concrete(GeneralIndex(i as u128).into()),
fun: NonFungible(asset_instance_from(i)),
}))
.collect::<Vec<_>>();

assets.push(MultiAsset{
id: Concrete(RelayLocation::get()),
fun: Fungible(1_000_000 * UNITS),
});
assets.into()
}
}

parameter_types! {
pub const TrustedTeleporter: Option<(MultiLocation, MultiAsset)> = Some((
RelayLocation::get(),
MultiAsset { fun: Fungible(1 * UNITS), id: Concrete(RelayLocation::get()) },
));
pub const TrustedReserve: Option<(MultiLocation, MultiAsset)> = None;
pub const CheckedAccount: Option<AccountId> = None;

}

impl pallet_xcm_benchmarks::fungible::Config for Runtime {
type TransactAsset = Balances;

type CheckedAccount = CheckedAccount;
type TrustedTeleporter = TrustedTeleporter;
type TrustedReserve = TrustedReserve;

fn get_multi_asset() -> MultiAsset {
MultiAsset {
id: Concrete(RelayLocation::get()),
fun: Fungible(1 * UNITS),
}
}
}

impl pallet_xcm_benchmarks::generic::Config for Runtime {
type RuntimeCall = RuntimeCall;

fn worst_case_response() -> (u64, Response) {
(0u64, Response::Version(Default::default()))
}

fn transact_origin() -> Result<MultiLocation, BenchmarkError> {
Ok(RelayLocation::get())
}

fn subscribe_origin() -> Result<MultiLocation, BenchmarkError> {
Ok(RelayLocation::get())
}

fn claimable_asset() -> Result<(MultiLocation, MultiLocation, MultiAssets), BenchmarkError> {
let origin = RelayLocation::get();
let assets: MultiAssets = (Concrete(RelayLocation::get()), 1_000 * UNITS).into();
let ticket = MultiLocation { parents: 0, interior: Here };
Ok((origin, ticket, assets))
}
}

type XcmBalances = pallet_xcm_benchmarks::fungible::Pallet::<Runtime>;
type XcmGeneric = pallet_xcm_benchmarks::generic::Pallet::<Runtime>;

use xcm_primitives::TrappistDropAssets;
use xcm::prelude::MultiLocation;
use crate::weights::TrappistDropAssetsWeigher;

use parachains_common::AssetId as TrappistAssetId;

impl trappist_runtime_benchmarks::Config for Runtime {
type AssetId = AssetId;
type AssetId = TrappistAssetId;
type Balance = Balance;
type ExistentialDeposit = ConstU128<EXISTENTIAL_DEPOSIT>;
type DropAssets = TrappistDropAssets<AssetId, AssetRegistry, Assets, Balances, (), AccountId, TrappistDropAssetsWeigher>;
type DropAssets = TrappistDropAssets<TrappistAssetId, AssetRegistry, Assets, Balances, (), AccountId, TrappistDropAssetsWeigher>;

fn register_asset(asset_id: Self::AssetId, location: MultiLocation) {
pallet_asset_registry::AssetMultiLocationId::<Runtime>::insert(&location, asset_id);
Expand Down
1 change: 1 addition & 0 deletions runtime/trappist/src/weights/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use xcm_primitives::DropAssetsWeigher;
use crate::Runtime;

mod trappist_runtime_benchmarks;
pub mod xcm;

pub struct TrappistDropAssetsWeigher();
impl DropAssetsWeigher for TrappistDropAssetsWeigher {
Expand Down
188 changes: 188 additions & 0 deletions runtime/trappist/src/weights/xcm/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
// Copyright 2022 Parity Technologies (UK) Ltd.
// This file is part of Cumulus.

// Cumulus is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Cumulus is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Cumulus. If not, see <http://www.gnu.org/licenses/>.

mod pallet_xcm_benchmarks_fungible;
mod pallet_xcm_benchmarks_generic;

use crate::Runtime;
use frame_support::weights::Weight;
use pallet_xcm_benchmarks_fungible::WeightInfo as XcmFungibleWeight;
use pallet_xcm_benchmarks_generic::WeightInfo as XcmGeneric;
use sp_std::{cmp, prelude::*};
use xcm::{
latest::{prelude::*, Weight as XCMWeight},
DoubleEncoded,
};

trait WeighMultiAssets {
fn weigh_multi_assets(&self, weight: Weight) -> XCMWeight;
}

const MAX_ASSETS: u32 = 100;

impl WeighMultiAssets for MultiAssetFilter {
fn weigh_multi_assets(&self, weight: Weight) -> XCMWeight {
let weight = match self {
Self::Definite(assets) =>
weight.saturating_mul(assets.inner().into_iter().count() as u64),
Self::Wild(_) => weight.saturating_mul(MAX_ASSETS as u64),
};
weight.ref_time()
}
}

impl WeighMultiAssets for MultiAssets {
fn weigh_multi_assets(&self, weight: Weight) -> XCMWeight {
weight.saturating_mul(self.inner().into_iter().count() as u64).ref_time()
}
}

pub struct TrappistXcmWeight<Call>(core::marker::PhantomData<Call>);
impl<Call> XcmWeightInfo<Call> for TrappistXcmWeight<Call> {
fn withdraw_asset(assets: &MultiAssets) -> XCMWeight {
assets.weigh_multi_assets(XcmFungibleWeight::<Runtime>::withdraw_asset())
}
// Currently there is no trusted reserve
fn reserve_asset_deposited(_assets: &MultiAssets) -> XCMWeight {
u64::MAX
}
fn receive_teleported_asset(assets: &MultiAssets) -> XCMWeight {
assets.weigh_multi_assets(XcmFungibleWeight::<Runtime>::receive_teleported_asset())
}
fn query_response(_query_id: &u64, _response: &Response, _max_weight: &u64) -> XCMWeight {
XcmGeneric::<Runtime>::query_response().ref_time()
}
fn transfer_asset(assets: &MultiAssets, _dest: &MultiLocation) -> XCMWeight {
assets.weigh_multi_assets(XcmFungibleWeight::<Runtime>::transfer_asset())
}
fn transfer_reserve_asset(
assets: &MultiAssets,
_dest: &MultiLocation,
_xcm: &Xcm<()>,
) -> XCMWeight {
assets.weigh_multi_assets(XcmFungibleWeight::<Runtime>::transfer_reserve_asset())
}
fn transact(
_origin_type: &OriginKind,
_require_weight_at_most: &u64,
_call: &DoubleEncoded<Call>,
) -> XCMWeight {
XcmGeneric::<Runtime>::transact().ref_time()
}
fn hrmp_new_channel_open_request(
_sender: &u32,
_max_message_size: &u32,
_max_capacity: &u32,
) -> XCMWeight {
// XCM Executor does not currently support HRMP channel operations
Weight::MAX.ref_time()
}
fn hrmp_channel_accepted(_recipient: &u32) -> XCMWeight {
// XCM Executor does not currently support HRMP channel operations
Weight::MAX.ref_time()
}
fn hrmp_channel_closing(_initiator: &u32, _sender: &u32, _recipient: &u32) -> XCMWeight {
// XCM Executor does not currently support HRMP channel operations
Weight::MAX.ref_time()
}
fn clear_origin() -> XCMWeight {
XcmGeneric::<Runtime>::clear_origin().ref_time()
}
fn descend_origin(_who: &InteriorMultiLocation) -> XCMWeight {
XcmGeneric::<Runtime>::descend_origin().ref_time()
}
fn report_error(
_query_id: &QueryId,
_dest: &MultiLocation,
_max_response_weight: &u64,
) -> XCMWeight {
XcmGeneric::<Runtime>::report_error().ref_time()
}

fn deposit_asset(
assets: &MultiAssetFilter,
_max_assets: &u32,
_dest: &MultiLocation,
) -> XCMWeight {
// Hardcoded till the XCM pallet is fixed
let hardcoded_weight = Weight::from_ref_time(1_000_000_000 as u64).ref_time();
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<()>,
) -> XCMWeight {
assets.weigh_multi_assets(XcmFungibleWeight::<Runtime>::deposit_reserve_asset())
}
fn exchange_asset(_give: &MultiAssetFilter, _receive: &MultiAssets) -> XCMWeight {
Weight::MAX.ref_time()
}
fn initiate_reserve_withdraw(
assets: &MultiAssetFilter,
_reserve: &MultiLocation,
_xcm: &Xcm<()>,
) -> XCMWeight {
assets.weigh_multi_assets(XcmGeneric::<Runtime>::initiate_reserve_withdraw())
}
fn initiate_teleport(
assets: &MultiAssetFilter,
_dest: &MultiLocation,
_xcm: &Xcm<()>,
) -> XCMWeight {
// Hardcoded till the XCM pallet is fixed
let hardcoded_weight = Weight::from_ref_time(200_000_000 as u64).ref_time();
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,
) -> XCMWeight {
XcmGeneric::<Runtime>::query_holding().ref_time()
}
fn buy_execution(_fees: &MultiAsset, _weight_limit: &WeightLimit) -> XCMWeight {
XcmGeneric::<Runtime>::buy_execution().ref_time()
}
fn refund_surplus() -> XCMWeight {
XcmGeneric::<Runtime>::refund_surplus().ref_time()
}
fn set_error_handler(_xcm: &Xcm<Call>) -> XCMWeight {
XcmGeneric::<Runtime>::set_error_handler().ref_time()
}
fn set_appendix(_xcm: &Xcm<Call>) -> XCMWeight {
XcmGeneric::<Runtime>::set_appendix().ref_time()
}
fn clear_error() -> XCMWeight {
XcmGeneric::<Runtime>::clear_error().ref_time()
}
fn claim_asset(_assets: &MultiAssets, _ticket: &MultiLocation) -> XCMWeight {
XcmGeneric::<Runtime>::claim_asset().ref_time()
}
fn trap(_code: &u64) -> XCMWeight {
XcmGeneric::<Runtime>::trap().ref_time()
}
fn subscribe_version(_query_id: &QueryId, _max_response_weight: &u64) -> XCMWeight {
XcmGeneric::<Runtime>::subscribe_version().ref_time()
}
fn unsubscribe_version() -> XCMWeight {
XcmGeneric::<Runtime>::unsubscribe_version().ref_time()
}
}
Loading