Skip to content

Commit

Permalink
fix: bump spec version command only bumps when necessary (#4422)
Browse files Browse the repository at this point in the history
* chore: getCurrentRuntimeVersion -> getNetworkRuntimeVersion

* chore: rename bumpSpecVersion to specVersion

* refactor: more explicit params

* fix: only bump the version when it needs to be bumped

* chore: lint
  • Loading branch information
kylezs authored Jan 17, 2024
1 parent e906024 commit a2bf65c
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 24 deletions.
4 changes: 2 additions & 2 deletions bouncer/commands/polkadot_runtime_update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
pushPolkadotRuntimeUpdate,
} from '../shared/polkadot_runtime_update';
import { runWithTimeout } from '../shared/utils';
import { getCurrentRuntimeVersion } from '../shared/utils/bump_spec_version';
import { getNetworkRuntimeVersion } from '../shared/utils/spec_version';

async function main(): Promise<void> {
// Bump the spec version
Expand All @@ -20,7 +20,7 @@ async function main(): Promise<void> {
await pushPolkadotRuntimeUpdate(wasmPath);

// Check the polkadot spec version has changed
const postUpgradeSpecVersion = await getCurrentRuntimeVersion('http://127.0.0.1:9947');
const postUpgradeSpecVersion = await getNetworkRuntimeVersion('http://127.0.0.1:9947');
if (postUpgradeSpecVersion.specVersion !== expectedSpecVersion) {
throw new Error(
`Polkadot runtime update failed. Currently at version ${postUpgradeSpecVersion.specVersion}, expected to be at ${expectedSpecVersion}`,
Expand Down
4 changes: 2 additions & 2 deletions bouncer/commands/read_workspace_tomls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import fs from 'fs';
import toml from '@iarna/toml';
import { compareSemVer } from '../shared/utils';
import { jsonRpc } from '../shared/json_rpc';
import { bumpSpecVersion } from '../shared/utils/bump_spec_version';
import { specVersion } from '../shared/utils/spec_version';

const projectRoot = process.argv[2];
const engineReleaseVersion = process.argv[3];
Expand Down Expand Up @@ -68,7 +68,7 @@ const releaseSpecVersion = Number(
);
console.log(`Release spec version: ${releaseSpecVersion}`);

const specVersionInToml = bumpSpecVersion(`${projectRoot}/state-chain/runtime/src/lib.rs`, true);
const specVersionInToml = specVersion(`${projectRoot}/state-chain/runtime/src/lib.rs`, 'read');
console.log(`Spec version in TOML: ${specVersionInToml}`);

if (specVersionInToml >= releaseSpecVersion) {
Expand Down
8 changes: 4 additions & 4 deletions bouncer/shared/polkadot_runtime_update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
sleep,
observeBadEvents,
} from '../shared/utils';
import { bumpSpecVersion, getCurrentRuntimeVersion } from '../shared/utils/bump_spec_version';
import { specVersion, getNetworkRuntimeVersion } from './utils/spec_version';
import { handleDispatchError, submitAndGetEvent } from '../shared/polkadot_utils';
import { testSwap } from './swapping';

Expand Down Expand Up @@ -136,7 +136,7 @@ export async function bumpAndBuildPolkadotRuntime(): Promise<[string, number]> {
const projectPath = process.cwd();
// tmp/ is ignored in the bouncer .gitignore file.
const workspacePath = path.join(projectPath, 'tmp/polkadot');
const nextSpecVersion = (await getCurrentRuntimeVersion(polkadotEndpoint)).specVersion + 1;
const nextSpecVersion = (await getNetworkRuntimeVersion(polkadotEndpoint)).specVersion + 1;
console.log('Current polkadot spec_version: ' + nextSpecVersion);

// No need to compile if the version we need is the pre-compiled version.
Expand Down Expand Up @@ -170,7 +170,7 @@ export async function bumpAndBuildPolkadotRuntime(): Promise<[string, number]> {
console.log('Updating polkadot source');
execSync(`git pull`, { cwd: workspacePath });

await bumpSpecVersion(`${workspacePath}/runtime/polkadot/src/lib.rs`, false, nextSpecVersion);
await specVersion(`${workspacePath}/runtime/polkadot/src/lib.rs`, 'write', nextSpecVersion);

// Compile polkadot runtime
console.log('Compiling polkadot...');
Expand Down Expand Up @@ -246,7 +246,7 @@ export async function testPolkadotRuntimeUpdate(): Promise<void> {
await pushPolkadotRuntimeUpdate(wasmPath);

// Check the polkadot spec version has changed
const postUpgradeSpecVersion = await getCurrentRuntimeVersion(polkadotEndpoint);
const postUpgradeSpecVersion = await getNetworkRuntimeVersion(polkadotEndpoint);
if (postUpgradeSpecVersion.specVersion !== expectedSpecVersion) {
throw new Error(
`Polkadot runtime update failed. Currently at version ${postUpgradeSpecVersion.specVersion}, expected to be at ${expectedSpecVersion}`,
Expand Down
4 changes: 2 additions & 2 deletions bouncer/shared/simple_runtime_upgrade.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { submitRuntimeUpgrade } from './submit_runtime_upgrade';
import { bumpSpecVersionAgainstNetwork, getCurrentRuntimeVersion } from './utils/bump_spec_version';
import { bumpSpecVersionAgainstNetwork, getNetworkRuntimeVersion } from './utils/spec_version';
import { compileBinaries } from './utils/compile_binaries';

// Do a runtime upgrade using the code in the projectRoot directory.
Expand All @@ -12,7 +12,7 @@ export async function simpleRuntimeUpgrade(projectRoot: string, tryRuntime = fal

await submitRuntimeUpgrade(projectRoot, tryRuntime);

const newSpecVersion = (await getCurrentRuntimeVersion()).specVersion;
const newSpecVersion = (await getNetworkRuntimeVersion()).specVersion;
console.log('New spec_version: ' + newSpecVersion);

if (newSpecVersion !== nextSpecVersion) {
Expand Down
2 changes: 1 addition & 1 deletion bouncer/shared/upgrade_network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import path from 'path';
import { SemVerLevel, bumpReleaseVersion } from './bump_release_version';
import { simpleRuntimeUpgrade } from './simple_runtime_upgrade';
import { compareSemVer, sleep } from './utils';
import { bumpSpecVersionAgainstNetwork } from './utils/bump_spec_version';
import { bumpSpecVersionAgainstNetwork } from './utils/spec_version';
import { compileBinaries } from './utils/compile_binaries';
import { submitRuntimeUpgradeWithRestrictions } from './submit_runtime_upgrade';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ type RuntimeVersion = {
stateVersion: number;
};

export async function getCurrentRuntimeVersion(endpoint?: string): Promise<RuntimeVersion> {
export async function getNetworkRuntimeVersion(endpoint?: string): Promise<RuntimeVersion> {
return (await jsonRpc('state_getRuntimeVersion', [], endpoint)) as unknown as RuntimeVersion;
}

// If `onlyReadCurrent` is true, it will only read the current spec version and return it.
// If `onlyReadCurrent` is false, it will increment the spec version and write it to the file. Returning the newly written version.
export function bumpSpecVersion(
export function specVersion(
filePath: string,
onlyReadCurrent?: boolean,
nextSpecVersion?: number,
readOrWrite: 'read' | 'write',
// Will only write this version if the current version is less than this.
// If this is not provided it will simply bump the version in the file by 1.
writeSpecVersion?: number,
): number {
try {
const fileContent = fs.readFileSync(filePath, 'utf-8');
Expand All @@ -42,15 +42,29 @@ export function bumpSpecVersion(
if (specVersionLine) {
const currentSpecVersion = parseInt(specVersionLine[2]);

if (onlyReadCurrent) {
if (readOrWrite === 'read') {
return currentSpecVersion;
}
// write

if (nextSpecVersion) {
incrementedVersion = nextSpecVersion;
if (writeSpecVersion) {
if (currentSpecVersion >= writeSpecVersion) {
console.log(
"Current spec version is greater than the one you're trying to write. Returning currentSpecVersion.",
);
return currentSpecVersion;
}
// if the version we provided is greater than the current one, then we can bump it to this new version.
incrementedVersion = writeSpecVersion;
} else {
// If we want to write, but didn't provide a version, we simply increment the current version.
incrementedVersion = currentSpecVersion + 1;
}

console.assert(
incrementedVersion !== -1,
'incrementedVersion should not be -1. It should be set above.',
);
lines[i] = ` spec_version: ${incrementedVersion},`;
break;
}
Expand Down Expand Up @@ -78,10 +92,10 @@ export async function bumpSpecVersionAgainstNetwork(
runtimeLibPath: string,
endpoint?: string,
): Promise<number> {
const currentSpecVersion = (await getCurrentRuntimeVersion(endpoint)).specVersion;
console.log('Current spec_version: ' + currentSpecVersion);
const nextSpecVersion = currentSpecVersion + 1;
const networkSpecVersion = (await getNetworkRuntimeVersion(endpoint)).specVersion;
console.log('Current spec_version: ' + networkSpecVersion);
const nextSpecVersion = networkSpecVersion + 1;
console.log('Bumping the spec version to: ' + nextSpecVersion);
bumpSpecVersion(runtimeLibPath, false, nextSpecVersion);
specVersion(runtimeLibPath, 'write', nextSpecVersion);
return nextSpecVersion;
}

0 comments on commit a2bf65c

Please sign in to comment.