Skip to content

Commit

Permalink
Benchmark generic XCM instructions (#248)
Browse files Browse the repository at this point in the history
* refactor benchmarks and generate first xcm weights

* add comment in worst_case_holding

* change weights file name

* fmt

* add missing pallets in define_benchmarks macro

* add generic weights to XCM weigher

* clippy

* make pallet-xcm-benchmarks optional
  • Loading branch information
Agusrodri authored Sep 8, 2023
1 parent 7f3d9af commit 35e1a27
Show file tree
Hide file tree
Showing 8 changed files with 1,015 additions and 89 deletions.
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.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ try-runtime-cli = { git = "https://github.com/moondance-labs/substrate", branch

# Polkadot (wasm)
pallet-xcm = { git = "https://github.com/moondance-labs/polkadot", branch = "tanssi-polkadot-v0.9.43", default-features = false }
pallet-xcm-benchmarks = { git = "https://github.com/moondance-labs/polkadot", branch = "tanssi-polkadot-v0.9.43", default-features = false }
polkadot-parachain = { git = "https://github.com/moondance-labs/polkadot", branch = "tanssi-polkadot-v0.9.43", default-features = false }
polkadot-runtime-common = { git = "https://github.com/moondance-labs/polkadot", branch = "tanssi-polkadot-v0.9.43", default-features = false }
polkadot-runtime-parachains = { git = "https://github.com/moondance-labs/polkadot", branch = "tanssi-polkadot-v0.9.43", default-features = false }
Expand Down
2 changes: 2 additions & 0 deletions runtime/dancebox/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ tp-traits = { workspace = true }

# Polkadot
pallet-xcm = { workspace = true }
pallet-xcm-benchmarks = { workspace = true, optional = true }
polkadot-parachain = { workspace = true }
polkadot-runtime-common = { workspace = true }
xcm = { workspace = true }
Expand Down Expand Up @@ -195,6 +196,7 @@ runtime-benchmarks = [
"pallet-registrar/runtime-benchmarks",
"pallet-timestamp/runtime-benchmarks",
"pallet-xcm/runtime-benchmarks",
"pallet-xcm-benchmarks/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"xcm-builder/runtime-benchmarks",
]
Expand Down
160 changes: 78 additions & 82 deletions runtime/dancebox/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use sp_version::NativeVersion;
pub use sp_runtime::BuildStorage;

pub mod migrations;
pub mod weights;

use {
cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases,
Expand Down Expand Up @@ -929,6 +930,19 @@ construct_runtime!(
}
);

#[cfg(feature = "runtime-benchmarks")]
mod benches {
frame_benchmarking::define_benchmarks!(
[frame_system, frame_system_benchmarking::Pallet::<Runtime>]
[pallet_author_noting, AuthorNoting]
[pallet_configuration, Configuration]
[pallet_registrar, Registrar]
[pallet_invulnerables, Invulnerables]
[pallet_pooled_staking, PooledStaking]
[pallet_xcm_benchmarks::generic, pallet_xcm_benchmarks::generic::Pallet::<Runtime>]
);
}

impl_runtime_apis! {
impl sp_consensus_aura::AuraApi<Block, NimbusId> for Runtime {
fn slot_duration() -> sp_consensus_aura::SlotDuration {
Expand Down Expand Up @@ -1051,67 +1065,81 @@ impl_runtime_apis! {
Vec<frame_benchmarking::BenchmarkList>,
Vec<frame_support::traits::StorageInfo>,
) {
use frame_benchmarking::{list_benchmark, BenchmarkList, Benchmarking};
use frame_benchmarking::{Benchmarking, BenchmarkList};
use frame_support::traits::StorageInfoTrait;
use frame_system_benchmarking::Pallet as SystemBench;
use pallet_author_noting::Pallet as PalletAuthorNotingBench;
use pallet_configuration::Pallet as PalletConfigurationBench;
use pallet_registrar::Pallet as PalletRegistrarBench;
use pallet_invulnerables::Pallet as PalletInvulnerablesBench;
use pallet_pooled_staking::Pallet as PalledPooledStaking;

let mut list = Vec::<BenchmarkList>::new();

list_benchmark!(list, extra, frame_system, SystemBench::<Runtime>);
list_benchmark!(
list,
extra,
pallet_configuration,
PalletConfigurationBench::<Runtime>
);
list_benchmark!(
list,
extra,
pallet_author_noting,
PalletAuthorNotingBench::<Runtime>
);
list_benchmark!(
list,
extra,
pallet_registrar,
PalletRegistrarBench::<Runtime>
);
list_benchmark!(
list,
extra,
pallet_invulnerables,
PalletInvulnerablesBench::<Runtime>
);

list_benchmark!(
list,
extra,
pallet_pooled_staking,
PalledPooledStaking::<Runtime>
);
list_benchmarks!(list, extra);

let storage_info = AllPalletsWithSystem::storage_info();

(list, storage_info)
}

fn dispatch_benchmark(
config: frame_benchmarking::BenchmarkConfig,
) -> Result<Vec<frame_benchmarking::BenchmarkBatch>, sp_runtime::RuntimeString> {
use frame_benchmarking::{add_benchmark, BenchmarkBatch, Benchmarking, TrackedStorageKey};
use frame_benchmarking::{BenchmarkBatch, Benchmarking, TrackedStorageKey};

use frame_system_benchmarking::Pallet as SystemBench;
impl frame_system_benchmarking::Config for Runtime {}
use pallet_author_noting::Pallet as PalletAuthorNotingBench;
use pallet_configuration::Pallet as PalletConfigurationBench;
use pallet_registrar::Pallet as PalletRegistrarBench;
use pallet_invulnerables::Pallet as PalletInvulnerablesBench;
use pallet_pooled_staking::Pallet as PalletPooledStaking;

use xcm::latest::prelude::*;
use frame_benchmarking::BenchmarkError;

impl pallet_xcm_benchmarks::Config for Runtime {
type XcmConfig = xcm_config::XcmConfig;
type AccountIdConverter = xcm_config::LocationToAccountId;
fn valid_destination() -> Result<MultiLocation, BenchmarkError> {
Ok(MultiLocation::parent())
}
fn worst_case_holding(_depositable_count: u32) -> MultiAssets {
// We only care for native asset until we support others
// TODO: refactor this case once other assets are supported
vec![MultiAsset{
id: Concrete(MultiLocation::here()),
fun: Fungible(u128::MAX),
}].into()
}
}

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

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

fn worst_case_asset_exchange() -> Result<(MultiAssets, MultiAssets), BenchmarkError> {
Err(BenchmarkError::Skip)
}

fn universal_alias() -> Result<(MultiLocation, Junction), BenchmarkError> {
Err(BenchmarkError::Skip)
}

fn transact_origin_and_runtime_call() -> Result<(MultiLocation, RuntimeCall), BenchmarkError> {
Ok((MultiLocation::parent(), frame_system::Call::remark_with_event { remark: vec![] }.into()))
}

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

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

fn unlockable_asset() -> Result<(MultiLocation, MultiLocation, MultiAsset), BenchmarkError> {
Err(BenchmarkError::Skip)
}

fn export_message_origin_and_destination(
) -> Result<(MultiLocation, NetworkId, InteriorMultiLocation), BenchmarkError> {
Err(BenchmarkError::Skip)
}
}

let whitelist: Vec<TrackedStorageKey> = vec![
// Block Number
Expand Down Expand Up @@ -1148,40 +1176,8 @@ impl_runtime_apis! {
let mut batches = Vec::<BenchmarkBatch>::new();
let params = (&config, &whitelist);

add_benchmark!(params, batches, frame_system, SystemBench::<Runtime>);
add_benchmark!(
params,
batches,
pallet_configuration,
PalletConfigurationBench::<Runtime>
);
add_benchmark!(
params,
batches,
pallet_author_noting,
PalletAuthorNotingBench::<Runtime>
);
add_benchmark!(
params,
batches,
pallet_registrar,
PalletRegistrarBench::<Runtime>
);
add_benchmark!(
params,
batches,
pallet_invulnerables,
PalletInvulnerablesBench::<Runtime>
);
add_benchmark!(
params,
batches,
pallet_pooled_staking,
PalletPooledStaking::<Runtime>
);
if batches.is_empty() {
return Err("Benchmark not found for this pallet.".into());
}
add_benchmarks!(params, batches);

Ok(batches)
}
}
Expand Down
17 changes: 17 additions & 0 deletions runtime/dancebox/src/weights/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (C) Moondance Labs Ltd.
// This file is part of Tanssi.

// Tanssi 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.

// Tanssi 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 Tanssi. If not, see <http://www.gnu.org/licenses/>

pub mod xcm;
Loading

0 comments on commit 35e1a27

Please sign in to comment.