git.delta.rocks / unique-network / refs/commits / 7ca598f7f6b8

difftreelog

source

js-packages/scripts/correctStateAfterMaintenance.ts2.1 KiBsourcehistory
1import config from '../tests/config.js';2import {usingPlaygrounds} from '@unique/test-utils/util.js';3import type {u32} from '@polkadot/types-codec';45const WS_ENDPOINT = config.substrateUrl;6const DONOR_SEED = '//Alice';78export const main = async(options: { wsEndpoint: string; donorSeed: string } = {9  wsEndpoint: WS_ENDPOINT,10  donorSeed: DONOR_SEED,11}) => {12  await usingPlaygrounds(async (helper, privateKey) => {13    const api = helper.getApi();1415    if((await api.query.maintenance.enabled()).valueOf()) {16      throw Error('The network is still in maintenance mode');17    }1819    const pendingBlocks = (20      await api.query.appPromotion.pendingUnstake.entries()21    ).map(([k, _v]) =>22      (k.args[0] as u32).toNumber());2324    const currentBlock = (await api.query.system.number() as u32).toNumber();2526    const filteredBlocks = pendingBlocks.filter(b => currentBlock > b);2728    if(filteredBlocks.length != 0) {29      console.log(`During maintenance mode, ${filteredBlocks.length} block(s) were not processed. Number(s): ${filteredBlocks}`);30    } else {31      console.log('Nothing to change');32      return;33    }3435    const skippedBlocks = chunk(filteredBlocks, 10);3637    const signer = await privateKey(options.donorSeed);3839    const txs = skippedBlocks.map((b) =>40      api.tx.appPromotion.resolveSkippedBlocks(b));414243    const promises = txs.map((tx) => () => helper.signTransaction(signer, tx));4445    await Promise.allSettled(promises.map((p) => p()));4647    const failedBlocks: number[] = [];48    let isSuccess = true;4950    for(const b of filteredBlocks) {51      if(((await api.query.appPromotion.pendingUnstake(b)).toJSON() as any[]).length != 0) {52        failedBlocks.push(b);53        isSuccess = false;54      }55    }5657    if(isSuccess) {58      console.log('Done. %d block(s) were processed.', filteredBlocks.length);59    } else {60      throw new Error(`Something went wrong. Block(s) have not been processed: ${failedBlocks}`);61    }626364  }, options.wsEndpoint);65};6667const chunk = <T>(arr: T[], size: number) =>68  Array.from({length: Math.ceil(arr.length / size)}, (_: any, i: number) =>69    arr.slice(i * size, i * size + size));