brainchain-worker-phala 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/agent.mjs +2 -1
- package/docker-compose.yml +1 -1
- package/npm-shrinkwrap.json +1276 -0
- package/package.json +23 -5
- package/start.mjs +42 -18
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "brainchain-worker-phala",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "Run a BrainChain worker in a Phala Cloud enclave: a key that never leaves the enclave, and every task answered through Phala's confidential AI with its enclave receipt checked.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "BrainChain",
|
|
@@ -11,11 +11,29 @@
|
|
|
11
11
|
"directory": "phala"
|
|
12
12
|
},
|
|
13
13
|
"bugs": "https://github.com/usebrainchain/brainchain-worker/issues",
|
|
14
|
-
"keywords": [
|
|
14
|
+
"keywords": [
|
|
15
|
+
"brainchain",
|
|
16
|
+
"phala",
|
|
17
|
+
"tee",
|
|
18
|
+
"dstack",
|
|
19
|
+
"agent",
|
|
20
|
+
"worker"
|
|
21
|
+
],
|
|
15
22
|
"type": "module",
|
|
16
|
-
"files": [
|
|
17
|
-
|
|
18
|
-
|
|
23
|
+
"files": [
|
|
24
|
+
"start.mjs",
|
|
25
|
+
"agent.mjs",
|
|
26
|
+
"lib.mjs",
|
|
27
|
+
"README.md",
|
|
28
|
+
"docker-compose.yml",
|
|
29
|
+
"npm-shrinkwrap.json"
|
|
30
|
+
],
|
|
31
|
+
"engines": {
|
|
32
|
+
"node": ">=22.12"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"test": "node --test lib-tests.mjs"
|
|
36
|
+
},
|
|
19
37
|
"dependencies": {
|
|
20
38
|
"@phala/aci-verifier": "0.11.1",
|
|
21
39
|
"@phala/dstack-sdk": "0.6.0",
|
package/start.mjs
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
// Runs a BrainChain worker inside a Phala Cloud CVM. The wallet key is derived by the enclave and handed to the worker
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
import { spawn } from "node:child_process";
|
|
6
|
-
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
|
|
3
|
+
// through a named pipe, so it is in no file, no environment and no log. On testnet it asks the gas drip for gas until
|
|
4
|
+
// it has had some, and it joins once; then it runs, doing each task it is hired for with agent.mjs.
|
|
5
|
+
import { execFileSync, spawn } from "node:child_process";
|
|
6
|
+
import { constants, existsSync, mkdirSync, openSync, closeSync, unlinkSync, writeFileSync } from "node:fs";
|
|
7
|
+
import { open } from "node:fs/promises";
|
|
7
8
|
import { createRequire } from "node:module";
|
|
8
9
|
import { homedir } from "node:os";
|
|
9
10
|
import { dirname, join } from "node:path";
|
|
@@ -44,32 +45,54 @@ const state = join(homedir(), ".brainchain-worker-phala");
|
|
|
44
45
|
mkdirSync(state, { recursive: true, mode: 0o700 });
|
|
45
46
|
const marker = (name) => join(state, `${name}-${s.network}-${address.toLowerCase()}`);
|
|
46
47
|
|
|
47
|
-
// The worker reads the key from
|
|
48
|
+
// The worker reads the key from a named pipe and keeps it out of the command it runs. Node gives a child a socket, not
|
|
49
|
+
// a pipe, for its stdin, and a socket cannot be opened by path, so the key goes through a FIFO only the worker reads.
|
|
50
|
+
let pipes = 0;
|
|
48
51
|
function run(args) {
|
|
52
|
+
const fifo = join(existsSync("/dev/shm") ? "/dev/shm" : state, `key-${process.pid}-${(pipes += 1)}`);
|
|
53
|
+
execFileSync("mkfifo", ["-m", "600", fifo]);
|
|
49
54
|
const child = spawn(process.execPath, [worker, ...args], {
|
|
50
|
-
stdio: ["
|
|
51
|
-
env: { ...process.env, BRAINCHAIN_NETWORK: s.network, BRAINCHAIN_WORKER_KEY_FILE:
|
|
55
|
+
stdio: ["ignore", "inherit", "inherit"],
|
|
56
|
+
env: { ...process.env, BRAINCHAIN_NETWORK: s.network, BRAINCHAIN_WORKER_KEY_FILE: fifo },
|
|
57
|
+
});
|
|
58
|
+
// A stop forwarded to the worker is a clean end, not a failure for the container's restart policy.
|
|
59
|
+
const exited = new Promise((resolve) => child.on("exit", (code, signal) => resolve(code ?? (signal === "SIGTERM" || signal === "SIGINT" ? 0 : 1))));
|
|
60
|
+
// Opening the write end waits for the worker to open the read end. A worker that exits first never will, so the
|
|
61
|
+
// read end is opened here, which lets the write go into a pipe nobody reads, and the FIFO is removed either way.
|
|
62
|
+
const handed = open(fifo, "w").then(async (fd) => {
|
|
63
|
+
await fd.writeFile(`${key}\n`).catch(() => {});
|
|
64
|
+
await fd.close();
|
|
65
|
+
});
|
|
66
|
+
exited.then(() => {
|
|
67
|
+
try {
|
|
68
|
+
closeSync(openSync(fifo, constants.O_RDONLY | constants.O_NONBLOCK));
|
|
69
|
+
} catch {
|
|
70
|
+
// Gone already: the key was handed over and the FIFO removed.
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
handed.finally(() => {
|
|
74
|
+
try {
|
|
75
|
+
unlinkSync(fifo);
|
|
76
|
+
} catch {
|
|
77
|
+
// Removed by the other path.
|
|
78
|
+
}
|
|
52
79
|
});
|
|
53
|
-
child.stdin.end(`${key}\n`);
|
|
54
80
|
const forward = (signal) => child.kill(signal);
|
|
55
81
|
process.on("SIGTERM", forward).on("SIGINT", forward);
|
|
56
|
-
return
|
|
57
|
-
child.on("exit", (code, signal) => {
|
|
58
|
-
process.off("SIGTERM", forward).off("SIGINT", forward);
|
|
59
|
-
resolve(code ?? (signal ? 1 : 0));
|
|
60
|
-
});
|
|
61
|
-
});
|
|
82
|
+
return exited.finally(() => process.off("SIGTERM", forward).off("SIGINT", forward));
|
|
62
83
|
}
|
|
63
84
|
|
|
64
|
-
// The drip serves each address once,
|
|
65
|
-
|
|
85
|
+
// The drip serves each address once: a 200 sent it, a 409 says it was sent before or the address holds enough. Anything
|
|
86
|
+
// else, such as a paused drip, is asked again before the next try at joining.
|
|
87
|
+
async function drip() {
|
|
88
|
+
if (s.network !== "testnet" || existsSync(marker("drip"))) return;
|
|
66
89
|
const response = await fetch("https://brain.testnet.usebrainchain.com/testnet/gas", {
|
|
67
90
|
method: "POST",
|
|
68
91
|
headers: { "content-type": "application/json" },
|
|
69
92
|
body: JSON.stringify({ address }),
|
|
70
|
-
}).catch((error) => ({ status:
|
|
93
|
+
}).catch((error) => ({ status: 0, text: async () => error.message }));
|
|
71
94
|
console.log(`Testnet gas drip: ${response.status} ${(await response.text()).slice(0, 200)}`);
|
|
72
|
-
writeFileSync(marker("drip"), new Date().toISOString());
|
|
95
|
+
if (response.status === 200 || response.status === 409) writeFileSync(marker("drip"), new Date().toISOString());
|
|
73
96
|
}
|
|
74
97
|
|
|
75
98
|
// At most once per amount: running it again after a partial move could buy the stake twice.
|
|
@@ -82,6 +105,7 @@ if (s.fundUsdc && !existsSync(marker(`fund-${s.fundUsdc}`))) {
|
|
|
82
105
|
|
|
83
106
|
// Joined once. A worker BrainChain refused is not sent in again by a restart: on mainnet that would forfeit another bond.
|
|
84
107
|
while (!existsSync(marker("joined"))) {
|
|
108
|
+
await drip();
|
|
85
109
|
const code = await run(joinArgs(s));
|
|
86
110
|
if (code === 0) {
|
|
87
111
|
writeFileSync(marker("joined"), new Date().toISOString());
|