@parity/ppn 0.9.0 → 0.10.1

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.
@@ -0,0 +1,121 @@
1
+ // The network `ppn start` runs is one process group: zombie-cli is spawned as its leader, and
2
+ // everything zombienet starts (nodes, custom processes, their launchers) is in that group unless
3
+ // it makes a group of its own. The group is recorded beside the network's data, so `ppn kill`
4
+ // stops exactly it, and a later `ppn start` of the same workspace can stop one a dead `ppn`
5
+ // left behind. Nothing is matched by name or by port.
6
+ import fs from 'node:fs';
7
+ import path from 'node:path';
8
+ import { spawnSync } from 'node:child_process';
9
+ /** Beside the network's data. */
10
+ export const GROUP_FILE = 'ppn-process-group.json';
11
+ /** The workspace's index of recorded groups, one file per network, so `ppn kill` finds them all. */
12
+ export const groupIndexDir = (workspace) => path.join(workspace, 'run');
13
+ export const groupIndexFile = (workspace, network) => path.join(groupIndexDir(workspace), `${network}.json`);
14
+ function psLines(args) {
15
+ const out = spawnSync('ps', args, { encoding: 'utf-8' });
16
+ return (out.stdout ?? '').split('\n').filter((line) => line.trim());
17
+ }
18
+ export const systemProcesses = {
19
+ members(pgid) {
20
+ // A zombie (state Z: exited, not yet reaped by its parent) is not a member to stop, and
21
+ // signalling a group of zombies only fails with EPERM on macOS.
22
+ return psLines(['-axo', 'pid=,pgid=,stat=,command=']).flatMap((line) => {
23
+ const m = /^\s*(\d+)\s+(\d+)\s+(\S+)\s+(.*)$/.exec(line);
24
+ return m && Number(m[2]) === pgid && !m[3].startsWith('Z') ? [{ pid: Number(m[1]), command: m[4] }] : [];
25
+ });
26
+ },
27
+ startOf(pid) {
28
+ const [line] = psLines(['-o', 'lstart=', '-p', String(pid)]);
29
+ return line?.trim() || undefined;
30
+ },
31
+ signalGroup(pgid, signal) {
32
+ try {
33
+ process.kill(-pgid, signal);
34
+ }
35
+ catch (err) {
36
+ if (err.code !== 'ESRCH')
37
+ throw err;
38
+ }
39
+ },
40
+ sleep(ms) {
41
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
42
+ },
43
+ };
44
+ /**
45
+ * Whether a recorded group is still this network's. `gone`: nothing is in it. `ours`: its leader
46
+ * is the recorded one (same start time), or, the leader having exited, a member runs this
47
+ * workspace's or this package's files (POSIX does not reuse a group id while the group lives).
48
+ * `foreign`: the id now belongs to something else, which is left alone.
49
+ */
50
+ export function classifyGroup(record, procs, ours) {
51
+ const members = procs.members(record.pgid);
52
+ if (!members.length)
53
+ return 'gone';
54
+ if (members.some((m) => m.pid === record.pgid)) {
55
+ return procs.startOf(record.pgid) === record.leaderStart ? 'ours' : 'foreign';
56
+ }
57
+ return members.some((m) => ours(m.command)) ? 'ours' : 'foreign';
58
+ }
59
+ /**
60
+ * Stop a group: SIGTERM to all of it (zombie-cli tears its nodes down, Postgres releases its
61
+ * shared memory), then SIGKILL to whatever is left after `graceMs`.
62
+ */
63
+ export function stopGroup(pgid, procs, { graceMs = 15_000, pollMs = 250, first = 'SIGTERM' } = {}) {
64
+ const members = procs.members(pgid).length;
65
+ if (!members)
66
+ return { members: 0, forced: false };
67
+ procs.signalGroup(pgid, first);
68
+ for (let waited = 0; waited < graceMs; waited += pollMs) {
69
+ if (!procs.members(pgid).length)
70
+ return { members, forced: false };
71
+ procs.sleep(pollMs);
72
+ }
73
+ procs.signalGroup(pgid, 'SIGKILL');
74
+ for (let waited = 0; waited < 5_000 && procs.members(pgid).length; waited += pollMs)
75
+ procs.sleep(pollMs);
76
+ return { members, forced: true };
77
+ }
78
+ export function writeGroupRecord(record) {
79
+ const text = `${JSON.stringify(record, null, 2)}\n`;
80
+ if (record.dataDir)
81
+ fs.writeFileSync(path.join(record.dataDir, GROUP_FILE), text);
82
+ fs.mkdirSync(groupIndexDir(record.workspace), { recursive: true });
83
+ fs.writeFileSync(groupIndexFile(record.workspace, record.network), text);
84
+ }
85
+ export function removeGroupRecord(record) {
86
+ const index = groupIndexFile(record.workspace, record.network);
87
+ // Only the record of this group: a later start of the same network may have replaced it.
88
+ if (readRecord(index)?.pgid === record.pgid)
89
+ fs.rmSync(index, { force: true });
90
+ if (record.dataDir) {
91
+ const beside = path.join(record.dataDir, GROUP_FILE);
92
+ if (readRecord(beside)?.pgid === record.pgid)
93
+ fs.rmSync(beside, { force: true });
94
+ }
95
+ }
96
+ function readRecord(file) {
97
+ try {
98
+ const value = JSON.parse(fs.readFileSync(file, 'utf-8'));
99
+ return value?.version === 1 && Number.isInteger(value.pgid) && value.pgid > 1 ? value : undefined;
100
+ }
101
+ catch {
102
+ // Absent or unreadable: no record to act on.
103
+ return undefined;
104
+ }
105
+ }
106
+ /** The groups this workspace has recorded. */
107
+ export function recordedGroups(workspace) {
108
+ const dir = groupIndexDir(workspace);
109
+ if (!fs.existsSync(dir))
110
+ return [];
111
+ return fs
112
+ .readdirSync(dir)
113
+ .filter((file) => file.endsWith('.json'))
114
+ .flatMap((file) => readRecord(path.join(dir, file)) ?? []);
115
+ }
116
+ /** A process runs this workspace's or this package's files (the same anchors as the sweep). */
117
+ export function runsFrom(roots) {
118
+ const prefixes = roots.map((root) => `${path.resolve(root)}${path.sep}`);
119
+ return (command) => prefixes.some((prefix) => command.includes(prefix));
120
+ }
121
+ //# sourceMappingURL=process-group.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"process-group.js","sourceRoot":"","sources":["../../src/lib/process-group.ts"],"names":[],"mappings":"AAAA,8FAA8F;AAC9F,iGAAiG;AACjG,8FAA8F;AAC9F,4FAA4F;AAC5F,sDAAsD;AAEtD,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAkB/C,iCAAiC;AACjC,MAAM,CAAC,MAAM,UAAU,GAAG,wBAAwB,CAAC;AAEnD,oGAAoG;AACpG,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,SAAiB,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAA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CAAC;IAAC,MAAM,CAAC;QACP,6CAA6C;QAC7C,OAAO,SAAS,CAAC;IACnB,CAAC;AACH,CAAC;AAED,8CAA8C;AAC9C,MAAM,UAAU,cAAc,CAAC,SAAiB;IAC9C,MAAM,GAAG,GAAG,aAAa,CAAC,SAAS,CAAC,CAAC;IACrC,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,GAAG,CAAC;QAAE,OAAO,EAAE,CAAC;IACnC,OAAO,EAAE;SACN,WAAW,CAAC,GAAG,CAAC;SAChB,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;SACxC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC;AAC/D,CAAC;AAED,+FAA+F;AAC/F,MAAM,UAAU,QAAQ,CAAC,KAAwB;IAC/C,MAAM,QAAQ,GAAG,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,GAAG,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC;IACzE,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC;AAC1E,CAAC"}
@@ -0,0 +1,12 @@
1
+ export type Section = 'server' | 'deployable' | 'optional';
2
+ /**
3
+ * The example this CLI ships. Searched for by the file itself, not the usual config/ports.env
4
+ * marker: the network-config package carries a config/ports.env of its own.
5
+ */
6
+ export declare function exampleFile(): string;
7
+ /** The keys of each section of the example, in file order. */
8
+ export declare function secretSections(file?: string): Record<Section, string[]>;
9
+ /** The keys a file must hold, non-empty, under `profile`. */
10
+ export declare function requiredSecrets(profile: string): string[];
11
+ /** What is wrong with a secrets file's contents; empty when nothing is. */
12
+ export declare function checkSecrets(values: Record<string, string>): string[];
@@ -0,0 +1,75 @@
1
+ // config/secrets.env.example is the list of keys a secrets file may hold, in three sections:
2
+ // every server, deployable servers and optional. Everything here reads that file; nothing
3
+ // lists keys itself.
4
+ import fs from 'node:fs';
5
+ import path from 'node:path';
6
+ const HEADINGS = {
7
+ 'every server': 'server',
8
+ 'deployable servers': 'deployable',
9
+ optional: 'optional',
10
+ };
11
+ const EXAMPLE = path.join('config', 'secrets.env.example');
12
+ /**
13
+ * The example this CLI ships. Searched for by the file itself, not the usual config/ports.env
14
+ * marker: the network-config package carries a config/ports.env of its own.
15
+ */
16
+ export function exampleFile() {
17
+ if (process.env.PPN_PACKAGE_ROOT)
18
+ return path.join(process.env.PPN_PACKAGE_ROOT, EXAMPLE);
19
+ for (let dir = import.meta.dirname;; dir = path.dirname(dir)) {
20
+ if (fs.existsSync(path.join(dir, EXAMPLE)))
21
+ return path.join(dir, EXAMPLE);
22
+ if (path.dirname(dir) === dir)
23
+ throw new Error(`${EXAMPLE} not found above ${import.meta.dirname}`);
24
+ }
25
+ }
26
+ /** The keys of each section of the example, in file order. */
27
+ export function secretSections(file = exampleFile()) {
28
+ const sections = { server: [], deployable: [], optional: [] };
29
+ let current = null;
30
+ for (const line of fs.readFileSync(file, 'utf-8').split('\n')) {
31
+ const heading = line.match(/^# ---- (.+) ----$/);
32
+ if (heading) {
33
+ current = HEADINGS[heading[1]];
34
+ if (!current)
35
+ throw new Error(`${file}: unknown section "${heading[1]}"`);
36
+ continue;
37
+ }
38
+ const key = line.match(/^([A-Z_][A-Z0-9_]*)=/)?.[1];
39
+ if (!key)
40
+ continue;
41
+ if (!current)
42
+ throw new Error(`${file}: ${key} is outside a section`);
43
+ sections[current].push(key);
44
+ }
45
+ return sections;
46
+ }
47
+ /** The keys a file must hold, non-empty, under `profile`. */
48
+ export function requiredSecrets(profile) {
49
+ const s = secretSections();
50
+ return profile === 'deployable' ? [...s.server, ...s.deployable] : s.server;
51
+ }
52
+ /** What is wrong with a secrets file's contents; empty when nothing is. */
53
+ export function checkSecrets(values) {
54
+ const s = secretSections();
55
+ const profile = values['PPN_PROFILE'];
56
+ if (profile !== 'local' && profile !== 'deployable') {
57
+ return [`PPN_PROFILE: must be local or deployable, not "${profile ?? ''}"`];
58
+ }
59
+ const required = requiredSecrets(profile);
60
+ const allowed = new Set([...required, ...s.optional]);
61
+ const problems = [];
62
+ for (const key of required) {
63
+ if (!values[key])
64
+ problems.push(`${key}: missing`);
65
+ }
66
+ for (const key of Object.keys(values)) {
67
+ if (allowed.has(key))
68
+ continue;
69
+ problems.push(s.deployable.includes(key)
70
+ ? `${key}: deployable only, and this file is local`
71
+ : `${key}: not in config/secrets.env.example`);
72
+ }
73
+ return problems;
74
+ }
75
+ //# sourceMappingURL=secrets-example.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"secrets-example.js","sourceRoot":"","sources":["../../src/lib/secrets-example.ts"],"names":[],"mappings":"AAAA,6FAA6F;AAC7F,0FAA0F;AAC1F,qBAAqB;AAErB,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAI7B,MAAM,QAAQ,GAA4B;IACxC,cAAc,EAAE,QAAQ;IACxB,oBAAoB,EAAE,YAAY;IAClC,QAAQ,EAAE,UAAU;CACrB,CAAC;AAEF,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,qBAAqB,CAAC,CAAC;AAE3D;;;GAGG;AACH,MAAM,UAAU,WAAW;IACzB,IAAI,OAAO,CAAC,GAAG,CAAC,gBAAgB;QAAE,OAAO,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,gBAAgB,EAAE,OAAO,CAAC,CAAC;IAC1F,KAAK,IAAI,GAAG,GAAG,MAAM,CAAC,IAAI,CAAC,OAAO,GAAI,GAAG,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAA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,QAAQ,CAAC,GAAG,CAAC;YACxB,CAAC,CAAC,GAAG,GAAG,2CAA2C;YACnD,CAAC,CAAC,GAAG,GAAG,qCAAqC,CAChD,CAAC;IACJ,CAAC;IACD,OAAO,QAAQ,CAAC;AAClB,CAAC"}
@@ -20,6 +20,28 @@ export interface ChildEnvFacts {
20
20
  ephemeral: boolean;
21
21
  }
22
22
  export declare function childEnv(f: ChildEnvFacts): Record<string, string>;
23
+ /**
24
+ * Facts every custom process is handed in its own `env` (custom-process-env): zombie-cli starts
25
+ * nodes and custom processes with an empty environment but TZ, LANG, PATH and what the TOML
26
+ * gives each one, so neither zombie-cli's environment (childEnv) nor the shell's reaches them.
27
+ * Without PPN_HOME a launcher falls back to the checkout it sits in, or ~/.ppn
28
+ * (scripts/lib/workspace.sh), and reads that workspace's ports.local.env, binaries and data
29
+ * instead of this run's.
30
+ */
31
+ export declare function customProcessEnv(f: {
32
+ workspace: string;
33
+ network: string;
34
+ dataDir: string;
35
+ }): Record<string, string>;
36
+ /**
37
+ * `toml` with `env` added to every `[[custom_processes]]` block, before any entry the block
38
+ * already has; an entry the block names itself is kept, not repeated. Other sections are left
39
+ * alone. Line-based, for the configs this repository generates and any hand-written one in the
40
+ * same shape: one key per line, `env = [` opening its array on the key's own line.
41
+ */
42
+ export declare function withCustomProcessEnv(toml: string, env: Record<string, string>): string;
43
+ /** Where `ppn start` writes the config it spawns: beside the one it was made from, so relative paths still resolve. */
44
+ export declare function spawnTomlFor(source: string): string;
23
45
  /** Keys `ppn start` writes into config/ports.local.env, derived from the real writer. */
24
46
  export declare function localEnvKeys(): string[];
25
47
  /** Keys `ppn start` puts into zombie-cli's environment, derived from the real builder. */
@@ -2,14 +2,16 @@
2
2
  //
3
3
  // zombienet forwards no environment to custom processes, so everything the dashboard,
4
4
  // eth-rpc, the dub stack and the storage provider cannot derive has to be *stated*.
5
- // `ppn start` states it twice: config/ports.local.env (read by custom processes) and
6
- // zombie-cli's child environment (inherited by node-level processes). A deployment that
7
- // spawns zombie-cli directly never runs `ppn start`, so it has to restate the same facts
8
- // itself.
5
+ // `ppn start` states it three times: config/ports.local.env (read by custom processes),
6
+ // zombie-cli's child environment (which the configs' {{BIN}} and {{SCRIPTS}} are filled from),
7
+ // and each custom process's own `env` in the config it spawns (customProcessEnv), the only
8
+ // environment zombie-cli hands them. A deployment that spawns zombie-cli directly never runs
9
+ // `ppn start`, so it has to restate the same facts itself.
9
10
  //
10
11
  // This module is the canonical statement, and the contract a deployment implements: it is
11
12
  // exported as `@parity/ppn/spawn-env` so the restating side can assert against the key lists
12
13
  // rather than keeping its own copy in step by hand.
14
+ import path from 'node:path';
13
15
  /** The gitignored file `ppn start` writes; every service overlays it on top of ports.env. */
14
16
  export function localEnvContent(f) {
15
17
  return ('# Generated by `ppn start` — gitignored, do not edit.\n' +
@@ -32,6 +34,71 @@ export function childEnv(f) {
32
34
  EPHEMERAL: f.ephemeral ? '1' : '',
33
35
  };
34
36
  }
37
+ /**
38
+ * Facts every custom process is handed in its own `env` (custom-process-env): zombie-cli starts
39
+ * nodes and custom processes with an empty environment but TZ, LANG, PATH and what the TOML
40
+ * gives each one, so neither zombie-cli's environment (childEnv) nor the shell's reaches them.
41
+ * Without PPN_HOME a launcher falls back to the checkout it sits in, or ~/.ppn
42
+ * (scripts/lib/workspace.sh), and reads that workspace's ports.local.env, binaries and data
43
+ * instead of this run's.
44
+ */
45
+ export function customProcessEnv(f) {
46
+ return { PPN_HOME: f.workspace, PPN_NETWORK: f.network, PPN_DATA_DIR: f.dataDir };
47
+ }
48
+ const SECTION = /^\s*\[\[?\s*([\w.-]+)\s*\]\]?\s*(#.*)?$/;
49
+ /**
50
+ * `toml` with `env` added to every `[[custom_processes]]` block, before any entry the block
51
+ * already has; an entry the block names itself is kept, not repeated. Other sections are left
52
+ * alone. Line-based, for the configs this repository generates and any hand-written one in the
53
+ * same shape: one key per line, `env = [` opening its array on the key's own line.
54
+ */
55
+ export function withCustomProcessEnv(toml, env) {
56
+ const lines = toml.split('\n');
57
+ const out = [];
58
+ const entry = (name, value) => `{ name = ${JSON.stringify(name)}, value = ${JSON.stringify(value)} }`;
59
+ let i = 0;
60
+ while (i < lines.length) {
61
+ const header = SECTION.exec(lines[i]);
62
+ out.push(lines[i++]);
63
+ if (!header || header[1] !== 'custom_processes' || !lines[i - 1].trim().startsWith('[['))
64
+ continue;
65
+ // The block runs to the next section header.
66
+ const start = i;
67
+ while (i < lines.length && !SECTION.test(lines[i]))
68
+ i++;
69
+ const block = lines.slice(start, i);
70
+ const named = new Set([...block.join('\n').matchAll(/\{\s*name\s*=\s*"([^"]+)"/g)].map((m) => m[1]));
71
+ const missing = Object.entries(env).filter(([name]) => !named.has(name)).map(([n, v]) => entry(n, v));
72
+ const at = block.findIndex((line) => /^\s*env\s*=\s*\[/.test(line));
73
+ if (missing.length === 0) {
74
+ out.push(...block);
75
+ }
76
+ else if (at === -1) {
77
+ // No env yet: after the block's last key, before the blank lines that separate it.
78
+ let last = block.length;
79
+ while (last > 0 && block[last - 1].trim() === '')
80
+ last--;
81
+ out.push(...block.slice(0, last), `env = [${missing.join(', ')}]`, ...block.slice(last));
82
+ }
83
+ else {
84
+ const open = block[at].indexOf('[') + 1;
85
+ const rest = block[at].slice(open).trim();
86
+ if (rest === '') {
87
+ // Multi-line array: one entry per line, as the generators write them.
88
+ out.push(...block.slice(0, at + 1), ...missing.map((e) => ` ${e},`), ...block.slice(at + 1));
89
+ }
90
+ else {
91
+ const joined = rest.startsWith(']') ? missing.join(', ') : `${missing.join(', ')}, `;
92
+ out.push(...block.slice(0, at), `${block[at].slice(0, open)}${joined}${rest}`, ...block.slice(at + 1));
93
+ }
94
+ }
95
+ }
96
+ return out.join('\n');
97
+ }
98
+ /** Where `ppn start` writes the config it spawns: beside the one it was made from, so relative paths still resolve. */
99
+ export function spawnTomlFor(source) {
100
+ return path.join(path.dirname(source), `${path.basename(source, '.toml')}.spawn.toml`);
101
+ }
35
102
  const DUMMY_LOCAL = { network: 'x', dataDir: 'x', ephemeral: false, identityDataDir: 'x' };
36
103
  const DUMMY_CHILD = { binDir: 'x', workspace: 'x', scriptsDir: 'x', dataDir: 'x', ephemeral: false };
37
104
  /** Keys `ppn start` writes into config/ports.local.env, derived from the real writer. */
@@ -1 +1 @@
1
- {"version":3,"file":"spawn-env.js","sourceRoot":"","sources":["../../src/lib/spawn-env.ts"],"names":[],"mappings":"AAAA,0DAA0D;AAC1D,EAAE;AACF,sFAAsF;AACtF,oFAAoF;AACpF,qFAAqF;AACrF,wFAAwF;AACxF,yFAAyF;AACzF,UAAU;AACV,EAAE;AACF,0FAA0F;AAC1F,6FAA6F;AAC7F,oDAAoD;AAapD,6FAA6F;AAC7F,MAAM,UAAU,eAAe,CAAC,CAAgB;IAC9C,OAAO,CACL,yDAAyD;QACzD,sCAAsC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,MAAM,IAAI;QAC3E,yBAAyB,CAAC,CAAC,eAAe,IAAI;QAC9C,eAAe,CAAC,CAAC,OAAO,IAAI;QAC5B,8EAA8E;QAC9E,6EAA6E;QAC7E,yEAAyE;QACzE,gBAAgB,CAAC,CAAC,OAAO,IAAI,CAC9B,CAAC;AACJ,CAAC;AAYD,MAAM,UAAU,QAAQ,CAAC,CAAgB;IACvC,OAAO;QACL,GAAG,EAAE,CAAC,CAAC,MAAM;QACb,qFAAqF;QACrF,oFAAoF;QACpF,QAAQ,EAAE,CAAC,CAAC,SAAS;QACrB,OAAO,EAAE,CAAC,CAAC,UAAU;QACrB,QAAQ,EAAE,CAAC,CAAC,OAAO;QACnB,SAAS,EAAE,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE;KAClC,CAAC;AACJ,CAAC;AAED,MAAM,WAAW,GAAkB,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,SAAS,EAAE,KAAK,EAAE,eAAe,EAAE,GAAG,EAAE,CAAC;AAC1G,MAAM,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AC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAC/F,CAAC;AAED,0FAA0F;AAC1F,MAAM,UAAU,YAAY;IAC1B,OAAO,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC,CAAC;AAC5C,CAAC;AAED,oEAAoE;AACpE,MAAM,UAAU,YAAY;IAC1B,OAAO,CAAC,GAAG,IAAI,GAAG,CAAC,CAAC,GAAG,YAAY,EAAE,EAAE,GAAG,YAAY,EAAE,CAAC,CAAC,CAAC,CAAC;AAC9D,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,MAAM,MAAM,GAA2B;IAC5C,uFAAuF;IACvF,SAAS,EAAE,8DAA8D;IAEzE,gEAAgE;IAChE,EAAE;IACF,gFAAgF;IAChF,yFAAyF;IACzF,uFAAuF;IACvF,wFAAwF;IACxF,8EAA8E;IAC9E,EAAE;IACF,2FAA2F;IAC3F,2FAA2F;IAC3F,oFAAoF;IACpF,aAAa;IACb,WAAW,EAAE,sEAAsE;CACpF,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@parity/ppn",
3
- "version": "0.9.0",
3
+ "version": "0.10.1",
4
4
  "type": "module",
5
5
  "description": "Spin up a local Polkadot ecosystem: fetch, generate, fork and run a network",
6
6
  "bin": {
@@ -8,7 +8,8 @@
8
8
  },
9
9
  "exports": {
10
10
  ".": "./dist/cli.js",
11
- "./spawn-env": "./dist/lib/spawn-env.js"
11
+ "./spawn-env": "./dist/lib/spawn-env.js",
12
+ "./secrets": "./dist/lib/secrets-example.js"
12
13
  },
13
14
  "dependencies": {
14
15
  "@polkadot-labs/hdkd-helpers": "^0.0.11",
@@ -20,7 +21,7 @@
20
21
  "commander": "^15.0.0",
21
22
  "polkadot-api": "^1.23.3",
22
23
  "toml": "^4.2.0",
23
- "@parity/ppn-network-config": "0.9.0"
24
+ "@parity/ppn-network-config": "0.10.1"
24
25
  },
25
26
  "devDependencies": {
26
27
  "@types/node": "^24.0.0",
@@ -49,6 +50,6 @@
49
50
  },
50
51
  "scripts": {
51
52
  "build": "tsc -b",
52
- "test": "tsx --test tests/*.test.ts"
53
+ "test": "PPN_PACKAGE_ROOT=../.. tsx --test tests/*.test.ts"
53
54
  }
54
55
  }
@@ -43,7 +43,7 @@ if [[ ! -x "$PG_DIST/bin/postgres" ]]; then
43
43
  exit 1
44
44
  fi
45
45
 
46
- "$SCRIPT_DIR/../kill-port.sh" "$DUB_POSTGRES_PORT"
46
+ "$SCRIPT_DIR/../require-free-ports.sh" "$DUB_POSTGRES_PORT" || exit 1
47
47
 
48
48
  # initdb once; on restart the existing cluster is reused.
49
49
  if [[ ! -f "$PGDATA/PG_VERSION" ]]; then
@@ -67,6 +67,9 @@ PG_OPTS=(
67
67
  -p "$DUB_POSTGRES_PORT"
68
68
  -c listen_addresses=127.0.0.1
69
69
  -c unix_socket_directories=
70
+ # Keep dynamic shared memory under PGDATA, not /dev/shm, where systemd-logind's
71
+ # RemoveIPC deletes it when the user's last session ends.
72
+ -c dynamic_shared_memory_type=mmap
70
73
  )
71
74
 
72
75
  PG_PID=""
@@ -154,12 +154,13 @@ if [[ -z "${JWT_ED25519_PUBLIC_KEY:-}" && -z "${JWT_JWKS_JSON:-}" ]]; then
154
154
  export JWT_ED25519_PUBLIC_KEY
155
155
  fi
156
156
 
157
- # 2. Free our own port. A leftover process from a previous run holds it, and the
158
- # supervision loop below would otherwise respawn against "Address already in
159
- # use (os error 48)" forever. BIND_ADDR comes from the TOML env and is only
160
- # set for the services that listen, so this is a no-op for the workers.
157
+ # 2. Refuse a port that is already held. The supervision loop below would otherwise
158
+ # respawn against "Address already in use (os error 48)" forever. The holder is
159
+ # not ours to stop: it may be a leftover of a previous run (`ppn kill`), or
160
+ # anyone's. BIND_ADDR comes from the TOML env and is only set for the services
161
+ # that listen, so this is a no-op for the workers.
161
162
  if [[ -n "${BIND_ADDR:-}" ]]; then
162
- "$PROJECT_DIR/scripts/kill-port.sh" "${BIND_ADDR##*:}" >/dev/null 2>&1 || true
163
+ "$PROJECT_DIR/scripts/require-free-ports.sh" "${BIND_ADDR##*:}" || exit 1
163
164
  fi
164
165
 
165
166
  # 3. Wait for Postgres. Every service connects and applies migrations before
@@ -12,8 +12,9 @@ source "$PROJECT_DIR/config/ports.env"
12
12
  export IPFS_PATH="$BIN_DIR/.ipfs"
13
13
  LOCK_FILE="$IPFS_PATH/repo.lock"
14
14
 
15
- # Kill any process already on IPFS ports (prevents stale daemons from blocking startup)
16
- "$SCRIPT_DIR/kill-port.sh" "$IPFS_GATEWAY_PORT" "$IPFS_API_PORT" "$IPFS_SWARM_PORT"
15
+ # A daemon that cannot bind its ports would restart below for ever: stop here instead, naming
16
+ # whatever holds them (a stale daemon from a previous run, or someone else's).
17
+ "$SCRIPT_DIR/require-free-ports.sh" "$IPFS_GATEWAY_PORT" "$IPFS_API_PORT" "$IPFS_SWARM_PORT" || exit 1
17
18
 
18
19
  IPFS_PID=""
19
20
  cleanup() {
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env bash
2
+ # Refuse to go on while anything listens on the given TCP port(s). Never kills it: the holder may
3
+ # be a previous run of this network, another checkout's network or a service that has nothing to
4
+ # do with PPN, and a signal cannot tell them apart. `ppn kill` stops a previous run of this
5
+ # workspace. Listeners only: a bare `lsof -i :port` also matches every client whose connection
6
+ # has that port at the other end.
7
+ # Usage: require-free-ports.sh 8080 5001 4001
8
+ set -uo pipefail
9
+
10
+ status=0
11
+ for port in "$@"; do
12
+ [[ -n "$port" ]] || continue
13
+ holders="" pid=""
14
+ # lsof -F pc: a `p<pid>` line, then its `c<command>` line. The name shown is argv[0]'s basename
15
+ # (`ps -o args=`): lsof's COMMAND is the kernel's name, on Linux the main thread's (`MainThread`
16
+ # for Node). lsof's COMMAND only when `ps` gives nothing.
17
+ while IFS= read -r line; do
18
+ case "$line" in
19
+ p*) pid=${line#p} ;;
20
+ c*)
21
+ name=$(ps -o args= -p "$pid" 2>/dev/null | awk 'NR == 1 { print $1 }')
22
+ name=${name##*/}
23
+ holders+="${holders:+, }${name:-${line#c}}($pid)"
24
+ ;;
25
+ esac
26
+ done < <(lsof -nP -iTCP:"$port" -sTCP:LISTEN -F pc 2>/dev/null)
27
+ if [[ -n "$holders" ]]; then
28
+ echo "port $port is in use by $holders; nothing was stopped (\`ppn kill\` stops a previous run of this workspace)" >&2
29
+ status=1
30
+ fi
31
+ done
32
+ exit "$status"
package/scripts/turn.sh CHANGED
@@ -5,13 +5,12 @@ ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
5
5
  ENTRY="$ROOT/bin/ppn.mjs"
6
6
  [[ -f "$ENTRY" ]] || ENTRY="$ROOT/dist/bin.js"
7
7
 
8
- # A leftover eturnal from a previous run holds the ports. Its listeners only: a bare `lsof -i
9
- # :3478` also matches browsers connected to some other TURN server.
8
+ # A leftover eturnal from a previous run holds the ports, or another network's relay does, or
9
+ # any other Erlang node (the old sweep killed every `beam` listener on them): refuse, naming it.
10
+ # `ppn kill` stops this workspace's own, through `ppn service turn`.
10
11
  source "$ROOT/scripts/lib/workspace.sh"
11
12
  source "$ROOT/config/ports.env"
12
13
  [[ -f "$PPN_WS/config/ports.local.env" ]] && source "$PPN_WS/config/ports.local.env"
13
- for port in "$TURN_PORT" "$TURN_PROXY_PORT"; do
14
- lsof -ti "tcp:$port" -sTCP:LISTEN -a -c beam 2>/dev/null | xargs kill 2>/dev/null || true
15
- done
14
+ "$ROOT/scripts/require-free-ports.sh" "$TURN_PORT" "$TURN_PROXY_PORT" || exit 1
16
15
 
17
16
  exec node "$ENTRY" service turn
@@ -185,6 +185,7 @@ env = [
185
185
  { name = "ATTESTER_ACCOUNT", value = "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY" },
186
186
  { name = "INVITER_ADDRESS", value = "5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty" },
187
187
  { name = "BIND_ADDR", value = "127.0.0.1:8092" },
188
+ { name = "TURN_PROVIDER", value = "coturn" },
188
189
  { name = "TURN_REALM", value = "previewnet.local" },
189
190
  ]
190
191
 
@@ -1,11 +0,0 @@
1
- #!/usr/bin/env bash
2
- # Kill any process listening on the given port(s).
3
- # Usage: kill-port.sh 8080 5001 4001
4
-
5
- for PORT in "$@"; do
6
- PIDS=$(lsof -ti ":$PORT" 2>/dev/null)
7
- if [ -n "$PIDS" ]; then
8
- echo "Killing process(es) on port $PORT: $PIDS"
9
- echo "$PIDS" | xargs kill -9 2>/dev/null || true
10
- fi
11
- done