@moonwall/cli
Version:
Testing framework for the Moon family of projects
1,481 lines (1,447 loc) • 150 kB
JavaScript
var __getOwnPropNames = Object.getOwnPropertyNames;
var __esm = (fn, res) => function __init() {
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
};
// src/lib/configReader.ts
import { readFile, access } from "fs/promises";
import { readFileSync, existsSync, constants } from "fs";
import JSONC from "jsonc-parser";
import path, { extname } from "path";
async function parseConfig(filePath) {
let result;
const file = await readFile(filePath, "utf8");
switch (extname(filePath)) {
case ".json":
result = JSON.parse(file);
break;
case ".config":
result = JSONC.parse(file);
break;
default:
result = void 0;
break;
}
return result;
}
function parseConfigSync(filePath) {
let result;
const file = readFileSync(filePath, "utf8");
switch (extname(filePath)) {
case ".json":
result = JSON.parse(file);
break;
case ".config":
result = JSONC.parse(file);
break;
default:
result = void 0;
break;
}
return result;
}
function isOptionSet(option) {
const env = getEnvironmentFromConfig();
const optionValue = traverseConfig(env, option);
return optionValue !== void 0;
}
function isEthereumZombieConfig() {
const env = getEnvironmentFromConfig();
return env.foundation.type === "zombie" && !env.foundation.zombieSpec.disableDefaultEthProviders;
}
function isEthereumDevConfig() {
const env = getEnvironmentFromConfig();
return env.foundation.type === "dev" && !env.foundation.launchSpec[0].disableDefaultEthProviders;
}
function getEnvironmentFromConfig() {
const globalConfig = importJsonConfig();
const config = globalConfig.environments.find(({ name }) => name === process.env.MOON_TEST_ENV);
if (!config) {
throw new Error(`Environment ${process.env.MOON_TEST_ENV} not found in config`);
}
return config;
}
function importJsonConfig() {
if (cachedConfig) {
return cachedConfig;
}
const configPath = process.env.MOON_CONFIG_PATH;
if (!configPath) {
throw new Error("No moonwall config path set. This is a defect, please raise it.");
}
const filePath = path.isAbsolute(configPath) ? configPath : path.join(process.cwd(), configPath);
try {
const config = parseConfigSync(filePath);
const replacedConfig = replaceEnvVars(config);
cachedConfig = replacedConfig;
return cachedConfig;
} catch (e) {
console.error(e);
throw new Error(`Error import config at ${filePath}`);
}
}
async function importAsyncConfig() {
if (cachedConfig) {
return cachedConfig;
}
const configPath = process.env.MOON_CONFIG_PATH;
if (!configPath) {
throw new Error("No moonwall config path set. This is a defect, please raise it.");
}
const filePath = path.isAbsolute(configPath) ? configPath : path.join(process.cwd(), configPath);
try {
const config = await parseConfig(filePath);
const replacedConfig = replaceEnvVars(config);
cachedConfig = replacedConfig;
return cachedConfig;
} catch (e) {
console.error(e);
throw new Error(`Error import config at ${filePath}`);
}
}
function replaceEnvVars(value) {
if (typeof value === "string") {
return value.replace(/\$\{([^}]+)\}/g, (match, group) => {
const envVarValue = process.env[group];
return envVarValue || match;
});
}
if (Array.isArray(value)) {
return value.map(replaceEnvVars);
}
if (typeof value === "object" && value !== null) {
return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, replaceEnvVars(v)]));
}
return value;
}
function traverseConfig(configObj, option) {
if (typeof configObj !== "object" || !configObj) return void 0;
if (Object.hasOwn(configObj, option)) {
return configObj[option];
}
for (const key in configObj) {
const result = traverseConfig(configObj[key], option);
if (result !== void 0) {
return result;
}
}
return void 0;
}
var cachedConfig;
var init_configReader = __esm({
"src/lib/configReader.ts"() {
"use strict";
}
});
// src/lib/handlers/devHandler.ts
import {
ALITH_PRIVATE_KEY as ALITH_PRIVATE_KEY3,
BALTATHAR_PRIVATE_KEY,
CHARLETH_PRIVATE_KEY,
DOROTHY_PRIVATE_KEY,
alith as alith2,
createEthersTransaction as createEthersTransaction2,
createViemTransaction as createViemTransaction2,
jumpBlocksDev,
jumpRoundsDev
} from "@moonwall/util";
import { Keyring as Keyring2 } from "@polkadot/api";
// src/internal/foundations/devModeHelpers.ts
init_configReader();
import {
alith,
createAndFinalizeBlock,
customWeb3Request,
generateKeyringPair
} from "@moonwall/util";
import { Keyring } from "@polkadot/api";
import chalk7 from "chalk";
import { createLogger as createLogger15 } from "@moonwall/util";
import { setTimeout as setTimeout3 } from "timers/promises";
// src/lib/contextHelpers.ts
function filterAndApply(events, section, methods, onFound) {
return events.filter(({ event }) => section === event.section && methods.includes(event.method)).map((record) => onFound(record));
}
function getDispatchError({
event: {
data: [dispatchError]
}
}) {
return dispatchError;
}
function extractError(events = []) {
return filterAndApply(events, "system", ["ExtrinsicFailed"], getDispatchError)[0];
}
// src/lib/globalContext.ts
import { createLogger as createLogger14 } from "@moonwall/util";
import zombie from "@zombienet/orchestrator";
import Docker4 from "dockerode";
import { ChildProcess, exec as exec3, execSync as execSync4 } from "child_process";
import fs15 from "fs";
import net3 from "net";
import path12 from "path";
import readline from "readline";
import { setTimeout as timer4 } from "timers/promises";
import { promisify as promisify3 } from "util";
import invariant5 from "tiny-invariant";
// src/internal/logging.ts
var originalWrite = process.stderr.write.bind(process.stderr);
var blockList = [
"has multiple versions, ensure that there is only one installed",
"Unable to map [u8; 32] to a lookup index",
"Either remove and explicitly install matching versions or dedupe using your package manager."
];
process.stderr.write = (chunk, encodingOrCallback, callback) => {
let shouldWrite = true;
if (typeof chunk === "string") {
shouldWrite = !blockList.some((phrase) => chunk.includes(phrase));
}
if (shouldWrite) {
if (typeof encodingOrCallback === "function") {
return originalWrite.call(process.stderr, chunk, void 0, encodingOrCallback);
}
return originalWrite.call(process.stderr, chunk, encodingOrCallback, callback);
}
const cb = typeof encodingOrCallback === "function" ? encodingOrCallback : callback;
if (cb) cb(null);
return true;
};
// src/internal/cmdFunctions/downloader.ts
import { SingleBar, Presets } from "cli-progress";
import fs from "fs";
import { Readable } from "stream";
// src/internal/cmdFunctions/fetchArtifact.ts
import fs2 from "fs/promises";
import path2 from "path";
import semver from "semver";
import chalk from "chalk";
// src/internal/processHelpers.ts
import child_process from "child_process";
import { promisify } from "util";
import { createLogger } from "@moonwall/util";
var logger = createLogger({ name: "actions:runner" });
var debug = logger.debug.bind(logger);
var execAsync = promisify(child_process.exec);
var withTimeout = (promise, ms) => {
return Promise.race([
promise,
new Promise(
(_, reject) => setTimeout(() => reject(new Error("Operation timed out")), ms)
)
]);
};
// src/internal/cmdFunctions/fetchArtifact.ts
import { minimatch } from "minimatch";
// src/lib/repoDefinitions/moonbeam.ts
var repo = {
name: "moonbeam",
binaries: [
{
name: "moonbeam",
defaultArgs: [
"--no-hardware-benchmarks",
"--no-telemetry",
"--reserved-only",
"--rpc-cors=all",
"--unsafe-rpc-external",
"--unsafe-force-node-key-generation",
"--no-grandpa",
"--sealing=manual",
"--force-authoring",
"--database=paritydb",
"--no-prometheus",
"--alice",
"--chain=moonbase-dev",
"--tmp"
]
},
{ name: "moonbase-runtime" },
{ name: "moonbeam-runtime" },
{ name: "moonriver-runtime" }
],
ghAuthor: "moonbeam-foundation",
ghRepo: "moonbeam"
};
var moonbeam_default = repo;
// src/lib/repoDefinitions/polkadot.ts
var repo2 = {
name: "polkadot",
binaries: [
{ name: "polkadot" },
{ name: "polkadot-prepare-worker" },
{ name: "polkadot-execute-worker" }
],
ghAuthor: "paritytech",
ghRepo: "polkadot-sdk"
};
var polkadot_default = repo2;
// src/lib/repoDefinitions/tanssi.ts
var repo3 = {
name: "tanssi",
binaries: [
{
name: "tanssi-node",
defaultArgs: ["--dev", "--sealing=manual", "--no-hardware-benchmarks"]
},
{ name: "container-chain-template-simple-node" },
{ name: "container-chain-template-frontier-node" }
],
ghAuthor: "moondance-labs",
ghRepo: "tanssi"
};
var tanssi_default = repo3;
// src/lib/repoDefinitions/index.ts
init_configReader();
function standardRepos() {
const defaultRepos = [moonbeam_default, polkadot_default, tanssi_default];
return [...defaultRepos];
}
// src/internal/cmdFunctions/fetchArtifact.ts
init_configReader();
import { execSync } from "child_process";
import { Octokit } from "@octokit/rest";
import { confirm } from "@inquirer/prompts";
var octokit = new Octokit({
baseUrl: "https://api.github.com",
log: {
debug: () => {
},
info: () => {
},
warn: console.warn,
error: console.error
}
});
// src/internal/cmdFunctions/initialisation.ts
import fs3 from "fs/promises";
import { confirm as confirm2, input, number } from "@inquirer/prompts";
// src/internal/cmdFunctions/tempLogs.ts
import path3 from "path";
import fs4 from "fs";
// src/internal/commandParsers.ts
import { createLogger as createLogger3 } from "@moonwall/util";
import path4 from "path";
import net from "net";
import { Effect as Effect3 } from "effect";
// src/lib/shardManager.ts
var ShardManager = class _ShardManager {
static instance = null;
_shardInfo = null;
constructor() {
}
static getInstance() {
if (!_ShardManager.instance) {
_ShardManager.instance = new _ShardManager();
}
return _ShardManager.instance;
}
/**
* Initialize shard configuration from command line argument or environment
* @param shardArg Optional shard argument from CLI (format: "current/total")
*/
initializeSharding(shardArg) {
if (shardArg) {
this._shardInfo = this.parseShardString(shardArg);
process.env.MOONWALL_TEST_SHARD = shardArg;
} else if (process.env.MOONWALL_TEST_SHARD) {
this._shardInfo = this.parseShardString(process.env.MOONWALL_TEST_SHARD);
} else {
this._shardInfo = { current: 1, total: 1, isSharded: false };
}
}
/**
* Get current shard information
*/
getShardInfo() {
if (!this._shardInfo) {
this.initializeSharding();
}
return this._shardInfo;
}
/**
* Get shard index (0-based) for port calculations
*/
getShardIndex() {
return this.getShardInfo().current - 1;
}
/**
* Get total number of shards
*/
getTotalShards() {
return this.getShardInfo().total;
}
/**
* Check if sharding is enabled
*/
isSharded() {
return this.getShardInfo().isSharded;
}
/**
* Reset shard configuration (mainly for testing)
*/
reset() {
this._shardInfo = null;
delete process.env.MOONWALL_TEST_SHARD;
}
parseShardString(shardString) {
if (!shardString.includes("/")) {
throw new Error(
`Invalid shard format: "${shardString}". Expected format: "current/total" (e.g., "1/3")`
);
}
const [currentStr, totalStr] = shardString.split("/");
const current = parseInt(currentStr, 10);
const total = parseInt(totalStr, 10);
if (Number.isNaN(current) || Number.isNaN(total) || current < 1 || total < 1) {
throw new Error(
`Invalid shard numbers in "${shardString}". Both current and total must be positive integers.`
);
}
if (current > total) {
throw new Error(
`Invalid shard configuration: current shard ${current} cannot be greater than total ${total}`
);
}
const isSharded = total > 1;
return { current, total, isSharded };
}
};
var shardManager = ShardManager.getInstance();
// src/internal/commandParsers.ts
import invariant from "tiny-invariant";
// src/internal/effect/StartupCacheService.ts
import { Command, FileSystem as FileSystem2, Path } from "@effect/platform";
import { NodeContext } from "@effect/platform-node";
import { createLogger as createLogger2 } from "@moonwall/util";
import { Context, Duration as Duration2, Effect as Effect2, Layer, Option, Stream } from "effect";
import * as crypto from "crypto";
// src/internal/effect/errors.ts
import { Data } from "effect";
var PortDiscoveryError = class extends Data.TaggedError("PortDiscoveryError") {
};
var NodeLaunchError = class extends Data.TaggedError("NodeLaunchError") {
};
var NodeReadinessError = class extends Data.TaggedError("NodeReadinessError") {
};
var ProcessError = class extends Data.TaggedError("ProcessError") {
};
var StartupCacheError = class extends Data.TaggedError("StartupCacheError") {
};
var FileLockError = class extends Data.TaggedError("FileLockError") {
};
// src/internal/effect/FileLock.ts
import { FileSystem } from "@effect/platform";
import { Duration, Effect, Schedule } from "effect";
import * as os from "os";
var LOCK_MAX_AGE = Duration.minutes(2);
var LOCK_POLL_INTERVAL = Duration.millis(500);
var isProcessAlive = (pid) => Effect.try(() => {
process.kill(pid, 0);
return true;
}).pipe(Effect.orElseSucceed(() => false));
var isLockStale = (info) => Effect.gen(function* () {
const isTimedOut = Date.now() - info.timestamp > Duration.toMillis(LOCK_MAX_AGE);
if (isTimedOut) return true;
const isSameHost = info.hostname === os.hostname();
if (!isSameHost) return false;
const alive = yield* isProcessAlive(info.pid);
return !alive;
});
var cleanupStaleLock = (lockPath) => Effect.gen(function* () {
const fs17 = yield* FileSystem.FileSystem;
const infoPath = `${lockPath}/lock.json`;
const exists = yield* fs17.exists(infoPath).pipe(Effect.orElseSucceed(() => false));
if (!exists) return;
const content = yield* fs17.readFileString(infoPath).pipe(Effect.orElseSucceed(() => ""));
const info = yield* Effect.try(() => JSON.parse(content)).pipe(
Effect.orElseSucceed(() => null)
);
if (!info) return;
const stale = yield* isLockStale(info);
if (stale) {
yield* fs17.remove(lockPath, { recursive: true }).pipe(Effect.ignore);
}
});
var writeLockInfo = (lockPath) => Effect.gen(function* () {
const fs17 = yield* FileSystem.FileSystem;
const info = {
pid: process.pid,
timestamp: Date.now(),
hostname: os.hostname()
};
yield* fs17.writeFileString(`${lockPath}/lock.json`, JSON.stringify(info)).pipe(Effect.ignore);
});
var tryAcquireLock = (lockPath) => Effect.gen(function* () {
const fs17 = yield* FileSystem.FileSystem;
yield* cleanupStaleLock(lockPath);
yield* fs17.makeDirectory(lockPath).pipe(Effect.mapError(() => new FileLockError({ reason: "acquisition_failed", lockPath })));
yield* writeLockInfo(lockPath);
});
var acquireFileLock = (lockPath, timeout = Duration.minutes(2)) => tryAcquireLock(lockPath).pipe(
Effect.retry(Schedule.fixed(LOCK_POLL_INTERVAL).pipe(Schedule.upTo(timeout))),
Effect.catchAll(() => Effect.fail(new FileLockError({ reason: "timeout", lockPath })))
);
var releaseFileLock = (lockPath) => Effect.gen(function* () {
const fs17 = yield* FileSystem.FileSystem;
yield* fs17.remove(lockPath, { recursive: true }).pipe(Effect.ignore);
});
var withFileLock = (lockPath, effect, timeout = Duration.minutes(2)) => Effect.acquireUseRelease(
acquireFileLock(lockPath, timeout),
() => effect,
() => releaseFileLock(lockPath)
);
// src/internal/effect/StartupCacheService.ts
var logger2 = createLogger2({ name: "StartupCacheService" });
var StartupCacheService = class extends Context.Tag("StartupCacheService")() {
};
var hashFile = (filePath) => Effect2.gen(function* () {
const fs17 = yield* FileSystem2.FileSystem;
const hash = crypto.createHash("sha256");
yield* fs17.stream(filePath).pipe(Stream.runForEach((chunk) => Effect2.sync(() => hash.update(chunk))));
return hash.digest("hex");
}).pipe(Effect2.mapError((cause) => new StartupCacheError({ cause, operation: "hash" })));
var findPrecompiledWasm = (dir) => Effect2.gen(function* () {
const fs17 = yield* FileSystem2.FileSystem;
const pathService = yield* Path.Path;
const exists = yield* fs17.exists(dir);
if (!exists) return Option.none();
const files = yield* fs17.readDirectory(dir).pipe(Effect2.orElseSucceed(() => []));
const wasmFile = files.find(
(f) => f.startsWith("precompiled_wasm_") || f.endsWith(".cwasm") || f.endsWith(".wasm")
);
return wasmFile ? Option.some(pathService.join(dir, wasmFile)) : Option.none();
}).pipe(Effect2.catchAll(() => Effect2.succeed(Option.none())));
var checkCache = (cacheDir, hashPath, expectedHash) => Effect2.gen(function* () {
const fs17 = yield* FileSystem2.FileSystem;
const savedHash = yield* fs17.readFileString(hashPath).pipe(Effect2.orElseSucceed(() => ""));
if (savedHash.trim() !== expectedHash) return Option.none();
const wasmPath = yield* findPrecompiledWasm(cacheDir);
if (Option.isNone(wasmPath)) return Option.none();
const accessible = yield* fs17.access(wasmPath.value).pipe(
Effect2.as(true),
Effect2.orElseSucceed(() => false)
);
return accessible ? wasmPath : Option.none();
});
var runPrecompile = (binPath, chainArg, outputDir) => Effect2.gen(function* () {
const fs17 = yield* FileSystem2.FileSystem;
const pathService = yield* Path.Path;
const args = chainArg ? ["precompile-wasm", chainArg, outputDir] : ["precompile-wasm", outputDir];
logger2.debug(`Precompiling: ${binPath} ${args.join(" ")}`);
const startTime = Date.now();
const exitCode = yield* Command.exitCode(Command.make(binPath, ...args)).pipe(
Effect2.mapError(
(e) => new StartupCacheError({ cause: e, operation: "precompile" })
)
);
const files = yield* fs17.readDirectory(outputDir).pipe(Effect2.mapError((e) => new StartupCacheError({ cause: e, operation: "precompile" })));
const wasmFile = files.find(
(f) => f.startsWith("precompiled_wasm_") || f.endsWith(".cwasm") || f.endsWith(".wasm")
);
if (!wasmFile) {
return yield* Effect2.fail(
new StartupCacheError({
cause: `precompile-wasm failed (code ${exitCode}): no WASM file generated`,
operation: "precompile"
})
);
}
const wasmPath = pathService.join(outputDir, wasmFile);
logger2.debug(`Precompiled in ${Date.now() - startTime}ms: ${wasmPath}`);
return wasmPath;
});
var generateRawChainSpec = (binPath, chainName, outputPath) => Effect2.gen(function* () {
const fs17 = yield* FileSystem2.FileSystem;
const args = chainName === "dev" || chainName === "default" ? ["build-spec", "--dev", "--raw"] : ["build-spec", `--chain=${chainName}`, "--raw"];
logger2.debug(`Generating raw chain spec: ${binPath} ${args.join(" ")}`);
const stdout = yield* Command.string(Command.make(binPath, ...args)).pipe(
Effect2.mapError(
(e) => new StartupCacheError({ cause: e, operation: "chainspec" })
)
);
if (!stdout.length) {
return yield* Effect2.fail(
new StartupCacheError({ cause: "build-spec produced no output", operation: "chainspec" })
);
}
yield* fs17.writeFileString(outputPath, stdout).pipe(Effect2.mapError((e) => new StartupCacheError({ cause: e, operation: "chainspec" })));
return outputPath;
});
var maybeGetRawChainSpec = (binPath, chainName, cacheSubDir, shouldGenerate) => Effect2.gen(function* () {
if (!shouldGenerate) return Option.none();
const fs17 = yield* FileSystem2.FileSystem;
const pathService = yield* Path.Path;
const rawSpecPath = pathService.join(cacheSubDir, `${chainName}-raw.json`);
const exists = yield* fs17.exists(rawSpecPath).pipe(Effect2.orElseSucceed(() => false));
if (exists) return Option.some(rawSpecPath);
return yield* generateRawChainSpec(binPath, chainName, rawSpecPath).pipe(
Effect2.map(Option.some),
Effect2.catchAll(() => Effect2.succeed(Option.none()))
);
});
var getCachedArtifactsImpl = (config) => Effect2.gen(function* () {
const fs17 = yield* FileSystem2.FileSystem;
const pathService = yield* Path.Path;
const binaryHash = yield* hashFile(config.binPath);
const shortHash = binaryHash.substring(0, 12);
const chainName = config.isDevMode ? "dev" : config.chainArg?.match(/--chain[=\s]?(\S+)/)?.[1] || "default";
const binName = pathService.basename(config.binPath);
const cacheSubDir = pathService.join(config.cacheDir, `${binName}-${chainName}-${shortHash}`);
const hashPath = pathService.join(cacheSubDir, "binary.hash");
const lockPath = pathService.join(config.cacheDir, `${binName}-${chainName}.lock`);
yield* fs17.makeDirectory(cacheSubDir, { recursive: true }).pipe(Effect2.mapError((e) => new StartupCacheError({ cause: e, operation: "cache" })));
const cached = yield* checkCache(cacheSubDir, hashPath, binaryHash);
if (Option.isSome(cached)) {
logger2.debug(`Using cached precompiled WASM: ${cached.value}`);
const rawChainSpecPath = yield* maybeGetRawChainSpec(
config.binPath,
chainName,
cacheSubDir,
config.generateRawChainSpec ?? false
);
return {
precompiledPath: cached.value,
fromCache: true,
rawChainSpecPath: Option.getOrUndefined(rawChainSpecPath)
};
}
return yield* withFileLock(
lockPath,
Effect2.gen(function* () {
const nowCached = yield* checkCache(cacheSubDir, hashPath, binaryHash);
if (Option.isSome(nowCached)) {
logger2.debug(
`Using cached precompiled WASM (created by another process): ${nowCached.value}`
);
const rawChainSpecPath2 = yield* maybeGetRawChainSpec(
config.binPath,
chainName,
cacheSubDir,
config.generateRawChainSpec ?? false
);
return {
precompiledPath: nowCached.value,
fromCache: true,
rawChainSpecPath: Option.getOrUndefined(rawChainSpecPath2)
};
}
logger2.debug("Precompiling WASM (this may take a moment)...");
const wasmPath = yield* runPrecompile(config.binPath, config.chainArg, cacheSubDir);
yield* fs17.writeFileString(hashPath, binaryHash).pipe(Effect2.mapError((e) => new StartupCacheError({ cause: e, operation: "cache" })));
const rawChainSpecPath = yield* maybeGetRawChainSpec(
config.binPath,
chainName,
cacheSubDir,
config.generateRawChainSpec ?? false
);
return {
precompiledPath: wasmPath,
fromCache: false,
rawChainSpecPath: Option.getOrUndefined(rawChainSpecPath)
};
}),
Duration2.minutes(2)
);
});
var StartupCacheServiceLive = Layer.succeed(StartupCacheService, {
getCachedArtifacts: (config) => getCachedArtifactsImpl(config).pipe(
Effect2.mapError(
(e) => e._tag === "FileLockError" ? new StartupCacheError({ cause: e, operation: "lock" }) : e
),
Effect2.provide(NodeContext.layer)
)
});
var StartupCacheServiceTestable = Layer.succeed(StartupCacheService, {
getCachedArtifacts: (config) => getCachedArtifactsImpl(config).pipe(
Effect2.mapError(
(e) => e._tag === "FileLockError" ? new StartupCacheError({ cause: e, operation: "lock" }) : e
)
)
});
// src/internal/commandParsers.ts
var logger3 = createLogger3({ name: "commandParsers" });
function parseZombieCmd(launchSpec) {
if (launchSpec) {
return { cmd: launchSpec.configPath };
}
throw new Error(
"No ZombieSpec found in config. Are you sure your moonwall.config.json file has the correct 'configPath' in zombieSpec?"
);
}
function fetchDefaultArgs(binName, additionalRepos = []) {
let defaultArgs;
const repos = [...standardRepos(), ...additionalRepos];
for (const repo4 of repos) {
const foundBin = repo4.binaries.find((bin) => bin.name === binName);
if (foundBin) {
defaultArgs = foundBin.defaultArgs;
break;
}
}
if (!defaultArgs) {
defaultArgs = ["--dev"];
}
return defaultArgs;
}
var LaunchCommandParser = class _LaunchCommandParser {
args;
cmd;
launch;
launchSpec;
launchOverrides;
constructor(options) {
const { launchSpec, additionalRepos, launchOverrides } = options;
this.launchSpec = launchSpec;
this.launchOverrides = launchOverrides;
this.launch = !launchSpec.running ? true : launchSpec.running;
this.cmd = launchSpec.binPath;
this.args = launchSpec.options ? [...launchSpec.options] : fetchDefaultArgs(path4.basename(launchSpec.binPath), additionalRepos);
}
overrideArg(newArg) {
const newArgKey = newArg.split("=")[0];
const existingIndex = this.args.findIndex((arg) => arg.startsWith(`${newArgKey}=`));
if (existingIndex !== -1) {
this.args[existingIndex] = newArg;
} else {
this.args.push(newArg);
}
}
async withPorts() {
if (process.env.MOON_RECYCLE === "true") {
const existingPort = process.env.MOONWALL_RPC_PORT;
if (existingPort) {
this.overrideArg(`--rpc-port=${existingPort}`);
}
return this;
}
if (this.launchSpec.ports) {
const ports = this.launchSpec.ports;
if (ports.p2pPort) {
this.overrideArg(`--port=${ports.p2pPort}`);
}
if (ports.wsPort) {
this.overrideArg(`--ws-port=${ports.wsPort}`);
}
if (ports.rpcPort) {
this.overrideArg(`--rpc-port=${ports.rpcPort}`);
}
} else {
const freePort = (await getFreePort()).toString();
process.env.MOONWALL_RPC_PORT = freePort;
this.overrideArg(`--rpc-port=${freePort}`);
}
return this;
}
withDefaultForkConfig() {
const forkOptions = this.launchSpec.defaultForkConfig;
if (forkOptions) {
this.applyForkOptions(forkOptions);
}
return this;
}
withLaunchOverrides() {
if (this.launchOverrides?.forkConfig) {
this.applyForkOptions(this.launchOverrides.forkConfig);
}
return this;
}
print() {
logger3.debug(`Command to run: ${this.cmd}`);
logger3.debug(`Arguments: ${this.args.join(" ")}`);
return this;
}
applyForkOptions(forkOptions) {
if (forkOptions.url) {
invariant(forkOptions.url.startsWith("http"), "Fork URL must start with http:// or https://");
this.overrideArg(`--fork-chain-from-rpc=${forkOptions.url}`);
}
if (forkOptions.blockHash) {
this.overrideArg(`--block=${forkOptions.blockHash}`);
}
if (forkOptions.stateOverridePath) {
this.overrideArg(`--fork-state-overrides=${forkOptions.stateOverridePath}`);
}
if (forkOptions.verbose) {
this.overrideArg("-llazy-loading=trace");
}
}
/**
* Cache startup artifacts if enabled in launchSpec.
* This uses an Effect-based service that caches artifacts by binary hash.
*
* When cacheStartupArtifacts is enabled, this generates:
* 1. Precompiled WASM for the runtime
* 2. Raw chain spec to skip genesis WASM compilation
*
* This reduces startup from ~3s to ~200ms (~10x improvement).
*/
async withStartupCache() {
if (!this.launchSpec.cacheStartupArtifacts) {
return this;
}
if (this.launchSpec.useDocker) {
logger3.warn("Startup caching is not supported for Docker images, skipping");
return this;
}
const chainArg = this.args.find((arg) => arg.startsWith("--chain"));
const hasDevFlag = this.args.includes("--dev");
const existingChainName = chainArg?.match(/--chain[=\s]?(\S+)/)?.[1];
const canGenerateRawSpec = hasDevFlag || !!existingChainName;
const cacheDir = this.launchSpec.startupCacheDir || path4.join(process.cwd(), "tmp", "startup-cache");
const program = StartupCacheService.pipe(
Effect3.flatMap(
(service) => service.getCachedArtifacts({
binPath: this.launchSpec.binPath,
chainArg,
cacheDir,
// Generate raw chain spec for faster startup (works for both --dev and --chain=XXX)
generateRawChainSpec: canGenerateRawSpec,
// Pass dev mode flag for proper chain name detection
isDevMode: hasDevFlag
})
),
Effect3.provide(StartupCacheServiceLive)
);
try {
const result = await Effect3.runPromise(program);
const precompiledDir = path4.dirname(result.precompiledPath);
this.overrideArg(`--wasmtime-precompiled=${precompiledDir}`);
if (result.rawChainSpecPath) {
if (hasDevFlag) {
this.args = this.args.filter((arg) => arg !== "--dev");
this.overrideArg(`--chain=${result.rawChainSpecPath}`);
this.overrideArg("--alice");
this.overrideArg("--force-authoring");
this.overrideArg("--rpc-cors=all");
this.overrideArg(
"--node-key=0000000000000000000000000000000000000000000000000000000000000001"
);
} else if (existingChainName) {
this.overrideArg(`--chain=${result.rawChainSpecPath}`);
}
logger3.debug(`Using raw chain spec for ~10x faster startup: ${result.rawChainSpecPath}`);
}
process.env.MOONWALL_CACHE_DIR = precompiledDir;
logger3.debug(
result.fromCache ? `Using cached precompiled WASM: ${result.precompiledPath}` : `Precompiled WASM created: ${result.precompiledPath}`
);
} catch (error) {
logger3.warn(`WASM precompilation failed, continuing without: ${error}`);
}
return this;
}
build() {
return {
cmd: this.cmd,
args: this.args,
launch: this.launch
};
}
static async create(options) {
const parser = new _LaunchCommandParser(options);
const parsed = await parser.withPorts().then((p) => p.withDefaultForkConfig().withLaunchOverrides()).then((p) => p.withStartupCache());
if (options.verbose) {
parsed.print();
}
return parsed.build();
}
};
function parseChopsticksRunCmd(launchSpecs) {
const launch = !launchSpecs[0].running ? true : launchSpecs[0].running;
if (launchSpecs.length === 1) {
const chopsticksCmd2 = "node";
const chopsticksArgs2 = [
"node_modules/@acala-network/chopsticks/chopsticks.cjs",
`--config=${launchSpecs[0].configPath}`,
`--host=${launchSpecs[0].address ?? "127.0.0.1"}`
];
const mode = launchSpecs[0].buildBlockMode ? launchSpecs[0].buildBlockMode : "manual";
const num = mode === "batch" ? "Batch" : mode === "instant" ? "Instant" : "Manual";
chopsticksArgs2.push(`--build-block-mode=${num}`);
if (launchSpecs[0].wsPort) {
chopsticksArgs2.push(`--port=${launchSpecs[0].wsPort}`);
}
if (launchSpecs[0].wasmOverride) {
chopsticksArgs2.push(`--wasm-override=${launchSpecs[0].wasmOverride}`);
}
if (launchSpecs[0].allowUnresolvedImports) {
chopsticksArgs2.push("--allow-unresolved-imports");
}
return {
cmd: chopsticksCmd2,
args: chopsticksArgs2,
launch
};
}
const chopsticksCmd = "node";
const chopsticksArgs = ["node_modules/@acala-network/chopsticks/chopsticks.cjs", "xcm"];
for (const spec of launchSpecs) {
const type = spec.type ? spec.type : "parachain";
switch (type) {
case "parachain":
chopsticksArgs.push(`--parachain=${spec.configPath}`);
break;
case "relaychain":
chopsticksArgs.push(`--relaychain=${spec.configPath}`);
}
}
return {
cmd: chopsticksCmd,
args: chopsticksArgs,
launch
};
}
var isPortAvailable = async (port) => {
return new Promise((resolve) => {
const server = net.createServer();
server.listen(port, () => {
server.once("close", () => resolve(true));
server.close();
});
server.on("error", () => resolve(false));
});
};
var getNextAvailablePort = async (startPort) => {
let port = startPort;
while (port <= 65535) {
if (await isPortAvailable(port)) {
return port;
}
port++;
}
throw new Error(`No available ports found starting from ${startPort}`);
};
var getFreePort = async () => {
const shardIndex = shardManager.getShardIndex();
const totalShards = shardManager.getTotalShards();
const poolId = parseInt(process.env.VITEST_POOL_ID || "0", 10);
const basePort = 1e4;
const shardOffset = shardIndex * 1e3;
const poolOffset = poolId * 100;
const processOffset = process.pid % 50;
const calculatedPort = basePort + shardOffset + poolOffset + processOffset;
const startPort = Math.min(calculatedPort, 6e4 + shardIndex * 100 + poolId);
logger3.debug(
`Port calculation: shard=${shardIndex + 1}/${totalShards}, pool=${poolId}, final=${startPort}`
);
return getNextAvailablePort(startPort);
};
// src/internal/deriveTestIds.ts
import chalk2 from "chalk";
import fs5 from "fs";
import { confirm as confirm3 } from "@inquirer/prompts";
import path5 from "path";
// src/internal/testIdParser.ts
import { Lang, parse, findInFiles } from "@ast-grep/napi";
// src/internal/fileCheckers.ts
import fs6 from "fs";
import { execSync as execSync2 } from "child_process";
import chalk3 from "chalk";
import os2 from "os";
import path6 from "path";
import { select } from "@inquirer/prompts";
async function checkExists(path14) {
const binPath = path14.split(" ")[0];
const fsResult = fs6.existsSync(binPath);
if (!fsResult) {
throw new Error(
`No binary file found at location: ${binPath}
Are you sure your ${chalk3.bgWhiteBright.blackBright(
"moonwall.config.json"
)} file has the correct "binPath" in launchSpec?`
);
}
const binArch = await getBinaryArchitecture(binPath);
const currentArch = os2.arch();
if (binArch !== currentArch && binArch !== "unknown") {
throw new Error(
`The binary architecture ${chalk3.bgWhiteBright.blackBright(
binArch
)} does not match this system's architecture ${chalk3.bgWhiteBright.blackBright(
currentArch
)}
Download or compile a new binary executable for ${chalk3.bgWhiteBright.blackBright(
currentArch
)} `
);
}
return true;
}
function checkAccess(path14) {
const binPath = path14.split(" ")[0];
try {
fs6.accessSync(binPath, fs6.constants.X_OK);
} catch (_err) {
console.error(`The file ${binPath} is not executable`);
throw new Error(`The file at ${binPath} , lacks execute permissions.`);
}
}
async function getBinaryArchitecture(filePath) {
return new Promise((resolve, reject) => {
const architectureMap = {
0: "unknown",
3: "x86",
62: "x64",
183: "arm64"
};
fs6.open(filePath, "r", (err, fd) => {
if (err) {
reject(err);
return;
}
const buffer = Buffer.alloc(20);
fs6.read(fd, buffer, 0, 20, 0, (err2, _bytesRead, buffer2) => {
if (err2) {
reject(err2);
return;
}
const e_machine = buffer2.readUInt16LE(18);
const architecture = architectureMap[e_machine] || "unknown";
resolve(architecture);
});
});
});
}
// src/internal/foundations/chopsticksHelpers.ts
import chalk4 from "chalk";
import { setTimeout as setTimeout2 } from "timers/promises";
// src/internal/foundations/zombieHelpers.ts
import chalk5 from "chalk";
import fs7 from "fs";
import invariant2 from "tiny-invariant";
import { setTimeout as timer } from "timers/promises";
import net2 from "net";
async function checkZombieBins(config) {
const relayBinPath = config.relaychain.default_command;
if (!relayBinPath) {
throw new Error("No relayBinPath '[relaychain.default_command]' specified in zombie config");
}
await checkExists(relayBinPath);
checkAccess(relayBinPath);
if (config.parachains) {
const promises3 = config.parachains.map(async (para) => {
if (para.collator) {
if (!para.collator.command) {
throw new Error(
"No command found for collator, please check your zombienet config file for collator command"
);
}
await checkExists(para.collator.command);
checkAccess(para.collator.command);
}
if (para.collators) {
for (const coll of para.collators) {
if (!coll.command) {
throw new Error(
"No command found for collators, please check your zombienet config file for collators command"
);
}
await checkExists(coll.command);
checkAccess(coll.command);
}
}
});
await Promise.all(promises3);
}
}
function getZombieConfig(path14) {
const fsResult = fs7.existsSync(path14);
if (!fsResult) {
throw new Error(
`No ZombieConfig file found at location: ${path14}
Are you sure your ${chalk5.bgWhiteBright.blackBright(
"moonwall.config.json"
)} file has the correct "configPath" in zombieSpec?`
);
}
const buffer = fs7.readFileSync(path14, "utf-8");
return JSON.parse(buffer);
}
// src/internal/launcherCommon.ts
init_configReader();
import chalk6 from "chalk";
import { execSync as execSync3 } from "child_process";
import fs8 from "fs";
import path7 from "path";
import Docker from "dockerode";
import { select as select2 } from "@inquirer/prompts";
// src/internal/localNode.ts
init_configReader();
import { createLogger as createLogger4 } from "@moonwall/util";
import Docker2 from "dockerode";
import { exec, spawn, spawnSync } from "child_process";
import fs9 from "fs";
import path8 from "path";
import { setTimeout as timer2 } from "timers/promises";
import util from "util";
import invariant3 from "tiny-invariant";
import WebSocket from "ws";
var execAsync2 = util.promisify(exec);
var logger4 = createLogger4({ name: "localNode" });
var debug2 = logger4.debug.bind(logger4);
async function launchDockerContainer(imageName, args, name, dockerConfig) {
const docker = new Docker2();
const port = args.find((a) => a.includes("port"))?.split("=")[1];
debug2(`\x1B[36mStarting Docker container ${imageName} on port ${port}...\x1B[0m`);
const dirPath = path8.join(process.cwd(), "tmp", "node_logs");
const logLocation = path8.join(dirPath, `${name}_docker_${Date.now()}.log`);
const fsStream = fs9.createWriteStream(logLocation);
process.env.MOON_LOG_LOCATION = logLocation;
const portBindings = dockerConfig?.exposePorts?.reduce(
(acc, { hostPort, internalPort }) => {
acc[`${internalPort}/tcp`] = [{ HostPort: hostPort.toString() }];
return acc;
},
{}
);
const rpcPort = args.find((a) => a.includes("rpc-port"))?.split("=")[1];
invariant3(rpcPort, "RPC port not found, this is a bug");
const containerOptions = {
Image: imageName,
platform: "linux/amd64",
Cmd: args,
name: dockerConfig?.containerName || `moonwall_${name}_${Date.now()}`,
ExposedPorts: {
...Object.fromEntries(
dockerConfig?.exposePorts?.map(({ internalPort }) => [`${internalPort}/tcp`, {}]) || []
),
[`${rpcPort}/tcp`]: {}
},
HostConfig: {
PortBindings: {
...portBindings,
[`${rpcPort}/tcp`]: [{ HostPort: rpcPort }]
}
},
Env: dockerConfig?.runArgs?.filter((arg) => arg.startsWith("env:")).map((arg) => arg.slice(4))
};
try {
await pullImage(imageName, docker);
const container = await docker.createContainer(containerOptions);
await container.start();
const containerInfo = await container.inspect();
if (!containerInfo.State.Running) {
const errorMessage = `Container failed to start: ${containerInfo.State.Error}`;
console.error(errorMessage);
fs9.appendFileSync(logLocation, `${errorMessage}
`);
throw new Error(errorMessage);
}
for (let i = 0; i < 300; i++) {
const isReady = await checkWebSocketJSONRPC(Number.parseInt(rpcPort, 10));
if (isReady) {
break;
}
await timer2(100);
}
return { runningNode: container, fsStream };
} catch (error) {
if (error instanceof Error) {
console.error(`Docker container launch failed: ${error.message}`);
fs9.appendFileSync(logLocation, `Docker launch error: ${error.message}
`);
}
throw error;
}
}
async function launchNodeLegacy(options) {
const { command: cmd, args, name, launchSpec: config } = options;
if (config?.useDocker) {
return launchDockerContainer(cmd, args, name, config.dockerConfig);
}
if (cmd.includes("moonbeam")) {
await checkExists(cmd);
checkAccess(cmd);
}
const port = args.find((a) => a.includes("port"))?.split("=")[1];
debug2(`\x1B[36mStarting ${name} node on port ${port}...\x1B[0m`);
const dirPath = path8.join(process.cwd(), "tmp", "node_logs");
const runningNode = spawn(cmd, args);
const logLocation = path8.join(
dirPath,
`${path8.basename(cmd)}_node_${args.find((a) => a.includes("port"))?.split("=")[1]}_${runningNode.pid}.log`
).replaceAll("node_node_undefined", "chopsticks");
process.env.MOON_LOG_LOCATION = logLocation;
const fsStream = fs9.createWriteStream(logLocation);
runningNode.on("error", (err) => {
if (err.errno === "ENOENT") {
console.error(`\x1B[31mMissing Local binary at(${cmd}).
Please compile the project\x1B[0m`);
}
throw new Error(err.message);
});
const logHandler = (chunk) => {
if (fsStream.writable) {
fsStream.write(chunk, (err) => {
if (err) console.error(err);
else fsStream.emit("drain");
});
}
};
runningNode.stderr?.on("data", logHandler);
runningNode.stdout?.on("data", logHandler);
runningNode.once("exit", (code, signal) => {
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
let message;
const moonwallNode = runningNode;
if (moonwallNode.isMoonwallTerminating) {
message = `${timestamp} [moonwall] process killed. reason: ${moonwallNode.moonwallTerminationReason || "unknown"}`;
} else if (code !== null) {
message = `${timestamp} [moonwall] process exited with status code ${code}`;
} else if (signal !== null) {
message = `${timestamp} [moonwall] process terminated by signal ${signal}`;
} else {
message = `${timestamp} [moonwall] process terminated unexpectedly`;
}
if (fsStream.writable) {
fsStream.write(`${message}
`, (err) => {
if (err) console.error(`Failed to write exit message to log: ${err}`);
fsStream.end();
});
} else {
try {
fs9.appendFileSync(logLocation, `${message}
`);
} catch (err) {
console.error(`Failed to append exit message to log file: ${err}`);
}
fsStream.end();
}
runningNode.stderr?.removeListener("data", logHandler);
runningNode.stdout?.removeListener("data", logHandler);
});
if (!runningNode.pid) {
const errorMessage = "Failed to start child process";
console.error(errorMessage);
fs9.appendFileSync(logLocation, `${errorMessage}
`);
throw new Error(errorMessage);
}
if (runningNode.exitCode !== null) {
const errorMessage = `Child process exited immediately with code ${runningNode.exitCode}`;
console.error(errorMessage);
fs9.appendFileSync(logLocation, `${errorMessage}
`);
throw new Error(errorMessage);
}
const isRunning = await isPidRunning(runningNode.pid);
if (!isRunning) {
const errorMessage = `Process with PID ${runningNode.pid} is not running`;
spawnSync(cmd, args, { stdio: "inherit" });
throw new Error(errorMessage);
}
probe: for (let i = 0; ; i++) {
try {
const ports = await findPortsByPid(runningNode.pid);
if (ports) {
for (const port2 of ports) {
try {
const isReady = await checkWebSocketJSONRPC(port2);
if (isReady) {
break probe;
}
} catch {
}
}
}
} catch {
if (i === 300) {
throw new Error("Could not find ports for node after 30 seconds");
}
await timer2(100);
continue;
}
await timer2(100);
}
return { runningNode, fsStream };
}
function isPidRunning(pid) {
return new Promise((resolve) => {
exec(`ps -p ${pid} -o pid=`, (error, stdout, _stderr) => {
if (error) {
resolve(false);
} else {
resolve(stdout.trim() !== "");
}
});
});
}
async function checkWebSocketJSONRPC(port) {
try {
const isEthereumChain = isEthereumDevConfig() || isEthereumZombieConfig();
const ws = new WebSocket(`ws://localhost:${port}`);
const checkWsMethod = async (method) => {
return new Promise((resolve) => {
const timeout = setTimeout(() => {
resolve(false);
}, 5e3);
ws.send(
JSON.stringify({
jsonrpc: "2.0",
id: Math.floor(Math.random() * 1e4),
method,
params: []
})
);
const messageHandler = (data) => {
try {
const response = JSON.parse(data.toString());
if (response.jsonrpc === "2.0" && !response.error) {
clearTimeout(timeout);
ws.removeListener("message", messageHandler);
resolve(true);
}
} catch (_e) {
}
};
ws.on("message", messageHandler);
});
};
const wsResult = await new Promise((resolve) => {
ws.on("open", async () => {
try {
const systemChainAvailable = await checkWsMethod("system_chain");
if (!systemChainAvailable) {
resolve(false);
return;
}
if (isEthereumChain) {
const ethChainIdAvailable = await checkWsMethod("eth_chainId");
if (!ethChainIdAvailable) {
resolve(false);
return;
}
}
resolve(true);
} catch (_e) {
resolve(false);
}
});
ws.on("error", () => {
resolve(false);
});
});
ws?.close();
if (!wsResult) {
return false;
}
const httpUrl = `http://localhost:${port}`;
const checkHttpMethod = async (method) => {
try {
const response = await fetch(httpUrl, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
jsonrpc: "2.0",
id: Math.floor(Math.random() * 1e4),
method,
params: []
})
});
if (!response.ok) {
return false;
}
const data = await response.json();
return !data.error;
} catch (_e) {
return false;
}
};
try {
const systemChainAvailable = await checkHttpMethod("system_chain");
if (!systemChainAvailable) {
return false;
}
if (isEthereumChain) {
const ethChainIdAvailable = await checkHttpMethod("eth_chainId");
return ethChainIdAvailable;
}
return true;
} catch (_e) {
return false;
}
} catch {
return false;
}
}
async function findPortsByPid(pid, retryCount = 600, retryDelay = 100) {
for (let i = 0; i < retryCount; i++) {
try {
const { stdout } = await execAsync2(`lsof -p ${pid} -n -P | grep LISTEN`);
const ports = [];
const lines = stdout.split("\n");
for (const line of lines) {
const regex = /(?:.+):(\d+)/;
const match = line.match(regex);
if (match) {
ports.push(Number(match[1]));
}
}
if (ports.length) {
return ports;
}
throw new Error("Could not find any ports");
} catch (error) {
if (i === retryCount - 1) {
throw error;
}
}
await new Promise((resolve) => setTimeout(resolve, retryDelay));
}
return [];
}
async function pullImage(imageName, docker) {
console.log(`Pulling Docker image: ${imageName}`);
const pullStream = await docker.pull(imageName);
await new Promise((resolve, reject) => {
docker.modem.followProgress(pullStream, (err, output) => {
if (err) {
reject(err);
} else {
resolve(output);
}
});
});
}
// src/internal/node.ts
import fs13 from "fs";
import path10 from "path";
import WebSocket2 from "ws";
init_configReader();
import { createLogger as createLogger12 } from "@moonwall/util";
import { setTimeout as timer3 } from "timers/promises";
import Docker3 from "dockerode";
import invariant4 from "tiny-invariant";
// src/internal/effect/ProcessManagerService.ts
import { Context as Context2, Effect as Effect4, Layer as Layer2, pipe } from "effect";
import { Path as Path2 } from "@effect/platform";
import { NodePath } from "@effect/platform-node";
import { spawn as spawn2 } from "child_process";
import * as fs10 from "fs";
import { createLogger as createLogger5 } from "@moonwall/util";
var logger5 = createLogger5({ name: "ProcessManagerService" });
var ProcessManagerService = class extends Context2.Tag("ProcessManagerService")() {
};
var getLogPath = (config, pid) => pipe(
Effect4.all([Path2.Path, Effect4.sync(() => process.cwd())]),
Effect4.map(([pathService, cwd]) => {
const dirPath = config.logDirectory || pathService.join(cwd, "tmp", "node_logs");
const portArg = config.args.find((a) => a.includes("port"));
const port = portArg?.split("=")[1] || "unknown";
const baseName = pathService.basename(config.command);
return pathService.join(dirPath, `${baseName}_node_${port}_${pid}.log`).replace(/node_node_undefined/g, "chopsticks");
}),
Effect4.provide(NodePath.layer),
Effect4.mapError(
(cause) => new ProcessError({
cause,
operation: "spawn"
})
)
);
var ensureLogDirectory = (dirPath) => Effect4.tryPromise({
try: async () => {
try {
await fs10.promises.access(dirPath);
} catch {
await fs10.promises.mk