This repository was archived by the owner on Dec 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.cjs
166 lines (146 loc) · 4.72 KB
/
index.cjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
const { getInput, setFailed, addPath } = require("@actions/core");
const { exec } = require("@actions/exec");
const tc = require("@actions/tool-cache");
const { spawn } = require("child_process");
const { fetch } = require("ofetch");
const axios = require("axios");
const ERA_TEST_NODE_RELEASE_TAG = getInput("releaseTag") || "latest";
const ERA_TEST_NODE_ARCH = getInput("target") || "x86_64-unknown-linux-gnu";
async function getDownloadUrl() {
let apiUrl;
if (ERA_TEST_NODE_RELEASE_TAG === "latest") {
apiUrl =
"https://api.github.com/repos/matter-labs/era-test-node/releases/tags/v0.1.0-alpha.36";
} else {
apiUrl = `https://api.github.com/repos/matter-labs/era-test-node/releases/tags/${ERA_TEST_NODE_RELEASE_TAG}`;
}
const response = await fetch(apiUrl);
if (!response.ok) {
throw new Error(
`Failed to fetch release info for tag ${ERA_TEST_NODE_RELEASE_TAG}. era-test-node-action will only support latest (v0.1.0-alpha.36) in favour of anvil-zksync-action. HTTP Status: ${response.status}`
);
}
const releaseInfo = await response.json();
if (!releaseInfo || !releaseInfo.assets || !releaseInfo.assets.length) {
throw new Error(
`Release assets for tag ${ERA_TEST_NODE_RELEASE_TAG} are not available.`
);
}
const assetInfo = releaseInfo.assets.find((asset) =>
asset.name.includes(ERA_TEST_NODE_ARCH)
);
if (!assetInfo) {
throw new Error(`Asset with architecture ${ERA_TEST_NODE_ARCH} not found.`);
}
return assetInfo.browser_download_url;
}
async function run() {
try {
const mode = getInput("mode") || "run";
const network = getInput("network");
const forkAtHeight = getInput("forkAtHeight");
const port = getInput("port");
const showCalls = getInput("showCalls");
const showStorageLogs = getInput("showStorageLogs");
const showVmDetails = getInput("showVmDetails");
const showGasDetails = getInput("showGasDetails");
const resolveHashes = getInput("resolveHashes");
const log = getInput("log");
const logFilePath = getInput("logFilePath");
let toolPath = tc.find("era_test_node", ERA_TEST_NODE_RELEASE_TAG);
if (!toolPath) {
const downloadUrl = await getDownloadUrl();
const tarFile = await tc.downloadTool(downloadUrl);
const extractedDir = await tc.extractTar(tarFile);
toolPath = await tc.cacheDir(
extractedDir,
"era_test_node",
ERA_TEST_NODE_RELEASE_TAG
);
}
addPath(toolPath);
await exec("chmod", ["+x", `${toolPath}/era_test_node`]);
let args = [];
if (port) {
args.push("--port", port);
}
if (showCalls) {
args.push("--show-calls", showCalls);
}
if (showStorageLogs) {
args.push("--show-storage-logs", showStorageLogs);
}
if (showVmDetails) {
args.push("--show-vm-details", showVmDetails);
}
if (showGasDetails) {
args.push("--show-gas-details", showGasDetails);
}
if (resolveHashes === "true") {
args.push("--resolve-hashes");
}
if (log) {
args.push("--log", log);
}
if (logFilePath) {
args.push("--log-file-path", logFilePath);
}
if (mode === "fork") {
args.push("fork");
if (network) {
args.push(network);
}
if (forkAtHeight) {
args.push("--fork-at", forkAtHeight);
}
} else {
args.push("run");
}
console.log("Starting era_test_node with args:", args);
const child = spawn(`${toolPath}/era_test_node`, args, {
detached: true,
stdio: "ignore",
});
child.on("error", (error) => {
console.error(`Failed to start child process: ${error}`);
});
child.on("exit", (code, signal) => {
if (code) {
console.log(`Child process exited with code ${code}`);
} else if (signal) {
console.log(`Child process killed with signal ${signal}`);
} else {
console.log("Child process exited");
}
});
child.unref();
// sanity check
// Adding a timeout to give the node some time to start up before checking
setTimeout(async () => {
if (port && (await isNodeRunning(port))) {
console.log(`Confirmed: era_test_node is running on port ${port}`);
} else {
console.error(
"Health check failed: era_test_node appears to be not running."
);
setFailed("Failed to start era_test_node");
}
}, 5000);
} catch (error) {
setFailed(error.message);
}
}
run();
async function isNodeRunning(port) {
try {
const response = await axios.post(`http://localhost:${port}`, {
jsonrpc: "2.0",
id: 1,
method: "eth_blockNumber",
params: [],
});
return response.data && response.data.result !== undefined;
} catch (error) {
return false;
}
}