Skip to content

Commit

Permalink
feat: bouncer command for submitting runtime upgrades (#4122)
Browse files Browse the repository at this point in the history
* feat: bouncer command for submitting runtime upgrades

* chore: linting
  • Loading branch information
kylezs authored Oct 16, 2023
1 parent 5c70808 commit b1fe871
Show file tree
Hide file tree
Showing 2 changed files with 64 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];

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

const 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);
});
33 changes: 33 additions & 0 deletions bouncer/shared/submit_runtime_upgrade.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { compactAddLength } from '@polkadot/util';
import { promises as fs } from 'fs';
import { submitGovernanceExtrinsic } from './cf_governance';
import { getChainflipApi } from '../shared/utils';

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 = 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 b1fe871

Please sign in to comment.