difftreelog
skip worflow if already upgraded
in: master
- fix manual migration script: use dirname - update envs
3 files changed
.envdiffbeforeafterboth--- a/.env
+++ b/.env
@@ -17,12 +17,12 @@
KARURA_BUILD_BRANCH=release-karura-2.17.0
MOONRIVER_BUILD_BRANCH=runtime-2302
SHIDEN_BUILD_BRANCH=v5.4.0
-QUARTZ_MAINNET_BRANCH=release-v941055
+QUARTZ_MAINNET_BRANCH=release-v941056
QUARTZ_REPLICA_FROM=wss://ws-quartz.unique.network:443
UNIQUEWEST_MAINNET_BRANCH=release-v0.9.42
WESTMINT_BUILD_BRANCH=parachains-v9420
-OPAL_MAINNET_BRANCH=release-v941055
+OPAL_MAINNET_BRANCH=release-v942057
OPAL_REPLICA_FROM=wss://ws-opal.unique.network:443
UNIQUEEAST_MAINNET_BRANCH=release-v0.9.42
tests/src/migrations/942057-appPromotion/lockedToFreeze.tsdiffbeforeafterboth--- a/tests/src/migrations/942057-appPromotion/lockedToFreeze.ts
+++ b/tests/src/migrations/942057-appPromotion/lockedToFreeze.ts
@@ -1,8 +1,9 @@
// import { usingApi, privateKey, onlySign } from "./../../load/lib";
import * as fs from 'fs';
import {usingPlaygrounds} from '../../util';
-import path from 'path';
+import path, {dirname} from 'path';
import {isInteger, parse} from 'lossless-json';
+import {fileURLToPath} from 'url';
const WS_ENDPOINT = 'ws://localhost:9944';
@@ -32,7 +33,8 @@
// 3. Parse data to migrate
console.log('3. Parsing chainql results...');
- const parsingResult = parse(fs.readFileSync(path.resolve('src', 'migrations', '942057-appPromotion', 'output.json'), 'utf-8'), undefined, customNumberParser);
+ const dirName = dirname(fileURLToPath(import.meta.url));
+ const parsingResult = parse(fs.readFileSync(path.resolve(dirName, 'output.json'), 'utf-8'), undefined, customNumberParser);
const chainqlImportData = parsingResult as {
address: string;
tests/src/util/frankenstein.tsdiffbeforeafterboth1// 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'+secondsseconds}`;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}107108const raiseZombienet = async (): Promise<void> => {109 const isUpgradeTesting = !!NEW_RELAY_BIN || !!NEW_RELAY_WASM || !!NEW_PARA_BIN || !!NEW_PARA_WASM;110 /*111 // If there is nothing to upgrade, what is the point112 if (!isUpgradeTesting) {113 console.warn('\nNeither the relay nor the parachain were selected for an upgrade! ' +114 'Please pass environment vars `NEW_RELAY_BIN`, `NEW_RELAY_WASM`, `NEW_PARA_BIN`, `NEW_PARA_WASM`.');115 process.exit(1);116 }117 */118119 // an unsavory practice, but very convenient, mwahahah120 process.env.PARA_DIR = PARA_DIR;121 process.env.RELAY_DIR = RELAY_DIR;122 process.env.REPLICA_FROM = REPLICA_FROM;123124 const configPath = resolve(NETWORK_CONFIG_FILE);125 const networkConfig = readNetworkConfig(configPath);126 // console.log(networkConfig);127 if(networkConfig.settings.provider !== 'native') {128 throw new Error(`Oh no! Expected native network, got ${networkConfig.settings.provider}.`);129 }130131 await cryptoWaitReady();132133 // Launch Zombienet!134 network = await zombie.start(ZOMBIENET_CREDENTIALS, networkConfig, {silent: false});135136 // Get the relay chain info like the epoch length and spec version137 // Then restart each parachain's binaries138 // // Stop and restart each node139 // // Send specified keys to parachain nodes in case the parachain requires it140 // If it is not needed to upgrade runtimes themselves, the job is done!141142 // Record some required information regarding the relay chain143 await network.relay[0].connectApi();144 let relayInfo = getRelayInfo((network.relay[0] as any).apiInstance!);145 await network.relay[0].apiInstance!.disconnect();146 if(isUpgradeTesting) {147 console.log('Relay stats:', relayInfo);148 }149150 // non-exported functionality of NativeClient151 const networkClient = (network.client as any);152153 if(NEW_RELAY_BIN) {154 console.log('\n🧶 Restarting relay nodes');155156 for(const [index, node] of network.relay.entries()) {157 await node.apiInstance?.disconnect();158159 console.log(`\n🚦 Starting timeout for the epoch change (node ${index+1}/${network.relay.length})...`);160 await waitWithTimer(relayInfo.epochTime);161162 // Replace the node-starting command with the new binary163 const cmd = networkClient.processMap[node.name].cmd[0].split(' ')[0];164 networkClient.processMap[node.name].cmd = networkClient.processMap[node.name].cmd.map((arg: string) => arg.replace(cmd, NEW_RELAY_BIN));165166 await node.restart();167 }168169 console.log('\n🌒 All relay nodes restarted with the new binaries.');170 }171172 if(NEW_PARA_BIN) {173 for(const paraId in network.paras) {174 const para = network.paras[paraId];175 console.log(`\n🧶 Restarting collator nodes of parachain ${paraId}`);176177 for(const [_index, node] of para.nodes.entries()) {178 await node.apiInstance?.disconnect();179180 // Replace the node-starting command with the new binary181 const cmd = networkClient.processMap[node.name].cmd[0].split(' ')[0];182 networkClient.processMap[node.name].cmd = networkClient.processMap[node.name].cmd.map((arg: string) => arg.replace(cmd, NEW_PARA_BIN));183184 await node.restart();185 // applyaurakey?186 // Zombienet handles it on first-time node creation187 }188 }189190 console.log('\n🌗 All parachain collators restarted with the new binaries.');191 }192193 // Re-establish connection to the relay node and get the runtime upgrade validation delay for parachains194 // For the relay, connect and set the new runtime code195 // For each parachain, connect, authorize and upgrade its runtime196 // Ping the the chains for the runtime upgrade after the minimal time and then every few blocks197 // // For each parachain, re-connect and verify that the runtime upgrade is successful198199 let relayUpgradeCompleted = false, paraUpgradeCompleted = false;200201 if(NEW_RELAY_WASM) {202 const relayOldVersion = relayInfo.specVersion;203 console.log('\n🚦 Starting timeout for the next epoch before upgrading the relay runtime code...');204 await waitWithTimer(relayInfo.epochTime);205206 console.log('--- Upgrading the relay chain runtime \t---');207208 // Read the new WASM code and set it as the relay's new code209 const code = fs.readFileSync(NEW_RELAY_WASM).toString('hex');210 await usingPlaygrounds(async (helper, privateKey) => {211 const superuser = await privateKey(SUPERUSER_KEY);212213 const result = await helper.executeExtrinsic(214 superuser,215 'api.tx.sudo.sudoUncheckedWeight',216 [helper.constructApiCall('api.tx.system.setCode', [`0x${code}`]), {}],217 );218219 if(result.status == 'Fail') {220 console.error('Failed to upgrade the runtime:', result);221 }222223 // Get the updated information from the relay's new runtime224 relayInfo = getRelayInfo(helper.getApi());225 }, network.relay[0].wsUri);226227 if(relayOldVersion != relayInfo.specVersion) {228 // eslint-disable-next-line no-useless-escape229 console.log(`\n\🛰️ The relay has successfully upgraded from version ${relayOldVersion} to ${relayInfo.specVersion}!`);230 relayUpgradeCompleted = true;231 } else {232 console.error(`\nThe relay did not upgrade from version ${relayOldVersion}!`);233 }234 } else {235 // If the relay did not need to be upgraded, it's already technically completed236 relayUpgradeCompleted = true;237 }238239 if(NEW_PARA_WASM) {240 let codeValidationDelayBlocks = 0;241 const upgradingParas: {[id: string]: {version: number, upgraded: boolean}} = {};242 // Calculate the code validation delay of the relay chain,243 // so that we know how much to wait before the parachains can be upgraded after the extrinsic244 await usingPlaygrounds(async (helper) => {245 const {validationUpgradeDelay, minimumValidationUpgradeDelay} =246 (await helper.callRpc('api.query.configuration.activeConfig', [])).toJSON() as any;247248 codeValidationDelayBlocks = Math.max(validationUpgradeDelay ?? 0, minimumValidationUpgradeDelay ?? 0);249 }, network.relay[0].wsUri);250251 // Wait for the next epoch so that the parachains will start cooperating with the relay252 if(relayUpgradeCompleted && NEW_RELAY_WASM) {253 console.log('\n🚥 Starting timeout for the next epoch before upgrading the parachains code...');254 await waitWithTimer(relayInfo.epochTime);255 }256257 const migration = migrations[DESTINATION_SPEC_VERSION];258 console.log('⭐️⭐️⭐️ DESTINATION_SPEC_VERSION ⭐️⭐️⭐️', DESTINATION_SPEC_VERSION);259 for(const paraId in network.paras) {260 console.log(`\n--- Upgrading the runtime of parachain ${paraId} \t---`);261 const para = network.paras[paraId];262263 // Enable maintenance mode if present264 await toggleMaintenanceMode(true, para.nodes[0].wsUri);265 if(migration) {266 console.log('⭐️⭐️⭐️ Running pre-upgrade scripts... ⭐️⭐️⭐️');267 await migration.before();268 }269270 // Read the WASM code and authorize the upgrade with its hash and set it as the new runtime271 const code = fs.readFileSync(NEW_PARA_WASM);272 const codeHash = blake2AsHex(code);273 await usingPlaygrounds(async (helper, privateKey) => {274 const superuser = await privateKey(SUPERUSER_KEY);275276 upgradingParas[paraId] = {version: getSpecVersion(helper.getApi()), upgraded: false};277278 console.log('--- Authorizing the parachain runtime upgrade \t---');279 let result = await helper.executeExtrinsic(280 superuser,281 'api.tx.sudo.sudoUncheckedWeight',282 [helper.constructApiCall('api.tx.parachainSystem.authorizeUpgrade', [codeHash, false]), {}],283 );284285 if(result.status == 'Fail') {286 console.error('Failed to authorize the upgrade:', result);287 return;288 }289290 console.log('--- Enacting the parachain runtime upgrade \t---');291 result = await helper.executeExtrinsic(292 superuser,293 'api.tx.sudo.sudoUncheckedWeight',294 [helper.constructApiCall('api.tx.parachainSystem.enactAuthorizedUpgrade', [`0x${code.toString('hex')}`]), {}],295 );296297 if(result.status == 'Fail') {298 console.error('Failed to upgrade the runtime:', result);299 }300 }, para.nodes[0].wsUri);301 }302303 // Check the upgrades of the parachains, first after the minimum code validation delay, and then after some block time increments304 let firstPass = true;305 for(let attempt = 0; attempt < 3 && !paraUpgradeCompleted; attempt++) {306 if(firstPass) {307 console.log('\nCode validation delay:', codeValidationDelayBlocks, 'blocks');308 console.log('🚥 Waiting for the minimum code validation delay before the parachain can upgrade...');309 await waitWithTimer(relayInfo.blockTime * codeValidationDelayBlocks);310 firstPass = false;311 } else {312 console.log('\n🚥 Waiting for a few blocks more to verify that the parachain upgrades are successful...');313 await waitWithTimer(PARACHAIN_BLOCK_TIME * 3);314 }315316 // Ping the parachains' nodes for new runtime versions317 let upgradeFailed = false;318 for(const paraId in network.paras) {319 if(upgradingParas[paraId].upgraded) continue;320321 const para = network.paras[paraId];322 // eslint-disable-next-line require-await323 await usingPlaygrounds(async (helper) => {324 const specVersion = getSpecVersion(helper.getApi());325326 if(specVersion != upgradingParas[paraId].version) {327 // eslint-disable-next-line no-useless-escape328 console.log(`\n\🛰️ Parachain ${paraId} has successfully upgraded from version ${upgradingParas[paraId].version} to ${specVersion}!`);329 upgradingParas[paraId].upgraded = true;330 } else {331 console.error(`\nParachain ${paraId} failed to upgrade from version ${upgradingParas[paraId].version}!`);332 upgradeFailed = true;333 }334 }, para.nodes[0].wsUri);335336 paraUpgradeCompleted = !upgradeFailed;337 }338 }339340 // Disable maintenance mode if present341 for(const paraId in network.paras) {342 // TODO only if our parachain343 if(migration) {344 console.log('⭐️⭐️⭐️ Running post-upgrade scripts... ⭐️⭐️⭐️');345 await migration.after();346 }347 await toggleMaintenanceMode(false, network.paras[paraId].nodes[0].wsUri);348 }349 } else {350 // If the relay did not need to be upgraded, it's already technically completed351 paraUpgradeCompleted = true;352 }353354 // await network.stop();355356 if(isUpgradeTesting) {357 if(paraUpgradeCompleted && relayUpgradeCompleted) {358 console.log("\n🛸 PARACHAINS' RUNTIME UPGRADE TESTING COMPLETE 🛸");359 } else {360 console.error("\n🚧 PARACHAINS' RUNTIME UPGRADE TESTING FAILED 🚧");361 }362 } else {363 console.log('🚀 ZOMBIENET RAISED 🚀');364 }365};366367raiseZombienet()/*.then(async () => await stop())*/.catch(async (e) => {368 console.error(e);369 await stop();370 process.exit(1);371});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'+secondsseconds}`;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});