@mikeargento/bitgraph-audit 0.10.0 → 0.11.1
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/LICENSE +1 -1
- package/README.md +7 -5
- package/dist/anchors.js +1 -1
- package/dist/anomalies.js +1 -1
- package/dist/attestation.js +1 -1
- package/dist/audit.d.ts +1 -1
- package/dist/audit.js +2 -2
- package/dist/authority.js +1 -1
- package/dist/aws-nitro-root-ca.js +1 -1
- package/dist/ceilings.js +1 -1
- package/dist/cli.js +54 -8
- package/dist/cli.js.map +1 -1
- package/dist/contents-hash.js +1 -1
- package/dist/export.js +1 -1
- package/dist/exports.js +1 -1
- package/dist/floors.js +1 -1
- package/dist/index.js +1 -1
- package/dist/ingest.js +1 -1
- package/dist/reconstruct.js +1 -1
- package/dist/report-json.js +1 -1
- package/dist/report-md.js +1 -1
- package/dist/rlp.js +1 -1
- package/dist/rollup-schedule.d.ts +58 -0
- package/dist/rollup-schedule.d.ts.map +1 -0
- package/dist/rollup-schedule.js +105 -0
- package/dist/rollup-schedule.js.map +1 -0
- package/dist/settlement-blobs.d.ts +16 -19
- package/dist/settlement-blobs.d.ts.map +1 -1
- package/dist/settlement-blobs.js +37 -17
- package/dist/settlement-blobs.js.map +1 -1
- package/dist/tar.js +1 -1
- package/dist/temporal.js +1 -1
- package/dist/types.js +1 -1
- package/dist/validity.js +1 -1
- package/dist/verify-tiers.js +1 -1
- package/dist/witness.js +1 -1
- package/package.json +3 -3
- package/src/__tests__/anchors.test.ts +1 -1
- package/src/__tests__/base-floor.test.ts +1 -1
- package/src/__tests__/carrier-tree.test.ts +95 -0
- package/src/__tests__/settlement.test.ts +1 -1
- package/src/anchors.ts +1 -1
- package/src/anomalies.ts +1 -1
- package/src/attestation.ts +1 -1
- package/src/audit.ts +2 -2
- package/src/authority.ts +1 -1
- package/src/aws-nitro-root-ca.ts +1 -1
- package/src/ceilings.ts +1 -1
- package/src/cli.ts +54 -8
- package/src/contents-hash.ts +1 -1
- package/src/export.ts +1 -1
- package/src/exports.ts +1 -1
- package/src/floors.ts +1 -1
- package/src/index.ts +1 -1
- package/src/ingest.ts +1 -1
- package/src/reconstruct.ts +1 -1
- package/src/report-json.ts +1 -1
- package/src/report-md.ts +1 -1
- package/src/rlp.ts +1 -1
- package/src/rollup-schedule.ts +105 -0
- package/src/settlement-blobs.ts +41 -21
- package/src/tar.ts +1 -1
- package/src/temporal.ts +1 -1
- package/src/types.ts +1 -1
- package/src/validity.ts +1 -1
- package/src/verify-tiers.ts +1 -1
- package/src/witness.ts +1 -1
package/src/cli.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
// Copyright (c)
|
|
2
|
+
// Copyright (c) 2025-2026 Argento Computing Inc. Licensed under the MIT License. See LICENSE.
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* bitgraph-audit CLI: run the full offline audit pipeline over a bundle
|
|
@@ -17,11 +17,11 @@
|
|
|
17
17
|
* input error (no report was produced).
|
|
18
18
|
*/
|
|
19
19
|
|
|
20
|
-
import { readFileSync } from "node:fs";
|
|
20
|
+
import { closeSync, openSync, readFileSync, readSync } from "node:fs";
|
|
21
21
|
import { mkdir, mkdtemp, stat, writeFile } from "node:fs/promises";
|
|
22
22
|
import { tmpdir } from "node:os";
|
|
23
23
|
import { join, basename } from "node:path";
|
|
24
|
-
import { CARRIER_VERSION_3, baseHeaderFields, parseCarrier, verifyCarrier } from "@mikeargento/bitgraph-verify";
|
|
24
|
+
import { CARRIER_MAGIC, CARRIER_VERSION_3, CARRIER_VERSION_4, baseHeaderFields, buildExport, parseCarrier, verifyCarrier } from "@mikeargento/bitgraph-verify";
|
|
25
25
|
import type { CarrierPayload, VerificationPolicy } from "@mikeargento/bitgraph-verify";
|
|
26
26
|
import { auditToolVersion, computeExitFlags, runAudit } from "./audit.js";
|
|
27
27
|
import { attestationTimestampMs } from "./attestation.js";
|
|
@@ -63,8 +63,9 @@ function helpText(): string {
|
|
|
63
63
|
USAGE_LINE,
|
|
64
64
|
"",
|
|
65
65
|
"The bundle may be a directory, a .tar archive, a .tar.gz/.tgz,",
|
|
66
|
-
"or a single BitGraphed file (bitgraph-carrier/1, /2 or /
|
|
67
|
-
"with its proof inside
|
|
66
|
+
"or a single BitGraphed file (bitgraph-carrier/1, /2, /3 or /4: a file",
|
|
67
|
+
"with its proof inside; /4 is one member of a tree/1 BitGraph, unpacked",
|
|
68
|
+
"as the file and its own bitgraph-export/1): a carrier is unpacked and audited as the",
|
|
68
69
|
"bundle it carries, and its own verdict (TRUE / FALSE / UNDETERMINED,",
|
|
69
70
|
"with the time window) is printed first. The unpacked bundle holds the",
|
|
70
71
|
"committed bytes and proof.json, and the floor: for /1 and /2 the",
|
|
@@ -316,6 +317,41 @@ async function writeCarrierFloor(dir: string, payload: CarrierPayload): Promise<
|
|
|
316
317
|
}
|
|
317
318
|
}
|
|
318
319
|
|
|
320
|
+
/**
|
|
321
|
+
* carrier/4: a tree member's file unpacks as the member and its own
|
|
322
|
+
* bitgraph-export/1, the shape a tree member always travels in, so the audit
|
|
323
|
+
* checks it with verifyExport like any export beside its file. The export
|
|
324
|
+
* carries the floor header (from the anchor's witness or the Base header) and
|
|
325
|
+
* the ceiling in time; the closing anchor and the settlement pointer have no
|
|
326
|
+
* place in export/1 and are judged in the carrier's own verdict printed above.
|
|
327
|
+
*/
|
|
328
|
+
function memberExportOf(payload: CarrierPayload): string {
|
|
329
|
+
const floor = payload.floor;
|
|
330
|
+
const fields = floor.basis === "base-header" ? baseHeaderFields(floor.header) : null;
|
|
331
|
+
const exp = buildExport({
|
|
332
|
+
proof: payload.proof as never,
|
|
333
|
+
tree: { rootDocument: payload.tree!.rootDocument, member: payload.tree!.member },
|
|
334
|
+
floor: floor.basis === "base-header"
|
|
335
|
+
? (fields !== null ? { chain: "base", blockNumber: fields.blockNumber, blockHash: fields.blockHash, header: floor.header.toLowerCase() } : null)
|
|
336
|
+
: { blockNumber: floor.witness.blockNumber, blockHash: floor.witness.blockHash.toLowerCase(), header: floor.witness.headerRlpHex.toLowerCase() },
|
|
337
|
+
ceiling: payload.ceilingInTime?.status === "present" ? (payload.ceilingInTime.sidecar as never) : { status: "pending" },
|
|
338
|
+
settlement: null,
|
|
339
|
+
});
|
|
340
|
+
return JSON.stringify(exp, null, 2) + "\n";
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/** True when the file's last eight bytes are the BitGraphed file's magic. One positioned read. */
|
|
344
|
+
function tailIsCarrier(path: string, size: number): boolean {
|
|
345
|
+
const fd = openSync(path, "r");
|
|
346
|
+
try {
|
|
347
|
+
const tail = Buffer.alloc(CARRIER_MAGIC.length);
|
|
348
|
+
readSync(fd, tail, 0, tail.length, size - tail.length);
|
|
349
|
+
return tail.equals(Buffer.from(CARRIER_MAGIC));
|
|
350
|
+
} finally {
|
|
351
|
+
closeSync(fd);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
319
355
|
async function main(): Promise<number> {
|
|
320
356
|
let parsed: ParsedArgs | "help";
|
|
321
357
|
try {
|
|
@@ -357,7 +393,10 @@ async function main(): Promise<number> {
|
|
|
357
393
|
const info = await stat(parsed.bundlePath);
|
|
358
394
|
const lower = parsed.bundlePath.toLowerCase();
|
|
359
395
|
const isArchive = lower.endsWith(".tar") || lower.endsWith(".tar.gz") || lower.endsWith(".tgz");
|
|
360
|
-
|
|
396
|
+
// A tree member placed in a container is a tar, and its BitGraphed file (carrier/4) is named
|
|
397
|
+
// as one: the carrier's tail decides before the extension does. A bundle tar never ends in it.
|
|
398
|
+
const endsInCarrier = info.isFile() && info.size >= 26 && tailIsCarrier(parsed.bundlePath, info.size);
|
|
399
|
+
if (info.isFile() && (!isArchive || endsInCarrier) && info.size >= 26) {
|
|
361
400
|
const bytes = new Uint8Array(readFileSync(parsed.bundlePath));
|
|
362
401
|
const parsedCarrier = parseCarrier(bytes);
|
|
363
402
|
if (parsedCarrier.kind !== "none") {
|
|
@@ -379,9 +418,11 @@ async function main(): Promise<number> {
|
|
|
379
418
|
// carrier/3 (enclave v10): the floor is a Base block and no closing
|
|
380
419
|
// anchor exists; order after the record is the chain of proof hashes.
|
|
381
420
|
const v3 = verdict.payload?.carrier === CARRIER_VERSION_3 || b?.notBefore.chain === "base";
|
|
421
|
+
const member = verdict.member ?? null;
|
|
382
422
|
const chainWord = b?.notBefore.chain === "base" ? "Base" : "Ethereum";
|
|
383
423
|
const lines = [
|
|
384
424
|
`carrier: ${verdict.verdict}${verdict.carrier === "corrupt" ? " (block unreadable: corrupted, not judged)" : ""}`,
|
|
425
|
+
member ? ` tree member: leaf ${member.index} of ${member.count}${member.placement === "as-is" ? " (kept as is: recorded after the floor, the bytes themselves are not dated)" : ` (${member.placement})`}` : null,
|
|
385
426
|
b ? ` no earlier than: ${chainWord} block ${b.notBefore.blockNumber}${b.notBefore.timestamp !== null ? ` (${v3 ? "stamped" : "mined"} ${new Date(b.notBefore.timestamp * 1000).toISOString()})` : ""}` : null,
|
|
386
427
|
attestedMs !== null ? ` committed: ${new Date(attestedMs).toISOString()} per the enclave platform's signed clock` : null,
|
|
387
428
|
b && v3
|
|
@@ -396,8 +437,13 @@ async function main(): Promise<number> {
|
|
|
396
437
|
const dir = await mkdtemp(join(tmpdir(), "bitgraph-carrier-"));
|
|
397
438
|
const name = basename(parsed.bundlePath).replace(/\.bitgraph(\.[^.]+)$/i, "$1").replace(/\.bitgraph$/i, "") || "artifact";
|
|
398
439
|
await writeFile(join(dir, name), parsedCarrier.inner);
|
|
399
|
-
|
|
400
|
-
|
|
440
|
+
if (parsedCarrier.payload.carrier === CARRIER_VERSION_4) {
|
|
441
|
+
// A tree member: its signed digest names the root document, so it travels as the file and its own export.
|
|
442
|
+
await writeFile(join(dir, `${name}.bitgraph.json`), memberExportOf(parsedCarrier.payload));
|
|
443
|
+
} else {
|
|
444
|
+
await writeFile(join(dir, "proof.json"), JSON.stringify(parsedCarrier.payload.proof, null, 2));
|
|
445
|
+
await writeCarrierFloor(dir, parsedCarrier.payload);
|
|
446
|
+
}
|
|
401
447
|
bundlePath = dir;
|
|
402
448
|
}
|
|
403
449
|
}
|
package/src/contents-hash.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Copyright (c)
|
|
1
|
+
// Copyright (c) 2025-2026 Argento Computing Inc. Licensed under the MIT License. See LICENSE.
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Deterministic bundle contents hash, per docs/BUNDLE-FORMAT.md section 8.
|
package/src/export.ts
CHANGED
package/src/exports.ts
CHANGED
package/src/floors.ts
CHANGED
package/src/index.ts
CHANGED
package/src/ingest.ts
CHANGED
package/src/reconstruct.ts
CHANGED
package/src/report-json.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Copyright (c)
|
|
1
|
+
// Copyright (c) 2025-2026 Argento Computing Inc. Licensed under the MIT License. See LICENSE.
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* JSON report builder: turns an AuditResult into the audit-report.json
|
package/src/report-md.ts
CHANGED
package/src/rlp.ts
CHANGED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* When a rollup's blocks are stamped.
|
|
3
|
+
*
|
|
4
|
+
* Base made one block every two seconds through block 53,556,126, so a
|
|
5
|
+
* block's time was genesis + 2 * number and a timestamp named one block.
|
|
6
|
+
* From block 53,556,127 (stamped 1,793,901,601, 2026-11-05 18:00:01 UTC) it
|
|
7
|
+
* makes five blocks a second. A block's timestamp is still whole seconds, so
|
|
8
|
+
* five consecutive blocks share each one: a timestamp names a second, and a
|
|
9
|
+
* block is one of the five in it.
|
|
10
|
+
*
|
|
11
|
+
* Both facts were read from Base itself: blocks 53,556,127 to 53,556,131 are
|
|
12
|
+
* stamped 1,793,901,601 and block 53,556,132 is stamped 1,793,901,602.
|
|
13
|
+
*
|
|
14
|
+
* Nothing here is trusted for what a record proves. The schedule only says
|
|
15
|
+
* when to look and where a batch sits among its neighbours; what is found is
|
|
16
|
+
* then checked by hash.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
export interface RollupSchedule {
|
|
20
|
+
l2GenesisTime: number;
|
|
21
|
+
/** Seconds per block before `subSecond` starts, and always for a rollup that has none. */
|
|
22
|
+
blockTime: number;
|
|
23
|
+
/** From `fromBlock`, stamped `fromTime`, each whole second holds `blocksPerSecond` blocks. */
|
|
24
|
+
subSecond?: { fromBlock: number; fromTime: number; blocksPerSecond: number };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export const BASE_MAINNET_ROLLUP: RollupSchedule = Object.freeze({
|
|
28
|
+
l2GenesisTime: 1686789347,
|
|
29
|
+
blockTime: 2,
|
|
30
|
+
subSecond: Object.freeze({ fromBlock: 53_556_127, fromTime: 1_793_901_601, blocksPerSecond: 5 }),
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
/** The timestamp the schedule gives block `blockNumber`, in seconds. */
|
|
34
|
+
export function timeOfBlock(blockNumber: number, s: RollupSchedule): number {
|
|
35
|
+
const sub = s.subSecond;
|
|
36
|
+
if (sub && blockNumber >= sub.fromBlock) return sub.fromTime + Math.floor((blockNumber - sub.fromBlock) / sub.blocksPerSecond);
|
|
37
|
+
return s.l2GenesisTime + blockNumber * s.blockTime;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** The blocks stamped `timestamp`: the first one and how many. Null when the schedule stamps no block with it. */
|
|
41
|
+
export function blocksAt(timestamp: number, s: RollupSchedule): { first: number; count: number } | null {
|
|
42
|
+
const sub = s.subSecond;
|
|
43
|
+
if (sub && timestamp >= sub.fromTime) {
|
|
44
|
+
if (!Number.isInteger(timestamp)) return null;
|
|
45
|
+
return { first: sub.fromBlock + (timestamp - sub.fromTime) * sub.blocksPerSecond, count: sub.blocksPerSecond };
|
|
46
|
+
}
|
|
47
|
+
const n = (timestamp - s.l2GenesisTime) / s.blockTime;
|
|
48
|
+
if (!Number.isInteger(n) || n < 0) return null;
|
|
49
|
+
return { first: n, count: 1 };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** The newest block the schedule has stamped by `timestamp` (the head, by the clock alone). */
|
|
53
|
+
export function headAt(timestamp: number, s: RollupSchedule): number {
|
|
54
|
+
const sub = s.subSecond;
|
|
55
|
+
if (sub && timestamp >= sub.fromTime) return sub.fromBlock + (Math.floor(timestamp) - sub.fromTime + 1) * sub.blocksPerSecond - 1;
|
|
56
|
+
return Math.max(0, Math.floor((timestamp - s.l2GenesisTime) / s.blockTime));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Block numbers for the singular batches of one channel, in the channel's
|
|
61
|
+
* order. A singular batch carries its block's timestamp and its parent's
|
|
62
|
+
* hash, never its number. Where a timestamp names one block the number is
|
|
63
|
+
* read from it. Where it names several, the batches sharing it are counted
|
|
64
|
+
* off in order: a channel holds consecutive blocks, so a second in the
|
|
65
|
+
* middle holds all of its blocks, the channel's last second holds the first
|
|
66
|
+
* of its blocks, and the channel's first second holds the last of its
|
|
67
|
+
* blocks.
|
|
68
|
+
*
|
|
69
|
+
* One case has no answer from the channel alone: every batch shares one
|
|
70
|
+
* second and there are fewer than the second holds. `pin` then places them:
|
|
71
|
+
* the index of a batch already identified by hash, and its block's number.
|
|
72
|
+
* Without it they are counted from the second's first block.
|
|
73
|
+
*/
|
|
74
|
+
export function numberSingularBatches(timestamps: readonly number[], s: RollupSchedule, pin?: { index: number; number: number }): number[] {
|
|
75
|
+
const out: number[] = [];
|
|
76
|
+
let i = 0;
|
|
77
|
+
while (i < timestamps.length) {
|
|
78
|
+
const t = timestamps[i]!;
|
|
79
|
+
const at = blocksAt(t, s);
|
|
80
|
+
if (!at) throw new TypeError(`batch timestamp ${t} is not on the rollup's schedule`);
|
|
81
|
+
let j = i;
|
|
82
|
+
while (j < timestamps.length && timestamps[j] === t) j++;
|
|
83
|
+
const run = j - i;
|
|
84
|
+
if (run > at.count) throw new TypeError(`${run} batches are stamped ${t}, which holds ${at.count} block${at.count === 1 ? "" : "s"}`);
|
|
85
|
+
const opens = i === 0;
|
|
86
|
+
const closes = j === timestamps.length;
|
|
87
|
+
let offset = 0;
|
|
88
|
+
if (run < at.count) {
|
|
89
|
+
if (!opens && !closes) throw new TypeError(`${run} batches are stamped ${t} in the middle of a channel, which holds ${at.count} blocks`);
|
|
90
|
+
if (opens && !closes) offset = at.count - run;
|
|
91
|
+
else if (opens && closes && pin && pin.index >= i && pin.index < j) {
|
|
92
|
+
offset = pin.number - pin.index - at.first;
|
|
93
|
+
if (offset < 0 || offset + run > at.count) throw new TypeError(`block ${pin.number} is not stamped ${t} on the rollup's schedule`);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
for (let k = 0; k < run; k++) out.push(at.first + offset + k);
|
|
97
|
+
i = j;
|
|
98
|
+
}
|
|
99
|
+
// Counting off is only right for consecutive blocks, so a gap where seconds are shared is refused.
|
|
100
|
+
const shared = s.subSecond?.fromTime ?? Number.POSITIVE_INFINITY;
|
|
101
|
+
for (let k = 1; k < out.length; k++) {
|
|
102
|
+
if (timestamps[k]! >= shared && out[k] !== out[k - 1]! + 1) throw new TypeError(`the batches stamped ${timestamps[k - 1]} and ${timestamps[k]} are not consecutive blocks`);
|
|
103
|
+
}
|
|
104
|
+
return out;
|
|
105
|
+
}
|
package/src/settlement-blobs.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Copyright (c)
|
|
1
|
+
// Copyright (c) 2025-2026 Argento Computing Inc. Licensed under the MIT License. See LICENSE.
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* The BLOB layer of a Base ceiling's settlement on Ethereum
|
|
@@ -39,8 +39,9 @@ import {
|
|
|
39
39
|
type SettlementPointer, type RlpItem,
|
|
40
40
|
} from "@mikeargento/bitgraph-verify";
|
|
41
41
|
|
|
42
|
-
|
|
43
|
-
|
|
42
|
+
import { BASE_MAINNET_ROLLUP, blocksAt, numberSingularBatches, type RollupSchedule } from "./rollup-schedule.js";
|
|
43
|
+
|
|
44
|
+
export { BASE_MAINNET_ROLLUP, type RollupSchedule };
|
|
44
45
|
|
|
45
46
|
export const BLOB_BYTES = 131072;
|
|
46
47
|
/** OP Stack blob encoding version 0: (4 * 31 + 3) * 1024 - 4 bytes of data per blob. */
|
|
@@ -55,7 +56,7 @@ export interface SettlementBlobsCheck {
|
|
|
55
56
|
}
|
|
56
57
|
|
|
57
58
|
export interface SettlementLocated {
|
|
58
|
-
/** The Base block the batch places the transaction in,
|
|
59
|
+
/** The Base block the batch places the transaction in: its timestamp's second, and its place among the batches of that second. */
|
|
59
60
|
baseBlockNumber: number;
|
|
60
61
|
baseBlockTimestamp: number;
|
|
61
62
|
/** The batch's parent_hash (the hash of the block before), when the batch form carries one. */
|
|
@@ -89,8 +90,8 @@ export interface SettlementBlobsResult {
|
|
|
89
90
|
}
|
|
90
91
|
|
|
91
92
|
export interface SettlementBlobsOptions {
|
|
92
|
-
/**
|
|
93
|
-
rollup?:
|
|
93
|
+
/** When the rollup's blocks are stamped, which places a batch by its timestamp. Base mainnet by default. */
|
|
94
|
+
rollup?: RollupSchedule;
|
|
94
95
|
/** The L2 chain id, for rebuilding transactions out of span batches. Base mainnet by default. */
|
|
95
96
|
chainId?: bigint;
|
|
96
97
|
/** What the ceiling record says, each matched against what the batch says when given. */
|
|
@@ -292,18 +293,26 @@ export interface DecodedBatch {
|
|
|
292
293
|
blocks: Array<{ number: number; timestamp: number; parentHash: string | null; txs: BatchTx[]; complete: boolean }>;
|
|
293
294
|
}
|
|
294
295
|
|
|
296
|
+
/** A batch already identified by hash, to number a channel that cannot be numbered from its own bytes. */
|
|
297
|
+
export interface BatchPin { number: number; parentHash?: string; txHash?: string }
|
|
298
|
+
|
|
295
299
|
/**
|
|
296
300
|
* Batches out of a decompressed channel. Each is an RLP byte string
|
|
297
301
|
* `batch_version ++ content`. When `complete` is false the stream may be cut:
|
|
298
302
|
* the last batch is then read as far as it goes and marked incomplete.
|
|
303
|
+
* Singular batches are numbered together once all are read
|
|
304
|
+
* (numberSingularBatches says how); `pin` is used only where the channel
|
|
305
|
+
* alone cannot say.
|
|
299
306
|
*/
|
|
300
307
|
export function decodeBatches(
|
|
301
308
|
channel: Uint8Array,
|
|
302
309
|
complete: boolean,
|
|
303
|
-
rollup:
|
|
310
|
+
rollup: RollupSchedule,
|
|
304
311
|
chainId: bigint,
|
|
312
|
+
pin?: BatchPin,
|
|
305
313
|
): DecodedBatch[] {
|
|
306
314
|
const out: DecodedBatch[] = [];
|
|
315
|
+
const singular: Array<DecodedBatch["blocks"][number]> = [];
|
|
307
316
|
const limit = channel.length;
|
|
308
317
|
let p = 0;
|
|
309
318
|
while (p < limit) {
|
|
@@ -321,8 +330,8 @@ export function decodeBatches(
|
|
|
321
330
|
const contentStart = s.start + 1;
|
|
322
331
|
const contentEnd = Math.min(s.end, limit);
|
|
323
332
|
if (type === 0) {
|
|
324
|
-
const sb = parseSingularBatch(channel, contentStart, contentEnd, !cut
|
|
325
|
-
if (sb) out.push({ type, offset: p, blocks: [sb] });
|
|
333
|
+
const sb = parseSingularBatch(channel, contentStart, contentEnd, !cut);
|
|
334
|
+
if (sb) { singular.push(sb); out.push({ type, offset: p, blocks: [sb] }); }
|
|
326
335
|
} else if (type === 1) {
|
|
327
336
|
if (!cut) out.push({ type, offset: p, blocks: decodeSpanBatch(channel.subarray(contentStart, contentEnd), rollup, chainId) });
|
|
328
337
|
} else if (!cut) {
|
|
@@ -331,6 +340,12 @@ export function decodeBatches(
|
|
|
331
340
|
if (cut) break;
|
|
332
341
|
p = s.end;
|
|
333
342
|
}
|
|
343
|
+
if (singular.length > 0) {
|
|
344
|
+
const want = pin && { parent: pin.parentHash?.toLowerCase(), tx: pin.txHash?.toLowerCase() };
|
|
345
|
+
const at = want ? singular.findIndex((b) => (want.parent !== undefined && b.parentHash === want.parent) || (want.tx !== undefined && b.txs.some((t) => t.hash === want.tx))) : -1;
|
|
346
|
+
const numbers = numberSingularBatches(singular.map((b) => b.timestamp), rollup, at >= 0 ? { index: at, number: pin!.number } : undefined);
|
|
347
|
+
for (const [k, b] of singular.entries()) b.number = numbers[k]!;
|
|
348
|
+
}
|
|
334
349
|
return out;
|
|
335
350
|
}
|
|
336
351
|
|
|
@@ -338,9 +353,10 @@ export function decodeBatches(
|
|
|
338
353
|
* rlp([parent_hash, epoch_number, epoch_hash, timestamp, transaction_list]).
|
|
339
354
|
* `limit` is where readable bytes end; with `complete` the list must end
|
|
340
355
|
* exactly there. Returns null when not even the header fields are readable.
|
|
356
|
+
* The block's number is set by decodeBatches.
|
|
341
357
|
*/
|
|
342
358
|
function parseSingularBatch(
|
|
343
|
-
buf: Uint8Array, start: number, limit: number, complete: boolean,
|
|
359
|
+
buf: Uint8Array, start: number, limit: number, complete: boolean,
|
|
344
360
|
): DecodedBatch["blocks"][number] | null {
|
|
345
361
|
const outer = rlpSpan(buf, start, limit);
|
|
346
362
|
if (!outer) return null;
|
|
@@ -382,14 +398,7 @@ function parseSingularBatch(
|
|
|
382
398
|
}
|
|
383
399
|
if (listComplete && q !== list.end) throw new TypeError("singular batch: transaction_list overran its length");
|
|
384
400
|
if (complete && list.end !== outer.end) throw new TypeError("singular batch: bytes after transaction_list");
|
|
385
|
-
|
|
386
|
-
return { number, timestamp: Number(timestamp), parentHash: bytesToHex(parent.bytes), txs, complete: listComplete };
|
|
387
|
-
}
|
|
388
|
-
|
|
389
|
-
function blockNumberOf(timestamp: number, rollup: { l2GenesisTime: number; blockTime: number }): number {
|
|
390
|
-
const n = (timestamp - rollup.l2GenesisTime) / rollup.blockTime;
|
|
391
|
-
if (!Number.isInteger(n) || n < 0) throw new TypeError(`batch timestamp ${timestamp} is not on the rollup's block grid`);
|
|
392
|
-
return n;
|
|
401
|
+
return { number: -1, timestamp: Number(timestamp), parentHash: bytesToHex(parent.bytes), txs, complete: listComplete };
|
|
393
402
|
}
|
|
394
403
|
|
|
395
404
|
// ── Span batches (type 1), rebuilt into raw transactions ───────────────────
|
|
@@ -434,7 +443,7 @@ class Reader {
|
|
|
434
443
|
* here into their raw form so they can be hashed.
|
|
435
444
|
*/
|
|
436
445
|
export function decodeSpanBatch(
|
|
437
|
-
data: Uint8Array, rollup:
|
|
446
|
+
data: Uint8Array, rollup: RollupSchedule, chainId: bigint,
|
|
438
447
|
): DecodedBatch["blocks"] {
|
|
439
448
|
const r = new Reader(data);
|
|
440
449
|
const relTimestamp = r.uvarint();
|
|
@@ -500,8 +509,14 @@ export function decodeSpanBatch(
|
|
|
500
509
|
}
|
|
501
510
|
return { raw, hash: bytesToHex(keccak256(raw)) };
|
|
502
511
|
});
|
|
512
|
+
// A span batch counts on from its first block's time one block time apiece, which only describes
|
|
513
|
+
// blocks that each have a timestamp of their own; one reaching into shared seconds is refused.
|
|
503
514
|
const firstTimestamp = rollup.l2GenesisTime + Number(relTimestamp);
|
|
504
|
-
const
|
|
515
|
+
const first = blocksAt(firstTimestamp, rollup);
|
|
516
|
+
if (!first) throw new TypeError(`batch timestamp ${firstTimestamp} is not on the rollup's schedule`);
|
|
517
|
+
const lastTimestamp = firstTimestamp + (blockCount - 1) * rollup.blockTime;
|
|
518
|
+
if (first.count !== 1 || blocksAt(lastTimestamp, rollup)?.count !== 1) throw new TypeError(`span batch: its blocks from ${firstTimestamp} to ${lastTimestamp} share their seconds, so their numbers cannot be read from it`);
|
|
519
|
+
const firstBlock = first.first;
|
|
505
520
|
const blocks: DecodedBatch["blocks"] = [];
|
|
506
521
|
let k = 0;
|
|
507
522
|
for (let b = 0; b < blockCount; b++) {
|
|
@@ -635,7 +650,12 @@ export async function verifySettlementBlobs(
|
|
|
635
650
|
if (pointer.channel?.compression && pointer.channel.compression !== decomp.compression) return fail("decompress", `the channel is ${decomp.compression}, the pointer says ${pointer.channel.compression}`);
|
|
636
651
|
let batches: DecodedBatch[];
|
|
637
652
|
try {
|
|
638
|
-
|
|
653
|
+
const pinTx = (options.expect?.txHash ?? pointer.located?.txHash)?.toLowerCase();
|
|
654
|
+
batches = decodeBatches(decomp.bytes, complete, rollup, chainId, {
|
|
655
|
+
number: pointer.baseBlockNumber,
|
|
656
|
+
...(options.expect?.baseParentHash ? { parentHash: options.expect.baseParentHash } : {}),
|
|
657
|
+
...(pinTx ? { txHash: pinTx } : {}),
|
|
658
|
+
});
|
|
639
659
|
} catch (e) {
|
|
640
660
|
return fail("batches", (e as Error).message);
|
|
641
661
|
}
|
package/src/tar.ts
CHANGED
package/src/temporal.ts
CHANGED
package/src/types.ts
CHANGED
package/src/validity.ts
CHANGED
package/src/verify-tiers.ts
CHANGED
package/src/witness.ts
CHANGED