Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

[Uniques V2] Refactor roles #12437

Merged
merged 78 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from 71 commits
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
97a7e30
Basics
jsidorenko Sep 21, 2022
a85608f
WIP: change the data format
jsidorenko Sep 23, 2022
6db840a
Refactor
jsidorenko Sep 24, 2022
fa3a2a7
Remove redundant new() method
jsidorenko Sep 24, 2022
472a753
Rename settings
jsidorenko Sep 24, 2022
2611dd4
Enable tests
jsidorenko Sep 24, 2022
52d7d38
Merge branch 'js/uniques-v2-main-branch' into js/uniques-v2-feature-f…
jsidorenko Sep 24, 2022
46a6b7e
Chore
jsidorenko Sep 24, 2022
5ff7048
Change params order
jsidorenko Sep 24, 2022
f7ddfed
Delete the config on collection removal
jsidorenko Sep 24, 2022
d0d1d60
Chore
jsidorenko Sep 24, 2022
7d4b31f
Remove redundant system features
jsidorenko Sep 24, 2022
dd84637
Rename force_item_status to force_collection_status
jsidorenko Sep 24, 2022
4920a0c
Update node runtime
jsidorenko Sep 26, 2022
7456e73
Chore
jsidorenko Sep 26, 2022
9ff45b8
Remove thaw_collection
jsidorenko Sep 26, 2022
c41a48c
Chore
jsidorenko Sep 26, 2022
fee3ade
Connect collection.is_frozen to config
jsidorenko Sep 26, 2022
a9ab6f8
Allow to lock the collection in a new way
jsidorenko Sep 26, 2022
254816d
Move free_holding into settings
jsidorenko Sep 26, 2022
5878197
Connect collection's metadata locker to feature flags
jsidorenko Sep 26, 2022
95e21fb
DRY
jsidorenko Sep 27, 2022
17b7c9e
Chore
jsidorenko Sep 27, 2022
dc6d2f0
Connect pallet level feature flags
jsidorenko Sep 27, 2022
cc64873
Prepare tests for the new changes
jsidorenko Sep 27, 2022
5153080
Implement Item settings
jsidorenko Sep 27, 2022
08a2048
Allow to lock the metadata or attributes of an item
jsidorenko Sep 27, 2022
1551714
Common -> Settings
jsidorenko Sep 27, 2022
8830a4c
Extract settings related code to a separate file
jsidorenko Sep 27, 2022
f8800df
Move feature flag checks inside the do_* methods
jsidorenko Sep 28, 2022
3ccf04e
Split settings.rs into parts
jsidorenko Sep 28, 2022
f83fb73
Extract repeated code into macro
jsidorenko Sep 28, 2022
df97cce
Extract macros into their own file
jsidorenko Sep 28, 2022
3bb6eb5
Chore
jsidorenko Sep 28, 2022
8563a31
Fix traits
jsidorenko Sep 29, 2022
09aca13
Fix traits
jsidorenko Sep 29, 2022
114e519
Test SystemFeatures
jsidorenko Sep 29, 2022
d6536fc
Fix benchmarks
jsidorenko Sep 29, 2022
09a4ed6
Add missing benchmark
jsidorenko Sep 29, 2022
d408b32
Fix node/runtime/lib.rs
jsidorenko Sep 29, 2022
fd86e41
".git/.scripts/bench-bot.sh" pallet dev pallet_nfts
Oct 3, 2022
c002f8b
Keep item's config on burn if it's not empty
jsidorenko Oct 5, 2022
b16435a
Merge branch 'js/uniques-v2-main-branch' into js/uniques-v2-feature-f…
jsidorenko Oct 5, 2022
d0ee824
Fix the merge artifacts
jsidorenko Oct 5, 2022
0eb568e
Fmt
jsidorenko Oct 5, 2022
998691e
Add SystemFeature::NoSwaps check
jsidorenko Oct 6, 2022
fbc7906
Refactor roles structure
jsidorenko Oct 6, 2022
b8c6f5e
Rename SystemFeatures to PalletFeatures
jsidorenko Oct 7, 2022
8bef776
Rename errors
jsidorenko Oct 7, 2022
ddaa884
Add docs
jsidorenko Oct 7, 2022
4c01fc5
Change error message
jsidorenko Oct 7, 2022
fa9d471
Rework pallet features
jsidorenko Oct 14, 2022
ad980de
Move macros
jsidorenko Oct 14, 2022
8532179
Change comments
jsidorenko Oct 14, 2022
fc09bbd
Fmt
jsidorenko Oct 14, 2022
e3f67ad
Refactor Incrementable
jsidorenko Oct 14, 2022
1966a58
Use pub(crate) for do_* functions
jsidorenko Oct 14, 2022
39c84b4
Update comments
jsidorenko Oct 14, 2022
71ff09e
Refactor freeze and lock functions
jsidorenko Oct 14, 2022
e3778ad
Rework Collection config and Item confg api
jsidorenko Oct 14, 2022
f84563d
Chore
jsidorenko Oct 14, 2022
5d5cc1c
Merge branch 'js/uniques-v2-main-branch' into js/uniques-v2-feature-f…
jsidorenko Oct 14, 2022
05b6d11
Make clippy happy
jsidorenko Oct 14, 2022
08ca688
Chore
jsidorenko Oct 15, 2022
1760e7e
Merge branch 'js/uniques-v2-feature-flags' into js/uniques-v2-roles-r…
jsidorenko Oct 17, 2022
26adcc9
Fix artifacts
jsidorenko Oct 17, 2022
0225479
Address comments
jsidorenko Oct 17, 2022
c944afc
Further refactoring
jsidorenko Oct 17, 2022
a665764
Add comments
jsidorenko Oct 17, 2022
2461bc0
Add tests for group_roles_by_account()
jsidorenko Oct 17, 2022
6608f87
Update frame/nfts/src/impl_nonfungibles.rs
jsidorenko Oct 18, 2022
e6c56d6
Add test
jsidorenko Oct 18, 2022
414bd8b
Replace Itertools group_by with a custom implementation
jsidorenko Oct 18, 2022
7ee0d2e
Merge branch 'js/uniques-v2-main-branch' into js/uniques-v2-roles-ref…
jsidorenko Oct 18, 2022
e688ad5
ItemsNotTransferable => ItemsNonTransferable
jsidorenko Oct 18, 2022
fc8d341
Update frame/nfts/src/features/roles.rs
jsidorenko Oct 19, 2022
2a3d11e
Address PR comments
jsidorenko Oct 19, 2022
e3ba2b2
Add missed comment
jsidorenko Oct 20, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ use pallet_grandpa::{
fg_primitives, AuthorityId as GrandpaId, AuthorityList as GrandpaAuthorityList,
};
use pallet_im_online::sr25519::AuthorityId as ImOnlineId;
use pallet_nfts::PalletFeatures;
use pallet_session::historical::{self as pallet_session_historical};
pub use pallet_transaction_payment::{CurrencyAdapter, Multiplier, TargetedFeeAdjustment};
use pallet_transaction_payment::{FeeDetails, RuntimeDispatchInfo};
Expand Down Expand Up @@ -1504,6 +1505,10 @@ impl pallet_uniques::Config for Runtime {
type Locker = ();
}

parameter_types! {
pub Features: PalletFeatures = PalletFeatures::all_enabled();
}

impl pallet_nfts::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type CollectionId = u32;
Expand All @@ -1521,6 +1526,7 @@ impl pallet_nfts::Config for Runtime {
type ApprovalsLimit = ApprovalsLimit;
type MaxTips = MaxTips;
type MaxDeadlineDuration = MaxDeadlineDuration;
type Features = Features;
type WeightInfo = pallet_nfts::weights::SubstrateWeight<Runtime>;
#[cfg(feature = "runtime-benchmarks")]
type Helper = ();
Expand Down
2 changes: 2 additions & 0 deletions frame/nfts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false }
enumflags2 = { version = "0.7.5" }
itertools = { version = "0.10.3" }
log = { version = "0.4.17", default-features = false }
scale-info = { version = "2.1.1", default-features = false, features = ["derive"] }
frame-benchmarking = { version = "4.0.0-dev", default-features = false, optional = true, path = "../benchmarking" }
Expand Down
107 changes: 52 additions & 55 deletions frame/nfts/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use frame_benchmarking::{
account, benchmarks_instance_pallet, whitelist_account, whitelisted_caller,
};
use frame_support::{
assert_ok,
dispatch::UnfilteredDispatchable,
traits::{EnsureOrigin, Get},
BoundedVec,
Expand All @@ -42,8 +43,11 @@ fn create_collection<T: Config<I>, I: 'static>(
let caller_lookup = T::Lookup::unlookup(caller.clone());
let collection = T::Helper::collection(0);
T::Currency::make_free_balance_be(&caller, DepositBalanceOf::<T, I>::max_value());
assert!(Nfts::<T, I>::force_create(SystemOrigin::Root.into(), caller_lookup.clone(), false,)
.is_ok());
assert_ok!(Nfts::<T, I>::force_create(
SystemOrigin::Root.into(),
caller_lookup.clone(),
CollectionConfig::all_settings_enabled()
));
(collection, caller, caller_lookup)
}

Expand All @@ -53,32 +57,30 @@ fn add_collection_metadata<T: Config<I>, I: 'static>() -> (T::AccountId, Account
whitelist_account!(caller);
}
let caller_lookup = T::Lookup::unlookup(caller.clone());
assert!(Nfts::<T, I>::set_collection_metadata(
assert_ok!(Nfts::<T, I>::set_collection_metadata(
SystemOrigin::Signed(caller.clone()).into(),
T::Helper::collection(0),
vec![0; T::StringLimit::get() as usize].try_into().unwrap(),
false,
)
.is_ok());
));
(caller, caller_lookup)
}

fn mint_item<T: Config<I>, I: 'static>(
index: u16,
) -> (T::ItemId, T::AccountId, AccountIdLookupOf<T>) {
let caller = Collection::<T, I>::get(T::Helper::collection(0)).unwrap().admin;
let caller = Collection::<T, I>::get(T::Helper::collection(0)).unwrap().owner;
if caller != whitelisted_caller() {
whitelist_account!(caller);
}
let caller_lookup = T::Lookup::unlookup(caller.clone());
let item = T::Helper::item(index);
assert!(Nfts::<T, I>::mint(
assert_ok!(Nfts::<T, I>::mint(
SystemOrigin::Signed(caller.clone()).into(),
T::Helper::collection(0),
item,
caller_lookup.clone(),
)
.is_ok());
ItemConfig::all_settings_enabled(),
));
(item, caller, caller_lookup)
}

Expand All @@ -90,14 +92,12 @@ fn add_item_metadata<T: Config<I>, I: 'static>(
whitelist_account!(caller);
}
let caller_lookup = T::Lookup::unlookup(caller.clone());
assert!(Nfts::<T, I>::set_metadata(
assert_ok!(Nfts::<T, I>::set_metadata(
SystemOrigin::Signed(caller.clone()).into(),
T::Helper::collection(0),
item,
vec![0; T::StringLimit::get() as usize].try_into().unwrap(),
false,
)
.is_ok());
));
(caller, caller_lookup)
}

Expand All @@ -110,14 +110,13 @@ fn add_item_attribute<T: Config<I>, I: 'static>(
}
let caller_lookup = T::Lookup::unlookup(caller.clone());
let key: BoundedVec<_, _> = vec![0; T::KeyLimit::get() as usize].try_into().unwrap();
assert!(Nfts::<T, I>::set_attribute(
assert_ok!(Nfts::<T, I>::set_attribute(
SystemOrigin::Signed(caller.clone()).into(),
T::Helper::collection(0),
Some(item),
key.clone(),
vec![0; T::ValueLimit::get() as usize].try_into().unwrap(),
)
.is_ok());
));
(key, caller, caller_lookup)
}

Expand All @@ -137,7 +136,7 @@ benchmarks_instance_pallet! {
whitelist_account!(caller);
let admin = T::Lookup::unlookup(caller.clone());
T::Currency::make_free_balance_be(&caller, DepositBalanceOf::<T, I>::max_value());
let call = Call::<T, I>::create { admin };
let call = Call::<T, I>::create { admin, config: CollectionConfig::all_settings_enabled() };
}: { call.dispatch_bypass_filter(origin)? }
verify {
assert_last_event::<T, I>(Event::Created { collection: T::Helper::collection(0), creator: caller.clone(), owner: caller }.into());
Expand All @@ -146,25 +145,19 @@ benchmarks_instance_pallet! {
force_create {
let caller: T::AccountId = whitelisted_caller();
let caller_lookup = T::Lookup::unlookup(caller.clone());
}: _(SystemOrigin::Root, caller_lookup, true)
}: _(SystemOrigin::Root, caller_lookup, CollectionConfig::all_settings_enabled())
verify {
assert_last_event::<T, I>(Event::ForceCreated { collection: T::Helper::collection(0), owner: caller }.into());
}

destroy {
let n in 0 .. 1_000;
let m in 0 .. 1_000;
let a in 0 .. 1_000;

let (collection, caller, caller_lookup) = create_collection::<T, I>();
add_collection_metadata::<T, I>();
for i in 0..n {
mint_item::<T, I>(i as u16);
}
for i in 0..m {
add_item_metadata::<T, I>(T::Helper::item(i as u16));
}
for i in 0..a {
add_item_attribute::<T, I>(T::Helper::item(i as u16));
}
let witness = Collection::<T, I>::get(collection).unwrap().destroy_witness();
Expand All @@ -176,7 +169,7 @@ benchmarks_instance_pallet! {
mint {
let (collection, caller, caller_lookup) = create_collection::<T, I>();
let item = T::Helper::item(0);
}: _(SystemOrigin::Signed(caller.clone()), collection, item, caller_lookup)
}: _(SystemOrigin::Signed(caller.clone()), collection, item, caller_lookup, ItemConfig::all_settings_enabled())
verify {
assert_last_event::<T, I>(Event::Issued { collection, item, owner: caller }.into());
}
Expand Down Expand Up @@ -204,56 +197,51 @@ benchmarks_instance_pallet! {
let i in 0 .. 5_000;
let (collection, caller, caller_lookup) = create_collection::<T, I>();
let items = (0..i).map(|x| mint_item::<T, I>(x as u16).0).collect::<Vec<_>>();
Nfts::<T, I>::force_item_status(
Nfts::<T, I>::force_collection_status(
SystemOrigin::Root.into(),
collection,
caller_lookup.clone(),
caller_lookup.clone(),
caller_lookup.clone(),
caller_lookup,
true,
false,
CollectionConfig(CollectionSetting::RequiredDeposit.into()),
)?;
}: _(SystemOrigin::Signed(caller.clone()), collection, items.clone())
verify {
assert_last_event::<T, I>(Event::Redeposited { collection, successful_items: items }.into());
}

freeze {
lock_item_transfer {
let (collection, caller, caller_lookup) = create_collection::<T, I>();
let (item, ..) = mint_item::<T, I>(0);
}: _(SystemOrigin::Signed(caller.clone()), T::Helper::collection(0), T::Helper::item(0))
verify {
assert_last_event::<T, I>(Event::Frozen { collection: T::Helper::collection(0), item: T::Helper::item(0) }.into());
assert_last_event::<T, I>(Event::ItemTransferLocked { collection: T::Helper::collection(0), item: T::Helper::item(0) }.into());
}

thaw {
unlock_item_transfer {
let (collection, caller, caller_lookup) = create_collection::<T, I>();
let (item, ..) = mint_item::<T, I>(0);
Nfts::<T, I>::freeze(
Nfts::<T, I>::lock_item_transfer(
SystemOrigin::Signed(caller.clone()).into(),
collection,
item,
)?;
}: _(SystemOrigin::Signed(caller.clone()), collection, item)
verify {
assert_last_event::<T, I>(Event::Thawed { collection, item }.into());
}

freeze_collection {
let (collection, caller, caller_lookup) = create_collection::<T, I>();
}: _(SystemOrigin::Signed(caller.clone()), collection)
verify {
assert_last_event::<T, I>(Event::CollectionFrozen { collection }.into());
assert_last_event::<T, I>(Event::ItemTransferUnlocked { collection, item }.into());
}

thaw_collection {
lock_collection {
let (collection, caller, caller_lookup) = create_collection::<T, I>();
let origin = SystemOrigin::Signed(caller.clone()).into();
Nfts::<T, I>::freeze_collection(origin, collection)?;
}: _(SystemOrigin::Signed(caller.clone()), collection)
let lock_config = CollectionConfig(
CollectionSetting::TransferableItems |
CollectionSetting::UnlockedMetadata |
CollectionSetting::UnlockedAttributes,
);
}: _(SystemOrigin::Signed(caller.clone()), collection, lock_config)
verify {
assert_last_event::<T, I>(Event::CollectionThawed { collection }.into());
assert_last_event::<T, I>(Event::CollectionLocked { collection }.into());
}

transfer_ownership {
Expand Down Expand Up @@ -283,21 +271,30 @@ benchmarks_instance_pallet! {
}.into());
}

force_item_status {
force_collection_status {
let (collection, caller, caller_lookup) = create_collection::<T, I>();
let origin = T::ForceOrigin::successful_origin();
let call = Call::<T, I>::force_item_status {
let call = Call::<T, I>::force_collection_status {
collection,
owner: caller_lookup.clone(),
issuer: caller_lookup.clone(),
admin: caller_lookup.clone(),
freezer: caller_lookup,
free_holding: true,
is_frozen: false,
config: CollectionConfig(CollectionSetting::RequiredDeposit.into()),
};
}: { call.dispatch_bypass_filter(origin)? }
verify {
assert_last_event::<T, I>(Event::ItemStatusChanged { collection }.into());
assert_last_event::<T, I>(Event::CollectionStatusChanged { collection }.into());
}

lock_item_properties {
let (collection, caller, caller_lookup) = create_collection::<T, I>();
let (item, ..) = mint_item::<T, I>(0);
let lock_metadata = true;
let lock_attributes = true;
}: _(SystemOrigin::Signed(caller), collection, item, lock_metadata, lock_attributes)
verify {
assert_last_event::<T, I>(Event::ItemPropertiesLocked { collection, item, lock_metadata, lock_attributes }.into());
}

set_attribute {
Expand Down Expand Up @@ -327,9 +324,9 @@ benchmarks_instance_pallet! {

let (collection, caller, _) = create_collection::<T, I>();
let (item, ..) = mint_item::<T, I>(0);
}: _(SystemOrigin::Signed(caller), collection, item, data.clone(), false)
}: _(SystemOrigin::Signed(caller), collection, item, data.clone())
verify {
assert_last_event::<T, I>(Event::MetadataSet { collection, item, data, is_frozen: false }.into());
assert_last_event::<T, I>(Event::MetadataSet { collection, item, data }.into());
}

clear_metadata {
Expand All @@ -345,9 +342,9 @@ benchmarks_instance_pallet! {
let data: BoundedVec<_, _> = vec![0u8; T::StringLimit::get() as usize].try_into().unwrap();

let (collection, caller, _) = create_collection::<T, I>();
}: _(SystemOrigin::Signed(caller), collection, data.clone(), false)
}: _(SystemOrigin::Signed(caller), collection, data.clone())
verify {
assert_last_event::<T, I>(Event::CollectionMetadataSet { collection, data, is_frozen: false }.into());
assert_last_event::<T, I>(Event::CollectionMetadataSet { collection, data }.into());
}

clear_collection_metadata {
Expand Down
15 changes: 12 additions & 3 deletions frame/nfts/src/features/atomic_swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use frame_support::{
};

impl<T: Config<I>, I: 'static> Pallet<T, I> {
pub fn do_create_swap(
pub(crate) fn do_create_swap(
caller: T::AccountId,
offered_collection_id: T::CollectionId,
offered_item_id: T::ItemId,
Expand All @@ -31,6 +31,10 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
maybe_price: Option<PriceWithDirection<ItemPrice<T, I>>>,
duration: <T as SystemConfig>::BlockNumber,
) -> DispatchResult {
ensure!(
Self::is_pallet_feature_enabled(PalletFeature::Swaps),
Error::<T, I>::MethodDisabled
);
ensure!(duration <= T::MaxDeadlineDuration::get(), Error::<T, I>::WrongDuration);

let item = Item::<T, I>::get(&offered_collection_id, &offered_item_id)
Expand Down Expand Up @@ -74,7 +78,7 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
Ok(())
}

pub fn do_cancel_swap(
pub(crate) fn do_cancel_swap(
caller: T::AccountId,
offered_collection_id: T::CollectionId,
offered_item_id: T::ItemId,
Expand Down Expand Up @@ -103,14 +107,19 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> {
Ok(())
}

pub fn do_claim_swap(
pub(crate) fn do_claim_swap(
caller: T::AccountId,
send_collection_id: T::CollectionId,
send_item_id: T::ItemId,
receive_collection_id: T::CollectionId,
receive_item_id: T::ItemId,
witness_price: Option<PriceWithDirection<ItemPrice<T, I>>>,
) -> DispatchResult {
ensure!(
Self::is_pallet_feature_enabled(PalletFeature::Swaps),
Error::<T, I>::MethodDisabled
);

let send_item = Item::<T, I>::get(&send_collection_id, &send_item_id)
.ok_or(Error::<T, I>::UnknownItem)?;
let receive_item = Item::<T, I>::get(&receive_collection_id, &receive_item_id)
Expand Down
2 changes: 1 addition & 1 deletion frame/nfts/src/features/buy_sell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use frame_support::{
};

impl<T: Config<I>, I: 'static> Pallet<T, I> {
pub fn do_pay_tips(
pub(crate) fn do_pay_tips(
sender: T::AccountId,
tips: BoundedVec<ItemTipOf<T, I>, T::MaxTips>,
) -> DispatchResult {
Expand Down
Loading