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

difftreelog

tests(util): fix and adapt feed-alices to asynchronisity

Fahrrader2022-10-07parent: #add44eb.patch.diff
in: master

4 files changed

modifiedtests/package.jsondiffbeforeafterboth
--- a/tests/package.json
+++ b/tests/package.json
@@ -29,14 +29,14 @@
     "feedAlices": "ts-node ./src/util/playgrounds/feedAlices.ts",
 
     "test": "yarn feedAlices && mocha --parallel --timeout 9999999 -r ts-node/register './src/**/*.test.ts'",
+    "testSequential": "yarn feedAlices && mocha --timeout 9999999 -r ts-node/register './src/**/*.seqtest.ts'",
     "testStructure": "yarn feedAlices && mocha --parallel --timeout 9999999 -r ts-node/register ./**/nesting/**.test.ts",
     "testEth": "yarn feedAlices && mocha --parallel --timeout 9999999 -r ts-node/register './**/eth/**/*.test.ts'",
     "testEthNesting": "yarn feedAlices && mocha --parallel --timeout 9999999 -r ts-node/register './**/eth/nesting/**/*.test.ts'",
     "testEthFractionalizer": "yarn feedAlices && mocha --parallel --timeout 9999999 -r ts-node/register './**/eth/fractionalizer/**/*.test.ts'",
     "testEthMarketplace": "yarn feedAlices && mocha --parallel --timeout 9999999 -r ts-node/register './**/eth/marketplace/**/*.test.ts'",
-    "testCollision": "yarn feedAlices && mocha --parallel --timeout 9999999 -r ts-node/register ./src/collision-tests/*.test.ts",
     "testEvent": "yarn feedAlices && mocha --parallel --timeout 9999999 -r ts-node/register ./src/check-event/*.test.ts",
-    "testRmrk": "mocha --timeout 9999999 -r ts-node/register ./**/rmrk/**.test.ts",
+    "testRmrk": "mocha --timeout 9999999 -r ts-node/register ./**/rmrk/**test.ts",
 
     "testEthPayable": "mocha --timeout 9999999 -r ts-node/register './**/eth/payable.test.ts'",
     "testEthTokenProperties": "mocha --timeout 9999999 -r ts-node/register ./**/eth/tokenProperties.test.ts",
@@ -48,7 +48,7 @@
     "testSetCollectionLimits": "mocha --timeout 9999999 -r ts-node/register ./**/setCollectionLimits.test.ts",
     "testChangeCollectionOwner": "mocha --timeout 9999999 -r ts-node/register ./**/change-collection-owner.test.ts",
     "testSetCollectionSponsor": "mocha --timeout 9999999 -r ts-node/register ./**/setCollectionSponsor.test.ts",
-    "testConfirmSponsorship": "mocha --timeout 9999999 -r ts-node/register ./**/confirmSponsorship.test.ts",
+    "testConfirmSponsorship": "mocha --timeout 9999999 --parallel -r ts-node/register ./**/confirmSponsorship.test.ts",
     "testRemoveCollectionAdmin": "mocha --timeout 9999999 -r ts-node/register ./**/removeCollectionAdmin.test.ts",
     "testRemoveCollectionSponsor": "mocha --timeout 9999999 -r ts-node/register ./**/removeCollectionSponsor.test.ts",
     "testAllowLists": "mocha --timeout 9999999 -r ts-node/register ./**/allowLists.test.ts",
@@ -78,12 +78,6 @@
     "testInflation": "mocha --timeout 9999999 -r ts-node/register ./**/inflation.test.ts",
     "testScheduler": "mocha --timeout 9999999 -r ts-node/register ./**/scheduler.test.ts",
     "testSchedulingEVM": "mocha --timeout 9999999 -r ts-node/register ./**/eth/scheduling.test.ts",
-    "testXcmUnique": "RUN_XCM_TESTS=1 mocha --timeout 9999999 -r ts-node/register ./**/xcm/xcmUnique.test.ts",
-    "testXcmQuartz": "RUN_XCM_TESTS=1 mocha --timeout 9999999 -r ts-node/register ./**/xcm/xcmQuartz.test.ts",
-    "testXcmOpal": "RUN_XCM_TESTS=1 mocha --timeout 9999999 -r ts-node/register ./**/xcm/xcmOpal.test.ts",
-    "testXcmTransferAcala": "mocha --timeout 9999999 -r ts-node/register ./**/xcm/xcmTransferAcala.test.ts acalaId=2000 uniqueId=5000",
-    "testXcmTransferStatemine": "mocha --timeout 9999999 -r ts-node/register ./**/xcm/xcmTransferStatemine.test.ts statemineId=1000 uniqueId=5000",
-    "testXcmTransferMoonbeam": "mocha --timeout 9999999 -r ts-node/register ./**/xcm/xcmTransferMoonbeam.test.ts",
     "testPalletPresence": "mocha --timeout 9999999 -r ts-node/register ./**/pallet-presence.test.ts",
     "testBlockProduction": "mocha --timeout 9999999 -r ts-node/register ./**/block-production.test.ts",
     "testEnableDisableTransfers": "mocha --timeout 9999999 -r ts-node/register ./**/enableDisableTransfer.test.ts",
@@ -97,6 +91,13 @@
     "testEthFT": "mocha --timeout 9999999 -r ts-node/register ./**/eth/fungible.test.ts",
     "testRPC": "mocha --timeout 9999999 -r ts-node/register ./**/rpc.test.ts",
     "testPromotion": "mocha --timeout 9999999 -r ts-node/register ./**/app-promotion.test.ts",
+    
+    "testXcmUnique": "RUN_XCM_TESTS=1 mocha --timeout 9999999 -r ts-node/register ./**/xcm/xcmUnique.test.ts",
+    "testXcmQuartz": "RUN_XCM_TESTS=1 mocha --timeout 9999999 -r ts-node/register ./**/xcm/xcmQuartz.test.ts",
+    "testXcmOpal": "RUN_XCM_TESTS=1 mocha --timeout 9999999 -r ts-node/register ./**/xcm/xcmOpal.test.ts",
+    "testXcmTransferAcala": "mocha --timeout 9999999 -r ts-node/register ./**/xcm/xcmTransferAcala.test.ts acalaId=2000 uniqueId=5000",
+    "testXcmTransferStatemine": "mocha --timeout 9999999 -r ts-node/register ./**/xcm/xcmTransferStatemine.test.ts statemineId=1000 uniqueId=5000",
+    "testXcmTransferMoonbeam": "mocha --timeout 9999999 -r ts-node/register ./**/xcm/xcmTransferMoonbeam.test.ts",
 
     "load": "mocha --timeout 9999999 -r ts-node/register './**/*.load.ts'",
     "loadTransfer": "ts-node src/transfer.nload.ts",
modifiedtests/src/eth/util/playgrounds/unique.dev.tsdiffbeforeafterboth
--- a/tests/src/eth/util/playgrounds/unique.dev.ts
+++ b/tests/src/eth/util/playgrounds/unique.dev.ts
@@ -142,14 +142,14 @@
     return account.address;
   }
 
-  async createAccountWithBalance(donor: IKeyringPair, amount=1000n) {
+  async createAccountWithBalance(donor: IKeyringPair, amount=100n) {
     const account = this.createAccount();
     await this.transferBalanceFromSubstrate(donor, account, amount);
   
     return account;
   }
 
-  async transferBalanceFromSubstrate(donor: IKeyringPair, recepient: string, amount=1000n, inTokens=true) {
+  async transferBalanceFromSubstrate(donor: IKeyringPair, recepient: string, amount=100n, inTokens=true) {
     return await this.helper.balance.transferToSubstrate(donor, evmToAddress(recepient), amount * (inTokens ? this.helper.balance.getOneTokenNominal() : 1n));
   }
   
modifiedtests/src/util/playgrounds/feedAlices.tsdiffbeforeafterboth
--- a/tests/src/util/playgrounds/feedAlices.ts
+++ b/tests/src/util/playgrounds/feedAlices.ts
@@ -26,23 +26,23 @@
     const nonce = await helper.chain.getNonce(alice.address);
     const filenames = await getFiles(path.resolve(__dirname, '../..'));
 
-    console.log(`Main Alice address: ${alice.address}, with balance:`, await helper.balance.getSubstrate(alice.address));
-
-    const batchSize = 20;
+    // batching is actually undesired, it takes away the time while all the transactions actually succeed
+    const batchSize = 300;
     let balanceGrantedCounter = 0;
     for (let b = 0; b < filenames.length; b += batchSize) {
       const tx = [];
       let batchBalanceGrantedCounter = 0;
-      for (const f of filenames.slice(b, b + batchSize)) {
-        if (!f.endsWith('.test.ts')) continue;
-        const account = await privateKey({filename: f});
+      for (let i = 0; batchBalanceGrantedCounter < batchSize && b + i < filenames.length; i++) {
+        const f = filenames[b + i];
+        if (!f.endsWith('.test.ts') || f.includes('.outdated')) continue;
+        const account = await privateKey({filename: f, ignoreFundsPresence: true});
         const aliceBalance = await helper.balance.getSubstrate(account.address);
 
-        if (aliceBalance < 5000n * oneToken) {
+        if (aliceBalance < 7500n * oneToken) {
           tx.push(helper.executeExtrinsic(
             alice, 
             'api.tx.balances.transfer',
-            [account.address, 10000n * oneToken],
+            [account.address, 15000n * oneToken],
             true,
             {nonce: nonce + balanceGrantedCounter++},
           ).then(() => true).catch(() => {console.error(`Transaction to ${path.basename(f)} registered as failed. Strange.`); return false;}));
@@ -65,13 +65,14 @@
   if (retriesLeft <= 0) return Promise.resolve(false);
   return fundFilenames()
     .then(() => Promise.resolve(true))
-    .catch(() => {
-      console.error(`Some transactions have failed. ${retriesLeft >= 1 ? 'Retrying...' : 'Something is wrong.'}`);
+    .catch(e => {
+      console.error(e);
+      console.error(`Some transactions might have failed. ${retriesLeft > 1 ? 'Retrying...' : 'Something is wrong.'}\n`);
       return fundFilenamesWithRetries(--retriesLeft);
     });
 };
 
-fundFilenamesWithRetries(2).then((result) => process.exit(result ? 0 : 1)).catch(e => {
+fundFilenamesWithRetries(3).then((result) => process.exit(result ? 0 : 1)).catch(e => {
   console.error(e);
   process.exit(1);
 });
modifiedtests/src/util/playgrounds/index.tsdiffbeforeafterboth
before · tests/src/util/playgrounds/index.ts
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// SPDX-License-Identifier: Apache-2.034import * as path from 'path';5import * as crypto from 'crypto';6import {IKeyringPair} from '@polkadot/types/types';7import chai from 'chai';8import chaiAsPromised from 'chai-as-promised';9import {Context} from 'mocha';10import config from '../../config';11import '../../interfaces/augment-api-events';12import {DevUniqueHelper, SilentLogger, SilentConsole} from './unique.dev';1314chai.use(chaiAsPromised);15export const expect = chai.expect;1617const getTestHash = (filename: string) => {18  return crypto.createHash('md5').update(path.basename(filename)).digest('hex');19};2021export const getTestSeed = (filename: string) => {22  return `//Alice+${getTestHash(filename)}`;23};2425// todo:playgrounds normalize to seed and filename26export const usingPlaygrounds = async (code: (helper: DevUniqueHelper, privateKey: (seed: string | {filename: string}) => Promise<IKeyringPair>) => Promise<void>, url: string = config.substrateUrl) => {27  const silentConsole = new SilentConsole();28  silentConsole.enable();2930  const helper = new DevUniqueHelper(new SilentLogger());3132  try {33    await helper.connect(url);34    const ss58Format = helper.chain.getChainProperties().ss58Format;35    const privateKey = async (seed: string | {filename: string}) => {36      if (typeof seed === 'string') {37        return helper.util.fromSeed(seed, ss58Format);38      }39      else {40        const actualSeed = getTestSeed(seed.filename);41        let account = helper.util.fromSeed(actualSeed, ss58Format);42        if (await helper.balance.getSubstrate(account.address) == 0n) {43          console.warn(`${path.basename(seed.filename)}: Not enough funds present on the filename account. Using the default one as the donor instead.`);44          account = helper.util.fromSeed('//Alice', ss58Format);45        }46        return account;47      }48    };49    await code(helper, privateKey);50  }51  finally {52    await helper.disconnect();53    silentConsole.disable();54  }55};5657export enum Pallets {58  Inflation = 'inflation',59  RmrkCore = 'rmrkcore',60  RmrkEquip = 'rmrkequip',61  ReFungible = 'refungible',62  Fungible = 'fungible',63  NFT = 'nonfungible',64  Scheduler = 'scheduler',65  AppPromotion = 'apppromotion',66}6768export function requirePalletsOrSkip(test: Context, helper: DevUniqueHelper, requiredPallets: string[]) {69  const missingPallets = helper.fetchMissingPalletNames(requiredPallets);70    71  if (missingPallets.length > 0) {72    const skipMsg = `\tSkipping test '${test.test?.title}'.\n\tThe following pallets are missing:\n\t- ${missingPallets.join('\n\t- ')}`;73    console.warn('\x1b[38:5:208m%s\x1b[0m', skipMsg);74    test.skip();75  }76}7778export async function itSub(name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {79  (opts.only ? it.only : 80    opts.skip ? it.skip : it)(name, async function () {81    await usingPlaygrounds(async (helper, privateKey) => {82      if (opts.requiredPallets) {83        requirePalletsOrSkip(this, helper, opts.requiredPallets);84      }85      86      await cb({helper, privateKey});87    });88  });89}90export async function itSubIfWithPallet(name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {91  return itSub(name, cb, {requiredPallets: required, ...opts});92}93itSub.only = (name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSub(name, cb, {only: true});94itSub.skip = (name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSub(name, cb, {skip: true});9596itSubIfWithPallet.only = (name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSubIfWithPallet(name, required, cb, {only: true});97itSubIfWithPallet.skip = (name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSubIfWithPallet(name, required, cb, {skip: true});98itSub.ifWithPallets = itSubIfWithPallet;
after · tests/src/util/playgrounds/index.ts
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// SPDX-License-Identifier: Apache-2.034import * as path from 'path';5import * as crypto from 'crypto';6import {IKeyringPair} from '@polkadot/types/types';7import chai from 'chai';8import chaiAsPromised from 'chai-as-promised';9import {Context} from 'mocha';10import config from '../../config';11import '../../interfaces/augment-api-events';12import {DevUniqueHelper, SilentLogger, SilentConsole} from './unique.dev';1314chai.use(chaiAsPromised);15export const expect = chai.expect;1617const getTestHash = (filename: string) => {18  return crypto.createHash('md5').update(path.basename(filename)).digest('hex');19};2021export const getTestSeed = (filename: string) => {22  return `//Alice+${getTestHash(filename)}`;23};2425export const usingPlaygrounds = async (code: (helper: DevUniqueHelper, privateKey: (seed: string | {filename: string, ignoreFundsPresence?: boolean}) => Promise<IKeyringPair>) => Promise<void>, url: string = config.substrateUrl) => {26  const silentConsole = new SilentConsole();27  silentConsole.enable();2829  const helper = new DevUniqueHelper(new SilentLogger());3031  try {32    await helper.connect(url);33    const ss58Format = helper.chain.getChainProperties().ss58Format;34    const privateKey = async (seed: string | {filename: string, ignoreFundsPresence?: boolean}) => {35      if (typeof seed === 'string') {36        return helper.util.fromSeed(seed, ss58Format);37      }38      else {39        const actualSeed = getTestSeed(seed.filename);40        let account = helper.util.fromSeed(actualSeed, ss58Format);41        if (!seed.ignoreFundsPresence && await helper.balance.getSubstrate(account.address) == 0n) {42          console.warn(`${path.basename(seed.filename)}: Not enough funds present on the filename account. Using the default one as the donor instead.`);43          account = helper.util.fromSeed('//Alice', ss58Format);44        }45        return account;46      }47    };48    await code(helper, privateKey);49  }50  finally {51    await helper.disconnect();52    silentConsole.disable();53  }54};5556export enum Pallets {57  Inflation = 'inflation',58  RmrkCore = 'rmrkcore',59  RmrkEquip = 'rmrkequip',60  ReFungible = 'refungible',61  Fungible = 'fungible',62  NFT = 'nonfungible',63  Scheduler = 'scheduler',64  AppPromotion = 'apppromotion',65}6667export function requirePalletsOrSkip(test: Context, helper: DevUniqueHelper, requiredPallets: string[]) {68  const missingPallets = helper.fetchMissingPalletNames(requiredPallets);69    70  if (missingPallets.length > 0) {71    const skipMsg = `\tSkipping test '${test.test?.title}'.\n\tThe following pallets are missing:\n\t- ${missingPallets.join('\n\t- ')}`;72    console.warn('\x1b[38:5:208m%s\x1b[0m', skipMsg);73    test.skip();74  }75}7677export async function itSub(name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {78  (opts.only ? it.only : 79    opts.skip ? it.skip : it)(name, async function () {80    await usingPlaygrounds(async (helper, privateKey) => {81      if (opts.requiredPallets) {82        requirePalletsOrSkip(this, helper, opts.requiredPallets);83      }84      85      await cb({helper, privateKey});86    });87  });88}89export async function itSubIfWithPallet(name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any, opts: { only?: boolean, skip?: boolean, requiredPallets?: string[] } = {}) {90  return itSub(name, cb, {requiredPallets: required, ...opts});91}92itSub.only = (name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSub(name, cb, {only: true});93itSub.skip = (name: string, cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSub(name, cb, {skip: true});9495itSubIfWithPallet.only = (name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSubIfWithPallet(name, required, cb, {only: true});96itSubIfWithPallet.skip = (name: string, required: string[], cb: (apis: { helper: DevUniqueHelper, privateKey: (seed: string) => Promise<IKeyringPair> }) => any) => itSubIfWithPallet(name, required, cb, {skip: true});97itSub.ifWithPallets = itSubIfWithPallet;