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

difftreelog

source

pallets/common/src/benchmarking.rs3.4 KiBsourcehistory
1use sp_std::vec::Vec;2use crate::{Config, CollectionHandle};3use up_data_structs::{4	CollectionMode, CreateCollectionData, CollectionId, MAX_COLLECTION_NAME_LENGTH,5	MAX_COLLECTION_DESCRIPTION_LENGTH, MAX_TOKEN_PREFIX_LENGTH, OFFCHAIN_SCHEMA_LIMIT,6	VARIABLE_ON_CHAIN_SCHEMA_LIMIT, CONST_ON_CHAIN_SCHEMA_LIMIT,7};8use frame_support::{9	traits::{Currency, Get},10	pallet_prelude::ConstU32,11	BoundedVec,12};13use core::convert::TryInto;14use sp_runtime::DispatchError;1516pub fn create_data<const S: u32>() -> BoundedVec<u8, ConstU32<S>> {17	create_var_data::<S>(S)18}19pub fn create_u16_data<const S: u32>() -> BoundedVec<u16, ConstU32<S>> {20	(0..S)21		.map(|v| (v & 0xffff) as u16)22		.collect::<Vec<_>>()23		.try_into()24		.unwrap()25}26pub fn create_var_data<const S: u32>(size: u32) -> BoundedVec<u8, ConstU32<S>> {27	assert!(28		size <= S,29		"size ({}) should be less within bound ({})",30		size,31		S32	);33	(0..size)34		.map(|v| (v & 0xff) as u8)35		.collect::<Vec<_>>()36		.try_into()37		.unwrap()38}3940pub fn create_collection_raw<T: Config, R>(41	owner: T::AccountId,42	mode: CollectionMode,43	handler: impl FnOnce(44		T::AccountId,45		CreateCollectionData<T::AccountId>,46	) -> Result<CollectionId, DispatchError>,47	cast: impl FnOnce(CollectionHandle<T>) -> R,48) -> Result<R, DispatchError> {49	T::Currency::deposit_creating(&owner, T::CollectionCreationPrice::get());50	let name = create_u16_data::<MAX_COLLECTION_NAME_LENGTH>();51	let description = create_u16_data::<MAX_COLLECTION_DESCRIPTION_LENGTH>();52	let token_prefix = create_data::<MAX_TOKEN_PREFIX_LENGTH>();53	let offchain_schema = create_data::<OFFCHAIN_SCHEMA_LIMIT>();54	let variable_on_chain_schema = create_data::<VARIABLE_ON_CHAIN_SCHEMA_LIMIT>();55	let const_on_chain_schema = create_data::<CONST_ON_CHAIN_SCHEMA_LIMIT>();56	handler(57		owner,58		CreateCollectionData {59			mode,60			name,61			description,62			token_prefix,63			offchain_schema,64			variable_on_chain_schema,65			const_on_chain_schema,66			..Default::default()67		},68	)69	.and_then(CollectionHandle::try_get)70	.map(cast)71}7273/// Helper macros, which handles all benchmarking preparation in semi-declarative way74///75/// `name` is a substrate account76/// - name: sub[(id)]77/// `name` is a collection with owner `owner`78/// - name: collection(owner)79/// `name` is a cross account based on substrate80/// - name: cross_sub[(id)]81/// `name` is a cross account, which maps to substrate account `name`82/// - name: cross_from_sub83/// `name` is a cross account, which maps to substrate account `other_name`84/// - name: cross_from_sub(other_name)85#[macro_export]86macro_rules! bench_init {87	($name:ident: sub $(($id:expr))?; $($rest:tt)*) => {88		let $name: T::AccountId = account(stringify!($name), 0 $(+ $id)?, SEED);89		bench_init!($($rest)*);90	};91	($name:ident: collection($owner:ident); $($rest:tt)*) => {92		let $name = create_collection::<T>($owner.clone())?;93		bench_init!($($rest)*);94	};95	($name:ident: cross; $($rest:tt)*) => {96		let $name = T::CrossAccountId::from_sub($name);97		bench_init!($($rest)*);98	};99	($name:ident: cross_sub $(($id:expr))?; $($rest:tt)*) => {100		let account: T::AccountId = account(stringify!($name), 0 $(+ $id)?, SEED);101		let $name = T::CrossAccountId::from_sub(account);102		bench_init!($($rest)*);103	};104	($name:ident: cross_from_sub; $($rest:tt)*) => {105		let $name = T::CrossAccountId::from_sub($name);106		bench_init!($($rest)*);107	};108	($name:ident: cross_from_sub($from:ident); $($rest:tt)*) => {109		let $name = T::CrossAccountId::from_sub($from);110		bench_init!($($rest)*);111	};112	() => {}113}