Skip to content

Commit

Permalink
Merge branch 'main' into dependabot/npm_and_yarn/javascript/jsdom-21.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored May 2, 2023
2 parents 1da337f + 57daefc commit a2e8619
Show file tree
Hide file tree
Showing 11 changed files with 295 additions and 42 deletions.
68 changes: 33 additions & 35 deletions javascript/packages/orchestrator/src/cmdGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
RPC_HTTP_PORT,
RPC_WS_PORT,
} from "./constants";
import { Node, ZombieRole } from "./types";
import { Node, SubstrateCliArgsVersion, ZombieRole } from "./types";

const debug = require("debug")("zombie::cmdGenerator");

Expand All @@ -18,11 +18,6 @@ interface PortsInterface {
[key: string]: number;
}

// Commented out as "Never used"
// interface ParachainCollatorsInterface {
// [key: number]: number;
// }

function parseCmdWithArguments(
commandWithArgs: string,
useWrapper = true,
Expand Down Expand Up @@ -69,6 +64,7 @@ export async function genCumulusCollatorCmd(
"--base-path": true,
"--port": true,
"--ws-port": true,
"--rpc-port": true,
"--chain": true,
"--prometheus-port": true,
};
Expand All @@ -85,22 +81,20 @@ export async function genCumulusCollatorCmd(
dataPath,
"--listen-addr",
`/ip4/0.0.0.0/tcp/${nodeSetup.p2pPort ? nodeSetup.p2pPort : P2P_PORT}/ws`,
"--rpc-port",
(nodeSetup.rpcPort ? nodeSetup.rpcPort : RPC_HTTP_PORT).toString(),
"--ws-port",
(nodeSetup.wsPort ? nodeSetup.wsPort : RPC_WS_PORT).toString(),
"--prometheus-external",
"--prometheus-port",
(nodeSetup.prometheusPort
? nodeSetup.prometheusPort
: PROMETHEUS_PORT
).toString(),
"--rpc-cors all",
"--unsafe-rpc-external",
"--rpc-methods unsafe",
"--unsafe-ws-external",
];

if (nodeSetup.substrateCliArgsVersion === SubstrateCliArgsVersion.V0)
fullCmd.push("--unsafe-ws-external");
const portFlags = getPortFlagsByCliArgsVersion(nodeSetup);

for (const [k, v] of Object.entries(portFlags)) {
fullCmd.push(...[k, v.toString()]);
}

const chainParts = chain.split("_");
const relayChain =
chainParts.length > 1 ? chainParts[chainParts.length - 1] : chainParts[0];
Expand All @@ -109,7 +103,6 @@ export async function genCumulusCollatorCmd(

const collatorPorts: PortsInterface = {
"--port": 0,
"--ws-port": 0,
"--rpc-port": 0,
};

Expand Down Expand Up @@ -284,12 +277,7 @@ export async function genCmd(
if (bootnodes && bootnodes.length)
args.push("--bootnodes", bootnodes.join(" "));

// port flags logic
const portFlags = {
"--prometheus-port": nodeSetup.prometheusPort,
"--rpc-port": nodeSetup.rpcPort,
"--ws-port": nodeSetup.wsPort,
};
const portFlags = getPortFlagsByCliArgsVersion(nodeSetup);

for (const [k, v] of Object.entries(portFlags)) {
args.push(...[k, v.toString()]);
Expand All @@ -311,6 +299,9 @@ export async function genCmd(
if (basePathFlagIndex >= 0) args.splice(basePathFlagIndex, 2);
args.push(...["--base-path", dataPath]);

if (nodeSetup.substrateCliArgsVersion === SubstrateCliArgsVersion.V0)
args.push("--unsafe-ws-external");

const finalArgs: string[] = [
command,
"--chain",
Expand All @@ -322,7 +313,6 @@ export async function genCmd(
"--unsafe-rpc-external",
"--rpc-methods",
"unsafe",
"--unsafe-ws-external",
...args,
];

Expand All @@ -331,16 +321,24 @@ export async function genCmd(
return resolvedCmd;
}

// Commented out as "Never used"
// helper
// const parachainCollators: ParachainCollatorsInterface =
// {} as ParachainCollatorsInterface;
const getPortFlagsByCliArgsVersion = (nodeSetup: Node) => {
// port flags logic
const portFlags: { [key: string]: string } = {
"--prometheus-port": (
nodeSetup.prometheusPort || PROMETHEUS_PORT
).toString(),
};

// Commented out as "Never used"
// function getCollatorIndex(paraId: number): number {
// if (parachainCollators[paraId] >= 0)
// parachainCollators[paraId] = parachainCollators[paraId] + 1;
// else parachainCollators[paraId] = 0;
if (nodeSetup.substrateCliArgsVersion === SubstrateCliArgsVersion.V0) {
portFlags["--rpc-port"] = (nodeSetup.rpcPort || RPC_HTTP_PORT).toString();
portFlags["--ws-port"] = (nodeSetup.wsPort || RPC_WS_PORT).toString();
} else {
// use ws port as default
const portToUse = nodeSetup.wsPort
? nodeSetup.wsPort
: nodeSetup.rpcPort || RPC_HTTP_PORT;
portFlags["--rpc-port"] = portToUse.toString();
}

// return parachainCollators[paraId];
// }
return portFlags;
};
13 changes: 10 additions & 3 deletions javascript/packages/orchestrator/src/orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,12 @@ export async function start(

debug(JSON.stringify(networkSpec, null, 4));

const { initClient, setupChainSpec, getChainSpecRaw } = getProvider(
networkSpec.settings.provider,
);
const {
initClient,
setupChainSpec,
getChainSpecRaw,
setSubstrateCliArdsVersion,
} = getProvider(networkSpec.settings.provider);

// global timeout to spin the network
const timeoutTimer = setTimeout(() => {
Expand Down Expand Up @@ -222,6 +225,10 @@ export async function start(
await client.staticSetup(networkSpec.settings);
await client.createPodMonitor("pod-monitor.yaml", chainName);

// Set substrate client argument version, needed from breaking change.
// see https://github.com/paritytech/substrate/pull/13384
await setSubstrateCliArdsVersion(networkSpec);

// create or copy relay chain spec
await setupChainSpec(
namespace,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@ export async function createTempNodeDef(
image: string,
chain: string,
fullCommand: string,
useCommandSuffix = true,
) {
const nodeName = getUniqueName("temp");
const node: Node = {
name: nodeName,
key: getSha256(nodeName),
image,
fullCommand:
fullCommand + " && " + TMP_DONE + " && " + WAIT_UNTIL_SCRIPT_SUFIX, // leave the pod runnig until we finish transfer files
fullCommand +
(useCommandSuffix ? ` && ${TMP_DONE} && ${WAIT_UNTIL_SCRIPT_SUFIX}` : ""), // leave the pod runnig until we finish transfer files
chain,
validator: false,
invulnerable: false,
Expand Down
2 changes: 2 additions & 0 deletions javascript/packages/orchestrator/src/providers/k8s/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
replaceNetworkRef,
} from "./dynResourceDefinition";
import { KubeClient, initClient } from "./kubeClient";
import { setSubstrateCliArdsVersion } from "./substrateCliArgsHelper";

export const provider = {
KubeClient,
Expand All @@ -14,4 +15,5 @@ export const provider = {
setupChainSpec,
getChainSpecRaw,
replaceNetworkRef,
setSubstrateCliArdsVersion,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { series } from "@zombienet/utils";
import { ComputedNetwork, SubstrateCliArgsVersion } from "../../types";
import { getClient } from "../client";
import { createTempNodeDef, genNodeDef } from "./dynResourceDefinition";
import { KubeClient } from "./kubeClient";

const getVersion = async (
image: string,
command: string,
): Promise<SubstrateCliArgsVersion> => {
const client = getClient() as KubeClient;
const fullCmd = `${command} --help | grep ws-port`;
const node = await createTempNodeDef(
"temp",
image,
"", // don't used
fullCmd,
false,
);

const podDef = await genNodeDef(client.namespace, node);
const podName = podDef.metadata.name;
await client.spawnFromDef(podDef);
const logs = await client.getNodeLogs(podName);

return logs.includes("--ws-port <PORT>")
? SubstrateCliArgsVersion.V0
: SubstrateCliArgsVersion.V1;
};

export const setSubstrateCliArdsVersion = async (network: ComputedNetwork) => {
// Calculate substrate cli version for each node
// and set in the node to use later when we build the cmd.
const imgCmdMap = new Map();
network.relaychain.nodes.reduce((memo, node) => {
const uniq_image_cmd = `${node.image}_${node.command}`;
if (!memo.has(uniq_image_cmd))
memo.set(uniq_image_cmd, { image: node.image, command: node.command });
return memo;
}, imgCmdMap);

network.parachains.reduce((memo, parachain) => {
for (const collator of parachain.collators) {
const uniq_image_cmd = `${collator.image}_${collator.command}`;
if (!memo.has(uniq_image_cmd))
memo.set(uniq_image_cmd, {
image: collator.image,
command: collator.command,
});
}
return memo;
}, imgCmdMap);

// check versions in series
const promiseGenerators = [];
for (const [, v] of imgCmdMap) {
const getVersionPromise = async () => {
const version = await getVersion(v.image, v.command);
v.version = version;
return version;
};
promiseGenerators.push(getVersionPromise);
}

await series(promiseGenerators, 4);

// now we need to iterate and set in each node the version
// IFF is not set
for (const node of network.relaychain.nodes) {
if (node.substrateCliArgsVersion) continue;
const uniq_image_cmd = `${node.image}_${node.command}`;
node.substrateCliArgsVersion = imgCmdMap.get(uniq_image_cmd).version;
}

for (const parachain of network.parachains) {
for (const collator of parachain.collators) {
if (collator.substrateCliArgsVersion) continue;
const uniq_image_cmd = `${collator.image}_${collator.command}`;
collator.substrateCliArgsVersion = imgCmdMap.get(uniq_image_cmd).version;
}
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
replaceNetworkRef,
} from "./dynResourceDefinition";
import { NativeClient, initClient } from "./nativeClient";
import { setSubstrateCliArdsVersion } from "./substrateCliArgsHelper";

export const provider = {
NativeClient,
Expand All @@ -14,4 +15,5 @@ export const provider = {
setupChainSpec,
getChainSpecRaw,
replaceNetworkRef,
setSubstrateCliArdsVersion,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { series } from "@zombienet/utils";
import { ComputedNetwork, SubstrateCliArgsVersion } from "../../types";
import { getClient } from "../client";

const getVersion = async (
image: string,
command: string,
): Promise<SubstrateCliArgsVersion> => {
const client = getClient();
const fullCmd = `${command} --help | grep ws-port`;
const logs = (await client.runCommand(["-c", fullCmd], { allowFail: true }))
.stdout;

return logs.includes("--ws-port <PORT>")
? SubstrateCliArgsVersion.V0
: SubstrateCliArgsVersion.V1;
};

export const setSubstrateCliArdsVersion = async (network: ComputedNetwork) => {
// Calculate substrate cli version for each node
// and set in the node to use later when we build the cmd.
const imgCmdMap = new Map();
network.relaychain.nodes.reduce((memo, node) => {
const uniq_image_cmd = `${node.image}_${node.command}`;
if (!memo.has(uniq_image_cmd))
memo.set(uniq_image_cmd, { image: node.image, command: node.command });
return memo;
}, imgCmdMap);

network.parachains.reduce((memo, parachain) => {
for (const collator of parachain.collators) {
const uniq_image_cmd = `${collator.image}_${collator.command}`;
if (!memo.has(uniq_image_cmd))
memo.set(uniq_image_cmd, {
image: collator.image,
command: collator.command,
});
}
return memo;
}, imgCmdMap);

// check versions in series
const promiseGenerators = [];

for (const [, v] of imgCmdMap) {
const getVersionPromise = async () => {
const version = await getVersion(v.image, v.command);
v.version = version;
return version;
};
promiseGenerators.push(getVersionPromise);
}

await series(promiseGenerators, 4);

// now we need to iterate and set in each node the version
// IFF is not set
for (const node of network.relaychain.nodes) {
if (node.substrateCliArgsVersion) continue;
const uniq_image_cmd = `${node.image}_${node.command}`;
node.substrateCliArgsVersion = imgCmdMap.get(uniq_image_cmd).version;
}

for (const parachain of network.parachains) {
for (const collator of parachain.collators) {
if (collator.substrateCliArgsVersion) continue;
const uniq_image_cmd = `${collator.image}_${collator.command}`;
collator.substrateCliArgsVersion = imgCmdMap.get(uniq_image_cmd).version;
}
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
replaceNetworkRef,
} from "./dynResourceDefinition";
import { PodmanClient, initClient } from "./podmanClient";
import { setSubstrateCliArdsVersion } from "./substrateCliArgsHelper";

export const provider = {
PodmanClient,
Expand All @@ -14,4 +15,5 @@ export const provider = {
setupChainSpec,
getChainSpecRaw,
replaceNetworkRef,
setSubstrateCliArdsVersion,
};
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,8 @@ export class PodmanClient extends Client {
async spawnFromDef(
podDef: any,
filesToCopy: fileMap[] = [],
keystore: string,
chainSpecId: string,
keystore?: string,
chainSpecId?: string,
dbSnapshot?: string,
): Promise<void> {
const name = podDef.metadata.name;
Expand Down Expand Up @@ -383,7 +383,7 @@ export class PodmanClient extends Client {
]);
}

if (keystore) {
if (keystore && chainSpecId) {
const keystoreRemoteDir = `${dataPath.hostPath.path}/chains/${chainSpecId}/keystore`;
await makeDir(keystoreRemoteDir, true);
const keystoreIsEmpty =
Expand Down
Loading

0 comments on commit a2e8619

Please sign in to comment.