git.delta.rocks / unique-network / refs/commits / 33d3cd09a241

difftreelog

source

tests/src/util/playgrounds/unique.dev.ts20.2 KiBsourcehistory
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        CheckMaintenance: {90          extrinsic: {},91          payload: {},92        },93        FakeTransactionFinalizer: {94          extrinsic: {},95          payload: {},96        },97      },98      rpc: {99        unique: defs.unique.rpc,100        appPromotion: defs.appPromotion.rpc,101        rmrk: defs.rmrk.rpc,102        eth: {103          feeHistory: {104            description: 'Dummy',105            params: [],106            type: 'u8',107          },108          maxPriorityFeePerGas: {109            description: 'Dummy',110            params: [],111            type: 'u8',112          },113        },114      },115    });116    await this.api.isReadyOrError;117    this.network = await UniqueHelper.detectNetwork(this.api);118  }119}120121export class DevRelayHelper extends RelayHelper {}122123export class DevWestmintHelper extends WestmintHelper {124  wait: WaitGroup;125126  constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {127    options.helperBase = options.helperBase ?? DevWestmintHelper;128129    super(logger, options);130    this.wait = new WaitGroup(this);131  }132}133134export class DevMoonbeamHelper extends MoonbeamHelper {135  account: MoonbeamAccountGroup;136  wait: WaitGroup;137138  constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {139    options.helperBase = options.helperBase ?? DevMoonbeamHelper;140141    super(logger, options);142    this.account = new MoonbeamAccountGroup(this);143    this.wait = new WaitGroup(this);144  }145}146147export class DevMoonriverHelper extends DevMoonbeamHelper {}148149export class DevAcalaHelper extends AcalaHelper {150  wait: WaitGroup;151152  constructor(logger: { log: (msg: any, level: any) => void, level: any }, options: {[key: string]: any} = {}) {153    options.helperBase = options.helperBase ?? DevAcalaHelper;154155    super(logger, options);156    this.wait = new WaitGroup(this);157  }158}159160export class DevKaruraHelper extends DevAcalaHelper {}161162class ArrangeGroup {163  helper: DevUniqueHelper;164165  scheduledIdSlider = 0;166167  constructor(helper: DevUniqueHelper) {168    this.helper = helper;169  }170171  /**172   * Generates accounts with the specified UNQ token balance 173   * @param balances balances for generated accounts. Each balance will be multiplied by the token nominal.174   * @param donor donor account for balances175   * @returns array of newly created accounts176   * @example const [acc1, acc2, acc3] = await createAccounts([0n, 10n, 20n], donor); 177   */178  createAccounts = async (balances: bigint[], donor: IKeyringPair): Promise<IKeyringPair[]> => {179    let nonce = await this.helper.chain.getNonce(donor.address);180    const wait = new WaitGroup(this.helper);181    const ss58Format = this.helper.chain.getChainProperties().ss58Format;182    const tokenNominal = this.helper.balance.getOneTokenNominal();183    const transactions = [];184    const accounts: IKeyringPair[] = [];185    for (const balance of balances) {186      const recipient = this.helper.util.fromSeed(mnemonicGenerate(), ss58Format);187      accounts.push(recipient);188      if (balance !== 0n) {189        const tx = this.helper.constructApiCall('api.tx.balances.transfer', [{Id: recipient.address}, balance * tokenNominal]);190        transactions.push(this.helper.signTransaction(donor, tx, {nonce}, 'account generation'));191        nonce++;192      }193    }194195    await Promise.all(transactions).catch(_e => {});196    197    //#region TODO remove this region, when nonce problem will be solved198    const checkBalances = async () => {199      let isSuccess = true;200      for (let i = 0; i < balances.length; i++) {201        const balance = await this.helper.balance.getSubstrate(accounts[i].address);202        if (balance !== balances[i] * tokenNominal) {203          isSuccess = false;204          break;205        }206      }207      return isSuccess;208    };209210    let accountsCreated = false;211    const maxBlocksChecked = await this.helper.arrange.isDevNode() ? 50 : 5;212    // checkBalances retry up to 5-50 blocks213    for (let index = 0; index < maxBlocksChecked; index++) {214      accountsCreated = await checkBalances();215      if(accountsCreated) break;216      await wait.newBlocks(1);217    }218219    if (!accountsCreated) throw Error('Accounts generation failed');220    //#endregion221222    return accounts;223  };224225  // TODO combine this method and createAccounts into one226  createCrowd = async (accountsToCreate: number, withBalance: bigint, donor: IKeyringPair): Promise<IKeyringPair[]> => {  227    const createAsManyAsCan = async () => {228      let transactions: any = [];229      const accounts: IKeyringPair[] = [];230      let nonce = await this.helper.chain.getNonce(donor.address);231      const tokenNominal = this.helper.balance.getOneTokenNominal();232      for (let i = 0; i < accountsToCreate; i++) {233        if (i === 500) { // if there are too many accounts to create234          await Promise.allSettled(transactions); // wait while first 500 (should be 100 for devnode) tx will be settled 235          transactions = []; //236          nonce = await this.helper.chain.getNonce(donor.address); // update nonce 237        }238        const recepient = this.helper.util.fromSeed(mnemonicGenerate());239        accounts.push(recepient);240        if (withBalance !== 0n) {241          const tx = this.helper.constructApiCall('api.tx.balances.transfer', [{Id: recepient.address}, withBalance * tokenNominal]);242          transactions.push(this.helper.signTransaction(donor, tx, {nonce}, 'account generation'));243          nonce++;244        }245      }246      247      const fullfilledAccounts = [];248      await Promise.allSettled(transactions);249      for (const account of accounts) {250        const accountBalance = await this.helper.balance.getSubstrate(account.address);251        if (accountBalance === withBalance * tokenNominal) {252          fullfilledAccounts.push(account);253        }254      }255      return fullfilledAccounts;256    };257258    259    const crowd: IKeyringPair[] = [];260    // do up to 5 retries261    for (let index = 0; index < 5 && accountsToCreate !== 0; index++) {262      const asManyAsCan = await createAsManyAsCan();263      crowd.push(...asManyAsCan);264      accountsToCreate -= asManyAsCan.length;265    }266267    if (accountsToCreate !== 0) throw Error(`Crowd generation failed: ${accountsToCreate} accounts left`);268269    return crowd;270  };271272  isDevNode = async () => {273    let blockNumber = (await this.helper.callRpc('api.query.system.number')).toJSON();274    if (blockNumber == 0) {275      await this.helper.wait.newBlocks(1); 276      blockNumber = (await this.helper.callRpc('api.query.system.number')).toJSON();277    }278    const block2 = await this.helper.callRpc('api.rpc.chain.getBlock', [await this.helper.callRpc('api.rpc.chain.getBlockHash', [blockNumber])]);279    const block1 = await this.helper.callRpc('api.rpc.chain.getBlock', [await this.helper.callRpc('api.rpc.chain.getBlockHash', [blockNumber - 1])]);280    const findCreationDate = (block: any) => {281      const humanBlock = block.toHuman();282      let date;283      humanBlock.block.extrinsics.forEach((ext: any) => {284        if(ext.method.section === 'timestamp') {285          date = Number(ext.method.args.now.replaceAll(',', ''));286        }287      });288      return date;289    };290    const block1date = await findCreationDate(block1);291    const block2date = await findCreationDate(block2);292    if(block2date! - block1date! < 9000) return true;293  };294  295  async calculcateFee(payer: ICrossAccountId, promise: () => Promise<any>): Promise<bigint> {296    const address = payer.Substrate ? payer.Substrate : await this.helper.address.ethToSubstrate(payer.Ethereum!);297    let balance = await this.helper.balance.getSubstrate(address); 298    299    await promise();300    301    balance -= await this.helper.balance.getSubstrate(address);302    303    return balance;304  }305306  calculatePalletAddress(palletId: any) {307    const address = stringToU8a(('modl' + palletId).padEnd(32, '\0'));308    return encodeAddress(address, this.helper.chain.getChainProperties().ss58Format);309  }310311  makeScheduledIds(num: number): string[] {312    function makeId(slider: number) {313      const scheduledIdSize = 64;314      const hexId = slider.toString(16);315      const prefixSize = scheduledIdSize - hexId.length;316317      const scheduledId = '0x' + '0'.repeat(prefixSize) + hexId;318319      return scheduledId;  320    }321322    const ids = [];323    for (let i = 0; i < num; i++) {324      ids.push(makeId(this.scheduledIdSlider));325      this.scheduledIdSlider += 1;326    }327328    return ids;329  }330331  makeScheduledId(): string {332    return (this.makeScheduledIds(1))[0];333  }334335  async captureEvents(eventSection: string, eventMethod: string): Promise<EventCapture> {336    const capture = new EventCapture(this.helper, eventSection, eventMethod);337    await capture.startCapture();338339    return capture;340  }341}342343class MoonbeamAccountGroup {344  helper: MoonbeamHelper;345346  keyring: Keyring;347  _alithAccount: IKeyringPair;348  _baltatharAccount: IKeyringPair;349  _dorothyAccount: IKeyringPair;350351  constructor(helper: MoonbeamHelper) {352    this.helper = helper;353354    this.keyring = new Keyring({type: 'ethereum'});355    const alithPrivateKey = '0x5fb92d6e98884f76de468fa3f6278f8807c48bebc13595d45af5bdc4da702133';356    const baltatharPrivateKey = '0x8075991ce870b93a8870eca0c0f91913d12f47948ca0fd25b49c6fa7cdbeee8b';357    const dorothyPrivateKey = '0x39539ab1876910bbf3a223d84a29e28f1cb4e2e456503e7e91ed39b2e7223d68';358359    this._alithAccount = this.keyring.addFromUri(alithPrivateKey, undefined, 'ethereum');360    this._baltatharAccount = this.keyring.addFromUri(baltatharPrivateKey, undefined, 'ethereum');361    this._dorothyAccount = this.keyring.addFromUri(dorothyPrivateKey, undefined, 'ethereum');362  }363364  alithAccount() {365    return this._alithAccount;366  }367368  baltatharAccount() {369    return this._baltatharAccount;370  }371372  dorothyAccount() {373    return this._dorothyAccount;374  }375376  create() {377    return this.keyring.addFromUri(mnemonicGenerate());378  }379}380381class WaitGroup {382  helper: ChainHelperBase;383384  constructor(helper: ChainHelperBase) {385    this.helper = helper;386  }387388  sleep(milliseconds: number) {389    return new Promise((resolve) => setTimeout(resolve, milliseconds));390  }391392  private async waitWithTimeout(promise: Promise<any>, timeout: number) {393    let isBlock = false;394    promise.then(() => isBlock = true).catch(() => isBlock = true);395    let totalTime = 0;396    const step = 100;397    while(!isBlock) {398      await this.sleep(step);399      totalTime += step;400      if(totalTime >= timeout) throw Error('Blocks production failed');401    }402    return promise;403  }404405  /**406   * Launch some async operation, or throw an error after some time. Note that it will still continue executing after the timeout.407   * @param promise async operation to race against the timeout408   * @param timeoutMS time after which to time out409   * @param timeoutError error message to throw410   * @returns promise of the same type the operation had411   */412  async withTimeout<T>(413    promise: Promise<T>,414    timeoutMS = 30000,415    timeoutError = 'The operation has timed out!',416  ): Promise<T> {417    const timeout = new Promise<never>((_, reject) => {418      setTimeout(() => {419        reject(new Error(timeoutError));420      }, timeoutMS);421    });422  423    return Promise.race<T>([promise, timeout]).catch(e => {throw new Error(e);});424  }425426  /**427   * Wait for specified number of blocks428   * @param blocksCount number of blocks to wait429   * @returns 430   */431  async newBlocks(blocksCount = 1, timeout?: number): Promise<void> {432    timeout = timeout ?? blocksCount * 60_000;433    // eslint-disable-next-line no-async-promise-executor434    const promise = new Promise<void>(async (resolve) => {435      const unsubscribe = await this.helper.getApi().rpc.chain.subscribeNewHeads(() => {436        if (blocksCount > 0) {437          blocksCount--;438        } else {439          unsubscribe();440          resolve();441        }442      });443    });444    await this.waitWithTimeout(promise, timeout);445    return promise;446  }447448  async forParachainBlockNumber(blockNumber: bigint | number, timeout?: number) {449    timeout = timeout ?? 30 * 60 * 1000;450    // eslint-disable-next-line no-async-promise-executor451    const promise = new Promise<void>(async (resolve) => {452      const unsubscribe = await this.helper.getApi().rpc.chain.subscribeNewHeads((data: any) => {453        if (data.number.toNumber() >= blockNumber) {454          unsubscribe();455          resolve();456        }457      });458    });459    await this.waitWithTimeout(promise, timeout);460    return promise;461  }462  463  async forRelayBlockNumber(blockNumber: bigint | number, timeout?: number) {464    timeout = timeout ?? 30 * 60 * 1000;465    // eslint-disable-next-line no-async-promise-executor466    const promise = new Promise<void>(async (resolve) => {467      const unsubscribe = await this.helper.getApi().query.parachainSystem.validationData((data: any) => {468        if (data.value.relayParentNumber.toNumber() >= blockNumber) {469          // @ts-ignore470          unsubscribe();471          resolve();472        }473      });474    });475    await this.waitWithTimeout(promise, timeout);476    return promise;477  }478479  noScheduledTasks() {480    const api = this.helper.getApi();481    482    // eslint-disable-next-line no-async-promise-executor483    const promise = new Promise<void>(async resolve => {484      const unsubscribe = await api.rpc.chain.subscribeNewHeads(async () => {485        const areThereScheduledTasks = await api.query.scheduler.lookup.entries();486487        if(areThereScheduledTasks.length == 0) {488          unsubscribe();489          resolve();490        }491      }); 492    });493494    return promise;495  }496497  event(maxBlocksToWait: number, eventSection: string, eventMethod: string) {498    // eslint-disable-next-line no-async-promise-executor499    const promise = new Promise<EventRecord | null>(async (resolve) => {500      const unsubscribe = await this.helper.getApi().rpc.chain.subscribeNewHeads(async header => {501        const blockNumber = header.number.toHuman();502        const blockHash = header.hash;503        const eventIdStr = `${eventSection}.${eventMethod}`;504        const waitLimitStr = `wait blocks remaining: ${maxBlocksToWait}`;505  506        this.helper.logger.log(`[Block #${blockNumber}] Waiting for event \`${eventIdStr}\` (${waitLimitStr})`);507  508        const apiAt = await this.helper.getApi().at(blockHash);509        const eventRecords = (await apiAt.query.system.events()) as any;510  511        const neededEvent = eventRecords.toArray().find((r: FrameSystemEventRecord) => {512          return r.event.section == eventSection && r.event.method == eventMethod;513        });514  515        if (neededEvent) {516          unsubscribe();517          resolve(neededEvent);518        } else if (maxBlocksToWait > 0) {519          maxBlocksToWait--;520        } else {521          this.helper.logger.log(`Event \`${eventIdStr}\` is NOT found`);522          unsubscribe();523          resolve(null);524        }525      });526    });527    return promise;528  }529}530531class TestUtilGroup {532  helper: DevUniqueHelper;533534  constructor(helper: DevUniqueHelper) {535    this.helper = helper;536  }537538  async enable() {539    if (this.helper.fetchMissingPalletNames([Pallets.TestUtils]).length != 0) {540      return;541    }542543    const signer = this.helper.util.fromSeed('//Alice');544    await this.helper.getSudo<DevUniqueHelper>().executeExtrinsic(signer, 'api.tx.testUtils.enable', [], true);545  }546547  async setTestValue(signer: TSigner, testVal: number) {548    await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.setTestValue', [testVal], true);549  }550551  async incTestValue(signer: TSigner) {552    await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.incTestValue', [], true);553  }554555  async setTestValueAndRollback(signer: TSigner, testVal: number) {556    await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.setTestValueAndRollback', [testVal], true);557  }558559  async testValue(blockIdx?: number) {560    const api = blockIdx561      ? await this.helper.getApi().at(await this.helper.callRpc('api.rpc.chain.getBlockHash', [blockIdx]))562      : this.helper.getApi();563564    return (await api.query.testUtils.testValue()).toJSON();565  }566567  async justTakeFee(signer: TSigner) {568    await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.justTakeFee', [], true);569  }570571  async selfCancelingInc(signer: TSigner, scheduledId: string, maxTestVal: number) {572    await this.helper.executeExtrinsic(signer, 'api.tx.testUtils.selfCancelingInc', [scheduledId, maxTestVal], true);573  }574}575576class EventCapture {577  helper: DevUniqueHelper;578  eventSection: string;579  eventMethod: string;580  events: EventRecord[] = [];581  unsubscribe: VoidFn | null = null;582583  constructor(584    helper: DevUniqueHelper,585    eventSection: string,586    eventMethod: string,587  ) {588    this.helper = helper;589    this.eventSection = eventSection;590    this.eventMethod = eventMethod;591  }592593  async startCapture() {594    this.stopCapture();595    this.unsubscribe = (await this.helper.getApi().query.system.events((eventRecords: FrameSystemEventRecord[]) => {596      const newEvents = eventRecords.filter(r => {597        return r.event.section == this.eventSection && r.event.method == this.eventMethod;598      });599600      this.events.push(...newEvents);601    })) as any;602  }603604  stopCapture() {605    if (this.unsubscribe !== null) {606      this.unsubscribe();607    }608  }609610  extractCapturedEvents() {611    return this.events;612  }613}614615class AdminGroup {616  helper: UniqueHelper;617618  constructor(helper: UniqueHelper) {619    this.helper = helper;620  }621622  async payoutStakers(signer: IKeyringPair, stakersToPayout: number) {623    const payoutResult = await this.helper.executeExtrinsic(signer, 'api.tx.appPromotion.payoutStakers', [stakersToPayout], true);624    return payoutResult.result.events.filter(e => e.event.method === 'StakingRecalculation').map(e => {625      return {626        staker: e.event.data[0].toString(),627        stake: e.event.data[1].toBigInt(),628        payout: e.event.data[2].toBigInt(),629      };630    });631  }632}