difftreelog
test update property size calculation
in: master
5 files changed
tests/src/nesting/collectionProperties.seqtest.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 with sudo', () => {21 let superuser: IKeyringPair;22 let alice: IKeyringPair;2324 before(async () => {25 await usingPlaygrounds(async (helper, privateKey) => {26 superuser = await privateKey('//Alice');27 const donor = await privateKey({filename: __filename});28 [alice] = await helper.arrange.createAccounts([100n], donor);29 });30 });3132 [33 {mode: 'nft' as const, requiredPallets: []},34 {mode: 'ft' as const, requiredPallets: []},35 {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},36 ].map(testSuite => describe(`${testSuite.mode.toUpperCase()}`, () => {37 before(async function() {38 // eslint-disable-next-line require-await39 await usingPlaygrounds(async helper => {40 requirePalletsOrSkip(this, helper, testSuite.requiredPallets);41 });42 });4344 itSub('Repairing an unbroken collection\'s properties preserves the consumed space', async({helper}) => {45 const properties = [46 {key: 'sea-creatures', value: 'mermaids'},47 {key: 'goldenratio', value: '1.6180339887498948482045868343656381177203091798057628621354486227052604628189'},48 ];49 const collection = await helper[testSuite.mode].mintCollection(alice, {properties});5051 const newProperty = ' '.repeat(4096);52 await collection.setProperties(alice, [{key: 'space', value: newProperty}]);53 const originalSpace = await collection.getPropertiesConsumedSpace();54 expect(originalSpace).to.be.equal(properties[0].value.length + properties[1].value.length + newProperty.length);5556 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.unique.forceRepairCollection', [collection.collectionId], true);57 const recomputedSpace = await collection.getPropertiesConsumedSpace();58 expect(recomputedSpace).to.be.equal(originalSpace);59 });60 }));61});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 with sudo', () => {21 let superuser: IKeyringPair;22 let alice: IKeyringPair;2324 before(async () => {25 await usingPlaygrounds(async (helper, privateKey) => {26 superuser = await privateKey('//Alice');27 const donor = await privateKey({filename: __filename});28 [alice] = await helper.arrange.createAccounts([100n], donor);29 });30 });3132 [33 {mode: 'nft' as const, requiredPallets: []},34 {mode: 'ft' as const, requiredPallets: []},35 {mode: 'rft' as const, requiredPallets: [Pallets.ReFungible]},36 ].map(testSuite => describe(`${testSuite.mode.toUpperCase()}`, () => {37 before(async function() {38 // eslint-disable-next-line require-await39 await usingPlaygrounds(async helper => {40 requirePalletsOrSkip(this, helper, testSuite.requiredPallets);41 });42 });4344 itSub('Repairing an unbroken collection\'s properties preserves the consumed space', async({helper}) => {45 const properties = [46 {key: 'sea-creatures', value: 'mermaids'},47 {key: 'goldenratio', value: '1.6180339887498948482045868343656381177203091798057628621354486227052604628189'},48 ];49 const collection = await helper[testSuite.mode].mintCollection(alice, {properties});5051 const newProperty = {key: 'space', value: ' '.repeat(4096)};52 await collection.setProperties(alice, [newProperty]);53 const originalSpace = await collection.getPropertiesConsumedSpace();54 expect(originalSpace).to.be.equal(sizeOfProperty(properties[0]) + sizeOfProperty(properties[1]) + sizeOfProperty(newProperty));5556 await helper.getSudo().executeExtrinsic(superuser, 'api.tx.unique.forceRepairCollection', [collection.collectionId], true);57 const recomputedSpace = await collection.getPropertiesConsumedSpace();58 expect(recomputedSpace).to.be.equal(originalSpace);59 });60 }));61});tests/src/nesting/collectionProperties.test.tsdiffbeforeafterboth--- a/tests/src/nesting/collectionProperties.test.ts
+++ b/tests/src/nesting/collectionProperties.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, usingPlaygrounds, expect, requirePalletsOrSkip} from '../util';
+import {itSub, Pallets, usingPlaygrounds, expect, requirePalletsOrSkip, sizeOfProperty} from '../util';
describe('Integration Test: Collection Properties', () => {
let alice: IKeyringPair;
@@ -134,9 +134,10 @@
return `${propDataChar}`.repeat(propDataSize);
};
- await collection.setProperties(alice, [{key: propKey, value: makeNewPropData()}]);
+ const property = {key: propKey, value: makeNewPropData()};
+ await collection.setProperties(alice, [property]);
const originalSpace = await collection.getPropertiesConsumedSpace();
- expect(originalSpace).to.be.equal(propDataSize);
+ expect(originalSpace).to.be.equal(sizeOfProperty(property));
const sameSizePropertiesPossibleNum = maxCollectionPropertiesSize / propDataSize;
@@ -158,9 +159,10 @@
const propDataSize = 4096;
const propData = 'a'.repeat(propDataSize);
- await collection.setProperties(alice, [{key: propKey, value: propData}]);
+ const property = {key: propKey, value: propData};
+ await collection.setProperties(alice, [property]);
let consumedSpace = await collection.getPropertiesConsumedSpace();
- expect(consumedSpace).to.be.equal(propDataSize);
+ expect(consumedSpace).to.be.equal(sizeOfProperty(property));
await collection.deleteProperties(alice, [propKey]);
consumedSpace = await collection.getPropertiesConsumedSpace();
@@ -173,31 +175,27 @@
const collection = await helper[testSuite.mode].mintCollection(alice);
const originalSpace = await collection.getPropertiesConsumedSpace();
- 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 collection.setProperties(alice, [{key: propKey, value: initPropData}]);
+ await collection.setProperties(alice, [initProp]);
consumedSpace = await collection.getPropertiesConsumedSpace();
- expectedConsumedSpaceDiff = initPropDataSize - originalSpace;
+ expectedConsumedSpaceDiff = sizeOfProperty(initProp) - originalSpace;
expect(consumedSpace).to.be.equal(originalSpace + expectedConsumedSpaceDiff);
- await collection.setProperties(alice, [{key: propKey, value: biggerPropData}]);
+ await collection.setProperties(alice, [biggerProp]);
consumedSpace = await collection.getPropertiesConsumedSpace();
- expectedConsumedSpaceDiff = biggerPropDataSize - initPropDataSize;
- expect(consumedSpace).to.be.equal(initPropDataSize + expectedConsumedSpaceDiff);
+ expectedConsumedSpaceDiff = sizeOfProperty(biggerProp) - sizeOfProperty(initProp);
+ expect(consumedSpace).to.be.equal(sizeOfProperty(initProp) + expectedConsumedSpaceDiff);
- await collection.setProperties(alice, [{key: propKey, value: smallerPropData}]);
+ await collection.setProperties(alice, [smallerProp]);
consumedSpace = await collection.getPropertiesConsumedSpace();
- expectedConsumedSpaceDiff = biggerPropDataSize - smallerPropDataSize;
- expect(consumedSpace).to.be.equal(biggerPropDataSize - expectedConsumedSpaceDiff);
+ expectedConsumedSpaceDiff = sizeOfProperty(biggerProp) - sizeOfProperty(smallerProp);
+ expect(consumedSpace).to.be.equal(sizeOfProperty(biggerProp) - expectedConsumedSpaceDiff);
});
}));
});
@@ -237,7 +235,7 @@
itSub('Fails to set properties that exceed the limits', async ({helper}) => {
const collection = await helper[testSuite.mode].mintCollection(alice);
- const spaceLimit = (await (collection.helper!.api! as any).query.common.collectionProperties(collection.collectionId)).spaceLimit.toNumber();
+ const spaceLimit = helper.getApi().consts.unique.maxCollectionPropertiesSize.toNumber();
// Mute the general tx parsing error, too many bytes to process
{
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);
+}