From 83b7f97d02fabc1b5f3a2f74a4f88d77392783ce Mon Sep 17 00:00:00 2001 From: kalaninja Date: Mon, 8 May 2023 14:26:41 +0700 Subject: [PATCH 1/3] [#113] Benchmark `drop_assets()` of `TrappistDropAssets` --- Cargo.lock | 17 ++++ README.md | 4 +- node/Cargo.toml | 3 +- node/cli/src/cli.rs | 1 - node/cli/src/lib.rs | 3 +- node/service/src/chain_spec/stout.rs | 3 +- node/service/src/chain_spec/trappist.rs | 1 - node/service/src/lib.rs | 3 + pallets/asset-registry/src/lib.rs | 2 +- pallets/benchmarks/Cargo.toml | 35 +++++++ pallets/benchmarks/src/benchmarking.rs | 33 +++++++ pallets/benchmarks/src/lib.rs | 43 +++++++++ pallets/benchmarks/src/weights.rs | 7 ++ primitives/xcm/src/lib.rs | 96 +++++++++++-------- runtime/trappist/Cargo.toml | 78 +++++++-------- runtime/trappist/src/impls.rs | 7 +- runtime/trappist/src/lib.rs | 81 +++++++++------- runtime/trappist/src/weights/mod.rs | 21 ++++ .../trappist/src/weights/pallet_benchmarks.rs | 53 ++++++++++ runtime/trappist/src/xcm_config.rs | 15 ++- 20 files changed, 378 insertions(+), 128 deletions(-) create mode 100644 pallets/benchmarks/Cargo.toml create mode 100644 pallets/benchmarks/src/benchmarking.rs create mode 100644 pallets/benchmarks/src/lib.rs create mode 100644 pallets/benchmarks/src/weights.rs create mode 100644 runtime/trappist/src/weights/mod.rs create mode 100644 runtime/trappist/src/weights/pallet_benchmarks.rs diff --git a/Cargo.lock b/Cargo.lock index ec8b159f..a877bacb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5626,6 +5626,22 @@ dependencies = [ "sp-std", ] +[[package]] +name = "pallet-benchmarks" +version = "0.1.0" +dependencies = [ + "frame-benchmarking", + "frame-support", + "frame-system", + "parity-scale-codec", + "scale-info", + "sp-core", + "sp-runtime", + "sp-std", + "xcm", + "xcm-executor", +] + [[package]] name = "pallet-bounties" version = "4.0.0-dev" @@ -12206,6 +12222,7 @@ dependencies = [ "pallet-aura", "pallet-authorship", "pallet-balances", + "pallet-benchmarks", "pallet-chess", "pallet-collator-selection", "pallet-collective", diff --git a/README.md b/README.md index 6490d89e..8d4fb80a 100644 --- a/README.md +++ b/README.md @@ -62,7 +62,7 @@ cargo b -r --no-default-features --features with-stout-runtime --target-dir targ ``` Alternatively, run -`bash build_runtimes.sh` . +`bash ./scripts/build_runtimes.sh` . ### XCM Playground via Zombienet @@ -70,8 +70,6 @@ Alternatively, run Create a `bin` directory into the root of this repository and place the following binaries inside of it: - `polkadot` (which you can download from [the releases](https://github.com/paritytech/polkadot/releases)) - `polkadot-parachain` (which you will build from [cumulus](https://github.com/paritytech/cumulus)) -- `trappist-collator` (which you will build from this repository) -- `stout-collator` (which you will build from this repository) Download the [latest release of zombienet](https://github.com/paritytech/zombienet/releases/) into the root of this repository and make it executable: ``` diff --git a/node/Cargo.toml b/node/Cargo.toml index b605ba7a..d8b8ba5f 100644 --- a/node/Cargo.toml +++ b/node/Cargo.toml @@ -7,6 +7,7 @@ license = "Unlicense" homepage = "https://trappist.io" repository = "https://github.com/TrappistNetwork/trappist" edition = "2021" +default-run = "trappist-collator" [package.metadata.docs.rs] targets = ["x86_64-unknown-linux-gnu"] @@ -41,7 +42,7 @@ tempfile = "3.2.0" [features] default = ["with-trappist-runtime"] runtime-benchmarks = [ - "trappist-cli/runtime-benchmarks" + "trappist-cli/runtime-benchmarks" ] with-trappist-runtime = [ "trappist-service/with-trappist-runtime", diff --git a/node/cli/src/cli.rs b/node/cli/src/cli.rs index 6a3e7aa1..2a7615e4 100644 --- a/node/cli/src/cli.rs +++ b/node/cli/src/cli.rs @@ -14,7 +14,6 @@ // You should have received a copy of the GNU General Public License // along with Cumulus. If not, see . -use service::chain_spec; #[cfg(feature = "with-stout-runtime")] use service::chain_spec::stout::Extensions; #[cfg(feature = "with-trappist-runtime")] diff --git a/node/cli/src/lib.rs b/node/cli/src/lib.rs index 59ec9bd6..89e28302 100644 --- a/node/cli/src/lib.rs +++ b/node/cli/src/lib.rs @@ -1,4 +1,5 @@ -#![warn(missing_docs)] +// TODO: Write missing docs. +//#![warn(missing_docs)] #![warn(unused_extern_crates)] #[cfg(feature = "cli")] diff --git a/node/service/src/chain_spec/stout.rs b/node/service/src/chain_spec/stout.rs index dcd27578..dbc7e7e5 100644 --- a/node/service/src/chain_spec/stout.rs +++ b/node/service/src/chain_spec/stout.rs @@ -1,9 +1,8 @@ use cumulus_primitives_core::ParaId; -use hex_literal::hex; use sc_chain_spec::{ChainSpecExtension, ChainSpecGroup}; use sc_service::ChainType; use serde::{Deserialize, Serialize}; -use sp_core::{crypto::UncheckedInto, sr25519, Pair, Public}; +use sp_core::{sr25519, Pair, Public}; use sp_runtime::traits::{IdentifyAccount, Verify}; use stout_runtime::{ constants::currency::EXISTENTIAL_DEPOSIT, AccountId, AssetsConfig, AuraId, BalancesConfig, diff --git a/node/service/src/chain_spec/trappist.rs b/node/service/src/chain_spec/trappist.rs index 2824d977..b2053db4 100644 --- a/node/service/src/chain_spec/trappist.rs +++ b/node/service/src/chain_spec/trappist.rs @@ -1,7 +1,6 @@ use cumulus_primitives_core::ParaId; use hex_literal::hex; use sc_chain_spec::{ChainSpecExtension, ChainSpecGroup}; -use sc_cli::DefaultConfigurationValues; use sc_service::ChainType; use serde::{Deserialize, Serialize}; use sp_core::{crypto::UncheckedInto, sr25519, Pair, Public}; diff --git a/node/service/src/lib.rs b/node/service/src/lib.rs index 01c12f36..56487948 100644 --- a/node/service/src/lib.rs +++ b/node/service/src/lib.rs @@ -30,6 +30,9 @@ use substrate_prometheus_endpoint::Registry; pub mod chain_spec; +#[cfg(all(feature = "with-trappist-runtime", feature = "with-stout-runtime"))] +compile_error!("features `with-trappist-runtime` and `with-stout-runtime` are mutually exclusive"); + #[cfg(feature = "with-stout-runtime")] pub mod stout_executor { pub use stout_runtime; diff --git a/pallets/asset-registry/src/lib.rs b/pallets/asset-registry/src/lib.rs index d5b89ded..7cc3ba78 100644 --- a/pallets/asset-registry/src/lib.rs +++ b/pallets/asset-registry/src/lib.rs @@ -137,7 +137,7 @@ pub mod pallet { AssetIdMultiLocation::::get(asset_id) } - fn get_asset_id(asset_type: MultiLocation) -> Option> { + fn get_asset_id(asset_type: &MultiLocation) -> Option> { AssetMultiLocationId::::get(asset_type) } } diff --git a/pallets/benchmarks/Cargo.toml b/pallets/benchmarks/Cargo.toml new file mode 100644 index 00000000..ba6faea7 --- /dev/null +++ b/pallets/benchmarks/Cargo.toml @@ -0,0 +1,35 @@ +[package] +name = "pallet-benchmarks" +version = "0.1.0" +edition = "2021" + +[package.metadata.docs.rs] +targets = ["x86_64-unknown-linux-gnu"] + +[dependencies] +codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive", ] } +scale-info = { version = "2.3.1", default-features = false, features = ["derive"] } +sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.37" } +sp-std = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.37" } +frame-benchmarking = { default-features = false, optional = true, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.37" } +frame-support = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.37" } +frame-system = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.37" } + +xcm = { default-features = false, git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.37" } +xcm-executor = { default-features = false, git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.37" } + +[dev-dependencies] +sp-core = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.37" } + +[features] +default = ["std"] +std = [ + "codec/std", + "frame-benchmarking/std", + "frame-support/std", + "frame-system/std", + "sp-runtime/std", + "sp-std/std", + "xcm-executor/std" +] +runtime-benchmarks = ["frame-benchmarking/runtime-benchmarks"] \ No newline at end of file diff --git a/pallets/benchmarks/src/benchmarking.rs b/pallets/benchmarks/src/benchmarking.rs new file mode 100644 index 00000000..5da6afb4 --- /dev/null +++ b/pallets/benchmarks/src/benchmarking.rs @@ -0,0 +1,33 @@ +use frame_benchmarking::benchmarks; +use sp_runtime::SaturatedConversion; +use xcm::prelude::AssetId as XcmAssetId; + +use crate::*; + +benchmarks! { + drop_assets_fungible { + let origin = MultiLocation::default(); + let asset_id = 1; + let location = Parachain(asset_id).into(); + T::register_asset(asset_id.into(), location.clone()); + let asset = MultiAsset { id: XcmAssetId::Concrete(location), fun: Fungibility::Fungible(100) }; + } : { + T::DropAssets::drop_assets(&origin, asset.into()); + } + + drop_assets_native { + let origin = MultiLocation::default(); + let location = MultiLocation { parents: 0, interior: Here }; + let amount = T::ExistentialDeposit::get().saturated_into(); + let asset = MultiAsset { id: XcmAssetId::Concrete(location), fun: Fungibility::Fungible(amount) }; + } : { + T::DropAssets::drop_assets(&origin, asset.into()); + } + + drop_assets_default { + let origin = MultiLocation::default(); + let asset = MultiAsset { id: XcmAssetId::Abstract(Default::default()), fun: Fungibility::Fungible(0) }; + } : { + T::DropAssets::drop_assets(&origin, asset.into()); + } +} diff --git a/pallets/benchmarks/src/lib.rs b/pallets/benchmarks/src/lib.rs new file mode 100644 index 00000000..34427034 --- /dev/null +++ b/pallets/benchmarks/src/lib.rs @@ -0,0 +1,43 @@ +//! Pallet for benchmarking. + +#![cfg_attr(not(feature = "std"), no_std)] + +use codec::Codec; +use frame_support::{pallet_prelude::*, traits::tokens::AssetId}; +use sp_runtime::traits::AtLeast32BitUnsigned; +use xcm::prelude::*; +use xcm_executor::traits::DropAssets; + +pub use pallet::*; +pub use weights::*; + +#[cfg(feature = "runtime-benchmarks")] +pub mod benchmarking; +pub mod weights; + +#[frame_support::pallet] +pub mod pallet { + use super::*; + + #[pallet::config] + pub trait Config: frame_system::Config { + /// Identifier for the class of asset. + type AssetId: AssetId + From; + + /// The balance of an account. + type Balance: Parameter + Member + AtLeast32BitUnsigned + Codec + TypeInfo; + + /// The minimum amount required to keep an account open. + #[pallet::constant] + type ExistentialDeposit: Get; + + /// Handler for when some non-empty `Assets` value should be dropped. + type DropAssets: DropAssets; + + /// Handler to register an asset. + fn register_asset(asset_id: Self::AssetId, location: MultiLocation); + } + + #[pallet::pallet] + pub struct Pallet(_); +} diff --git a/pallets/benchmarks/src/weights.rs b/pallets/benchmarks/src/weights.rs new file mode 100644 index 00000000..411eef76 --- /dev/null +++ b/pallets/benchmarks/src/weights.rs @@ -0,0 +1,7 @@ +use frame_support::weights::Weight; + +pub trait WeightInfo { + fn drop_assets_fungible() -> Weight; + fn drop_assets_native() -> Weight; + fn drop_assets_default() -> Weight; +} diff --git a/primitives/xcm/src/lib.rs b/primitives/xcm/src/lib.rs index fc625775..9acc4c67 100644 --- a/primitives/xcm/src/lib.rs +++ b/primitives/xcm/src/lib.rs @@ -4,7 +4,7 @@ use frame_support::{ sp_runtime::SaturatedConversion, traits::{fungibles::Inspect, Currency}, }; -use sp_std::{borrow::Borrow, marker::PhantomData, vec::Vec}; +use sp_std::{borrow::Borrow, marker::PhantomData}; use xcm::latest::{ AssetId::Concrete, Fungibility::Fungible, Junctions::Here, MultiAsset, MultiLocation, }; @@ -23,7 +23,7 @@ where AssetIdInfoGetter: AssetMultiLocationGetter, { fn convert_ref(asset_multi_location: impl Borrow) -> Result { - AssetIdInfoGetter::get_asset_id(asset_multi_location.borrow().clone()).ok_or(()) + AssetIdInfoGetter::get_asset_id(asset_multi_location.borrow()).ok_or(()) } fn reverse_ref(asset_id: impl Borrow) -> Result { @@ -33,7 +33,7 @@ where pub trait AssetMultiLocationGetter { fn get_asset_multi_location(asset_id: AssetId) -> Option; - fn get_asset_id(asset_multi_location: MultiLocation) -> Option; + fn get_asset_id(asset_multi_location: &MultiLocation) -> Option; } pub struct ConvertedRegisteredAssetId( @@ -59,15 +59,34 @@ impl< } } +pub trait DropAssetsWeigher { + fn fungible() -> u64; + fn native() -> u64; + fn default() -> u64; +} + pub struct TrappistDropAssets< AssetId, AssetIdInfoGetter, AssetsPallet, BalancesPallet, XcmPallet, - AccoundId, ->(PhantomData<(AssetId, AssetIdInfoGetter, AssetsPallet, BalancesPallet, XcmPallet, AccoundId)>); -impl DropAssets + AccountId, + Weigher, +>( + PhantomData<( + AssetId, + AssetIdInfoGetter, + AssetsPallet, + BalancesPallet, + XcmPallet, + AccountId, + Weigher, + )>, +); + +impl + DropAssets for TrappistDropAssets< AssetId, AssetIdInfoGetter, @@ -75,52 +94,53 @@ impl where - AssetId: Clone, AssetIdInfoGetter: AssetMultiLocationGetter, AssetsPallet: Inspect, BalancesPallet: Currency, XcmPallet: DropAssets, + Weigher: DropAssetsWeigher, { // assets are whatever the Holding Register had when XCVM halts - fn drop_assets(origin: &MultiLocation, assets: Assets) -> u64 { - let multi_assets: Vec = assets.into(); - let mut trap: Vec = Vec::new(); + fn drop_assets(origin: &MultiLocation, mut assets: Assets) -> u64 { + const NATIVE_LOCATION: MultiLocation = MultiLocation { parents: 0, interior: Here }; - for asset in multi_assets { - if let MultiAsset { id: Concrete(location), fun: Fungible(amount) } = asset.clone() { - // is location a fungible on AssetRegistry? - if let Some(asset_id) = AssetIdInfoGetter::get_asset_id(location.clone()) { - let min_balance = AssetsPallet::minimum_balance(asset_id); + let mut weight = { + assets.non_fungible.clear(); + Weigher::default() + }; - // only trap if amount ≥ min_balance - // do nothing otherwise (asset is lost) - if min_balance <= amount.saturated_into::() { - trap.push(asset); - } + assets.fungible.retain(|id, &mut amount| { + if let Concrete(location) = id { + match AssetIdInfoGetter::get_asset_id(location) { + Some(asset_id) => { + weight += Weigher::fungible(); - // is location the native token? - } else if location == (MultiLocation { parents: 0, interior: Here }) { - let min_balance = BalancesPallet::minimum_balance(); + // only trap if amount ≥ min_balance + // do nothing otherwise (asset is lost) + amount.saturated_into::() >= + AssetsPallet::minimum_balance(asset_id) + }, + None => { + weight += Weigher::native(); - // only trap if amount ≥ min_balance - // do nothing otherwise (asset is lost) - if min_balance <= amount.saturated_into::() { - trap.push(asset); - } + // only trap if native token and amount ≥ min_balance + // do nothing otherwise (asset is lost) + *location == NATIVE_LOCATION && + amount.saturated_into::() >= + BalancesPallet::minimum_balance() + }, } + } else { + weight += Weigher::default(); + false } - } - - // TODO: put real weight of execution up until this point here - let mut weight = 0; - - if !trap.is_empty() { - // we have filtered out non-compliant assets - // insert valid assets into the asset trap implemented by XcmPallet - weight += XcmPallet::drop_assets(origin, trap.into()); - } + }); + // we have filtered out non-compliant assets + // insert valid assets into the asset trap implemented by XcmPallet + weight += XcmPallet::drop_assets(origin, assets); weight } } diff --git a/runtime/trappist/Cargo.toml b/runtime/trappist/Cargo.toml index 88db8587..215f9ce9 100644 --- a/runtime/trappist/Cargo.toml +++ b/runtime/trappist/Cargo.toml @@ -15,7 +15,7 @@ substrate-wasm-builder = { git = "https://github.com/paritytech/substrate", bran [dependencies] hex-literal = { version = "0.3.4", optional = true } -codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"]} +codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] } log = { version = "0.4.17", default-features = false } scale-info = { version = "2.3.1", default-features = false, features = ["derive"] } serde = { version = "1.0.140", optional = true, features = ["derive"] } @@ -85,7 +85,7 @@ cumulus-primitives-utility = { git = "https://github.com/paritytech/cumulus", br pallet-collator-selection = { git = "https://github.com/paritytech/cumulus", branch = "polkadot-v0.9.37", default-features = false } parachains-common = { git = "https://github.com/paritytech/cumulus", branch = "polkadot-v0.9.37", default-features = false } parachain-info = { git = "https://github.com/paritytech/cumulus", branch = "polkadot-v0.9.37", default-features = false } -cumulus-pallet-session-benchmarking = { git = "https://github.com/paritytech/cumulus", branch = "polkadot-v0.9.37", default-features = false, version = "3.0.0"} +cumulus-pallet-session-benchmarking = { git = "https://github.com/paritytech/cumulus", branch = "polkadot-v0.9.37", default-features = false, version = "3.0.0" } # Polkadot Dependencies kusama-runtime-constants = { git = "https://github.com/paritytech/polkadot", default-features = false, branch = "release-v0.9.37" } @@ -104,15 +104,16 @@ pallet-dex-rpc-runtime-api = { version = "0.0.1", git = "https://github.com/pari pallet-chess = { git = "https://github.com/SubstrateChess/pallet-chess.git", default-features = false, branch = "polkadot-v0.9.37" } # Trappist Pallets -pallet-asset-registry = { version = "0.0.1", default-features = false, path = "../../pallets/asset-registry" } +pallet-asset-registry = { default-features = false, path = "../../pallets/asset-registry" } +pallet-benchmarks = { default-features = false, path = "../../pallets/benchmarks" } [features] default = ["std"] std = [ - "codec/std", - "serde", - "scale-info/std", - "log/std", + "codec/std", + "serde", + "scale-info/std", + "log/std", "sp-api/std", "sp-block-builder/std", "sp-consensus-aura/std", @@ -129,32 +130,32 @@ std = [ "frame-support/std", "frame-system/std", "frame-system-rpc-runtime-api/std", - "pallet-assets/std", + "pallet-assets/std", "pallet-asset-tx-payment/std", - "pallet-aura/std", - "pallet-authorship/std", - "pallet-balances/std", + "pallet-aura/std", + "pallet-authorship/std", + "pallet-balances/std", "pallet-chess/std", - "pallet-collator-selection/std", - "pallet-collective/std", - "pallet-contracts/std", - "pallet-contracts-primitives/std", + "pallet-collator-selection/std", + "pallet-collective/std", + "pallet-contracts/std", + "pallet-contracts-primitives/std", "pallet-democracy/std", - "pallet-dex/std", + "pallet-dex/std", "pallet-dex-rpc-runtime-api/std", "pallet-identity/std", "pallet-multisig/std", - "pallet-randomness-collective-flip/std", - "pallet-scheduler/std", - "pallet-session/std", - "pallet-sudo/std", - "pallet-timestamp/std", - "pallet-transaction-payment-rpc-runtime-api/std", - "pallet-transaction-payment/std", + "pallet-randomness-collective-flip/std", + "pallet-scheduler/std", + "pallet-session/std", + "pallet-sudo/std", + "pallet-timestamp/std", + "pallet-transaction-payment-rpc-runtime-api/std", + "pallet-transaction-payment/std", "pallet-treasury/std", - "pallet-uniques/std", - "pallet-utility/std", - "pallet-xcm/std", + "pallet-uniques/std", + "pallet-utility/std", + "pallet-xcm/std", "cumulus-pallet-aura-ext/std", "cumulus-pallet-parachain-system/std", "cumulus-pallet-xcm/std", @@ -173,29 +174,30 @@ std = [ "xcm/std" ] runtime-benchmarks = [ - "hex-literal", + "hex-literal", "sp-runtime/runtime-benchmarks", "xcm-builder/runtime-benchmarks", "frame-benchmarking/runtime-benchmarks", "frame-system-benchmarking/runtime-benchmarks", "frame-support/runtime-benchmarks", "frame-system/runtime-benchmarks", - "pallet-assets/runtime-benchmarks", + "pallet-assets/runtime-benchmarks", "pallet-asset-registry/runtime-benchmarks", - "pallet-balances/runtime-benchmarks", - "pallet-collator-selection/runtime-benchmarks", - "pallet-collective/runtime-benchmarks", - "pallet-contracts/runtime-benchmarks", + "pallet-balances/runtime-benchmarks", + "pallet-benchmarks/runtime-benchmarks", + "pallet-collator-selection/runtime-benchmarks", + "pallet-collective/runtime-benchmarks", + "pallet-contracts/runtime-benchmarks", "pallet-democracy/runtime-benchmarks", - "pallet-dex/runtime-benchmarks", + "pallet-dex/runtime-benchmarks", "pallet-identity/runtime-benchmarks", "pallet-multisig/runtime-benchmarks", - "pallet-scheduler/runtime-benchmarks", - "pallet-timestamp/runtime-benchmarks", + "pallet-scheduler/runtime-benchmarks", + "pallet-timestamp/runtime-benchmarks", "pallet-treasury/runtime-benchmarks", - "pallet-uniques/runtime-benchmarks", - "pallet-utility/runtime-benchmarks", - "pallet-xcm/runtime-benchmarks", + "pallet-uniques/runtime-benchmarks", + "pallet-utility/runtime-benchmarks", + "pallet-xcm/runtime-benchmarks", "cumulus-pallet-session-benchmarking/runtime-benchmarks", "cumulus-pallet-xcmp-queue/runtime-benchmarks" ] diff --git a/runtime/trappist/src/impls.rs b/runtime/trappist/src/impls.rs index 393d4ed5..8c53ef2f 100644 --- a/runtime/trappist/src/impls.rs +++ b/runtime/trappist/src/impls.rs @@ -81,9 +81,8 @@ mod tests { use sp_runtime::{ testing::Header, traits::{BlakeTwo256, ConstU32, ConstU64, IdentityLookup}, - Perbill, + Perbill, Permill, }; - use xcm::prelude::*; type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; type Block = frame_system::mocking::MockBlock; @@ -252,9 +251,9 @@ mod tests { DealWithFees::on_unbalanceds(vec![fee, tip].into_iter()); - /// Author should get 20% of the fee + the 100% of the tip. (50) + // Author should get 20% of the fee + the 100% of the tip. (50) assert_eq!(Balances::free_balance(CollatorSelection::account_id()), 50); - /// Treasury should get 80% of the fee. (80) + // Treasury should get 80% of the fee. (80) assert_eq!(Treasury::pot(), 80); }); } diff --git a/runtime/trappist/src/lib.rs b/runtime/trappist/src/lib.rs index 3e2be6ff..ef8fe6a4 100644 --- a/runtime/trappist/src/lib.rs +++ b/runtime/trappist/src/lib.rs @@ -17,31 +17,11 @@ #![cfg_attr(not(feature = "std"), no_std)] #![recursion_limit = "256"] -// Make the WASM binary available. -#[cfg(feature = "std")] -include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); - -pub mod constants; -mod contracts; -pub mod impls; -pub mod xcm_config; +#[cfg(feature = "runtime-benchmarks")] +#[macro_use] +extern crate frame_benchmarking; use cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases; -use sp_api::impl_runtime_apis; -use sp_core::{crypto::KeyTypeId, ConstU8, OpaqueMetadata}; -use sp_runtime::{ - create_runtime_str, generic, impl_opaque_keys, - traits::{AccountIdLookup, BlakeTwo256, Block as BlockT, ConvertInto}, - transaction_validity::{TransactionSource, TransactionValidity}, - ApplyExtrinsicResult, Percent, Permill, -}; - -use sp_std::prelude::*; -#[cfg(feature = "std")] -use sp_version::NativeVersion; -use sp_version::RuntimeVersion; - -use constants::{currency::*, fee::WeightToFee}; use frame_support::{ construct_runtime, dispatch::DispatchClass, @@ -60,25 +40,44 @@ use frame_system::{ limits::{BlockLength, BlockWeights}, EnsureRoot, EnsureSigned, }; - +// Polkadot imports +use pallet_xcm::{EnsureXcm, IsMajorityOfBody}; pub use parachains_common as common; pub use parachains_common::{ impls::AssetsToBlockAuthor, opaque, AccountId, AssetId, AuraId, Balance, BlockNumber, Hash, Header, Index, Signature, AVERAGE_ON_INITIALIZE_RATIO, DAYS, HOURS, MAXIMUM_BLOCK_WEIGHT, MINUTES, NORMAL_DISPATCH_RATIO, SLOT_DURATION, }; +use polkadot_runtime_common::{prod_or_fast, BlockHashCount, SlowAdjustingFeeUpdate}; +use sp_api::impl_runtime_apis; +use sp_core::{crypto::KeyTypeId, ConstU8, OpaqueMetadata}; +#[cfg(any(feature = "std", test))] +pub use sp_runtime::BuildStorage; +use sp_runtime::{ + create_runtime_str, generic, impl_opaque_keys, + traits::{AccountIdLookup, BlakeTwo256, Block as BlockT, ConvertInto}, + transaction_validity::{TransactionSource, TransactionValidity}, + ApplyExtrinsicResult, Percent, Permill, +}; +use sp_std::prelude::*; +#[cfg(feature = "std")] +use sp_version::NativeVersion; +use sp_version::RuntimeVersion; +use xcm::latest::prelude::BodyId; +use constants::{currency::*, fee::WeightToFee}; use impls::DealWithFees; - use xcm_config::{CollatorSelectionUpdateOrigin, RelayLocation}; -#[cfg(any(feature = "std", test))] -pub use sp_runtime::BuildStorage; +// Make the WASM binary available. +#[cfg(feature = "std")] +include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); -// Polkadot imports -use pallet_xcm::{EnsureXcm, IsMajorityOfBody}; -use polkadot_runtime_common::{prod_or_fast, BlockHashCount, SlowAdjustingFeeUpdate}; -use xcm::latest::prelude::BodyId; +pub mod constants; +mod contracts; +pub mod impls; +mod weights; +pub mod xcm_config; pub const MICROUNIT: Balance = 1_000_000; @@ -689,15 +688,12 @@ construct_runtime!( } ); -#[cfg(feature = "runtime-benchmarks")] -#[macro_use] -extern crate frame_benchmarking; - #[cfg(feature = "runtime-benchmarks")] mod benches { define_benchmarks!( [frame_system, SystemBench::] [pallet_asset_registry, AssetRegistry] + [pallet_benchmarks, pallet_benchmarks::Pallet::] [pallet_balances, Balances] [pallet_session, SessionBench::] [pallet_timestamp, Timestamp] @@ -962,6 +958,21 @@ impl_runtime_apis! { use cumulus_pallet_session_benchmarking::Pallet as SessionBench; impl cumulus_pallet_session_benchmarking::Config for Runtime {} + use xcm_primitives::TrappistDropAssets; + use xcm::prelude::MultiLocation; + use crate::weights::TrappistDropAssetsWeigher; + impl pallet_benchmarks::Config for Runtime { + type AssetId = AssetId; + type Balance = Balance; + type ExistentialDeposit = ConstU128; + type DropAssets = TrappistDropAssets; + + fn register_asset(asset_id: Self::AssetId, location: MultiLocation) { + pallet_asset_registry::AssetMultiLocationId::::insert(&location, asset_id); + pallet_asset_registry::AssetIdMultiLocation::::insert(asset_id, location); + } + } + let whitelist: Vec = vec![ // Block Number hex_literal::hex!("26aa394eea5630e07c48ae0c9558cef702a5c1b19ab7a04f536c519aca4983ac").to_vec().into(), diff --git a/runtime/trappist/src/weights/mod.rs b/runtime/trappist/src/weights/mod.rs new file mode 100644 index 00000000..7ee697c9 --- /dev/null +++ b/runtime/trappist/src/weights/mod.rs @@ -0,0 +1,21 @@ +use ::pallet_benchmarks::WeightInfo; +use xcm_primitives::DropAssetsWeigher; + +use crate::Runtime; + +mod pallet_benchmarks; + +pub struct TrappistDropAssetsWeigher(); +impl DropAssetsWeigher for TrappistDropAssetsWeigher { + fn fungible() -> u64 { + pallet_benchmarks::WeightInfo::::drop_assets_fungible().ref_time() + } + + fn native() -> u64 { + pallet_benchmarks::WeightInfo::::drop_assets_native().ref_time() + } + + fn default() -> u64 { + pallet_benchmarks::WeightInfo::::drop_assets_default().ref_time() + } +} diff --git a/runtime/trappist/src/weights/pallet_benchmarks.rs b/runtime/trappist/src/weights/pallet_benchmarks.rs new file mode 100644 index 00000000..0affc815 --- /dev/null +++ b/runtime/trappist/src/weights/pallet_benchmarks.rs @@ -0,0 +1,53 @@ + +//! Autogenerated weights for `pallet_benchmarks` +//! +//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev +//! DATE: 2023-05-08, STEPS: `20`, REPEAT: 10, LOW RANGE: `[]`, HIGH RANGE: `[]` +//! HOSTNAME: `kalan-x1x`, CPU: `12th Gen Intel(R) Core(TM) i7-12800H` +//! EXECUTION: None, WASM-EXECUTION: Compiled, CHAIN: Some("dev"), DB CACHE: 1024 + +// Executed Command: +// target/release/trappist-collator +// benchmark +// pallet +// --chain +// dev +// --pallet +// pallet_benchmarks +// --extrinsic +// * +// --steps +// 20 +// --repeat +// 10 +// --output +// runtime/trappist/src/weights/pallet_benchmarks.rs + +#![cfg_attr(rustfmt, rustfmt_skip)] +#![allow(unused_parens)] +#![allow(unused_imports)] + +use frame_support::{traits::Get, weights::Weight}; +use sp_std::marker::PhantomData; + +/// Weight functions for `pallet_benchmarks`. +pub struct WeightInfo(PhantomData); +impl pallet_benchmarks::WeightInfo for WeightInfo { + // Storage: AssetRegistry AssetMultiLocationId (r:1 w:0) + // Storage: Assets Asset (r:1 w:0) + fn drop_assets_fungible() -> Weight { + // Minimum execution time: 4_589 nanoseconds. + Weight::from_ref_time(4_898_000) + .saturating_add(T::DbWeight::get().reads(2)) + } + // Storage: AssetRegistry AssetMultiLocationId (r:1 w:0) + fn drop_assets_native() -> Weight { + // Minimum execution time: 2_157 nanoseconds. + Weight::from_ref_time(2_314_000) + .saturating_add(T::DbWeight::get().reads(1)) + } + fn drop_assets_default() -> Weight { + // Minimum execution time: 130 nanoseconds. + Weight::from_ref_time(154_000) + } +} diff --git a/runtime/trappist/src/xcm_config.rs b/runtime/trappist/src/xcm_config.rs index 7033c600..ab8c30ef 100644 --- a/runtime/trappist/src/xcm_config.rs +++ b/runtime/trappist/src/xcm_config.rs @@ -13,7 +13,9 @@ // See the License for the specific language governing permissions and // limitations under the License. -use crate::{constants::fee::default_fee_per_second, impls::ToAuthor}; +use crate::{ + constants::fee::default_fee_per_second, impls::ToAuthor, weights::TrappistDropAssetsWeigher, +}; use super::{ AccountId, AssetRegistry, Assets, Balance, Balances, ParachainInfo, ParachainSystem, @@ -262,8 +264,15 @@ impl xcm_executor::Config for XcmConfig { UsingComponents>, ); type ResponseHandler = PolkadotXcm; - type AssetTrap = - TrappistDropAssets; + type AssetTrap = TrappistDropAssets< + AssetId, + AssetRegistry, + Assets, + Balances, + PolkadotXcm, + AccountId, + TrappistDropAssetsWeigher, + >; type AssetClaims = PolkadotXcm; type SubscriptionService = PolkadotXcm; } From cff6385721e21003a3a41b559fe427d52fae044c Mon Sep 17 00:00:00 2001 From: kalaninja Date: Wed, 10 May 2023 12:01:17 +0700 Subject: [PATCH 2/3] fix arithmetic --- primitives/xcm/src/lib.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/primitives/xcm/src/lib.rs b/primitives/xcm/src/lib.rs index 9acc4c67..88a11361 100644 --- a/primitives/xcm/src/lib.rs +++ b/primitives/xcm/src/lib.rs @@ -1,7 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] use frame_support::{ - sp_runtime::SaturatedConversion, + sp_runtime::{SaturatedConversion, Saturating}, traits::{fungibles::Inspect, Currency}, }; use sp_std::{borrow::Borrow, marker::PhantomData}; @@ -115,7 +115,7 @@ impl { - weight += Weigher::fungible(); + weight.saturating_accrue(Weigher::fungible()); // only trap if amount ≥ min_balance // do nothing otherwise (asset is lost) @@ -123,7 +123,7 @@ impl { - weight += Weigher::native(); + weight.saturating_accrue(Weigher::native()); // only trap if native token and amount ≥ min_balance // do nothing otherwise (asset is lost) @@ -133,14 +133,13 @@ impl Date: Thu, 11 May 2023 10:17:55 +0700 Subject: [PATCH 3/3] rename `pallet-benchmark` to `trappist_runtime_benchmarks` --- Cargo.lock | 34 +++++++++---------- pallets/benchmarks/Cargo.toml | 2 +- runtime/trappist/Cargo.toml | 4 +-- runtime/trappist/src/lib.rs | 4 +-- runtime/trappist/src/weights/mod.rs | 10 +++--- ...arks.rs => trappist_runtime_benchmarks.rs} | 10 +++--- 6 files changed, 32 insertions(+), 32 deletions(-) rename runtime/trappist/src/weights/{pallet_benchmarks.rs => trappist_runtime_benchmarks.rs} (81%) diff --git a/Cargo.lock b/Cargo.lock index a877bacb..718bfc71 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5626,22 +5626,6 @@ dependencies = [ "sp-std", ] -[[package]] -name = "pallet-benchmarks" -version = "0.1.0" -dependencies = [ - "frame-benchmarking", - "frame-support", - "frame-system", - "parity-scale-codec", - "scale-info", - "sp-core", - "sp-runtime", - "sp-std", - "xcm", - "xcm-executor", -] - [[package]] name = "pallet-bounties" version = "4.0.0-dev" @@ -12222,7 +12206,6 @@ dependencies = [ "pallet-aura", "pallet-authorship", "pallet-balances", - "pallet-benchmarks", "pallet-chess", "pallet-collator-selection", "pallet-collective", @@ -12267,12 +12250,29 @@ dependencies = [ "sp-transaction-pool", "sp-version", "substrate-wasm-builder", + "trappist-runtime-benchmarks", "xcm", "xcm-builder", "xcm-executor", "xcm-primitives", ] +[[package]] +name = "trappist-runtime-benchmarks" +version = "0.1.0" +dependencies = [ + "frame-benchmarking", + "frame-support", + "frame-system", + "parity-scale-codec", + "scale-info", + "sp-core", + "sp-runtime", + "sp-std", + "xcm", + "xcm-executor", +] + [[package]] name = "trappist-service" version = "1.0.0" diff --git a/pallets/benchmarks/Cargo.toml b/pallets/benchmarks/Cargo.toml index ba6faea7..09defed2 100644 --- a/pallets/benchmarks/Cargo.toml +++ b/pallets/benchmarks/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "pallet-benchmarks" +name = "trappist-runtime-benchmarks" version = "0.1.0" edition = "2021" diff --git a/runtime/trappist/Cargo.toml b/runtime/trappist/Cargo.toml index 215f9ce9..796d1c1e 100644 --- a/runtime/trappist/Cargo.toml +++ b/runtime/trappist/Cargo.toml @@ -105,7 +105,7 @@ pallet-chess = { git = "https://github.com/SubstrateChess/pallet-chess.git", def # Trappist Pallets pallet-asset-registry = { default-features = false, path = "../../pallets/asset-registry" } -pallet-benchmarks = { default-features = false, path = "../../pallets/benchmarks" } +trappist-runtime-benchmarks = { default-features = false, path = "../../pallets/benchmarks" } [features] default = ["std"] @@ -184,7 +184,7 @@ runtime-benchmarks = [ "pallet-assets/runtime-benchmarks", "pallet-asset-registry/runtime-benchmarks", "pallet-balances/runtime-benchmarks", - "pallet-benchmarks/runtime-benchmarks", + "trappist-runtime-benchmarks/runtime-benchmarks", "pallet-collator-selection/runtime-benchmarks", "pallet-collective/runtime-benchmarks", "pallet-contracts/runtime-benchmarks", diff --git a/runtime/trappist/src/lib.rs b/runtime/trappist/src/lib.rs index ef8fe6a4..826efc49 100644 --- a/runtime/trappist/src/lib.rs +++ b/runtime/trappist/src/lib.rs @@ -693,7 +693,7 @@ mod benches { define_benchmarks!( [frame_system, SystemBench::] [pallet_asset_registry, AssetRegistry] - [pallet_benchmarks, pallet_benchmarks::Pallet::] + [trappist_runtime_benchmarks, trappist_runtime_benchmarks::Pallet::] [pallet_balances, Balances] [pallet_session, SessionBench::] [pallet_timestamp, Timestamp] @@ -961,7 +961,7 @@ impl_runtime_apis! { use xcm_primitives::TrappistDropAssets; use xcm::prelude::MultiLocation; use crate::weights::TrappistDropAssetsWeigher; - impl pallet_benchmarks::Config for Runtime { + impl trappist_runtime_benchmarks::Config for Runtime { type AssetId = AssetId; type Balance = Balance; type ExistentialDeposit = ConstU128; diff --git a/runtime/trappist/src/weights/mod.rs b/runtime/trappist/src/weights/mod.rs index 7ee697c9..878e0679 100644 --- a/runtime/trappist/src/weights/mod.rs +++ b/runtime/trappist/src/weights/mod.rs @@ -1,21 +1,21 @@ -use ::pallet_benchmarks::WeightInfo; +use ::trappist_runtime_benchmarks::WeightInfo; use xcm_primitives::DropAssetsWeigher; use crate::Runtime; -mod pallet_benchmarks; +mod trappist_runtime_benchmarks; pub struct TrappistDropAssetsWeigher(); impl DropAssetsWeigher for TrappistDropAssetsWeigher { fn fungible() -> u64 { - pallet_benchmarks::WeightInfo::::drop_assets_fungible().ref_time() + trappist_runtime_benchmarks::WeightInfo::::drop_assets_fungible().ref_time() } fn native() -> u64 { - pallet_benchmarks::WeightInfo::::drop_assets_native().ref_time() + trappist_runtime_benchmarks::WeightInfo::::drop_assets_native().ref_time() } fn default() -> u64 { - pallet_benchmarks::WeightInfo::::drop_assets_default().ref_time() + trappist_runtime_benchmarks::WeightInfo::::drop_assets_default().ref_time() } } diff --git a/runtime/trappist/src/weights/pallet_benchmarks.rs b/runtime/trappist/src/weights/trappist_runtime_benchmarks.rs similarity index 81% rename from runtime/trappist/src/weights/pallet_benchmarks.rs rename to runtime/trappist/src/weights/trappist_runtime_benchmarks.rs index 0affc815..c6b0661c 100644 --- a/runtime/trappist/src/weights/pallet_benchmarks.rs +++ b/runtime/trappist/src/weights/trappist_runtime_benchmarks.rs @@ -1,5 +1,5 @@ -//! Autogenerated weights for `pallet_benchmarks` +//! Autogenerated weights for `trappist_runtime_benchmarks` //! //! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev //! DATE: 2023-05-08, STEPS: `20`, REPEAT: 10, LOW RANGE: `[]`, HIGH RANGE: `[]` @@ -13,7 +13,7 @@ // --chain // dev // --pallet -// pallet_benchmarks +// trappist_runtime_benchmarks // --extrinsic // * // --steps @@ -21,7 +21,7 @@ // --repeat // 10 // --output -// runtime/trappist/src/weights/pallet_benchmarks.rs +// runtime/trappist/src/weights/trappist_runtime_benchmarks.rs #![cfg_attr(rustfmt, rustfmt_skip)] #![allow(unused_parens)] @@ -30,9 +30,9 @@ use frame_support::{traits::Get, weights::Weight}; use sp_std::marker::PhantomData; -/// Weight functions for `pallet_benchmarks`. +/// Weight functions for `trappist_runtime_benchmarks`. pub struct WeightInfo(PhantomData); -impl pallet_benchmarks::WeightInfo for WeightInfo { +impl trappist_runtime_benchmarks::WeightInfo for WeightInfo { // Storage: AssetRegistry AssetMultiLocationId (r:1 w:0) // Storage: Assets Asset (r:1 w:0) fn drop_assets_fungible() -> Weight {