From 93fbb21284786fbec1ec047d6d61fa47f3d43a20 Mon Sep 17 00:00:00 2001 From: kylezs Date: Fri, 20 Oct 2023 01:44:06 +1100 Subject: [PATCH] feat: bouncer, auto bump spec version for runtime upgrades (#4143) * feat: auto-bump spec version in noop-runtime-upgrade test * refactor: replace interactive test run with command line arg * chore: lints --- bouncer/commands/noop_runtime_upgrade.ts | 12 +++--- bouncer/shared/noop_runtime_upgrade.ts | 8 +--- bouncer/shared/utils/bump_spec_version.ts | 45 +++++++++++++++++++++++ 3 files changed, 53 insertions(+), 12 deletions(-) create mode 100644 bouncer/shared/utils/bump_spec_version.ts diff --git a/bouncer/commands/noop_runtime_upgrade.ts b/bouncer/commands/noop_runtime_upgrade.ts index 59a05a052b..bcfab6febc 100755 --- a/bouncer/commands/noop_runtime_upgrade.ts +++ b/bouncer/commands/noop_runtime_upgrade.ts @@ -3,22 +3,22 @@ // // Performs a noop runtime upgrade. You will bump the runtime `spec_version` and nothing else. // This should not affect the CFEs in any way. Everything should just function straight through the upgrade. +// +// Optional args: +// -test: Run the swap tests after the upgrade. // For example ./commands/noop_runtime_upgrade.ts // NB: It *must* be run from the bouncer directory. import { noopRuntimeUpgrade } from '../shared/noop_runtime_upgrade'; -import { promptUser } from '../shared/prompt_user'; import { testAllSwaps } from '../shared/swapping'; import { runWithTimeout } from '../shared/utils'; async function main(): Promise { await noopRuntimeUpgrade(); - await promptUser( - 'Would you like to test all swaps after the upgrade now? The vaults and liquidity must be set up already.', - ); - - await testAllSwaps(); + if (process.argv[2] === '-test') { + await testAllSwaps(); + } process.exit(0); } diff --git a/bouncer/shared/noop_runtime_upgrade.ts b/bouncer/shared/noop_runtime_upgrade.ts index fb34b76b1c..742689cbea 100755 --- a/bouncer/shared/noop_runtime_upgrade.ts +++ b/bouncer/shared/noop_runtime_upgrade.ts @@ -5,7 +5,7 @@ import { execSync } from 'node:child_process'; import { submitRuntimeUpgrade } from './submit_runtime_upgrade'; import { jsonRpc } from './json_rpc'; import { getChainflipApi, observeEvent } from './utils'; -import { promptUser } from './prompt_user'; +import { bumpSpecVersion } from './utils/bump_spec_version'; async function getCurrentSpecVersion(): Promise { return Number((await jsonRpc('state_getRuntimeVersion', [], 9944)).specVersion); @@ -20,11 +20,7 @@ export async function noopRuntimeUpgrade(): Promise { const nextSpecVersion = currentSpecVersion + 1; - await promptUser( - 'Go to state-chain/runtime/src/lib.rs and then in that file go to #[sp_version::runtime_version]. Set the `spec_version` to ' + - nextSpecVersion + - ' and save the file.', - ); + bumpSpecVersion('../state-chain/runtime/src/lib.rs', nextSpecVersion); console.log('Building the new runtime'); execSync('cd ../state-chain/runtime && cargo build --release'); diff --git a/bouncer/shared/utils/bump_spec_version.ts b/bouncer/shared/utils/bump_spec_version.ts new file mode 100644 index 0000000000..0373156667 --- /dev/null +++ b/bouncer/shared/utils/bump_spec_version.ts @@ -0,0 +1,45 @@ +import fs from 'fs'; + +export function bumpSpecVersion(filePath: string, nextSpecVersion?: number) { + console.log('Bumping the spec version'); + try { + const fileContent = fs.readFileSync(filePath, 'utf-8'); + const lines = fileContent.split('\n'); + + let incrementedVersion; + let foundMacro = false; + for (let i = 0; i < lines.length; i++) { + const line = lines[i]; + + if (line.trim() === '#[sp_version::runtime_version]') { + foundMacro = true; + } + + if (foundMacro && line.includes('spec_version:')) { + const specVersionLine = line.match(/(spec_version:\s*)(\d+)/); + + if (specVersionLine) { + if (nextSpecVersion) { + incrementedVersion = nextSpecVersion; + } else { + incrementedVersion = parseInt(specVersionLine[2]) + 1; + } + lines[i] = ` spec_version: ${incrementedVersion},`; + break; + } + } + } + + if (!foundMacro) { + console.error('spec_version within #[sp_version::runtime_version] not found.'); + return; + } + + const updatedContent = lines.join('\n'); + fs.writeFileSync(filePath, updatedContent); + + console.log(`Successfully updated spec_version to ${incrementedVersion}.`); + } catch (error) { + console.error(`An error occurred: ${error.message}`); + } +}