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

difftreelog

add before and after hooks to frankenstein

Max Andreev2023-06-09parent: #e38520f.patch.diff
in: master

3 files changed

modified.envdiffbeforeafterboth
--- a/.env
+++ b/.env
@@ -2,6 +2,7 @@
 POLKADOT_LAUNCH_BRANCH=unique-network
 RELAY_CHAIN_TYPE=westend
 CHAINQL=v0.4.1
+DESTINATION_SPEC_VERSION=v942057
 
 POLKADOT_MAINNET_BRANCH=release-v0.9.37
 STATEMINT_BUILD_BRANCH=release-parachains-v9370
modifiedtests/src/util/frankenstein.tsdiffbeforeafterboth
before · tests/src/util/frankenstein.ts
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 fs from 'fs';2425const ZOMBIENET_CREDENTIALS = process.env.ZOMBIENET_CREDENTIALS || '../.env';26const NETWORK_CONFIG_FILE = process.argv[2] ?? '../launch-zombienet.toml';27const PARA_DIR = process.env.PARA_DIR || '../';28const RELAY_DIR = process.env.RELAY_DIR || '../../polkadot/';29const REPLICA_FROM = process.env.REPLICA_FROM || 'wss://ws-opal.unique.network:443';30const NEW_RELAY_BIN = process.env.NEW_RELAY_BIN;31const NEW_RELAY_WASM = process.env.NEW_RELAY_WASM;32const NEW_PARA_BIN = process.env.NEW_PARA_BIN;33const NEW_PARA_WASM = process.env.NEW_PARA_WASM;34const PARACHAIN_BLOCK_TIME = 12_000;35const SUPERUSER_KEY = '//Alice';3637let network: zombie.Network | undefined;3839// Stop the network if it is running40const stop = async () => {41  await network?.stop();42};4344// Promise of a timeout45function delay(ms: number) {46  return new Promise(resolve => setTimeout(resolve, ms));47}4849// Countdown with time left on display50async function waitWithTimer(time: number) {51  const secondsTotal = Math.ceil(time / 1000);52  for(let i = secondsTotal; i > 0; i--) {53    // could also introduce hours, but wth54    const seconds = i % 60;55    const text = `Time left: ${Math.floor(i / 60)}:${seconds < 10 ? '0' + seconds : seconds}`;56    if(process.stdout.isTTY)57      process.stdout.write(text);58    else if(seconds % 10 == 0)59      console.log(text);60    await delay(1000);61    if(process.stdout.isTTY) {62      process.stdout.clearLine(0);63      process.stdout.cursorTo(0);64    }65  }66}6768// Get the runtime's current version69function getSpecVersion(api: ApiPromise): number {70  return (api.consts.system.version as any).specVersion.toNumber();71}7273// Get the required information on the relay chain74function getRelayInfo(api: ApiPromise): {specVersion: number, epochBlockLength: number, blockTime: number, epochTime: number} {75  const info = {76    specVersion: getSpecVersion(api),77    epochBlockLength: (api.consts.babe.epochDuration as any).toNumber(),78    blockTime: (api.consts.babe.expectedBlockTime as any).toNumber(),79    epochTime: 0,80  };81  info.epochTime = info.epochBlockLength * info.blockTime;82  return info;83}8485// Enable or disable maintenance mode if present on the chain86async function toggleMaintenanceMode(value: boolean, wsUri: string) {87  await usingPlaygrounds(async (helper, privateKey) => {88    const superuser = await privateKey(SUPERUSER_KEY);89    try {90      const toggle = value ? 'enable' : 'disable';91      await helper.getSudo().executeExtrinsic(superuser, `api.tx.maintenance.${toggle}`, []);92      console.log(`Maintenance mode ${value ? 'engaged' : 'disengaged'}.`);93    } catch (e) {94      console.error('Couldn\'t set maintenance mode. The maintenance pallet probably does not exist. Log:', e);95    }96  }, wsUri);97}9899const raiseZombienet = async (): Promise<void> => {100  const isUpgradeTesting = !!NEW_RELAY_BIN || !!NEW_RELAY_WASM || !!NEW_PARA_BIN || !!NEW_PARA_WASM;101  /*102  // If there is nothing to upgrade, what is the point103  if (!isUpgradeTesting) {104    console.warn('\nNeither the relay nor the parachain were selected for an upgrade! ' +105      'Please pass environment vars `NEW_RELAY_BIN`, `NEW_RELAY_WASM`, `NEW_PARA_BIN`, `NEW_PARA_WASM`.');106    process.exit(1);107  }108  */109110  // an unsavory practice, but very convenient, mwahahah111  process.env.PARA_DIR = PARA_DIR;112  process.env.RELAY_DIR = RELAY_DIR;113  process.env.REPLICA_FROM = REPLICA_FROM;114115  const configPath = resolve(NETWORK_CONFIG_FILE);116  const networkConfig = readNetworkConfig(configPath);117  // console.log(networkConfig);118  if(networkConfig.settings.provider !== 'native') {119    throw new Error(`Oh no! Expected native network, got ${networkConfig.settings.provider}.`);120  }121122  await cryptoWaitReady();123124  // Launch Zombienet!125  network = await zombie.start(ZOMBIENET_CREDENTIALS, networkConfig, {silent: false});126127  // Get the relay chain info like the epoch length and spec version128  // Then restart each parachain's binaries129  // // Stop and restart each node130  // // Send specified keys to parachain nodes in case the parachain requires it131  // If it is not needed to upgrade runtimes themselves, the job is done!132133  // Record some required information regarding the relay chain134  await network.relay[0].connectApi();135  let relayInfo = getRelayInfo((network.relay[0] as any).apiInstance!);136  await network.relay[0].apiInstance!.disconnect();137  if(isUpgradeTesting) {138    console.log('Relay stats:', relayInfo);139  }140141  // non-exported functionality of NativeClient142  const networkClient = (network.client as any);143144  if(NEW_RELAY_BIN) {145    console.log('\n🧶 Restarting relay nodes');146147    for(const [index, node] of network.relay.entries()) {148      await node.apiInstance?.disconnect();149150      console.log(`\n🚦 Starting timeout for the epoch change (node ${index + 1}/${network.relay.length})...`);151      await waitWithTimer(relayInfo.epochTime);152153      // Replace the node-starting command with the new binary154      const cmd = networkClient.processMap[node.name].cmd[0].split(' ')[0];155      networkClient.processMap[node.name].cmd = networkClient.processMap[node.name].cmd.map((arg: string) => arg.replace(cmd, NEW_RELAY_BIN));156157      await node.restart();158    }159160    console.log('\n🌒 All relay nodes restarted with the new binaries.');161  }162163  if(NEW_PARA_BIN) {164    for(const paraId in network.paras) {165      const para = network.paras[paraId];166      console.log(`\n🧶 Restarting collator nodes of parachain ${paraId}`);167168      for(const [_index, node] of para.nodes.entries()) {169        await node.apiInstance?.disconnect();170171        // Replace the node-starting command with the new binary172        const cmd = networkClient.processMap[node.name].cmd[0].split(' ')[0];173        networkClient.processMap[node.name].cmd = networkClient.processMap[node.name].cmd.map((arg: string) => arg.replace(cmd, NEW_PARA_BIN));174175        await node.restart();176        // applyaurakey?177        // Zombienet handles it on first-time node creation178      }179    }180181    console.log('\n🌗 All parachain collators restarted with the new binaries.');182  }183184  // Re-establish connection to the relay node and get the runtime upgrade validation delay for parachains185  // For the relay, connect and set the new runtime code186  // For each parachain, connect, authorize and upgrade its runtime187  // Ping the the chains for the runtime upgrade after the minimal time and then every few blocks188  // // For each parachain, re-connect and verify that the runtime upgrade is successful189190  let relayUpgradeCompleted = false, paraUpgradeCompleted = false;191192  if(NEW_RELAY_WASM) {193    const relayOldVersion = relayInfo.specVersion;194    console.log('\n🚦 Starting timeout for the next epoch before upgrading the relay runtime code...');195    await waitWithTimer(relayInfo.epochTime);196197    console.log('--- Upgrading the relay chain runtime \t---');198199    // Read the new WASM code and set it as the relay's new code200    const code = fs.readFileSync(NEW_RELAY_WASM).toString('hex');201    await usingPlaygrounds(async (helper, privateKey) => {202      const superuser = await privateKey(SUPERUSER_KEY);203204      const result = await helper.executeExtrinsic(205        superuser,206        'api.tx.sudo.sudoUncheckedWeight',207        [helper.constructApiCall('api.tx.system.setCode', [`0x${code}`]), {}],208      );209210      if(result.status == 'Fail') {211        console.error('Failed to upgrade the runtime:', result);212      }213214      // Get the updated information from the relay's new runtime215      relayInfo = getRelayInfo(helper.getApi());216    }, network.relay[0].wsUri);217218    if(relayOldVersion != relayInfo.specVersion) {219      // eslint-disable-next-line no-useless-escape220      console.log(`\n\🛰️ The relay has successfully upgraded from version ${relayOldVersion} to ${relayInfo.specVersion}!`);221      relayUpgradeCompleted = true;222    } else {223      console.error(`\nThe relay did not upgrade from version ${relayOldVersion}!`);224    }225  } else {226    // If the relay did not need to be upgraded, it's already technically completed227    relayUpgradeCompleted = true;228  }229230  if(NEW_PARA_WASM) {231    let codeValidationDelayBlocks = 0;232    const upgradingParas: {[id: string]: {version: number, upgraded: boolean}} = {};233    // Calculate the code validation delay of the relay chain,234    // so that we know how much to wait before the parachains can be upgraded after the extrinsic235    await usingPlaygrounds(async (helper) => {236      const {validationUpgradeDelay, minimumValidationUpgradeDelay} =237        (await helper.callRpc('api.query.configuration.activeConfig', [])).toJSON() as any;238239      codeValidationDelayBlocks = Math.max(validationUpgradeDelay ?? 0, minimumValidationUpgradeDelay ?? 0);240    }, network.relay[0].wsUri);241242    // Wait for the next epoch so that the parachains will start cooperating with the relay243    if(relayUpgradeCompleted && NEW_RELAY_WASM) {244      console.log('\n🚥 Starting timeout for the next epoch before upgrading the parachains code...');245      await waitWithTimer(relayInfo.epochTime);246    }247248    for(const paraId in network.paras) {249      console.log(`\n--- Upgrading the runtime of parachain ${paraId} \t---`);250      const para = network.paras[paraId];251252      // Enable maintenance mode if present253      await toggleMaintenanceMode(true, para.nodes[0].wsUri);254255      // Read the WASM code and authorize the upgrade with its hash and set it as the new runtime256      const code = fs.readFileSync(NEW_PARA_WASM);257      const codeHash = blake2AsHex(code);258      await usingPlaygrounds(async (helper, privateKey) => {259        const superuser = await privateKey(SUPERUSER_KEY);260261        upgradingParas[paraId] = {version: getSpecVersion(helper.getApi()), upgraded: false};262263        console.log('--- Authorizing the parachain runtime upgrade \t---');264        let result = await helper.executeExtrinsic(265          superuser,266          'api.tx.sudo.sudoUncheckedWeight',267          [helper.constructApiCall('api.tx.parachainSystem.authorizeUpgrade', [codeHash, false]), {}],268        );269270        if(result.status == 'Fail') {271          console.error('Failed to authorize the upgrade:', result);272          return;273        }274275        console.log('--- Enacting the parachain runtime upgrade \t---');276        result = await helper.executeExtrinsic(277          superuser,278          'api.tx.sudo.sudoUncheckedWeight',279          [helper.constructApiCall('api.tx.parachainSystem.enactAuthorizedUpgrade', [`0x${code.toString('hex')}`]), {}],280        );281282        if(result.status == 'Fail') {283          console.error('Failed to upgrade the runtime:', result);284        }285      }, para.nodes[0].wsUri);286    }287288    // Check the upgrades of the parachains, first after the minimum code validation delay, and then after some block time increments289    let firstPass = true;290    for(let attempt = 0; attempt < 3 && !paraUpgradeCompleted; attempt++) {291      if(firstPass) {292        console.log('\nCode validation delay:', codeValidationDelayBlocks, 'blocks');293        console.log('🚥 Waiting for the minimum code validation delay before the parachain can upgrade...');294        await waitWithTimer(relayInfo.blockTime * codeValidationDelayBlocks);295        firstPass = false;296      } else {297        console.log('\n🚥 Waiting for a few blocks more to verify that the parachain upgrades are successful...');298        await waitWithTimer(PARACHAIN_BLOCK_TIME * 3);299      }300301      // Ping the parachains' nodes for new runtime versions302      let upgradeFailed = false;303      for(const paraId in network.paras) {304        if(upgradingParas[paraId].upgraded) continue;305306        const para = network.paras[paraId];307        // eslint-disable-next-line require-await308        await usingPlaygrounds(async (helper) => {309          const specVersion = getSpecVersion(helper.getApi());310311          if(specVersion != upgradingParas[paraId].version) {312            // eslint-disable-next-line no-useless-escape313            console.log(`\n\🛰️  Parachain ${paraId} has successfully upgraded from version ${upgradingParas[paraId].version} to ${specVersion}!`);314            upgradingParas[paraId].upgraded = true;315          } else {316            console.error(`\nParachain ${paraId} failed to upgrade from version ${upgradingParas[paraId].version}!`);317            upgradeFailed = true;318          }319        }, para.nodes[0].wsUri);320321        paraUpgradeCompleted = !upgradeFailed;322      }323    }324325    // Disable maintenance mode if present326    for(const paraId in network.paras) {327      await toggleMaintenanceMode(false, network.paras[paraId].nodes[0].wsUri);328    }329  } else {330    // If the relay did not need to be upgraded, it's already technically completed331    paraUpgradeCompleted = true;332  }333334  // await network.stop();335336  if(isUpgradeTesting) {337    if(paraUpgradeCompleted && relayUpgradeCompleted) {338      console.log("\n🛸 PARACHAINS' RUNTIME UPGRADE TESTING COMPLETE 🛸");339    } else {340      console.error("\n🚧 PARACHAINS' RUNTIME UPGRADE TESTING FAILED 🚧");341    }342  } else {343    console.log('🚀 ZOMBIENET RAISED 🚀');344  }345};346347raiseZombienet()/*.then(async () => await stop())*/.catch(async (e) => {348  console.error(e);349  await stop();350  process.exit(1);351});
after · tests/src/util/frankenstein.ts
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 PARACHAIN_BLOCK_TIME = 12_000;36const SUPERUSER_KEY = '//Alice';3738let network: zombie.Network | undefined;3940// Stop the network if it is running41const stop = async () => {42  await network?.stop();43};4445// Promise of a timeout46function delay(ms: number) {47  return new Promise(resolve => setTimeout(resolve, ms));48}4950// Countdown with time left on display51async function waitWithTimer(time: number) {52  const secondsTotal = Math.ceil(time / 1000);53  for(let i = secondsTotal; i > 0; i--) {54    // could also introduce hours, but wth55    const seconds = i % 60;56    const text = `Time left: ${Math.floor(i / 60)}:${seconds < 10 ? '0' + seconds : seconds}`;57    if(process.stdout.isTTY)58      process.stdout.write(text);59    else if(seconds % 10 == 0)60      console.log(text);61    await delay(1000);62    if(process.stdout.isTTY) {63      process.stdout.clearLine(0);64      process.stdout.cursorTo(0);65    }66  }67}6869// Get the runtime's current version70function getSpecVersion(api: ApiPromise): number {71  return (api.consts.system.version as any).specVersion.toNumber();72}7374// Get the required information on the relay chain75function getRelayInfo(api: ApiPromise): {specVersion: number, epochBlockLength: number, blockTime: number, epochTime: number} {76  const info = {77    specVersion: getSpecVersion(api),78    epochBlockLength: (api.consts.babe.epochDuration as any).toNumber(),79    blockTime: (api.consts.babe.expectedBlockTime as any).toNumber(),80    epochTime: 0,81  };82  info.epochTime = info.epochBlockLength * info.blockTime;83  return info;84}8586// Enable or disable maintenance mode if present on the chain87async function toggleMaintenanceMode(value: boolean, wsUri: string) {88  await usingPlaygrounds(async (helper, privateKey) => {89    const superuser = await privateKey(SUPERUSER_KEY);90    try {91      const toggle = value ? 'enable' : 'disable';92      await helper.getSudo().executeExtrinsic(superuser, `api.tx.maintenance.${toggle}`, []);93      console.log(`Maintenance mode ${value ? 'engaged' : 'disengaged'}.`);94    } catch (e) {95      console.error('Couldn\'t set maintenance mode. The maintenance pallet probably does not exist. Log:', e);96    }97  }, wsUri);98}99100const raiseZombienet = async (): Promise<void> => {101  const isUpgradeTesting = !!NEW_RELAY_BIN || !!NEW_RELAY_WASM || !!NEW_PARA_BIN || !!NEW_PARA_WASM;102  /*103  // If there is nothing to upgrade, what is the point104  if (!isUpgradeTesting) {105    console.warn('\nNeither the relay nor the parachain were selected for an upgrade! ' +106      'Please pass environment vars `NEW_RELAY_BIN`, `NEW_RELAY_WASM`, `NEW_PARA_BIN`, `NEW_PARA_WASM`.');107    process.exit(1);108  }109  */110111  // an unsavory practice, but very convenient, mwahahah112  process.env.PARA_DIR = PARA_DIR;113  process.env.RELAY_DIR = RELAY_DIR;114  process.env.REPLICA_FROM = REPLICA_FROM;115116  const configPath = resolve(NETWORK_CONFIG_FILE);117  const networkConfig = readNetworkConfig(configPath);118  // console.log(networkConfig);119  if(networkConfig.settings.provider !== 'native') {120    throw new Error(`Oh no! Expected native network, got ${networkConfig.settings.provider}.`);121  }122123  await cryptoWaitReady();124125  // Launch Zombienet!126  network = await zombie.start(ZOMBIENET_CREDENTIALS, networkConfig, {silent: false});127128  // Get the relay chain info like the epoch length and spec version129  // Then restart each parachain's binaries130  // // Stop and restart each node131  // // Send specified keys to parachain nodes in case the parachain requires it132  // If it is not needed to upgrade runtimes themselves, the job is done!133134  // Record some required information regarding the relay chain135  await network.relay[0].connectApi();136  let relayInfo = getRelayInfo((network.relay[0] as any).apiInstance!);137  await network.relay[0].apiInstance!.disconnect();138  if(isUpgradeTesting) {139    console.log('Relay stats:', relayInfo);140  }141142  // non-exported functionality of NativeClient143  const networkClient = (network.client as any);144145  if(NEW_RELAY_BIN) {146    console.log('\n🧶 Restarting relay nodes');147148    for(const [index, node] of network.relay.entries()) {149      await node.apiInstance?.disconnect();150151      console.log(`\n🚦 Starting timeout for the epoch change (node ${index + 1}/${network.relay.length})...`);152      await waitWithTimer(relayInfo.epochTime);153154      // Replace the node-starting command with the new binary155      const cmd = networkClient.processMap[node.name].cmd[0].split(' ')[0];156      networkClient.processMap[node.name].cmd = networkClient.processMap[node.name].cmd.map((arg: string) => arg.replace(cmd, NEW_RELAY_BIN));157158      await node.restart();159    }160161    console.log('\n🌒 All relay nodes restarted with the new binaries.');162  }163164  if(NEW_PARA_BIN) {165    for(const paraId in network.paras) {166      const para = network.paras[paraId];167      console.log(`\n🧶 Restarting collator nodes of parachain ${paraId}`);168169      for(const [_index, node] of para.nodes.entries()) {170        await node.apiInstance?.disconnect();171172        // Replace the node-starting command with the new binary173        const cmd = networkClient.processMap[node.name].cmd[0].split(' ')[0];174        networkClient.processMap[node.name].cmd = networkClient.processMap[node.name].cmd.map((arg: string) => arg.replace(cmd, NEW_PARA_BIN));175176        await node.restart();177        // applyaurakey?178        // Zombienet handles it on first-time node creation179      }180    }181182    console.log('\n🌗 All parachain collators restarted with the new binaries.');183  }184185  // Re-establish connection to the relay node and get the runtime upgrade validation delay for parachains186  // For the relay, connect and set the new runtime code187  // For each parachain, connect, authorize and upgrade its runtime188  // Ping the the chains for the runtime upgrade after the minimal time and then every few blocks189  // // For each parachain, re-connect and verify that the runtime upgrade is successful190191  let relayUpgradeCompleted = false, paraUpgradeCompleted = false;192193  if(NEW_RELAY_WASM) {194    const relayOldVersion = relayInfo.specVersion;195    console.log('\n🚦 Starting timeout for the next epoch before upgrading the relay runtime code...');196    await waitWithTimer(relayInfo.epochTime);197198    console.log('--- Upgrading the relay chain runtime \t---');199200    // Read the new WASM code and set it as the relay's new code201    const code = fs.readFileSync(NEW_RELAY_WASM).toString('hex');202    await usingPlaygrounds(async (helper, privateKey) => {203      const superuser = await privateKey(SUPERUSER_KEY);204205      const result = await helper.executeExtrinsic(206        superuser,207        'api.tx.sudo.sudoUncheckedWeight',208        [helper.constructApiCall('api.tx.system.setCode', [`0x${code}`]), {}],209      );210211      if(result.status == 'Fail') {212        console.error('Failed to upgrade the runtime:', result);213      }214215      // Get the updated information from the relay's new runtime216      relayInfo = getRelayInfo(helper.getApi());217    }, network.relay[0].wsUri);218219    if(relayOldVersion != relayInfo.specVersion) {220      // eslint-disable-next-line no-useless-escape221      console.log(`\n\🛰️ The relay has successfully upgraded from version ${relayOldVersion} to ${relayInfo.specVersion}!`);222      relayUpgradeCompleted = true;223    } else {224      console.error(`\nThe relay did not upgrade from version ${relayOldVersion}!`);225    }226  } else {227    // If the relay did not need to be upgraded, it's already technically completed228    relayUpgradeCompleted = true;229  }230231  if(NEW_PARA_WASM) {232    let codeValidationDelayBlocks = 0;233    const upgradingParas: {[id: string]: {version: number, upgraded: boolean}} = {};234    // Calculate the code validation delay of the relay chain,235    // so that we know how much to wait before the parachains can be upgraded after the extrinsic236    await usingPlaygrounds(async (helper) => {237      const {validationUpgradeDelay, minimumValidationUpgradeDelay} =238        (await helper.callRpc('api.query.configuration.activeConfig', [])).toJSON() as any;239240      codeValidationDelayBlocks = Math.max(validationUpgradeDelay ?? 0, minimumValidationUpgradeDelay ?? 0);241    }, network.relay[0].wsUri);242243    // Wait for the next epoch so that the parachains will start cooperating with the relay244    if(relayUpgradeCompleted && NEW_RELAY_WASM) {245      console.log('\n🚥 Starting timeout for the next epoch before upgrading the parachains code...');246      await waitWithTimer(relayInfo.epochTime);247    }248249    const migration = migrations[process.env.DESTINATION_SPEC_VERSION!];250    for(const paraId in network.paras) {251      console.log(`\n--- Upgrading the runtime of parachain ${paraId} \t---`);252      const para = network.paras[paraId];253254      // Enable maintenance mode if present255      await toggleMaintenanceMode(true, para.nodes[0].wsUri);256      if(migration) await migration.before();257258      // Read the WASM code and authorize the upgrade with its hash and set it as the new runtime259      const code = fs.readFileSync(NEW_PARA_WASM);260      const codeHash = blake2AsHex(code);261      await usingPlaygrounds(async (helper, privateKey) => {262        const superuser = await privateKey(SUPERUSER_KEY);263264        upgradingParas[paraId] = {version: getSpecVersion(helper.getApi()), upgraded: false};265266        console.log('--- Authorizing the parachain runtime upgrade \t---');267        let result = await helper.executeExtrinsic(268          superuser,269          'api.tx.sudo.sudoUncheckedWeight',270          [helper.constructApiCall('api.tx.parachainSystem.authorizeUpgrade', [codeHash, false]), {}],271        );272273        if(result.status == 'Fail') {274          console.error('Failed to authorize the upgrade:', result);275          return;276        }277278        console.log('--- Enacting the parachain runtime upgrade \t---');279        result = await helper.executeExtrinsic(280          superuser,281          'api.tx.sudo.sudoUncheckedWeight',282          [helper.constructApiCall('api.tx.parachainSystem.enactAuthorizedUpgrade', [`0x${code.toString('hex')}`]), {}],283        );284285        if(result.status == 'Fail') {286          console.error('Failed to upgrade the runtime:', result);287        }288      }, para.nodes[0].wsUri);289    }290291    // Check the upgrades of the parachains, first after the minimum code validation delay, and then after some block time increments292    let firstPass = true;293    for(let attempt = 0; attempt < 3 && !paraUpgradeCompleted; attempt++) {294      if(firstPass) {295        console.log('\nCode validation delay:', codeValidationDelayBlocks, 'blocks');296        console.log('🚥 Waiting for the minimum code validation delay before the parachain can upgrade...');297        await waitWithTimer(relayInfo.blockTime * codeValidationDelayBlocks);298        firstPass = false;299      } else {300        console.log('\n🚥 Waiting for a few blocks more to verify that the parachain upgrades are successful...');301        await waitWithTimer(PARACHAIN_BLOCK_TIME * 3);302      }303304      // Ping the parachains' nodes for new runtime versions305      let upgradeFailed = false;306      for(const paraId in network.paras) {307        if(upgradingParas[paraId].upgraded) continue;308309        const para = network.paras[paraId];310        // eslint-disable-next-line require-await311        await usingPlaygrounds(async (helper) => {312          const specVersion = getSpecVersion(helper.getApi());313314          if(specVersion != upgradingParas[paraId].version) {315            // eslint-disable-next-line no-useless-escape316            console.log(`\n\🛰️  Parachain ${paraId} has successfully upgraded from version ${upgradingParas[paraId].version} to ${specVersion}!`);317            upgradingParas[paraId].upgraded = true;318          } else {319            console.error(`\nParachain ${paraId} failed to upgrade from version ${upgradingParas[paraId].version}!`);320            upgradeFailed = true;321          }322        }, para.nodes[0].wsUri);323324        paraUpgradeCompleted = !upgradeFailed;325      }326    }327328    // Disable maintenance mode if present329    for(const paraId in network.paras) {330      // TODO only if our parachain331      if(migration) await migration.after();332      await toggleMaintenanceMode(false, network.paras[paraId].nodes[0].wsUri);333    }334  } else {335    // If the relay did not need to be upgraded, it's already technically completed336    paraUpgradeCompleted = true;337  }338339  // await network.stop();340341  if(isUpgradeTesting) {342    if(paraUpgradeCompleted && relayUpgradeCompleted) {343      console.log("\n🛸 PARACHAINS' RUNTIME UPGRADE TESTING COMPLETE 🛸");344    } else {345      console.error("\n🚧 PARACHAINS' RUNTIME UPGRADE TESTING FAILED 🚧");346    }347  } else {348    console.log('🚀 ZOMBIENET RAISED 🚀');349  }350};351352raiseZombienet()/*.then(async () => await stop())*/.catch(async (e) => {353  console.error(e);354  await stop();355  process.exit(1);356});
addedtests/src/util/frankensteinMigrate.tsdiffbeforeafterboth
--- /dev/null
+++ b/tests/src/util/frankensteinMigrate.ts
@@ -0,0 +1,9 @@
+import {migration as locksToFreezesMigration} from '../migrations/942057-appPromotion';
+export interface Migration {
+  before: () => Promise<void>,
+  after: () => Promise<void>,
+}
+
+export const migrations: {[key: string]: Migration} = {
+  'v942057': locksToFreezesMigration,
+};