git.delta.rocks / unique-network / refs/commits / c12162af7885

difftreelog

Merge pull request #785 from UniqueNetwork/feature/force-repair-things

ut-akuznetsov2022-12-16parents: #d8b03ef #196e170.patch.diff
in: master

12 files changed

modifiedpallets/common/src/lib.rsdiffbeforeafterboth
1223 }1223 }
12241224
1225 /// Set a scoped collection property, where the scope is a special prefix1225 /// Set a scoped collection property, where the scope is a special prefix
1226 /// prohibiting a user to change the property.1226 /// prohibiting a user access to change the property directly.
1227 ///1227 ///
1228 /// * `collection_id` - ID of the collection for which the property is being set.1228 /// * `collection_id` - ID of the collection for which the property is being set.
1229 /// * `scope` - Property scope.1229 /// * `scope` - Property scope.
1242 }1242 }
12431243
1244 /// Set scoped collection properties, where the scope is a special prefix1244 /// Set scoped collection properties, where the scope is a special prefix
1245 /// prohibiting a user to change the properties.1245 /// prohibiting a user access to change the properties directly.
1246 ///1246 ///
1247 /// * `collection_id` - ID of the collection for which the properties is being set.1247 /// * `collection_id` - ID of the collection for which the properties is being set.
1248 /// * `scope` - Property scope.1248 /// * `scope` - Property scope.
1731 Ok(new_permission)1731 Ok(new_permission)
1732 }1732 }
1733
1734 /// Repair possibly broken properties of a collection.
1735 pub fn repair_collection(collection_id: CollectionId) -> DispatchResult {
1736 CollectionProperties::<T>::mutate(collection_id, |properties| {
1737 properties.recompute_consumed_space();
1738 });
1739
1740 Ok(())
1741 }
1733}1742}
17341743
1735/// Indicates unsupported methods by returning [Error::UnsupportedOperation].1744/// Indicates unsupported methods by returning [Error::UnsupportedOperation].
1819 fn set_allowance_for_all() -> Weight;1828 fn set_allowance_for_all() -> Weight;
18201829
1821 /// The price of repairing an item.1830 /// The price of repairing an item.
1822 fn repair_item() -> Weight;1831 fn force_repair_item() -> Weight;
1823}1832}
18241833
1825/// Weight info extension trait for refungible pallet.1834/// Weight info extension trait for refungible pallet.
modifiedpallets/fungible/src/common.rsdiffbeforeafterboth
113 Weight::zero()113 Weight::zero()
114 }114 }
115115
116 fn repair_item() -> Weight {116 fn force_repair_item() -> Weight {
117 Weight::zero()117 Weight::zero()
118 }118 }
119}119}
modifiedpallets/nonfungible/src/common.rsdiffbeforeafterboth
127 <SelfWeightOf<T>>::set_allowance_for_all()127 <SelfWeightOf<T>>::set_allowance_for_all()
128 }128 }
129129
130 fn repair_item() -> Weight {130 fn force_repair_item() -> Weight {
131 <SelfWeightOf<T>>::repair_item()131 <SelfWeightOf<T>>::repair_item()
132 }132 }
133}133}
540 fn repair_item(&self, token: TokenId) -> DispatchResultWithPostInfo {540 fn repair_item(&self, token: TokenId) -> DispatchResultWithPostInfo {
541 with_weight(541 with_weight(
542 <Pallet<T>>::repair_item(self, token),542 <Pallet<T>>::repair_item(self, token),
543 <CommonWeights<T>>::repair_item(),543 <CommonWeights<T>>::force_repair_item(),
544 )544 )
545 }545 }
546}546}
modifiedpallets/refungible/src/common.rsdiffbeforeafterboth
157 <SelfWeightOf<T>>::set_allowance_for_all()157 <SelfWeightOf<T>>::set_allowance_for_all()
158 }158 }
159159
160 fn repair_item() -> Weight {160 fn force_repair_item() -> Weight {
161 <SelfWeightOf<T>>::repair_item()161 <SelfWeightOf<T>>::repair_item()
162 }162 }
163}163}
544 fn repair_item(&self, token: TokenId) -> DispatchResultWithPostInfo {544 fn repair_item(&self, token: TokenId) -> DispatchResultWithPostInfo {
545 with_weight(545 with_weight(
546 <Pallet<T>>::repair_item(self, token),546 <Pallet<T>>::repair_item(self, token),
547 <CommonWeights<T>>::repair_item(),547 <CommonWeights<T>>::force_repair_item(),
548 )548 )
549 }549 }
550}550}
modifiedpallets/unique/src/lib.rsdiffbeforeafterboth
82 BoundedVec,82 BoundedVec,
83};83};
84use scale_info::TypeInfo;84use scale_info::TypeInfo;
85use frame_system::{self as system, ensure_signed};85use frame_system::{self as system, ensure_signed, ensure_root};
86use sp_std::{vec, vec::Vec};86use sp_std::{vec, vec::Vec};
87use up_data_structs::{87use up_data_structs::{
88 MAX_COLLECTION_NAME_LENGTH, MAX_COLLECTION_DESCRIPTION_LENGTH, MAX_TOKEN_PREFIX_LENGTH,88 MAX_COLLECTION_NAME_LENGTH, MAX_COLLECTION_DESCRIPTION_LENGTH, MAX_TOKEN_PREFIX_LENGTH,
983 })983 })
984 }984 }
985985
986 /// Repairs a broken item986 /// Repairs a collection if the data was somehow corrupted.
987 ///
988 /// # Arguments
989 ///
990 /// * `collection_id`: ID of the collection to repair.
991 #[weight = <SelfWeightOf<T>>::force_repair_collection()]
992 pub fn force_repair_collection(
993 origin,
994 collection_id: CollectionId,
995 ) -> DispatchResult {
996 ensure_root(origin)?;
997 <PalletCommon<T>>::repair_collection(collection_id)
998 }
999
1000 /// Repairs a token if the data was somehow corrupted.
987 ///1001 ///
988 /// # Arguments1002 /// # Arguments
989 ///1003 ///
990 /// * `collection_id`: ID of the collection the item belongs to.1004 /// * `collection_id`: ID of the collection the item belongs to.
991 /// * `item_id`: ID of the item.1005 /// * `item_id`: ID of the item.
992 #[weight = T::CommonWeightInfo::repair_item()]1006 #[weight = T::CommonWeightInfo::force_repair_item()]
993 pub fn repair_item(1007 pub fn force_repair_item(
994 _origin,1008 origin,
995 collection_id: CollectionId,1009 collection_id: CollectionId,
996 item_id: TokenId,1010 item_id: TokenId,
997 ) -> DispatchResultWithPostInfo {1011 ) -> DispatchResultWithPostInfo {
1012 ensure_root(origin)?;
998 dispatch_tx::<T, _>(collection_id, |d| {1013 dispatch_tx::<T, _>(collection_id, |d| {
999 d.repair_item(item_id)1014 d.repair_item(item_id)
1000 })1015 })
modifiedpallets/unique/src/weights.rsdiffbeforeafterboth
45 fn remove_collection_sponsor() -> Weight;45 fn remove_collection_sponsor() -> Weight;
46 fn set_transfers_enabled_flag() -> Weight;46 fn set_transfers_enabled_flag() -> Weight;
47 fn set_collection_limits() -> Weight;47 fn set_collection_limits() -> Weight;
48 fn force_repair_collection() -> Weight;
48}49}
4950
50/// Weights for pallet_unique using the Substrate node and recommended hardware.51/// Weights for pallet_unique using the Substrate node and recommended hardware.
139 .saturating_add(T::DbWeight::get().reads(1 as u64))140 .saturating_add(T::DbWeight::get().reads(1 as u64))
140 .saturating_add(T::DbWeight::get().writes(1 as u64))141 .saturating_add(T::DbWeight::get().writes(1 as u64))
141 }142 }
143 // Storage: Common CollectionProperties (r:1 w:1)
144 fn force_repair_collection() -> Weight {
145 Weight::from_ref_time(5_701_000 as u64)
146 .saturating_add(T::DbWeight::get().reads(1 as u64))
147 .saturating_add(T::DbWeight::get().writes(1 as u64))
148 }
142}149}
143150
144// For backwards compatibility and tests151// For backwards compatibility and tests
232 .saturating_add(RocksDbWeight::get().reads(1 as u64))239 .saturating_add(RocksDbWeight::get().reads(1 as u64))
233 .saturating_add(RocksDbWeight::get().writes(1 as u64))240 .saturating_add(RocksDbWeight::get().writes(1 as u64))
234 }241 }
242 // Storage: Common CollectionProperties (r:1 w:1)
243 fn force_repair_collection() -> Weight {
244 Weight::from_ref_time(5_701_000 as u64)
245 .saturating_add(RocksDbWeight::get().reads(1 as u64))
246 .saturating_add(RocksDbWeight::get().writes(1 as u64))
247 }
235}248}
236249
modifiedruntime/common/weights.rsdiffbeforeafterboth
125 max_weight_of!(set_allowance_for_all())125 max_weight_of!(set_allowance_for_all())
126 }126 }
127127
128 fn repair_item() -> Weight {128 fn force_repair_item() -> Weight {
129 max_weight_of!(repair_item())129 max_weight_of!(force_repair_item())
130 }130 }
131}131}
132132
modifiedtests/package.jsondiffbeforeafterboth
44 "testEvmCoder": "mocha --timeout 9999999 -r ts-node/register './**/eth/evmCoder.test.ts'",44 "testEvmCoder": "mocha --timeout 9999999 -r ts-node/register './**/eth/evmCoder.test.ts'",
45 "testNesting": "mocha --timeout 9999999 -r ts-node/register ./**/nest.test.ts",45 "testNesting": "mocha --timeout 9999999 -r ts-node/register ./**/nest.test.ts",
46 "testUnnesting": "mocha --timeout 9999999 -r ts-node/register ./**/unnest.test.ts",46 "testUnnesting": "mocha --timeout 9999999 -r ts-node/register ./**/unnest.test.ts",
47 "testProperties": "mocha --timeout 9999999 -r ts-node/register ./**/collectionProperties.test.ts ./**/tokenProperties.test.ts ./**/getPropertiesRpc.test.ts",47 "testProperties": "mocha --timeout 9999999 -r ts-node/register ./**/collectionProperties.*test.ts ./**/tokenProperties.*test.ts ./**/getPropertiesRpc.test.ts",
48 "testCollectionProperties": "mocha --timeout 9999999 -r ts-node/register ./**/collectionProperties.test.ts",48 "testCollectionProperties": "mocha --timeout 9999999 -r ts-node/register ./**/collectionProperties.*test.ts",
49 "testTokenProperties": "mocha --timeout 9999999 -r ts-node/register ./**/tokenProperties.*test.ts",
49 "testMigration": "mocha --timeout 9999999 -r ts-node/register ./**/nesting/migration-check.test.ts",50 "testMigration": "mocha --timeout 9999999 -r ts-node/register ./**/nesting/migration-check.test.ts",
50 "testAddCollectionAdmin": "mocha --timeout 9999999 -r ts-node/register ./**/addCollectionAdmin.test.ts",51 "testAddCollectionAdmin": "mocha --timeout 9999999 -r ts-node/register ./**/addCollectionAdmin.test.ts",
51 "testSetCollectionLimits": "mocha --timeout 9999999 -r ts-node/register ./**/setCollectionLimits.test.ts",52 "testSetCollectionLimits": "mocha --timeout 9999999 -r ts-node/register ./**/setCollectionLimits.test.ts",
addedtests/src/nesting/collectionProperties.seqtest.tsdiffbeforeafterboth

no changes

modifiedtests/src/nesting/collectionProperties.test.tsdiffbeforeafterboth
315 }315 }
316 });316 });
317
318 itSub('Forbids to repair a collection if called with non-sudo', async({helper}) => {
319 const collection = await helper[testSuite.mode].mintCollection(alice, {properties: [
320 {key: 'sea-creatures', value: 'mermaids'},
321 {key: 'goldenratio', value: '1.6180339887498948482045868343656381177203091798057628621354486227052604628189'},
322 ]});
323
324 await expect(helper.executeExtrinsic(alice, 'api.tx.unique.forceRepairCollection', [collection.collectionId], true))
325 .to.be.rejectedWith(/BadOrigin/);
326 });
317 }));327 }));
318});328});
319 329
addedtests/src/nesting/tokenProperties.seqtest.tsdiffbeforeafterboth

no changes

modifiedtests/src/nesting/tokenProperties.test.tsdiffbeforeafterboth
402 expect(consumedSpace).to.be.equal(originalSpace);402 expect(consumedSpace).to.be.equal(originalSpace);
403 }));403 }));
404
405 [
406 {mode: 'nft' as const, pieces: undefined, requiredPallets: []},
407 {mode: 'rft' as const, pieces: 100n, requiredPallets: [Pallets.ReFungible]},
408 ].map(testCase =>
409 itSub.ifWithPallets(`repair_item preserves valid consumed space (${testCase.mode})`, testCase.requiredPallets, async({helper}) => {
410 const propKey = 'tok-prop';
411
412 const collection = await helper[testCase.mode].mintCollection(alice, {
413 tokenPropertyPermissions: [
414 {
415 key: propKey,
416 permission: {mutable: true, tokenOwner: true},
417 },
418 ],
419 });
420 const token = await (
421 testCase.pieces
422 ? collection.mintToken(alice, testCase.pieces)
423 : collection.mintToken(alice)
424 );
425
426 const propDataSize = 4096;
427 const propData = 'a'.repeat(propDataSize);
428
429 await token.setProperties(alice, [{key: propKey, value: propData}]);
430 const originalSpace = await token.getTokenPropertiesConsumedSpace();
431 expect(originalSpace).to.be.equal(propDataSize);
432
433 await helper.executeExtrinsic(alice, 'api.tx.unique.repairItem', [token.collectionId, token.tokenId], true);
434 const recomputedSpace = await token.getTokenPropertiesConsumedSpace();
435 expect(recomputedSpace).to.be.equal(originalSpace);
436 }));
437404
438 [405 [
439 {mode: 'nft' as const, pieces: undefined, requiredPallets: []},406 {mode: 'nft' as const, pieces: undefined, requiredPallets: []},
698 }])).to.be.rejectedWith(/common\.PropertyLimitReached/);665 }])).to.be.rejectedWith(/common\.PropertyLimitReached/);
699 }));666 }));
667
668 [
669 {mode: 'nft' as const, pieces: undefined, requiredPallets: []},
670 {mode: 'rft' as const, pieces: 100n, requiredPallets: [Pallets.ReFungible]},
671 ].map(testCase =>
672 itSub.ifWithPallets(`Forbids force_repair_item from non-sudo (${testCase.mode})`, testCase.requiredPallets, async({helper}) => {
673 const propKey = 'tok-prop';
674
675 const collection = await helper[testCase.mode].mintCollection(alice, {
676 tokenPropertyPermissions: [
677 {
678 key: propKey,
679 permission: {mutable: true, tokenOwner: true},
680 },
681 ],
682 });
683 const token = await (
684 testCase.pieces
685 ? collection.mintToken(alice, testCase.pieces)
686 : collection.mintToken(alice)
687 );
688
689 const propDataSize = 4096;
690 const propData = 'a'.repeat(propDataSize);
691 await token.setProperties(alice, [{key: propKey, value: propData}]);
692
693 await expect(helper.executeExtrinsic(alice, 'api.tx.unique.forceRepairItem', [token.collectionId, token.tokenId], true))
694 .to.be.rejectedWith(/BadOrigin/);
695 }));
700});696});
701697
702describe('ReFungible token properties permissions tests', () => {698describe('ReFungible token properties permissions tests', () => {