| … | … | @@ -0,0 +1,178 @@ |
| 1 | // PointerSubstrate.migrateInline — convert INLINE RECORDS in the autobase into fetchable pointers. |
| 2 | // |
| 3 | // A DID written through the LEGACY substrate but now running the pointer design has full records where |
| 4 | // { $oid, size } pointers belong. It reads perfectly on the node holding it — _resolve passes an |
| 5 | // undefined $oid to ensureLocal, which falls back to the local file — and is UNFETCHABLE BY EVERY OTHER |
| 6 | // NODE, because there is no oid to fetch by. A co-owner converges the keys and can never turn them into |
| 7 | // bytes. |
| 8 | // |
| 9 | // Live on loser-club (2026-07-27): all three records inline |
| 10 | // (app.bsky.feed.generator/loser-club, app.bsky.feed.post/self.hello, net.favela.federation/self), |
| 11 | // so that DID cannot be co-owned at all regardless of roster or config. |
| 12 | // |
| 13 | // backfill() cannot fix it: it skips any key that already has a value, which is correct for its job |
| 14 | // (never clobber a converged pointer or tombstone) but an inline record is such a value. |
| 15 | // |
| 16 | // The load-bearing property is that CONTENT IS PRESERVED. What the repo publishes today is the LOCAL |
| 17 | // FILE, not the inline copy, so the pointer must address the file's bytes — otherwise the signed repo |
| 18 | // changes underneath the migration. |
| 19 | // |
| 20 | // Run: brittle-node test/migrate-inline.test.js |
| 21 | import test from 'brittle' |
| 22 | import fs from 'fs' |
| 23 | import os from 'os' |
| 24 | import path from 'path' |
| 25 | import crypto from 'hypercore-crypto' |
| 26 | import * as dagCbor from '@ipld/dag-cbor' |
| 27 | import { OverlayRepoStore } from '../lib/overlay-repo-store.js' |
| 28 | import { PointerSubstrate } from '../lib/pointer-substrate.js' |
| 29 | |
| 30 | const tmp = () => fs.mkdtempSync(path.join(os.tmpdir(), 'mig-')) |
| 31 | const GEN = 'app.bsky.feed.generator' |
| 32 | const POST = 'app.bsky.feed.post' |
| 33 | |
| 34 | function memKV () { |
| 35 | const m = new Map() |
| 36 | return { |
| 37 | _m: m, |
| 38 | async set (k, v) { m.set(k, v) }, |
| 39 | async del (k) { m.delete(k) }, |
| 40 | async get (k) { return m.has(k) ? m.get(k) : null }, |
| 41 | async list (prefix = '') { const o = {}; for (const [k, v] of m) if (k.startsWith(prefix)) o[k] = v; return o }, |
| 42 | async listNow (prefix = '') { return this.list(prefix) } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | function mkOverlay (net) { |
| 47 | return { |
| 48 | async registerFile (overlayPath, diskPath) { |
| 49 | const bytes = fs.readFileSync(diskPath) |
| 50 | const oid = crypto.data(bytes).toString('hex') |
| 51 | net.set(oid, bytes) |
| 52 | return { contentHash: oid } |
| 53 | }, |
| 54 | async fetchFile (oid, o = {}) { |
| 55 | const bytes = net.get(oid) |
| 56 | if (!bytes) return null |
| 57 | fs.writeFileSync(o.destPath, bytes) |
| 58 | return { destPath: o.destPath, size: bytes.length } |
| 59 | }, |
| 60 | async hashFile (p) { |
| 61 | let bytes |
| 62 | try { bytes = fs.readFileSync(p) } catch { return null } |
| 63 | return { contentHash: crypto.data(bytes).toString('hex'), size: bytes.length } |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | const GEN_REC = { $type: GEN, did: 'did:web:x', displayName: 'loser-club', description: 'a feed', createdAt: '2026-07-01T00:00:00.000Z' } |
| 69 | const POST_REC = { $type: POST, text: 'hello from loser-club', createdAt: '2026-07-01T00:00:00.000Z' } |
| 70 | |
| 71 | // Reproduce the live shape: the INLINE RECORD is in the autobase and the record FILE is on disk. |
| 72 | async function seedInline (sb, store, collection, rkey, record) { |
| 73 | fs.mkdirSync(path.join(store._recordsDir, collection), { recursive: true }) |
| 74 | fs.writeFileSync(path.join(store._recordsDir, collection, rkey + '.dagcbor'), Buffer.from(dagCbor.encode(record))) |
| 75 | await sb.set(collection + '/' + rkey, record) // the legacy substrate wrote the whole record here |
| 76 | } |
| 77 | |
| 78 | test('inline records become pointers that address the SAME bytes (published content unchanged)', async (t) => { |
| 79 | const net = new Map() |
| 80 | const root = tmp() |
| 81 | const store = await new OverlayRepoStore(root, { overlay: mkOverlay(net), did: 'did:plc:lc' }).ready() |
| 82 | const sb = memKV() |
| 83 | const sub = new PointerSubstrate(sb, store) |
| 84 | |
| 85 | await seedInline(sb, store, GEN, 'loser-club', GEN_REC) |
| 86 | await seedInline(sb, store, POST, 'self.hello', POST_REC) |
| 87 | |
| 88 | // Precondition: the entries are records, not pointers, and nothing is fetchable by oid. |
| 89 | t.absent((await sb.get(GEN + '/loser-club')).$oid, 'precondition: the autobase holds a record, not a pointer') |
| 90 | const beforeOid = (await store.localOid(GEN, 'loser-club')).oid |
| 91 | const beforeRec = await sub.get(GEN + '/loser-club') |
| 92 | t.alike(beforeRec, GEN_REC, 'precondition: it resolves locally (via the file fallback)') |
| 93 | |
| 94 | const r = await sub.migrateInline() |
| 95 | t.is(r.converted, 2, 'both inline records converted') |
| 96 | t.is(r.mismatched, 0, 'no key was refused for changing content') |
| 97 | t.is(r.failed, 0, 'and none failed') |
| 98 | |
| 99 | const ptr = await sb.get(GEN + '/loser-club') |
| 100 | t.ok(ptr.$oid, 'the entry is now a pointer') |
| 101 | t.is(ptr.$oid, beforeOid, 'addressing EXACTLY the bytes that were already resolving — the repo does not change') |
| 102 | t.is(ptr.size, Buffer.from(dagCbor.encode(GEN_REC)).length, 'and carries the record\'s real byte length') |
| 103 | t.alike(await sub.get(GEN + '/loser-club'), GEN_REC, 'the record still resolves, byte-identical') |
| 104 | t.ok(net.has(ptr.$oid), 'the bytes are now registered with the overlay — a PEER can fetch them by oid') |
| 105 | }) |
| 106 | |
| 107 | test('a peer holding only the pointer can now resolve the record (the whole point)', async (t) => { |
| 108 | const net = new Map() |
| 109 | const store = await new OverlayRepoStore(tmp(), { overlay: mkOverlay(net), did: 'did:plc:lc' }).ready() |
| 110 | const sb = memKV() |
| 111 | const sub = new PointerSubstrate(sb, store) |
| 112 | await seedInline(sb, store, POST, 'self.hello', POST_REC) |
| 113 | |
| 114 | // BEFORE: a co-owner replicates the entry. It has no oid, so it can never become bytes. |
| 115 | const peerStore = await new OverlayRepoStore(tmp(), { overlay: mkOverlay(net), did: 'did:plc:lc' }).ready() |
| 116 | const peerSb = memKV() |
| 117 | await peerSb.set(POST + '/self.hello', await sb.get(POST + '/self.hello')) |
| 118 | const peerSub = new PointerSubstrate(peerSb, peerStore) |
| 119 | t.is(await peerSub.get(POST + '/self.hello'), null, 'CONTROL: the peer cannot resolve an inline entry — no oid to fetch by') |
| 120 | |
| 121 | await sub.migrateInline() |
| 122 | |
| 123 | const peerSb2 = memKV() |
| 124 | await peerSb2.set(POST + '/self.hello', await sb.get(POST + '/self.hello')) // replicate the POINTER |
| 125 | const peerSub2 = new PointerSubstrate(peerSb2, await new OverlayRepoStore(tmp(), { overlay: mkOverlay(net), did: 'did:plc:lc' }).ready()) |
| 126 | t.alike(await peerSub2.get(POST + '/self.hello'), POST_REC, 'after migration the peer fetches the record by oid') |
| 127 | }) |
| 128 | |
| 129 | test('idempotent: already-correct pointers and tombstones are left alone', async (t) => { |
| 130 | const net = new Map() |
| 131 | const store = await new OverlayRepoStore(tmp(), { overlay: mkOverlay(net), did: 'did:plc:lc' }).ready() |
| 132 | const sb = memKV() |
| 133 | const sub = new PointerSubstrate(sb, store) |
| 134 | |
| 135 | await sub.set(POST + '/proper', POST_REC) // a real pointer |
| 136 | await sb.set(POST + '/dead', { $deleted: true }) // a tombstone |
| 137 | await seedInline(sb, store, GEN, 'loser-club', GEN_REC) // one inline record |
| 138 | |
| 139 | const first = await sub.migrateInline() |
| 140 | t.is(first.converted, 1, 'only the inline record was converted') |
| 141 | t.is(first.alreadyPointer, 1, 'the healthy pointer was skipped') |
| 142 | t.is(first.tombstones, 1, 'the tombstone was skipped — never resurrected') |
| 143 | |
| 144 | const second = await sub.migrateInline() |
| 145 | t.is(second.converted, 0, 'a second run converts nothing') |
| 146 | t.is(second.alreadyPointer, 2, 'everything is now a pointer') |
| 147 | t.alike(await sb.get(POST + '/dead'), { $deleted: true }, 'the tombstone is untouched') |
| 148 | }) |
| 149 | |
| 150 | test('reserved keys are never touched', async (t) => { |
| 151 | const net = new Map() |
| 152 | const store = await new OverlayRepoStore(tmp(), { overlay: mkOverlay(net), did: 'did:plc:lc' }).ready() |
| 153 | const sb = memKV() |
| 154 | const sub = new PointerSubstrate(sb, store) |
| 155 | |
| 156 | await sb.set('__commit__/3kzzz', { rev: '3kzzz', ops: [] }) |
| 157 | await sb.set('__members__', { a: 1 }) |
| 158 | await sb.set('__blob__/bafkreiX', { $oid: 'abc', size: 1 }) |
| 159 | |
| 160 | const r = await sub.migrateInline() |
| 161 | t.is(r.total, 0, 'no reserved key was considered a record') |
| 162 | t.is(r.converted, 0, 'and nothing was rewritten') |
| 163 | t.alike(await sb.get('__commit__/3kzzz'), { rev: '3kzzz', ops: [] }, 'the firehose frame is intact') |
| 164 | }) |
| 165 | |
| 166 | test('with no local file the inline record itself is used (it is the only copy)', async (t) => { |
| 167 | const net = new Map() |
| 168 | const store = await new OverlayRepoStore(tmp(), { overlay: mkOverlay(net), did: 'did:plc:lc' }).ready() |
| 169 | const sb = memKV() |
| 170 | const sub = new PointerSubstrate(sb, store) |
| 171 | |
| 172 | await sb.set(POST + '/orphan', POST_REC) // inline entry, NO file on disk |
| 173 | |
| 174 | const r = await sub.migrateInline() |
| 175 | t.is(r.converted, 1, 'converted') |
| 176 | t.is(r.fromInline, 1, 'and reports that the content came from the inline copy, not a file') |
| 177 | t.alike(await sub.get(POST + '/orphan'), POST_REC, 'the record resolves afterwards') |
| 178 | }) |