@fangorn-network/sdk 2026.10.26-dev → 2026.10.27-dev
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/lib/cli/index.d.ts +2 -2
- package/lib/engine/car.d.ts +5 -1
- package/lib/engine/car.js +2 -2
- package/lib/engine/index.d.ts +10 -2
- package/lib/engine/index.js +30 -12
- package/lib/engine/records.d.ts +15 -1
- package/lib/engine/records.js +75 -1
- package/lib/test/testbed.d.ts +5161 -5161
- package/package.json +1 -1
package/lib/cli/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import "./cli.js";
|
|
2
|
-
import * as
|
|
2
|
+
import * as viem129 from "viem";
|
|
3
3
|
|
|
4
4
|
//#region src/cli/index.d.ts
|
|
5
|
-
declare const getChain: (chainStr: string) =>
|
|
5
|
+
declare const getChain: (chainStr: string) => viem129.Chain;
|
|
6
6
|
declare const handleCancel: (value: unknown) => void;
|
|
7
7
|
declare const selectChain: () => Promise<string>;
|
|
8
8
|
declare function parseGadgetArg(raw: string): {
|
package/lib/engine/car.d.ts
CHANGED
|
@@ -5,6 +5,10 @@ import { CID } from "multiformats/cid";
|
|
|
5
5
|
/** Serialize blocks into CAR v1 bytes under a single root. */
|
|
6
6
|
declare function packCar(root: CID, blocks: Iterable<Block>): Promise<Uint8Array>;
|
|
7
7
|
/** Decode CAR bytes back into blocks, each checked against its CID (the host is untrusted). */
|
|
8
|
-
declare function readCar(bytes: Uint8Array
|
|
8
|
+
declare function readCar(bytes: Uint8Array, {
|
|
9
|
+
verify
|
|
10
|
+
}?: {
|
|
11
|
+
verify?: boolean | undefined;
|
|
12
|
+
}): Promise<Block[]>;
|
|
9
13
|
//#endregion
|
|
10
14
|
export { packCar, readCar };
|
package/lib/engine/car.js
CHANGED
|
@@ -24,12 +24,12 @@ async function packCar(root, blocks) {
|
|
|
24
24
|
return buf;
|
|
25
25
|
}
|
|
26
26
|
/** Decode CAR bytes back into blocks, each checked against its CID (the host is untrusted). */
|
|
27
|
-
async function readCar(bytes) {
|
|
27
|
+
async function readCar(bytes, { verify = true } = {}) {
|
|
28
28
|
const reader = await CarReader.fromBytes(bytes);
|
|
29
29
|
const blocks = [];
|
|
30
30
|
for await (const { cid, bytes: blockBytes } of reader.blocks()) {
|
|
31
31
|
const ours = CID.parse(cid.toString());
|
|
32
|
-
assertBlockMatches(ours, blockBytes);
|
|
32
|
+
if (verify) assertBlockMatches(ours, blockBytes);
|
|
33
33
|
blocks.push({
|
|
34
34
|
cid: ours,
|
|
35
35
|
bytes: blockBytes
|
package/lib/engine/index.d.ts
CHANGED
|
@@ -31,6 +31,8 @@ interface RepoDiff {
|
|
|
31
31
|
puts: StoredRecord[];
|
|
32
32
|
/** Ids of records that are gone. */
|
|
33
33
|
deletes: string[];
|
|
34
|
+
/** CIDs of the old versions: records deleted, or replaced by a put. An index keyed by record CID drops these. */
|
|
35
|
+
superseded: string[];
|
|
34
36
|
}
|
|
35
37
|
/**
|
|
36
38
|
* Upper bound on a repo name's length. The name is stored in every commit
|
|
@@ -86,6 +88,12 @@ declare class FangornEngine {
|
|
|
86
88
|
private cache;
|
|
87
89
|
/** Commits whose CAR has already been loaded into the cache. */
|
|
88
90
|
private loadedCars;
|
|
91
|
+
/**
|
|
92
|
+
* Blocks from a loaded CAR, not yet hash-checked. A CAR holds every block its commit introduced
|
|
93
|
+
* (the first commit of a 50k-record repo: all of them), and a diff reads a handful: each block is
|
|
94
|
+
* checked when `getBlock` first hands it out, then moves to `cache`.
|
|
95
|
+
*/
|
|
96
|
+
private unverified;
|
|
89
97
|
constructor(storage: MetadataStorage, registry: Registry);
|
|
90
98
|
/**
|
|
91
99
|
* Reconstructs the CID a commit's raw digest hex refers to. The contract only
|
|
@@ -165,8 +173,8 @@ declare class FangornEngine {
|
|
|
165
173
|
* Diffing root-to-root (rather than walking commits) is deliberately robust:
|
|
166
174
|
* it yields the correct net delta even across a force-push / non-fast-forward
|
|
167
175
|
* reorg — exactly what an index builder needs (what to (re)embed, what to evict).
|
|
168
|
-
*
|
|
169
|
-
*
|
|
176
|
+
* Reads only the nodes on changed paths (`diffMst`): 10 new records in a
|
|
177
|
+
* 50k-record repo cost ~10 × depth nodes, not two 50k-key walks.
|
|
170
178
|
*/
|
|
171
179
|
repoDiff(oldRootHex: string | null, newRootHex: string): Promise<RepoDiff>;
|
|
172
180
|
/** Diff a commit against its parent. */
|
package/lib/engine/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { assertBlockMatches, assertValidRecordId, buildMst, decodeBlock, encodeBlock, lookupMst, readMst } from "./records.js";
|
|
1
|
+
import { assertBlockMatches, assertValidRecordId, buildMst, decodeBlock, diffMst, encodeBlock, lookupMst, readMst } from "./records.js";
|
|
2
2
|
import { packCar, readCar } from "./car.js";
|
|
3
3
|
import { ZERO_HEAD } from "../registry.js";
|
|
4
4
|
import { CID } from "multiformats/cid";
|
|
@@ -38,6 +38,12 @@ var FangornEngine = class {
|
|
|
38
38
|
cache = /* @__PURE__ */ new Map();
|
|
39
39
|
/** Commits whose CAR has already been loaded into the cache. */
|
|
40
40
|
loadedCars = /* @__PURE__ */ new Set();
|
|
41
|
+
/**
|
|
42
|
+
* Blocks from a loaded CAR, not yet hash-checked. A CAR holds every block its commit introduced
|
|
43
|
+
* (the first commit of a 50k-record repo: all of them), and a diff reads a handful: each block is
|
|
44
|
+
* checked when `getBlock` first hands it out, then moves to `cache`.
|
|
45
|
+
*/
|
|
46
|
+
unverified = /* @__PURE__ */ new Map();
|
|
41
47
|
constructor(storage, registry) {
|
|
42
48
|
this.storage = storage;
|
|
43
49
|
this.registry = registry;
|
|
@@ -77,7 +83,10 @@ var FangornEngine = class {
|
|
|
77
83
|
/** Pull every block of `commit`'s CAR into the local cache. */
|
|
78
84
|
async loadCar(commitCid, commit) {
|
|
79
85
|
const bytes = await this.storage.getFile(commit.car);
|
|
80
|
-
for (const block of await readCar(bytes))
|
|
86
|
+
for (const block of await readCar(bytes, { verify: false })) {
|
|
87
|
+
const key = block.cid.toString();
|
|
88
|
+
if (!this.cache.has(key)) this.unverified.set(key, block.bytes);
|
|
89
|
+
}
|
|
81
90
|
this.loadedCars.add(commitCid.toString());
|
|
82
91
|
}
|
|
83
92
|
/**
|
|
@@ -86,14 +95,24 @@ var FangornEngine = class {
|
|
|
86
95
|
*/
|
|
87
96
|
async getBlock(cid, hint) {
|
|
88
97
|
const key = cid.toString();
|
|
89
|
-
const
|
|
98
|
+
const local = () => {
|
|
99
|
+
const hit$1 = this.cache.get(key);
|
|
100
|
+
if (hit$1) return hit$1;
|
|
101
|
+
const raw = this.unverified.get(key);
|
|
102
|
+
if (!raw) return null;
|
|
103
|
+
assertBlockMatches(cid, raw);
|
|
104
|
+
this.unverified.delete(key);
|
|
105
|
+
this.cache.set(key, raw);
|
|
106
|
+
return raw;
|
|
107
|
+
};
|
|
108
|
+
const hit = local();
|
|
90
109
|
if (hit) return hit;
|
|
91
110
|
let cursor = hint;
|
|
92
111
|
while (cursor) {
|
|
93
112
|
const commit = await this.getCommit(cursor);
|
|
94
113
|
if (!this.loadedCars.has(cursor.toString())) {
|
|
95
114
|
await this.loadCar(cursor, commit);
|
|
96
|
-
const found =
|
|
115
|
+
const found = local();
|
|
97
116
|
if (found) return found;
|
|
98
117
|
}
|
|
99
118
|
cursor = commit.parent;
|
|
@@ -244,21 +263,20 @@ var FangornEngine = class {
|
|
|
244
263
|
* Diffing root-to-root (rather than walking commits) is deliberately robust:
|
|
245
264
|
* it yields the correct net delta even across a force-push / non-fast-forward
|
|
246
265
|
* reorg — exactly what an index builder needs (what to (re)embed, what to evict).
|
|
247
|
-
*
|
|
248
|
-
*
|
|
266
|
+
* Reads only the nodes on changed paths (`diffMst`): 10 new records in a
|
|
267
|
+
* 50k-record repo cost ~10 × depth nodes, not two 50k-key walks.
|
|
249
268
|
*/
|
|
250
269
|
async repoDiff(oldRootHex, newRootHex) {
|
|
251
270
|
const newCommit = newRootHex === ZERO_BYTES32 ? null : this.hexToRootCid(newRootHex);
|
|
252
271
|
const oldCommit = oldRootHex && oldRootHex !== ZERO_BYTES32 ? this.hexToRootCid(oldRootHex) : null;
|
|
253
|
-
const
|
|
254
|
-
const
|
|
272
|
+
const root = async (c) => c ? (await this.getCommit(c)).root : null;
|
|
273
|
+
const diff = await diffMst(await root(oldCommit), await root(newCommit), (cid) => this.getBlock(cid, oldCommit), (cid) => this.getBlock(cid, newCommit));
|
|
255
274
|
const puts = [];
|
|
256
|
-
if (newCommit)
|
|
257
|
-
for (const [id, cid] of after) if (before.get(id)?.toString() !== cid.toString()) puts.push(await this.decodeRecord(id, cid, newCommit));
|
|
258
|
-
}
|
|
275
|
+
if (newCommit) for (const [id, cid] of diff.puts) puts.push(await this.decodeRecord(id, cid, newCommit));
|
|
259
276
|
return {
|
|
260
277
|
puts,
|
|
261
|
-
deletes:
|
|
278
|
+
deletes: diff.deletes,
|
|
279
|
+
superseded: [...diff.before.values()].map(String)
|
|
262
280
|
};
|
|
263
281
|
}
|
|
264
282
|
/** Diff a commit against its parent. */
|
package/lib/engine/records.d.ts
CHANGED
|
@@ -99,6 +99,20 @@ declare function readMst(root: CID, getBlock: (cid: CID) => Promise<Uint8Array>)
|
|
|
99
99
|
state: Map<string, CID>;
|
|
100
100
|
nodeCids: Set<string>;
|
|
101
101
|
}>;
|
|
102
|
+
/**
|
|
103
|
+
* What changed from one tree to another: `puts` (new keys, and keys with a new value) and `deletes`
|
|
104
|
+
* (keys gone), each with its old value in `before`. Both trees are read as in-order streams of entries
|
|
105
|
+
* and unopened subtrees; a subtree with the same CID at the head of both streams holds the same keys
|
|
106
|
+
* and values, so it is dropped unread. Only nodes on changed paths are fetched: ~changes × depth,
|
|
107
|
+
* not the size of either tree.
|
|
108
|
+
* ponytail: unlike readMst, does not prove either tree canonical (that needs every key). Blocks are
|
|
109
|
+
* still hash-checked by `getBlock`; a publisher's malformed tree can only misreport its own records.
|
|
110
|
+
*/
|
|
111
|
+
declare function diffMst(oldRoot: CID | null, newRoot: CID | null, getOld: (cid: CID) => Promise<Uint8Array>, getNew: (cid: CID) => Promise<Uint8Array>): Promise<{
|
|
112
|
+
puts: Map<string, CID>;
|
|
113
|
+
deletes: string[];
|
|
114
|
+
before: Map<string, CID>;
|
|
115
|
+
}>;
|
|
102
116
|
/**
|
|
103
117
|
* The record CID stored under `key`, or null if absent — one node per level.
|
|
104
118
|
* ponytail: trusts node ordering on the path, so a malformed tree can hide a
|
|
@@ -107,4 +121,4 @@ declare function readMst(root: CID, getBlock: (cid: CID) => Promise<Uint8Array>)
|
|
|
107
121
|
*/
|
|
108
122
|
declare function lookupMst(root: CID, key: string, getBlock: (cid: CID) => Promise<Uint8Array>): Promise<CID | null>;
|
|
109
123
|
//#endregion
|
|
110
|
-
export { Block, CommitBlock, MAX_RECORD_ID_LENGTH, MST_BITS, MstEntry, MstNode, RecordBlock, Ref, assertBlockMatches, assertValidRecordId, buildMst, decodeBlock, encodeBlock, keyLevel, lookupMst, readMst };
|
|
124
|
+
export { Block, CommitBlock, MAX_RECORD_ID_LENGTH, MST_BITS, MstEntry, MstNode, RecordBlock, Ref, assertBlockMatches, assertValidRecordId, buildMst, decodeBlock, diffMst, encodeBlock, keyLevel, lookupMst, readMst };
|
package/lib/engine/records.js
CHANGED
|
@@ -141,6 +141,80 @@ async function readMst(root, getBlock) {
|
|
|
141
141
|
};
|
|
142
142
|
}
|
|
143
143
|
/**
|
|
144
|
+
* What changed from one tree to another: `puts` (new keys, and keys with a new value) and `deletes`
|
|
145
|
+
* (keys gone), each with its old value in `before`. Both trees are read as in-order streams of entries
|
|
146
|
+
* and unopened subtrees; a subtree with the same CID at the head of both streams holds the same keys
|
|
147
|
+
* and values, so it is dropped unread. Only nodes on changed paths are fetched: ~changes × depth,
|
|
148
|
+
* not the size of either tree.
|
|
149
|
+
* ponytail: unlike readMst, does not prove either tree canonical (that needs every key). Blocks are
|
|
150
|
+
* still hash-checked by `getBlock`; a publisher's malformed tree can only misreport its own records.
|
|
151
|
+
*/
|
|
152
|
+
async function diffMst(oldRoot, newRoot, getOld, getNew) {
|
|
153
|
+
const a = oldRoot ? [{
|
|
154
|
+
sub: oldRoot,
|
|
155
|
+
level: Infinity
|
|
156
|
+
}] : [];
|
|
157
|
+
const b = newRoot ? [{
|
|
158
|
+
sub: newRoot,
|
|
159
|
+
level: Infinity
|
|
160
|
+
}] : [];
|
|
161
|
+
const open = async (s, get) => {
|
|
162
|
+
const { sub } = s.pop();
|
|
163
|
+
const node = decodeBlock(await get(sub));
|
|
164
|
+
const items = node.left ? [{
|
|
165
|
+
sub: node.left,
|
|
166
|
+
level: node.level - 1
|
|
167
|
+
}] : [];
|
|
168
|
+
for (const e of node.entries) {
|
|
169
|
+
items.push({
|
|
170
|
+
key: e.key,
|
|
171
|
+
value: e.value
|
|
172
|
+
});
|
|
173
|
+
if (e.right) items.push({
|
|
174
|
+
sub: e.right,
|
|
175
|
+
level: node.level - 1
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
s.push(...items.reverse());
|
|
179
|
+
};
|
|
180
|
+
const puts = /* @__PURE__ */ new Map(), before = /* @__PURE__ */ new Map(), deletes = [];
|
|
181
|
+
for (let steps = 0; a.length || b.length; steps++) {
|
|
182
|
+
if (steps > 1e8) throw new Error("MST diff did not terminate");
|
|
183
|
+
const x = a.at(-1), y = b.at(-1);
|
|
184
|
+
if (x && y && "sub" in x && "sub" in y) if (x.sub.equals(y.sub)) {
|
|
185
|
+
a.pop();
|
|
186
|
+
b.pop();
|
|
187
|
+
} else if (x.level > y.level) await open(a, getOld);
|
|
188
|
+
else if (y.level > x.level) await open(b, getNew);
|
|
189
|
+
else {
|
|
190
|
+
await open(a, getOld);
|
|
191
|
+
await open(b, getNew);
|
|
192
|
+
}
|
|
193
|
+
else if (x && "sub" in x) await open(a, getOld);
|
|
194
|
+
else if (y && "sub" in y) await open(b, getNew);
|
|
195
|
+
else if (x && (!y || x.key < y.key)) {
|
|
196
|
+
a.pop();
|
|
197
|
+
deletes.push(x.key);
|
|
198
|
+
before.set(x.key, x.value);
|
|
199
|
+
} else if (y && (!x || y.key < x.key)) {
|
|
200
|
+
b.pop();
|
|
201
|
+
puts.set(y.key, y.value);
|
|
202
|
+
} else if (x && y) {
|
|
203
|
+
a.pop();
|
|
204
|
+
b.pop();
|
|
205
|
+
if (!x.value.equals(y.value)) {
|
|
206
|
+
puts.set(y.key, y.value);
|
|
207
|
+
before.set(x.key, x.value);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return {
|
|
212
|
+
puts,
|
|
213
|
+
deletes,
|
|
214
|
+
before
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
144
218
|
* The record CID stored under `key`, or null if absent — one node per level.
|
|
145
219
|
* ponytail: trusts node ordering on the path, so a malformed tree can hide a
|
|
146
220
|
* record (never forge one: the caller checks the record's id). Run `readMst`
|
|
@@ -162,4 +236,4 @@ async function lookupMst(root, key, getBlock) {
|
|
|
162
236
|
}
|
|
163
237
|
|
|
164
238
|
//#endregion
|
|
165
|
-
export { MAX_RECORD_ID_LENGTH, MST_BITS, assertBlockMatches, assertValidRecordId, buildMst, decodeBlock, encodeBlock, keyLevel, lookupMst, readMst };
|
|
239
|
+
export { MAX_RECORD_ID_LENGTH, MST_BITS, assertBlockMatches, assertValidRecordId, buildMst, decodeBlock, diffMst, encodeBlock, keyLevel, lookupMst, readMst };
|