difftreelog
test update property size calculation
in: master
5 files changed
tests/src/nesting/collectionProperties.seqtest.tsdiffbeforeafterboth--- a/tests/src/nesting/collectionProperties.seqtest.ts
+++ b/tests/src/nesting/collectionProperties.seqtest.ts
@@ -15,7 +15,7 @@
// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
import {IKeyringPair} from '@polkadot/types/types';
-import {itSub, Pallets, usingPlaygrounds, expect, requirePalletsOrSkip} from '../util';
+import {itSub, Pallets, usingPlaygrounds, expect, requirePalletsOrSkip, sizeOfProperty} from '../util';
describe('Integration Test: Collection Properties with sudo', () => {
let superuser: IKeyringPair;
@@ -48,14 +48,14 @@
];
const collection = await helper[testSuite.mode].mintCollection(alice, {properties});
- const newProperty = ' '.repeat(4096);
- await collection.setProperties(alice, [{key: 'space', value: newProperty}]);
+ const newProperty = {key: 'space', value: ' '.repeat(4096)};
+ await collection.setProperties(alice, [newProperty]);
const originalSpace = await collection.getPropertiesConsumedSpace();
- expect(originalSpace).to.be.equal(properties[0].value.length + properties[1].value.length + newProperty.length);
+ expect(originalSpace).to.be.equal(sizeOfProperty(properties[0]) + sizeOfProperty(properties[1]) + sizeOfProperty(newProperty));
await helper.getSudo().executeExtrinsic(superuser, 'api.tx.unique.forceRepairCollection', [collection.collectionId], true);
const recomputedSpace = await collection.getPropertiesConsumedSpace();
expect(recomputedSpace).to.be.equal(originalSpace);
});
}));
-});
\ No newline at end of file
+});
tests/src/nesting/collectionProperties.test.tsdiffbeforeafterboth1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import {IKeyringPair} from '@polkadot/types/types';18import {itSub, Pallets, usingPlaygrounds, expect, requirePalletsOrSkip} from '../util';1920describe('Integration Test: Collection Properties', () => {21 let alice: IKeyringPair;22 let bob: IKeyringPair;2324 before(async () => {25 await usingPlaygrounds(async (helper, privateKey) => {26 const donor = await privateKey({filename: __filename});27 [alice, bob] = await helper.arrange.createAccounts([200n, 10n], donor);28 });29 });3031 itSub('Properties are initially empty', async ({helper}) => {32 const collection = await helper.nft.mintCollection(alice);33 expect(await collection.getProperties()).to.be.empty;34 });3536 [37 {mode: 'nft' as const, requiredPallets: []},38 {mode: 'ft' as const, requiredPallets: []},39 {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},40 ].map(testSuite => describe(`${testSuite.mode.toUpperCase()}`, () => {41 before(async function() {42 // eslint-disable-next-line require-await43 await usingPlaygrounds(async helper => {44 requirePalletsOrSkip(this, helper, testSuite.requiredPallets);45 });46 });4748 itSub('Sets properties for a collection', async ({helper}) => {49 const collection = await helper[testSuite.mode].mintCollection(alice);5051 // As owner52 await expect(collection.setProperties(alice, [{key: 'electron', value: 'come bond'}])).to.be.fulfilled;5354 await collection.addAdmin(alice, {Substrate: bob.address});5556 // As administrator57 await expect(collection.setProperties(bob, [{key: 'black_hole'}])).to.be.fulfilled;5859 const properties = await collection.getProperties();60 expect(properties).to.include.deep.members([61 {key: 'electron', value: 'come bond'},62 {key: 'black_hole', value: ''},63 ]);64 });6566 itSub('Check valid names for collection properties keys', async ({helper}) => {67 const collection = await helper[testSuite.mode].mintCollection(alice);6869 // alpha symbols70 await expect(collection.setProperties(alice, [{key: 'answer'}])).to.be.fulfilled;7172 // numeric symbols73 await expect(collection.setProperties(alice, [{key: '451'}])).to.be.fulfilled;7475 // underscore symbol76 await expect(collection.setProperties(alice, [{key: 'black_hole'}])).to.be.fulfilled;7778 // dash symbol79 await expect(collection.setProperties(alice, [{key: '-'}])).to.be.fulfilled;8081 // dot symbol82 await expect(collection.setProperties(alice, [{key: 'once.in.a.long.long.while...', value: 'you get a little lost'}])).to.be.fulfilled;8384 const properties = await collection.getProperties();85 expect(properties).to.include.deep.members([86 {key: 'answer', value: ''},87 {key: '451', value: ''},88 {key: 'black_hole', value: ''},89 {key: '-', value: ''},90 {key: 'once.in.a.long.long.while...', value: 'you get a little lost'},91 ]);92 });9394 itSub('Changes properties of a collection', async ({helper}) => {95 const collection = await helper[testSuite.mode].mintCollection(alice);9697 await expect(collection.setProperties(alice, [{key: 'electron', value: 'come bond'}, {key: 'black_hole', value: ''}])).to.be.fulfilled;9899 // Mutate the properties100 await expect(collection.setProperties(alice, [{key: 'black_hole', value: 'LIGO'}])).to.be.fulfilled;101102 const properties = await collection.getProperties();103 expect(properties).to.include.deep.members([104 {key: 'electron', value: 'come bond'},105 {key: 'black_hole', value: 'LIGO'},106 ]);107 });108109 itSub('Deletes properties of a collection', async ({helper}) => {110 const collection = await helper[testSuite.mode].mintCollection(alice);111112 await expect(collection.setProperties(alice, [{key: 'electron', value: 'come bond'}, {key: 'black_hole', value: 'LIGO'}])).to.be.fulfilled;113114 await expect(collection.deleteProperties(alice, ['electron'])).to.be.fulfilled;115116 const properties = await collection.getProperties(['black_hole', 'electron']);117 expect(properties).to.be.deep.equal([118 {key: 'black_hole', value: 'LIGO'},119 ]);120 });121122 itSub('Allows modifying a collection property multiple times with the same size', async({helper}) => {123 const propKey = 'tok-prop';124125 const collection = await helper[testSuite.mode].mintCollection(alice);126127 const maxCollectionPropertiesSize = 40960;128129 const propDataSize = 4096;130131 let propDataChar = 'a';132 const makeNewPropData = () => {133 propDataChar = String.fromCharCode(propDataChar.charCodeAt(0) + 1);134 return `${propDataChar}`.repeat(propDataSize);135 };136137 await collection.setProperties(alice, [{key: propKey, value: makeNewPropData()}]);138 const originalSpace = await collection.getPropertiesConsumedSpace();139 expect(originalSpace).to.be.equal(propDataSize);140141 const sameSizePropertiesPossibleNum = maxCollectionPropertiesSize / propDataSize;142143 // It is possible to modify a property as many times as needed.144 // It will not consume any additional space.145 for (let i = 0; i < sameSizePropertiesPossibleNum + 1; i++) {146 await collection.setProperties(alice, [{key: propKey, value: makeNewPropData()}]);147 const consumedSpace = await collection.getPropertiesConsumedSpace();148 expect(consumedSpace).to.be.equal(originalSpace);149 }150 });151152 itSub('Adding then removing a collection property doesn\'t change the consumed space', async({helper}) => {153 const propKey = 'tok-prop';154155 const collection = await helper[testSuite.mode].mintCollection(alice);156 const originalSpace = await collection.getPropertiesConsumedSpace();157158 const propDataSize = 4096;159 const propData = 'a'.repeat(propDataSize);160161 await collection.setProperties(alice, [{key: propKey, value: propData}]);162 let consumedSpace = await collection.getPropertiesConsumedSpace();163 expect(consumedSpace).to.be.equal(propDataSize);164165 await collection.deleteProperties(alice, [propKey]);166 consumedSpace = await collection.getPropertiesConsumedSpace();167 expect(consumedSpace).to.be.equal(originalSpace);168 });169170 itSub('Modifying a collection property with different sizes correctly changes the consumed space', async({helper}) => {171 const propKey = 'tok-prop';172173 const collection = await helper[testSuite.mode].mintCollection(alice);174 const originalSpace = await collection.getPropertiesConsumedSpace();175176 const initPropDataSize = 4096;177 const biggerPropDataSize = 5000;178 const smallerPropDataSize = 4000;179180 const initPropData = 'a'.repeat(initPropDataSize);181 const biggerPropData = 'b'.repeat(biggerPropDataSize);182 const smallerPropData = 'c'.repeat(smallerPropDataSize);183184 let consumedSpace;185 let expectedConsumedSpaceDiff;186187 await collection.setProperties(alice, [{key: propKey, value: initPropData}]);188 consumedSpace = await collection.getPropertiesConsumedSpace();189 expectedConsumedSpaceDiff = initPropDataSize - originalSpace;190 expect(consumedSpace).to.be.equal(originalSpace + expectedConsumedSpaceDiff);191192 await collection.setProperties(alice, [{key: propKey, value: biggerPropData}]);193 consumedSpace = await collection.getPropertiesConsumedSpace();194 expectedConsumedSpaceDiff = biggerPropDataSize - initPropDataSize;195 expect(consumedSpace).to.be.equal(initPropDataSize + expectedConsumedSpaceDiff);196197 await collection.setProperties(alice, [{key: propKey, value: smallerPropData}]);198 consumedSpace = await collection.getPropertiesConsumedSpace();199 expectedConsumedSpaceDiff = biggerPropDataSize - smallerPropDataSize;200 expect(consumedSpace).to.be.equal(biggerPropDataSize - expectedConsumedSpaceDiff);201 });202 }));203});204205describe('Negative Integration Test: Collection Properties', () => {206 let alice: IKeyringPair;207 let bob: IKeyringPair;208209 before(async () => {210 await usingPlaygrounds(async (helper, privateKey) => {211 const donor = await privateKey({filename: __filename});212 [alice, bob] = await helper.arrange.createAccounts([1000n, 100n], donor);213 });214 });215216 [217 {mode: 'nft' as const, requiredPallets: []},218 {mode: 'ft' as const, requiredPallets: []},219 {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},220 ].map(testSuite => describe(`${testSuite.mode.toUpperCase()}`, () => {221 before(async function() {222 // eslint-disable-next-line require-await223 await usingPlaygrounds(async helper => {224 requirePalletsOrSkip(this, helper, testSuite.requiredPallets);225 });226 });227228 itSub('Fails to set properties in a collection if not its onwer/administrator', async ({helper}) => {229 const collection = await helper[testSuite.mode].mintCollection(alice);230231 await expect(collection.setProperties(bob, [{key: 'electron', value: 'come bond'}, {key: 'black_hole', value: 'LIGO'}]))232 .to.be.rejectedWith(/common\.NoPermission/);233234 expect(await collection.getProperties()).to.be.empty;235 });236237 itSub('Fails to set properties that exceed the limits', async ({helper}) => {238 const collection = await helper[testSuite.mode].mintCollection(alice);239240 const spaceLimit = (await (collection.helper!.api! as any).query.common.collectionProperties(collection.collectionId)).spaceLimit.toNumber();241242 // Mute the general tx parsing error, too many bytes to process243 {244 console.error = () => {};245 await expect(collection.setProperties(alice, [246 {key: 'electron', value: 'low high '.repeat(Math.ceil(spaceLimit! / 9))},247 ])).to.be.rejected;248 }249250 expect(await collection.getProperties(['electron'])).to.be.empty;251252 await expect(collection.setProperties(alice, [253 {key: 'electron', value: 'low high '.repeat(Math.ceil(spaceLimit! / 18))},254 {key: 'black_hole', value: '0'.repeat(Math.ceil(spaceLimit! / 2))},255 ])).to.be.rejectedWith(/common\.NoSpaceForProperty/);256257 expect(await collection.getProperties(['electron', 'black_hole'])).to.be.empty;258 });259260 itSub('Fails to set more properties than it is allowed', async ({helper}) => {261 const collection = await helper[testSuite.mode].mintCollection(alice);262263 const propertiesToBeSet = [];264 for (let i = 0; i < 65; i++) {265 propertiesToBeSet.push({266 key: 'electron_' + i,267 value: Math.random() > 0.5 ? 'high' : 'low',268 });269 }270271 await expect(collection.setProperties(alice, propertiesToBeSet)).272 to.be.rejectedWith(/common\.PropertyLimitReached/);273274 expect(await collection.getProperties()).to.be.empty;275 });276277 itSub('Fails to set properties with invalid names', async ({helper}) => {278 const collection = await helper[testSuite.mode].mintCollection(alice);279280 const invalidProperties = [281 [{key: 'electron', value: 'negative'}, {key: 'string theory', value: 'understandable'}],282 [{key: 'Mr/Sandman', value: 'Bring me a gene'}],283 [{key: 'déjà vu', value: 'hmm...'}],284 ];285286 for (let i = 0; i < invalidProperties.length; i++) {287 await expect(288 collection.setProperties(alice, invalidProperties[i]),289 `on rejecting the new badly-named property #${i}`,290 ).to.be.rejectedWith(/common\.InvalidCharacterInPropertyKey/);291 }292293 await expect(294 collection.setProperties(alice, [{key: '', value: 'nothing must not exist'}]),295 'on rejecting an unnamed property',296 ).to.be.rejectedWith(/common\.EmptyPropertyKey/);297298 await expect(299 collection.setProperties(alice, [{key: 'CRISPR-Cas9', value: 'rewriting nature!'}]),300 'on setting the correctly-but-still-badly-named property',301 ).to.be.fulfilled;302303 const keys = invalidProperties.flatMap(propertySet => propertySet.map(property => property.key)).concat('CRISPR-Cas9').concat('');304305 const properties = await collection.getProperties(keys);306 expect(properties).to.be.deep.equal([307 {key: 'CRISPR-Cas9', value: 'rewriting nature!'},308 ]);309310 for (let i = 0; i < invalidProperties.length; i++) {311 await expect(312 collection.deleteProperties(alice, invalidProperties[i].map(propertySet => propertySet.key)),313 `on trying to delete the non-existent badly-named property #${i}`,314 ).to.be.rejectedWith(/common\.InvalidCharacterInPropertyKey/);315 }316 });317318 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 ]});323324 await expect(helper.executeExtrinsic(alice, 'api.tx.unique.forceRepairCollection', [collection.collectionId], true))325 .to.be.rejectedWith(/BadOrigin/);326 });327 }));328});1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import {IKeyringPair} from '@polkadot/types/types';18import {itSub, Pallets, usingPlaygrounds, expect, requirePalletsOrSkip, sizeOfProperty} from '../util';1920describe('Integration Test: Collection Properties', () => {21 let alice: IKeyringPair;22 let bob: IKeyringPair;2324 before(async () => {25 await usingPlaygrounds(async (helper, privateKey) => {26 const donor = await privateKey({filename: __filename});27 [alice, bob] = await helper.arrange.createAccounts([200n, 10n], donor);28 });29 });3031 itSub('Properties are initially empty', async ({helper}) => {32 const collection = await helper.nft.mintCollection(alice);33 expect(await collection.getProperties()).to.be.empty;34 });3536 [37 {mode: 'nft' as const, requiredPallets: []},38 {mode: 'ft' as const, requiredPallets: []},39 {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},40 ].map(testSuite => describe(`${testSuite.mode.toUpperCase()}`, () => {41 before(async function() {42 // eslint-disable-next-line require-await43 await usingPlaygrounds(async helper => {44 requirePalletsOrSkip(this, helper, testSuite.requiredPallets);45 });46 });4748 itSub('Sets properties for a collection', async ({helper}) => {49 const collection = await helper[testSuite.mode].mintCollection(alice);5051 // As owner52 await expect(collection.setProperties(alice, [{key: 'electron', value: 'come bond'}])).to.be.fulfilled;5354 await collection.addAdmin(alice, {Substrate: bob.address});5556 // As administrator57 await expect(collection.setProperties(bob, [{key: 'black_hole'}])).to.be.fulfilled;5859 const properties = await collection.getProperties();60 expect(properties).to.include.deep.members([61 {key: 'electron', value: 'come bond'},62 {key: 'black_hole', value: ''},63 ]);64 });6566 itSub('Check valid names for collection properties keys', async ({helper}) => {67 const collection = await helper[testSuite.mode].mintCollection(alice);6869 // alpha symbols70 await expect(collection.setProperties(alice, [{key: 'answer'}])).to.be.fulfilled;7172 // numeric symbols73 await expect(collection.setProperties(alice, [{key: '451'}])).to.be.fulfilled;7475 // underscore symbol76 await expect(collection.setProperties(alice, [{key: 'black_hole'}])).to.be.fulfilled;7778 // dash symbol79 await expect(collection.setProperties(alice, [{key: '-'}])).to.be.fulfilled;8081 // dot symbol82 await expect(collection.setProperties(alice, [{key: 'once.in.a.long.long.while...', value: 'you get a little lost'}])).to.be.fulfilled;8384 const properties = await collection.getProperties();85 expect(properties).to.include.deep.members([86 {key: 'answer', value: ''},87 {key: '451', value: ''},88 {key: 'black_hole', value: ''},89 {key: '-', value: ''},90 {key: 'once.in.a.long.long.while...', value: 'you get a little lost'},91 ]);92 });9394 itSub('Changes properties of a collection', async ({helper}) => {95 const collection = await helper[testSuite.mode].mintCollection(alice);9697 await expect(collection.setProperties(alice, [{key: 'electron', value: 'come bond'}, {key: 'black_hole', value: ''}])).to.be.fulfilled;9899 // Mutate the properties100 await expect(collection.setProperties(alice, [{key: 'black_hole', value: 'LIGO'}])).to.be.fulfilled;101102 const properties = await collection.getProperties();103 expect(properties).to.include.deep.members([104 {key: 'electron', value: 'come bond'},105 {key: 'black_hole', value: 'LIGO'},106 ]);107 });108109 itSub('Deletes properties of a collection', async ({helper}) => {110 const collection = await helper[testSuite.mode].mintCollection(alice);111112 await expect(collection.setProperties(alice, [{key: 'electron', value: 'come bond'}, {key: 'black_hole', value: 'LIGO'}])).to.be.fulfilled;113114 await expect(collection.deleteProperties(alice, ['electron'])).to.be.fulfilled;115116 const properties = await collection.getProperties(['black_hole', 'electron']);117 expect(properties).to.be.deep.equal([118 {key: 'black_hole', value: 'LIGO'},119 ]);120 });121122 itSub('Allows modifying a collection property multiple times with the same size', async({helper}) => {123 const propKey = 'tok-prop';124125 const collection = await helper[testSuite.mode].mintCollection(alice);126127 const maxCollectionPropertiesSize = 40960;128129 const propDataSize = 4096;130131 let propDataChar = 'a';132 const makeNewPropData = () => {133 propDataChar = String.fromCharCode(propDataChar.charCodeAt(0) + 1);134 return `${propDataChar}`.repeat(propDataSize);135 };136137 const property = {key: propKey, value: makeNewPropData()};138 await collection.setProperties(alice, [property]);139 const originalSpace = await collection.getPropertiesConsumedSpace();140 expect(originalSpace).to.be.equal(sizeOfProperty(property));141142 const sameSizePropertiesPossibleNum = maxCollectionPropertiesSize / propDataSize;143144 // It is possible to modify a property as many times as needed.145 // It will not consume any additional space.146 for (let i = 0; i < sameSizePropertiesPossibleNum + 1; i++) {147 await collection.setProperties(alice, [{key: propKey, value: makeNewPropData()}]);148 const consumedSpace = await collection.getPropertiesConsumedSpace();149 expect(consumedSpace).to.be.equal(originalSpace);150 }151 });152153 itSub('Adding then removing a collection property doesn\'t change the consumed space', async({helper}) => {154 const propKey = 'tok-prop';155156 const collection = await helper[testSuite.mode].mintCollection(alice);157 const originalSpace = await collection.getPropertiesConsumedSpace();158159 const propDataSize = 4096;160 const propData = 'a'.repeat(propDataSize);161162 const property = {key: propKey, value: propData};163 await collection.setProperties(alice, [property]);164 let consumedSpace = await collection.getPropertiesConsumedSpace();165 expect(consumedSpace).to.be.equal(sizeOfProperty(property));166167 await collection.deleteProperties(alice, [propKey]);168 consumedSpace = await collection.getPropertiesConsumedSpace();169 expect(consumedSpace).to.be.equal(originalSpace);170 });171172 itSub('Modifying a collection property with different sizes correctly changes the consumed space', async({helper}) => {173 const propKey = 'tok-prop';174175 const collection = await helper[testSuite.mode].mintCollection(alice);176 const originalSpace = await collection.getPropertiesConsumedSpace();177178 const initProp = {key: propKey, value: 'a'.repeat(4096)};179 const biggerProp = {key: propKey, value: 'b'.repeat(5000)};180 const smallerProp = {key: propKey, value: 'c'.repeat(4000)};181182 let consumedSpace;183 let expectedConsumedSpaceDiff;184185 await collection.setProperties(alice, [initProp]);186 consumedSpace = await collection.getPropertiesConsumedSpace();187 expectedConsumedSpaceDiff = sizeOfProperty(initProp) - originalSpace;188 expect(consumedSpace).to.be.equal(originalSpace + expectedConsumedSpaceDiff);189190 await collection.setProperties(alice, [biggerProp]);191 consumedSpace = await collection.getPropertiesConsumedSpace();192 expectedConsumedSpaceDiff = sizeOfProperty(biggerProp) - sizeOfProperty(initProp);193 expect(consumedSpace).to.be.equal(sizeOfProperty(initProp) + expectedConsumedSpaceDiff);194195 await collection.setProperties(alice, [smallerProp]);196 consumedSpace = await collection.getPropertiesConsumedSpace();197 expectedConsumedSpaceDiff = sizeOfProperty(biggerProp) - sizeOfProperty(smallerProp);198 expect(consumedSpace).to.be.equal(sizeOfProperty(biggerProp) - expectedConsumedSpaceDiff);199 });200 }));201});202203describe('Negative Integration Test: Collection Properties', () => {204 let alice: IKeyringPair;205 let bob: IKeyringPair;206207 before(async () => {208 await usingPlaygrounds(async (helper, privateKey) => {209 const donor = await privateKey({filename: __filename});210 [alice, bob] = await helper.arrange.createAccounts([1000n, 100n], donor);211 });212 });213214 [215 {mode: 'nft' as const, requiredPallets: []},216 {mode: 'ft' as const, requiredPallets: []},217 {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},218 ].map(testSuite => describe(`${testSuite.mode.toUpperCase()}`, () => {219 before(async function() {220 // eslint-disable-next-line require-await221 await usingPlaygrounds(async helper => {222 requirePalletsOrSkip(this, helper, testSuite.requiredPallets);223 });224 });225226 itSub('Fails to set properties in a collection if not its onwer/administrator', async ({helper}) => {227 const collection = await helper[testSuite.mode].mintCollection(alice);228229 await expect(collection.setProperties(bob, [{key: 'electron', value: 'come bond'}, {key: 'black_hole', value: 'LIGO'}]))230 .to.be.rejectedWith(/common\.NoPermission/);231232 expect(await collection.getProperties()).to.be.empty;233 });234235 itSub('Fails to set properties that exceed the limits', async ({helper}) => {236 const collection = await helper[testSuite.mode].mintCollection(alice);237238 const spaceLimit = helper.getApi().consts.unique.maxCollectionPropertiesSize.toNumber();239240 // Mute the general tx parsing error, too many bytes to process241 {242 console.error = () => {};243 await expect(collection.setProperties(alice, [244 {key: 'electron', value: 'low high '.repeat(Math.ceil(spaceLimit! / 9))},245 ])).to.be.rejected;246 }247248 expect(await collection.getProperties(['electron'])).to.be.empty;249250 await expect(collection.setProperties(alice, [251 {key: 'electron', value: 'low high '.repeat(Math.ceil(spaceLimit! / 18))},252 {key: 'black_hole', value: '0'.repeat(Math.ceil(spaceLimit! / 2))},253 ])).to.be.rejectedWith(/common\.NoSpaceForProperty/);254255 expect(await collection.getProperties(['electron', 'black_hole'])).to.be.empty;256 });257258 itSub('Fails to set more properties than it is allowed', async ({helper}) => {259 const collection = await helper[testSuite.mode].mintCollection(alice);260261 const propertiesToBeSet = [];262 for (let i = 0; i < 65; i++) {263 propertiesToBeSet.push({264 key: 'electron_' + i,265 value: Math.random() > 0.5 ? 'high' : 'low',266 });267 }268269 await expect(collection.setProperties(alice, propertiesToBeSet)).270 to.be.rejectedWith(/common\.PropertyLimitReached/);271272 expect(await collection.getProperties()).to.be.empty;273 });274275 itSub('Fails to set properties with invalid names', async ({helper}) => {276 const collection = await helper[testSuite.mode].mintCollection(alice);277278 const invalidProperties = [279 [{key: 'electron', value: 'negative'}, {key: 'string theory', value: 'understandable'}],280 [{key: 'Mr/Sandman', value: 'Bring me a gene'}],281 [{key: 'déjà vu', value: 'hmm...'}],282 ];283284 for (let i = 0; i < invalidProperties.length; i++) {285 await expect(286 collection.setProperties(alice, invalidProperties[i]),287 `on rejecting the new badly-named property #${i}`,288 ).to.be.rejectedWith(/common\.InvalidCharacterInPropertyKey/);289 }290291 await expect(292 collection.setProperties(alice, [{key: '', value: 'nothing must not exist'}]),293 'on rejecting an unnamed property',294 ).to.be.rejectedWith(/common\.EmptyPropertyKey/);295296 await expect(297 collection.setProperties(alice, [{key: 'CRISPR-Cas9', value: 'rewriting nature!'}]),298 'on setting the correctly-but-still-badly-named property',299 ).to.be.fulfilled;300301 const keys = invalidProperties.flatMap(propertySet => propertySet.map(property => property.key)).concat('CRISPR-Cas9').concat('');302303 const properties = await collection.getProperties(keys);304 expect(properties).to.be.deep.equal([305 {key: 'CRISPR-Cas9', value: 'rewriting nature!'},306 ]);307308 for (let i = 0; i < invalidProperties.length; i++) {309 await expect(310 collection.deleteProperties(alice, invalidProperties[i].map(propertySet => propertySet.key)),311 `on trying to delete the non-existent badly-named property #${i}`,312 ).to.be.rejectedWith(/common\.InvalidCharacterInPropertyKey/);313 }314 });315316 itSub('Forbids to repair a collection if called with non-sudo', async({helper}) => {317 const collection = await helper[testSuite.mode].mintCollection(alice, {properties: [318 {key: 'sea-creatures', value: 'mermaids'},319 {key: 'goldenratio', value: '1.6180339887498948482045868343656381177203091798057628621354486227052604628189'},320 ]});321322 await expect(helper.executeExtrinsic(alice, 'api.tx.unique.forceRepairCollection', [collection.collectionId], true))323 .to.be.rejectedWith(/BadOrigin/);324 });325 }));326});tests/src/nesting/tokenProperties.seqtest.tsdiffbeforeafterboth--- a/tests/src/nesting/tokenProperties.seqtest.ts
+++ b/tests/src/nesting/tokenProperties.seqtest.ts
@@ -15,7 +15,7 @@
// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
import {IKeyringPair} from '@polkadot/types/types';
-import {itSub, Pallets, usingPlaygrounds, expect, requirePalletsOrSkip} from '../util';
+import {itSub, Pallets, usingPlaygrounds, expect, requirePalletsOrSkip, sizeOfProperty} from '../util';
describe('Integration Test: Token Properties with sudo', () => {
let superuser: IKeyringPair;
@@ -57,16 +57,15 @@
: collection.mintToken(alice)
);
- const propDataSize = 4096;
- const propData = 'a'.repeat(propDataSize);
+ const prop = {key: propKey, value: 'a'.repeat(4096)};
- await token.setProperties(alice, [{key: propKey, value: propData}]);
+ await token.setProperties(alice, [prop]);
const originalSpace = await token.getTokenPropertiesConsumedSpace();
- expect(originalSpace).to.be.equal(propDataSize);
+ expect(originalSpace).to.be.equal(sizeOfProperty(prop));
await helper.getSudo().executeExtrinsic(superuser, 'api.tx.unique.forceRepairItem', [token.collectionId, token.tokenId], true);
const recomputedSpace = await token.getTokenPropertiesConsumedSpace();
expect(recomputedSpace).to.be.equal(originalSpace);
});
}));
-});
\ No newline at end of file
+});
tests/src/nesting/tokenProperties.test.tsdiffbeforeafterboth--- a/tests/src/nesting/tokenProperties.test.ts
+++ b/tests/src/nesting/tokenProperties.test.ts
@@ -15,7 +15,7 @@
// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
import {IKeyringPair} from '@polkadot/types/types';
-import {itSub, Pallets, requirePalletsOrSkip, usingPlaygrounds, expect} from '../util';
+import {itSub, Pallets, requirePalletsOrSkip, usingPlaygrounds, expect, sizeOfProperty} from '../util';
import {UniqueHelper, UniqueNFToken, UniqueRFToken} from '../util/playgrounds/unique';
describe('Integration Test: Token Properties', () => {
@@ -353,9 +353,10 @@
: collection.mintToken(alice)
);
- await token.setProperties(alice, [{key: propKey, value: makeNewPropData()}]);
+ const property = {key: propKey, value: makeNewPropData()};
+ await token.setProperties(alice, [property]);
const originalSpace = await token.getTokenPropertiesConsumedSpace();
- expect(originalSpace).to.be.equal(propDataSize);
+ expect(originalSpace).to.be.equal(sizeOfProperty(property));
const sameSizePropertiesPossibleNum = maxTokenPropertiesSize / propDataSize;
@@ -393,9 +394,10 @@
const propDataSize = 4096;
const propData = 'a'.repeat(propDataSize);
- await token.setProperties(alice, [{key: propKey, value: propData}]);
+ const property = {key: propKey, value: propData};
+ await token.setProperties(alice, [property]);
let consumedSpace = await token.getTokenPropertiesConsumedSpace();
- expect(consumedSpace).to.be.equal(propDataSize);
+ expect(consumedSpace).to.be.equal(sizeOfProperty(property));
await token.deleteProperties(alice, [propKey]);
consumedSpace = await token.getTokenPropertiesConsumedSpace();
@@ -424,31 +426,27 @@
);
const originalSpace = await token.getTokenPropertiesConsumedSpace();
- const initPropDataSize = 4096;
- const biggerPropDataSize = 5000;
- const smallerPropDataSize = 4000;
-
- const initPropData = 'a'.repeat(initPropDataSize);
- const biggerPropData = 'b'.repeat(biggerPropDataSize);
- const smallerPropData = 'c'.repeat(smallerPropDataSize);
+ const initProp = {key: propKey, value: 'a'.repeat(4096)};
+ const biggerProp = {key: propKey, value: 'b'.repeat(5000)};
+ const smallerProp = {key: propKey, value: 'c'.repeat(4000)};
let consumedSpace;
let expectedConsumedSpaceDiff;
- await token.setProperties(alice, [{key: propKey, value: initPropData}]);
+ await token.setProperties(alice, [initProp]);
consumedSpace = await token.getTokenPropertiesConsumedSpace();
- expectedConsumedSpaceDiff = initPropDataSize - originalSpace;
+ expectedConsumedSpaceDiff = sizeOfProperty(initProp) - originalSpace;
expect(consumedSpace).to.be.equal(originalSpace + expectedConsumedSpaceDiff);
- await token.setProperties(alice, [{key: propKey, value: biggerPropData}]);
+ await token.setProperties(alice, [biggerProp]);
consumedSpace = await token.getTokenPropertiesConsumedSpace();
- expectedConsumedSpaceDiff = biggerPropDataSize - initPropDataSize;
- expect(consumedSpace).to.be.equal(initPropDataSize + expectedConsumedSpaceDiff);
+ expectedConsumedSpaceDiff = sizeOfProperty(biggerProp) - sizeOfProperty(initProp);
+ expect(consumedSpace).to.be.equal(sizeOfProperty(initProp) + expectedConsumedSpaceDiff);
- await token.setProperties(alice, [{key: propKey, value: smallerPropData}]);
+ await token.setProperties(alice, [smallerProp]);
consumedSpace = await token.getTokenPropertiesConsumedSpace();
- expectedConsumedSpaceDiff = biggerPropDataSize - smallerPropDataSize;
- expect(consumedSpace).to.be.equal(biggerPropDataSize - expectedConsumedSpaceDiff);
+ expectedConsumedSpaceDiff = sizeOfProperty(biggerProp) - sizeOfProperty(smallerProp);
+ expect(consumedSpace).to.be.equal(sizeOfProperty(biggerProp) - expectedConsumedSpaceDiff);
}));
});
tests/src/util/index.tsdiffbeforeafterboth--- a/tests/src/util/index.ts
+++ b/tests/src/util/index.ts
@@ -176,3 +176,26 @@
}
describeXCM.skip = (name: string, fn: (this: Mocha.Suite) => void) => describeXCM(name, fn, {skip: true});
+
+export function sizeOfInt(i: number) {
+ if (i < 0 || i > 0xffffffff) throw new Error('out of range');
+ if(i < 0b11_1111) {
+ return 1;
+ } else if (i < 0b11_1111_1111_1111) {
+ return 2;
+ } else if (i < 0b11_1111_1111_1111_1111_1111_1111_1111) {
+ return 4;
+ } else {
+ return 5;
+ }
+}
+
+const UTF8_ENCODER = new TextEncoder();
+export function sizeOfEncodedStr(v: string) {
+ const encoded = UTF8_ENCODER.encode(v);
+ return sizeOfInt(encoded.length) + encoded.length;
+}
+
+export function sizeOfProperty(prop: {key: string, value: string}) {
+ return sizeOfEncodedStr(prop.key) + sizeOfEncodedStr(prop.value);
+}