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

difftreelog

source

tests/src/nesting/migration-check.test.ts6.3 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 migrations11describe.skip('Migration testing', () => {12  let alice: IKeyringPair;1314  before(async() => {15    await usingApi(async (_, privateKeyWrapper) => {16      alice = privateKeyWrapper('//Alice');17    });18  });1920  it('Preserves collection settings after migration', async () => {21    let oldVersion: number;22    let collectionId: number;23    let collectionOld: any;24    let nftId: number;25    let nftOld: any;2627    await usingApi(async api => {28      // ----------- Collection pre-upgrade ------------29      const txCollection = api.tx.unique.createCollectionEx({30        mode: 'NFT',31        access: 'AllowList',32        name: strToUTF16('Mojave Pictures'),33        description: strToUTF16('$2.2 billion power plant!'),34        tokenPrefix: '0x0002030',35        offchainSchema: '0x111111',36        schemaVersion: 'Unique',37        limits: {38          accountTokenOwnershipLimit: 3,39        },40        constOnChainSchema: '0x333333',41        variableOnChainSchema: '0x22222',42      });43      const events0 = await submitTransactionAsync(alice, txCollection);44      const result0 = getCreateCollectionResult(events0);45      collectionId = result0.collectionId;4647      // Get the pre-upgrade collection info48      collectionOld = (await api.query.common.collectionById(collectionId)).toJSON();4950      // ---------- NFT pre-upgrade ------------51      const txNft = api.tx.unique.createItem(52        collectionId, 53        normalizeAccountId(alice), 54        {55          NFT: {56            owner: {substrate: alice.address},57            constData: '0x0000',58            variableData: '0x1111',59          },60        },61      );62      const events1 = await executeTransaction(api, alice, txNft);63      const result1 = getCreateItemResult(events1);64      nftId = result1.itemId;6566      // Get the pre-upgrade NFT data67      nftOld = (await api.query.nonfungible.tokenData(collectionId, nftId)).toJSON();6869      // Get the pre-upgrade spec version70      oldVersion = (api.consts.system.version.toJSON() as any).specVersion;71    });7273    console.log(`Now waiting for the parachain upgrade from ${oldVersion!}...`);7475    let newVersion = oldVersion!;76    let connectionFailCounter = 0;7778    // Cooperate with polkadot-launch if it's running (assuming custom name change to 'polkadot-launch'), and send a custom signal79    find('name', 'polkadot-launch', true).then((list) => {80      for (const proc of list) {81        process.kill(proc.pid, 'SIGUSR1');82      }83    });8485    // And wait for the parachain upgrade86    {87      // Catch warnings like 'RPC methods not decorated' and keep the 'waiting' message in front88      const stdlog = console.warn.bind(console);89      let warnCount = 0;90      console.warn = function(...args){91        if (arguments.length <= 2 || !args[2].includes('RPC methods not decorated')) {92          warnCount++;93          stdlog.apply(console, args as any);94        }95      };9697      let oldWarnCount = 0;98      while (newVersion == oldVersion! && connectionFailCounter < 5) {99        await new Promise(resolve => setTimeout(resolve, 12000));100        try {101          await usingApi(async api => {102            await waitNewBlocks(api);103            newVersion = (api.consts.system.version.toJSON() as any).specVersion;104            if (warnCount > oldWarnCount) {105              console.log(`Still waiting for the parachain upgrade from ${oldVersion!}...`);106              oldWarnCount = warnCount;107            }108          });109        } catch (_) {110          connectionFailCounter++;111        }112      }113    }114115    await usingApi(async api => {116      // ---------- Collection comparison -----------117      const collectionNew = (await api.query.common.collectionById(collectionId)).toJSON() as any;118119      // Make sure the extra fields are what they should be120      expect((121        await api.rpc.unique.collectionProperties(collectionId, ['_old_constOnChainSchema'])122      )[0].value.toHex()).to.be.equal(collectionOld.constOnChainSchema);123124      expect((125        await api.rpc.unique.collectionProperties(collectionId, ['_old_variableOnChainSchema'])126      )[0].value.toHex()).to.be.equal(collectionOld.variableOnChainSchema);127128      expect((129        await api.rpc.unique.collectionProperties(collectionId, ['_old_offchainSchema'])130      )[0].value.toHex()).to.be.equal(collectionOld.offchainSchema);131132      expect((133        await api.rpc.unique.collectionProperties(collectionId, ['_old_schemaVersion'])134      )[0].value.toHuman()).to.be.equal(collectionOld.schemaVersion);135136      expect(collectionNew.permissions).to.be.deep.equal({137        access: collectionOld.access,138        mintMode: collectionOld.mintMode,139        nesting: null,140      });141142      expect(collectionNew.externalCollection).to.be.equal(false);143144      // Get rid of extra fields to perform comparison on the rest of the collection145      delete collectionNew.permissions;146      delete collectionNew.externalCollection;147      delete collectionOld.schemaVersion;148      delete collectionOld.constOnChainSchema;149      delete collectionOld.variableOnChainSchema;150      delete collectionOld.offchainSchema;151      delete collectionOld.mintMode;152      delete collectionOld.access;153      delete collectionOld.metaUpdatePermission; // todo look into, doesn't migrate154155      expect(collectionNew).to.be.deep.equal(collectionOld);156157      // ---------- NFT comparison -----------158      const nftNew = (await api.query.nonfungible.tokenData(collectionId, nftId)).toJSON() as any;159160      // Make sure the extra fields are what they should be161      expect((await api.rpc.unique.tokenProperties(collectionId, nftId, ['_old_constData']))[0].value.toHex()).to.be.equal(nftOld.constData);162163      expect((await api.rpc.unique.tokenProperties(collectionId, nftId, ['_old_variableData']))[0].value.toHex()).to.be.equal(nftOld.variableData);164165      // Get rid of extra fields to perform comparison on the rest of the NFT166      delete nftOld.constData;167      delete nftOld.variableData;168169      expect(nftNew).to.be.deep.equal(nftOld);170    });171  });172});