git.delta.rocks / unique-network / refs/commits / 25d2901b8a5d

difftreelog

source

tests/src/nesting/migration-check.test.ts6.4 KiBsourcehistory
1import {expect} from 'chai';2import usingApi, {executeTransaction, submitTransactionAsync} from '../substrate/substrate-api';3import {getCreateCollectionResult, getCreateItemResult, normalizeAccountId} from '../util/helpers';4import {IKeyringPair} from '@polkadot/types/types';5import {strToUTF16} from '../util/util';6import waitNewBlocks from '../substrate/wait-new-blocks';7// Used for polkadot-launch signalling8import find from 'find-process';910// todo un-skip for migrations11// todo:playgrounds skipped, this one is outdated. Probably to be deleted/replaced.12describe.skip('Migration testing: Properties', () => {13  let alice: IKeyringPair;1415  before(async() => {16    await usingApi(async (_, privateKeyWrapper) => {17      alice = privateKeyWrapper('//Alice');18    });19  });2021  it('Preserves collection settings after migration', async () => {22    let oldVersion: number;23    let collectionId: number;24    let collectionOld: any;25    let nftId: number;26    let nftOld: any;2728    await usingApi(async api => {29      // ----------- Collection pre-upgrade ------------30      const txCollection = api.tx.unique.createCollectionEx({31        mode: 'NFT',32        access: 'AllowList',33        name: strToUTF16('Mojave Pictures'),34        description: strToUTF16('$2.2 billion power plant!'),35        tokenPrefix: '0x0002030',36        offchainSchema: '0x111111',37        schemaVersion: 'Unique',38        limits: {39          accountTokenOwnershipLimit: 3,40        },41        constOnChainSchema: '0x333333',42        variableOnChainSchema: '0x22222',43      });44      const events0 = await submitTransactionAsync(alice, txCollection);45      const result0 = getCreateCollectionResult(events0);46      collectionId = result0.collectionId;4748      // Get the pre-upgrade collection info49      collectionOld = (await api.query.common.collectionById(collectionId)).toJSON();5051      // ---------- NFT pre-upgrade ------------52      const txNft = api.tx.unique.createItem(53        collectionId, 54        normalizeAccountId(alice), 55        {56          NFT: {57            owner: {substrate: alice.address},58            constData: '0x0000',59            variableData: '0x1111',60          },61        },62      );63      const events1 = await executeTransaction(api, alice, txNft);64      const result1 = getCreateItemResult(events1);65      nftId = result1.itemId;6667      // Get the pre-upgrade NFT data68      nftOld = (await api.query.nonfungible.tokenData(collectionId, nftId)).toJSON();6970      // Get the pre-upgrade spec version71      oldVersion = (api.consts.system.version.toJSON() as any).specVersion;72    });7374    console.log(`Now waiting for the parachain upgrade from ${oldVersion!}...`);7576    let newVersion = oldVersion!;77    let connectionFailCounter = 0;7879    // Cooperate with polkadot-launch if it's running (assuming custom name change to 'polkadot-launch'), and send a custom signal80    find('name', 'polkadot-launch', true).then((list) => {81      for (const proc of list) {82        process.kill(proc.pid, 'SIGUSR1');83      }84    });8586    // And wait for the parachain upgrade87    {88      // Catch warnings like 'RPC methods not decorated' and keep the 'waiting' message in front89      const stdlog = console.warn.bind(console);90      let warnCount = 0;91      console.warn = function(...args){92        if (arguments.length <= 2 || !args[2].includes('RPC methods not decorated')) {93          warnCount++;94          stdlog.apply(console, args as any);95        }96      };9798      let oldWarnCount = 0;99      while (newVersion == oldVersion! && connectionFailCounter < 5) {100        await new Promise(resolve => setTimeout(resolve, 12000));101        try {102          await usingApi(async api => {103            await waitNewBlocks(api);104            newVersion = (api.consts.system.version.toJSON() as any).specVersion;105            if (warnCount > oldWarnCount) {106              console.log(`Still waiting for the parachain upgrade from ${oldVersion!}...`);107              oldWarnCount = warnCount;108            }109          });110        } catch (_) {111          connectionFailCounter++;112        }113      }114    }115116    await usingApi(async api => {117      // ---------- Collection comparison -----------118      const collectionNew = (await api.query.common.collectionById(collectionId)).toJSON() as any;119120      // Make sure the extra fields are what they should be121      expect((122        await api.rpc.unique.collectionProperties(collectionId, ['_old_constOnChainSchema'])123      )[0].value.toHex()).to.be.equal(collectionOld.constOnChainSchema);124125      expect((126        await api.rpc.unique.collectionProperties(collectionId, ['_old_variableOnChainSchema'])127      )[0].value.toHex()).to.be.equal(collectionOld.variableOnChainSchema);128129      expect((130        await api.rpc.unique.collectionProperties(collectionId, ['_old_offchainSchema'])131      )[0].value.toHex()).to.be.equal(collectionOld.offchainSchema);132133      expect((134        await api.rpc.unique.collectionProperties(collectionId, ['_old_schemaVersion'])135      )[0].value.toHuman()).to.be.equal(collectionOld.schemaVersion);136137      expect(collectionNew.permissions).to.be.deep.equal({138        access: collectionOld.access,139        mintMode: collectionOld.mintMode,140        nesting: null,141      });142143      expect(collectionNew.externalCollection).to.be.equal(false);144145      // Get rid of extra fields to perform comparison on the rest of the collection146      delete collectionNew.permissions;147      delete collectionNew.externalCollection;148      delete collectionOld.schemaVersion;149      delete collectionOld.constOnChainSchema;150      delete collectionOld.variableOnChainSchema;151      delete collectionOld.offchainSchema;152      delete collectionOld.mintMode;153      delete collectionOld.access;154      delete collectionOld.metaUpdatePermission; // todo look into, doesn't migrate155156      expect(collectionNew).to.be.deep.equal(collectionOld);157158      // ---------- NFT comparison -----------159      const nftNew = (await api.query.nonfungible.tokenData(collectionId, nftId)).toJSON() as any;160161      // Make sure the extra fields are what they should be162      expect((await api.rpc.unique.tokenProperties(collectionId, nftId, ['_old_constData']))[0].value.toHex()).to.be.equal(nftOld.constData);163164      expect((await api.rpc.unique.tokenProperties(collectionId, nftId, ['_old_variableData']))[0].value.toHex()).to.be.equal(nftOld.variableData);165166      // Get rid of extra fields to perform comparison on the rest of the NFT167      delete nftOld.constData;168      delete nftOld.variableData;169170      expect(nftNew).to.be.deep.equal(nftOld);171    });172  });173});