Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add xcm benchmarks #1129

Merged
merged 16 commits into from
Jan 15, 2024
Merged
Changes from 1 commit
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
Next Next commit
feat: add generic xcm benchmarks wrapper
ashutoshvarma committed Jan 9, 2024
commit 4f4596404c2cdd144cbd629585be35eb1bfc12fc
26 changes: 26 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -255,6 +255,7 @@ polkadot-runtime-parachains = { git = "https://github.com/paritytech/polkadot",
cumulus-pallet-xcm = { git = "https://github.com/paritytech/cumulus", branch = "polkadot-v0.9.43", default-features = false }
xcm = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.43", default-features = false }
pallet-xcm = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.43", default-features = false }
pallet-xcm-benchmarks = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.43", default-features = false }
xcm-builder = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.43", default-features = false }
xcm-executor = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.43", default-features = false }
xcm-simulator = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.43", default-features = false }
@@ -282,6 +283,7 @@ pallet-ethereum-checked = { path = "./pallets/ethereum-checked", default-feature
pallet-inflation = { path = "./pallets/inflation", default-features = false }
pallet-dynamic-evm-base-fee = { path = "./pallets/dynamic-evm-base-fee", default-features = false }
pallet-unified-accounts = { path = "./pallets/unified-accounts", default-features = false }
astar-xcm-benchmarks = { path = "./pallets/astar-xcm-benchmarks", default-features = false }

dapp-staking-v3-runtime-api = { path = "./pallets/dapp-staking-v3/rpc/runtime-api", default-features = false }

193 changes: 193 additions & 0 deletions pallets/astar-xcm-benchmarks/src/generic/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
// This file is part of Astar.

// Copyright (C) 2019-2023 Stake Technologies Pte.Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later

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

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

use super::*;
use frame_benchmarking::v2::*;
// use frame_benchmarking::{benchmarks, BenchmarkError, BenchmarkResult};
use frame_support::dispatch::Weight;
use pallet_xcm_benchmarks::{new_executor, XcmCallOf};
use sp_std::vec;
use sp_std::vec::Vec;
use xcm::latest::prelude::*;

#[benchmarks]
mod benchmarks {
use super::*;

/// We need re-write buy_execution benchmark becuase our runtime
/// needs 1 additional DB read (XcAssetConfig) for fetching unit per sec
/// for a fungible asset. The upstream benchmark use native assets thus
/// won't accout for it.
Comment on lines +31 to +34
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One general question about this approach - how viable is to reuse code from the official repo if we have to account for different actions for some calls?

This can change in the future, upstream, and it means we should be re-checking all of the benchmarks each time we do an uplift?

It would seem easier to just have all the benchmarks in our repo since that way we have full control over it. If I understood the current approach correctly, it's highly likely we miss something in the future.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The upstream benchmarks are quite generic themselves, but not generic enough maybe because not many parachains are using them.
The benchmarks we had to re-write were only due the use of hardcoded native asset inside some benchmarks.

  • buy_execution: hardcoded native asset used for buying execution
  • transfer_*: does not take ED into account, they use native asset (pallet_balances) which defaults to allow death unlike fungibles (pallet_assets).
  • expect_pallet: hardcoded pallet index

Other than that all benchmarks were configurable through the pallet's Config. As long as we we used same release upstream pallet (XcmConfig is not changed) it should be fine.

For the hardcoded cases, we can make a upstream PR to make it more generic for other parachains to use as well.

I'm fine either ways, if we copy-paste all benchmarks then we need to keep them updated during uplift if there are major changes like precompile-utils and if we don't copy-paste then we still need to check them during uplifts

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the explanation!

Let's keep it like this, as you suggested.
For the upstream PR, let's start with an issue first perhaps?

#[benchmark]
fn buy_execution() -> Result<(), BenchmarkError> {
let holding = T::worst_case_holding(0).into();

let mut executor = new_executor::<T>(Default::default());
executor.set_holding(holding);

// A fungible asset
let fee_asset = Concrete(MultiLocation::parent());

let instruction = Instruction::<XcmCallOf<T>>::BuyExecution {
fees: (fee_asset, 100_000_000u128).into(), // should be something inside of holding
weight_limit: WeightLimit::Unlimited,
};

let xcm = Xcm(vec![instruction]);

#[block]
{
executor.bench_process(xcm)?;
}
// The completion of execution above is enough to validate this is completed.
Ok(())
}

/// Re-write as upstream one has hardcoded system pallet index as 1 whereas our runtimes
/// uses index 10.
#[benchmark]
fn expect_pallet() -> Result<(), BenchmarkError> {
let mut executor = new_executor::<T>(Default::default());

let instruction = Instruction::ExpectPallet {
// used index 10 for our runtimes
index: 10,
name: b"System".to_vec(),
module_name: b"frame_system".to_vec(),
crate_major: 4,
min_crate_minor: 0,
};
let xcm = Xcm(vec![instruction]);

#[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!(
Pallet,
crate::generic::mock::new_test_ext(),
crate::generic::mock::Test
);
}

pub struct XcmGenericBenchmarks<T>(sp_std::marker::PhantomData<T>);
// Benchmarks wrapper
impl<T: Config> frame_benchmarking::Benchmarking for XcmGenericBenchmarks<T> {
fn benchmarks(extra: bool) -> Vec<frame_benchmarking::BenchmarkMetadata> {
// all the generic xcm benchmarks
use pallet_xcm_benchmarks::generic::Pallet as PalletXcmGenericBench;
PalletXcmGenericBench::<T>::benchmarks(extra)
}
fn run_benchmark(
extrinsic: &[u8],
c: &[(frame_benchmarking::BenchmarkParameter, u32)],
whitelist: &[frame_benchmarking::TrackedStorageKey],
verify: bool,
internal_repeats: u32,
) -> Result<Vec<frame_benchmarking::BenchmarkResult>, frame_benchmarking::BenchmarkError> {
use pallet_xcm_benchmarks::generic::Pallet as PalletXcmGenericBench;

use crate::generic::Pallet as AstarXcmGenericBench;
if AstarXcmGenericBench::<T>::benchmarks(true)
.iter()
.any(|x| x.name == extrinsic)
{
AstarXcmGenericBench::<T>::run_benchmark(
extrinsic,
c,
whitelist,
verify,
internal_repeats,
)
} else {
PalletXcmGenericBench::<T>::run_benchmark(
extrinsic,
c,
whitelist,
verify,
internal_repeats,
)
}
}
}
230 changes: 230 additions & 0 deletions pallets/astar-xcm-benchmarks/src/generic/mock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
// This file is part of Astar.

// Copyright (C) 2019-2023 Stake Technologies Pte.Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later

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

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

//! A mock runtime for XCM benchmarking.
use crate::{generic, mock::*, *};
use frame_benchmarking::BenchmarkError;
use frame_support::{
parameter_types,
traits::{Everything, OriginTrait},
};
use parity_scale_codec::Decode;
use sp_core::H256;
use sp_runtime::{
testing::Header,
traits::{BlakeTwo256, IdentityLookup, TrailingZeroInput},
BuildStorage,
};
use xcm::latest::prelude::*;
use xcm_builder::{
test_utils::{
Assets, TestAssetExchanger, TestAssetLocker, TestAssetTrap, TestSubscriptionService,
TestUniversalAliases,
},
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>} = 10,
PolkadotXcmBenchmarks: pallet_xcm_benchmarks::generic::{Pallet},
XcmGenericBenchmarks: generic::{Pallet},
}
);

parameter_types! {
pub const BlockHashCount: u64 = 250;
pub UniversalLocation: InteriorMultiLocation = Here;
}

impl frame_system::Config for Test {
type BaseCallFilter = Everything;
type BlockWeights = ();
type BlockLength = ();
type DbWeight = ();
type RuntimeOrigin = RuntimeOrigin;
type Index = u64;
type BlockNumber = u64;
type Hash = H256;
type RuntimeCall = RuntimeCall;
type Hashing = BlakeTwo256;
type AccountId = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type RuntimeEvent = RuntimeEvent;
type BlockHashCount = BlockHashCount;
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<u64>;
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type SS58Prefix = ();
type OnSetCode = ();
type MaxConsumers = frame_support::traits::ConstU32<16>;
}

/// 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, _: &XcmContext) -> Result<(), XcmError> {
unreachable!();
}

fn withdraw_asset(
_: &MultiAsset,
_: &MultiLocation,
_: Option<&XcmContext>,
) -> Result<Assets, XcmError> {
unreachable!();
}
}

parameter_types! {
pub const MaxInstructions: u32 = 100;
pub const MaxAssetsIntoHolding: u32 = 64;
}

pub struct XcmConfig;
impl xcm_executor::Config for XcmConfig {
type RuntimeCall = RuntimeCall;
type XcmSender = DevNull;
type AssetTransactor = NoAssetTransactor;
type OriginConverter = AlwaysSignedByDefault<RuntimeOrigin>;
type IsReserve = AllAssetLocationsPass;
type IsTeleporter = ();
type UniversalLocation = UniversalLocation;
type Barrier = AllowUnpaidExecutionFrom<Everything>;
type Weigher = xcm_builder::FixedWeightBounds<UnitWeightCost, RuntimeCall, MaxInstructions>;
type Trader = xcm_builder::FixedRateOfFungible<WeightPrice, ()>;
type ResponseHandler = DevNull;
type AssetTrap = TestAssetTrap;
type AssetLocker = TestAssetLocker;
type AssetExchanger = TestAssetExchanger;
type AssetClaims = TestAssetTrap;
type SubscriptionService = TestSubscriptionService;
type PalletInstancesInfo = AllPalletsWithSystem;
type MaxAssetsIntoHolding = MaxAssetsIntoHolding;
type FeeManager = ();
// No bridges yet...
type MessageExporter = ();
type UniversalAliases = TestUniversalAliases;
type CallDispatcher = RuntimeCall;
type SafeCallFilter = Everything;
}

impl pallet_xcm_benchmarks::Config for Test {
type XcmConfig = XcmConfig;
type AccountIdConverter = AccountIdConverter;
fn valid_destination() -> Result<MultiLocation, BenchmarkError> {
let valid_destination: MultiLocation = Junction::AccountId32 {
network: None,
id: [0u8; 32],
}
.into();

Ok(valid_destination)
}
fn worst_case_holding(_depositable_count: u32) -> MultiAssets {
crate::mock::mock_worst_case_holding()
}
}

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

fn worst_case_response() -> (u64, Response) {
let assets: MultiAssets = (Concrete(Here.into()), 100).into();
(0, Response::Assets(assets))
}

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

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

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

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

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))
}

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

impl generic::Config for Test {}
impl Config for Test {}

pub fn new_test_ext() -> sp_io::TestExternalities {
let t = GenesisConfig {
..Default::default()
}
.build_storage()
.unwrap();
t.into()
}

pub struct AlwaysSignedByDefault<Origin>(core::marker::PhantomData<Origin>);
impl<Origin> ConvertOrigin<Origin> for AlwaysSignedByDefault<Origin>
where
Origin: OriginTrait,
<Origin as OriginTrait>::AccountId: Decode,
{
fn convert_origin(
_origin: impl Into<MultiLocation>,
_kind: OriginKind,
) -> Result<Origin, MultiLocation> {
Ok(Origin::signed(
<Origin as OriginTrait>::AccountId::decode(&mut TrailingZeroInput::zeroes())
.expect("infinite length input; no invalid inputs for type; qed"),
))
}
}
36 changes: 36 additions & 0 deletions pallets/astar-xcm-benchmarks/src/generic/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// This file is part of Astar.

// Copyright (C) 2019-2023 Stake Technologies Pte.Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later

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

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

pub use pallet::*;

pub mod benchmarking;

#[cfg(test)]
pub mod mock;

#[frame_support::pallet]
pub mod pallet {
#[pallet::config]
pub trait Config<I: 'static = ()>:
frame_system::Config + crate::Config + pallet_xcm_benchmarks::generic::Config
{
}

#[pallet::pallet]
pub struct Pallet<T, I = ()>(_);
}
32 changes: 32 additions & 0 deletions pallets/astar-xcm-benchmarks/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// This file is part of Astar.

// Copyright (C) 2019-2023 Stake Technologies Pte.Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later

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

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

#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(feature = "runtime-benchmarks")]
pub mod generic;

#[cfg(feature = "runtime-benchmarks")]
pub mod fungible;

#[cfg(test)]
mod mock;

#[cfg(feature = "runtime-benchmarks")]
/// A base trait for all individual pallets
pub trait Config: frame_system::Config + pallet_xcm_benchmarks::Config {}
92 changes: 92 additions & 0 deletions pallets/astar-xcm-benchmarks/src/mock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// This file is part of Astar.

// Copyright (C) 2019-2023 Stake Technologies Pte.Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later

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

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

use frame_support::{dispatch::Weight, parameter_types, traits::ContainsPair};
use xcm::latest::prelude::*;

// An xcm sender/receiver akin to > /dev/null
pub struct DevNull;
impl SendXcm for DevNull {
type Ticket = ();

fn validate(
_destination: &mut Option<MultiLocation>,
_message: &mut Option<opaque::Xcm>,
) -> SendResult<Self::Ticket> {
Ok(((), MultiAssets::new()))
}

fn deliver(_: Self::Ticket) -> Result<XcmHash, SendError> {
Ok(XcmHash::default())
}
}

impl xcm_executor::traits::OnResponse for DevNull {
fn expecting_response(_: &MultiLocation, _: u64, _: Option<&MultiLocation>) -> bool {
false
}
fn on_response(
_: &MultiLocation,
_: u64,
_: Option<&MultiLocation>,
_: Response,
_: Weight,
_: &XcmContext,
) -> Weight {
Weight::zero()
}
}

pub struct AccountIdConverter;
impl xcm_executor::traits::Convert<MultiLocation, u64> for AccountIdConverter {
fn convert(ml: MultiLocation) -> Result<u64, MultiLocation> {
match ml {
MultiLocation {
parents: 0,
interior: X1(Junction::AccountId32 { id, .. }),
} => Ok(<u64 as parity_scale_codec::Decode>::decode(&mut &*id.to_vec()).unwrap()),
_ => Err(ml),
}
}

fn reverse(acc: u64) -> Result<MultiLocation, u64> {
Err(acc)
}
}

parameter_types! {
pub Ancestry: MultiLocation = Junction::Parachain(101).into();
pub UnitWeightCost: u64 = 10;
pub WeightPrice: (AssetId, u128, u128) = (Concrete(MultiLocation::parent()), 1_000_000, 1024);
}

pub struct AllAssetLocationsPass;
impl ContainsPair<MultiAsset, MultiLocation> for AllAssetLocationsPass {
fn contains(_: &MultiAsset, _: &MultiLocation) -> bool {
true
}
}

#[cfg(feature = "runtime-benchmarks")]
pub fn mock_worst_case_holding() -> MultiAssets {
let assets: Vec<MultiAsset> = vec![MultiAsset {
id: Concrete(MultiLocation::parent()),
fun: Fungible(u128::MAX),
}];
assets.into()
}