Skip to content

Commit

Permalink
Fix #1712, add new cliArgsVersion and clean the logic (#1715)
Browse files Browse the repository at this point in the history
* fix(orchestrator): Add new cliArgs version to support 'export-genesis-head' subcommand and clean the logic to set the version in the diff. providers

* typo

* add debug
  • Loading branch information
pepoviola authored Feb 2, 2024
1 parent c59c353 commit 28a665d
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 49 deletions.
4 changes: 2 additions & 2 deletions javascript/packages/orchestrator/src/configGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
DEFAULT_COLLATOR_IMAGE,
DEFAULT_COMMAND,
DEFAULT_CUMULUS_COLLATOR_BIN,
DEFAULT_GENESIS_GENERATE_SUBCOMMAND,
DEFAULT_GLOBAL_TIMEOUT,
DEFAULT_IMAGE,
DEFAULT_KEYSTORE_KEY_TYPES,
Expand Down Expand Up @@ -357,9 +356,10 @@ export async function generateNetworkSpec(
computedStatePath = genesisStatePath;
}
} else {
// NOTE: the subcommand to execute will be set later based on `substrateCliArgsVersion`.
computedStateCommand = parachain.genesis_state_generator
? parachain.genesis_state_generator
: `${collatorBinary} ${DEFAULT_GENESIS_GENERATE_SUBCOMMAND}`;
: `${collatorBinary} {{GENESIS_GENERATE_SUBCOMMAND}}`;

// TODO: we should remove this conditional ones
// https://github.com/paritytech/polkadot-sdk/pull/2375 and
Expand Down
2 changes: 2 additions & 0 deletions javascript/packages/orchestrator/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const DEFAULT_CHAIN_SPEC_RAW = "{{chainName}}-raw.json";
const DEFAULT_CHAIN_SPEC_COMMAND =
"{{DEFAULT_COMMAND}} build-spec --chain {{chainName}} --disable-default-bootnode";
const DEFAULT_GENESIS_GENERATE_SUBCOMMAND = "export-genesis-state";
const DEFAULT_GENESIS_HEAD_GENERATE_SUBCOMMAND = "export-genesis-head";
const DEFAULT_WASM_GENERATE_SUBCOMMAND = "export-genesis-wasm";
const DEFAULT_ADDER_COLLATOR_BIN = "adder-collator";
const UNDYING_COLLATOR_BIN = "undying-collator";
Expand Down Expand Up @@ -146,6 +147,7 @@ export {
DEFAULT_CUMULUS_COLLATOR_BIN,
DEFAULT_DATA_DIR,
DEFAULT_GENESIS_GENERATE_SUBCOMMAND,
DEFAULT_GENESIS_HEAD_GENERATE_SUBCOMMAND,
DEFAULT_GLOBAL_TIMEOUT,
DEFAULT_IMAGE,
DEFAULT_INDIVIDUAL_TEST_TIMEOUT,
Expand Down
16 changes: 11 additions & 5 deletions javascript/packages/orchestrator/src/paras.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import chainSpecFns, { isRawSpec } from "./chainSpec";
import { getUniqueName } from "./configGenerator";
import {
DEFAULT_COLLATOR_IMAGE,
DEFAULT_GENESIS_GENERATE_SUBCOMMAND,
DEFAULT_GENESIS_HEAD_GENERATE_SUBCOMMAND,
GENESIS_STATE_FILENAME,
GENESIS_WASM_FILENAME,
K8S_WAIT_UNTIL_SCRIPT_SUFIX,
Expand Down Expand Up @@ -225,10 +227,14 @@ export async function generateParachainFiles(

const commands = [];
if (parachain.genesisStateGenerator) {
let genesisStateGenerator = parachain.genesisStateGenerator.replace(
"{{CLIENT_REMOTE_DIR}}",
client.remoteDir as string,
);
const subcommand =
parachain.collators[0].substrateCliArgsVersion! >= 3
? DEFAULT_GENESIS_HEAD_GENERATE_SUBCOMMAND
: DEFAULT_GENESIS_GENERATE_SUBCOMMAND;
let genesisStateGenerator = parachain.genesisStateGenerator
.replace("{{CLIENT_REMOTE_DIR}}", client.remoteDir as string)
.replace("{{GENESIS_GENERATE_SUBCOMMAND}}", subcommand);

// cumulus
if (parachain.cumulusBased) {
const chainSpecPathInNode =
Expand All @@ -242,7 +248,7 @@ export async function generateParachainFiles(
);
}

if (client.providerName === "native" && !parachain.cumulusBased) {
if (client.providerName === "native" && parachain.cumulusBased) {
// Inject a tmp base-path to prevent the use of a pre-existing un-purged data directory. This should only
// be injected for `cumulus` base parachains.
// See https://github.com/paritytech/zombienet/issues/1519
Expand Down
4 changes: 2 additions & 2 deletions javascript/packages/orchestrator/src/providers/k8s/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
genChaosDef,
} from "./dynResourceDefinition";
import { KubeClient, initClient } from "./kubeClient";
import { getCliArgsVersion } from "./substrateCliArgsHelper";
import { getCliArgsHelp } from "./substrateCliArgsHelper";

export const provider = {
KubeClient,
Expand All @@ -16,6 +16,6 @@ export const provider = {
setupChainSpec,
getChainSpecRaw,
replaceNetworkRef,
getCliArgsVersion,
getCliArgsHelp,
genChaosDef,
};
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import { SubstrateCliArgsVersion } from "../../sharedTypes";
import { getClient } from "../client";
import { createTempNodeDef, genNodeDef } from "./dynResourceDefinition";
import { KubeClient } from "./kubeClient";

export const getCliArgsVersion = async (
export const getCliArgsHelp = async (
image: string,
command: string,
): Promise<SubstrateCliArgsVersion> => {
): Promise<string> => {
const client = getClient() as KubeClient;
// use echo to not finish the pod with error status.
// Use echo to not finish the pod with error status.
const fullCmd = `${command} --help || echo ""`;
const node = await createTempNodeDef(
"temp",
image,
"", // don't used
"", // Don't used
fullCmd,
false,
);
Expand All @@ -23,11 +22,5 @@ export const getCliArgsVersion = async (
await client.spawnFromDef(podDef);
const logs = await client.getNodeLogs(podName);

if (logs.includes("--ws-port <PORT>")) {
return SubstrateCliArgsVersion.V0;
} else if (!logs.includes("--insecure-validator-i-know-what-i-do")) {
return SubstrateCliArgsVersion.V1;
} else {
return SubstrateCliArgsVersion.V2;
}
return logs;
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
replaceNetworkRef,
} from "./dynResourceDefinition";
import { NativeClient, initClient } from "./nativeClient";
import { getCliArgsVersion } from "./substrateCliArgsHelper";
import { getCliArgsHelp } from "./substrateCliArgsHelper";

export const provider = {
NativeClient,
Expand All @@ -15,5 +15,5 @@ export const provider = {
setupChainSpec,
getChainSpecRaw,
replaceNetworkRef,
getCliArgsVersion,
getCliArgsHelp,
};
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import { SubstrateCliArgsVersion } from "../../sharedTypes";
import { getClient } from "../client";

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

if (logs.includes("--ws-port <PORT>")) {
return SubstrateCliArgsVersion.V0;
} else if (!logs.includes("--insecure-validator-i-know-what-i-do")) {
return SubstrateCliArgsVersion.V1;
} else {
return SubstrateCliArgsVersion.V2;
}
return logs;
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
replaceNetworkRef,
} from "./dynResourceDefinition";
import { PodmanClient, initClient } from "./podmanClient";
import { getCliArgsVersion } from "./substrateCliArgsHelper";
import { getCliArgsHelp } from "./substrateCliArgsHelper";

export const provider = {
PodmanClient,
Expand All @@ -15,5 +15,5 @@ export const provider = {
setupChainSpec,
getChainSpecRaw,
replaceNetworkRef,
getCliArgsVersion,
getCliArgsHelp,
};
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { SubstrateCliArgsVersion } from "../../sharedTypes";
import { getClient } from "../client";
import { createTempNodeDef, genNodeDef } from "./dynResourceDefinition";

export const getCliArgsVersion = async (
export const getCliArgsHelp = async (
image: string,
command: string,
): Promise<SubstrateCliArgsVersion> => {
): Promise<string> => {
const client = getClient();
const fullCmd = `${command} --help`;
const node = await createTempNodeDef(
Expand All @@ -19,12 +18,5 @@ export const getCliArgsVersion = async (
const podName = podDef.metadata.name;
await client.spawnFromDef(podDef);
const logs = await client.getNodeLogs(podName);

if (logs.includes("--ws-port <PORT>")) {
return SubstrateCliArgsVersion.V0;
} else if (!logs.includes("--insecure-validator-i-know-what-i-do")) {
return SubstrateCliArgsVersion.V1;
} else {
return SubstrateCliArgsVersion.V2;
}
return logs;
};
2 changes: 2 additions & 0 deletions javascript/packages/orchestrator/src/sharedTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export enum SubstrateCliArgsVersion {
V1 = 1,
// Includes the --insecure-validator-i-know-what-i-do flag.
V2 = 2,
// Rename export-genesis-head to export-genesis-head in polkadot-parachain
V3 = 3,
}

// enums
Expand Down
50 changes: 47 additions & 3 deletions javascript/packages/orchestrator/src/substrateCliArgsHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,28 @@ import { series } from "@zombienet/utils";
import { getProvider } from "./providers";
import { Client } from "./providers/client";
import { ComputedNetwork } from "./configTypes";
import { SubstrateCliArgsVersion } from "./sharedTypes";
import { Scope } from "./network";

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

export const setSubstrateCliArgsVersion = async (
network: ComputedNetwork,
client: Client,
) => {
const { getCliArgsVersion } = getProvider(client.providerName);
const { getCliArgsHelp } = getProvider(client.providerName);
// 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) => {
if (node.substrateCliArgsVersion) return memo;
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 });
memo.set(uniq_image_cmd, {
image: node.image,
command: node.command,
scope: Scope.RELAY,
});
return memo;
}, imgCmdMap);

Expand All @@ -27,6 +35,7 @@ export const setSubstrateCliArgsVersion = async (
memo.set(uniq_image_cmd, {
image: collator.image,
command: collator.command,
scope: Scope.PARA,
});
}
return memo;
Expand All @@ -36,7 +45,8 @@ export const setSubstrateCliArgsVersion = async (
const promiseGenerators = [];
for (const [, v] of imgCmdMap) {
const getVersionPromise = async () => {
const version = await getCliArgsVersion(v.image, v.command);
const helpStdout = await getCliArgsHelp(v.image, v.command);
const version = await getCliArgsVersion(helpStdout, v.scope);
v.version = version;
return version;
};
Expand All @@ -61,3 +71,37 @@ export const setSubstrateCliArgsVersion = async (
}
}
};

function getCliArgsVersion(
helpStdout: string,
scope: Scope,
): SubstrateCliArgsVersion {
// IFF stdout includes `ws-port` flag we are always in V0
if (helpStdout.includes("--ws-port <PORT>")) {
debug(`returning cliArgsVersion ${SubstrateCliArgsVersion.V0}`);
return SubstrateCliArgsVersion.V0;
}

// If not, we should check the scope
if (scope == Scope.RELAY) {
const version = !helpStdout.includes(
"--insecure-validator-i-know-what-i-do",
)
? SubstrateCliArgsVersion.V1
: SubstrateCliArgsVersion.V2;

debug(`returning cliArgsVersion ${version}`);
return version;
} else if (scope == Scope.PARA) {
const version = !helpStdout.includes("export-genesis-head")
? SubstrateCliArgsVersion.V2
: SubstrateCliArgsVersion.V3;

debug(`returning cliArgsVersion ${version}`);
return version;
} else {
debug(`returning default cliArgsVersion`);
// For other scopes we just return the latest version.
return SubstrateCliArgsVersion.V3;
}
}

0 comments on commit 28a665d

Please sign in to comment.