git.delta.rocks / unique-network / refs/commits / 55c4d052d75c

difftreelog

source

pallets/common/src/benchmarking.rs4.0 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	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 const_on_chain_schema = create_data::<CONST_ON_CHAIN_SCHEMA_LIMIT>();71	handler(72		owner,73		CreateCollectionData {74			mode,75			name,76			description,77			token_prefix,78			offchain_schema,79			const_on_chain_schema,80			..Default::default()81		},82	)83	.and_then(CollectionHandle::try_get)84	.map(cast)85}8687/// Helper macros, which handles all benchmarking preparation in semi-declarative way88///89/// `name` is a substrate account90/// - name: sub[(id)]91/// `name` is a collection with owner `owner`92/// - name: collection(owner)93/// `name` is a cross account based on substrate94/// - name: cross_sub[(id)]95/// `name` is a cross account, which maps to substrate account `name`96/// - name: cross_from_sub97/// `name` is a cross account, which maps to substrate account `other_name`98/// - name: cross_from_sub(other_name)99#[macro_export]100macro_rules! bench_init {101	($name:ident: sub $(($id:expr))?; $($rest:tt)*) => {102		let $name: T::AccountId = account(stringify!($name), 0 $(+ $id)?, SEED);103		bench_init!($($rest)*);104	};105	($name:ident: collection($owner:ident); $($rest:tt)*) => {106		let $name = create_collection::<T>($owner.clone())?;107		bench_init!($($rest)*);108	};109	($name:ident: cross; $($rest:tt)*) => {110		let $name = T::CrossAccountId::from_sub($name);111		bench_init!($($rest)*);112	};113	($name:ident: cross_sub $(($id:expr))?; $($rest:tt)*) => {114		let account: T::AccountId = account(stringify!($name), 0 $(+ $id)?, SEED);115		let $name = T::CrossAccountId::from_sub(account);116		bench_init!($($rest)*);117	};118	($name:ident: cross_from_sub; $($rest:tt)*) => {119		let $name = T::CrossAccountId::from_sub($name);120		bench_init!($($rest)*);121	};122	($name:ident: cross_from_sub($from:ident); $($rest:tt)*) => {123		let $name = T::CrossAccountId::from_sub($from);124		bench_init!($($rest)*);125	};126	() => {}127}