XCM Benchmarks for Generic Instructions (#3940)

* initial stuff

* quick fixes

* move to individual tests

* dont need these

* Update benchmarking.rs

* add to westend

* make benchmarks execute

* fix compile

* add post_execute

* ClaimAsset benchmark working

* subscribe and unsubscribe benchmarks

* benchmark for initiate reserve withdraw

* cargo run --quiet --release --features=runtime-benchmarks -- benchmark --chain=westend-dev --steps=50 --repeat=20 --pallet=pallet_xcm_benchmarks::generic --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --header=./file_header.txt --template=./xcm/pallet-xcm-benchmarks/template.hbs --output=./runtime/westend/src/weights/xcm/pallet_xcm_benchmarks_generic.rs

* fix spell check

* cargo run --quiet --release --features=runtime-benchmarks -- benchmark --chain=westend-dev --steps=50 --repeat=20 --pallet=pallet_xcm_benchmarks::generic --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --header=./file_header.txt --template=./xcm/pallet-xcm-benchmarks/template.hbs --output=./runtime/westend/src/weights/xcm/pallet_xcm_benchmarks_generic.rs

* update worst case for query_holding

* cargo run --quiet --release --features=runtime-benchmarks -- benchmark --chain=westend-dev --steps=50 --repeat=20 --pallet=pallet_xcm_benchmarks::generic --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --header=./file_header.txt --template=./xcm/pallet-xcm-benchmarks/template.hbs --output=./runtime/westend/src/weights/xcm/pallet_xcm_benchmarks_generic.rs

* move verification logic below

* introduce worst case holding to initiate reserve withdraw

* cargo run --quiet --release --features=runtime-benchmarks -- benchmark --chain=westend-dev --steps=50 --repeat=20 --pallet=pallet_xcm_benchmarks::generic --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --header=./file_header.txt --template=./xcm/pallet-xcm-benchmarks/template.hbs --output=./runtime/westend/src/weights/xcm/pallet_xcm_benchmarks_generic.rs

* feedback

* cargo run --quiet --release --features=runtime-benchmarks -- benchmark --chain=westend-dev --steps=50 --repeat=20 --pallet=pallet_xcm_benchmarks::generic --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --header=./file_header.txt --template=./xcm/pallet-xcm-benchmarks/template.hbs --output=./runtime/westend/src/weights/xcm/pallet_xcm_benchmarks_generic.rs

* Revert "cargo run --quiet --release --features=runtime-benchmarks -- benchmark --chain=westend-dev --steps=50 --repeat=20 --pallet=pallet_xcm_benchmarks::generic --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --header=./file_header.txt --template=./xcm/pallet-xcm-benchmarks/template.hbs --output=./runtime/westend/src/weights/xcm/pallet_xcm_benchmarks_generic.rs"

This reverts commit 277903944be620dc57d83b9bcf3b462d2fb73ab5.

* fix benchmark template

* cargo run --quiet --release --features=runtime-benchmarks -- benchmark --chain=westend-dev --steps=50 --repeat=20 --pallet=pallet_xcm_benchmarks::generic --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --header=./file_header.txt --template=./xcm/pallet-xcm-benchmarks/template.hbs --output=./runtime/westend/src/weights/xcm/pallet_xcm_benchmarks_generic.rs

* use response::version

* cargo run --quiet --release --features=runtime-benchmarks -- benchmark --chain=westend-dev --steps=50 --repeat=20 --pallet=pallet_xcm_benchmarks::generic --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --header=./file_header.txt --template=./xcm/pallet-xcm-benchmarks/template.hbs --output=./runtime/westend/src/weights/xcm/pallet_xcm_benchmarks_generic.rs

* refactor worst case holding

* cargo run --quiet --release --features=runtime-benchmarks -- benchmark --chain=westend-dev --steps=50 --repeat=20 --pallet=pallet_xcm_benchmarks::generic --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --header=./file_header.txt --template=./xcm/pallet-xcm-benchmarks/template.hbs --output=./runtime/westend/src/weights/xcm/pallet_xcm_benchmarks_generic.rs

* fmt

* one more todo needs to be labeled

* change option to result with benchmark error

* fmt and fix import

Co-authored-by: Parity Bot <admin@parity.io>
This commit is contained in:
Shawn Tabrizi
2021-12-01 19:52:18 -04:00
committed by GitHub
parent 55d07ee356
commit bf5808eb7f
14 changed files with 737 additions and 114 deletions
@@ -15,7 +15,7 @@
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
use super::*;
use crate::{account_and_location, new_executor, worst_case_holding, AssetTransactorOf, XcmCallOf};
use crate::{account_and_location, new_executor, AssetTransactorOf, XcmCallOf};
use frame_benchmarking::{benchmarks_instance_pallet, BenchmarkError, BenchmarkResult};
use frame_support::{
pallet_prelude::Get,
@@ -41,7 +41,7 @@ benchmarks_instance_pallet! {
withdraw_asset {
let (sender_account, sender_location) = account_and_location::<T>(1);
let worst_case_holding = worst_case_holding();
let worst_case_holding = T::worst_case_holding();
let asset = T::get_multi_asset();
<AssetTransactorOf<T>>::deposit_asset(&asset, &sender_location).unwrap();
@@ -49,7 +49,7 @@ benchmarks_instance_pallet! {
assert!(!T::TransactAsset::balance(&sender_account).is_zero());
let mut executor = new_executor::<T>(sender_location);
executor.holding = worst_case_holding;
executor.holding = worst_case_holding.into();
let instruction = Instruction::<XcmCallOf<T>>::WithdrawAsset(vec![asset.clone()].into());
let xcm = Xcm(vec![instruction]);
}: {
@@ -104,7 +104,7 @@ benchmarks_instance_pallet! {
} verify {
assert!(T::TransactAsset::balance(&sender_account).is_zero());
assert!(!T::TransactAsset::balance(&dest_account).is_zero());
// TODO: Check sender queue is not empty.
// TODO: Check sender queue is not empty. #4426
}
receive_teleported_asset {
@@ -140,10 +140,10 @@ benchmarks_instance_pallet! {
deposit_asset {
let asset = T::get_multi_asset();
let mut holding = worst_case_holding();
let mut holding = T::worst_case_holding();
// Add our asset to the holding.
holding.subsume(asset.clone());
holding.push(asset.clone());
// our dest must have no balance initially.
let dest_location = T::valid_destination()?;
@@ -151,7 +151,7 @@ benchmarks_instance_pallet! {
assert!(T::TransactAsset::balance(&dest_account).is_zero());
let mut executor = new_executor::<T>(Default::default());
executor.holding = holding;
executor.holding = holding.into();
let instruction = Instruction::<XcmCallOf<T>>::DepositAsset {
assets: asset.into(),
max_assets: 1,
@@ -167,10 +167,10 @@ benchmarks_instance_pallet! {
deposit_reserve_asset {
let asset = T::get_multi_asset();
let mut holding = worst_case_holding();
let mut holding = T::worst_case_holding();
// Add our asset to the holding.
holding.subsume(asset.clone());
holding.push(asset.clone());
// our dest must have no balance initially.
let dest_location = T::valid_destination()?;
@@ -178,7 +178,7 @@ benchmarks_instance_pallet! {
assert!(T::TransactAsset::balance(&dest_account).is_zero());
let mut executor = new_executor::<T>(Default::default());
executor.holding = holding;
executor.holding = holding.into();
let instruction = Instruction::<XcmCallOf<T>>::DepositReserveAsset {
assets: asset.into(),
max_assets: 1,
@@ -195,16 +195,16 @@ benchmarks_instance_pallet! {
initiate_teleport {
let asset = T::get_multi_asset();
let mut holding = worst_case_holding();
let mut holding = T::worst_case_holding();
// Add our asset to the holding.
holding.subsume(asset.clone());
holding.push(asset.clone());
// Checked account starts at zero
assert!(T::CheckedAccount::get().map_or(true, |c| T::TransactAsset::balance(&c).is_zero()));
let mut executor = new_executor::<T>(Default::default());
executor.holding = holding;
executor.holding = holding.into();
let instruction = Instruction::<XcmCallOf<T>>::InitiateTeleport {
assets: asset.into(),
dest: T::valid_destination()?,
@@ -17,6 +17,7 @@
//! A mock runtime for XCM benchmarking.
use crate::{fungible as xcm_balances_benchmark, mock::*};
use frame_benchmarking::BenchmarkError;
use frame_support::{parameter_types, traits::Everything};
use sp_core::H256;
use sp_runtime::{
@@ -145,12 +146,15 @@ impl xcm_executor::Config for XcmConfig {
impl crate::Config for Test {
type XcmConfig = XcmConfig;
type AccountIdConverter = AccountIdConverter;
fn valid_destination() -> Result<MultiLocation, sp_runtime::DispatchError> {
fn valid_destination() -> Result<MultiLocation, BenchmarkError> {
let valid_destination: MultiLocation =
X1(AccountId32 { network: NetworkId::Any, id: [0u8; 32] }).into();
Ok(valid_destination)
}
fn worst_case_holding() -> MultiAssets {
crate::mock_worst_case_holding()
}
}
pub type TrustedTeleporters = (xcm_builder::Case<TeleConcreteFung>,);
@@ -0,0 +1,318 @@
// Copyright 2021 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot 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.
// Polkadot 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 Polkadot. If not, see <http://www.gnu.org/licenses/>.
use super::*;
use crate::{new_executor, XcmCallOf};
use codec::Encode;
use frame_benchmarking::{benchmarks, BenchmarkError};
use frame_support::dispatch::GetDispatchInfo;
use sp_std::vec;
use xcm::{
latest::{prelude::*, MultiAssets},
DoubleEncoded,
};
benchmarks! {
query_holding {
let holding = T::worst_case_holding();
let mut executor = new_executor::<T>(Default::default());
executor.holding = holding.clone().into();
let instruction = Instruction::<XcmCallOf<T>>::QueryHolding {
query_id: Default::default(),
dest: T::valid_destination()?,
// Worst case is looking through all holdings for every asset explicitly.
assets: Definite(holding),
max_response_weight: u64::MAX,
};
let xcm = Xcm(vec![instruction]);
} : {
executor.execute(xcm)?;
} verify {
// The completion of execution above is enough to validate this is completed.
}
// This benchmark does not use any additional orders or instructions. This should be managed
// by the `deep` and `shallow` implementation.
buy_execution {
let holding = T::worst_case_holding().into();
let mut executor = new_executor::<T>(Default::default());
executor.holding = holding;
let fee_asset = Concrete(Here.into());
let instruction = Instruction::<XcmCallOf<T>>::BuyExecution {
fees: (fee_asset, 100_000_000).into(), // should be something inside of holding
weight_limit: WeightLimit::Unlimited,
};
let xcm = Xcm(vec![instruction]);
} : {
executor.execute(xcm)?;
} verify {
}
// Worst case scenario for this benchmark is a large number of assets to
// filter through the reserve.
reserve_asset_deposited {
const MAX_ASSETS: u32 = 100; // TODO when executor has a built in limit, use it here. #4426
let mut executor = new_executor::<T>(Default::default());
let assets = (0..MAX_ASSETS).map(|i| MultiAsset {
id: Abstract(i.encode()),
fun: Fungible(i as u128),
}).collect::<vec::Vec<_>>();
let multiassets: MultiAssets = assets.into();
let instruction = Instruction::ReserveAssetDeposited(multiassets.clone());
let xcm = Xcm(vec![instruction]);
}: {
executor.execute(xcm).map_err(|_| BenchmarkError::Skip)?;
} verify {
assert_eq!(executor.holding, multiassets.into());
}
query_response {
let mut executor = new_executor::<T>(Default::default());
let (query_id, response) = T::worst_case_response();
let max_weight = u64::MAX;
let instruction = Instruction::QueryResponse { query_id, response, max_weight };
let xcm = Xcm(vec![instruction]);
}: {
executor.execute(xcm)?;
} verify {
// The assert above is enough to show this XCM succeeded
}
// We don't care about the call itself, since that is accounted for in the weight parameter
// and included in the final weight calculation. So this is just the overhead of submitting
// a noop call.
transact {
let origin = T::transact_origin()?;
let mut executor = new_executor::<T>(origin);
let noop_call: <T as Config>::Call = frame_system::Call::remark_with_event {
remark: Default::default()
}.into();
let double_encoded_noop_call: DoubleEncoded<_> = noop_call.encode().into();
let instruction = Instruction::Transact {
origin_type: OriginKind::SovereignAccount,
require_weight_at_most: noop_call.get_dispatch_info().weight,
call: double_encoded_noop_call,
};
let xcm = Xcm(vec![instruction]);
let num_events = frame_system::Pallet::<T>::events().len();
}: {
executor.execute(xcm)?;
} verify {
// TODO make better assertion? #4426
let num_events2 = frame_system::Pallet::<T>::events().len();
assert_eq!(num_events + 1, num_events2);
}
refund_surplus {
let holding = T::worst_case_holding().into();
let mut executor = new_executor::<T>(Default::default());
executor.holding = holding;
executor.total_surplus = 1337;
executor.total_refunded = 0;
let instruction = Instruction::<XcmCallOf<T>>::RefundSurplus;
let xcm = Xcm(vec![instruction]);
} : {
let result = executor.execute(xcm)?;
} verify {
assert_eq!(executor.total_surplus, 1337);
assert_eq!(executor.total_refunded, 1337);
}
set_error_handler {
let mut executor = new_executor::<T>(Default::default());
let instruction = Instruction::<XcmCallOf<T>>::SetErrorHandler(Xcm(vec![]));
let xcm = Xcm(vec![instruction]);
} : {
executor.execute(xcm)?;
} verify {
assert_eq!(executor.error_handler, Xcm(vec![]));
}
set_appendix {
let mut executor = new_executor::<T>(Default::default());
let appendix = Xcm(vec![]);
let instruction = Instruction::<XcmCallOf<T>>::SetAppendix(appendix);
let xcm = Xcm(vec![instruction]);
} : {
executor.execute(xcm)?;
} verify {
assert_eq!(executor.appendix, Xcm(vec![]));
}
clear_error {
let mut executor = new_executor::<T>(Default::default());
executor.error = Some((5u32, XcmError::Overflow));
let instruction = Instruction::<XcmCallOf<T>>::ClearError;
let xcm = Xcm(vec![instruction]);
} : {
executor.execute(xcm)?;
} verify {
assert!(executor.error.is_none())
}
descend_origin {
let mut executor = new_executor::<T>(Default::default());
let who = X2(OnlyChild, OnlyChild);
let instruction = Instruction::DescendOrigin(who.clone());
let xcm = Xcm(vec![instruction]);
} : {
executor.execute(xcm)?;
} verify {
assert_eq!(
executor.origin,
Some(MultiLocation {
parents: 0,
interior: who,
}),
);
}
clear_origin {
let mut executor = new_executor::<T>(Default::default());
let instruction = Instruction::ClearOrigin;
let xcm = Xcm(vec![instruction]);
} : {
executor.execute(xcm)?;
} verify {
assert_eq!(executor.origin, None);
}
report_error {
let mut executor = new_executor::<T>(Default::default());
executor.error = Some((0u32, XcmError::Unimplemented));
let query_id = Default::default();
let dest = T::valid_destination().map_err(|_| BenchmarkError::Skip)?;
let max_response_weight = Default::default();
let instruction = Instruction::ReportError { query_id, dest, max_response_weight };
let xcm = Xcm(vec![instruction]);
}: {
executor.execute(xcm)?;
} verify {
// the execution succeeding is all we need to verify this xcm was successful
}
claim_asset {
use xcm_executor::traits::DropAssets;
let (origin, ticket, assets) = T::claimable_asset()?;
// We place some items into the asset trap to claim.
<T::XcmConfig as xcm_executor::Config>::AssetTrap::drop_assets(
&origin,
assets.clone().into(),
);
// Assets should be in the trap now.
let mut executor = new_executor::<T>(origin);
let instruction = Instruction::ClaimAsset { assets: assets.clone(), ticket };
let xcm = Xcm(vec![instruction]);
} :{
executor.execute(xcm)?;
} verify {
assert!(executor.holding.ensure_contains(&assets).is_ok());
}
trap {
let mut executor = new_executor::<T>(Default::default());
let instruction = Instruction::Trap(10);
let xcm = Xcm(vec![instruction]);
// In order to access result in the verification below, it needs to be defined here.
let mut _result = Ok(());
} : {
_result = executor.execute(xcm);
} verify {
match _result {
Err(error) if error.xcm_error == XcmError::Trap(10) => {
// This is the success condition
},
_ => Err("xcm trap did not return the expected error")?
};
}
subscribe_version {
use xcm_executor::traits::VersionChangeNotifier;
let origin = T::subscribe_origin()?;
let query_id = Default::default();
let max_response_weight = Default::default();
let mut executor = new_executor::<T>(origin.clone());
let instruction = Instruction::SubscribeVersion { query_id, max_response_weight };
let xcm = Xcm(vec![instruction]);
} : {
executor.execute(xcm)?;
} verify {
assert!(<T::XcmConfig as xcm_executor::Config>::SubscriptionService::is_subscribed(&origin));
}
unsubscribe_version {
use xcm_executor::traits::VersionChangeNotifier;
// First we need to subscribe to notifications.
let origin = T::transact_origin()?;
let query_id = Default::default();
let max_response_weight = Default::default();
<T::XcmConfig as xcm_executor::Config>::SubscriptionService::start(
&origin,
query_id,
max_response_weight
).map_err(|_| "Could not start subscription")?;
assert!(<T::XcmConfig as xcm_executor::Config>::SubscriptionService::is_subscribed(&origin));
let mut executor = new_executor::<T>(origin.clone());
let instruction = Instruction::UnsubscribeVersion;
let xcm = Xcm(vec![instruction]);
} : {
executor.execute(xcm)?;
} verify {
assert!(!<T::XcmConfig as xcm_executor::Config>::SubscriptionService::is_subscribed(&origin));
}
initiate_reserve_withdraw {
let holding = T::worst_case_holding();
let assets_filter = MultiAssetFilter::Definite(holding.clone());
let reserve = T::valid_destination().map_err(|_| BenchmarkError::Skip)?;
let mut executor = new_executor::<T>(Default::default());
executor.holding = holding.into();
let instruction = Instruction::InitiateReserveWithdraw { assets: assets_filter, reserve, xcm: Xcm(vec![]) };
let xcm = Xcm(vec![instruction]);
}:{
executor.execute(xcm)?;
} verify {
// The execute completing successfully is as good as we can check.
// TODO: Potentially add new trait to XcmSender to detect a queued outgoing message. #4426
}
impl_benchmark_test_suite!(
Pallet,
crate::generic::mock::new_test_ext(),
crate::generic::mock::Test
);
}
@@ -0,0 +1,171 @@
// Copyright 2021 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot 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.
// Polkadot 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 Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! A mock runtime for XCM benchmarking.
use crate::{generic, mock::*, *};
use frame_support::{
parameter_types,
traits::{Everything, OriginTrait},
};
use sp_core::H256;
use sp_runtime::{
testing::Header,
traits::{BlakeTwo256, IdentityLookup},
BuildStorage,
};
use xcm_builder::{
test_utils::{Assets, TestAssetTrap, TestSubscriptionService},
AllowUnpaidExecutionFrom,
};
use xcm_executor::traits::ConvertOrigin;
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;
frame_support::construct_runtime!(
pub enum Test where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
XcmGenericBenchmarks: generic::{Pallet},
}
);
parameter_types! {
pub const BlockHashCount: u64 = 250;
pub BlockWeights: frame_system::limits::BlockWeights =
frame_system::limits::BlockWeights::simple_max(1024);
}
impl frame_system::Config for Test {
type BaseCallFilter = Everything;
type BlockWeights = ();
type BlockLength = ();
type DbWeight = ();
type Origin = Origin;
type Index = u64;
type BlockNumber = u64;
type Hash = H256;
type Call = Call;
type Hashing = BlakeTwo256;
type AccountId = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type Event = Event;
type BlockHashCount = BlockHashCount;
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<u64>;
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type SS58Prefix = ();
type OnSetCode = ();
}
/// The benchmarks in this pallet should never need an asset transactor to begin with.
pub struct NoAssetTransactor;
impl xcm_executor::traits::TransactAsset for NoAssetTransactor {
fn deposit_asset(_: &MultiAsset, _: &MultiLocation) -> Result<(), XcmError> {
unreachable!();
}
fn withdraw_asset(_: &MultiAsset, _: &MultiLocation) -> Result<Assets, XcmError> {
unreachable!();
}
}
parameter_types! {
pub const MaxInstructions: u32 = 100;
}
pub struct XcmConfig;
impl xcm_executor::Config for XcmConfig {
type Call = Call;
type XcmSender = DevNull;
type AssetTransactor = NoAssetTransactor;
type OriginConverter = AlwaysSignedByDefault<Origin>;
type IsReserve = AllAssetLocationsPass;
type IsTeleporter = ();
type LocationInverter = xcm_builder::LocationInverter<Ancestry>;
type Barrier = AllowUnpaidExecutionFrom<Everything>;
type Weigher = xcm_builder::FixedWeightBounds<UnitWeightCost, Call, MaxInstructions>;
type Trader = xcm_builder::FixedRateOfFungible<WeightPrice, ()>;
type ResponseHandler = DevNull;
type AssetTrap = TestAssetTrap;
type AssetClaims = TestAssetTrap;
type SubscriptionService = TestSubscriptionService;
}
impl crate::Config for Test {
type XcmConfig = XcmConfig;
type AccountIdConverter = AccountIdConverter;
fn valid_destination() -> Result<MultiLocation, BenchmarkError> {
let valid_destination: MultiLocation =
Junction::AccountId32 { network: NetworkId::Any, id: [0u8; 32] }.into();
Ok(valid_destination)
}
fn worst_case_holding() -> MultiAssets {
crate::mock_worst_case_holding()
}
}
impl generic::Config for Test {
type Call = Call;
fn worst_case_response() -> (u64, Response) {
let assets: MultiAssets = (Concrete(Here.into()), 100).into();
(0, Response::Assets(assets))
}
fn transact_origin() -> Result<MultiLocation, BenchmarkError> {
Ok(Default::default())
}
fn subscribe_origin() -> Result<MultiLocation, BenchmarkError> {
Ok(Default::default())
}
fn claimable_asset() -> Result<(MultiLocation, MultiLocation, MultiAssets), BenchmarkError> {
let assets: MultiAssets = (Concrete(Here.into()), 100).into();
let ticket = MultiLocation { parents: 0, interior: X1(GeneralIndex(0)) };
Ok((Default::default(), ticket, assets))
}
}
pub fn new_test_ext() -> sp_io::TestExternalities {
let t = GenesisConfig { ..Default::default() }.build_storage().unwrap();
sp_tracing::try_init_simple();
t.into()
}
pub struct AlwaysSignedByDefault<Origin>(core::marker::PhantomData<Origin>);
impl<Origin> ConvertOrigin<Origin> for AlwaysSignedByDefault<Origin>
where
Origin: OriginTrait,
<Origin as OriginTrait>::AccountId: Default,
{
fn convert_origin(
_origin: impl Into<MultiLocation>,
_kind: OriginKind,
) -> Result<Origin, MultiLocation> {
Ok(Origin::signed(Default::default()))
}
}
@@ -0,0 +1,39 @@
pub use pallet::*;
pub mod benchmarking;
#[cfg(test)]
mod mock;
#[frame_support::pallet]
pub mod pallet {
use frame_benchmarking::BenchmarkError;
use frame_support::{dispatch::Dispatchable, pallet_prelude::Encode, weights::GetDispatchInfo};
use xcm::latest::{MultiAssets, MultiLocation, Response};
#[pallet::config]
pub trait Config<I: 'static = ()>: frame_system::Config + crate::Config {
type Call: Dispatchable<Origin = Self::Origin>
+ GetDispatchInfo
+ From<frame_system::Call<Self>>
+ Encode;
/// The response which causes the most runtime weight.
fn worst_case_response() -> (u64, Response);
/// The `MultiLocation` used for successful transaction XCMs.
///
/// If set to `None`, benchmarks which rely on a `transact_origin` will be skipped.
fn transact_origin() -> Result<MultiLocation, BenchmarkError>;
/// A valid `MultiLocation` we can successfully subscribe to.
///
/// If set to `None`, benchmarks which rely on a `subscribe_origin` will be skipped.
fn subscribe_origin() -> Result<MultiLocation, BenchmarkError>;
/// Return an origin, ticket, and assets that can be trapped and claimed.
fn claimable_asset() -> Result<(MultiLocation, MultiLocation, MultiAssets), BenchmarkError>;
}
#[pallet::pallet]
pub struct Pallet<T, I = ()>(_);
}
+11 -9
View File
@@ -19,12 +19,13 @@
#![cfg_attr(not(feature = "std"), no_std)]
use codec::Encode;
use frame_benchmarking::account;
use frame_benchmarking::{account, BenchmarkError};
use sp_std::prelude::*;
use xcm::latest::prelude::*;
use xcm_executor::{traits::Convert, Assets};
use xcm_executor::traits::Convert;
pub mod fungible;
pub mod generic;
#[cfg(test)]
mod mock;
@@ -42,7 +43,10 @@ pub trait Config: frame_system::Config {
/// Does any necessary setup to create a valid destination for XCM messages.
/// Returns that destination's multi-location to be used in benchmarks.
fn valid_destination() -> Result<MultiLocation, sp_runtime::DispatchError>;
fn valid_destination() -> Result<MultiLocation, BenchmarkError>;
/// Worst case scenario for a holding account in this runtime.
fn worst_case_holding() -> MultiAssets;
}
const SEED: u32 = 0;
@@ -56,12 +60,10 @@ pub type AssetTransactorOf<T> = <<T as Config>::XcmConfig as xcm_executor::Confi
/// The call type of executor's config. Should eventually resolve to the same overarching call type.
pub type XcmCallOf<T> = <<T as Config>::XcmConfig as xcm_executor::Config>::Call;
/// The worst case number of assets in the holding.
const HOLDING_FUNGIBLES: u32 = 99;
const HOLDING_NON_FUNGIBLES: u32 = 99;
pub fn worst_case_holding() -> Assets {
let fungibles_amount: u128 = 100; // TODO probably update
pub fn mock_worst_case_holding() -> MultiAssets {
const HOLDING_FUNGIBLES: u32 = 99;
const HOLDING_NON_FUNGIBLES: u32 = 99;
let fungibles_amount: u128 = 100;
(0..HOLDING_FUNGIBLES)
.map(|i| {
MultiAsset {
+15 -15
View File
@@ -21,7 +21,7 @@
//! EXECUTION: {{cmd.execution}}, WASM-EXECUTION: {{cmd.wasm_execution}}, CHAIN: {{cmd.chain}}, DB CACHE: {{cmd.db_cache}}
// Executed Command:
{{#each args as |arg|~}}
{{#each args as |arg|}}
// {{arg}}
{{/each}}
@@ -35,32 +35,32 @@ use sp_std::marker::PhantomData;
/// Weights for `{{pallet}}`.
pub struct WeightInfo<T>(PhantomData<T>);
impl<T: frame_system::Config> WeightInfo<T> {
{{~#each benchmarks as |benchmark|}}
{{~#each benchmark.comments as |comment|}}
{{#each benchmarks as |benchmark|}}
{{#each benchmark.comments as |comment|}}
// {{comment}}
{{~/each}}
{{/each}}
pub(crate) fn {{benchmark.name~}}
(
{{~#each benchmark.components as |c| ~}}
{{~#if (not c.is_used)}}_{{/if}}{{c.name}}: u32, {{/each~}}
) -> Weight {
({{underscore benchmark.base_weight}} as Weight)
{{~#each benchmark.component_weight as |cw|}}
{{#each benchmark.component_weight as |cw|}}
// Standard Error: {{underscore cw.error}}
.saturating_add(({{underscore cw.slope}} as Weight).saturating_mul({{cw.name}} as Weight))
{{~/each}}
{{~#if (ne benchmark.base_reads "0")}}
{{/each}}
{{#if (ne benchmark.base_reads "0")}}
.saturating_add(T::DbWeight::get().reads({{benchmark.base_reads}} as Weight))
{{~/if}}
{{~#each benchmark.component_reads as |cr|}}
{{/if}}
{{#each benchmark.component_reads as |cr|}}
.saturating_add(T::DbWeight::get().reads(({{cr.slope}} as Weight).saturating_mul({{cr.name}} as Weight)))
{{~/each}}
{{~#if (ne benchmark.base_writes "0")}}
{{/each}}
{{#if (ne benchmark.base_writes "0")}}
.saturating_add(T::DbWeight::get().writes({{benchmark.base_writes}} as Weight))
{{~/if}}
{{~#each benchmark.component_writes as |cw|}}
{{/if}}
{{#each benchmark.component_writes as |cw|}}
.saturating_add(T::DbWeight::get().writes(({{cw.slope}} as Weight).saturating_mul({{cw.name}} as Weight)))
{{~/each}}
{{/each}}
}
{{~/each}}
{{/each}}
}