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

difftreelog

source

tests/src/util/frankenstein.ts15.1 KiBsourcehistory
1// Copyright 2019-2023 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617import {ApiPromise} from '@polkadot/api';18import {blake2AsHex, cryptoWaitReady} from '@polkadot/util-crypto';19import zombie from '@zombienet/orchestrator/dist';20import {readNetworkConfig} from '@zombienet/utils/dist';21import {resolve} from 'path';22import {usingPlaygrounds} from '.';23import {migrations} from './frankensteinMigrate';24import fs from 'fs';2526const ZOMBIENET_CREDENTIALS = process.env.ZOMBIENET_CREDENTIALS || '../.env';27const NETWORK_CONFIG_FILE = process.argv[2] ?? '../launch-zombienet.toml';28const PARA_DIR = process.env.PARA_DIR || '../';29const RELAY_DIR = process.env.RELAY_DIR || '../../polkadot/';30const REPLICA_FROM = process.env.REPLICA_FROM || 'wss://ws-opal.unique.network:443';31const NEW_RELAY_BIN = process.env.NEW_RELAY_BIN;32const NEW_RELAY_WASM = process.env.NEW_RELAY_WASM;33const NEW_PARA_BIN = process.env.NEW_PARA_BIN;34const NEW_PARA_WASM = process.env.NEW_PARA_WASM;35const DESTINATION_SPEC_VERSION = process.env.DESTINATION_SPEC_VERSION!;36const PARACHAIN_BLOCK_TIME = 12_000;37const SUPERUSER_KEY = '//Alice';3839let network: zombie.Network | undefined;4041// Stop the network if it is running42const stop = async () => {43  await network?.stop();44};4546// Promise of a timeout47function delay(ms: number) {48  return new Promise(resolve => setTimeout(resolve, ms));49}5051// Countdown with time left on display52async function waitWithTimer(time: number) {53  const secondsTotal = Math.ceil(time / 1000);54  for(let i = secondsTotal; i > 0; i--) {55    // could also introduce hours, but wth56    const seconds = i % 60;57    const text = `Time left: ${Math.floor(i / 60)}:${seconds < 10 ? '0' + seconds : seconds}`;58    if(process.stdout.isTTY)59      process.stdout.write(text);60    else if(seconds % 10 == 0)61      console.log(text);62    await delay(1000);63    if(process.stdout.isTTY) {64      process.stdout.clearLine(0);65      process.stdout.cursorTo(0);66    }67  }68}6970// Get the runtime's current version71function getSpecVersion(api: ApiPromise): number {72  return (api.consts.system.version as any).specVersion.toNumber();73}7475// Get the required information on the relay chain76function getRelayInfo(api: ApiPromise): {specVersion: number, epochBlockLength: number, blockTime: number, epochTime: number} {77  const info = {78    specVersion: getSpecVersion(api),79    epochBlockLength: (api.consts.babe.epochDuration as any).toNumber(),80    blockTime: (api.consts.babe.expectedBlockTime as any).toNumber(),81    epochTime: 0,82  };83  info.epochTime = info.epochBlockLength * info.blockTime;84  return info;85}8687// Enable or disable maintenance mode if present on the chain88async function toggleMaintenanceMode(value: boolean, wsUri: string, retries = 5) {89  try {90    await usingPlaygrounds(async (helper, privateKey) => {91      const superuser = await privateKey(SUPERUSER_KEY);92      try {93        const toggle = value ? 'enable' : 'disable';94        await helper.getSudo().executeExtrinsic(superuser, `api.tx.maintenance.${toggle}`, []);95        console.log(`Maintenance mode ${value ? 'engaged' : 'disengaged'}.`);96      } catch (e) {97        console.error('Couldn\'t set maintenance mode. The maintenance pallet probably does not exist. Log:', e);98      }99    }, wsUri);100  } catch (error) {101    console.error(error);102    console.log('Trying for retry toggle maintanence mode');103    await delay(12_000);104    await toggleMaintenanceMode(value, wsUri, retries - 1);105  }106}107108async function skipIfAlreadyUpgraded() {109  await usingPlaygrounds(async (helper) => {110    const specVersion = await getSpecVersion(helper.getApi());111    if(`v${specVersion}` === DESTINATION_SPEC_VERSION) {112      console.log('\n🛸 Current version equal DESTINATION_SPEC_VERSION 🛸');113      console.log("\n🛸 PARACHAINS' RUNTIME UPGRADE TESTING COMPLETE 🛸");114    }115  }, REPLICA_FROM);116}117118const raiseZombienet = async (): Promise<void> => {119  await skipIfAlreadyUpgraded();120  const isUpgradeTesting = !!NEW_RELAY_BIN || !!NEW_RELAY_WASM || !!NEW_PARA_BIN || !!NEW_PARA_WASM;121  /*122  // If there is nothing to upgrade, what is the point123  if (!isUpgradeTesting) {124    console.warn('\nNeither the relay nor the parachain were selected for an upgrade! ' +125      'Please pass environment vars `NEW_RELAY_BIN`, `NEW_RELAY_WASM`, `NEW_PARA_BIN`, `NEW_PARA_WASM`.');126    process.exit(1);127  }128  */129130  // an unsavory practice, but very convenient, mwahahah131  process.env.PARA_DIR = PARA_DIR;132  process.env.RELAY_DIR = RELAY_DIR;133  process.env.REPLICA_FROM = REPLICA_FROM;134135  const configPath = resolve(NETWORK_CONFIG_FILE);136  const networkConfig = readNetworkConfig(configPath);137  // console.log(networkConfig);138  if(networkConfig.settings.provider !== 'native') {139    throw new Error(`Oh no! Expected native network, got ${networkConfig.settings.provider}.`);140  }141142  await cryptoWaitReady();143144  // Launch Zombienet!145  network = await zombie.start(ZOMBIENET_CREDENTIALS, networkConfig, {silent: false});146147  // Get the relay chain info like the epoch length and spec version148  // Then restart each parachain's binaries149  // // Stop and restart each node150  // // Send specified keys to parachain nodes in case the parachain requires it151  // If it is not needed to upgrade runtimes themselves, the job is done!152153  // Record some required information regarding the relay chain154  await network.relay[0].connectApi();155  let relayInfo = getRelayInfo((network.relay[0] as any).apiInstance!);156  await network.relay[0].apiInstance!.disconnect();157  if(isUpgradeTesting) {158    console.log('Relay stats:', relayInfo);159  }160161  // non-exported functionality of NativeClient162  const networkClient = (network.client as any);163164  if(NEW_RELAY_BIN) {165    console.log('\n🧶 Restarting relay nodes');166167    for(const [index, node] of network.relay.entries()) {168      await node.apiInstance?.disconnect();169170      console.log(`\n🚦 Starting timeout for the epoch change (node ${index + 1}/${network.relay.length})...`);171      await waitWithTimer(relayInfo.epochTime);172173      // Replace the node-starting command with the new binary174      const cmd = networkClient.processMap[node.name].cmd[0].split(' ')[0];175      networkClient.processMap[node.name].cmd = networkClient.processMap[node.name].cmd.map((arg: string) => arg.replace(cmd, NEW_RELAY_BIN));176177      await node.restart();178    }179180    console.log('\n🌒 All relay nodes restarted with the new binaries.');181  }182183  if(NEW_PARA_BIN) {184    for(const paraId in network.paras) {185      const para = network.paras[paraId];186      console.log(`\n🧶 Restarting collator nodes of parachain ${paraId}`);187188      for(const [_index, node] of para.nodes.entries()) {189        await node.apiInstance?.disconnect();190191        // Replace the node-starting command with the new binary192        const cmd = networkClient.processMap[node.name].cmd[0].split(' ')[0];193        networkClient.processMap[node.name].cmd = networkClient.processMap[node.name].cmd.map((arg: string) => arg.replace(cmd, NEW_PARA_BIN));194195        await node.restart();196        // applyaurakey?197        // Zombienet handles it on first-time node creation198      }199    }200201    console.log('\n🌗 All parachain collators restarted with the new binaries.');202  }203204  // Re-establish connection to the relay node and get the runtime upgrade validation delay for parachains205  // For the relay, connect and set the new runtime code206  // For each parachain, connect, authorize and upgrade its runtime207  // Ping the the chains for the runtime upgrade after the minimal time and then every few blocks208  // // For each parachain, re-connect and verify that the runtime upgrade is successful209210  let relayUpgradeCompleted = false, paraUpgradeCompleted = false;211212  if(NEW_RELAY_WASM) {213    const relayOldVersion = relayInfo.specVersion;214    console.log('\n🚦 Starting timeout for the next epoch before upgrading the relay runtime code...');215    await waitWithTimer(relayInfo.epochTime);216217    console.log('--- Upgrading the relay chain runtime \t---');218219    // Read the new WASM code and set it as the relay's new code220    const code = fs.readFileSync(NEW_RELAY_WASM).toString('hex');221    await usingPlaygrounds(async (helper, privateKey) => {222      const superuser = await privateKey(SUPERUSER_KEY);223224      const result = await helper.executeExtrinsic(225        superuser,226        'api.tx.sudo.sudoUncheckedWeight',227        [helper.constructApiCall('api.tx.system.setCode', [`0x${code}`]), {}],228      );229230      if(result.status == 'Fail') {231        console.error('Failed to upgrade the runtime:', result);232      }233234      // Get the updated information from the relay's new runtime235      relayInfo = getRelayInfo(helper.getApi());236    }, network.relay[0].wsUri);237238    if(relayOldVersion != relayInfo.specVersion) {239      // eslint-disable-next-line no-useless-escape240      console.log(`\n\🛰️ The relay has successfully upgraded from version ${relayOldVersion} to ${relayInfo.specVersion}!`);241      relayUpgradeCompleted = true;242    } else {243      console.error(`\nThe relay did not upgrade from version ${relayOldVersion}!`);244    }245  } else {246    // If the relay did not need to be upgraded, it's already technically completed247    relayUpgradeCompleted = true;248  }249250  if(NEW_PARA_WASM) {251    let codeValidationDelayBlocks = 0;252    const upgradingParas: {[id: string]: {version: number, upgraded: boolean}} = {};253    // Calculate the code validation delay of the relay chain,254    // so that we know how much to wait before the parachains can be upgraded after the extrinsic255    await usingPlaygrounds(async (helper) => {256      const {validationUpgradeDelay, minimumValidationUpgradeDelay} =257        (await helper.callRpc('api.query.configuration.activeConfig', [])).toJSON() as any;258259      codeValidationDelayBlocks = Math.max(validationUpgradeDelay ?? 0, minimumValidationUpgradeDelay ?? 0);260    }, network.relay[0].wsUri);261262    // Wait for the next epoch so that the parachains will start cooperating with the relay263    if(relayUpgradeCompleted && NEW_RELAY_WASM) {264      console.log('\n🚥 Starting timeout for the next epoch before upgrading the parachains code...');265      await waitWithTimer(relayInfo.epochTime);266    }267268    const migration = migrations[DESTINATION_SPEC_VERSION];269    console.log('⭐️⭐️⭐️ DESTINATION_SPEC_VERSION ⭐️⭐️⭐️', DESTINATION_SPEC_VERSION);270    for(const paraId in network.paras) {271      console.log(`\n--- Upgrading the runtime of parachain ${paraId} \t---`);272      const para = network.paras[paraId];273274      // Enable maintenance mode if present275      await toggleMaintenanceMode(true, para.nodes[0].wsUri);276      if(migration) {277        console.log('⭐️⭐️⭐️ Running pre-upgrade scripts... ⭐️⭐️⭐️');278        await migration.before();279      }280281      // Read the WASM code and authorize the upgrade with its hash and set it as the new runtime282      const code = fs.readFileSync(NEW_PARA_WASM);283      const codeHash = blake2AsHex(code);284      await usingPlaygrounds(async (helper, privateKey) => {285        const superuser = await privateKey(SUPERUSER_KEY);286287        upgradingParas[paraId] = {version: getSpecVersion(helper.getApi()), upgraded: false};288289        console.log('--- Authorizing the parachain runtime upgrade \t---');290        let result = await helper.executeExtrinsic(291          superuser,292          'api.tx.sudo.sudoUncheckedWeight',293          [helper.constructApiCall('api.tx.parachainSystem.authorizeUpgrade', [codeHash, false]), {}],294        );295296        if(result.status == 'Fail') {297          console.error('Failed to authorize the upgrade:', result);298          return;299        }300301        console.log('--- Enacting the parachain runtime upgrade \t---');302        result = await helper.executeExtrinsic(303          superuser,304          'api.tx.sudo.sudoUncheckedWeight',305          [helper.constructApiCall('api.tx.parachainSystem.enactAuthorizedUpgrade', [`0x${code.toString('hex')}`]), {}],306        );307308        if(result.status == 'Fail') {309          console.error('Failed to upgrade the runtime:', result);310        }311      }, para.nodes[0].wsUri);312    }313314    // Check the upgrades of the parachains, first after the minimum code validation delay, and then after some block time increments315    let firstPass = true;316    for(let attempt = 0; attempt < 3 && !paraUpgradeCompleted; attempt++) {317      if(firstPass) {318        console.log('\nCode validation delay:', codeValidationDelayBlocks, 'blocks');319        console.log('🚥 Waiting for the minimum code validation delay before the parachain can upgrade...');320        await waitWithTimer(relayInfo.blockTime * codeValidationDelayBlocks);321        firstPass = false;322      } else {323        console.log('\n🚥 Waiting for a few blocks more to verify that the parachain upgrades are successful...');324        await waitWithTimer(PARACHAIN_BLOCK_TIME * 3);325      }326327      // Ping the parachains' nodes for new runtime versions328      let upgradeFailed = false;329      for(const paraId in network.paras) {330        if(upgradingParas[paraId].upgraded) continue;331332        const para = network.paras[paraId];333        // eslint-disable-next-line require-await334        await usingPlaygrounds(async (helper) => {335          const specVersion = getSpecVersion(helper.getApi());336337          if(specVersion != upgradingParas[paraId].version) {338            // eslint-disable-next-line no-useless-escape339            console.log(`\n\🛰️  Parachain ${paraId} has successfully upgraded from version ${upgradingParas[paraId].version} to ${specVersion}!`);340            upgradingParas[paraId].upgraded = true;341          } else {342            console.error(`\nParachain ${paraId} failed to upgrade from version ${upgradingParas[paraId].version}!`);343            upgradeFailed = true;344          }345        }, para.nodes[0].wsUri);346347        paraUpgradeCompleted = !upgradeFailed;348      }349    }350351    // Disable maintenance mode if present352    for(const paraId in network.paras) {353      // TODO only if our parachain354      if(migration) {355        console.log('⭐️⭐️⭐️ Running post-upgrade scripts... ⭐️⭐️⭐️');356        await migration.after();357      }358      await toggleMaintenanceMode(false, network.paras[paraId].nodes[0].wsUri);359    }360  } else {361    // If the relay did not need to be upgraded, it's already technically completed362    paraUpgradeCompleted = true;363  }364365  // await network.stop();366367  if(isUpgradeTesting) {368    if(paraUpgradeCompleted && relayUpgradeCompleted) {369      console.log("\n🛸 PARACHAINS' RUNTIME UPGRADE TESTING COMPLETE 🛸");370    } else {371      console.error("\n🚧 PARACHAINS' RUNTIME UPGRADE TESTING FAILED 🚧");372    }373  } else {374    console.log('🚀 ZOMBIENET RAISED 🚀');375  }376};377378raiseZombienet()/*.then(async () => await stop())*/.catch(async (e) => {379  console.error(e);380  await stop();381  process.exit(1);382});