Skip to content

Commit

Permalink
feat: spec_version of PR is greater than spec version of current rele…
Browse files Browse the repository at this point in the history
…ase (#4355)

* feat: enforce spec version is greater than current release

* fix: endpoint
  • Loading branch information
kylezs committed Dec 15, 2023
1 parent 4318931 commit db0b773
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 13 deletions.
27 changes: 25 additions & 2 deletions bouncer/commands/read_workspace_tomls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
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';

const projectRoot = process.argv[2];
const engineReleaseVersion = process.argv[3];
Expand Down Expand Up @@ -49,9 +51,30 @@ if (
) {
throw Error('All versions should be the same');
} else if (compareSemVer(engineTomlVersion, releaseVersion) === 'greater') {
console.log(`Version is correct. Your branch has a version greater than the current release.`);
console.log(
`Binary versions are correct. Your branch has a version greater than the current release.`,
);
} else {
throw Error(
`Binary versions are incorrect. The version of your branch (${engineTomlVersion}) should be greater than the current release (${releaseVersion}).)`,
);
}

const releaseSpecVersion = Number(
(await jsonRpc('state_getRuntimeVersion', [], 'https://perseverance.chainflip.xyz:443'))
.specVersion,
);
console.log(`Release spec version: ${releaseSpecVersion}`);

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

if (specVersionInToml >= releaseSpecVersion) {
console.log(
`Spec version is correct. Version in TOML is greater than or equal to the release spec version.`,
);
} else {
throw Error(
`Version is incorrect. The version of your branch (${engineTomlVersion}) should be greater than the current release (${releaseVersion}).)`,
`Spec version is incorrect. Version in TOML (${specVersionInToml}) should be greater than or equal to the release spec version (${releaseSpecVersion}).`,
);
}
6 changes: 4 additions & 2 deletions bouncer/shared/json_rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export async function jsonRpc(
method: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
params: any[],
port: number,
endpoint?: string,
): Promise<JSON> {
console.log('Sending json RPC', method);

Expand All @@ -14,7 +14,9 @@ export async function jsonRpc(
params,
id,
});
const response = await fetch(`http://127.0.0.1:${port}`, {

const fetchEndpoint = endpoint ?? 'http://localhost:9944';
const response = await fetch(`${fetchEndpoint}`, {
method: 'POST',
headers: {
Accept: 'application/json',
Expand Down
3 changes: 1 addition & 2 deletions bouncer/shared/lp_api_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ const testAddress = '0x1594300cbd587694affd70c933b9ee9155b186d9';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
async function lpApiRpc(method: string, params: any[]): Promise<any> {
// The port for the lp api is defined in `start_lp_api.sh`
const port = 10589;
return jsonRpc(method, params, port);
return jsonRpc(method, params, 'http://127.0.0.1:10589');
}

async function provideLiquidityAndTestAssetBalances() {
Expand Down
26 changes: 20 additions & 6 deletions bouncer/shared/utils/bump_spec_version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@ import fs from 'fs';
import { jsonRpc } from '../json_rpc';

export async function getCurrentSpecVersion(): Promise<number> {
return Number((await jsonRpc('state_getRuntimeVersion', [], 9944)).specVersion);
return Number((await jsonRpc('state_getRuntimeVersion', [])).specVersion);
}

export function bumpSpecVersion(filePath: string, nextSpecVersion?: number) {
// 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(
filePath: string,
onlyReadCurrent?: boolean,
nextSpecVersion?: number,
): number {
try {
const fileContent = fs.readFileSync(filePath, 'utf-8');
const lines = fileContent.split('\n');

let incrementedVersion;
let incrementedVersion = -1;
let foundMacro = false;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
Expand All @@ -23,10 +29,16 @@ export function bumpSpecVersion(filePath: string, nextSpecVersion?: number) {
const specVersionLine = line.match(/(spec_version:\s*)(\d+)/);

if (specVersionLine) {
const currentSpecVersion = parseInt(specVersionLine[2]);

if (onlyReadCurrent) {
return currentSpecVersion;
}

if (nextSpecVersion) {
incrementedVersion = nextSpecVersion;
} else {
incrementedVersion = parseInt(specVersionLine[2]) + 1;
incrementedVersion = currentSpecVersion + 1;
}
lines[i] = ` spec_version: ${incrementedVersion},`;
break;
Expand All @@ -36,15 +48,17 @@ export function bumpSpecVersion(filePath: string, nextSpecVersion?: number) {

if (!foundMacro) {
console.error('spec_version within #[sp_version::runtime_version] not found.');
return;
return -1;
}

const updatedContent = lines.join('\n');
fs.writeFileSync(filePath, updatedContent);

console.log(`Successfully updated spec_version to ${incrementedVersion}.`);
return incrementedVersion;
} catch (error) {
console.error(`An error occurred: ${error.message}`);
return -1;
}
}

Expand All @@ -54,6 +68,6 @@ export async function bumpSpecVersionAgainstNetwork(projectRoot: string): Promis
console.log('Current spec_version: ' + currentSpecVersion);
const nextSpecVersion = currentSpecVersion + 1;
console.log('Bumping the spec version to: ' + nextSpecVersion);
bumpSpecVersion(`${projectRoot}/state-chain/runtime/src/lib.rs`, nextSpecVersion);
bumpSpecVersion(`${projectRoot}/state-chain/runtime/src/lib.rs`, false, nextSpecVersion);
return nextSpecVersion;
}
2 changes: 1 addition & 1 deletion state-chain/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("chainflip-node"),
impl_name: create_runtime_str!("chainflip-node"),
authoring_version: 1,
spec_version: 110,
spec_version: 112,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 12,
Expand Down

0 comments on commit db0b773

Please sign in to comment.