Skip to content

Commit

Permalink
add support for chain-upgrade from local and js-custom for groups (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
pepoviola authored May 21, 2022
1 parent c43ff85 commit 168f614
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 34 deletions.
48 changes: 26 additions & 22 deletions src/jsapi-helpers/chain-upgrade.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ApiPromise, Keyring } from "@polkadot/api";
import { cryptoWaitReady, blake2AsHex } from "@polkadot/util-crypto";
import { readFileSync, promises as fsPromises } from "fs";
import { resolve } from "path";
import { promises as fsPromises } from "fs";
import { DEFAULT_INDIVIDUAL_TEST_TIMEOUT } from "../constants";
import { compress, decompress } from "napi-maybe-compressed-blob";
import axios from "axios";
Expand Down Expand Up @@ -81,28 +80,33 @@ export async function validateRuntimeCode(
hash: string,
timeout = DEFAULT_INDIVIDUAL_TEST_TIMEOUT
): Promise<boolean> {
let expired = false;
let limitTimeout;

try {
limitTimeout = setTimeout(() => {
expired = true;
}, timeout * 1000);

let done = false;
while (!done) {
if (expired) throw new Error(`Timeout(${timeout}s)`);
// wait 2 secs between checks
await new Promise((resolve) => setTimeout(resolve, 2000));
const currentHash = await api.query.paras.currentCodeHash(paraId);
console.log(`parachain ${paraId} current code hash : ${currentHash}`);
done = hash === currentHash.toString();
const validate = async (hash: string) => {
let done;
while (!done) {
const currentHash = await api.query.paras.currentCodeHash(paraId);
console.log(`parachain ${paraId} current code hash : ${currentHash}`);
if(hash === currentHash.toString()) break;
// wait 2 secs between checks
await new Promise((resolve) => setTimeout(resolve, 2000));
}

return true;
}

return true;
} catch (err) {
console.log(err);
if (limitTimeout) clearTimeout(limitTimeout);
return false;
const resp:any = await Promise.race([
validate(hash),
new Promise((resolve) => setTimeout(() => {
const err = new Error(`Timeout(${timeout}), "validating the hash of the runtime upgrade`);
return resolve(err);
}, timeout * 1000))
]);
if( resp instanceof Error ) throw resp

return resp;

} catch (err: any) {
throw err;
}
}

Expand Down
29 changes: 17 additions & 12 deletions src/test-runner/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,6 @@ function parseAssertionLine(assertion: string) {
backchannelMap: BackchannelMap,
testFile: string
) => {
let limitTimeout;
const networkInfo = {
chainSpecPath: network.chainSpecFullPath,
relay: network.relay.map((node) => {
Expand Down Expand Up @@ -472,6 +471,8 @@ function parseAssertionLine(assertion: string) {
),
};

const nodes = network.getNodes(nodeName);
const args = withArgs === "" ? [] : withArgs.split("with ").slice(1)[0].replaceAll('"',"").split(",");
const fileTestPath = path.dirname(testFile);
const resolvedJsFilePath = path.resolve(fileTestPath, jsFile);

Expand All @@ -482,18 +483,18 @@ function parseAssertionLine(assertion: string) {
(global as any).window = dom.window;
(global as any).document = dom.window.document;
const jsScript = await import(resolvedJsFilePath);
const args = withArgs === "" ? [] : withArgs.split("with ").slice(1)[0].replaceAll('"',"").split(",");
let value;

let values;
try {
const resp = await Promise.race([
jsScript.run(nodeName, networkInfo, args),
const resp: any = await Promise.race([
await Promise.all(nodes.map(node => jsScript.run(node.name, networkInfo, args))),
new Promise((resolve) => setTimeout(() => {
const err = new Error(`Timeout(${timeout}), "custom-js ${jsFile} within ${timeout} secs" didn't complete on time.`);
return resolve(err);
}, timeout * 1000))
]);
if( resp instanceof Error ) throw resp
else value = resp;
else values = resp;

} catch (err: any) {
console.log(`\n\t ${decorators.red(`Error running script: ${jsFile}`)}`);
Expand All @@ -504,11 +505,13 @@ function parseAssertionLine(assertion: string) {
// remove shim
(global as any).window = undefined;
(global as any).document = undefined;
clearTimeout(limitTimeout);

if (targetValue) {
if (comparatorFn !== "equals")
targetValue = parseInt(targetValue as string, 10);
assert[comparatorFn](value, targetValue);
for( const value of values) {
assert[comparatorFn](value, targetValue);
}
} else {
// test don't have matching output
expect(true).to.be.ok;
Expand Down Expand Up @@ -613,11 +616,13 @@ function parseAssertionLine(assertion: string) {
} else {
const fileTestPath = path.dirname(testFile);
const resolvedJsFilePath = path.resolve(fileTestPath, upgradeFileOrUrl);
hash = await chainUpgradeFromLocalFile(api, resolvedJsFilePath)
hash = await chainUpgradeFromLocalFile(api, resolvedJsFilePath);
}
// validate in the <node>: of the relay chain
node = network.node(nodeName);
api = await connect(node.wsUri);

// validate in a node of the relay chain
api.disconnect();
const {wsUri, userDefinedTypes } = network.relay[0];
api = await connect(wsUri, userDefinedTypes);
const valid = await validateRuntimeCode(api, parachainId, hash, timeout);
api.disconnect();

Expand Down

0 comments on commit 168f614

Please sign in to comment.