1use sp_std::vec;23use frame_benchmarking::{benchmarks, account};4use frame_system::RawOrigin;5use frame_support::{6 traits::{Currency, Get},7 BoundedVec,8};9use sp_runtime::Permill;1011use up_data_structs::*;1213use super::*;1415const SEED: u32 = 1;1617fn create_data<S: Get<u32>>() -> BoundedVec<u8, S> {18 vec![b'A'; S::get() as usize].try_into().expect("size == S")19}2021fn create_u32_array<S: Get<u32>>() -> BoundedVec<u32, S> {22 vec![0; S::get() as usize].try_into().expect("size == S")23}2425fn create_basic_resource() -> RmrkBasicResource {26 RmrkBasicResource {27 src: Some(create_data()),28 metadata: Some(create_data()),29 license: Some(create_data()),30 thumb: Some(create_data()),31 }32}3334fn create_composable_resource() -> RmrkComposableResource {35 RmrkComposableResource {36 parts: create_u32_array(),37 base: 100,38 src: Some(create_data()),39 metadata: Some(create_data()),40 license: Some(create_data()),41 thumb: Some(create_data()),42 }43}4445fn create_slot_resource() -> RmrkSlotResource {46 RmrkSlotResource {47 base: 100,48 slot: 200,49 src: Some(create_data()),50 metadata: Some(create_data()),51 license: Some(create_data()),52 thumb: Some(create_data()),53 }54}5556fn create_max_resource_types_array<S: Get<u32>>(num: usize) -> BoundedVec<RmrkResourceTypes, S> {57 vec![RmrkResourceTypes::Composable(create_composable_resource()); num]58 .try_into()59 .expect("num <= S")60}6162fn create_max_collection<T: Config>(owner: &T::AccountId) -> DispatchResult {63 <T as pallet_common::Config>::Currency::deposit_creating(64 owner,65 T::CollectionCreationPrice::get(),66 );6768 let metadata = create_data();69 let max = None;70 let symbol = create_data();7172 <Pallet<T>>::create_collection(73 RawOrigin::Signed(owner.clone()).into(),74 metadata,75 max,76 symbol,77 )78}7980fn create_nft<T: Config>(owner: &T::AccountId, collection_id: RmrkCollectionId) -> DispatchResult {81 let royalty_recipient = Some(owner.clone());82 let royalty_amount = Some(Permill::from_percent(25));83 let metadata = create_data();84 let transferable = true;8586 <Pallet<T>>::mint_nft(87 RawOrigin::Signed(owner.clone()).into(),88 None,89 collection_id,90 royalty_recipient,91 royalty_amount,92 metadata,93 transferable,94 None,95 )96}9798struct NftBuilder {99 collection_id: RmrkCollectionId,100 current_nft_id: RmrkNftId,101}102103impl NftBuilder {104 fn new(collection_id: RmrkCollectionId) -> Self {105 Self {106 collection_id,107 current_nft_id: 0,108 }109 }110111 fn build<T: Config>(&mut self, owner: &T::AccountId) -> Result<RmrkNftId, DispatchError> {112 create_nft::<T>(owner, self.collection_id)?;113 self.current_nft_id += 1;114115 Ok(self.current_nft_id)116 }117118 fn build_tower<T: Config>(119 &mut self,120 owner: &T::AccountId,121 height: u32,122 ) -> Result<(RmrkNftId, RmrkNftId), DispatchError> {123 self.build::<T>(owner)?;124125 let root_nft_id = self.current_nft_id;126 let mut prev_nft_id = root_nft_id;127128 for _ in 0..height {129 self.build::<T>(owner)?;130131 let new_owner =132 <RmrkAccountIdOrCollectionNftTuple<T::AccountId>>::CollectionAndNftTuple(133 self.collection_id,134 prev_nft_id,135 );136137 <Pallet<T>>::send(138 RawOrigin::Signed(owner.clone()).into(),139 self.collection_id,140 self.current_nft_id,141 new_owner,142 )?;143144 prev_nft_id = self.current_nft_id;145 }146147 let deepest_nft_id = self.current_nft_id;148149 Ok((root_nft_id, deepest_nft_id))150 }151152 fn build_wide_tree<T: Config>(153 &mut self,154 owner: &T::AccountId,155 width: u32,156 ) -> Result<RmrkNftId, DispatchError> {157 self.build::<T>(owner)?;158159 let root_nft_id = self.current_nft_id;160161 let root_owner = <RmrkAccountIdOrCollectionNftTuple<T::AccountId>>::CollectionAndNftTuple(162 self.collection_id,163 root_nft_id,164 );165166 for _ in 0..width {167 self.build::<T>(owner)?;168169 <Pallet<T>>::send(170 RawOrigin::Signed(owner.clone()).into(),171 self.collection_id,172 self.current_nft_id,173 root_owner.clone(),174 )?;175 }176177 Ok(root_nft_id)178 }179}180181benchmarks! {182 create_collection {183 let caller = account("caller", 0, SEED);184 <T as pallet_common::Config>::Currency::deposit_creating(&caller, T::CollectionCreationPrice::get());185 let metadata = create_data();186 let max = None;187 let symbol = create_data();188 }: _(RawOrigin::Signed(caller), metadata, max, symbol)189190 destroy_collection {191 let caller = account("caller", 0, SEED);192193 create_max_collection::<T>(&caller)?;194 let collection_id = 0;195 }: _(RawOrigin::Signed(caller), collection_id)196197 change_collection_issuer {198 let caller: T::AccountId = account("caller", 0, SEED);199200 create_max_collection::<T>(&caller)?;201 let collection_id = 0;202203 let new_owner: T::AccountId = account("new_owner", 0, SEED);204205 let new_owner_source = T::Lookup::unlookup(new_owner);206 }: _(RawOrigin::Signed(caller), collection_id, new_owner_source)207208 lock_collection {209 let caller: T::AccountId = account("caller", 0, SEED);210211 create_max_collection::<T>(&caller)?;212 let collection_id = 0;213 }: _(RawOrigin::Signed(caller), collection_id)214215 mint_nft {216 let b in 0..100;217218 let caller: T::AccountId = account("caller", 0, SEED);219220 create_max_collection::<T>(&caller)?;221 let collection_id = 0;222 let owner = caller.clone();223224 let royalty_recipient = Some(caller.clone());225 let royalty_amount = Some(Permill::from_percent(25));226 let metadata = create_data();227 let transferable = true;228 }: _(229 RawOrigin::Signed(caller),230 None,231 collection_id,232 royalty_recipient,233 royalty_amount,234 metadata,235 transferable,236 Some(create_max_resource_types_array(b as usize))237 )238239 burn_nft {240 let b in 0..200;241242 let caller: T::AccountId = account("caller", 0, SEED);243 create_max_collection::<T>(&caller)?;244 let collection_id = 0;245246 let mut nft_builder = NftBuilder::new(collection_id);247 let root_nft_id = nft_builder.build_wide_tree::<T>(&caller, b)?;248 let max_burns = b + 1;249 }: _(250 RawOrigin::Signed(caller),251 collection_id,252 root_nft_id,253 max_burns254 )255256 send {257 let caller: T::AccountId = account("caller", 0, SEED);258 create_max_collection::<T>(&caller)?;259 let collection_id = 0;260261 let mut nft_builder = NftBuilder::new(collection_id);262 let src_nft_id = nft_builder.build::<T>(&caller)?;263 let (_, target_nft_id) = nft_builder.build_tower::<T>(&caller, NESTING_BUDGET - 2)?;264 }: _(265 RawOrigin::Signed(caller),266 collection_id,267 src_nft_id,268 <RmrkAccountIdOrCollectionNftTuple<T::AccountId>>::CollectionAndNftTuple(collection_id, target_nft_id)269 )270271 accept_nft {272 let caller: T::AccountId = account("caller", 0, SEED);273 let sender: T::AccountId = account("sender", 0, SEED);274275 create_max_collection::<T>(&sender)?;276 let src_collection_id = 0;277278 create_max_collection::<T>(&caller)?;279 let target_collection_id = 1;280281 let mut src_nft_builder = NftBuilder::new(src_collection_id);282 let src_nft_id = src_nft_builder.build::<T>(&sender)?;283284 let mut target_nft_builder = NftBuilder::new(target_collection_id);285 let fake_target_nft_id = target_nft_builder.build::<T>(&caller)?;286 let (_, target_nft_id) = target_nft_builder.build_tower::<T>(&caller, NESTING_BUDGET - 1)?;287288 let new_owner = <RmrkAccountIdOrCollectionNftTuple<T::AccountId>>::CollectionAndNftTuple(289 target_collection_id,290 fake_target_nft_id291 );292293 let actual_new_owner = <RmrkAccountIdOrCollectionNftTuple<T::AccountId>>::CollectionAndNftTuple(294 target_collection_id,295 target_nft_id296 );297298 <Pallet<T>>::send(299 RawOrigin::Signed(sender.clone()).into(),300 src_collection_id,301 src_nft_id,302 new_owner,303 )?;304 }: _(305 RawOrigin::Signed(caller),306 src_collection_id,307 src_nft_id,308 actual_new_owner309 )310311 reject_nft {312 let caller: T::AccountId = account("caller", 0, SEED);313 let sender: T::AccountId = account("sender", 0, SEED);314315 create_max_collection::<T>(&sender)?;316 let src_collection_id = 0;317318 create_max_collection::<T>(&caller)?;319 let target_collection_id = 1;320321 let mut src_nft_builder = NftBuilder::new(src_collection_id);322 let (src_root_nft_id, _) = src_nft_builder.build_tower::<T>(&sender, NESTING_BUDGET - 1)?;323324 let mut target_nft_builder = NftBuilder::new(target_collection_id);325 let target_nft_id = target_nft_builder.build::<T>(&caller)?;326327 let new_owner = <RmrkAccountIdOrCollectionNftTuple<T::AccountId>>::CollectionAndNftTuple(328 target_collection_id,329 target_nft_id330 );331332 <Pallet<T>>::send(333 RawOrigin::Signed(sender.clone()).into(),334 src_collection_id,335 src_root_nft_id,336 new_owner,337 )?;338 }: _(339 RawOrigin::Signed(caller),340 src_collection_id,341 src_root_nft_id342 )343344 set_property {345 let caller: T::AccountId = account("caller", 0, SEED);346 create_max_collection::<T>(&caller)?;347 let collection_id = 0;348349 let mut nft_builder = NftBuilder::new(collection_id);350 let (_, nft_id) = nft_builder.build_tower::<T>(&caller, NESTING_BUDGET - 1)?;351352 let key = create_data();353 let value = create_data();354 }: _(355 RawOrigin::Signed(caller),356 collection_id,357 Some(nft_id),358 key,359 value360 )361362 set_priority {363 let caller: T::AccountId = account("caller", 0, SEED);364 create_max_collection::<T>(&caller)?;365 let collection_id = 0;366367 let mut nft_builder = NftBuilder::new(collection_id);368 let (_, nft_id) = nft_builder.build_tower::<T>(&caller, NESTING_BUDGET - 1)?;369 let priorities = create_u32_array();370 }: _(371 RawOrigin::Signed(caller),372 collection_id,373 nft_id,374 priorities375 )376377 add_basic_resource {378 let caller: T::AccountId = account("caller", 0, SEED);379380 create_max_collection::<T>(&caller)?;381 let collection_id = 0;382383 let mut nft_builder = NftBuilder::new(collection_id);384 let (_, nft_id) = nft_builder.build_tower::<T>(&caller, NESTING_BUDGET - 1)?;385 let resource = create_basic_resource();386 }: _(387 RawOrigin::Signed(caller),388 collection_id,389 nft_id,390 resource391 )392393 add_composable_resource {394 let caller: T::AccountId = account("caller", 0, SEED);395396 create_max_collection::<T>(&caller)?;397 let collection_id = 0;398399 let mut nft_builder = NftBuilder::new(collection_id);400 let (_, nft_id) = nft_builder.build_tower::<T>(&caller, NESTING_BUDGET - 1)?;401 let resource = create_composable_resource();402 }: _(403 RawOrigin::Signed(caller),404 collection_id,405 nft_id,406 resource407 )408409 add_slot_resource {410 let caller: T::AccountId = account("caller", 0, SEED);411412 create_max_collection::<T>(&caller)?;413 let collection_id = 0;414415 let mut nft_builder = NftBuilder::new(collection_id);416 let (_, nft_id) = nft_builder.build_tower::<T>(&caller, NESTING_BUDGET - 1)?;417 let resource = create_slot_resource();418 }: _(419 RawOrigin::Signed(caller),420 collection_id,421 nft_id,422 resource423 )424425 remove_resource {426 let caller: T::AccountId = account("caller", 0, SEED);427428 create_max_collection::<T>(&caller)?;429 let collection_id = 0;430431 let mut nft_builder = NftBuilder::new(collection_id);432 let (_, nft_id) = nft_builder.build_tower::<T>(&caller, NESTING_BUDGET - 1)?;433 let resource = create_basic_resource();434435 <Pallet<T>>::add_basic_resource(436 RawOrigin::Signed(caller.clone()).into(),437 collection_id,438 nft_id,439 resource440 )?;441442 let resource_id = 0;443 }: _(444 RawOrigin::Signed(caller),445 collection_id,446 nft_id,447 resource_id448 )449450 accept_resource {451 let caller: T::AccountId = account("caller", 0, SEED);452 let admin: T::AccountId = account("admin", 0, SEED);453454 create_max_collection::<T>(&admin)?;455 let collection_id = 0;456457 let mut nft_builder = NftBuilder::new(collection_id);458 let root_nft_id = 1;459 let (_, nested_nft_id) = nft_builder.build_tower::<T>(&admin, NESTING_BUDGET - 1)?;460 let resource = create_basic_resource();461462 let new_owner = <RmrkAccountIdOrCollectionNftTuple<T::AccountId>>::AccountId(caller.clone());463464 <Pallet<T>>::send(465 RawOrigin::Signed(admin.clone()).into(),466 collection_id,467 root_nft_id,468 new_owner,469 )?;470471 <Pallet<T>>::add_basic_resource(472 RawOrigin::Signed(admin.clone()).into(),473 collection_id,474 nested_nft_id,475 resource476 )?;477478 let resource_id = 0;479 }: _(480 RawOrigin::Signed(caller),481 collection_id,482 nested_nft_id,483 resource_id484 )485486 accept_resource_removal {487 let caller: T::AccountId = account("caller", 0, SEED);488 let admin: T::AccountId = account("admin", 0, SEED);489490 create_max_collection::<T>(&admin)?;491 let collection_id = 0;492493 let mut nft_builder = NftBuilder::new(collection_id);494 let root_nft_id = 1;495 let (_, nested_nft_id) = nft_builder.build_tower::<T>(&admin, NESTING_BUDGET - 1)?;496 let resource = create_basic_resource();497498 <Pallet<T>>::add_basic_resource(499 RawOrigin::Signed(admin.clone()).into(),500 collection_id,501 nested_nft_id,502 resource503 )?;504505 let resource_id = 0;506507 let new_owner = <RmrkAccountIdOrCollectionNftTuple<T::AccountId>>::AccountId(caller.clone());508509 <Pallet<T>>::send(510 RawOrigin::Signed(admin.clone()).into(),511 collection_id,512 root_nft_id,513 new_owner,514 )?;515516 <Pallet<T>>::remove_resource(517 RawOrigin::Signed(admin).into(),518 collection_id,519 nested_nft_id,520 resource_id521 )?;522 }: _(523 RawOrigin::Signed(caller),524 collection_id,525 nested_nft_id,526 resource_id527 )528}