ckb-cycle-tools 1.2.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/CHANGELOG.md +118 -0
- package/README.md +135 -6
- package/dist/cli.js +26 -7
- package/dist/diff.d.ts +157 -2
- package/dist/diff.js +226 -12
- package/dist/index.d.ts +4 -3
- package/dist/index.js +3 -2
- package/dist/manifest.d.ts +37 -0
- package/dist/manifest.js +107 -0
- package/dist/mock.d.ts +5 -0
- package/dist/mock.js +2 -1
- package/dist/replay.d.ts +98 -1
- package/dist/replay.js +107 -1
- package/dist/report.d.ts +13 -1
- package/dist/report.js +116 -11
- package/package.json +3 -2
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
|
-
/**
|
|
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
|
|
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,4 +1,4 @@
|
|
|
1
|
-
import type { CycleReport, GroupResult, ScenarioResult } from "./diff.js";
|
|
1
|
+
import type { CycleReport, GroupResult, ReportManifest, ScenarioResult } from "./diff.js";
|
|
2
2
|
import { type RpcOutPoint } from "./mock.js";
|
|
3
3
|
import { type Profile, type ProfileOptions } from "./profile.js";
|
|
4
4
|
export interface BinaryConfig {
|
|
@@ -64,6 +64,17 @@ export interface ReportOptions {
|
|
|
64
64
|
onScenario?: (s: ScenarioResult) => void;
|
|
65
65
|
/** Runs ckb-debugger instead of the native binary (see ProfileOptions): for tests. */
|
|
66
66
|
runDebugger?: ProfileOptions["runDebugger"];
|
|
67
|
+
/**
|
|
68
|
+
* With `runDebugger`: what it runs, for the manifest (a WebAssembly build's
|
|
69
|
+
* id, or a native binary's sha256). Without `runDebugger`, the binary that
|
|
70
|
+
* runs is hashed instead.
|
|
71
|
+
*/
|
|
72
|
+
debuggerBuild?: {
|
|
73
|
+
wasm?: string;
|
|
74
|
+
sha256?: string;
|
|
75
|
+
};
|
|
76
|
+
/** Free text for the manifest: the compiler and build profile the binaries were built with (e.g. `rustc -V`). */
|
|
77
|
+
toolchain?: string;
|
|
67
78
|
}
|
|
68
79
|
export declare function groupResults(p: Profile): GroupResult[];
|
|
69
80
|
/**
|
|
@@ -74,6 +85,7 @@ export declare function groupResults(p: Profile): GroupResult[];
|
|
|
74
85
|
export declare function runReport(configPath: string, options?: ReportOptions): Promise<CycleReport & {
|
|
75
86
|
errors: string[];
|
|
76
87
|
notices?: string[];
|
|
88
|
+
manifest: ReportManifest;
|
|
77
89
|
}>;
|
|
78
90
|
/**
|
|
79
91
|
* `file` with its symlinks resolved, when it is inside one of `roots` (already
|
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 {
|
|
19
|
+
import { checkDebuggerPath, resolveDebugger } from "./debugger.js";
|
|
20
|
+
import { FUNCTION_LIMIT, overTxLimitOf, resultLabel, spawnNote, termLine, termLines, unmatchedError } from "./diff.js";
|
|
20
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
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
/**
|
|
@@ -433,6 +503,28 @@ export function formatReport(report) {
|
|
|
433
503
|
for (const g of s.groups.filter((g) => g.error))
|
|
434
504
|
out.push("", `${t(s.name)} ${t(g.selector)} stopped: ${t(g.error)}`);
|
|
435
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
|
+
}
|
|
436
528
|
for (const s of report.scenarios) {
|
|
437
529
|
for (const g of s.groups) {
|
|
438
530
|
if (g.functionsWarning)
|
|
@@ -447,5 +539,18 @@ export function formatReport(report) {
|
|
|
447
539
|
}
|
|
448
540
|
if (report.debugger)
|
|
449
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
|
+
}
|
|
450
555
|
return out.join("\n");
|
|
451
556
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ckb-cycle-tools",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "Cycle measurement for CKB on-chain scripts",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ckb",
|
|
@@ -43,7 +43,8 @@
|
|
|
43
43
|
"prepack": "npm run build",
|
|
44
44
|
"pack:check": "node scripts/pack-check.mjs",
|
|
45
45
|
"atlas": "tsx scripts/atlas.ts",
|
|
46
|
-
"compat": "tsx scripts/compat.ts"
|
|
46
|
+
"compat": "tsx scripts/compat.ts",
|
|
47
|
+
"hardfork": "tsx scripts/hardfork-check.ts"
|
|
47
48
|
},
|
|
48
49
|
"devDependencies": {
|
|
49
50
|
"@ckb-js-std/bindings": "^1.0.0",
|