Skip to content

Commit

Permalink
Separate switchToPos from runtimeUpgrade.ts
Browse files Browse the repository at this point in the history
will be used in other jobs in CI
  • Loading branch information
atodorov committed Jun 28, 2023
1 parent 74fb3b2 commit 4536362
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 28 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/runtime-upgrade.yml
Original file line number Diff line number Diff line change
Expand Up @@ -284,14 +284,16 @@ jobs:
with:
name: creditcoin_node_runtime.compact.compressed.wasm

- name: Upgrade WASM
- name: Upgrade WASM & switchToPos
run: |
yarn --cwd ./scripts/js upgrade 'creditcoin-js'
echo "DEBUG: **** free memory before runtimeUpgrade ****"
free -m
ps aux --sort=-%mem | head -n 20
echo "DEBUG: *********************"
yarn --cwd ./scripts/js runtimeUpgrade ws://127.0.0.1:9944 ../../creditcoin_node_runtime.compact.compressed.wasm //Alice 0
sleep 10
yarn --cwd ./scripts/js switchToPos ws://127.0.0.1:9944 //Alice
# TODO: wait & confirm wasm upgrade has finished, incl. migrations
- name: Execute integration tests
Expand Down
1 change: 1 addition & 0 deletions scripts/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"main": "index.js",
"scripts": {
"runtimeUpgrade": "ts-node ./src/runtimeUpgrade.ts",
"switchToPos": "ts-node ./src/switchToPos.ts",
"lint": "eslint -c .eslintrc.js --ext .ts ./src",
"format": "prettier --write \"src/**/*.ts\"",
"check-format": "prettier --check \"src/**/*.ts\"",
Expand Down
27 changes: 0 additions & 27 deletions scripts/js/src/runtimeUpgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,33 +113,6 @@ async function doRuntimeUpgrade(
}
});
});

// WARNING: only used during fork-and-migrate testing
if (scheduleDelay === 0) {
callback = api.tx.posSwitch.switchToPos();

await new Promise<void>((resolve, reject) => {
const unsubscribe = api.tx.sudo
.sudoUncheckedWeight(callback, overrideWeight)
.signAndSend(keyring, { nonce: -1 }, (result) => {
const finish = (fn: () => void) => {
unsubscribe
.then((unsub) => {
unsub();
fn();
})
.catch(reject);
};
if (result.isInBlock && !result.isError) {
console.log('switchToPos called');
finish(resolve);
} else if (result.isError) {
const error = new Error(`Failed calling switchToPos: ${result.toString()}`);
finish(() => reject(error));
}
});
});
}
} finally {
await api.disconnect();
}
Expand Down
50 changes: 50 additions & 0 deletions scripts/js/src/switchToPos.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { creditcoinApi, Keyring } from 'creditcoin-js';
import { createOverrideWeight } from 'creditcoin-js/lib/utils';

async function doSwitchToPos(wsUrl: string, sudoKeyUri: string): Promise<void> {
// init the api client
const { api } = await creditcoinApi(wsUrl);
try {
// make the keyring for the sudo account
const keyring = new Keyring({ type: 'sr25519' }).createFromUri(sudoKeyUri);
const overrideWeight = createOverrideWeight(api);
const callback = api.tx.posSwitch.switchToPos();

await new Promise<void>((resolve, reject) => {
const unsubscribe = api.tx.sudo
.sudoUncheckedWeight(callback, overrideWeight)
.signAndSend(keyring, { nonce: -1 }, (result) => {
const finish = (fn: () => void) => {
unsubscribe
.then((unsub) => {
unsub();
fn();
})
.catch(reject);
};
if (result.isInBlock && !result.isError) {
console.log('switchToPos called');
finish(resolve);
} else if (result.isError) {
const error = new Error(`Failed calling switchToPos: ${result.toString()}`);
finish(() => reject(error));
}
});
});
} finally {
await api.disconnect();
}
}

if (process.argv.length < 3) {
console.error('switchToPos.ts <wsUrl> <sudoKeyUri>');
process.exit(1);
}

const inputWsUrl = process.argv[2];
const inputSudoKeyUri = process.argv[3];

doSwitchToPos(inputWsUrl, inputSudoKeyUri).catch((reason) => {
console.error(reason);
process.exit(1);
});

0 comments on commit 4536362

Please sign in to comment.