diff --git a/Cargo.lock b/Cargo.lock index 6800652949..d7c7b65e46 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -636,6 +636,7 @@ dependencies = [ "log", "pallet-assets", "pallet-balances", + "pallet-xc-asset-config", "pallet-xcm", "parity-scale-codec", "paste", diff --git a/pallets/astar-xcm-benchmarks/Cargo.toml b/pallets/astar-xcm-benchmarks/Cargo.toml index d97e16ae90..de5c7a3a63 100644 --- a/pallets/astar-xcm-benchmarks/Cargo.toml +++ b/pallets/astar-xcm-benchmarks/Cargo.toml @@ -31,6 +31,7 @@ frame-benchmarking = { workspace = true } [dev-dependencies] pallet-assets = { workspace = true } pallet-balances = { workspace = true } +pallet-xc-asset-config = { workspace = true } pallet-xcm = { workspace = true } paste = { workspace = true } sp-core = { workspace = true } @@ -55,6 +56,7 @@ std = [ "astar-primitives/std", "pallet-balances/std", "pallet-assets/std", + "pallet-xc-asset-config/std", ] runtime-benchmarks = [ diff --git a/pallets/astar-xcm-benchmarks/src/fungible/assets/benchmarking.rs b/pallets/astar-xcm-benchmarks/src/fungible/assets/benchmarking.rs index 85c9182a2e..76b4c79d92 100644 --- a/pallets/astar-xcm-benchmarks/src/fungible/assets/benchmarking.rs +++ b/pallets/astar-xcm-benchmarks/src/fungible/assets/benchmarking.rs @@ -19,33 +19,35 @@ // Copyright (C) Parity Technologies (UK) Ltd. use super::*; use crate::{account_and_location, new_executor, AssetTransactorOf, XcmCallOf}; -use frame_benchmarking::{benchmarks_instance_pallet, BenchmarkError, BenchmarkResult}; +use frame_benchmarking::v2::*; use frame_support::traits::fungible::Inspect; use sp_runtime::traits::Zero; use sp_std::{prelude::*, vec}; use xcm::latest::{prelude::*, Weight}; use xcm_executor::traits::{Convert, TransactAsset}; -benchmarks_instance_pallet! { - where_clause { where +#[benchmarks( + where + < < - < - T::TransactAsset - as - Inspect - >::Balance + ::TransactAsset as - TryInto - >::Error: sp_std::fmt::Debug, + Inspect + >::Balance + as + TryInto + >::Error: sp_std::fmt::Debug, - < T::TransactAsset - as - Inspect - >::Balance : Into - - } - - withdraw_asset { + < ::TransactAsset + as + Inspect + >::Balance : Into +)] +mod benchmarks { + use super::*; + + #[benchmark] + fn withdraw_asset() -> Result<(), BenchmarkError> { let (sender_account, sender_location) = account_and_location::(1); let worst_case_holding = T::worst_case_holding(0); let asset = T::get_multi_asset(); @@ -58,7 +60,8 @@ benchmarks_instance_pallet! { message_hash: [0; 32], topic: None, }, - ).unwrap(); + ) + .unwrap(); // check the assets of origin. assert!(!T::TransactAsset::balance(&sender_account).is_zero()); @@ -66,18 +69,23 @@ benchmarks_instance_pallet! { executor.set_holding(worst_case_holding.into()); let instruction = Instruction::>::WithdrawAsset(vec![asset.clone()].into()); let xcm = Xcm(vec![instruction]); - }: { - executor.bench_process(xcm)?; - } verify { + #[block] + { + executor.bench_process(xcm)?; + } // check one of the assets of origin. assert!(T::TransactAsset::balance(&sender_account).is_zero()); - assert!(executor.holding().ensure_contains(&vec![asset].into()).is_ok()); + assert!(executor + .holding() + .ensure_contains(&vec![asset].into()) + .is_ok()); + Ok(()) } - transfer_asset { + #[benchmark] + fn transfer_asset() -> Result<(), BenchmarkError> { let (sender_account, sender_location) = account_and_location::(1); let mut asset = T::get_multi_asset(); - let assets: MultiAssets = vec![ asset.clone() ].into(); // this xcm doesn't use holding let dest_location = T::valid_destination()?; @@ -90,15 +98,16 @@ benchmarks_instance_pallet! { message_hash: [0; 32], topic: None, }, - ).unwrap(); + ) + .unwrap(); assert!(T::TransactAsset::balance(&dest_account).is_zero()); // reducing some assets for Existential deposit if let Fungible(x) = asset.fun { - asset.fun = Fungible(x/10) + asset.fun = Fungible(x / 10) }; - let assets: MultiAssets = vec![ asset.clone() ].into(); + let assets: MultiAssets = vec![asset.clone()].into(); log::trace!( target: "xcm::process", "assets is {:?}",assets.clone()); @@ -112,26 +121,35 @@ benchmarks_instance_pallet! { message_hash: [0; 32], topic: None, }, - ).unwrap(); + ) + .unwrap(); let mut executor = new_executor::(sender_location); - let instruction = Instruction::TransferAsset { assets, beneficiary: dest_location }; + let instruction = Instruction::TransferAsset { + assets, + beneficiary: dest_location, + }; let xcm = Xcm(vec![instruction]); log::trace!( target: "xcm::process", "destination balance is {:?}, sender balance is {:?}",T::TransactAsset::balance(&dest_account),T::TransactAsset::balance(&sender_account)); assert!(!T::TransactAsset::balance(&dest_account).is_zero()); let previous_balance: u128 = T::TransactAsset::balance(&dest_account).into(); - }: { - executor.bench_process(xcm)?; - } verify { + + #[block] + { + executor.bench_process(xcm)?; + } + log::trace!( - target: "xcm::process", - "destination balance is {:?}, sender balance is {:?}",T::TransactAsset::balance(&dest_account),T::TransactAsset::balance(&sender_account)); + target: "xcm::process", + "destination balance is {:?}, sender balance is {:?}",T::TransactAsset::balance(&dest_account),T::TransactAsset::balance(&sender_account)); assert!(T::TransactAsset::balance(&dest_account).into() == 2 * previous_balance); + Ok(()) } - transfer_reserve_asset { - let (sender_account, sender_location) = account_and_location::(1); + #[benchmark] + fn transfer_reserve_asset() -> Result<(), BenchmarkError> { + let (_, sender_location) = account_and_location::(1); let dest_location = T::valid_destination()?; let dest_account = T::AccountIdConverter::convert(dest_location.clone()).unwrap(); @@ -144,12 +162,13 @@ benchmarks_instance_pallet! { message_hash: [0; 32], topic: None, }, - ).unwrap(); + ) + .unwrap(); assert!(T::TransactAsset::balance(&dest_account).is_zero()); if let Fungible(x) = asset.fun { - asset.fun = Fungible(x/10) + asset.fun = Fungible(x / 10) }; >::transfer_asset( @@ -161,29 +180,40 @@ benchmarks_instance_pallet! { message_hash: [0; 32], topic: None, }, - ).unwrap(); - let assets: MultiAssets = vec![ asset ].into(); + ) + .unwrap(); + let assets: MultiAssets = vec![asset].into(); let mut executor = new_executor::(sender_location); let instruction = Instruction::TransferReserveAsset { assets, dest: dest_location, - xcm: Xcm::new() + xcm: Xcm::new(), }; let xcm = Xcm(vec![instruction]); assert!(!T::TransactAsset::balance(&dest_account).is_zero()); let previous_balance: u128 = T::TransactAsset::balance(&dest_account).into(); - }: { - executor.bench_process(xcm)?; - } verify { + + #[block] + { + executor.bench_process(xcm)?; + } + assert!(T::TransactAsset::balance(&dest_account).into() == 2 * previous_balance); + Ok(()) } - receive_teleported_asset { - } : { - Err(BenchmarkError::Override(BenchmarkResult::from_weight(Weight::MAX)))?; + #[benchmark] + fn receive_teleported_asset() -> Result<(), BenchmarkError> { + // need to add an empty block as it is necessary to have either #[block] or #[extrinsic_call] + #[block] + {} + Err(BenchmarkError::Override(BenchmarkResult::from_weight( + Weight::MAX, + ))) } - deposit_asset { + #[benchmark] + fn deposit_asset() -> Result<(), BenchmarkError> { let asset = T::get_multi_asset(); let mut holding = T::worst_case_holding(1); @@ -202,14 +232,18 @@ benchmarks_instance_pallet! { beneficiary: dest_location, }; let xcm = Xcm(vec![instruction]); - }: { - executor.bench_process(xcm)?; - } verify { + + #[block] + { + executor.bench_process(xcm)?; + } // dest should have received some asset. - assert!(!T::TransactAsset::balance(&dest_account).is_zero()) + assert!(!T::TransactAsset::balance(&dest_account).is_zero()); + Ok(()) } - deposit_reserve_asset { + #[benchmark] + fn deposit_reserve_asset() -> Result<(), BenchmarkError> { let asset = T::get_multi_asset(); let mut holding = T::worst_case_holding(1); @@ -229,21 +263,29 @@ benchmarks_instance_pallet! { xcm: Xcm::new(), }; let xcm = Xcm(vec![instruction]); - }: { - executor.bench_process(xcm)?; - } verify { + + #[block] + { + executor.bench_process(xcm)?; + } + // dest should have received some asset. - assert!(!T::TransactAsset::balance(&dest_account).is_zero()) + assert!(!T::TransactAsset::balance(&dest_account).is_zero()); + Ok(()) } - initiate_teleport { - }: { - Err(BenchmarkError::Override(BenchmarkResult::from_weight(Weight::MAX)))?; + #[benchmark] + fn initiate_teleport() -> Result<(), BenchmarkError> { + #[block] + {} + Err(BenchmarkError::Override(BenchmarkResult::from_weight( + Weight::MAX, + ))) } impl_benchmark_test_suite!( Pallet, - crate::fungible::mock::new_test_ext(), - crate::fungible::mock::Test + crate::fungible::assets::mock::new_test_ext(), + crate::fungible::assets::mock::Test ); } diff --git a/pallets/astar-xcm-benchmarks/src/fungible/assets/mock.rs b/pallets/astar-xcm-benchmarks/src/fungible/assets/mock.rs index 5feec89f7d..842b320382 100644 --- a/pallets/astar-xcm-benchmarks/src/fungible/assets/mock.rs +++ b/pallets/astar-xcm-benchmarks/src/fungible/assets/mock.rs @@ -22,13 +22,13 @@ use crate::{fungible::assets as xcm_assets_benchmark, mock::*}; use frame_benchmarking::BenchmarkError; use frame_support::{ - parameter_types, - traits::{AsEnsureOriginWithArg, ConstU32, Everything, Nothing}, + assert_ok, parameter_types, + traits::{tokens::fungible::ItemOf, AsEnsureOriginWithArg, ConstU32, Everything, Nothing}, weights::Weight, }; use frame_system::{EnsureRoot, EnsureSigned}; use parity_scale_codec::Compact; -use sp_core::H256; +use sp_core::{ConstU64, H256}; use sp_runtime::{ testing::Header, traits::{BlakeTwo256, IdentityLookup}, @@ -53,6 +53,7 @@ frame_support::construct_runtime!( Balances: pallet_balances, XcmAssetsBenchmark: xcm_assets_benchmark, Assets: pallet_assets, + XcAssetConfig: pallet_xc_asset_config, } ); @@ -119,6 +120,13 @@ parameter_types! { } +impl pallet_xc_asset_config::Config for Test { + type RuntimeEvent = RuntimeEvent; + type AssetId = u64; + type ManagerOrigin = EnsureRoot; + type WeightInfo = (); +} + impl pallet_assets::Config for Test { type RuntimeEvent = RuntimeEvent; type Balance = u64; @@ -139,7 +147,16 @@ impl pallet_assets::Config for Test { type AssetIdParameter = Compact; type CallbackHandle = (); #[cfg(feature = "runtime-benchmarks")] - type BenchmarkHelper = (); + type BenchmarkHelper = AssetsBenchmarkHelper; +} + +pub struct AssetsBenchmarkHelper; +impl> pallet_assets::BenchmarkHelper + for AssetsBenchmarkHelper +{ + fn create_asset_id_parameter(id: u32) -> AssetIdParameter { + u64::from(id).into() + } } parameter_types! { @@ -223,7 +240,6 @@ impl crate::Config for Test { ) } } - pub type TrustedTeleporters = (xcm_builder::Case,); parameter_types! { @@ -237,29 +253,40 @@ parameter_types! { (Wild(AllOf { fun: WildFungible, id: Concrete(Here.into_location()) }), ChildTeleporter::get()); pub const ReserveConcreteFungible: (MultiAssetFilter, MultiLocation) = (Wild(AllOf { fun: WildFungible, id: Concrete(Here.into_location()) }), ChildTeleporter::get()); + pub const NoCheckingAccount: Option<(::AccountId, MintLocation)> = None; + pub const NoTeleporter: Option<(xcm::latest::MultiLocation, xcm::latest::MultiAsset)> = None; } impl xcm_assets_benchmark::Config for Test { - type TransactAsset = Balances; - type CheckedAccount = CheckingAccount; - type TrustedTeleporter = TrustedTeleporter; + type TransactAsset = ItemOf, u64>; + type CheckedAccount = NoCheckingAccount; + type TrustedTeleporter = NoTeleporter; fn get_multi_asset() -> MultiAsset { - let amount = - >::minimum_balance() as u128; + // create an asset and make it sufficient + assert_ok!(pallet_assets::Pallet::::force_create( + RuntimeOrigin::root(), + parity_scale_codec::Compact(1), + 0_u64, + true, + 1 + )); let location = MultiLocation { - parents: 1, - interior: X2( - Parachain(1000), - AccountId32 { - network: None, - id: [152; 32], - }, - ), + parents: 0, + interior: X1(GeneralIndex(1)), }; + // convert mapping for asset id + assert_ok!( + pallet_xc_asset_config::Pallet::::register_asset_location( + RuntimeOrigin::root(), + Box::new(location.clone().into_versioned()), + 1 + ) + ); + MultiAsset { id: Concrete(location), - fun: Fungible(amount), + fun: Fungible(100_000_000_000u128), } } } @@ -267,7 +294,7 @@ impl xcm_assets_benchmark::Config for Test { #[cfg(feature = "runtime-benchmarks")] pub fn new_test_ext() -> sp_io::TestExternalities { use sp_runtime::BuildStorage; - let t = RuntimeGenesisConfig { + let t = GenesisConfig { ..Default::default() } .build_storage() diff --git a/pallets/astar-xcm-benchmarks/src/fungible/balances/benchmarking.rs b/pallets/astar-xcm-benchmarks/src/fungible/balances/benchmarking.rs index 312b0141fd..464a0169e3 100644 --- a/pallets/astar-xcm-benchmarks/src/fungible/balances/benchmarking.rs +++ b/pallets/astar-xcm-benchmarks/src/fungible/balances/benchmarking.rs @@ -19,27 +19,30 @@ // Copyright (C) Parity Technologies (UK) Ltd. use super::*; use crate::{account_and_location, new_executor, AssetTransactorOf, XcmCallOf}; -use frame_benchmarking::{benchmarks_instance_pallet, BenchmarkError, BenchmarkResult}; +use frame_benchmarking::v2::*; use frame_support::traits::fungible::Inspect; use sp_runtime::traits::Zero; use sp_std::{prelude::*, vec}; use xcm::latest::{prelude::*, Weight}; use xcm_executor::traits::{Convert, TransactAsset}; -benchmarks_instance_pallet! { - where_clause { where +#[benchmarks( + where + < < - < - T::TransactAsset - as - Inspect - >::Balance + ::TransactAsset as - TryInto - >::Error: sp_std::fmt::Debug, - } - - withdraw_asset { + Inspect + >::Balance + as + TryInto + >::Error: sp_std::fmt::Debug, +)] +mod benchmarks { + use super::*; + + #[benchmark] + fn withdraw_asset() -> Result<(), BenchmarkError> { let (sender_account, sender_location) = account_and_location::(1); let worst_case_holding = T::worst_case_holding(0); let asset = T::get_multi_asset(); @@ -52,7 +55,8 @@ benchmarks_instance_pallet! { message_hash: [0; 32], topic: None, }, - ).unwrap(); + ) + .unwrap(); // check the assets of origin. assert!(!T::TransactAsset::balance(&sender_account).is_zero()); @@ -60,18 +64,24 @@ benchmarks_instance_pallet! { executor.set_holding(worst_case_holding.into()); let instruction = Instruction::>::WithdrawAsset(vec![asset.clone()].into()); let xcm = Xcm(vec![instruction]); - }: { - executor.bench_process(xcm)?; - } verify { + #[block] + { + executor.bench_process(xcm)?; + } // check one of the assets of origin. assert!(T::TransactAsset::balance(&sender_account).is_zero()); - assert!(executor.holding().ensure_contains(&vec![asset].into()).is_ok()); + assert!(executor + .holding() + .ensure_contains(&vec![asset].into()) + .is_ok()); + Ok(()) } - transfer_asset { + #[benchmark] + fn transfer_asset() -> Result<(), BenchmarkError> { let (sender_account, sender_location) = account_and_location::(1); let asset = T::get_multi_asset(); - let assets: MultiAssets = vec![ asset.clone() ].into(); + let assets: MultiAssets = vec![asset.clone()].into(); // this xcm doesn't use holding let dest_location = T::valid_destination()?; @@ -85,20 +95,29 @@ benchmarks_instance_pallet! { message_hash: [0; 32], topic: None, }, - ).unwrap(); + ) + .unwrap(); assert!(T::TransactAsset::balance(&dest_account).is_zero()); let mut executor = new_executor::(sender_location); - let instruction = Instruction::TransferAsset { assets, beneficiary: dest_location }; + let instruction = Instruction::TransferAsset { + assets, + beneficiary: dest_location, + }; let xcm = Xcm(vec![instruction]); - }: { - executor.bench_process(xcm)?; - } verify { + + #[block] + { + executor.bench_process(xcm)?; + } + assert!(T::TransactAsset::balance(&sender_account).is_zero()); assert!(!T::TransactAsset::balance(&dest_account).is_zero()); + Ok(()) } - transfer_reserve_asset { + #[benchmark] + fn transfer_reserve_asset() -> Result<(), BenchmarkError> { let (sender_account, sender_location) = account_and_location::(1); let dest_location = T::valid_destination()?; let dest_account = T::AccountIdConverter::convert(dest_location.clone()).unwrap(); @@ -112,30 +131,41 @@ benchmarks_instance_pallet! { message_hash: [0; 32], topic: None, }, - ).unwrap(); - let assets: MultiAssets = vec![ asset ].into(); + ) + .unwrap(); + let assets: MultiAssets = vec![asset].into(); assert!(T::TransactAsset::balance(&dest_account).is_zero()); let mut executor = new_executor::(sender_location); let instruction = Instruction::TransferReserveAsset { assets, dest: dest_location, - xcm: Xcm::new() + xcm: Xcm::new(), }; let xcm = Xcm(vec![instruction]); - }: { - executor.bench_process(xcm)?; - } verify { + + #[block] + { + executor.bench_process(xcm)?; + } + assert!(T::TransactAsset::balance(&sender_account).is_zero()); assert!(!T::TransactAsset::balance(&dest_account).is_zero()); + Ok(()) } - receive_teleported_asset { - } : { - Err(BenchmarkError::Override(BenchmarkResult::from_weight(Weight::MAX)))?; + #[benchmark] + fn receive_teleported_asset() -> Result<(), BenchmarkError> { + // need to add an empty block as it is necessary to have either #[block] or #[extrinsic_call] + #[block] + {} + Err(BenchmarkError::Override(BenchmarkResult::from_weight( + Weight::MAX, + ))) } - deposit_asset { + #[benchmark] + fn deposit_asset() -> Result<(), BenchmarkError> { let asset = T::get_multi_asset(); let mut holding = T::worst_case_holding(1); @@ -154,14 +184,18 @@ benchmarks_instance_pallet! { beneficiary: dest_location, }; let xcm = Xcm(vec![instruction]); - }: { - executor.bench_process(xcm)?; - } verify { + + #[block] + { + executor.bench_process(xcm)?; + } // dest should have received some asset. - assert!(!T::TransactAsset::balance(&dest_account).is_zero()) + assert!(!T::TransactAsset::balance(&dest_account).is_zero()); + Ok(()) } - deposit_reserve_asset { + #[benchmark] + fn deposit_reserve_asset() -> Result<(), BenchmarkError> { let asset = T::get_multi_asset(); let mut holding = T::worst_case_holding(1); @@ -181,21 +215,28 @@ benchmarks_instance_pallet! { xcm: Xcm::new(), }; let xcm = Xcm(vec![instruction]); - }: { - executor.bench_process(xcm)?; - } verify { + + #[block] + { + executor.bench_process(xcm)?; + } + // dest should have received some asset. - assert!(!T::TransactAsset::balance(&dest_account).is_zero()) + assert!(!T::TransactAsset::balance(&dest_account).is_zero()); + Ok(()) } - initiate_teleport { - }: { - Err(BenchmarkError::Override(BenchmarkResult::from_weight(Weight::MAX)))?; + #[benchmark] + fn initiate_teleport() -> Result<(), BenchmarkError> { + #[block] + {} + Err(BenchmarkError::Override(BenchmarkResult::from_weight( + Weight::MAX, + ))) } - impl_benchmark_test_suite!( Pallet, - crate::fungible::mock::new_test_ext(), - crate::fungible::mock::Test + crate::fungible::balances::mock::new_test_ext(), + crate::fungible::balances::mock::Test ); } diff --git a/pallets/astar-xcm-benchmarks/src/fungible/balances/mock.rs b/pallets/astar-xcm-benchmarks/src/fungible/balances/mock.rs index 347ebc2eb0..e8ed442036 100644 --- a/pallets/astar-xcm-benchmarks/src/fungible/balances/mock.rs +++ b/pallets/astar-xcm-benchmarks/src/fungible/balances/mock.rs @@ -219,7 +219,7 @@ impl xcm_balances_benchmark::Config for Test { #[cfg(feature = "runtime-benchmarks")] pub fn new_test_ext() -> sp_io::TestExternalities { use sp_runtime::BuildStorage; - let t = RuntimeGenesisConfig { + let t = GenesisConfig { ..Default::default() } .build_storage() diff --git a/pallets/astar-xcm-benchmarks/src/generic/benchmarking.rs b/pallets/astar-xcm-benchmarks/src/generic/benchmarking.rs index 4e1bbe71b9..0fe0a6c4d5 100644 --- a/pallets/astar-xcm-benchmarks/src/generic/benchmarking.rs +++ b/pallets/astar-xcm-benchmarks/src/generic/benchmarking.rs @@ -20,7 +20,7 @@ use super::*; use crate::{new_executor, XcmCallOf}; -use frame_benchmarking::{benchmarks, BenchmarkError, BenchmarkResult}; +use frame_benchmarking::v2::*; use frame_support::dispatch::GetDispatchInfo; use parity_scale_codec::Encode; use sp_std::vec; @@ -30,8 +30,12 @@ use xcm::{ }; use xcm_executor::{ExecutorError, FeesMode}; -benchmarks! { - report_holding { +#[benchmarks] +mod benchmarks { + use super::*; + + #[benchmark] + fn report_holding() -> Result<(), BenchmarkError> { let holding = T::worst_case_holding(0); let mut executor = new_executor::(Default::default()); @@ -46,18 +50,18 @@ benchmarks! { // Worst case is looking through all holdings for every asset explicitly. assets: Definite(holding), }; - let xcm = Xcm(vec![instruction]); - } : { - executor.bench_process(xcm)?; - } verify { + #[block] + { + executor.bench_process(xcm)?; + } // The completion of execution above is enough to validate this is completed. + Ok(()) } - // This benchmark does not use any additional orders or instructions. This should be managed - // by the `deep` and `shallow` implementation. - buy_execution { + #[benchmark] + fn buy_execution() -> Result<(), BenchmarkError> { let holding = T::worst_case_holding(0).into(); let mut executor = new_executor::(Default::default()); @@ -67,33 +71,44 @@ benchmarks! { let instruction = Instruction::>::BuyExecution { fees: (fee_asset, 100_000_000_000u128).into(), // should be something inside of holding - weight_limit: WeightLimit::Limited(Weight::from_parts(1u64, 64*1024)), + weight_limit: WeightLimit::Limited(Weight::from_parts(1u64, 64 * 1024)), }; let xcm = Xcm(vec![instruction]); - } : { - executor.bench_process(xcm)?; - } verify { + #[block] + { + executor.bench_process(xcm)?; + } + Ok(()) } - query_response { + #[benchmark] + fn query_response() -> Result<(), BenchmarkError> { let mut executor = new_executor::(Default::default()); let (query_id, response) = T::worst_case_response(); let max_weight = Weight::MAX; let querier: Option = Some(Here.into()); - let instruction = Instruction::QueryResponse { query_id, response, max_weight, querier }; + let instruction = Instruction::QueryResponse { + query_id, + response, + max_weight, + querier, + }; let xcm = Xcm(vec![instruction]); - }: { - executor.bench_process(xcm)?; - } verify { - // The assert above is enough to show this XCM succeeded + + #[block] + { + executor.bench_process(xcm)?; + } + Ok(()) } // 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 { + #[benchmark] + fn transact() -> Result<(), BenchmarkError> { let (origin, noop_call) = T::transact_origin_and_runtime_call()?; let mut executor = new_executor::(origin); let double_encoded_noop_call: DoubleEncoded<_> = noop_call.encode().into(); @@ -104,13 +119,17 @@ benchmarks! { call: double_encoded_noop_call, }; let xcm = Xcm(vec![instruction]); - }: { - executor.bench_process(xcm)?; - } verify { + + #[block] + { + executor.bench_process(xcm)?; + } // The assert above is enough to show this XCM succeeded + Ok(()) } - refund_surplus { + #[benchmark] + fn refund_surplus() -> Result<(), BenchmarkError> { let holding = T::worst_case_holding(0).into(); let mut executor = new_executor::(Default::default()); executor.set_holding(holding); @@ -119,53 +138,69 @@ benchmarks! { let instruction = Instruction::>::RefundSurplus; let xcm = Xcm(vec![instruction]); - } : { - let result = executor.bench_process(xcm)?; - } verify { + + #[block] + { + executor.bench_process(xcm)?; + } + assert_eq!(executor.total_surplus(), &Weight::from_parts(1337, 1337)); assert_eq!(executor.total_refunded(), &Weight::from_parts(1337, 1337)); + Ok(()) } - set_error_handler { + #[benchmark] + fn set_error_handler() -> Result<(), BenchmarkError> { let mut executor = new_executor::(Default::default()); let instruction = Instruction::>::SetErrorHandler(Xcm(vec![])); let xcm = Xcm(vec![instruction]); - } : { - executor.bench_process(xcm)?; - } verify { + + #[block] + { + executor.bench_process(xcm)?; + } assert_eq!(executor.error_handler(), &Xcm(vec![])); + Ok(()) } - set_appendix { + #[benchmark] + fn set_appendix() -> Result<(), BenchmarkError> { let mut executor = new_executor::(Default::default()); let appendix = Xcm(vec![]); let instruction = Instruction::>::SetAppendix(appendix); let xcm = Xcm(vec![instruction]); - } : { - executor.bench_process(xcm)?; - } verify { + #[block] + { + executor.bench_process(xcm)?; + } assert_eq!(executor.appendix(), &Xcm(vec![])); + Ok(()) } - clear_error { + #[benchmark] + fn clear_error() -> Result<(), BenchmarkError> { let mut executor = new_executor::(Default::default()); executor.set_error(Some((5u32, XcmError::Overflow))); let instruction = Instruction::>::ClearError; let xcm = Xcm(vec![instruction]); - } : { - executor.bench_process(xcm)?; - } verify { - assert!(executor.error().is_none()) + #[block] + { + executor.bench_process(xcm)?; + } + assert!(executor.error().is_none()); + Ok(()) } - descend_origin { + #[benchmark] + fn descend_origin() -> Result<(), BenchmarkError> { let mut executor = new_executor::(Default::default()); let who = X2(OnlyChild, OnlyChild); let instruction = Instruction::DescendOrigin(who.clone()); let xcm = Xcm(vec![instruction]); - } : { - executor.bench_process(xcm)?; - } verify { + #[block] + { + executor.bench_process(xcm)?; + } assert_eq!( executor.origin(), &Some(MultiLocation { @@ -173,19 +208,24 @@ benchmarks! { interior: who, }), ); + Ok(()) } - clear_origin { + #[benchmark] + fn clear_origin() -> Result<(), BenchmarkError> { let mut executor = new_executor::(Default::default()); let instruction = Instruction::ClearOrigin; let xcm = Xcm(vec![instruction]); - } : { - executor.bench_process(xcm)?; - } verify { + #[block] + { + executor.bench_process(xcm)?; + } assert_eq!(executor.origin(), &None); + Ok(()) } - report_error { + #[benchmark] + fn report_error() -> Result<(), BenchmarkError> { let mut executor = new_executor::(Default::default()); executor.set_error(Some((0u32, XcmError::Unimplemented))); let query_id = Default::default(); @@ -193,16 +233,21 @@ benchmarks! { let max_weight = Default::default(); let instruction = Instruction::ReportError(QueryResponseInfo { - query_id, destination, max_weight + query_id, + destination, + max_weight, }); let xcm = Xcm(vec![instruction]); - }: { - executor.bench_process(xcm)?; - } verify { + #[block] + { + executor.bench_process(xcm)?; + } // the execution succeeding is all we need to verify this xcm was successful + Ok(()) } - claim_asset { + #[benchmark] + fn claim_asset() -> Result<(), BenchmarkError> { use xcm_executor::traits::DropAssets; let (origin, ticket, assets) = T::claimable_asset()?; @@ -221,44 +266,65 @@ benchmarks! { // Assets should be in the trap now. let mut executor = new_executor::(origin); - let instruction = Instruction::ClaimAsset { assets: assets.clone(), ticket }; + let instruction = Instruction::ClaimAsset { + assets: assets.clone(), + ticket, + }; let xcm = Xcm(vec![instruction]); - } :{ - executor.bench_process(xcm)?; - } verify { + #[block] + { + executor.bench_process(xcm)?; + } assert!(executor.holding().ensure_contains(&assets).is_ok()); + Ok(()) } - trap { + #[benchmark] + fn trap() -> Result<(), BenchmarkError> { let mut executor = new_executor::(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.bench_process(xcm); - } verify { - assert!(matches!(_result, Err(ExecutorError { - xcm_error: XcmError::Trap(10), - .. - }))); - } - subscribe_version { + #[block] + { + _result = executor.bench_process(xcm); + } + assert!(matches!( + _result, + Err(ExecutorError { + xcm_error: XcmError::Trap(10), + .. + }) + )); + Ok(()) + } + + #[benchmark] + fn subscribe_version() -> Result<(), BenchmarkError> { 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::(origin.clone()); - let instruction = Instruction::SubscribeVersion { query_id, max_response_weight }; + let instruction = Instruction::SubscribeVersion { + query_id, + max_response_weight, + }; let xcm = Xcm(vec![instruction]); - } : { - executor.bench_process(xcm)?; - } verify { - assert!(::SubscriptionService::is_subscribed(&origin)); + #[block] + { + executor.bench_process(xcm)?; + } + assert!( + ::SubscriptionService::is_subscribed(&origin) + ); + Ok(()) } - unsubscribe_version { + #[benchmark] + fn unsubscribe_version() -> Result<(), BenchmarkError> { use xcm_executor::traits::VersionChangeNotifier; // First we need to subscribe to notifications. let origin = T::subscribe_origin()?; @@ -273,34 +339,48 @@ benchmarks! { message_hash: [0; 32], topic: None, }, - ).map_err(|_| "Could not start subscription")?; - assert!(::SubscriptionService::is_subscribed(&origin)); + ) + .map_err(|_| "Could not start subscription")?; + assert!( + ::SubscriptionService::is_subscribed(&origin) + ); let mut executor = new_executor::(origin.clone()); let instruction = Instruction::UnsubscribeVersion; let xcm = Xcm(vec![instruction]); - } : { - executor.bench_process(xcm)?; - } verify { - assert!(!::SubscriptionService::is_subscribed(&origin)); + #[block] + { + executor.bench_process(xcm)?; + } + assert!( + !::SubscriptionService::is_subscribed(&origin) + ); + Ok(()) } - initiate_reserve_withdraw { + #[benchmark] + fn initiate_reserve_withdraw() -> Result<(), BenchmarkError> { let holding = T::worst_case_holding(1); let assets_filter = MultiAssetFilter::Definite(holding.clone()); let reserve = T::valid_destination().map_err(|_| BenchmarkError::Skip)?; let mut executor = new_executor::(Default::default()); executor.set_holding(holding.into()); - let instruction = Instruction::InitiateReserveWithdraw { assets: assets_filter, reserve, xcm: Xcm(vec![]) }; + let instruction = Instruction::InitiateReserveWithdraw { + assets: assets_filter, + reserve, + xcm: Xcm(vec![]), + }; let xcm = Xcm(vec![instruction]); - }: { - executor.bench_process(xcm)?; - } verify { + #[block] + { + executor.bench_process(xcm)?; + } // 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 + Ok(()) } - burn_asset { + #[benchmark] + fn burn_asset() -> Result<(), BenchmarkError> { let holding = T::worst_case_holding(0); let assets = holding.clone(); @@ -309,13 +389,16 @@ benchmarks! { let instruction = Instruction::BurnAsset(assets.into()); let xcm = Xcm(vec![instruction]); - }: { - executor.bench_process(xcm)?; - } verify { + #[block] + { + executor.bench_process(xcm)?; + } assert!(executor.holding().is_empty()); + Ok(()) } - expect_asset { + #[benchmark] + fn expect_asset() -> Result<(), BenchmarkError> { let holding = T::worst_case_holding(0); let assets = holding.clone(); @@ -324,61 +407,78 @@ benchmarks! { let instruction = Instruction::ExpectAsset(assets.into()); let xcm = Xcm(vec![instruction]); - }: { - executor.bench_process(xcm)?; - } verify { + #[block] + { + executor.bench_process(xcm)?; + } // `execute` completing successfully is as good as we can check. + Ok(()) } - expect_origin { + #[benchmark] + fn expect_origin() -> Result<(), BenchmarkError> { let expected_origin = Parent.into(); let mut executor = new_executor::(Default::default()); let instruction = Instruction::ExpectOrigin(Some(expected_origin)); let xcm = Xcm(vec![instruction]); let mut _result = Ok(()); - }: { - _result = executor.bench_process(xcm); - } verify { - assert!(matches!(_result, Err(ExecutorError { - xcm_error: XcmError::ExpectationFalse, - .. - }))); - } - - expect_error { + #[block] + { + _result = executor.bench_process(xcm); + } + assert!(matches!( + _result, + Err(ExecutorError { + xcm_error: XcmError::ExpectationFalse, + .. + }) + )); + Ok(()) + } + + #[benchmark] + fn expect_error() -> Result<(), BenchmarkError> { let mut executor = new_executor::(Default::default()); executor.set_error(Some((3u32, XcmError::Overflow))); let instruction = Instruction::ExpectError(None); let xcm = Xcm(vec![instruction]); let mut _result = Ok(()); - }: { - _result = executor.bench_process(xcm); - } verify { - assert!(matches!(_result, Err(ExecutorError { - xcm_error: XcmError::ExpectationFalse, - .. - }))); - } - - expect_transact_status { + #[block] + { + _result = executor.bench_process(xcm); + } + assert!(matches!( + _result, + Err(ExecutorError { + xcm_error: XcmError::ExpectationFalse, + .. + }) + )); + Ok(()) + } + + #[benchmark] + fn expect_transact_status() -> Result<(), BenchmarkError> { let mut executor = new_executor::(Default::default()); - let worst_error = || -> MaybeErrorCode { - vec![0; MaxDispatchErrorLen::get() as usize].into() - }; + let worst_error = + || -> MaybeErrorCode { vec![0; MaxDispatchErrorLen::get() as usize].into() }; executor.set_transact_status(worst_error()); let instruction = Instruction::ExpectTransactStatus(worst_error()); let xcm = Xcm(vec![instruction]); let mut _result = Ok(()); - }: { - _result = executor.bench_process(xcm); - } verify { + #[block] + { + _result = executor.bench_process(xcm); + } assert!(matches!(_result, Ok(..))); + Ok(()) } - query_pallet { + #[benchmark] + fn query_pallet() -> Result<(), BenchmarkError> { let query_id = Default::default(); let destination = T::valid_destination().map_err(|_| BenchmarkError::Skip)?; let max_weight = Default::default(); @@ -386,16 +486,22 @@ benchmarks! { let instruction = Instruction::QueryPallet { module_name: b"frame_system".to_vec(), - response_info: QueryResponseInfo { destination, query_id, max_weight }, + response_info: QueryResponseInfo { + destination, + query_id, + max_weight, + }, }; let xcm = Xcm(vec![instruction]); - }: { - executor.bench_process(xcm)?; - } verify { - // TODO: Potentially add new trait to XcmSender to detect a queued outgoing message. #4426 + #[block] + { + executor.bench_process(xcm)?; + } + Ok(()) } - expect_pallet { + #[benchmark] + fn expect_pallet() -> Result<(), BenchmarkError> { let mut executor = new_executor::(Default::default()); let instruction = Instruction::ExpectPallet { @@ -406,13 +512,16 @@ benchmarks! { min_crate_minor: 0, }; let xcm = Xcm(vec![instruction]); - }: { - executor.bench_process(xcm)?; - } verify { + #[block] + { + executor.bench_process(xcm)?; + } // the execution succeeding is all we need to verify this xcm was successful + Ok(()) } - report_transact_status { + #[benchmark] + fn report_transact_status() -> Result<(), BenchmarkError> { let query_id = Default::default(); let destination = T::valid_destination().map_err(|_| BenchmarkError::Skip)?; let max_weight = Default::default(); @@ -426,60 +535,76 @@ benchmarks! { max_weight, }); let xcm = Xcm(vec![instruction]); - }: { - executor.bench_process(xcm)?; - } verify { - // TODO: Potentially add new trait to XcmSender to detect a queued outgoing message. #4426 + #[block] + { + executor.bench_process(xcm)?; + } + Ok(()) } - clear_transact_status { + #[benchmark] + fn clear_transact_status() -> Result<(), BenchmarkError> { let mut executor = new_executor::(Default::default()); executor.set_transact_status(b"MyError".to_vec().into()); let instruction = Instruction::ClearTransactStatus; let xcm = Xcm(vec![instruction]); - }: { - executor.bench_process(xcm)?; - } verify { + #[block] + { + executor.bench_process(xcm)?; + } assert_eq!(executor.transact_status(), &MaybeErrorCode::Success); + Ok(()) } - set_topic { + #[benchmark] + fn set_topic() -> Result<(), BenchmarkError> { let mut executor = new_executor::(Default::default()); let instruction = Instruction::SetTopic([1; 32]); let xcm = Xcm(vec![instruction]); - }: { - executor.bench_process(xcm)?; - } verify { + #[block] + { + executor.bench_process(xcm)?; + } assert_eq!(executor.topic(), &Some([1; 32])); + Ok(()) } - clear_topic { + #[benchmark] + fn clear_topic() -> Result<(), BenchmarkError> { let mut executor = new_executor::(Default::default()); executor.set_topic(Some([2; 32])); let instruction = Instruction::ClearTopic; let xcm = Xcm(vec![instruction]); - }: { - executor.bench_process(xcm)?; - } verify { + #[block] + { + executor.bench_process(xcm)?; + } assert_eq!(executor.topic(), &None); + Ok(()) } - set_fees_mode { + #[benchmark] + fn set_fees_mode() -> Result<(), BenchmarkError> { let mut executor = new_executor::(Default::default()); - executor.set_fees_mode(FeesMode { jit_withdraw: false }); + executor.set_fees_mode(FeesMode { + jit_withdraw: false, + }); let instruction = Instruction::SetFeesMode { jit_withdraw: true }; let xcm = Xcm(vec![instruction]); - }: { - executor.bench_process(xcm)?; - } verify { + #[block] + { + executor.bench_process(xcm)?; + } assert_eq!(executor.fees_mode(), &FeesMode { jit_withdraw: true }); + Ok(()) } - unpaid_execution { + #[benchmark] + fn unpaid_execution() -> Result<(), BenchmarkError> { let mut executor = new_executor::(Default::default()); executor.set_origin(Some(Here.into())); @@ -489,43 +614,74 @@ benchmarks! { }; let xcm = Xcm(vec![instruction]); - }: { - executor.bench_process(xcm)?; - } - - exchange_asset { - } : { - Err(BenchmarkError::Override(BenchmarkResult::from_weight(Weight::MAX)))?; - } - - export_message { - } : { - Err(BenchmarkError::Override(BenchmarkResult::from_weight(Weight::MAX)))?; - } - - lock_asset { - } : { - Err(BenchmarkError::Override(BenchmarkResult::from_weight(Weight::MAX)))?; - } - - unlock_asset { - } : { - Err(BenchmarkError::Override(BenchmarkResult::from_weight(Weight::MAX)))?; - } - - note_unlockable { - } : { - Err(BenchmarkError::Override(BenchmarkResult::from_weight(Weight::MAX)))?; - } - - request_unlock { - } : { - Err(BenchmarkError::Override(BenchmarkResult::from_weight(Weight::MAX)))?; - } - - universal_origin { - } : { - Err(BenchmarkError::Override(BenchmarkResult::from_weight(Weight::MAX)))?; + #[block] + { + executor.bench_process(xcm)?; + } + Ok(()) + } + + #[benchmark] + fn exchange_asset() -> Result<(), BenchmarkError> { + #[block] + {} + Err(BenchmarkError::Override(BenchmarkResult::from_weight( + Weight::MAX, + ))) + } + + #[benchmark] + fn export_message() -> Result<(), BenchmarkError> { + #[block] + {} + Err(BenchmarkError::Override(BenchmarkResult::from_weight( + Weight::MAX, + ))) + } + + #[benchmark] + fn lock_asset() -> Result<(), BenchmarkError> { + #[block] + {} + Err(BenchmarkError::Override(BenchmarkResult::from_weight( + Weight::MAX, + ))) + } + + #[benchmark] + fn unlock_asset() -> Result<(), BenchmarkError> { + #[block] + {} + Err(BenchmarkError::Override(BenchmarkResult::from_weight( + Weight::MAX, + ))) + } + + #[benchmark] + fn note_unlockable() -> Result<(), BenchmarkError> { + #[block] + {} + Err(BenchmarkError::Override(BenchmarkResult::from_weight( + Weight::MAX, + ))) + } + + #[benchmark] + fn request_unlock() -> Result<(), BenchmarkError> { + #[block] + {} + Err(BenchmarkError::Override(BenchmarkResult::from_weight( + Weight::MAX, + ))) + } + + #[benchmark] + fn universal_origin() -> Result<(), BenchmarkError> { + #[block] + {} + Err(BenchmarkError::Override(BenchmarkResult::from_weight( + Weight::MAX, + ))) } impl_benchmark_test_suite!( diff --git a/pallets/astar-xcm-benchmarks/src/generic/mock.rs b/pallets/astar-xcm-benchmarks/src/generic/mock.rs index aa73c969cc..76a9493373 100644 --- a/pallets/astar-xcm-benchmarks/src/generic/mock.rs +++ b/pallets/astar-xcm-benchmarks/src/generic/mock.rs @@ -40,17 +40,17 @@ use xcm_builder::{ }; use xcm_executor::traits::ConvertOrigin; -type Block = frame_system::mocking::MockBlock; -type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; +type Block = frame_system::mocking::MockBlock; +type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; frame_support::construct_runtime!( - pub struct TestRuntime + pub struct Test where Block = Block, NodeBlock = Block, UncheckedExtrinsic = UncheckedExtrinsic, { - System: frame_system, + System: frame_system = 10, XcmGenericBenchmarks: generic, } ); @@ -61,7 +61,7 @@ parameter_types! { frame_system::limits::BlockWeights::simple_max(Weight::from_parts(1024, u64::MAX)); } -impl frame_system::Config for TestRuntime { +impl frame_system::Config for Test { type BaseCallFilter = Everything; type BlockWeights = (); type BlockLength = (); @@ -143,7 +143,7 @@ impl xcm_executor::Config for XcmConfig { type SafeCallFilter = Everything; } -impl crate::Config for TestRuntime { +impl crate::Config for Test { type XcmConfig = XcmConfig; type AccountIdConverter = AccountIdConverter; fn valid_destination() -> Result { @@ -163,7 +163,7 @@ impl crate::Config for TestRuntime { } } -impl generic::Config for TestRuntime { +impl generic::Config for Test { type RuntimeCall = RuntimeCall; fn worst_case_response() -> (u64, Response) { @@ -215,7 +215,7 @@ impl generic::Config for TestRuntime { #[cfg(feature = "runtime-benchmarks")] pub fn new_test_ext() -> sp_io::TestExternalities { use sp_runtime::BuildStorage; - let t = RuntimeGenesisConfig { + let t = GenesisConfig { ..Default::default() } .build_storage()