git.delta.rocks / unique-network / refs/commits / 7495d2ae3303

difftreelog

tests(maintenance)

Fahrrader2022-11-07parent: #e9b69c5.patch.diff
in: master

3 files changed

modifiedtests/package.jsondiffbeforeafterboth
--- a/tests/package.json
+++ b/tests/package.json
@@ -70,7 +70,7 @@
     "testBurnItem": "mocha --timeout 9999999 -r ts-node/register ./**/burnItem.test.ts",
     "testAdminTransferAndBurn": "mocha --timeout 9999999 -r ts-node/register ./**/adminTransferAndBurn.test.ts",
     "testSetPermissions": "mocha --timeout 9999999 -r ts-node/register ./**/setPermissions.test.ts",
-    "testCreditFeesToTreasury": "mocha --timeout 9999999 -r ts-node/register ./**/creditFeesToTreasury.test.ts",
+    "testCreditFeesToTreasury": "mocha --timeout 9999999 -r ts-node/register ./**/creditFeesToTreasury.seqtest.ts",
     "testContractSponsoring": "mocha --timeout 9999999 -r ts-node/register ./**/eth/contractSponsoring.test.ts",
     "testEnableContractSponsoring": "mocha --timeout 9999999 -r ts-node/register ./**/enableContractSponsoring.test.ts",
     "testRemoveFromContractAllowList": "mocha --timeout 9999999 -r ts-node/register ./**/removeFromContractAllowList.test.ts",
@@ -78,8 +78,9 @@
     "testSetOffchainSchema": "mocha --timeout 9999999 -r ts-node/register ./**/setOffchainSchema.test.ts",
     "testNextSponsoring": "mocha --timeout 9999999 -r ts-node/register ./**/nextSponsoring.test.ts",
     "testOverflow": "mocha --timeout 9999999 -r ts-node/register ./**/overflow.test.ts",
-    "testInflation": "mocha --timeout 9999999 -r ts-node/register ./**/inflation.test.ts",
-    "testScheduler": "mocha --timeout 9999999 -r ts-node/register ./**/scheduler.test.ts",
+    "testMaintenance": "mocha --timeout 9999999 -r ts-node/register ./**/maintenanceMode.seqtest.ts",
+    "testInflation": "mocha --timeout 9999999 -r ts-node/register ./**/inflation.seqtest.ts",
+    "testScheduler": "mocha --timeout 9999999 -r ts-node/register ./**/scheduler.seqtest.ts",
     "testSchedulingEVM": "mocha --timeout 9999999 -r ts-node/register ./**/eth/scheduling.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",
@@ -93,7 +94,7 @@
     "testFT": "mocha --timeout 9999999 -r ts-node/register ./**/fungible.test.ts",
     "testEthFT": "mocha --timeout 9999999 -r ts-node/register ./**/eth/fungible.test.ts",
     "testRPC": "mocha --timeout 9999999 -r ts-node/register ./**/rpc.test.ts",
-    "testPromotion": "yarn setup && mocha --timeout 9999999 -r ts-node/register ./**/app-promotion.test.ts",
+    "testPromotion": "yarn setup && 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",
addedtests/src/maintenanceMode.seqtest.tsdiffbeforeafterboth
--- /dev/null
+++ b/tests/src/maintenanceMode.seqtest.ts
@@ -0,0 +1,264 @@
+// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.
+// This file is part of Unique Network.
+
+// Unique Network is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+
+// Unique Network is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+
+// You should have received a copy of the GNU General Public License
+// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.
+
+import {IKeyringPair} from '@polkadot/types/types';
+import {ApiPromise} from '@polkadot/api';
+import {expect, itSub, Pallets, usingPlaygrounds} from './util';
+import {itEth} from './eth/util';
+
+async function maintenanceEnabled(api: ApiPromise): Promise<boolean> {
+  return (await api.query.maintenance.enabled()).toJSON() as boolean;
+}
+
+describe('Integration Test: Maintenance Mode', () => {
+  let superuser: IKeyringPair;
+  let donor: IKeyringPair;
+  let bob: IKeyringPair;
+
+  before(async () => {
+    await usingPlaygrounds(async (helper, privateKey) => {
+      superuser = await privateKey('//Alice');
+      donor = await privateKey({filename: __filename});
+      [bob] = await helper.arrange.createAccounts([100n], donor);
+
+      if (await maintenanceEnabled(helper.getApi())) {
+        console.warn('\tMaintenance mode was left enabled BEFORE the test suite! Disabling it now.');
+        await expect(helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.disable', [])).to.be.fulfilled;
+      }
+    });
+  });
+
+  itSub('Allows superuser to enable and disable maintenance mode - and disallows anyone else', async ({helper}) => {
+    // Make sure non-sudo can't enable maintenance mode
+    await expect(helper.executeExtrinsic(superuser, 'api.tx.maintenance.enable', []), 'on commoner enabling MM').to.be.rejected; //With(/NoPermission/);
+
+    // Set maintenance mode
+    await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.enable', []);
+    expect(await maintenanceEnabled(helper.getApi()), 'MM is OFF when it should be ON').to.be.true;
+
+    // Make sure non-sudo can't disable maintenance mode
+    await expect(helper.executeExtrinsic(bob, 'api.tx.maintenance.disable', []), 'on commoner disabling MM').to.be.rejected; //With(/NoPermission/);
+
+    // Disable maintenance mode
+    await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.disable', []);
+    expect(await maintenanceEnabled(helper.getApi()), 'MM is ON when it should be OFF').to.be.false;
+  });
+
+  itSub('MM blocks unique pallet calls', async ({helper}) => {
+    // Can create an NFT collection before enabling the MM
+    const nftCollection = await helper.nft.mintCollection(bob, {
+      tokenPropertyPermissions: [{key: 'test', permission: {
+        collectionAdmin: true,
+        tokenOwner: true,
+        mutable: true,
+      }}],
+    });
+
+    // Can mint an NFT before enabling the MM
+    const nft = await nftCollection.mintToken(bob);
+
+    // Can create an FT collection before enabling the MM
+    const ftCollection = await helper.ft.mintCollection(superuser);
+
+    // Can mint an FT before enabling the MM
+    await expect(ftCollection.mint(superuser)).to.be.fulfilled;
+
+    await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.enable', []);
+    expect(await maintenanceEnabled(helper.getApi()), 'MM is OFF when it should be ON').to.be.true;
+
+    // Unable to create a collection when the MM is enabled
+    await expect(helper.nft.mintCollection(superuser), 'cudo forbidden stuff').to.be.rejected;
+
+    // Unable to set token properties when the MM is enabled
+    await expect(nft.setProperties(
+      bob,
+      [{key: 'test', value: 'test-val'}],
+    )).to.be.rejected;
+
+    // Unable to mint an NFT when the MM is enabled
+    await expect(nftCollection.mintToken(superuser)).to.be.rejected;
+
+    // Unable to mint an FT when the MM is enabled
+    await expect(ftCollection.mint(superuser)).to.be.rejected;
+
+    await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.disable', []);
+    expect(await maintenanceEnabled(helper.getApi()), 'MM is ON when it should be OFF').to.be.false;
+
+    // Can create a collection after disabling the MM
+    await expect(helper.nft.mintCollection(bob), 'MM is disabled, the collection should be created').to.be.fulfilled;
+
+    // Can set token properties after disabling the MM
+    await nft.setProperties(bob, [{key: 'test', value: 'test-val'}]);
+
+    // Can mint an NFT after disabling the MM
+    await nftCollection.mintToken(bob);
+
+    // Can mint an FT after disabling the MM
+    await ftCollection.mint(superuser);
+  });
+
+  itSub.ifWithPallets('MM blocks unique pallet calls (Re-Fungible)', [Pallets.ReFungible], async ({helper}) => {
+    // Can create an RFT collection before enabling the MM
+    const rftCollection = await helper.rft.mintCollection(superuser);
+
+    // Can mint an RFT before enabling the MM
+    await expect(rftCollection.mintToken(superuser)).to.be.fulfilled;
+
+    await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.enable', []);
+    expect(await maintenanceEnabled(helper.getApi()), 'MM is OFF when it should be ON').to.be.true;
+
+    // Unable to mint an RFT when the MM is enabled
+    await expect(rftCollection.mintToken(superuser)).to.be.rejected;
+    
+    await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.disable', []);
+    expect(await maintenanceEnabled(helper.getApi()), 'MM is ON when it should be OFF').to.be.false;
+
+    // Can mint an RFT after disabling the MM
+    await rftCollection.mintToken(superuser);
+  });
+
+  itSub('MM allows native token transfers and RPC calls', async ({helper}) => {
+    // We can use RPC before the MM is enabled
+    const totalCount = await helper.collection.getTotalCount();
+
+    // We can transfer funds before the MM is enabled
+    await expect(helper.balance.transferToSubstrate(superuser, bob.address, 2n)).to.be.fulfilled;
+
+    await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.enable', []);
+    expect(await maintenanceEnabled(helper.getApi()), 'MM is OFF when it should be ON').to.be.true;
+
+    // RPCs work while in maintenance
+    expect(await helper.collection.getTotalCount()).to.be.deep.equal(totalCount);
+
+    // We still able to transfer funds
+    await expect(helper.balance.transferToSubstrate(bob, superuser.address, 1n)).to.be.fulfilled;
+
+    await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.disable', []);
+    expect(await maintenanceEnabled(helper.getApi()), 'MM is ON when it should be OFF').to.be.false;
+
+    // RPCs work after maintenance
+    expect(await helper.collection.getTotalCount()).to.be.deep.equal(totalCount);
+
+    // Transfers work after maintenance
+    await expect(helper.balance.transferToSubstrate(bob, superuser.address, 1n)).to.be.fulfilled;
+  });
+
+  itSub.ifWithPallets('MM blocks scheduled calls and the scheduler itself', [Pallets.Scheduler], async ({helper}) => {
+    const collection = await helper.nft.mintCollection(bob);
+
+    const nftBeforeMM = await collection.mintToken(bob);
+    const nftDuringMM = await collection.mintToken(bob);
+    const nftAfterMM = await collection.mintToken(bob);
+
+    const scheduledIdBeforeMM = '0x' + '0'.repeat(31) + '0';
+    const scheduledIdDuringMM = '0x' + '0'.repeat(31) + '1';
+    const scheduledIdBunkerThroughMM = '0x' + '0'.repeat(31) + '2';
+    const scheduledIdAttemptDuringMM = '0x' + '0'.repeat(31) + '3';
+    const scheduledIdAfterMM = '0x' + '0'.repeat(31) + '4';
+
+    const blocksToWait = 6;
+
+    // Scheduling works before the maintenance
+    await nftBeforeMM.scheduleAfter(scheduledIdBeforeMM, blocksToWait)
+      .transfer(bob, {Substrate: superuser.address});
+
+    await helper.wait.newBlocks(blocksToWait + 1);
+    expect(await nftBeforeMM.getOwner()).to.be.deep.equal({Substrate: superuser.address});
+
+    // Schedule a transaction that should occur *during* the maintenance
+    await nftDuringMM.scheduleAfter(scheduledIdDuringMM, blocksToWait)
+      .transfer(bob, {Substrate: superuser.address});
+    
+    // Schedule a transaction that should occur *after* the maintenance
+    await nftDuringMM.scheduleAfter(scheduledIdBunkerThroughMM, blocksToWait * 2)
+      .transfer(bob, {Substrate: superuser.address});
+
+    await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.enable', []);
+    expect(await maintenanceEnabled(helper.getApi()), 'MM is OFF when it should be ON').to.be.true;
+
+    await helper.wait.newBlocks(blocksToWait + 1);
+    // The owner should NOT change since the scheduled transaction should be rejected
+    expect(await nftDuringMM.getOwner()).to.be.deep.equal({Substrate: bob.address});
+
+    // Any attempts to schedule a tx during the MM should be rejected
+    await expect(nftDuringMM.scheduleAfter(scheduledIdAttemptDuringMM, blocksToWait)
+      .transfer(bob, {Substrate: superuser.address})).to.be.rejected;
+
+    await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.disable', []);
+    expect(await maintenanceEnabled(helper.getApi()), 'MM is ON when it should be OFF').to.be.false;
+
+    // Scheduling works after the maintenance
+    await nftAfterMM.scheduleAfter(scheduledIdAfterMM, blocksToWait)
+      .transfer(bob, {Substrate: superuser.address});
+    
+    await helper.wait.newBlocks(blocksToWait + 1);
+
+    expect(await nftAfterMM.getOwner()).to.be.deep.equal({Substrate: superuser.address});
+    // The owner of the token scheduled for transaction *before* maintenance should now change *after* maintenance
+    expect(await nftDuringMM.getOwner()).to.be.deep.equal({Substrate: superuser.address});
+  });
+
+  itEth('Disallows Ethereum transactions to execute while in maintenance', async ({helper}) => {
+    const owner = await helper.eth.createAccountWithBalance(donor);
+    const receiver = helper.eth.createAccount();
+    
+    const {collectionAddress} = await helper.eth.createERC721MetadataCompatibleNFTCollection(owner, 'A', 'B', 'C', '');
+
+    // Set maintenance mode
+    await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.enable', []);
+    expect(await maintenanceEnabled(helper.getApi()), 'MM is OFF when it should be ON').to.be.true;
+
+    const contract = helper.ethNativeContract.collection(collectionAddress, 'nft', owner);
+    const tokenId = await contract.methods.nextTokenId().call();
+    expect(tokenId).to.be.equal('1');
+
+    /*const result = */
+    await contract.methods.mintWithTokenURI(
+      receiver,
+      'Test URI',
+    ).send();
+    /*const expectedTokenId = result.events.Transfer.returnValues.tokenId;
+    expect(expectedTokenId).to.be.equal(tokenId);*/
+
+    await expect(contract.methods.ownerOf(tokenId).call()).rejectedWith(/token not found/);
+
+    // Disable maintenance mode
+    await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.disable', []);
+    expect(await maintenanceEnabled(helper.getApi()), 'MM is ON when it should be OFF').to.be.false;
+  });
+
+  itSub('Allows to enable and disable MM repeatedly', async ({helper}) => {
+    // Set maintenance mode
+    await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.enable', []);
+    await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.enable', []);
+    expect(await maintenanceEnabled(helper.getApi()), 'MM is OFF when it should be ON').to.be.true;
+
+    // Disable maintenance mode
+    await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.disable', []);
+    await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.disable', []);
+    expect(await maintenanceEnabled(helper.getApi()), 'MM is ON when it should be OFF').to.be.false;
+  });
+
+  afterEach(async () => {
+    await usingPlaygrounds(async helper => {
+      if (await maintenanceEnabled(helper.getApi())) {
+        console.warn('\tMaintenance mode was left enabled AFTER a test has finished! Be careful. Disabling it now.');
+        await helper.getSudo().executeExtrinsic(superuser, 'api.tx.maintenance.disable', []);
+      }
+      expect(await maintenanceEnabled(helper.getApi()), 'Disastrous! Exited the test suite with maintenance mode on.').to.be.false;
+    });
+  });
+});
\ No newline at end of file
modifiedtests/src/util/playgrounds/unique.dev.tsdiffbeforeafterboth
before · tests/src/util/playgrounds/unique.dev.ts
1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// SPDX-License-Identifier: Apache-2.034import {stringToU8a} from '@polkadot/util';5import {encodeAddress, mnemonicGenerate} from '@polkadot/util-crypto';6import {UniqueHelper, MoonbeamHelper, ChainHelperBase, AcalaHelper, RelayHelper, WestmintHelper} from './unique';7import {ApiPromise, Keyring, WsProvider} from '@polkadot/api';8import * as defs from '../../interfaces/definitions';9import {IKeyringPair} from '@polkadot/types/types';10import {EventRecord} from '@polkadot/types/interfaces';11import {ICrossAccountId, TSigner} from './types';12import {FrameSystemEventRecord} from '@polkadot/types/lookup';13import {VoidFn} from '@polkadot/api/types';14import {Pallets} from '..';1516export class SilentLogger {17  log(_msg: any, _level: any): void { }18  level = {19    ERROR: 'ERROR' as const,20    WARNING: 'WARNING' as const,21    INFO: 'INFO' as const,22  };23}2425export class SilentConsole {26  // TODO: Remove, this is temporary: Filter unneeded API output27  // (Jaco promised it will be removed in the next version)28  consoleErr: any;29  consoleLog: any;30  consoleWarn: any;3132  constructor() {33    this.consoleErr = console.error;34    this.consoleLog = console.log;35    this.consoleWarn = console.warn;36  }3738  enable() {  39    const outFn = (printer: any) => (...args: any[]) => {40      for (const arg of args) {41        if (typeof arg !== 'string')42          continue;43        if (arg.includes('1000:: Normal connection closure') || arg.includes('Not decorating unknown runtime apis:') || arg.includes('RPC methods not decorated:') || arg === 'Normal connection closure')44          return;45      }46      printer(...args);47    };48  49    console.error = outFn(this.consoleErr.bind(console));50    console.log = outFn(this.consoleLog.bind(console));51    console.warn = outFn(this.consoleWarn.bind(console));52  }5354  disable() {55    console.error = this.consoleErr;56    console.log = this.consoleLog;57    console.warn = this.consoleWarn;58  }59}6061export class DevUniqueHelper extends UniqueHelper {62  /**63   * Arrange methods for tests64   */65  arrange: ArrangeGroup;66  wait: WaitGroup;67  admin: AdminGroup;68  testUtils: TestUtilGroup;6970  constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {71    options.helperBase = options.helperBase ?? DevUniqueHelper;7273    super(logger, options);74    this.arrange = new ArrangeGroup(this);75    this.wait = new WaitGroup(this);76    this.admin = new AdminGroup(this);77    this.testUtils = new TestUtilGroup(this);78  }7980  async connect(wsEndpoint: string, _listeners?: any): Promise<void> {81    const wsProvider = new WsProvider(wsEndpoint);82    this.api = new ApiPromise({83      provider: wsProvider,84      signedExtensions: {85        ContractHelpers: {86          extrinsic: {},87          payload: {},88        },89        FakeTransactionFinalizer: {90          extrinsic: {},91          payload: {},92        },93      },94      rpc: {95        unique: defs.unique.rpc,96        appPromotion: defs.appPromotion.rpc,97        rmrk: defs.rmrk.rpc,98        eth: {99          feeHistory: {100            description: 'Dummy',101            params: [],102            type: 'u8',103          },104          maxPriorityFeePerGas: {105            description: 'Dummy',106            params: [],107            type: 'u8',108          },109        },110      },111    });112    await this.api.isReadyOrError;113    this.network = await UniqueHelper.detectNetwork(this.api);114  }115}116117export class DevRelayHelper extends RelayHelper {}118119export class DevWestmintHelper extends WestmintHelper {120  wait: WaitGroup;121122  constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {123    options.helperBase = options.helperBase ?? DevWestmintHelper;124125    super(logger, options);126    this.wait = new WaitGroup(this);127  }128}129130export class DevMoonbeamHelper extends MoonbeamHelper {131  account: MoonbeamAccountGroup;132  wait: WaitGroup;133134  constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {135    options.helperBase = options.helperBase ?? DevMoonbeamHelper;136137    super(logger, options);138    this.account = new MoonbeamAccountGroup(this);139    this.wait = new WaitGroup(this);140  }141}142143export class DevMoonriverHelper extends DevMoonbeamHelper {}144145export class DevAcalaHelper extends AcalaHelper {146  wait: WaitGroup;147148  constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {149    options.helperBase = options.helperBase ?? DevAcalaHelper;150151    super(logger, options);152    this.wait = new WaitGroup(this);153  }154}155156export class DevKaruraHelper extends DevAcalaHelper {}157158class ArrangeGroup {159  helper: DevUniqueHelper;160161  scheduledIdSlider = 0;162163  constructor(helper: DevUniqueHelper) {164    this.helper = helper;165  }166167  /**168   * Generates accounts with the specified UNQ token balance 169   * @param balances balances for generated accounts. Each balance will be multiplied by the token nominal.170   * @param donor donor account for balances171   * @returns array of newly created accounts172   * @example const [acc1, acc2, acc3] = await createAccounts([0n, 10n, 20n], donor); 173   */174  createAccounts = async (balances: bigint[], donor: IKeyringPair): Promise<IKeyringPair[]> => {175    let nonce = await this.helper.chain.getNonce(donor.address);176    const wait = new WaitGroup(this.helper);177    const ss58Format = this.helper.chain.getChainProperties().ss58Format;178    const tokenNominal = this.helper.balance.getOneTokenNominal();179    const transactions = [];180    const accounts: IKeyringPair[] = [];181    for (const balance of balances) {182      const recipient = this.helper.util.fromSeed(mnemonicGenerate(), ss58Format);183      accounts.push(recipient);184      if (balance !== 0n) {185        const tx = this.helper.constructApiCall('api.tx.balances.transfer', [{Id: recipient.address}, balance * tokenNominal]);186        transactions.push(this.helper.signTransaction(donor, tx, {nonce}, 'account generation'));187        nonce++;188      }189    }190191    await Promise.all(transactions).catch(_e => {});192    193    //#region TODO remove this region, when nonce problem will be solved194    const checkBalances = async () => {195      let isSuccess = true;196      for (let i = 0; i < balances.length; i++) {197        const balance = await this.helper.balance.getSubstrate(accounts[i].address);198        if (balance !== balances[i] * tokenNominal) {199          isSuccess = false;200          break;201        }202      }203      return isSuccess;204    };205206    let accountsCreated = false;207    const maxBlocksChecked = await this.helper.arrange.isDevNode() ? 50 : 5;208    // checkBalances retry up to 5-50 blocks209    for (let index = 0; index < maxBlocksChecked; index++) {210      accountsCreated = await checkBalances();211      if(accountsCreated) break;212      await wait.newBlocks(1);213    }214215    if (!accountsCreated) throw Error('Accounts generation failed');216    //#endregion217218    return accounts;219  };220221  // TODO combine this method and createAccounts into one222  createCrowd = async (accountsToCreate: number, withBalance: bigint, donor: IKeyringPair): Promise<IKeyringPair[]> => {  223    const createAsManyAsCan = async () => {224      let transactions: any = [];225      const accounts: IKeyringPair[] = [];226      let nonce = await this.helper.chain.getNonce(donor.address);227      const tokenNominal = this.helper.balance.getOneTokenNominal();228      for (let i = 0; i < accountsToCreate; i++) {229        if (i === 500) { // if there are too many accounts to create230          await Promise.allSettled(transactions); // wait while first 500 (should be 100 for devnode) tx will be settled 231          transactions = []; //232          nonce = await this.helper.chain.getNonce(donor.address); // update nonce 233        }234        const recepient = this.helper.util.fromSeed(mnemonicGenerate());235        accounts.push(recepient);236        if (withBalance !== 0n) {237          const tx = this.helper.constructApiCall('api.tx.balances.transfer', [{Id: recepient.address}, withBalance * tokenNominal]);238          transactions.push(this.helper.signTransaction(donor, tx, {nonce}, 'account generation'));239          nonce++;240        }241      }242      243      const fullfilledAccounts = [];244      await Promise.allSettled(transactions);245      for (const account of accounts) {246        const accountBalance = await this.helper.balance.getSubstrate(account.address);247        if (accountBalance === withBalance * tokenNominal) {248          fullfilledAccounts.push(account);249        }250      }251      return fullfilledAccounts;252    };253254    255    const crowd: IKeyringPair[] = [];256    // do up to 5 retries257    for (let index = 0; index < 5 && accountsToCreate !== 0; index++) {258      const asManyAsCan = await createAsManyAsCan();259      crowd.push(...asManyAsCan);260      accountsToCreate -= asManyAsCan.length;261    }262263    if (accountsToCreate !== 0) throw Error(`Crowd generation failed: ${accountsToCreate} accounts left`);264265    return crowd;266  };267268  isDevNode = async () => {269    let blockNumber = (await this.helper.callRpc('api.query.system.number')).toJSON();270    if (blockNumber == 0) {271      await this.helper.wait.newBlocks(1); 272      blockNumber = (await this.helper.callRpc('api.query.system.number')).toJSON();273    }274    const block2 = await this.helper.callRpc('api.rpc.chain.getBlock', [await this.helper.callRpc('api.rpc.chain.getBlockHash', [blockNumber])]);275    const block1 = await this.helper.callRpc('api.rpc.chain.getBlock', [await this.helper.callRpc('api.rpc.chain.getBlockHash', [blockNumber - 1])]);276    const findCreationDate = (block: any) => {277      const humanBlock = block.toHuman();278      let date;279      humanBlock.block.extrinsics.forEach((ext: any) => {280        if(ext.method.section === 'timestamp') {281          date = Number(ext.method.args.now.replaceAll(',', ''));282        }283      });284      return date;285    };286    const block1date = await findCreationDate(block1);287    const block2date = await findCreationDate(block2);288    if(block2date! - block1date! < 9000) return true;289  };290  291  async calculcateFee(payer: ICrossAccountId, promise: () => Promise<any>): Promise<bigint> {292    const address = payer.Substrate ? payer.Substrate : await this.helper.address.ethToSubstrate(payer.Ethereum!);293    let balance = await this.helper.balance.getSubstrate(address); 294    295    await promise();296    297    balance -= await this.helper.balance.getSubstrate(address);298    299    return balance;300  }301302  calculatePalletAddress(palletId: any) {303    const address = stringToU8a(('modl' + palletId).padEnd(32, '\0'));304    return encodeAddress(address);305  }306307  async makeScheduledIds(num: number): Promise<string[]> {308    await this.helper.wait.noScheduledTasks();309310    function makeId(slider: number) {311      const scheduledIdSize = 32;312      const hexId = slider.toString(16);313      const prefixSize = scheduledIdSize - hexId.length;314315      const scheduledId = '0x' + '0'.repeat(prefixSize) + hexId;316317      return scheduledId;  318    }319320    const ids = [];321    for (let i = 0; i < num; i++) {322      ids.push(makeId(this.scheduledIdSlider));323      this.scheduledIdSlider += 1;324    }325326    return ids;327  }328329  async makeScheduledId(): Promise<string> {330    return (await this.makeScheduledIds(1))[0];331  }332333  async captureEvents(eventSection: string, eventMethod: string): Promise<EventCapture> {334    const capture = new EventCapture(this.helper, eventSection, eventMethod);335    await capture.startCapture();336337    return capture;338  }339}340341class MoonbeamAccountGroup {342  helper: MoonbeamHelper;343344  keyring: Keyring;345  _alithAccount: IKeyringPair;346  _baltatharAccount: IKeyringPair;347  _dorothyAccount: IKeyringPair;348349  constructor(helper: MoonbeamHelper) {350    this.helper = helper;351352    this.keyring = new Keyring({type: 'ethereum'});353    const alithPrivateKey = '0x5fb92d6e98884f76de468fa3f6278f8807c48bebc13595d45af5bdc4da702133';354    const baltatharPrivateKey = '0x8075991ce870b93a8870eca0c0f91913d12f47948ca0fd25b49c6fa7cdbeee8b';355    const dorothyPrivateKey = '0x39539ab1876910bbf3a223d84a29e28f1cb4e2e456503e7e91ed39b2e7223d68';356357    this._alithAccount = this.keyring.addFromUri(alithPrivateKey, undefined, 'ethereum');358    this._baltatharAccount = this.keyring.addFromUri(baltatharPrivateKey, undefined, 'ethereum');359    this._dorothyAccount = this.keyring.addFromUri(dorothyPrivateKey, undefined, 'ethereum');360  }361362  alithAccount() {363    return this._alithAccount;364  }365366  baltatharAccount() {367    return this._baltatharAccount;368  }369370  dorothyAccount() {371    return this._dorothyAccount;372  }373374  create() {375    return this.keyring.addFromUri(mnemonicGenerate());376  }377}378379class WaitGroup {380  helper: ChainHelperBase;381382  constructor(helper: ChainHelperBase) {383    this.helper = helper;384  }385386  sleep(milliseconds: number) {387    return new Promise((resolve) => setTimeout(resolve, milliseconds));388  }389390  private async waitWithTimeout(promise: Promise<any>, timeout: number) {391    let isBlock = false;392    promise.then(() => isBlock = true).catch(() => isBlock = true);393    let totalTime = 0;394    const step = 100;395    while(!isBlock) {396      await this.sleep(step);397      totalTime += step;398      if(totalTime >= timeout) throw Error('Blocks production failed');399    }400    return promise;401  }402403  /**404   * Wait for specified number of blocks405   * @param blocksCount number of blocks to wait406   * @returns 407   */408  async newBlocks(blocksCount = 1, timeout?: number): Promise<void> {409    timeout = timeout ?? blocksCount * 60_000;410    // eslint-disable-next-line no-async-promise-executor411    const promise = new Promise<void>(async (resolve) => {412      const unsubscribe = await this.helper.getApi().rpc.chain.subscribeNewHeads(() => {413        if (blocksCount > 0) {414          blocksCount--;415        } else {416          unsubscribe();417          resolve();418        }419      });420    });421    await this.waitWithTimeout(promise, timeout);422    return promise;423  }424425  async forParachainBlockNumber(blockNumber: bigint | number, timeout?: number) {426    timeout = timeout ?? 30 * 60 * 1000;427    // eslint-disable-next-line no-async-promise-executor428    const promise = new Promise<void>(async (resolve) => {429      const unsubscribe = await this.helper.getApi().rpc.chain.subscribeNewHeads((data: any) => {430        if (data.number.toNumber() >= blockNumber) {431          unsubscribe();432          resolve();433        }434      });435    });436    await this.waitWithTimeout(promise, timeout);437    return promise;438  }439  440  async forRelayBlockNumber(blockNumber: bigint | number, timeout?: number) {441    timeout = timeout ?? 30 * 60 * 1000;442    // eslint-disable-next-line no-async-promise-executor443    const promise = new Promise<void>(async (resolve) => {444      const unsubscribe = await this.helper.getApi().query.parachainSystem.validationData((data: any) => {445        if (data.value.relayParentNumber.toNumber() >= blockNumber) {446          // @ts-ignore447          unsubscribe();448          resolve();449        }450      });451    });452    await this.waitWithTimeout(promise, timeout);453    return promise;454  }455456  noScheduledTasks() {457    const api = this.helper.getApi();458    459    // eslint-disable-next-line no-async-promise-executor460    const promise = new Promise<void>(async resolve => {461      const unsubscribe = await api.rpc.chain.subscribeNewHeads(async () => {462        const areThereScheduledTasks = await api.query.scheduler.lookup.entries();463464        if(areThereScheduledTasks.length == 0) {465          unsubscribe();466          resolve();467        }468      }); 469    });470471    return promise;472  }473474  event(maxBlocksToWait: number, eventSection: string, eventMethod: string) {475    // eslint-disable-next-line no-async-promise-executor476    const promise = new Promise<EventRecord | null>(async (resolve) => {477      const unsubscribe = await this.helper.getApi().rpc.chain.subscribeNewHeads(async header => {478        const blockNumber = header.number.toHuman();479        const blockHash = header.hash;480        const eventIdStr = `${eventSection}.${eventMethod}`;481        const waitLimitStr = `wait blocks remaining: ${maxBlocksToWait}`;482  483        this.helper.logger.log(`[Block #${blockNumber}] Waiting for event \`${eventIdStr}\` (${waitLimitStr})`);484  485        const apiAt = await this.helper.getApi().at(blockHash);486        const eventRecords = (await apiAt.query.system.events()) as any;487  488        const neededEvent = eventRecords.toArray().find((r: FrameSystemEventRecord) => {489          return r.event.section == eventSection && r.event.method == eventMethod;490        });491  492        if (neededEvent) {493          unsubscribe();494          resolve(neededEvent);495        } else if (maxBlocksToWait > 0) {496          maxBlocksToWait--;497        } else {498          this.helper.logger.log(`Event \`${eventIdStr}\` is NOT found`);499          unsubscribe();500          resolve(null);501        }502      });503    });504    return promise;505  }506}507508class TestUtilGroup {509  helper: DevUniqueHelper;510511  constructor(helper: DevUniqueHelper) {512    this.helper = helper;513  }514515  async enable() {516    if (this.helper.fetchMissingPalletNames([Pallets.TestUtils]).length != 0) {517      return;518    }519520    const signer = this.helper.util.fromSeed('//Alice');521    await this.helper.getSudo<DevUniqueHelper>().executeExtrinsic(signer, 'api.tx.testUtils.enable', [], true);522  }523524  async setTestValue(signer: TSigner, testVal: number) {525    await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.setTestValue', [testVal], true);526  }527528  async incTestValue(signer: TSigner) {529    await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.incTestValue', [], true);530  }531532  async setTestValueAndRollback(signer: TSigner, testVal: number) {533    await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.setTestValueAndRollback', [testVal], true);534  }535536  async testValue() {537    return (await this.helper.callRpc('api.query.testUtils.testValue', [])).toNumber();538  }539540  async justTakeFee(signer: TSigner) {541    await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.justTakeFee', [], true);542  }543544  async selfCancelingInc(signer: TSigner, scheduledId: string, maxTestVal: number) {545    await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.selfCancelingInc', [scheduledId, maxTestVal], true);546  }547}548549class EventCapture {550  helper: DevUniqueHelper;551  eventSection: string;552  eventMethod: string;553  events: EventRecord[] = [];554  unsubscribe: VoidFn | null = null;555556  constructor(557    helper: DevUniqueHelper,558    eventSection: string,559    eventMethod: string,560  ) {561    this.helper = helper;562    this.eventSection = eventSection;563    this.eventMethod = eventMethod;564  }565566  async startCapture() {567    this.stopCapture();568    this.unsubscribe = (await this.helper.getApi().query.system.events((eventRecords: FrameSystemEventRecord[]) => {569      const newEvents = eventRecords.filter(r => {570        return r.event.section == this.eventSection && r.event.method == this.eventMethod;571      });572573      this.events.push(...newEvents);574    })) as any;575  }576577  stopCapture() {578    if (this.unsubscribe !== null) {579      this.unsubscribe();580    }581  }582583  extractCapturedEvents() {584    return this.events;585  }586}587588class AdminGroup {589  helper: UniqueHelper;590591  constructor(helper: UniqueHelper) {592    this.helper = helper;593  }594595  async payoutStakers(signer: IKeyringPair, stakersToPayout: number) {596    const payoutResult = await this.helper.executeExtrinsic(signer, 'api.tx.appPromotion.payoutStakers', [stakersToPayout], true);597    return payoutResult.result.events.filter(e => e.event.method === 'StakingRecalculation').map(e => {598      return {599        staker: e.event.data[0].toString(),600        stake: e.event.data[1].toBigInt(),601        payout: e.event.data[2].toBigInt(),602      };603    });604  }605}