Skip to content

Commit

Permalink
zombienet two-way IPC communication
Browse files Browse the repository at this point in the history
  • Loading branch information
timbrinded committed Nov 6, 2023
1 parent 273f8a1 commit b71fe1c
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 310 deletions.
8 changes: 4 additions & 4 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,16 @@
"@polkadot/types-codec": "^10.10.1",
"@polkadot/util": "^12.5.1",
"@polkadot/util-crypto": "^12.5.1",
"@zombienet/orchestrator": "^0.0.59",
"@zombienet/utils": "^0.0.23",
"@zombienet/orchestrator": "^0.0.62",
"@zombienet/utils": "^0.0.24",
"bottleneck": "^2.19.5",
"chalk": "^5.3.0",
"clear": "^0.1.0",
"cli-progress": "^3.12.0",
"colors": "^1.4.0",
"debug": "^4.3.4",
"dotenv": "^16.3.1",
"ethers": "^6.8.0",
"ethers": "^6.8.1",
"execa": "^8.0.1",
"inquirer": "^9.2.11",
"inquirer-press-to-continue": "^1.2.0",
Expand All @@ -101,7 +101,7 @@
"devDependencies": {
"@types/cli-progress": "^3.11.4",
"@types/debug": "^4.1.10",
"@types/node": "^20.8.9",
"@types/node": "^20.8.10",
"@types/yargs": "^17.0.29",
"prettier": "^2.8.8",
"tsup": "^7.2.0",
Expand Down
63 changes: 49 additions & 14 deletions packages/cli/src/lib/globalContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import zombie, { Network } from "@zombienet/orchestrator";
import { execaCommand, execaCommandSync } from "execa";
import Debug from "debug";
import fs from "fs";
import net from "net";
import readline from "readline";
import { setTimeout as timer } from "timers/promises";
import { parseChopsticksRunCmd, parseRunCmd, parseZombieCmd } from "../internal/commandParsers";
Expand All @@ -38,6 +39,7 @@ export class MoonwallContext {
foundation: FoundationType;
zombieNetwork?: Network;
rtUpgradePath?: string;
ipcServer?: net.Server;

constructor(config: MoonwallConfig) {
const env = config.environments.find(({ name }) => name == process.env.MOON_TEST_ENV)!;
Expand Down Expand Up @@ -187,6 +189,47 @@ export class MoonwallContext {
}
};

const socketPath = `${network.tmpDir}/node-ipc.sock`;

const server = net.createServer((client) => {
client.on("end", () => {
console.log("📨 IPC client disconnected");
});

client.on("data", async (data) => {
try {
const message = JSON.parse(data.toString());

if (message.cmd === "restart") {
console.log(`Restarting ${message.node}, please wait...`);
await network.client.restartNode(message.node, 0);
console.log(`✅ Node: "${message.node}" restarted`);

// change the process map to use the new PID
await this.disconnect();
await this.connectEnvironment(true);

if (client.writable) {
client.write(
JSON.stringify({ status: "success", message: `${message.node} restarted` })
);
} else {
console.log("Client disconnected, cannot send response.");
}
}
} catch (e) {
console.log("📨 Message from client:", data.toString());
}
});
});

server.listen(socketPath, () => {
console.log("📨 IPC Server listening on", socketPath);
});

this.ipcServer = server;
process.env.MOON_IPC_SOCKET = socketPath;

process.once("exit", onProcessExit);
process.once("SIGINT", onProcessExit);

Expand Down Expand Up @@ -225,7 +268,7 @@ export class MoonwallContext {
return MoonwallContext.getContext();
}

public async connectEnvironment(): Promise<MoonwallContext> {
public async connectEnvironment(silent: boolean = false): Promise<MoonwallContext> {
const config = await importAsyncConfig();
const env = config.environments.find(({ name }) => name == process.env.MOON_TEST_ENV)!;

Expand Down Expand Up @@ -253,7 +296,7 @@ export class MoonwallContext {
if (this.foundation == "zombie") {
let readStreams: any[];
if (!isOptionSet("disableLogEavesdropping")) {
console.log(`🦻 Eavesdropping on node logs at ${process.env.MOON_ZOMBIE_DIR}`);
!silent && console.log(`🦻 Eavesdropping on node logs at ${process.env.MOON_ZOMBIE_DIR}`);
const zombieNodeLogs = process.env
.MOON_ZOMBIE_NODES!.split("|")
.map((nodeName) => `${process.env.MOON_ZOMBIE_DIR}/${nodeName}.log`);
Expand Down Expand Up @@ -283,15 +326,15 @@ export class MoonwallContext {
)
.map(async (provider) => {
return await new Promise(async (resolve) => {
console.log(`⏲️ Waiting for chain ${provider.name} to produce blocks...`);
!silent && console.log(`⏲️ Waiting for chain ${provider.name} to produce blocks...`);
while (
(
await (provider.api as ApiPromise).rpc.chain.getBlock()
).block.header.number.toNumber() == 0
) {
await timer(500);
}
console.log(`✅ Chain ${provider.name} producing blocks, continuing`);
!silent && console.log(`✅ Chain ${provider.name} producing blocks, continuing`);
resolve("");
});
});
Expand Down Expand Up @@ -337,16 +380,6 @@ export class MoonwallContext {
return MoonwallContext.instance;
}

private killAllChildProcesses() {
this.nodes.forEach((child) => {
try {
child.kill();
} catch (err) {
console.error("Failed to kill child process", err);
}
});
}

public static async destroy() {
const ctx = this.instance;

Expand All @@ -372,6 +405,8 @@ export class MoonwallContext {
if (ctx.zombieNetwork) {
console.log("🪓 Killing zombie nodes");
await ctx.zombieNetwork.stop();
ctx.ipcServer?.close();
ctx.ipcServer?.removeAllListeners();
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@
"@polkadot/util": "^12.5.1",
"@polkadot/util-crypto": "^12.5.1",
"@types/node": "^20.8.9",
"@zombienet/utils": "^0.0.22",
"@zombienet/utils": "^0.0.24",
"bottleneck": "^2.19.5",
"debug": "^4.3.4",
"ethers": "^6.8.0",
"ethers": "^6.8.1",
"viem": "^1.18.0",
"web3": "4.2.1"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"colors": "^1.4.0",
"debug": "^4.3.4",
"dotenv": "^16.3.1",
"ethers": "^6.8.0",
"ethers": "^6.8.1",
"inquirer": "^9.2.11",
"inquirer-press-to-continue": "^1.2.0",
"node-fetch": "^3.3.2",
Expand Down
Loading

0 comments on commit b71fe1c

Please sign in to comment.