ckb-cycle-tools 1.3.0 → 1.4.0

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/dist/spawn.js ADDED
@@ -0,0 +1,77 @@
1
+ /**
2
+ * Which binary a spawned VM ran, from the locator ckb-debugger prints in its
3
+ * spawn tree (`cell_dep[1][0..11552]`, see flame.ts `spawnTree`), read from
4
+ * the mock transaction the group was replayed with. Plain TypeScript with no
5
+ * Node built-ins, like flame.ts.
6
+ */
7
+ import { ccc } from "@ckb-ccc/core";
8
+ import { parseOutPointVec } from "./mock.js";
9
+ const sameOutPoint = (a, b) => a.tx_hash.toLowerCase() === b.tx_hash.toLowerCase() && Number(a.index) === Number(b.index);
10
+ /**
11
+ * The cell deps as the node resolves them, the order `cell_dep[i]` counts in:
12
+ * each code dep is itself, each dep group is the cells it lists (the group
13
+ * cell is not one of them).
14
+ */
15
+ export function resolvedCellDeps(mock) {
16
+ const find = (o) => {
17
+ const c = mock.mock_info.cell_deps.find((d) => sameOutPoint(d.cell_dep.out_point, o));
18
+ if (!c)
19
+ throw new Error(`cell dep ${o.tx_hash}:${Number(o.index)} is not in the mock`);
20
+ return c;
21
+ };
22
+ return mock.tx.cell_deps.flatMap((d) => d.dep_type === "dep_group" ? parseOutPointVec(find(d.out_point).data).map(find) : [find(d.out_point)]);
23
+ }
24
+ /**
25
+ * The binary a spawned VM ran. `group` gives the group's input and output
26
+ * indices, for `group_input[i]` and `group_output[i]`. Undefined when the
27
+ * locator does not name a cell of this transaction (a witness, or a line this
28
+ * cannot parse).
29
+ */
30
+ export function spawnedBinary(mock, vm, group) {
31
+ if (vm.source === undefined || vm.index === undefined || vm.start === undefined || vm.end === undefined)
32
+ return undefined;
33
+ let cell;
34
+ try {
35
+ const i = vm.index;
36
+ switch (vm.source) {
37
+ case "cell_dep":
38
+ cell = resolvedCellDeps(mock)[i];
39
+ break;
40
+ case "input":
41
+ cell = mock.mock_info.inputs[i];
42
+ break;
43
+ case "group_input":
44
+ cell = group.inputs[i] === undefined ? undefined : mock.mock_info.inputs[group.inputs[i]];
45
+ break;
46
+ case "output":
47
+ cell = mock.tx.outputs_data[i] === undefined ? undefined : { output: mock.tx.outputs[i], data: mock.tx.outputs_data[i] };
48
+ break;
49
+ case "group_output": {
50
+ const o = group.outputs[i];
51
+ cell = o === undefined || mock.tx.outputs_data[o] === undefined ? undefined : { output: mock.tx.outputs[o], data: mock.tx.outputs_data[o] };
52
+ break;
53
+ }
54
+ default:
55
+ cell = undefined;
56
+ }
57
+ }
58
+ catch {
59
+ return undefined;
60
+ }
61
+ if (!cell)
62
+ return undefined;
63
+ const bytes = ccc.bytesFrom(cell.data);
64
+ if (vm.end > bytes.length || vm.start > vm.end)
65
+ return undefined;
66
+ const data = bytes.subarray(vm.start, vm.end);
67
+ const whole = vm.start === 0 && vm.end === bytes.length;
68
+ const type = cell.output?.type;
69
+ return {
70
+ data,
71
+ dataHash: ccc.hashCkb(data).toLowerCase(),
72
+ ...(whole &&
73
+ type && {
74
+ typeHash: ccc.Script.from({ codeHash: type.code_hash, hashType: type.hash_type, args: type.args }).hash().toLowerCase(),
75
+ }),
76
+ };
77
+ }
package/dist/wasm.d.ts CHANGED
@@ -1,8 +1,13 @@
1
- import { type RunDebugger } from "./replay.js";
1
+ import { type DebuggerOutput, type RunDebugger } from "./replay.js";
2
2
  export interface WasmRun {
3
3
  exitCode: number;
4
4
  stdout: string;
5
5
  stderr: string;
6
+ /**
7
+ * Files the run created in `/` (e.g. `--flamegraph-output /vm0.folded`), by
8
+ * name. The files passed in are not repeated here.
9
+ */
10
+ files: Record<string, Uint8Array>;
6
11
  }
7
12
  /**
8
13
  * Runs the debugger once. `files` are placed in `/`, so `--tx-file /tx.json`
@@ -20,3 +25,19 @@ export declare const WASM_TX_FILE = "/tx.json";
20
25
  * WASM_TX_FILE: `replayGroups(plan, wasmRunner(module, tx), { txFile: WASM_TX_FILE, … })`.
21
26
  */
22
27
  export declare function wasmRunner(module: WebAssembly.Module, tx: Uint8Array): RunDebugger;
28
+ /** A run that may carry the files it wrote: a WasmRun, or one passed on from a Web Worker. */
29
+ export type RunWithFiles = (args: string[]) => Promise<DebuggerOutput & {
30
+ files?: Record<string, Uint8Array>;
31
+ }>;
32
+ /**
33
+ * `exec`, `readFile` and `out` for profileSpawns (children.ts) over the
34
+ * WebAssembly debugger, whose files live only in each run's memory: `exec`
35
+ * keeps what the last run wrote (and only that, so a run that writes nothing
36
+ * never reads an earlier one's file), `readFile` reads it back by the path
37
+ * the debugger was given, and `out` names those paths (`/vm1.folded`).
38
+ */
39
+ export declare function wasmFiles(run: RunWithFiles): {
40
+ exec: (args: string[]) => Promise<DebuggerOutput>;
41
+ readFile: (path: string) => Promise<string>;
42
+ out: (vm: number) => string;
43
+ };
package/dist/wasm.js CHANGED
@@ -17,6 +17,7 @@ export async function runWasmDebugger(module, args, files = {}) {
17
17
  const decoders = [new TextDecoder(), new TextDecoder()];
18
18
  const text = ["", ""];
19
19
  const capture = (i) => new ConsoleStdout((bytes) => (text[i] += decoders[i].decode(bytes, { stream: true })));
20
+ // The run may add files (and directories) to it: what it wrote is read back from here.
20
21
  const root = new Map(Object.entries(files).map(([name, data]) => [name, new File(data, { readonly: true })]));
21
22
  const fds = [new OpenFile(new File([])), capture(0), capture(1), new PreopenDirectory("/", root)];
22
23
  // `debug: false` explicitly: the shim logs every call when it is left undefined.
@@ -32,7 +33,11 @@ export async function runWasmDebugger(module, args, files = {}) {
32
33
  text[1] += `\n${e instanceof Error ? e.message : String(e)}`;
33
34
  exitCode = -1;
34
35
  }
35
- return { exitCode, stdout: text[0] + decoders[0].decode(), stderr: text[1] + decoders[1].decode() };
36
+ const created = {};
37
+ for (const [name, entry] of root)
38
+ if (!Object.hasOwn(files, name) && entry instanceof File)
39
+ created[name] = entry.data;
40
+ return { exitCode, stdout: text[0] + decoders[0].decode(), stderr: text[1] + decoders[1].decode(), files: created };
36
41
  }
37
42
  /** "ckb-debugger 0.200.1", as `--version` prints it. */
38
43
  export async function wasmDebuggerVersion(module) {
@@ -56,3 +61,28 @@ export function wasmRunner(module, tx) {
56
61
  // The whole run, exit code included: replayGroups checks the result lines against it.
57
62
  return (args) => runWasmDebugger(module, args, { "tx.json": tx });
58
63
  }
64
+ /**
65
+ * `exec`, `readFile` and `out` for profileSpawns (children.ts) over the
66
+ * WebAssembly debugger, whose files live only in each run's memory: `exec`
67
+ * keeps what the last run wrote (and only that, so a run that writes nothing
68
+ * never reads an earlier one's file), `readFile` reads it back by the path
69
+ * the debugger was given, and `out` names those paths (`/vm1.folded`).
70
+ */
71
+ export function wasmFiles(run) {
72
+ let written = new Map();
73
+ return {
74
+ exec: async (args) => {
75
+ written = new Map();
76
+ const r = await run(args);
77
+ written = new Map(Object.entries(r.files ?? {}).map(([name, data]) => [`/${name}`, data]));
78
+ return r;
79
+ },
80
+ readFile: async (path) => {
81
+ const data = written.get(path);
82
+ if (!data)
83
+ throw new Error(`${path}: not written by the last run`);
84
+ return new TextDecoder().decode(data);
85
+ },
86
+ out: (vm) => `/vm${vm}.folded`,
87
+ };
88
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ckb-cycle-tools",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "Cycle measurement for CKB on-chain scripts",
5
5
  "keywords": [
6
6
  "ckb",
@@ -64,6 +64,7 @@
64
64
  "exports": {
65
65
  ".": "./dist/index.js",
66
66
  "./analyze": "./dist/analyze.js",
67
+ "./children": "./dist/children.js",
67
68
  "./flame": "./dist/flame.js",
68
69
  "./replay": "./dist/replay.js",
69
70
  "./wasm": "./dist/wasm.js"