1234567891011121314151617#![allow(missing_docs)]1819use core::convert::TryInto;2021use frame_benchmarking::{account, v2::*};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::Vec};30use up_data_structs::{31 AccessMode, CollectionId, CollectionMode, CollectionPermissions, CreateCollectionData,32 NestingPermissions, Property, PropertyKey, PropertyValue, MAX_COLLECTION_DESCRIPTION_LENGTH,33 MAX_COLLECTION_NAME_LENGTH, MAX_PROPERTIES_PER_ITEM, MAX_TOKEN_PREFIX_LENGTH,34};3536use crate::{BenchmarkPropertyWriter, CollectionHandle, Config, Pallet};3738const SEED: u32 = 1;3940pub fn create_data<const S: u32>() -> BoundedVec<u8, ConstU32<S>> {41 create_var_data::<S>(S)42}43pub fn create_u16_data<const S: u32>() -> BoundedVec<u16, ConstU32<S>> {44 (0..S)45 .map(|v| (v & 0xffff) as u16)46 .collect::<Vec<_>>()47 .try_into()48 .unwrap()49}50pub fn create_var_data<const S: u32>(size: u32) -> BoundedVec<u8, ConstU32<S>> {51 assert!(size <= S, "size ({size}) should be less within bound ({S})",);52 (0..size)53 .map(|v| (v & 0xff) as u8)54 .collect::<Vec<_>>()55 .try_into()56 .unwrap()57}58pub fn property_key(id: usize) -> PropertyKey {59 #[cfg(not(feature = "std"))]60 use alloc::string::ToString;61 let mut data = create_data();62 63 for i in 0..data.len() {64 data[i] = b'0';65 }66 let bytes = id.to_string();67 let len = data.len();68 data[len - bytes.len()..].copy_from_slice(bytes.as_bytes());69 data70}71pub fn property_value() -> PropertyValue {72 create_data()73}7475pub fn create_collection_raw<T: Config, R>(76 owner: T::CrossAccountId,77 mode: CollectionMode,78 handler: impl FnOnce(79 T::CrossAccountId,80 CreateCollectionData<T::CrossAccountId>,81 ) -> Result<CollectionId, DispatchError>,82 cast: impl FnOnce(CollectionHandle<T>) -> R,83) -> Result<R, DispatchError> {84 let imbalance = <T as Config>::Currency::deposit(85 owner.as_sub(),86 T::CollectionCreationPrice::get(),87 Precision::Exact,88 )?;89 debug_assert!(imbalance.peek().is_zero());90 let name = create_u16_data::<MAX_COLLECTION_NAME_LENGTH>();91 let description = create_u16_data::<MAX_COLLECTION_DESCRIPTION_LENGTH>();92 let token_prefix = create_data::<MAX_TOKEN_PREFIX_LENGTH>();93 handler(94 owner,95 CreateCollectionData {96 mode,97 name,98 description,99 token_prefix,100 permissions: Some(CollectionPermissions {101 nesting: Some(NestingPermissions {102 token_owner: false,103 collection_admin: false,104 restricted: None,105 #[cfg(feature = "runtime-benchmarks")]106 permissive: true,107 }),108 mint_mode: Some(true),109 ..Default::default()110 }),111 ..Default::default()112 },113 )114 .and_then(CollectionHandle::try_get)115 .map(cast)116}117fn create_collection<T: Config>(118 owner: T::CrossAccountId,119) -> Result<CollectionHandle<T>, DispatchError> {120 create_collection_raw(121 owner,122 CollectionMode::NFT,123 |owner: T::CrossAccountId, data| {124 <Pallet<T>>::init_collection(owner.clone(), Some(owner), data)125 },126 |h| h,127 )128}129130131132133134135136137138139140141142#[macro_export]143macro_rules! bench_init {144 ($name:ident: sub $(($id:expr))?; $($rest:tt)*) => {145 let $name: T::AccountId = account(stringify!($name), 0 $(+ $id)?, SEED);146 bench_init!($($rest)*);147 };148 ($name:ident: collection($owner:ident); $($rest:tt)*) => {149 let $name = create_collection::<T>(T::CrossAccountId::from_sub($owner.clone()))?;150 bench_init!($($rest)*);151 };152 ($name:ident: cross; $($rest:tt)*) => {153 let $name = T::CrossAccountId::from_sub($name);154 bench_init!($($rest)*);155 };156 ($name:ident: cross_sub $(($id:expr))?; $($rest:tt)*) => {157 let account: T::AccountId = account(stringify!($name), 0 $(+ $id)?, SEED);158 let $name = T::CrossAccountId::from_sub(account);159 bench_init!($($rest)*);160 };161 ($name:ident: cross_from_sub; $($rest:tt)*) => {162 let $name = T::CrossAccountId::from_sub($name);163 bench_init!($($rest)*);164 };165 ($name:ident: cross_from_sub($from:ident); $($rest:tt)*) => {166 let $name = T::CrossAccountId::from_sub($from);167 bench_init!($($rest)*);168 };169 () => {}170}171172#[benchmarks]173mod benchmarks {174 use super::*;175176 #[benchmark]177 fn set_collection_properties(178 b: Linear<0, MAX_PROPERTIES_PER_ITEM>,179 ) -> Result<(), BenchmarkError> {180 bench_init! {181 owner: sub; collection: collection(owner);182 owner: cross_from_sub;183 };184 let props = (0..b)185 .map(|p| Property {186 key: property_key(p as usize),187 value: property_value(),188 })189 .collect::<Vec<_>>();190191 #[block]192 {193 <Pallet<T>>::set_collection_properties(&collection, &owner, props.into_iter())?;194 }195196 Ok(())197 }198199 #[benchmark]200 fn check_accesslist() -> Result<(), BenchmarkError> {201 bench_init! {202 owner: sub; collection: collection(owner);203 sender: cross_from_sub(owner);204 };205206 let mut collection_handle = <CollectionHandle<T>>::try_get(collection.id)?;207 <Pallet<T>>::update_permissions(208 &sender,209 &mut collection_handle,210 CollectionPermissions {211 access: Some(AccessMode::AllowList),212 ..Default::default()213 },214 )?;215216 <Pallet<T>>::toggle_allowlist(&collection, &sender, &sender, true)?;217218 assert_eq!(219 collection_handle.permissions.access(),220 AccessMode::AllowList221 );222223 #[block]224 {225 collection_handle.check_allowlist(&sender)?;226 }227228 Ok(())229 }230231 #[benchmark]232 fn property_writer_load_collection_info() -> Result<(), BenchmarkError> {233 bench_init! {234 owner: sub; collection: collection(owner);235 sender: sub;236 sender: cross_from_sub(sender);237 };238239 #[block]240 {241 <BenchmarkPropertyWriter<T>>::load_collection_info(&&collection, &sender);242 }243244 Ok(())245 }246}