Skip to content

Commit

Permalink
Merge branch 'main' into bench-equip
Browse files Browse the repository at this point in the history
  • Loading branch information
Szegoo authored Jan 12, 2023
2 parents da927e3 + 1e96507 commit a50ed73
Show file tree
Hide file tree
Showing 12 changed files with 551 additions and 105 deletions.
104 changes: 86 additions & 18 deletions pallets/rmrk-core/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,22 @@ fn mint_and_send_to_parent<T: Config>(owner: T::AccountId, collection_id: T::Col
}
}

// premint nfts & make deep nested chain of nfts ( send child to the specified parent )
fn mint_and_send_to<T: Config>(
owner: T::AccountId,
collection_id: T::CollectionId,
n: u32,
parent: u32,
) {
for i in (parent + 1)..(n + parent) {
let id = mint_test_nft::<T>(owner.clone(), None, collection_id, i);
let parent_nft_id = T::Helper::item(i.saturating_sub(1));
let new_owner =
AccountIdOrCollectionNftTuple::CollectionAndNftTuple(collection_id, parent_nft_id);
send_test_nft::<T>(owner.clone(), collection_id, id, new_owner);
}
}

// Send nft to Account or to another nft
fn send_test_nft<T: Config>(
owner: T::AccountId,
Expand All @@ -134,7 +150,7 @@ fn prepare_resource<T: Config>(
let bob = funded_account::<T>("bob", 0);
let collection_index = 1;
let collection_id = create_test_collection::<T>(alice.clone(), collection_index);
let nft_id = mint_test_nft::<T>(alice.clone(), Some(bob.clone()), collection_id, 1);
let nft_id = mint_test_nft::<T>(alice.clone(), Some(bob.clone()), collection_id, 0);
let resource_id = 0;
(alice, bob, collection_id, nft_id, resource_id)
}
Expand Down Expand Up @@ -173,12 +189,17 @@ benchmarks! {
}

mint_nft_directly_to_nft {
let n in 1 .. (T::NestingBudget::get() - 1);

let owner: T::AccountId = whitelisted_caller();
let collection_index = 1;
let collection_id = create_test_collection::<T>(owner.clone(), collection_index);
let nft_id = mint_test_nft::<T>(owner.clone(), None, collection_id, 1);
mint_test_nft::<T>(owner.clone(), None, collection_id, 0);

mint_and_send_to_parent::<T>(owner.clone(), collection_id, n);
let nft_id = T::Helper::item(n-1);

let nft_child_id: <T as pallet_uniques::Config>::ItemId = T::Helper::item(2);
let nft_child_id: <T as pallet_uniques::Config>::ItemId = T::Helper::item(n);
let owner_tuple = AccountIdOrCollectionNftTuple::CollectionAndNftTuple(collection_id, nft_id);
let nft_owner_tuple = (collection_id, nft_id);
let royalty_recipient = owner.clone();
Expand All @@ -205,10 +226,15 @@ benchmarks! {
}

send_to_account {
let n in 1 .. T::NestingBudget::get();
let owner: T::AccountId = whitelisted_caller();
let collection_index = 1;
let collection_id = create_test_collection::<T>(owner.clone(), collection_index);
let nft_id = mint_test_nft::<T>(owner.clone(), None, collection_id, 42);
mint_test_nft::<T>(owner.clone(), None, collection_id, 0);

mint_and_send_to_parent::<T>(owner.clone(), collection_id, n);
let nft_id = T::Helper::item(n-1);

let bob = funded_account::<T>("bob", 0);
let new_owner = AccountIdOrCollectionNftTuple::AccountId(bob);

Expand All @@ -224,17 +250,20 @@ benchmarks! {
}

send_to_nft {
let n in 1 .. T::NestingBudget::get();
let alice: T::AccountId = whitelisted_caller();
let collection_index = 1;
let collection_id = create_test_collection::<T>(alice.clone(), collection_index);
let nft_id1 = mint_test_nft::<T>(alice.clone(), None, collection_id, 1);
let child_nft = mint_test_nft::<T>(alice.clone(), None, collection_id, 2);
let nft_id1 = mint_test_nft::<T>(alice.clone(), None, collection_id, 0);
mint_test_nft::<T>(alice.clone(), None, collection_id, 1);
mint_and_send_to::<T>(alice.clone(), collection_id, n, 1);
let child_nft = T::Helper::item(n);
// Alice sends NFT (0,1) to Bob's account
let bob = funded_account::<T>("bob", 0);
let new_owner = AccountIdOrCollectionNftTuple::AccountId(bob);
send_test_nft::<T>(alice.clone(), collection_id, nft_id1, new_owner.clone());

// Alice sends child NFT (0,2) to parent NFT (0,1)
// Alice sends child NFT (0,n) to parent NFT (0,1)
let parent_nft = AccountIdOrCollectionNftTuple::CollectionAndNftTuple(collection_id, nft_id1);

}: send(RawOrigin::Signed(alice.clone()), collection_id, child_nft, parent_nft.clone())
Expand Down Expand Up @@ -262,11 +291,16 @@ benchmarks! {
}

accept_nft {
let n in 1 .. T::NestingBudget::get();

let alice: T::AccountId = whitelisted_caller();
let collection_index = 1;
let collection_id = create_test_collection::<T>(alice.clone(), collection_index);
let nft_id1 = mint_test_nft::<T>(alice.clone(), None, collection_id, 1);
let child_nft = mint_test_nft::<T>(alice.clone(), None, collection_id, 2);
let nft_id1 = mint_test_nft::<T>(alice.clone(), None, collection_id, 0);
mint_test_nft::<T>(alice.clone(), None, collection_id, 1);
mint_and_send_to::<T>(alice.clone(), collection_id, n, 1);
let child_nft = T::Helper::item(n);

// Alice sends NFT (0,1) to Bob's account
let bob = funded_account::<T>("bob", 0);
let new_owner = AccountIdOrCollectionNftTuple::AccountId(bob.clone());
Expand Down Expand Up @@ -373,16 +407,25 @@ benchmarks! {
}

add_basic_resource{
let (alice, _, collection_id, nft_id, resource_id) = prepare_resource::<T>();
let basic_resource = BasicResource{ metadata: stbd::<T> ("basic test metadata") };
let (alice, _, collection_id, _, resource_id) = prepare_resource::<T>();

let n in 1 .. T::NestingBudget::get();
mint_and_send_to_parent::<T>(alice.clone(), collection_id, n);
let nft_id = T::Helper::item(n-1);

let basic_resource = BasicResource{ metadata: stbd::<T> ("basic test metadata") };
}: _(RawOrigin::Signed(alice.clone()), collection_id, nft_id, basic_resource, resource_id)
verify {
assert_last_event::<T>(Event::ResourceAdded { nft_id, resource_id, collection_id }.into());
}

add_composable_resource{
let (alice, _, collection_id, nft_id, resource_id) = prepare_resource::<T>();
let (alice, _, collection_id, _, resource_id) = prepare_resource::<T>();

let n in 1 .. T::NestingBudget::get();
mint_and_send_to_parent::<T>(alice.clone(), collection_id, n);
let nft_id = T::Helper::item(n-1);

let composable_resource = ComposableResource {
parts: vec![0, 1].try_into().unwrap(), // BoundedVec of Parts
base: 0, // BaseID
Expand All @@ -396,7 +439,12 @@ benchmarks! {
}

add_slot_resource{
let (alice, _, collection_id, nft_id, resource_id) = prepare_resource::<T>();
let (alice, _, collection_id, _, resource_id) = prepare_resource::<T>();

let n in 1 .. T::NestingBudget::get();
mint_and_send_to_parent::<T>(alice.clone(), collection_id, n);
let nft_id = T::Helper::item(n-1);

let slot_resource = SlotResource {
base: 0, // BaseID
metadata: Some(stbd::<T> ("basic test metadata")),
Expand All @@ -409,7 +457,12 @@ benchmarks! {
}

accept_resource{
let (alice, bob, collection_id, nft_id, resource_id) = prepare_resource::<T>();
let (alice, bob, collection_id, _, resource_id) = prepare_resource::<T>();

let n in 1 .. T::NestingBudget::get();
mint_and_send_to_parent::<T>(alice.clone(), collection_id, n);
let nft_id = T::Helper::item(n-1);

let basic_resource = BasicResource{ metadata: stbd::<T> ("basic test metadata") };
// Alice is collection issuer and she adds resource to bob's nft
let _ = RmrkCore::<T>::add_basic_resource(RawOrigin::Signed(alice.clone()).into(), collection_id, nft_id, basic_resource, resource_id);
Expand All @@ -420,7 +473,12 @@ benchmarks! {
}

remove_resource{
let (alice, bob, collection_id, nft_id, resource_id) = prepare_resource::<T>();
let (alice, bob, collection_id, _, resource_id) = prepare_resource::<T>();

let n in 1 .. T::NestingBudget::get();
mint_and_send_to_parent::<T>(alice.clone(), collection_id, n);
let nft_id = T::Helper::item(n-1);

let basic_resource = BasicResource{ metadata: stbd::<T> ("basic test metadata") };
// Alice is collection issuer and she adds resource to bob's nft
let _ = RmrkCore::<T>::add_basic_resource(RawOrigin::Signed(alice.clone()).into(), collection_id, nft_id, basic_resource, resource_id);
Expand All @@ -433,7 +491,12 @@ benchmarks! {
}

accept_resource_removal{
let (alice, bob, collection_id, nft_id, resource_id) = prepare_resource::<T>();
let (alice, bob, collection_id, _, resource_id) = prepare_resource::<T>();

let n in 1 .. T::NestingBudget::get();
mint_and_send_to_parent::<T>(alice.clone(), collection_id, n);
let nft_id = T::Helper::item(n-1);

let basic_resource = BasicResource{ metadata: stbd::<T> ("basic test metadata") };
// Alice is collection issuer and she adds resource to bob's nft
let _ = RmrkCore::<T>::add_basic_resource(RawOrigin::Signed(alice.clone()).into(), collection_id, nft_id, basic_resource, resource_id);
Expand All @@ -449,10 +512,15 @@ benchmarks! {

set_priority{
let n in 1 .. T::MaxPriorities::get();
let (alice, bob, collection_id, nft_id, resource_id) = prepare_resource::<T>();
let k in 1 .. T::NestingBudget::get();
let (alice, bob, collection_id, _, resource_id) = prepare_resource::<T>();

mint_and_send_to_parent::<T>(alice.clone(), collection_id, k);
let nft_id = T::Helper::item(k-1);

let basic_resource = BasicResource{ metadata: stbd::<T> ("basic test metadata") };
let mut priorities: BoundedVec<ResourceId, T::MaxPriorities> = vec![].try_into().unwrap();
for resource_id in 1.. n{
for resource_id in 1 .. n{
let _ = priorities.try_push(resource_id);
}

Expand Down
4 changes: 2 additions & 2 deletions pallets/rmrk-core/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl<T: Config>
priority_index += 1;
}
Self::deposit_event(Event::PrioritySet { collection_id, nft_id });
Ok(Some(<T as pallet::Config>::WeightInfo::set_priority(priority_index)).into())
Ok(Some(<T as pallet::Config>::WeightInfo::set_priority(priority_index, T::NestingBudget::get())).into())
}
}

Expand Down Expand Up @@ -597,7 +597,7 @@ impl<T: Config>
collection_id: T::CollectionId,
nft_id: T::ItemId,
new_owner: AccountIdOrCollectionNftTuple<T::AccountId, T::CollectionId, T::ItemId>,
) -> sp_std::result::Result<(T::AccountId, bool), DispatchError> {
) -> Result<(T::AccountId, bool), DispatchError> {
// Get current owner for child removal later
let parent = pallet_uniques::Pallet::<T>::owner(collection_id, nft_id);
// Check if parent returns None which indicates the NFT is not available
Expand Down
25 changes: 13 additions & 12 deletions pallets/rmrk-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,8 +483,8 @@ pub mod pallet {
/// - `recipient`: Receiver of the royalty
/// - `royalty`: Permillage reward from each trade for the Recipient
/// - `metadata`: Arbitrary data about an nft, e.g. IPFS hash
#[pallet::call_index(1)]
#[pallet::weight(<T as pallet::Config>::WeightInfo::mint_nft_directly_to_nft())]
#[pallet::call_index(1)]
#[pallet::weight(<T as pallet::Config>::WeightInfo::mint_nft_directly_to_nft(T::NestingBudget::get()))]
#[transactional]
pub fn mint_nft_directly_to_nft(
origin: OriginFor<T>,
Expand Down Expand Up @@ -582,8 +582,8 @@ pub mod pallet {
/// - `nft_id`: nft id of the nft to be transferred
/// - `new_owner`: new owner of the nft which can be either an account or a NFT
#[pallet::call_index(5)]
#[pallet::weight(<T as pallet::Config>::WeightInfo::send_to_account().max(<T as
pallet::Config>::WeightInfo::send_to_nft()))]
#[pallet::weight(<T as pallet::Config>::WeightInfo::send_to_account(T::NestingBudget::get()).max(<T as
pallet::Config>::WeightInfo::send_to_nft(T::NestingBudget::get())))]
#[transactional]
pub fn send(
origin: OriginFor<T>,
Expand All @@ -598,6 +598,7 @@ pub mod pallet {

Ok(())
}

/// Accepts an NFT sent from another account to self or owned NFT
///
/// Parameters:
Expand All @@ -607,7 +608,7 @@ pub mod pallet {
/// - `new_owner`: either origin's account ID or origin-owned NFT, whichever the NFT was
/// sent to
#[pallet::call_index(6)]
#[pallet::weight(<T as pallet::Config>::WeightInfo::accept_nft())]
#[pallet::weight(<T as pallet::Config>::WeightInfo::accept_nft(T::NestingBudget::get()))]
#[transactional]
pub fn accept_nft(
origin: OriginFor<T>,
Expand Down Expand Up @@ -736,7 +737,7 @@ pub mod pallet {

/// Create basic resource
#[pallet::call_index(11)]
#[pallet::weight(<T as pallet::Config>::WeightInfo::add_basic_resource())]
#[pallet::weight(<T as pallet::Config>::WeightInfo::add_basic_resource(T::NestingBudget::get()))]
#[transactional]
pub fn add_basic_resource(
origin: OriginFor<T>,
Expand Down Expand Up @@ -774,7 +775,7 @@ pub mod pallet {

/// Create composable resource
#[pallet::call_index(12)]
#[pallet::weight(<T as pallet::Config>::WeightInfo::add_composable_resource())]
#[pallet::weight(<T as pallet::Config>::WeightInfo::add_composable_resource(T::NestingBudget::get()))]
#[transactional]
pub fn add_composable_resource(
origin: OriginFor<T>,
Expand Down Expand Up @@ -813,7 +814,7 @@ pub mod pallet {

/// Create slot resource
#[pallet::call_index(13)]
#[pallet::weight(<T as pallet::Config>::WeightInfo::add_slot_resource())]
#[pallet::weight(<T as pallet::Config>::WeightInfo::add_slot_resource(T::NestingBudget::get()))]
#[transactional]
pub fn add_slot_resource(
origin: OriginFor<T>,
Expand Down Expand Up @@ -868,7 +869,7 @@ pub mod pallet {

/// accept the addition of a new resource to an existing NFT
#[pallet::call_index(15)]
#[pallet::weight(<T as pallet::Config>::WeightInfo::accept_resource())]
#[pallet::weight(<T as pallet::Config>::WeightInfo::accept_resource(T::NestingBudget::get()))]
#[transactional]
pub fn accept_resource(
origin: OriginFor<T>,
Expand All @@ -890,7 +891,7 @@ pub mod pallet {

/// remove resource
#[pallet::call_index(16)]
#[pallet::weight(<T as pallet::Config>::WeightInfo::remove_resource())]
#[pallet::weight(<T as pallet::Config>::WeightInfo::remove_resource(T::NestingBudget::get()))]
#[transactional]
pub fn remove_resource(
origin: OriginFor<T>,
Expand All @@ -915,7 +916,7 @@ pub mod pallet {

/// accept the removal of a resource of an existing NFT
#[pallet::call_index(17)]
#[pallet::weight(<T as pallet::Config>::WeightInfo::accept_resource_removal())]
#[pallet::weight(<T as pallet::Config>::WeightInfo::accept_resource_removal(T::NestingBudget::get()))]
#[transactional]
pub fn accept_resource_removal(
origin: OriginFor<T>,
Expand All @@ -936,7 +937,7 @@ pub mod pallet {

/// set a different order of resource priority
#[pallet::call_index(18)]
#[pallet::weight(<T as pallet::Config>::WeightInfo::set_priority(T::MaxPriorities::get()))]
#[pallet::weight(<T as pallet::Config>::WeightInfo::set_priority(T::MaxPriorities::get(), T::NestingBudget::get()))]
pub fn set_priority(
origin: OriginFor<T>,
collection_id: T::CollectionId,
Expand Down
Loading

0 comments on commit a50ed73

Please sign in to comment.