git.delta.rocks / unique-network / refs/commits / dd5e7347d781

difftreelog

source

pallets/common/src/benchmarking.rs4.1 KiBsourcehistory
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617use sp_std::vec::Vec;18use crate::{Config, CollectionHandle};19use up_data_structs::{20	CollectionMode, CreateCollectionData, CollectionId, MAX_COLLECTION_NAME_LENGTH,21	MAX_COLLECTION_DESCRIPTION_LENGTH, MAX_TOKEN_PREFIX_LENGTH, OFFCHAIN_SCHEMA_LIMIT,22	VARIABLE_ON_CHAIN_SCHEMA_LIMIT, CONST_ON_CHAIN_SCHEMA_LIMIT,23};24use frame_support::{25	traits::{Currency, Get},26	pallet_prelude::ConstU32,27	BoundedVec,28};29use core::convert::TryInto;30use sp_runtime::DispatchError;3132pub fn create_data<const S: u32>() -> BoundedVec<u8, ConstU32<S>> {33	create_var_data::<S>(S)34}35pub fn create_u16_data<const S: u32>() -> BoundedVec<u16, ConstU32<S>> {36	(0..S)37		.map(|v| (v & 0xffff) as u16)38		.collect::<Vec<_>>()39		.try_into()40		.unwrap()41}42pub fn create_var_data<const S: u32>(size: u32) -> BoundedVec<u8, ConstU32<S>> {43	assert!(44		size <= S,45		"size ({}) should be less within bound ({})",46		size,47		S48	);49	(0..size)50		.map(|v| (v & 0xff) as u8)51		.collect::<Vec<_>>()52		.try_into()53		.unwrap()54}5556pub fn create_collection_raw<T: Config, R>(57	owner: T::AccountId,58	mode: CollectionMode,59	handler: impl FnOnce(60		T::AccountId,61		CreateCollectionData<T::AccountId>,62	) -> Result<CollectionId, DispatchError>,63	cast: impl FnOnce(CollectionHandle<T>) -> R,64) -> Result<R, DispatchError> {65	T::Currency::deposit_creating(&owner, T::CollectionCreationPrice::get());66	let name = create_u16_data::<MAX_COLLECTION_NAME_LENGTH>();67	let description = create_u16_data::<MAX_COLLECTION_DESCRIPTION_LENGTH>();68	let token_prefix = create_data::<MAX_TOKEN_PREFIX_LENGTH>();69	let offchain_schema = create_data::<OFFCHAIN_SCHEMA_LIMIT>();70	let variable_on_chain_schema = create_data::<VARIABLE_ON_CHAIN_SCHEMA_LIMIT>();71	let const_on_chain_schema = create_data::<CONST_ON_CHAIN_SCHEMA_LIMIT>();72	handler(73		owner,74		CreateCollectionData {75			mode,76			name,77			description,78			token_prefix,79			offchain_schema,80			variable_on_chain_schema,81			const_on_chain_schema,82			..Default::default()83		},84	)85	.and_then(CollectionHandle::try_get)86	.map(cast)87}8889/// Helper macros, which handles all benchmarking preparation in semi-declarative way90///91/// `name` is a substrate account92/// - name: sub[(id)]93/// `name` is a collection with owner `owner`94/// - name: collection(owner)95/// `name` is a cross account based on substrate96/// - name: cross_sub[(id)]97/// `name` is a cross account, which maps to substrate account `name`98/// - name: cross_from_sub99/// `name` is a cross account, which maps to substrate account `other_name`100/// - name: cross_from_sub(other_name)101#[macro_export]102macro_rules! bench_init {103	($name:ident: sub $(($id:expr))?; $($rest:tt)*) => {104		let $name: T::AccountId = account(stringify!($name), 0 $(+ $id)?, SEED);105		bench_init!($($rest)*);106	};107	($name:ident: collection($owner:ident); $($rest:tt)*) => {108		let $name = create_collection::<T>($owner.clone())?;109		bench_init!($($rest)*);110	};111	($name:ident: cross; $($rest:tt)*) => {112		let $name = T::CrossAccountId::from_sub($name);113		bench_init!($($rest)*);114	};115	($name:ident: cross_sub $(($id:expr))?; $($rest:tt)*) => {116		let account: T::AccountId = account(stringify!($name), 0 $(+ $id)?, SEED);117		let $name = T::CrossAccountId::from_sub(account);118		bench_init!($($rest)*);119	};120	($name:ident: cross_from_sub; $($rest:tt)*) => {121		let $name = T::CrossAccountId::from_sub($name);122		bench_init!($($rest)*);123	};124	($name:ident: cross_from_sub($from:ident); $($rest:tt)*) => {125		let $name = T::CrossAccountId::from_sub($from);126		bench_init!($($rest)*);127	};128	() => {}129}