1234567891011121314151617#![allow(missing_docs)]1819use sp_std::vec::Vec;20use crate::{Config, CollectionHandle, Pallet};21use pallet_evm::account::CrossAccountId;22use frame_benchmarking::{benchmarks, account};23use up_data_structs::{24 CollectionMode, CreateCollectionData, CollectionId, Property, PropertyKey, PropertyValue,25 CollectionPermissions, NestingPermissions, AccessMode, MAX_COLLECTION_NAME_LENGTH,26 MAX_COLLECTION_DESCRIPTION_LENGTH, MAX_TOKEN_PREFIX_LENGTH, MAX_PROPERTIES_PER_ITEM,27};28use frame_support::{29 traits::{Get, fungible::Balanced, Imbalance, tokens::Precision},30 pallet_prelude::ConstU32,31 BoundedVec,32};33use core::convert::TryInto;34use sp_runtime::{DispatchError, traits::Zero};3536const SEED: u32 = 1;3738pub fn create_data<const S: u32>() -> BoundedVec<u8, ConstU32<S>> {39 create_var_data::<S>(S)40}41pub fn create_u16_data<const S: u32>() -> BoundedVec<u16, ConstU32<S>> {42 (0..S)43 .map(|v| (v & 0xffff) as u16)44 .collect::<Vec<_>>()45 .try_into()46 .unwrap()47}48pub fn create_var_data<const S: u32>(size: u32) -> BoundedVec<u8, ConstU32<S>> {49 assert!(size <= S, "size ({size}) should be less within bound ({S})",);50 (0..size)51 .map(|v| (v & 0xff) as u8)52 .collect::<Vec<_>>()53 .try_into()54 .unwrap()55}56pub fn property_key(id: usize) -> PropertyKey {57 #[cfg(not(feature = "std"))]58 use alloc::string::ToString;59 let mut data = create_data();60 61 for i in 0..data.len() {62 data[i] = b'0';63 }64 let bytes = id.to_string();65 let len = data.len();66 data[len - bytes.len()..].copy_from_slice(bytes.as_bytes());67 data68}69pub fn property_value() -> PropertyValue {70 create_data()71}7273pub fn create_collection_raw<T: Config, R>(74 owner: T::CrossAccountId,75 mode: CollectionMode,76 handler: impl FnOnce(77 T::CrossAccountId,78 CreateCollectionData<T::CrossAccountId>,79 ) -> Result<CollectionId, DispatchError>,80 cast: impl FnOnce(CollectionHandle<T>) -> R,81) -> Result<R, DispatchError> {82 let imbalance = <T as Config>::Currency::deposit(83 owner.as_sub(),84 T::CollectionCreationPrice::get(),85 Precision::Exact,86 )?;87 debug_assert!(imbalance.peek().is_zero());88 let name = create_u16_data::<MAX_COLLECTION_NAME_LENGTH>();89 let description = create_u16_data::<MAX_COLLECTION_DESCRIPTION_LENGTH>();90 let token_prefix = create_data::<MAX_TOKEN_PREFIX_LENGTH>();91 handler(92 owner,93 CreateCollectionData {94 mode,95 name,96 description,97 token_prefix,98 permissions: Some(CollectionPermissions {99 nesting: Some(NestingPermissions {100 token_owner: false,101 collection_admin: false,102 restricted: None,103 #[cfg(feature = "runtime-benchmarks")]104 permissive: true,105 }),106 mint_mode: Some(true),107 ..Default::default()108 }),109 ..Default::default()110 },111 )112 .and_then(CollectionHandle::try_get)113 .map(cast)114}115fn create_collection<T: Config>(116 owner: T::CrossAccountId,117) -> Result<CollectionHandle<T>, DispatchError> {118 create_collection_raw(119 owner,120 CollectionMode::NFT,121 |owner: T::CrossAccountId, data| <Pallet<T>>::init_collection(owner.clone(), owner, data),122 |h| h,123 )124}125126127128129130131132133134135136137138#[macro_export]139macro_rules! bench_init {140 ($name:ident: sub $(($id:expr))?; $($rest:tt)*) => {141 let $name: T::AccountId = account(stringify!($name), 0 $(+ $id)?, SEED);142 bench_init!($($rest)*);143 };144 ($name:ident: collection($owner:ident); $($rest:tt)*) => {145 let $name = create_collection::<T>(T::CrossAccountId::from_sub($owner.clone()))?;146 bench_init!($($rest)*);147 };148 ($name:ident: cross; $($rest:tt)*) => {149 let $name = T::CrossAccountId::from_sub($name);150 bench_init!($($rest)*);151 };152 ($name:ident: cross_sub $(($id:expr))?; $($rest:tt)*) => {153 let account: T::AccountId = account(stringify!($name), 0 $(+ $id)?, SEED);154 let $name = T::CrossAccountId::from_sub(account);155 bench_init!($($rest)*);156 };157 ($name:ident: cross_from_sub; $($rest:tt)*) => {158 let $name = T::CrossAccountId::from_sub($name);159 bench_init!($($rest)*);160 };161 ($name:ident: cross_from_sub($from:ident); $($rest:tt)*) => {162 let $name = T::CrossAccountId::from_sub($from);163 bench_init!($($rest)*);164 };165 () => {}166}167168benchmarks! {169 set_collection_properties {170 let b in 0..MAX_PROPERTIES_PER_ITEM;171 bench_init!{172 owner: sub; collection: collection(owner);173 owner: cross_from_sub;174 };175 let props = (0..b).map(|p| Property {176 key: property_key(p as usize),177 value: property_value(),178 }).collect::<Vec<_>>();179 }: {<Pallet<T>>::set_collection_properties(&collection, &owner, props.into_iter())?}180181 delete_collection_properties {182 let b in 0..MAX_PROPERTIES_PER_ITEM;183 bench_init!{184 owner: sub; collection: collection(owner);185 owner: cross_from_sub;186 };187 let props = (0..b).map(|p| Property {188 key: property_key(p as usize),189 value: property_value(),190 }).collect::<Vec<_>>();191 <Pallet<T>>::set_collection_properties(&collection, &owner, props.into_iter())?;192 let to_delete = (0..b).map(|p| property_key(p as usize)).collect::<Vec<_>>();193 }: {<Pallet<T>>::delete_collection_properties(&collection, &owner, to_delete.into_iter())?}194195 check_accesslist{196 bench_init!{197 owner: sub; collection: collection(owner);198 sender: cross_from_sub(owner);199 };200201 let mut collection_handle = <CollectionHandle<T>>::try_get(collection.id)?;202 <Pallet<T>>::update_permissions(203 &sender,204 &mut collection_handle,205 CollectionPermissions { access: Some(AccessMode::AllowList), ..Default::default() }206 )?;207208 <Pallet<T>>::toggle_allowlist(209 &collection,210 &sender,211 &sender,212 true,213 )?;214215 assert_eq!(collection_handle.permissions.access(), AccessMode::AllowList);216217 }: {collection_handle.check_allowlist(&sender)?;}218}