ckb-cycle-tools 1.2.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/replay.d.ts CHANGED
@@ -10,6 +10,12 @@ export interface GroupRun {
10
10
  cycles: bigint;
11
11
  /** Lines the script printed through ckb_debug. */
12
12
  logs: string[];
13
+ /**
14
+ * Set only when some group rejects: whether the node would run this group at
15
+ * all. It verifies groups in its own order (nodeOrder) and stops at the first
16
+ * one that fails, so the groups after that one never run there.
17
+ */
18
+ nodeReaches?: boolean;
13
19
  }
14
20
  export type ProfiledGroup = ScriptGroup & GroupRun;
15
21
  export interface Profile {
@@ -18,8 +24,30 @@ export interface Profile {
18
24
  status: string;
19
25
  /** Total cycles as reported by the node. */
20
26
  nodeCycles?: bigint;
21
- /** Sum of the per-group cycles measured by ckb-debugger. */
27
+ /**
28
+ * Sum of the per-group cycles measured by ckb-debugger, each group run on its
29
+ * own. When a group rejects, this is not what the node spent: see nodeStopsAt.
30
+ */
22
31
  measuredCycles: bigint;
32
+ /**
33
+ * Set when some group rejects: the selector of the group the node would stop
34
+ * at, the first failing one in the node's order (nodeOrder). The node then
35
+ * rejects the transaction and reports no cycles for it.
36
+ */
37
+ nodeStopsAt?: string;
38
+ /**
39
+ * Set only when the groups' cycles add up to more than the node's limit for
40
+ * one transaction (`limit`: the consensus `max_block_cycles`, or
41
+ * DEFAULT_TX_CYCLE_LIMIT). ckb-debugger gives every group that limit on its
42
+ * own; the node gives the whole transaction that limit, each group what the
43
+ * ones before it left, so it would reject this transaction (exceeded max
44
+ * cycles). `nodeStopsAt` then names the group at which the running sum, in
45
+ * the node's order, passes the limit, unless a failing group comes first.
46
+ */
47
+ overTxLimit?: {
48
+ limit: bigint;
49
+ sum: bigint;
50
+ };
23
51
  groups: ProfiledGroup[];
24
52
  /** Where the numbers came from, so a result can be reproduced. */
25
53
  env: {
@@ -49,7 +77,19 @@ export interface ReplayOptions {
49
77
  * transaction's block, or the current one (2) for a transaction not on chain.
50
78
  */
51
79
  scriptVersion?: 0 | 1 | 2;
80
+ /**
81
+ * The node's cycle limit for one transaction. By default, the `max_block_cycles`
82
+ * of the node's `get_consensus` when it was asked, else DEFAULT_TX_CYCLE_LIMIT.
83
+ */
84
+ txCycleLimit?: bigint;
52
85
  }
86
+ /**
87
+ * The node's cycle limit for one transaction when its consensus is not known:
88
+ * `max_block_cycles` on mainnet and testnet. The node verifies a transaction's
89
+ * groups one after another under this one budget (each gets what the ones
90
+ * before it left), where ckb-debugger gives each group all of it.
91
+ */
92
+ export declare const DEFAULT_TX_CYCLE_LIMIT = 3500000000n;
53
93
  /**
54
94
  * Type ID is verified natively by the node rather than by a script binary, so
55
95
  * ckb-debugger cannot run it. The node charges it a fixed cost (TYPE_ID_CYCLES
@@ -111,6 +151,8 @@ export interface ReplayPlan {
111
151
  /** "testnet", "mainnet", or a devnet's chain id: what the node says it serves, when it was asked. */
112
152
  network: string;
113
153
  rpcUrl: string;
154
+ /** The node's limit for one transaction, when known (the option, or the node's `max_block_cycles`); else DEFAULT_TX_CYCLE_LIMIT applies. */
155
+ txCycleLimit?: bigint;
114
156
  }
115
157
  /**
116
158
  * Refuses a mock transaction that ckb-debugger would read as more than data.
@@ -155,6 +197,61 @@ export declare function replayGroups(plan: ReplayPlan, run: RunDebugger, options
155
197
  debugger: string;
156
198
  onGroup?: (index: number, selector: string) => void;
157
199
  }): Promise<Profile>;
200
+ /**
201
+ * The groups in the order the node verifies them: every lock group, then every
202
+ * type group, each set sorted by script hash (byte order, the same as the order
203
+ * of the lowercase hex). ckb-script keeps them in two BTreeMaps keyed by script
204
+ * hash and chains locks before types (TxData::new and TxInfo::groups in
205
+ * script/src/types.rs); TransactionScriptsVerifier::verify (script/src/verify.rs)
206
+ * runs them in that order and returns at the first error, with no cycles.
207
+ * Groups are listed elsewhere in first-appearance order, which is not this one.
208
+ */
209
+ export declare function nodeOrder<G extends {
210
+ role: "lock" | "type";
211
+ scriptHash: string;
212
+ }>(groups: G[]): G[];
213
+ /** A group the node would count as a failure: a non-zero exit, a VM stop, a refused Type ID. */
214
+ export declare const rejects: (g: {
215
+ result: number;
216
+ error?: string;
217
+ }) => boolean;
218
+ /**
219
+ * Where the node would stop, for a transaction some group rejects: the first
220
+ * failing group in nodeOrder, and the groups the node reaches (every one up to
221
+ * and including it). Undefined when every group accepts.
222
+ *
223
+ * With `limit` (the node's cycle limit for the transaction), a group at which
224
+ * the running sum of cycles in nodeOrder passes the limit also stops the node
225
+ * (exceeded max cycles), even though it accepts on its own here: the node gives
226
+ * each group only what the ones before it left. `overLimit` says that is why.
227
+ */
228
+ export declare function nodeStop<G extends {
229
+ role: "lock" | "type";
230
+ scriptHash: string;
231
+ result: number;
232
+ error?: string;
233
+ cycles?: bigint;
234
+ }>(groups: G[], limit?: bigint): {
235
+ stopsAt: G;
236
+ reaches: Set<G>;
237
+ overLimit?: boolean;
238
+ } | undefined;
239
+ /**
240
+ * The line(s) a profile's table ends its groups with: the sum. For a transaction
241
+ * every group accepts it is what the node spends, and the CLI checks it against
242
+ * the node. When a group rejects, the node would stop at the first failing group
243
+ * in its order and report no cycles, so the sum is labelled as what it is: every
244
+ * group run on its own. `name` cleans script names for the output (termLine).
245
+ */
246
+ export declare function sumLines(p: Profile, name?: (s: string) => string): string[];
247
+ /**
248
+ * What a transaction whose groups add up to more than the node's limit for one
249
+ * transaction is told: the CLI, a report and the web page say the same.
250
+ */
251
+ export declare function overTxLimitLines(o: {
252
+ limit: bigint | number;
253
+ sum: bigint | number;
254
+ }): string[];
158
255
  /** How a profile's total compares with the node's: what the CLI and the web page both print. */
159
256
  export declare function nodeVerdict(p: Profile): "match" | "mismatch" | "not recorded" | undefined;
160
257
  /**
package/dist/replay.js CHANGED
@@ -9,6 +9,13 @@ import { ccc } from "@ckb-ccc/core";
9
9
  import { cccA } from "@ckb-ccc/core/advanced";
10
10
  import { groupScripts, knownScriptNames } from "./analyze.js";
11
11
  import { buildMockTransaction, httpRpc, isCellbase, mockFromTransaction, nodeConsensus, scriptVersionAt, } from "./mock.js";
12
+ /**
13
+ * The node's cycle limit for one transaction when its consensus is not known:
14
+ * `max_block_cycles` on mainnet and testnet. The node verifies a transaction's
15
+ * groups one after another under this one budget (each gets what the ones
16
+ * before it left), where ckb-debugger gives each group all of it.
17
+ */
18
+ export const DEFAULT_TX_CYCLE_LIMIT = 3500000000n;
12
19
  /**
13
20
  * Type ID is verified natively by the node rather than by a script binary, so
14
21
  * ckb-debugger cannot run it. The node charges it a fixed cost (TYPE_ID_CYCLES
@@ -177,6 +184,7 @@ export async function planReplay(target, options = {}) {
177
184
  ]);
178
185
  const { mock, cycles, status, epoch } = fetched;
179
186
  const network = consensus?.network ?? options.network ?? "testnet";
187
+ const txCycleLimit = options.txCycleLimit ?? consensus?.maxBlockCycles;
180
188
  const groups = groupsOf(mock);
181
189
  // Known-script names are CCC's table for the network, through the same node;
182
190
  // a network CCC does not know (a devnet) names its scripts with `scriptNames`.
@@ -202,6 +210,7 @@ export async function planReplay(target, options = {}) {
202
210
  (epoch === undefined ? 2 : scriptVersionAt(consensus?.vmEpochs ?? (network === "mainnet" ? "mainnet" : "testnet"), epoch)),
203
211
  network,
204
212
  rpcUrl,
213
+ ...(txCycleLimit !== undefined && { txCycleLimit }),
205
214
  };
206
215
  }
207
216
  /**
@@ -246,15 +255,112 @@ export async function replayGroups(plan, run, options) {
246
255
  const out = await run(debuggerArgs(options.txFile, selector, plan.scriptVersion));
247
256
  profiled.push({ ...g, selector, ...(typeof out === "string" ? parseDebuggerOutput(out) : parseDebuggerRun(out)) });
248
257
  }
258
+ const limit = plan.txCycleLimit ?? DEFAULT_TX_CYCLE_LIMIT;
259
+ const stop = nodeStop(profiled, limit);
260
+ if (stop)
261
+ for (const g of profiled)
262
+ g.nodeReaches = stop.reaches.has(g);
263
+ const measuredCycles = profiled.reduce((sum, g) => sum + g.cycles, 0n);
249
264
  return {
250
265
  hash: plan.hash,
251
266
  status: plan.status,
252
267
  ...(plan.nodeCycles !== undefined && { nodeCycles: plan.nodeCycles }),
253
- measuredCycles: profiled.reduce((sum, g) => sum + g.cycles, 0n),
268
+ measuredCycles,
269
+ ...(stop && { nodeStopsAt: stop.stopsAt.selector }),
270
+ ...(measuredCycles > limit && { overTxLimit: { limit, sum: measuredCycles } }),
254
271
  groups: profiled,
255
272
  env: { debugger: options.debugger, network: plan.network, rpcUrl: plan.rpcUrl, scriptVersion: plan.scriptVersion },
256
273
  };
257
274
  }
275
+ /**
276
+ * The groups in the order the node verifies them: every lock group, then every
277
+ * type group, each set sorted by script hash (byte order, the same as the order
278
+ * of the lowercase hex). ckb-script keeps them in two BTreeMaps keyed by script
279
+ * hash and chains locks before types (TxData::new and TxInfo::groups in
280
+ * script/src/types.rs); TransactionScriptsVerifier::verify (script/src/verify.rs)
281
+ * runs them in that order and returns at the first error, with no cycles.
282
+ * Groups are listed elsewhere in first-appearance order, which is not this one.
283
+ */
284
+ export function nodeOrder(groups) {
285
+ const rank = (g) => (g.role === "lock" ? 0 : 1);
286
+ return [...groups].sort((a, b) => {
287
+ if (rank(a) !== rank(b))
288
+ return rank(a) - rank(b);
289
+ const x = a.scriptHash.toLowerCase();
290
+ const y = b.scriptHash.toLowerCase();
291
+ return x < y ? -1 : x > y ? 1 : 0;
292
+ });
293
+ }
294
+ /** A group the node would count as a failure: a non-zero exit, a VM stop, a refused Type ID. */
295
+ export const rejects = (g) => g.result !== 0 || g.error !== undefined;
296
+ /**
297
+ * Where the node would stop, for a transaction some group rejects: the first
298
+ * failing group in nodeOrder, and the groups the node reaches (every one up to
299
+ * and including it). Undefined when every group accepts.
300
+ *
301
+ * With `limit` (the node's cycle limit for the transaction), a group at which
302
+ * the running sum of cycles in nodeOrder passes the limit also stops the node
303
+ * (exceeded max cycles), even though it accepts on its own here: the node gives
304
+ * each group only what the ones before it left. `overLimit` says that is why.
305
+ */
306
+ export function nodeStop(groups, limit) {
307
+ const ordered = nodeOrder(groups);
308
+ let sum = 0n;
309
+ for (const [at, g] of ordered.entries()) {
310
+ sum += g.cycles ?? 0n;
311
+ const over = limit !== undefined && sum > limit;
312
+ if (rejects(g) || over) {
313
+ return { stopsAt: g, reaches: new Set(ordered.slice(0, at + 1)), ...(!rejects(g) && { overLimit: true }) };
314
+ }
315
+ }
316
+ return undefined;
317
+ }
318
+ /**
319
+ * The line(s) a profile's table ends its groups with: the sum. For a transaction
320
+ * every group accepts it is what the node spends, and the CLI checks it against
321
+ * the node. When a group rejects, the node would stop at the first failing group
322
+ * in its order and report no cycles, so the sum is labelled as what it is: every
323
+ * group run on its own. `name` cleans script names for the output (termLine).
324
+ */
325
+ export function sumLines(p, name = (s) => s) {
326
+ const n = (x) => x.toLocaleString("en-US");
327
+ if (p.nodeStopsAt === undefined)
328
+ return [`measured (sum of groups) ${n(p.measuredCycles)}`];
329
+ const stop = p.groups.find((g) => g.selector === p.nodeStopsAt);
330
+ const label = stop?.name ? `${p.nodeStopsAt} (${name(stop.name)})` : p.nodeStopsAt;
331
+ const out = p.overTxLimit && stop && !rejects(stop)
332
+ ? [
333
+ `sum of groups, each run on its own ${n(p.measuredCycles)}`,
334
+ ...overTxLimitLines(p.overTxLimit).map((l) => ` ${l}`),
335
+ ` ckb-debugger gives each group the whole limit; the node gives each group what the`,
336
+ ` ones before it left, so it stops at ${label}, where the running sum passes it`,
337
+ ` (it runs lock groups, then type groups, each in script-hash order)`,
338
+ ]
339
+ : [
340
+ `sum of groups, each run on its own ${n(p.measuredCycles)}`,
341
+ ` not what the node spends: it stops at the first failing group, ${label},`,
342
+ ` and reports no cycles for a rejected tx (it runs lock groups, then type groups,`,
343
+ ` each in script-hash order)`,
344
+ ...(p.overTxLimit ? [` the sum is also over the node's per-transaction limit of ${n(p.overTxLimit.limit)} cycles`] : []),
345
+ ];
346
+ const skipped = p.groups.filter((g) => g.nodeReaches === false).map((g) => g.selector);
347
+ if (skipped.length) {
348
+ const shown = skipped.slice(0, 8).join(", ");
349
+ out.push(` the node never runs: ${shown}${skipped.length > 8 ? `, and ${skipped.length - 8} more` : ""}`);
350
+ }
351
+ return out;
352
+ }
353
+ /**
354
+ * What a transaction whose groups add up to more than the node's limit for one
355
+ * transaction is told: the CLI, a report and the web page say the same.
356
+ */
357
+ export function overTxLimitLines(o) {
358
+ const n = (x) => x.toLocaleString("en-US");
359
+ return [
360
+ `over the node's per-transaction limit of ${n(o.limit)} cycles: the node would reject`,
361
+ `this transaction (exceeded max cycles)`,
362
+ ];
363
+ }
258
364
  /** How a profile's total compares with the node's: what the CLI and the web page both print. */
259
365
  export function nodeVerdict(p) {
260
366
  if (p.nodeCycles !== undefined)
package/dist/report.d.ts CHANGED
@@ -1,6 +1,8 @@
1
- import type { CycleReport, GroupResult, ScenarioResult } from "./diff.js";
1
+ import { SERVER_EXIT_NOTE } from "./children.js";
2
+ import type { ChildResult, CycleReport, GroupResult, ReportManifest, ScenarioResult } from "./diff.js";
2
3
  import { type RpcOutPoint } from "./mock.js";
3
- import { type Profile, type ProfileOptions } from "./profile.js";
4
+ import { type FlameChild, type Profile, type ProfileOptions } from "./profile.js";
5
+ export { SERVER_EXIT_NOTE };
4
6
  export interface BinaryConfig {
5
7
  /** The built binary, relative to the config file. */
6
8
  path: string;
@@ -64,6 +66,17 @@ export interface ReportOptions {
64
66
  onScenario?: (s: ScenarioResult) => void;
65
67
  /** Runs ckb-debugger instead of the native binary (see ProfileOptions): for tests. */
66
68
  runDebugger?: ProfileOptions["runDebugger"];
69
+ /**
70
+ * With `runDebugger`: what it runs, for the manifest (a WebAssembly build's
71
+ * id, or a native binary's sha256). Without `runDebugger`, the binary that
72
+ * runs is hashed instead.
73
+ */
74
+ debuggerBuild?: {
75
+ wasm?: string;
76
+ sha256?: string;
77
+ };
78
+ /** Free text for the manifest: the compiler and build profile the binaries were built with (e.g. `rustc -V`). */
79
+ toolchain?: string;
67
80
  }
68
81
  export declare function groupResults(p: Profile): GroupResult[];
69
82
  /**
@@ -74,7 +87,10 @@ export declare function groupResults(p: Profile): GroupResult[];
74
87
  export declare function runReport(configPath: string, options?: ReportOptions): Promise<CycleReport & {
75
88
  errors: string[];
76
89
  notices?: string[];
90
+ manifest: ReportManifest;
77
91
  }>;
92
+ /** A spawned script's profile as a report records it: profiled frames, never a share of the group's total. */
93
+ export declare function childResult(c: FlameChild): ChildResult;
78
94
  /**
79
95
  * `file` with its symlinks resolved, when it is inside one of `roots` (already
80
96
  * resolved); an error otherwise. A file that does not exist is checked by its
package/dist/report.js CHANGED
@@ -16,12 +16,16 @@
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, spawnNote, termLine, termLines, unmatchedError } from "./diff.js";
19
+ import { SERVER_EXIT_NOTE } from "./children.js";
20
+ import { checkDebuggerPath, resolveDebugger } from "./debugger.js";
21
+ import { FUNCTION_LIMIT, overTxLimitOf, resultLabel, spawnNote, termLine, termLines, unmatchedError } from "./diff.js";
20
22
  import { foldedTotal, functionCosts } from "./flame.js";
21
23
  import { buildMockTransaction, httpRpc, mockFromTransaction, scriptVersionAt } from "./mock.js";
24
+ import { fileDigest, gitCommit, inputsHash, scenarioInputs, sha256, toolchainOf, toolIdentity } from "./manifest.js";
22
25
  import { flameTransaction, parseNames, profileTransaction, readTransactionJson } from "./profile.js";
23
- import { consensusOf, nodeVerdict, PUBLIC_RPC } from "./replay.js";
26
+ import { consensusOf, nodeVerdict, overTxLimitLines, PUBLIC_RPC } from "./replay.js";
24
27
  import { substituteBinaries, targetScriptKeys } from "./substitute.js";
28
+ export { SERVER_EXIT_NOTE };
25
29
  const HASH = /^0x[0-9a-fA-F]{64}$/;
26
30
  /** Parses and checks a cycles.json, with a message that says what to fix. */
27
31
  export function parseCyclesConfig(json) {
@@ -99,6 +103,7 @@ export function groupResults(p) {
99
103
  result: g.result,
100
104
  ...(g.error && { error: g.error }),
101
105
  cycles: Number(g.cycles),
106
+ ...(g.nodeReaches !== undefined && { nodeReaches: g.nodeReaches }),
102
107
  }));
103
108
  }
104
109
  /**
@@ -123,6 +128,27 @@ export async function runReport(configPath, options = {}) {
123
128
  const rpcUrl = options.rpcUrl ?? (roots ? undefined : config.rpc);
124
129
  const baseNames = parseNames(Object.entries(config.names ?? {}).map(([k, v]) => `${k}=${v}`));
125
130
  const inside = (file) => confine(file, roots);
131
+ // The manifest: what the numbers were measured with. The debugger is resolved
132
+ // once, so the binary hashed is the one every scenario runs; when it cannot be
133
+ // found, each scenario says so as before.
134
+ let debuggerPath;
135
+ if (!options.runDebugger) {
136
+ try {
137
+ debuggerPath = options.debuggerPath ? checkDebuggerPath(options.debuggerPath) : resolveDebugger();
138
+ }
139
+ catch {
140
+ debuggerPath = undefined;
141
+ }
142
+ }
143
+ const environment = Promise.all([
144
+ toolIdentity(),
145
+ debuggerPath ? fileDigest(debuggerPath).catch(() => undefined) : Promise.resolve(undefined),
146
+ gitCommit(dir),
147
+ options.binariesRoot ? gitCommit(binDir) : Promise.resolve(undefined),
148
+ ]);
149
+ const scenarioManifests = [];
150
+ /** Binary name → what was read: for the manifest of the scenarios it is swapped into. */
151
+ const digests = new Map();
126
152
  // A binary resolves to null when it is absent from another checkout (the
127
153
  // base, --binaries-from): that binary is new there, not broken. Only it, and
128
154
  // the groups whose code it is, count as new; every other binary still swaps in.
@@ -133,12 +159,15 @@ export async function runReport(configPath, options = {}) {
133
159
  const b = config.binaries[name];
134
160
  p = inside(resolve(binDir, b.path))
135
161
  .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) => {
162
+ .then((data) => {
163
+ digests.set(name, { path: b.path, sha256: sha256(data), size: data.length });
164
+ return {
165
+ name,
166
+ data: new Uint8Array(data),
167
+ ...(b.outPoint && { outPoint: b.outPoint }),
168
+ ...(b.codeHash && { codeHash: b.codeHash }),
169
+ };
170
+ }, (e) => {
142
171
  if (options.binariesRoot && e.code === "ENOENT") {
143
172
  missingNames.add(name);
144
173
  return null;
@@ -155,6 +184,11 @@ export async function runReport(configPath, options = {}) {
155
184
  const scenarios = [];
156
185
  for (const s of config.scenarios) {
157
186
  let result;
187
+ const sm = { name: s.name };
188
+ const entry = scenarioInputs(s, network);
189
+ // A tx scenario has no file: its entry (the hash) is its input.
190
+ if (s.tx !== undefined)
191
+ sm.inputs = inputsHash(entry);
158
192
  try {
159
193
  const explicit = s.binaries !== undefined;
160
194
  const names = explicit ? s.binaries : Object.keys(config.binaries ?? {});
@@ -169,7 +203,8 @@ export async function runReport(configPath, options = {}) {
169
203
  ...(network && { network }),
170
204
  ...(rpcUrl && { rpcUrl }),
171
205
  ...(options.timeoutMs !== undefined && { timeoutMs: options.timeoutMs }),
172
- ...(options.debuggerPath && { debuggerPath: options.debuggerPath }),
206
+ // The binary the manifest hashed; an explicit path that does not check out fails each scenario, as before.
207
+ ...((debuggerPath ?? options.debuggerPath) && { debuggerPath: debuggerPath ?? options.debuggerPath }),
173
208
  ...(options.runDebugger && { runDebugger: options.runDebugger }),
174
209
  ...(s.scriptVersion !== undefined && { scriptVersion: s.scriptVersion }),
175
210
  };
@@ -185,13 +220,19 @@ export async function runReport(configPath, options = {}) {
185
220
  const rpc = () => httpRpc(url, { ...(options.timeoutMs !== undefined && { timeoutMs: options.timeoutMs }) });
186
221
  let mock;
187
222
  let scriptVersion = s.scriptVersion;
223
+ // The node's own limit for one transaction, when its consensus is fetched here.
224
+ let txCycleLimit;
188
225
  if (s.mock) {
189
- mock = JSON.parse(await readFile(await inside(resolve(dir, s.mock)), "utf8"));
226
+ const bytes = await readFile(await inside(resolve(dir, s.mock)));
227
+ sm.inputs = inputsHash(entry, bytes);
228
+ mock = JSON.parse(bytes.toString("utf8"));
190
229
  if (!mock?.mock_info || !mock.tx)
191
230
  throw new Error(`${s.mock} is not a ckb-debugger mock transaction`);
192
231
  }
193
232
  else if (s.file) {
194
- mock = await mockFromTransaction(rpc(), readTransactionJson(await readFile(await inside(resolve(dir, s.file)), "utf8")));
233
+ const bytes = await readFile(await inside(resolve(dir, s.file)));
234
+ sm.inputs = inputsHash(entry, bytes);
235
+ mock = await mockFromTransaction(rpc(), readTransactionJson(bytes.toString("utf8")));
195
236
  }
196
237
  else {
197
238
  const node = rpc();
@@ -200,6 +241,7 @@ export async function runReport(configPath, options = {}) {
200
241
  consensusOf(node, url, { ...(network && { network }), ...(rpcUrl && { rpcUrl }) }),
201
242
  ]);
202
243
  mock = built.mock;
244
+ txCycleLimit = consensus?.maxBlockCycles;
203
245
  if (scriptVersion === undefined && built.epoch !== undefined) {
204
246
  scriptVersion = scriptVersionAt(consensus?.vmEpochs ?? network ?? "testnet", built.epoch);
205
247
  }
@@ -231,16 +273,21 @@ export async function runReport(configPath, options = {}) {
231
273
  profile = await profileTransaction(swapped, {
232
274
  ...profileOptions,
233
275
  ...(scriptVersion !== undefined && { scriptVersion }),
276
+ ...(txCycleLimit !== undefined && { txCycleLimit }),
234
277
  scriptNames,
235
278
  });
236
279
  // The scripts whose code is a binary with symbols, when the scenario asks for functions.
237
280
  const symbols = new Map();
281
+ // The same builds by data hash: a spawned script is found by the binary it ran, not by a group.
282
+ const vmSymbols = new Map();
238
283
  if (s.functions ?? config.functions) {
239
284
  for (const a of applied) {
240
285
  const sym = config.binaries[a.name].symbols;
241
286
  if (sym)
242
287
  for (const k of a.scriptKeys)
243
288
  symbols.set(k.toLowerCase(), sym);
289
+ if (sym)
290
+ vmSymbols.set(a.newDataHash.toLowerCase(), sym);
244
291
  }
245
292
  }
246
293
  if (symbols.size) {
@@ -248,10 +295,18 @@ export async function runReport(configPath, options = {}) {
248
295
  await recordFunctions(groups, swapped, symbols, binDir, inside, {
249
296
  ...profileOptions,
250
297
  ...(scriptVersion !== undefined && { scriptVersion }),
251
- });
298
+ scriptNames,
299
+ }, vmSymbols);
252
300
  }
253
301
  }
254
302
  debuggerVersion ??= profile.env.debugger;
303
+ sm.scriptVersion = profile.env.scriptVersion;
304
+ const swappedIn = substituted.flatMap((name) => {
305
+ const d = digests.get(name);
306
+ return d ? [{ name, ...d }] : [];
307
+ });
308
+ if (swappedIn.length)
309
+ sm.binaries = swappedIn;
255
310
  // Kept with the scenario: numbers that do not add up to the node's are not to be trusted.
256
311
  if (nodeVerdict(profile) === "mismatch") {
257
312
  warnings.push(`the groups add up to ${profile.measuredCycles} cycles, the node reported ${profile.nodeCycles}`);
@@ -269,12 +324,17 @@ export async function runReport(configPath, options = {}) {
269
324
  ...(substituted.length && { substituted }),
270
325
  ...(unmatched.length && { unmatched }),
271
326
  ...(missing.length && { binaryMissing: true, missingBinaries: missing }),
327
+ ...(profile.nodeStopsAt !== undefined && { nodeStopsAt: profile.nodeStopsAt }),
328
+ ...(profile.overTxLimit && {
329
+ overTxLimit: { limit: Number(profile.overTxLimit.limit), sum: Number(profile.overTxLimit.sum) },
330
+ }),
272
331
  };
273
332
  }
274
333
  catch (e) {
275
334
  result = { name: s.name, error: e instanceof Error ? e.message : String(e), groups: [] };
276
335
  }
277
336
  scenarios.push(result);
337
+ scenarioManifests.push(sm);
278
338
  options.onScenario?.(result);
279
339
  }
280
340
  const errors = scenarios.filter((s) => s.error).map((s) => `scenario "${s.name}": ${s.error}`);
@@ -287,6 +347,22 @@ export async function runReport(configPath, options = {}) {
287
347
  errors.push(unmatchedError(name));
288
348
  }
289
349
  }
350
+ const [tool, debuggerDigest, commit, binariesCommit] = await environment;
351
+ const toolchain = toolchainOf(options.toolchain);
352
+ const manifest = {
353
+ schema: 1,
354
+ tool,
355
+ debugger: {
356
+ version: debuggerVersion ?? "unknown",
357
+ ...(debuggerPath && { path: debuggerPath }),
358
+ ...(debuggerDigest && { sha256: debuggerDigest.sha256 }),
359
+ ...(options.runDebugger && options.debuggerBuild?.sha256 && { sha256: options.debuggerBuild.sha256 }),
360
+ ...(options.runDebugger && options.debuggerBuild?.wasm && { wasm: options.debuggerBuild.wasm }),
361
+ },
362
+ ...((commit || binariesCommit) && { git: { ...(commit && { commit }), ...(binariesCommit && { binaries: binariesCommit }) } }),
363
+ ...(toolchain && { toolchain }),
364
+ scenarios: scenarioManifests,
365
+ };
290
366
  return {
291
367
  tool: "ckb-cycles",
292
368
  version: 1,
@@ -294,6 +370,7 @@ export async function runReport(configPath, options = {}) {
294
370
  scenarios,
295
371
  errors,
296
372
  ...(notices.length && { notices }),
373
+ manifest,
297
374
  };
298
375
  }
299
376
  /**
@@ -302,7 +379,7 @@ export async function runReport(configPath, options = {}) {
302
379
  * of that group with that build. A build that does not cost the same cycles as
303
380
  * the binary it stands for is not the same code: the group gets a warning instead.
304
381
  */
305
- async function recordFunctions(groups, mock, symbols, binDir, inside, options) {
382
+ async function recordFunctions(groups, mock, symbols, binDir, inside, options, vmSymbolPaths = new Map()) {
306
383
  const fmt = (n) => n.toLocaleString("en-US");
307
384
  const symbolsOf = (g) => symbols.get(`${g.codeHash}:${g.hashType}`.toLowerCase());
308
385
  const wanted = groups.filter((g) => symbolsOf(g));
@@ -326,11 +403,19 @@ async function recordFunctions(groups, mock, symbols, binDir, inside, options) {
326
403
  const run = wanted.filter((g) => bins[g.codeHash]);
327
404
  if (!run.length)
328
405
  return;
406
+ // Builds for spawned scripts: one that cannot be read leaves that child's frames unnamed (said on the child).
407
+ const vmSymbols = {};
408
+ for (const [hash, sym] of vmSymbolPaths) {
409
+ const file = await inside(resolve(binDir, sym)).catch(() => undefined);
410
+ if (file && (await stat(file).then((st) => st.isFile(), () => false)))
411
+ vmSymbols[hash] = file;
412
+ }
329
413
  let flame;
330
414
  try {
331
415
  flame = await flameTransaction(mock, {
332
416
  ...options,
333
417
  bins,
418
+ vmSymbols,
334
419
  scripts: run.map((g) => g.selector),
335
420
  readFile: (path) => readFile(path, "utf8"),
336
421
  });
@@ -359,9 +444,34 @@ async function recordFunctions(groups, mock, symbols, binDir, inside, options) {
359
444
  g.functions = functionCosts(f.folded)
360
445
  .slice(0, FUNCTION_LIMIT)
361
446
  .map((c) => ({ name: c.name, self: Number(c.self), total: Number(c.total) }));
447
+ // Only beside a profile that checked out: the children were replayed with the same build.
448
+ if (f.children?.length)
449
+ g.children = f.children.map(childResult);
450
+ if (f.childrenNote)
451
+ g.childrenNote = f.childrenNote;
362
452
  }
363
453
  }
364
454
  }
455
+ /** A spawned script's profile as a report records it: profiled frames, never a share of the group's total. */
456
+ export function childResult(c) {
457
+ const script = c.name ?? (c.codeHash ? short(c.codeHash) : c.locator);
458
+ const base = { vm: c.vm, parent: c.parent, script, ...(c.codeHash && { codeHash: c.codeHash }) };
459
+ if (c.failed)
460
+ return { ...base, failed: c.failed };
461
+ const out = { ...base, result: c.result, ...(c.error && { error: c.error }), profiled: Number(foldedTotal(c.folded)) };
462
+ if (c.symbolsWarning)
463
+ out.functionsWarning = c.symbolsWarning;
464
+ else if (!c.symbols)
465
+ out.functionsWarning = "no symbols build for the binary it ran: its frames have no names";
466
+ else if (!c.folded.size)
467
+ out.functionsWarning = "the profiler recorded no stacks";
468
+ else {
469
+ out.functions = functionCosts(c.folded)
470
+ .slice(0, FUNCTION_LIMIT)
471
+ .map((f) => ({ name: f.name, self: Number(f.self), total: Number(f.total) }));
472
+ }
473
+ return out;
474
+ }
365
475
  /**
366
476
  * `file` with its symlinks resolved, when it is inside one of `roots` (already
367
477
  * resolved); an error otherwise. A file that does not exist is checked by its
@@ -402,6 +512,19 @@ async function realParent(p) {
402
512
  return join(await realParent(parent), basename(p));
403
513
  }
404
514
  }
515
+ /** One spawned script under its group in the terminal report. */
516
+ function childReportLine(c, groupOk = false) {
517
+ const fmt = (n) => n.toLocaleString("en-US");
518
+ const t = termLine;
519
+ const head = `spawned vm ${c.vm} (${t(c.script)})`;
520
+ if (c.failed || c.result === undefined)
521
+ return `${head}: not profiled: ${t(c.failed ?? "no result")}`;
522
+ const result = resultLabel({ result: c.result, ...(c.error && { error: c.error }) });
523
+ const top = (c.functions ?? []).slice(0, 3).map((f) => `${t(f.name)} ${fmt(f.self)}`);
524
+ return (`${head}: ${t(result)}, ${fmt(c.profiled ?? 0)} cycles in its profiled frames` +
525
+ (top.length ? `; costliest (self): ${top.join(", ")}` : c.functionsWarning ? `; functions not recorded: ${t(c.functionsWarning)}` : "") +
526
+ (groupOk && (c.error || c.result !== 0) ? ` (${SERVER_EXIT_NOTE})` : ""));
527
+ }
405
528
  /**
406
529
  * The report for a terminal: one block per scenario. Every name, warning and
407
530
  * error from the config, the build or the debugger goes through termLine (or
@@ -433,6 +556,28 @@ export function formatReport(report) {
433
556
  for (const g of s.groups.filter((g) => g.error))
434
557
  out.push("", `${t(s.name)} ${t(g.selector)} stopped: ${t(g.error)}`);
435
558
  }
559
+ // A rejected scenario's cycles are each group on its own: not what the node spends.
560
+ for (const s of report.scenarios) {
561
+ if (s.nodeStopsAt === undefined)
562
+ continue;
563
+ const stop = s.groups.find((g) => g.selector === s.nodeStopsAt);
564
+ const skipped = s.groups.filter((g) => g.nodeReaches === false).map((g) => t(g.selector));
565
+ const at = `${t(s.nodeStopsAt)}${stop ? ` (${t(stop.script)})` : ""}`;
566
+ const limit = overTxLimitOf(s);
567
+ if (limit && stop && resultLabel(stop) === "ok") {
568
+ out.push("", `${t(s.name)}: ${overTxLimitLines(limit).join(" ")}. Each group above ran on its own with the whole limit; ` +
569
+ `the node gives each group what the ones before it left, and stops at ${at}, where the running sum passes it`);
570
+ }
571
+ else {
572
+ out.push("", `${t(s.name)}: rejected. Each group above ran on its own; the node stops at the first failing group, ` +
573
+ `${at}, and reports no cycles for a rejected tx`);
574
+ if (limit)
575
+ out.push(` the sum of its groups is also over the node's per-transaction limit of ${limit.limit.toLocaleString("en-US")} cycles`);
576
+ }
577
+ if (skipped.length) {
578
+ out.push(` the node never runs: ${skipped.slice(0, 8).join(", ")}${skipped.length > 8 ? `, and ${skipped.length - 8} more` : ""}`);
579
+ }
580
+ }
436
581
  for (const s of report.scenarios) {
437
582
  for (const g of s.groups) {
438
583
  if (g.functionsWarning)
@@ -443,9 +588,26 @@ export function formatReport(report) {
443
588
  if (g.spawned)
444
589
  out.push(` ${spawnNote({ ...g, head: g })}`);
445
590
  }
591
+ for (const c of g.children ?? [])
592
+ out.push(` ${childReportLine(c, g.result === 0 && !g.error)}`);
593
+ if (g.childrenNote)
594
+ out.push(` spawned scripts: ${t(g.childrenNote)}`);
446
595
  }
447
596
  }
448
597
  if (report.debugger)
449
598
  out.push("", t(report.debugger));
599
+ const m = report.manifest;
600
+ if (m) {
601
+ const parts = [
602
+ ...(m.debugger?.sha256 ? [`debugger sha256 ${m.debugger.sha256}`] : []),
603
+ ...(m.debugger?.wasm ? [`debugger wasm ${m.debugger.wasm}`] : []),
604
+ ...(m.git?.commit ? [`commit ${m.git.commit}`] : []),
605
+ ...(m.git?.binaries && m.git.binaries !== m.git.commit ? [`binaries from ${m.git.binaries}`] : []),
606
+ ...(m.toolchain ? [`toolchain: ${m.toolchain}`] : []),
607
+ ...(m.tool?.version ? [`${m.tool.name} ${m.tool.version}`] : []),
608
+ ];
609
+ if (parts.length)
610
+ out.push(t(`manifest: ${parts.join(" · ")}`));
611
+ }
450
612
  return out.join("\n");
451
613
  }