-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: automate compatible CFE upgrades (#4149)
* feat: bouncer bump release version * chore: compare SemVer versions function * feat: (bouncer) upgrade network command * refactor: pull spec version check out of compileBinaries * chore: move compileBinaries to its own file * chore: lint * chore: remove unused promptUser * prettier * chore: remove stale comment * fix: use && between commands
- Loading branch information
Showing
11 changed files
with
183 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules | ||
pnpm-lock.yaml | ||
.idea | ||
.idea | ||
tmp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#!/usr/bin/env -S pnpm tsx | ||
// INSTRUCTIONS | ||
// Upgrades a localnet network to a new version. | ||
// Start a network with the version you want to upgrade from. Then run this command, providing the git reference (commit, branch, tag) you wish to upgrade to. | ||
// | ||
// Optional args: | ||
// patch/minor/major: If the version of the commit we're upgrading to is the same as the version of the commit we're upgrading from, we bump the version by the specified level. | ||
// | ||
// For example: ./commands/upgrade_network.ts v0.10.1 | ||
// or: ./commands/upgrade_network.ts v0.10.1 patch | ||
|
||
import { upgradeNetwork } from '../shared/upgrade_network'; | ||
import { runWithTimeout } from '../shared/utils'; | ||
|
||
async function main(): Promise<void> { | ||
const upgradeTo = process.argv[2]?.trim(); | ||
|
||
if (!upgradeTo) { | ||
console.error('Please provide a git reference to upgrade to.'); | ||
process.exit(-1); | ||
} | ||
|
||
const optBumptTo: string = process.argv[3]?.trim().toLowerCase(); | ||
if (optBumptTo === 'patch' || optBumptTo === 'minor' || optBumptTo === 'major') { | ||
await upgradeNetwork(upgradeTo, optBumptTo); | ||
} else { | ||
await upgradeNetwork(upgradeTo); | ||
} | ||
|
||
process.exit(0); | ||
} | ||
|
||
runWithTimeout(main(), 15 * 60 * 1000).catch((error) => { | ||
console.error(error); | ||
process.exit(-1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { execSync } from 'child_process'; | ||
|
||
export type SemVerLevel = 'major' | 'minor' | 'patch'; | ||
|
||
// Bumps the version of all the packages in the workspace by the specified level. | ||
export async function bumpReleaseVersion(level: SemVerLevel, projectRoot: string) { | ||
console.log(`Bumping the version of all packages in the workspace by ${level}...`); | ||
try { | ||
execSync(`cd ${projectRoot} && cargo ws version ${level} --no-git-commit -y`); | ||
} catch (error) { | ||
console.log(error); | ||
console.log('Ensure you have cargo workspaces installed: `cargo install cargo-workspaces`'); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
21 changes: 7 additions & 14 deletions
21
bouncer/shared/noop_runtime_upgrade.ts → bouncer/shared/simple_runtime_upgrade.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { execSync } from 'child_process'; | ||
import fs from 'fs/promises'; | ||
import * as toml from 'toml'; | ||
import path from 'path'; | ||
import { SemVerLevel, bumpReleaseVersion } from './bump_release_version'; | ||
import { simpleRuntimeUpgrade } from './simple_runtime_upgrade'; | ||
import { compareSemVer } from './utils'; | ||
|
||
async function readPackageTomlVersion(projectRoot: string): Promise<string> { | ||
const data = await fs.readFile(path.join(projectRoot, '/state-chain/runtime/Cargo.toml'), 'utf8'); | ||
const parsedData = toml.parse(data); | ||
const version = parsedData.package.version; | ||
return version; | ||
} | ||
|
||
// The javascript version of state-chain/primitives/src/lib.rs - SemVer::is_compatible_with() | ||
function isCompatibleWith(semVer1: string, semVer2: string) { | ||
const [major1, minor1] = semVer1.split('.').map(Number); | ||
const [major2, minor2] = semVer2.split('.').map(Number); | ||
|
||
return major1 === major2 && minor1 === minor2; | ||
} | ||
|
||
// Create a git workspace in the tmp/ directory and check out the specified commit. | ||
// Remember to delete it when you're done! | ||
function createGitWorkspaceAt(absoluteWorkspacePath: string, toGitRef: string) { | ||
try { | ||
// Create a directory for the new workspace | ||
execSync(`mkdir -p ${absoluteWorkspacePath}`); | ||
|
||
// Create a new workspace using git worktree. | ||
execSync(`git worktree add ${absoluteWorkspacePath}`); | ||
|
||
// Navigate to the new workspace and checkout the specific commit | ||
execSync(`cd ${absoluteWorkspacePath} && git checkout ${toGitRef}`); | ||
|
||
console.log('Commit checked out successfully in new workspace.'); | ||
} catch (error) { | ||
console.error(`Error: ${error}`); | ||
} | ||
} | ||
|
||
// Upgrades a bouncer network from the commit currently running on localnet to the provided git reference (commit, branch, tag). | ||
// If the version of the commit we're upgrading to is the same as the version of the commit we're upgrading from, we bump the version by the specified level. | ||
export async function upgradeNetwork(toGitRef: string, bumpByIfEqual: SemVerLevel = 'patch') { | ||
const fromTomlVersion = await readPackageTomlVersion(path.dirname(process.cwd())); | ||
console.log("Version we're upgrading from: " + fromTomlVersion); | ||
|
||
// tmp/ is ignored in the bouncer .gitignore file. | ||
const absoluteWorkspacePath = path.join(process.cwd(), 'tmp/upgrade-network'); | ||
|
||
console.log('Creating a new git workspace at: ' + absoluteWorkspacePath); | ||
|
||
createGitWorkspaceAt(absoluteWorkspacePath, toGitRef); | ||
|
||
const toTomlVersion = await readPackageTomlVersion(`${absoluteWorkspacePath}`); | ||
console.log("Version we're upgrading to: " + toTomlVersion); | ||
|
||
if (compareSemVer(fromTomlVersion, toTomlVersion) === 'greater') { | ||
throw new Error( | ||
"The version we're upgrading to is older than the version we're upgrading from. Ensure you selected the correct commits.", | ||
); | ||
} | ||
|
||
if (fromTomlVersion === toTomlVersion) { | ||
await bumpReleaseVersion(bumpByIfEqual, absoluteWorkspacePath); | ||
} | ||
|
||
const newToTomlVersion = await readPackageTomlVersion(path.join(absoluteWorkspacePath)); | ||
const isCompatible = isCompatibleWith(fromTomlVersion, newToTomlVersion); | ||
|
||
if (isCompatible) { | ||
// The CFE could be upgraded too. But an incompatible CFE upgrade would mean it's... incompatible, so covered in the other path. | ||
console.log('The versions are compatible.'); | ||
|
||
// Runtime upgrade using the *new* version. | ||
await simpleRuntimeUpgrade(absoluteWorkspacePath); | ||
console.log('Upgrade complete.'); | ||
} else if (!isCompatible) { | ||
// Incompatible upgrades requires running two versions of the CFEs side by side. | ||
console.log('Incompatible CFE upgrades are not yet supported :('); | ||
} | ||
|
||
execSync(`cd ${absoluteWorkspacePath} && git worktree remove . --force`); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { execSync } from 'child_process'; | ||
|
||
// Returns the expected next version of the runtime. | ||
export async function compileBinaries(type: 'runtime' | 'all', projectRoot: string) { | ||
if (type === 'all') { | ||
console.log('Building all the binaries...'); | ||
execSync(`cd ${projectRoot} && cargo build --release`); | ||
} else { | ||
console.log('Building the new runtime...'); | ||
execSync(`cd ${projectRoot}/state-chain/runtime && cargo build --release`); | ||
} | ||
|
||
console.log('Build complete.'); | ||
} |