ckb-cycle-tools 1.1.0 → 1.3.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/report.js CHANGED
@@ -16,11 +16,13 @@
16
16
  */
17
17
  import { readFile, realpath, stat } from "node:fs/promises";
18
18
  import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
19
- import { FUNCTION_LIMIT, resultLabel, termLine, termLines, unmatchedError } from "./diff.js";
20
- import { functionCosts } from "./flame.js";
19
+ import { checkDebuggerPath, resolveDebugger } from "./debugger.js";
20
+ import { FUNCTION_LIMIT, overTxLimitOf, resultLabel, spawnNote, termLine, termLines, unmatchedError } from "./diff.js";
21
+ import { foldedTotal, functionCosts } from "./flame.js";
21
22
  import { buildMockTransaction, httpRpc, mockFromTransaction, scriptVersionAt } from "./mock.js";
23
+ import { fileDigest, gitCommit, inputsHash, scenarioInputs, sha256, toolchainOf, toolIdentity } from "./manifest.js";
22
24
  import { flameTransaction, parseNames, profileTransaction, readTransactionJson } from "./profile.js";
23
- import { consensusOf, nodeVerdict, PUBLIC_RPC } from "./replay.js";
25
+ import { consensusOf, nodeVerdict, overTxLimitLines, PUBLIC_RPC } from "./replay.js";
24
26
  import { substituteBinaries, targetScriptKeys } from "./substitute.js";
25
27
  const HASH = /^0x[0-9a-fA-F]{64}$/;
26
28
  /** Parses and checks a cycles.json, with a message that says what to fix. */
@@ -99,6 +101,7 @@ export function groupResults(p) {
99
101
  result: g.result,
100
102
  ...(g.error && { error: g.error }),
101
103
  cycles: Number(g.cycles),
104
+ ...(g.nodeReaches !== undefined && { nodeReaches: g.nodeReaches }),
102
105
  }));
103
106
  }
104
107
  /**
@@ -123,6 +126,27 @@ export async function runReport(configPath, options = {}) {
123
126
  const rpcUrl = options.rpcUrl ?? (roots ? undefined : config.rpc);
124
127
  const baseNames = parseNames(Object.entries(config.names ?? {}).map(([k, v]) => `${k}=${v}`));
125
128
  const inside = (file) => confine(file, roots);
129
+ // The manifest: what the numbers were measured with. The debugger is resolved
130
+ // once, so the binary hashed is the one every scenario runs; when it cannot be
131
+ // found, each scenario says so as before.
132
+ let debuggerPath;
133
+ if (!options.runDebugger) {
134
+ try {
135
+ debuggerPath = options.debuggerPath ? checkDebuggerPath(options.debuggerPath) : resolveDebugger();
136
+ }
137
+ catch {
138
+ debuggerPath = undefined;
139
+ }
140
+ }
141
+ const environment = Promise.all([
142
+ toolIdentity(),
143
+ debuggerPath ? fileDigest(debuggerPath).catch(() => undefined) : Promise.resolve(undefined),
144
+ gitCommit(dir),
145
+ options.binariesRoot ? gitCommit(binDir) : Promise.resolve(undefined),
146
+ ]);
147
+ const scenarioManifests = [];
148
+ /** Binary name → what was read: for the manifest of the scenarios it is swapped into. */
149
+ const digests = new Map();
126
150
  // A binary resolves to null when it is absent from another checkout (the
127
151
  // base, --binaries-from): that binary is new there, not broken. Only it, and
128
152
  // the groups whose code it is, count as new; every other binary still swaps in.
@@ -133,12 +157,15 @@ export async function runReport(configPath, options = {}) {
133
157
  const b = config.binaries[name];
134
158
  p = inside(resolve(binDir, b.path))
135
159
  .then((file) => readFile(file))
136
- .then((data) => ({
137
- name,
138
- data: new Uint8Array(data),
139
- ...(b.outPoint && { outPoint: b.outPoint }),
140
- ...(b.codeHash && { codeHash: b.codeHash }),
141
- }), (e) => {
160
+ .then((data) => {
161
+ digests.set(name, { path: b.path, sha256: sha256(data), size: data.length });
162
+ return {
163
+ name,
164
+ data: new Uint8Array(data),
165
+ ...(b.outPoint && { outPoint: b.outPoint }),
166
+ ...(b.codeHash && { codeHash: b.codeHash }),
167
+ };
168
+ }, (e) => {
142
169
  if (options.binariesRoot && e.code === "ENOENT") {
143
170
  missingNames.add(name);
144
171
  return null;
@@ -155,6 +182,11 @@ export async function runReport(configPath, options = {}) {
155
182
  const scenarios = [];
156
183
  for (const s of config.scenarios) {
157
184
  let result;
185
+ const sm = { name: s.name };
186
+ const entry = scenarioInputs(s, network);
187
+ // A tx scenario has no file: its entry (the hash) is its input.
188
+ if (s.tx !== undefined)
189
+ sm.inputs = inputsHash(entry);
158
190
  try {
159
191
  const explicit = s.binaries !== undefined;
160
192
  const names = explicit ? s.binaries : Object.keys(config.binaries ?? {});
@@ -169,7 +201,8 @@ export async function runReport(configPath, options = {}) {
169
201
  ...(network && { network }),
170
202
  ...(rpcUrl && { rpcUrl }),
171
203
  ...(options.timeoutMs !== undefined && { timeoutMs: options.timeoutMs }),
172
- ...(options.debuggerPath && { debuggerPath: options.debuggerPath }),
204
+ // The binary the manifest hashed; an explicit path that does not check out fails each scenario, as before.
205
+ ...((debuggerPath ?? options.debuggerPath) && { debuggerPath: debuggerPath ?? options.debuggerPath }),
173
206
  ...(options.runDebugger && { runDebugger: options.runDebugger }),
174
207
  ...(s.scriptVersion !== undefined && { scriptVersion: s.scriptVersion }),
175
208
  };
@@ -185,13 +218,19 @@ export async function runReport(configPath, options = {}) {
185
218
  const rpc = () => httpRpc(url, { ...(options.timeoutMs !== undefined && { timeoutMs: options.timeoutMs }) });
186
219
  let mock;
187
220
  let scriptVersion = s.scriptVersion;
221
+ // The node's own limit for one transaction, when its consensus is fetched here.
222
+ let txCycleLimit;
188
223
  if (s.mock) {
189
- mock = JSON.parse(await readFile(await inside(resolve(dir, s.mock)), "utf8"));
224
+ const bytes = await readFile(await inside(resolve(dir, s.mock)));
225
+ sm.inputs = inputsHash(entry, bytes);
226
+ mock = JSON.parse(bytes.toString("utf8"));
190
227
  if (!mock?.mock_info || !mock.tx)
191
228
  throw new Error(`${s.mock} is not a ckb-debugger mock transaction`);
192
229
  }
193
230
  else if (s.file) {
194
- mock = await mockFromTransaction(rpc(), readTransactionJson(await readFile(await inside(resolve(dir, s.file)), "utf8")));
231
+ const bytes = await readFile(await inside(resolve(dir, s.file)));
232
+ sm.inputs = inputsHash(entry, bytes);
233
+ mock = await mockFromTransaction(rpc(), readTransactionJson(bytes.toString("utf8")));
195
234
  }
196
235
  else {
197
236
  const node = rpc();
@@ -200,6 +239,7 @@ export async function runReport(configPath, options = {}) {
200
239
  consensusOf(node, url, { ...(network && { network }), ...(rpcUrl && { rpcUrl }) }),
201
240
  ]);
202
241
  mock = built.mock;
242
+ txCycleLimit = consensus?.maxBlockCycles;
203
243
  if (scriptVersion === undefined && built.epoch !== undefined) {
204
244
  scriptVersion = scriptVersionAt(consensus?.vmEpochs ?? network ?? "testnet", built.epoch);
205
245
  }
@@ -231,6 +271,7 @@ export async function runReport(configPath, options = {}) {
231
271
  profile = await profileTransaction(swapped, {
232
272
  ...profileOptions,
233
273
  ...(scriptVersion !== undefined && { scriptVersion }),
274
+ ...(txCycleLimit !== undefined && { txCycleLimit }),
234
275
  scriptNames,
235
276
  });
236
277
  // The scripts whose code is a binary with symbols, when the scenario asks for functions.
@@ -252,6 +293,13 @@ export async function runReport(configPath, options = {}) {
252
293
  }
253
294
  }
254
295
  debuggerVersion ??= profile.env.debugger;
296
+ sm.scriptVersion = profile.env.scriptVersion;
297
+ const swappedIn = substituted.flatMap((name) => {
298
+ const d = digests.get(name);
299
+ return d ? [{ name, ...d }] : [];
300
+ });
301
+ if (swappedIn.length)
302
+ sm.binaries = swappedIn;
255
303
  // Kept with the scenario: numbers that do not add up to the node's are not to be trusted.
256
304
  if (nodeVerdict(profile) === "mismatch") {
257
305
  warnings.push(`the groups add up to ${profile.measuredCycles} cycles, the node reported ${profile.nodeCycles}`);
@@ -269,12 +317,17 @@ export async function runReport(configPath, options = {}) {
269
317
  ...(substituted.length && { substituted }),
270
318
  ...(unmatched.length && { unmatched }),
271
319
  ...(missing.length && { binaryMissing: true, missingBinaries: missing }),
320
+ ...(profile.nodeStopsAt !== undefined && { nodeStopsAt: profile.nodeStopsAt }),
321
+ ...(profile.overTxLimit && {
322
+ overTxLimit: { limit: Number(profile.overTxLimit.limit), sum: Number(profile.overTxLimit.sum) },
323
+ }),
272
324
  };
273
325
  }
274
326
  catch (e) {
275
327
  result = { name: s.name, error: e instanceof Error ? e.message : String(e), groups: [] };
276
328
  }
277
329
  scenarios.push(result);
330
+ scenarioManifests.push(sm);
278
331
  options.onScenario?.(result);
279
332
  }
280
333
  const errors = scenarios.filter((s) => s.error).map((s) => `scenario "${s.name}": ${s.error}`);
@@ -287,6 +340,22 @@ export async function runReport(configPath, options = {}) {
287
340
  errors.push(unmatchedError(name));
288
341
  }
289
342
  }
343
+ const [tool, debuggerDigest, commit, binariesCommit] = await environment;
344
+ const toolchain = toolchainOf(options.toolchain);
345
+ const manifest = {
346
+ schema: 1,
347
+ tool,
348
+ debugger: {
349
+ version: debuggerVersion ?? "unknown",
350
+ ...(debuggerPath && { path: debuggerPath }),
351
+ ...(debuggerDigest && { sha256: debuggerDigest.sha256 }),
352
+ ...(options.runDebugger && options.debuggerBuild?.sha256 && { sha256: options.debuggerBuild.sha256 }),
353
+ ...(options.runDebugger && options.debuggerBuild?.wasm && { wasm: options.debuggerBuild.wasm }),
354
+ },
355
+ ...((commit || binariesCommit) && { git: { ...(commit && { commit }), ...(binariesCommit && { binaries: binariesCommit }) } }),
356
+ ...(toolchain && { toolchain }),
357
+ scenarios: scenarioManifests,
358
+ };
290
359
  return {
291
360
  tool: "ckb-cycles",
292
361
  version: 1,
@@ -294,6 +363,7 @@ export async function runReport(configPath, options = {}) {
294
363
  scenarios,
295
364
  errors,
296
365
  ...(notices.length && { notices }),
366
+ manifest,
297
367
  };
298
368
  }
299
369
  /**
@@ -342,6 +412,10 @@ async function recordFunctions(groups, mock, symbols, binDir, inside, options) {
342
412
  }
343
413
  for (const g of run) {
344
414
  const f = flame.groups.find((f) => f.selector === g.selector);
415
+ if (f?.spawned) {
416
+ g.spawned = f.spawned;
417
+ g.profiled = Number(foldedTotal(f.folded));
418
+ }
345
419
  if (!f)
346
420
  g.functionsWarning = "the flamegraph run did not profile this group";
347
421
  else if (f.cycles !== BigInt(g.cycles) || resultLabel(f) !== resultLabel(g)) {
@@ -429,6 +503,28 @@ export function formatReport(report) {
429
503
  for (const g of s.groups.filter((g) => g.error))
430
504
  out.push("", `${t(s.name)} ${t(g.selector)} stopped: ${t(g.error)}`);
431
505
  }
506
+ // A rejected scenario's cycles are each group on its own: not what the node spends.
507
+ for (const s of report.scenarios) {
508
+ if (s.nodeStopsAt === undefined)
509
+ continue;
510
+ const stop = s.groups.find((g) => g.selector === s.nodeStopsAt);
511
+ const skipped = s.groups.filter((g) => g.nodeReaches === false).map((g) => t(g.selector));
512
+ const at = `${t(s.nodeStopsAt)}${stop ? ` (${t(stop.script)})` : ""}`;
513
+ const limit = overTxLimitOf(s);
514
+ if (limit && stop && resultLabel(stop) === "ok") {
515
+ out.push("", `${t(s.name)}: ${overTxLimitLines(limit).join(" ")}. Each group above ran on its own with the whole limit; ` +
516
+ `the node gives each group what the ones before it left, and stops at ${at}, where the running sum passes it`);
517
+ }
518
+ else {
519
+ out.push("", `${t(s.name)}: rejected. Each group above ran on its own; the node stops at the first failing group, ` +
520
+ `${at}, and reports no cycles for a rejected tx`);
521
+ if (limit)
522
+ out.push(` the sum of its groups is also over the node's per-transaction limit of ${limit.limit.toLocaleString("en-US")} cycles`);
523
+ }
524
+ if (skipped.length) {
525
+ out.push(` the node never runs: ${skipped.slice(0, 8).join(", ")}${skipped.length > 8 ? `, and ${skipped.length - 8} more` : ""}`);
526
+ }
527
+ }
432
528
  for (const s of report.scenarios) {
433
529
  for (const g of s.groups) {
434
530
  if (g.functionsWarning)
@@ -436,10 +532,25 @@ export function formatReport(report) {
436
532
  else if (g.functions?.length) {
437
533
  const top = g.functions.slice(0, 3).map((f) => `${t(f.name)} ${fmt(f.self)}`);
438
534
  out.push("", `${t(s.name)} ${t(g.selector)}: costliest functions (self cycles): ${top.join(", ")}`);
535
+ if (g.spawned)
536
+ out.push(` ${spawnNote({ ...g, head: g })}`);
439
537
  }
440
538
  }
441
539
  }
442
540
  if (report.debugger)
443
541
  out.push("", t(report.debugger));
542
+ const m = report.manifest;
543
+ if (m) {
544
+ const parts = [
545
+ ...(m.debugger?.sha256 ? [`debugger sha256 ${m.debugger.sha256}`] : []),
546
+ ...(m.debugger?.wasm ? [`debugger wasm ${m.debugger.wasm}`] : []),
547
+ ...(m.git?.commit ? [`commit ${m.git.commit}`] : []),
548
+ ...(m.git?.binaries && m.git.binaries !== m.git.commit ? [`binaries from ${m.git.binaries}`] : []),
549
+ ...(m.toolchain ? [`toolchain: ${m.toolchain}`] : []),
550
+ ...(m.tool?.version ? [`${m.tool.name} ${m.tool.version}`] : []),
551
+ ];
552
+ if (parts.length)
553
+ out.push(t(`manifest: ${parts.join(" · ")}`));
554
+ }
444
555
  return out.join("\n");
445
556
  }
@@ -130,6 +130,17 @@ function substituteOne(mock, sub, skipUnmatched) {
130
130
  if (argsMentions) {
131
131
  warnings.push(`the old code hash still appears in the args of ${argsMentions} script(s); args are not rewritten`);
132
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
+ }
133
144
  }
134
145
  return {
135
146
  ...(sub.name && { name: sub.name }),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ckb-cycle-tools",
3
- "version": "1.1.0",
3
+ "version": "1.3.0",
4
4
  "description": "Cycle measurement for CKB on-chain scripts",
5
5
  "keywords": [
6
6
  "ckb",
@@ -39,10 +39,12 @@
39
39
  "cycles": "npm run build && node dist/cli.js",
40
40
  "bench:build": "npm run build && node benchmarks/session-lock/build-js.mjs",
41
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",
42
43
  "prepack": "npm run build",
43
44
  "pack:check": "node scripts/pack-check.mjs",
44
45
  "atlas": "tsx scripts/atlas.ts",
45
- "compat": "tsx scripts/compat.ts"
46
+ "compat": "tsx scripts/compat.ts",
47
+ "hardfork": "tsx scripts/hardfork-check.ts"
46
48
  },
47
49
  "devDependencies": {
48
50
  "@ckb-js-std/bindings": "^1.0.0",