Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: bouncer command for submitting runtime upgrades
Browse files Browse the repository at this point in the history
kylezs committed Oct 16, 2023
1 parent 5c70808 commit ada93ab
Showing 2 changed files with 60 additions and 0 deletions.
31 changes: 31 additions & 0 deletions bouncer/commands/submit_runtime_upgrade.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env -S pnpm tsx
// INSTRUCTIONS
//
// This command takes 1 mandatory argument, and 2 optional arguments.
// Arguments:
// 1. Path to the runtime wasm file
// 2. Optional: A JSON string representing the semver restriction for the upgrade. If not provided, the upgrade will not be restricted by semver.
// 3. Optional: A number representing the percentage of nodes that must be upgraded before the upgrade will be allowed to proceed. If not provided, the upgrade will not be restricted by the number of nodes that have upgraded.
//
// For example: ./commands/submit_runtime_upgrade.ts /path/to/state_chain_runtime.compact.compressed.wasm '{"major": 1, "minor": 2, "patch": 3}' 50

import { submitRuntimeUpgrade } from '../shared/submit_runtime_upgrade';
import { runWithTimeout } from '../shared/utils';

async function main() {
const wasmPath = process.argv[2];

let arg3 = process.argv[3].trim();
const semverRestriction = arg3 ? JSON.parse(arg3) : undefined;

let arg4 = process.argv[4].trim();
const percentNodesUpgraded = arg4 ? Number(arg4) : undefined;

await submitRuntimeUpgrade(wasmPath, semverRestriction, percentNodesUpgraded);
process.exit(0);
}

runWithTimeout(main(), 20000).catch((error) => {
console.error(error);
process.exit(-1);
});
29 changes: 29 additions & 0 deletions bouncer/shared/submit_runtime_upgrade.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { submitGovernanceExtrinsic } from './cf_governance';
import { getChainflipApi } from '../shared/utils';
import { compactAddLength } from '@polkadot/util';

import { promises as fs } from 'fs';

async function readRuntimeWasmFromFile(filePath: string): Promise<Uint8Array> {
return compactAddLength(new Uint8Array(await fs.readFile(filePath)));
}

// By default we don't want to restrict that any of the nodes need to be upgraded.
export async function submitRuntimeUpgrade(wasmPath: string, semverRestriction?: Record<string, number>, percentNodesUpgraded: number = 0) {

const runtimeWasm = await readRuntimeWasmFromFile(wasmPath);

console.log("Submitting runtime upgrade.")
const chainflip = await getChainflipApi();

let versionPercentRestriction;
if (semverRestriction && percentNodesUpgraded) {
versionPercentRestriction = [semverRestriction, percentNodesUpgraded]
} else {
versionPercentRestriction = undefined;
}

await submitGovernanceExtrinsic(chainflip.tx.governance.chainflipRuntimeUpgrade(versionPercentRestriction, runtimeWasm));

console.log("Runtime upgrade completed.");
}

0 comments on commit ada93ab

Please sign in to comment.