Skip to content

Commit

Permalink
feat: bouncer, auto bump spec version for runtime upgrades (#4143)
Browse files Browse the repository at this point in the history
* feat: auto-bump spec version in noop-runtime-upgrade test

* refactor: replace interactive test run with command line arg

* chore: lints
  • Loading branch information
kylezs authored Oct 19, 2023
1 parent abb0f48 commit 93fbb21
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 deletions.
12 changes: 6 additions & 6 deletions bouncer/commands/noop_runtime_upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
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);
}
Expand Down
8 changes: 2 additions & 6 deletions bouncer/shared/noop_runtime_upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<number> {
return Number((await jsonRpc('state_getRuntimeVersion', [], 9944)).specVersion);
Expand All @@ -20,11 +20,7 @@ export async function noopRuntimeUpgrade(): Promise<void> {

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');
Expand Down
45 changes: 45 additions & 0 deletions bouncer/shared/utils/bump_spec_version.ts
Original file line number Diff line number Diff line change
@@ -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}`);
}
}

0 comments on commit 93fbb21

Please sign in to comment.