ckb-cycle-tools 1.0.0 → 1.2.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/CHANGELOG.md +169 -0
- package/README.md +659 -394
- package/dist/cli.js +79 -23
- package/dist/diff.d.ts +94 -8
- package/dist/diff.js +167 -29
- package/dist/elf.d.ts +18 -0
- package/dist/elf.js +209 -0
- package/dist/flame.d.ts +21 -1
- package/dist/flame.js +43 -3
- package/dist/index.d.ts +4 -2
- package/dist/index.js +3 -2
- package/dist/profile.d.ts +11 -1
- package/dist/profile.js +20 -4
- package/dist/replay.d.ts +14 -1
- package/dist/replay.js +44 -2
- package/dist/report.d.ts +6 -1
- package/dist/report.js +86 -31
- package/dist/scan.d.ts +46 -0
- package/dist/scan.js +122 -0
- package/dist/substitute.d.ts +7 -0
- package/dist/substitute.js +39 -7
- package/dist/wasm.d.ts +1 -1
- package/dist/wasm.js +7 -0
- package/package.json +12 -4
package/dist/scan.js
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `ckb-cycles scan`: a cycles.json from the transactions a test suite wrote.
|
|
3
|
+
*
|
|
4
|
+
* The tests (Rust ckb-testtool, through integrations/ckb-testtool, or anything
|
|
5
|
+
* else that writes ckb-debugger mock transactions) leave one mock per
|
|
6
|
+
* transaction in a directory. Each becomes a `"mock"` scenario, named by its
|
|
7
|
+
* file. Each file in the binaries directory (a ckb-script-templates project's
|
|
8
|
+
* `build/release`) whose data hash is the data of a code cell dep in some mock
|
|
9
|
+
* becomes a binary, matched by that hash, with `<name>.debug` beside it as its
|
|
10
|
+
* symbols build.
|
|
11
|
+
*
|
|
12
|
+
* The hashes are those of the build the tests ran against, so the file is
|
|
13
|
+
* regenerated after each build rather than written by hand.
|
|
14
|
+
*/
|
|
15
|
+
import { readdir, readFile, stat, writeFile } from "node:fs/promises";
|
|
16
|
+
import { basename, dirname, join, relative, resolve, sep } from "node:path";
|
|
17
|
+
import { ccc } from "@ckb-ccc/core";
|
|
18
|
+
import { termLine } from "./diff.js";
|
|
19
|
+
/** The cycles.json for these mocks and binaries. Pure: the caller reads the files. */
|
|
20
|
+
export function configFromMocks(mocks, binaries, options = {}) {
|
|
21
|
+
if (!mocks.length)
|
|
22
|
+
throw new Error("no mock transactions to scan");
|
|
23
|
+
const deployed = new Set();
|
|
24
|
+
for (const m of mocks) {
|
|
25
|
+
for (const d of m.mock.mock_info.cell_deps)
|
|
26
|
+
if (d.cell_dep.dep_type === "code")
|
|
27
|
+
deployed.add(ccc.hashCkb(d.data));
|
|
28
|
+
}
|
|
29
|
+
const found = {};
|
|
30
|
+
const matched = [];
|
|
31
|
+
const unused = [];
|
|
32
|
+
const hashes = new Set();
|
|
33
|
+
for (const b of [...binaries].sort((a, b) => a.name.localeCompare(b.name))) {
|
|
34
|
+
const codeHash = ccc.hashCkb(b.data);
|
|
35
|
+
if (!deployed.has(codeHash)) {
|
|
36
|
+
unused.push(b.name);
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
if (hashes.has(codeHash)) {
|
|
40
|
+
// Two files with the same bytes: the first one names it.
|
|
41
|
+
unused.push(b.name);
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
hashes.add(codeHash);
|
|
45
|
+
found[b.name] = { path: b.path, codeHash, ...(b.symbols && { symbols: b.symbols }) };
|
|
46
|
+
matched.push(b.name);
|
|
47
|
+
}
|
|
48
|
+
if (options.functions && !Object.values(found).some((b) => b.symbols)) {
|
|
49
|
+
throw new Error(`--functions needs a <name>.debug (an unstripped build) beside a binary the tests deploy`);
|
|
50
|
+
}
|
|
51
|
+
const config = {
|
|
52
|
+
...(options.functions && { functions: true }),
|
|
53
|
+
binaries: found,
|
|
54
|
+
scenarios: [...mocks]
|
|
55
|
+
.sort((a, b) => a.name.localeCompare(b.name))
|
|
56
|
+
.map((m) => ({ name: m.name, mock: m.path })),
|
|
57
|
+
};
|
|
58
|
+
return { config, matched, unused, others: [...deployed].filter((h) => !hashes.has(h)).sort() };
|
|
59
|
+
}
|
|
60
|
+
const posix = (p) => p.split(sep).join("/");
|
|
61
|
+
/** Reads the mocks and the binaries, and writes the cycles.json. */
|
|
62
|
+
export async function scanTestMocks(mocksDir, options = {}) {
|
|
63
|
+
const out = resolve(options.out ?? "cycles.json");
|
|
64
|
+
const binariesDir = resolve(options.binariesDir ?? join("build", "release"));
|
|
65
|
+
const rel = (file) => posix(relative(dirname(out), file));
|
|
66
|
+
const mocks = [];
|
|
67
|
+
const mockFiles = (await readdir(mocksDir).catch((e) => {
|
|
68
|
+
if (e.code === "ENOENT")
|
|
69
|
+
throw new Error(`${mocksDir} does not exist: run the tests with CKB_CYCLES_MOCKS set to it`);
|
|
70
|
+
throw e;
|
|
71
|
+
}))
|
|
72
|
+
.filter((f) => f.endsWith(".json"))
|
|
73
|
+
.sort();
|
|
74
|
+
for (const f of mockFiles) {
|
|
75
|
+
const file = resolve(mocksDir, f);
|
|
76
|
+
let mock;
|
|
77
|
+
try {
|
|
78
|
+
mock = JSON.parse(await readFile(file, "utf8"));
|
|
79
|
+
}
|
|
80
|
+
catch (e) {
|
|
81
|
+
throw new Error(`${join(mocksDir, f)}: not JSON (${e instanceof Error ? e.message : e})`);
|
|
82
|
+
}
|
|
83
|
+
if (!mock?.mock_info || !mock.tx)
|
|
84
|
+
throw new Error(`${join(mocksDir, f)} is not a ckb-debugger mock transaction`);
|
|
85
|
+
mocks.push({ name: f.slice(0, -".json".length), path: rel(file), mock });
|
|
86
|
+
}
|
|
87
|
+
if (!mocks.length)
|
|
88
|
+
throw new Error(`no mock transactions in ${mocksDir}: run the tests with CKB_CYCLES_MOCKS set to it`);
|
|
89
|
+
const names = await readdir(binariesDir).catch((e) => {
|
|
90
|
+
if (e.code === "ENOENT")
|
|
91
|
+
throw new Error(`${options.binariesDir ?? "build/release"} does not exist: build the contracts first`);
|
|
92
|
+
throw e;
|
|
93
|
+
});
|
|
94
|
+
const binaries = [];
|
|
95
|
+
for (const name of names.filter((n) => !n.endsWith(".debug")).sort()) {
|
|
96
|
+
const file = join(binariesDir, name);
|
|
97
|
+
if (!(await stat(file)).isFile())
|
|
98
|
+
continue;
|
|
99
|
+
const debug = `${file}.debug`;
|
|
100
|
+
const symbols = await stat(debug).then((s) => s.isFile(), () => false);
|
|
101
|
+
binaries.push({ name, path: rel(file), data: new Uint8Array(await readFile(file)), ...(symbols && { symbols: rel(debug) }) });
|
|
102
|
+
}
|
|
103
|
+
const result = configFromMocks(mocks, binaries, { functions: options.functions });
|
|
104
|
+
await writeFile(out, JSON.stringify(result.config, null, 2) + "\n");
|
|
105
|
+
return { ...result, out };
|
|
106
|
+
}
|
|
107
|
+
/** What scan found, for stderr. */
|
|
108
|
+
export function formatScan(r, binariesDir) {
|
|
109
|
+
const short = (h) => `${h.slice(0, 10)}…${h.slice(-6)}`;
|
|
110
|
+
const lines = [
|
|
111
|
+
`${basename(r.out)}: ${r.config.scenarios.length} scenario(s), ${r.matched.length} binar${r.matched.length === 1 ? "y" : "ies"}` +
|
|
112
|
+
(r.matched.length ? ` (${r.matched.map(termLine).join(", ")})` : ""),
|
|
113
|
+
];
|
|
114
|
+
if (!r.matched.length) {
|
|
115
|
+
lines.push(`no file in ${binariesDir} is deployed by any test: the report cannot swap in another build (rebuild, then rerun the tests?)`);
|
|
116
|
+
}
|
|
117
|
+
if (r.unused.length)
|
|
118
|
+
lines.push(`not deployed by any test: ${r.unused.map(termLine).join(", ")}`);
|
|
119
|
+
if (r.others.length)
|
|
120
|
+
lines.push(`code cells that are none of the binaries: ${r.others.map(short).join(", ")}`);
|
|
121
|
+
return lines.join("\n");
|
|
122
|
+
}
|
package/dist/substitute.d.ts
CHANGED
|
@@ -38,4 +38,11 @@ export declare function substituteBinaries(mock: MockTransaction, substitutions:
|
|
|
38
38
|
mock: MockTransaction;
|
|
39
39
|
applied: AppliedSubstitution[];
|
|
40
40
|
};
|
|
41
|
+
/**
|
|
42
|
+
* The scripts (`codeHash:hashType` keys, lower case) that run the code a
|
|
43
|
+
* substitution would replace, in the mock as it is: for a binary that could
|
|
44
|
+
* not be read (absent from the base checkout), the groups whose numbers are
|
|
45
|
+
* the mock's code instead of that build's. Empty when it matches no cell dep.
|
|
46
|
+
*/
|
|
47
|
+
export declare function targetScriptKeys(mock: MockTransaction, sub: Pick<BinarySubstitution, "outPoint" | "codeHash">): string[];
|
|
41
48
|
export {};
|
package/dist/substitute.js
CHANGED
|
@@ -46,20 +46,41 @@ export function substituteBinaries(mock, substitutions, options = {}) {
|
|
|
46
46
|
}
|
|
47
47
|
return { mock: next, applied };
|
|
48
48
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
if (!sub.outPoint === !sub.codeHash)
|
|
52
|
-
throw new Error(`substitution ${label}: give an out point or a code hash, not both`);
|
|
53
|
-
const data = ccc.hexFrom(sub.data);
|
|
54
|
-
const newDataHash = ccc.hashCkb(data);
|
|
49
|
+
/** The code cell deps a substitution would replace: by out point, or by data hash or type script hash. */
|
|
50
|
+
function targetsOf(mock, sub) {
|
|
55
51
|
const want = sub.codeHash?.toLowerCase();
|
|
56
|
-
|
|
52
|
+
return mock.mock_info.cell_deps.filter((d) => {
|
|
57
53
|
if (d.cell_dep.dep_type !== "code")
|
|
58
54
|
return false;
|
|
59
55
|
if (sub.outPoint)
|
|
60
56
|
return sameOutPoint(d.cell_dep.out_point, sub.outPoint);
|
|
61
57
|
return ccc.hashCkb(d.data) === want || (d.output.type != null && typeHash(d.output.type) === want);
|
|
62
58
|
});
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* The scripts (`codeHash:hashType` keys, lower case) that run the code a
|
|
62
|
+
* substitution would replace, in the mock as it is: for a binary that could
|
|
63
|
+
* not be read (absent from the base checkout), the groups whose numbers are
|
|
64
|
+
* the mock's code instead of that build's. Empty when it matches no cell dep.
|
|
65
|
+
*/
|
|
66
|
+
export function targetScriptKeys(mock, sub) {
|
|
67
|
+
const keys = new Set();
|
|
68
|
+
for (const d of targetsOf(mock, sub)) {
|
|
69
|
+
const dataHash = ccc.hashCkb(d.data).toLowerCase();
|
|
70
|
+
for (const t of DATA_HASH_TYPES)
|
|
71
|
+
keys.add(`${dataHash}:${t}`);
|
|
72
|
+
if (d.output.type)
|
|
73
|
+
keys.add(`${typeHash(d.output.type)}:type`.toLowerCase());
|
|
74
|
+
}
|
|
75
|
+
return [...keys];
|
|
76
|
+
}
|
|
77
|
+
function substituteOne(mock, sub, skipUnmatched) {
|
|
78
|
+
const label = sub.name ?? sub.codeHash ?? (sub.outPoint && `${sub.outPoint.tx_hash}:${Number(sub.outPoint.index)}`);
|
|
79
|
+
if (!sub.outPoint === !sub.codeHash)
|
|
80
|
+
throw new Error(`substitution ${label}: give an out point or a code hash, not both`);
|
|
81
|
+
const data = ccc.hexFrom(sub.data);
|
|
82
|
+
const newDataHash = ccc.hashCkb(data);
|
|
83
|
+
const targets = targetsOf(mock, sub);
|
|
63
84
|
if (!targets.length) {
|
|
64
85
|
if (skipUnmatched)
|
|
65
86
|
return undefined;
|
|
@@ -109,6 +130,17 @@ function substituteOne(mock, sub, skipUnmatched) {
|
|
|
109
130
|
if (argsMentions) {
|
|
110
131
|
warnings.push(`the old code hash still appears in the args of ${argsMentions} script(s); args are not rewritten`);
|
|
111
132
|
}
|
|
133
|
+
// A script that spawns or execs this code by its data hash may read that hash
|
|
134
|
+
// from a cell's data (a match cell naming its game): it will not find the new build.
|
|
135
|
+
const needle = oldDataHash.slice(2);
|
|
136
|
+
const dataMentions = [...mock.mock_info.inputs, ...mock.mock_info.cell_deps]
|
|
137
|
+
.map((c) => c.data)
|
|
138
|
+
.concat(mock.tx.outputs_data)
|
|
139
|
+
.filter((d) => d.toLowerCase().includes(needle)).length;
|
|
140
|
+
if (dataMentions) {
|
|
141
|
+
warnings.push(`the old code hash still appears in the data of ${dataMentions} cell(s); cell data is not rewritten, ` +
|
|
142
|
+
`so a script that spawns or execs this code by the hash it reads there does not find the new build`);
|
|
143
|
+
}
|
|
112
144
|
}
|
|
113
145
|
return {
|
|
114
146
|
...(sub.name && { name: sub.name }),
|
package/dist/wasm.d.ts
CHANGED
package/dist/wasm.js
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
* directory at `/`, and stdout/stderr are collected as text.
|
|
9
9
|
*/
|
|
10
10
|
import { ConsoleStdout, File, OpenFile, PreopenDirectory, WASI } from "@bjorn3/browser_wasi_shim";
|
|
11
|
+
import { checkMockTransaction } from "./replay.js";
|
|
11
12
|
/**
|
|
12
13
|
* Runs the debugger once. `files` are placed in `/`, so `--tx-file /tx.json`
|
|
13
14
|
* reads `files["tx.json"]`. A fresh instance each time: no state leaks between runs.
|
|
@@ -46,6 +47,12 @@ export const WASM_TX_FILE = "/tx.json";
|
|
|
46
47
|
* WASM_TX_FILE: `replayGroups(plan, wasmRunner(module, tx), { txFile: WASM_TX_FILE, … })`.
|
|
47
48
|
*/
|
|
48
49
|
export function wasmRunner(module, tx) {
|
|
50
|
+
// The bytes the debugger reads, checked as they are: no `{{ … }}` directive
|
|
51
|
+
// anywhere in them, and a mock checkMockTransaction accepts.
|
|
52
|
+
const text = new TextDecoder().decode(tx);
|
|
53
|
+
if (text.includes("{{"))
|
|
54
|
+
throw new Error(`mock transaction refused: contains "{{", a ckb-debugger template directive`);
|
|
55
|
+
checkMockTransaction(JSON.parse(text));
|
|
49
56
|
// The whole run, exit code included: replayGroups checks the result lines against it.
|
|
50
57
|
return (args) => runWasmDebugger(module, args, { "tx.json": tx });
|
|
51
58
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ckb-cycle-tools",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Cycle measurement for CKB on-chain scripts",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ckb",
|
|
@@ -12,7 +12,10 @@
|
|
|
12
12
|
],
|
|
13
13
|
"license": "MIT",
|
|
14
14
|
"author": "Luong Thanh Tung (https://github.com/luongthanhtung03)",
|
|
15
|
-
"repository":
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/luongthanhtung03/ckb-cycle-tools.git"
|
|
18
|
+
},
|
|
16
19
|
"homepage": "https://github.com/luongthanhtung03/ckb-cycle-tools#readme",
|
|
17
20
|
"bugs": "https://github.com/luongthanhtung03/ckb-cycle-tools/issues",
|
|
18
21
|
"type": "module",
|
|
@@ -27,20 +30,25 @@
|
|
|
27
30
|
},
|
|
28
31
|
"scripts": {
|
|
29
32
|
"build": "node -e \"require('fs').rmSync('dist',{recursive:true,force:true})\" && tsc -p tsconfig.build.json",
|
|
30
|
-
"typecheck": "tsc --noEmit",
|
|
33
|
+
"typecheck": "tsc --noEmit && tsc -p e2e",
|
|
31
34
|
"test": "vitest run",
|
|
35
|
+
"e2e": "playwright test -c e2e",
|
|
32
36
|
"demo": "npm run build && npm run dev -w demo",
|
|
33
37
|
"build:demo": "npm run build && npm run build -w demo",
|
|
34
38
|
"analyze:testnet": "npm run build && node scripts/analyze-testnet.mjs",
|
|
35
39
|
"cycles": "npm run build && node dist/cli.js",
|
|
36
40
|
"bench:build": "npm run build && node benchmarks/session-lock/build-js.mjs",
|
|
37
41
|
"bench:session-lock": "npm run bench:build && node benchmarks/session-lock/compare.mjs",
|
|
42
|
+
"bench:floor": "npm run build && node benchmarks/session-lock/floor.mjs",
|
|
38
43
|
"prepack": "npm run build",
|
|
39
|
-
"pack:check": "node scripts/pack-check.mjs"
|
|
44
|
+
"pack:check": "node scripts/pack-check.mjs",
|
|
45
|
+
"atlas": "tsx scripts/atlas.ts",
|
|
46
|
+
"compat": "tsx scripts/compat.ts"
|
|
40
47
|
},
|
|
41
48
|
"devDependencies": {
|
|
42
49
|
"@ckb-js-std/bindings": "^1.0.0",
|
|
43
50
|
"@ckb-js-std/core": "^1.0.1",
|
|
51
|
+
"@playwright/test": "^1.64.1",
|
|
44
52
|
"@types/node": "^22.20.5",
|
|
45
53
|
"ckb-testtool": "^1.0.5",
|
|
46
54
|
"esbuild": "^0.28.2",
|