1234567891011121314151617#![allow(missing_docs)]1819use core::convert::TryInto;2021use frame_benchmarking::{account, benchmarks};22use frame_support::{23 pallet_prelude::ConstU32,24 traits::{fungible::Balanced, tokens::Precision, Get, Imbalance},25 BoundedVec,26};27use pallet_evm::account::CrossAccountId;28use sp_runtime::{traits::Zero, DispatchError};29use sp_std::vec::Vec;30use up_data_structs::{31 AccessMode, CollectionId, CollectionMode, CollectionPermissions, CreateCollectionData,32 NestingPermissions, PropertiesPermissionMap, Property, PropertyKey, PropertyValue,33 MAX_COLLECTION_DESCRIPTION_LENGTH, MAX_COLLECTION_NAME_LENGTH, MAX_PROPERTIES_PER_ITEM,34 MAX_TOKEN_PREFIX_LENGTH,35};3637use crate::{CollectionHandle, Config, Pallet};3839const SEED: u32 = 1;4041pub fn create_data<const S: u32>() -> BoundedVec<u8, ConstU32<S>> {42 create_var_data::<S>(S)43}44pub fn create_u16_data<const S: u32>() -> BoundedVec<u16, ConstU32<S>> {45 (0..S)46 .map(|v| (v & 0xffff) as u16)47 .collect::<Vec<_>>()48 .try_into()49 .unwrap()50}51pub fn create_var_data<const S: u32>(size: u32) -> BoundedVec<u8, ConstU32<S>> {52 assert!(size <= S, "size ({size}) should be less within bound ({S})",);53 (0..size)54 .map(|v| (v & 0xff) as u8)55 .collect::<Vec<_>>()56 .try_into()57 .unwrap()58}59pub fn property_key(id: usize) -> PropertyKey {60 #[cfg(not(feature = "std"))]61 use alloc::string::ToString;62 let mut data = create_data();63 64 for i in 0..data.len() {65 data[i] = b'0';66 }67 let bytes = id.to_string();68 let len = data.len();69 data[len - bytes.len()..].copy_from_slice(bytes.as_bytes());70 data71}72pub fn property_value() -> PropertyValue {73 create_data()74}7576pub fn create_collection_raw<T: Config, R>(77 owner: T::CrossAccountId,78 mode: CollectionMode,79 handler: impl FnOnce(80 T::CrossAccountId,81 CreateCollectionData<T::CrossAccountId>,82 ) -> Result<CollectionId, DispatchError>,83 cast: impl FnOnce(CollectionHandle<T>) -> R,84) -> Result<R, DispatchError> {85 let imbalance = <T as Config>::Currency::deposit(86 owner.as_sub(),87 T::CollectionCreationPrice::get(),88 Precision::Exact,89 )?;90 debug_assert!(imbalance.peek().is_zero());91 let name = create_u16_data::<MAX_COLLECTION_NAME_LENGTH>();92 let description = create_u16_data::<MAX_COLLECTION_DESCRIPTION_LENGTH>();93 let token_prefix = create_data::<MAX_TOKEN_PREFIX_LENGTH>();94 handler(95 owner,96 CreateCollectionData {97 mode,98 name,99 description,100 token_prefix,101 permissions: Some(CollectionPermissions {102 nesting: Some(NestingPermissions {103 token_owner: false,104 collection_admin: false,105 restricted: None,106 #[cfg(feature = "runtime-benchmarks")]107 permissive: true,108 }),109 mint_mode: Some(true),110 ..Default::default()111 }),112 ..Default::default()113 },114 )115 .and_then(CollectionHandle::try_get)116 .map(cast)117}118fn create_collection<T: Config>(119 owner: T::CrossAccountId,120) -> Result<CollectionHandle<T>, DispatchError> {121 create_collection_raw(122 owner,123 CollectionMode::NFT,124 |owner: T::CrossAccountId, data| <Pallet<T>>::init_collection(owner.clone(), owner, data),125 |h| h,126 )127}128129pub fn load_is_admin_and_property_permissions<T: Config>(130 collection: &CollectionHandle<T>,131 sender: &T::CrossAccountId,132) -> (bool, PropertiesPermissionMap) {133 (134 collection.is_owner_or_admin(sender),135 <Pallet<T>>::property_permissions(collection.id),136 )137}138139140141142143144145146147148149150151#[macro_export]152macro_rules! bench_init {153 ($name:ident: sub $(($id:expr))?; $($rest:tt)*) => {154 let $name: T::AccountId = account(stringify!($name), 0 $(+ $id)?, SEED);155 bench_init!($($rest)*);156 };157 ($name:ident: collection($owner:ident); $($rest:tt)*) => {158 let $name = create_collection::<T>(T::CrossAccountId::from_sub($owner.clone()))?;159 bench_init!($($rest)*);160 };161 ($name:ident: cross; $($rest:tt)*) => {162 let $name = T::CrossAccountId::from_sub($name);163 bench_init!($($rest)*);164 };165 ($name:ident: cross_sub $(($id:expr))?; $($rest:tt)*) => {166 let account: T::AccountId = account(stringify!($name), 0 $(+ $id)?, SEED);167 let $name = T::CrossAccountId::from_sub(account);168 bench_init!($($rest)*);169 };170 ($name:ident: cross_from_sub; $($rest:tt)*) => {171 let $name = T::CrossAccountId::from_sub($name);172 bench_init!($($rest)*);173 };174 ($name:ident: cross_from_sub($from:ident); $($rest:tt)*) => {175 let $name = T::CrossAccountId::from_sub($from);176 bench_init!($($rest)*);177 };178 () => {}179}180181benchmarks! {182 set_collection_properties {183 let b in 0..MAX_PROPERTIES_PER_ITEM;184 bench_init!{185 owner: sub; collection: collection(owner);186 owner: cross_from_sub;187 };188 let props = (0..b).map(|p| Property {189 key: property_key(p as usize),190 value: property_value(),191 }).collect::<Vec<_>>();192 }: {<Pallet<T>>::set_collection_properties(&collection, &owner, props.into_iter())?}193194 delete_collection_properties {195 let b in 0..MAX_PROPERTIES_PER_ITEM;196 bench_init!{197 owner: sub; collection: collection(owner);198 owner: cross_from_sub;199 };200 let props = (0..b).map(|p| Property {201 key: property_key(p as usize),202 value: property_value(),203 }).collect::<Vec<_>>();204 <Pallet<T>>::set_collection_properties(&collection, &owner, props.into_iter())?;205 let to_delete = (0..b).map(|p| property_key(p as usize)).collect::<Vec<_>>();206 }: {<Pallet<T>>::delete_collection_properties(&collection, &owner, to_delete.into_iter())?}207208 check_accesslist{209 bench_init!{210 owner: sub; collection: collection(owner);211 sender: cross_from_sub(owner);212 };213214 let mut collection_handle = <CollectionHandle<T>>::try_get(collection.id)?;215 <Pallet<T>>::update_permissions(216 &sender,217 &mut collection_handle,218 CollectionPermissions { access: Some(AccessMode::AllowList), ..Default::default() }219 )?;220221 <Pallet<T>>::toggle_allowlist(222 &collection,223 &sender,224 &sender,225 true,226 )?;227228 assert_eq!(collection_handle.permissions.access(), AccessMode::AllowList);229230 }: {collection_handle.check_allowlist(&sender)?;}231232 init_token_properties_common {233 bench_init!{234 owner: sub; collection: collection(owner);235 sender: sub;236 sender: cross_from_sub(sender);237 };238 }: {load_is_admin_and_property_permissions(&collection, &sender);}239}