// // Copyright 2026 DXOS.org // import * as A from '@automerge/automerge'; import { initSubduction } from '@automerge/automerge-repo'; import { BlobMeta, CommitId, CommitInput, CommitWithBlob, Fragment, FragmentInput, FragmentWithBlob, LooseCommit, MemorySigner, SedimentreeId, type SedimentreeStorage, SignedFragment, SignedLooseCommit, Subduction, } from '@automerge/automerge-subduction'; import bs58check from 'bs58check'; import * as Effect from 'effect/Effect'; import * as SqlClient from 'effect/sql/SqlClient'; import { readFileSync } from 'node:fs'; import { beforeAll, describe, expect, onTestFinished, test } from 'vitest'; import { Context } from '@dxos/context'; import * as RuntimeProvider from '@dxos/effect/RuntimeProvider'; import { PublicKey } from '@dxos/keys'; import { type TestSqliteRuntime, createTestSqliteRuntime } from '../../testing/index.ts'; import { AutomergeHost } from '../automerge-host.ts'; import { SqliteStorageAdapter, SUBDUCTION_PREFIX } from '../sqlite-storage-adapter.ts'; import { deleteRemoteHeads } from './0001_delete_remote_heads.ts'; import { parseSignedFragmentRecord, selfCheckpointedFragments, selfCheckpointRepair, } from './0002_self_checkpointed_fragments.ts'; import { type Migration, runMigrations } from './index.ts'; /** * What a client on `@automerge/automerge` 3.3.2 stored for one synthetic document, produced by that * version's own `getFragmentMetadata` / `bundleFragmentMetadata`: level-0 entries as loose commits, * level-1+ entries as fragments, metadata verbatim. Generated by editing until the newest change * closed a fragment — the state whose head the engine then fails to report. */ type OldClientFixture = { automerge: string; documentIdBytesHex: string; text: string; heads: string[]; changes: number; commits: Array<{ id: string; parents: string[]; bytes: string }>; fragments: Array<{ head: string; level: number; boundary: string[]; checkpoints: string[]; bytes: string }>; }; const fixture: OldClientFixture = JSON.parse( readFileSync(new URL('./fixtures/automerge-3.3.2-fragments.json', import.meta.url), 'utf8'), ); /** * The data migrations `AutomergeHost.open()` runs over the Subduction records in `automerge_chunks`, * and the runner that records them in `automerge_subduction_migrations`. Fixtures are synthetic * documents and ids. */ describe('subduction migrations', () => { beforeAll(async () => { await initSubduction(); }); const setup = async () => { const { runtime, dispose } = createTestSqliteRuntime(); const adapter = new SqliteStorageAdapter({ runtime }); await adapter.open(); await RuntimeProvider.runPromise(runtime)(adapter.migrate); onTestFinished(async () => { await adapter.close(); await dispose(); }); return { runtime, adapter }; }; const openHost = async (runtime: TestSqliteRuntime['runtime']) => { const host = new AutomergeHost({ runtime, useSubduction: true }); await host.open(); onTestFinished(async () => { if (host.isOpen) { await host.close(); } }); return host; }; const applied = (runtime: TestSqliteRuntime['runtime'], name: string) => RuntimeProvider.runPromise(runtime)( Effect.gen(function* () { const sql = yield* SqlClient.SqlClient; const rows = yield* sql<{ n: number; }>`SELECT count(*) AS n FROM automerge_subduction_migrations WHERE name = ${name}`; return rows[0].n > 0; }), ); /** Empties the ledger, as a profile from before it existed has none. */ const forgetMigrations = (runtime: TestSqliteRuntime['runtime']) => RuntimeProvider.runPromise(runtime)( Effect.gen(function* () { const sql = yield* SqlClient.SqlClient; yield* sql`DELETE FROM automerge_subduction_migrations`; }), ); describe('runner', () => { const migration = (name: string, done: boolean, ran: string[]): Migration => ({ name, run: async () => { ran.push(name); return done; }, }); test('runs unrecorded migrations in order, records the complete ones, and stops at a throw', async () => { const { runtime, adapter } = await setup(); const subduction = new Subduction({ signer: MemorySigner.generate(), storage: memoryStorage() }); const context = { storage: adapter, subduction }; const ran: string[] = []; const migrations = [ migration('0001_first', true, ran), migration('0002_partial', false, ran), migration('0003_complete', true, ran), { name: '0004_throws', run: async () => Promise.reject(new Error('boom')) }, migration('0005_after', true, ran), ]; await expect(runMigrations(context, migrations)).rejects.toThrow('boom'); expect(ran).toEqual(['0001_first', '0002_partial', '0003_complete']); expect(await applied(runtime, '0001_first')).toBe(true); expect(await applied(runtime, '0002_partial')).toBe(false); expect(await applied(runtime, '0003_complete')).toBe(true); expect(await applied(runtime, '0005_after')).toBe(false); // A second run picks up the partial one again and nothing that is recorded. ran.length = 0; await runMigrations(context, migrations.slice(0, 3)); expect(ran).toEqual(['0002_partial']); }); }); describe('0001_delete_remote_heads', () => { const remoteHeads = ['sid1', 'sid2'].flatMap((sedimentreeId) => ['peerA', 'peerB'].map((peerId) => [SUBDUCTION_PREFIX, 'remote-heads', sedimentreeId, peerId]), ); const unrelated = [ [SUBDUCTION_PREFIX, 'ids', 'sid1'], [SUBDUCTION_PREFIX, 'commits', 'sid1', 'commit1'], ['doc1', 'incremental', 'hash1'], ]; test('deletes the stored remote heads once, on the first open', async () => { const { runtime, adapter } = await setup(); const marker = new Uint8Array([1]); for (const key of [...remoteHeads, ...unrelated]) { await adapter.save(key, marker); } const host = await openHost(runtime); expect(await adapter.loadRange([SUBDUCTION_PREFIX, 'remote-heads'])).toEqual([]); for (const key of unrelated) { expect(await adapter.load(key), key.join('/')).toEqual(marker); } expect(await applied(runtime, deleteRemoteHeads.name)).toBe(true); expect(await applied(runtime, selfCheckpointedFragments.name)).toBe(true); await host.close(); // Recorded: records written after the first open are the live cache, and stay. for (const key of remoteHeads) { await adapter.save(key, marker); } await openHost(runtime); expect(await adapter.loadRange([SUBDUCTION_PREFIX, 'remote-heads'])).toHaveLength(remoteHeads.length); }); }); describe('0002_self_checkpointed_fragments', () => { /** A fragment-only document: its whole history in one blob, the old self-checkpointed shape. */ const syntheticDocument = () => { let doc = A.from<{ text: string }>({ text: 'one' }); doc = A.change(doc, (draft) => { draft.text = 'two'; }); doc = A.change(doc, (draft) => { draft.text = 'three'; }); const [head] = A.getHeads(doc); return { doc, headBytes: hexToBytes(head), blob: A.save(doc) }; }; test('rewrites the stored fragment on open; the engine then lists the head and serves the same bytes', async () => { const { runtime, adapter } = await setup(); const { doc, headBytes, blob } = syntheticDocument(); const sedimentreeBytes = padTo32(PublicKey.random().asUint8Array().slice(0, 16)); const planted = await plantOldShapeFragment(adapter, { sedimentreeBytes, headBytes, blob }); // Before: the stored records yield no head. expect(await headsSeenBy(adapter)).toEqual([{ id: bytesToHex(sedimentreeBytes), heads: [] }]); const host = await openHost(runtime); // The record chunk was replaced under its key with the valid shape; the blob chunk is untouched. const rewritten = await adapter.load(planted.fragmentKey); expect(rewritten).toBeDefined(); expect(rewritten).not.toEqual(planted.signed); const record = parseSignedFragmentRecord(rewritten!); expect(record?.head).toEqual(headBytes); expect(record?.boundary).toEqual([]); expect(record?.checkpoints).toEqual([]); expect(selfCheckpointRepair(rewritten!)).toBeUndefined(); expect(await adapter.load(planted.blobKey)).toEqual(blob); expect(await applied(runtime, selfCheckpointedFragments.name)).toBe(true); // After: the host's own engine and a fresh one over the rows both report the head, and the // engine hands out the very blob that was stored — the document it carries is unchanged. const subduction = await host.subduction; const allHeads = await subduction.getAllHeads(); expect(allHeads.map((entry) => entry.heads.map((head) => head.toHexString()))).toEqual([[bytesToHex(headBytes)]]); expect(await headsSeenBy(adapter)).toEqual([ { id: bytesToHex(sedimentreeBytes), heads: [bytesToHex(headBytes)] }, ]); const blobs = await subduction.getBlobs(SedimentreeId.fromBytes(sedimentreeBytes)); expect(blobs).toEqual([blob]); const loaded = A.load<{ text: string }>(blobs[0]); expect(loaded.text).toEqual('three'); expect(A.getHeads(loaded)).toEqual(A.getHeads(doc)); }); test('a document the host wrote reopens unchanged, with the head the old fragment hid restored', async () => { const { runtime, adapter } = await setup(); const host = await openHost(runtime); const created = await host.createDoc<{ text: string }>({ text: 'first' }); created.change((draft) => { draft.text = 'second'; }); await host.flush(Context.default()); const url = created.url; const documentId = created.documentId; const doc = created.doc()!; const [head] = A.getHeads(doc); const sedimentreeBytes = padTo32(bs58check.decode(documentId)); await host.close(); // A pre-3.5 client's fragment over the same history, at the document's head: the loose // commits the host stored are valid, but the fragment's own checkpoint hides the head. The // ledger is cleared with it: a profile that build wrote has no ledger yet. await plantOldShapeFragment(adapter, { sedimentreeBytes, headBytes: hexToBytes(head), blob: A.save(doc) }); await forgetMigrations(runtime); const [before] = await headsSeenBy(adapter); expect(before.heads).not.toContain(head); const reopened = await openHost(runtime); const lease = await reopened.loadDoc<{ text: string }>(Context.default(), url); expect(lease).not.toBeNull(); await lease!.waitUntilReady(); expect(lease!.doc()?.text).toEqual('second'); expect(A.getHeads(lease!.doc()!)).toEqual([head]); const allHeads = await (await reopened.subduction).getAllHeads(); const entry = allHeads.find((candidate) => candidate.id.toString() === bytesToHex(sedimentreeBytes)); expect(entry?.heads.map((commitId) => commitId.toHexString())).toEqual([head]); }); test('a second open changes nothing', async () => { const { runtime, adapter } = await setup(); const { headBytes, blob } = syntheticDocument(); const sedimentreeBytes = padTo32(PublicKey.random().asUint8Array().slice(0, 16)); const planted = await plantOldShapeFragment(adapter, { sedimentreeBytes, headBytes, blob }); const host = await openHost(runtime); const rewritten = await adapter.load(planted.fragmentKey); await host.close(); await openHost(runtime); expect(await adapter.load(planted.fragmentKey)).toEqual(rewritten); expect(await adapter.load(planted.blobKey)).toEqual(blob); }); // The migration is recorded once, but the shape keeps arriving: clients on automerge < 3.5 still write it, and a // peer forwards a fragment with the signature it was received with. Stored as received, it would be uploaded to // the next empty EDGE store on its own sync, where it hides the document's head again. test('rewrites an old-shape fragment received after the migration was recorded, on the next open', async () => { const { runtime, adapter } = await setup(); const host = await openHost(runtime); expect(await applied(runtime, selfCheckpointedFragments.name)).toBe(true); await host.close(); const { headBytes, blob } = syntheticDocument(); const sedimentreeBytes = padTo32(PublicKey.random().asUint8Array().slice(0, 16)); const planted = await plantOldShapeFragment(adapter, { sedimentreeBytes, headBytes, blob }); await openHost(runtime); const rewritten = await adapter.load(planted.fragmentKey); expect(selfCheckpointRepair(rewritten!)).toBeUndefined(); expect(await headsSeenBy(adapter)).toEqual([ { id: bytesToHex(sedimentreeBytes), heads: [bytesToHex(headBytes)] }, ]); }); // What a client on @automerge/automerge 3.3.2 stored for a document whose last edit closed a // fragment: one fragment at the document's head listing itself as a checkpoint, produced by // that version's own `getFragmentMetadata` / `bundleFragmentMetadata` (see the fixture). test('rewrites the fragments a client on automerge 3.3.2 stored, and the document survives', async () => { const { runtime, adapter } = await setup(); const sedimentreeBytes = padTo32(hexToBytes(fixture.documentIdBytesHex)); const [head] = fixture.heads; expect(fixture.fragments.some((fragment) => fragment.checkpoints.includes(fragment.head))).toBe(true); const planted = await plantOldClientRecords(adapter, sedimentreeBytes); // Before: the document's head is missing, so it would be reported missing on the remote. const [before] = await headsSeenBy(adapter); expect(before.heads).not.toContain(head); const host = await openHost(runtime); for (const { fragmentKey, blobKey, bytes, checkpoints } of planted.fragments) { const rewritten = await adapter.load(fragmentKey); expect(selfCheckpointRepair(rewritten!)).toBeUndefined(); const record = parseSignedFragmentRecord(rewritten!); expect(record?.checkpoints).toEqual(checkpoints.map((id) => hexToBytes(id).slice(0, 12))); expect(await adapter.load(blobKey)).toEqual(bytes); } const subduction = await host.subduction; const allHeads = await subduction.getAllHeads(); expect(allHeads.map((entry) => entry.heads.map((commitId) => commitId.toHexString()))).toEqual([[head]]); // The stored history still loads to the document the old client had. let loaded = A.init<{ text: string }>(); for (const blob of await subduction.getBlobs(SedimentreeId.fromBytes(sedimentreeBytes))) { loaded = A.loadIncremental(loaded, blob); } expect(loaded.text).toEqual(fixture.text); expect(A.getHeads(loaded)).toEqual(fixture.heads); expect(A.getAllChanges(loaded)).toHaveLength(fixture.changes); }); // A loaded tree keeps one in-memory fragment per head, the lower digest, and the digest // depends only on the fragment payload — so for about half of all document ids the old // fragment would win over its rewrite for the rest of the session. A document id where it // does is mined here, and the host is then shown to report the head anyway: the rewrite // landed before the engine loaded the tree. test('runs before the engine loads the tree', async () => { const { runtime, adapter } = await setup(); const { headBytes, blob } = syntheticDocument(); const sedimentreeBytes = await mineSedimentreeWhereOldFragmentWins({ headBytes, blob }); await plantOldShapeFragment(adapter, { sedimentreeBytes, headBytes, blob }); const host = await openHost(runtime); const subduction = await host.subduction; const allHeads = await subduction.getAllHeads(); expect(allHeads.map((entry) => entry.heads.map((head) => head.toHexString()))).toEqual([[bytesToHex(headBytes)]]); }); }); }); type OldShapeFragment = { sedimentreeBytes: Uint8Array; headBytes: Uint8Array; blob: Uint8Array }; /** * Stores `fragment` into `storage` in the old shape — the head listed as its own checkpoint — * through a throwaway engine, so the record is byte-for-byte what a pre-3.5 client wrote. */ const storeOldShape = async (storage: SedimentreeStorage, { sedimentreeBytes, headBytes, blob }: OldShapeFragment) => { const engine = new Subduction({ signer: MemorySigner.generate(), storage }); await engine.storeFragment( SedimentreeId.fromBytes(sedimentreeBytes), CommitId.fromBytes(headBytes), [], [CommitId.fromBytes(headBytes)], blob, ); }; /** Writes the old shape into the adapter under the keys `SubductionStorageBridge` uses. */ const plantOldShapeFragment = async (adapter: SqliteStorageAdapter, fragment: OldShapeFragment) => { const captured = memoryStorage(); await storeOldShape(captured, fragment); const [row] = [...captured.fragments.values()]; const sedimentreeHex = bytesToHex(fragment.sedimentreeBytes); const headHex = bytesToHex(fragment.headBytes); const fragmentKey = [SUBDUCTION_PREFIX, 'fragments', sedimentreeHex, headHex]; const blobKey = [SUBDUCTION_PREFIX, 'fragment-blobs', sedimentreeHex, headHex]; await adapter.saveBatch([ [[SUBDUCTION_PREFIX, 'ids', sedimentreeHex], new Uint8Array([1])], [fragmentKey, row.signed], [blobKey, fragment.blob], ]); return { fragmentKey, blobKey, signed: row.signed }; }; /** * Stores the fixture into the adapter through a throwaway engine, with the inputs the old client * built (a loose commit per level-0 entry with its boundary as parents; a fragment per level-1+ * entry with its metadata verbatim), under the keys `SubductionStorageBridge` uses. */ const plantOldClientRecords = async (adapter: SqliteStorageAdapter, sedimentreeBytes: Uint8Array) => { const sedimentreeId = SedimentreeId.fromBytes(sedimentreeBytes); const fromBase64 = (encoded: string) => Uint8Array.from(Buffer.from(encoded, 'base64')); const commits = fixture.commits.map((commit) => { const bytes = fromBase64(commit.bytes); const parents = commit.parents.map((id) => CommitId.fromHexString(id)); return new CommitInput( new LooseCommit(sedimentreeId, CommitId.fromHexString(commit.id), parents, new BlobMeta(bytes)), bytes, ); }); const fragments = fixture.fragments.map((fragment) => { const bytes = fromBase64(fragment.bytes); return new FragmentInput( new Fragment( sedimentreeId, CommitId.fromHexString(fragment.head), fragment.boundary.map((id) => CommitId.fromHexString(id)), fragment.checkpoints.map((id) => CommitId.fromHexString(id)), new BlobMeta(bytes), ), bytes, ); }); const captured = memoryStorage(); const engine = new Subduction({ signer: MemorySigner.generate(), storage: captured }); await engine.storeBuiltBatch(sedimentreeId, commits, fragments); const sedimentreeHex = bytesToHex(sedimentreeBytes); const entries: Array<[string[], Uint8Array]> = [[[SUBDUCTION_PREFIX, 'ids', sedimentreeHex], new Uint8Array([1])]]; for (const [rowKey, row] of captured.commits) { const idHex = rowKey.split('/')[1]; entries.push([[SUBDUCTION_PREFIX, 'commits', sedimentreeHex, idHex], row.signed]); entries.push([[SUBDUCTION_PREFIX, 'blobs', sedimentreeHex, idHex], row.blob]); } const plantedFragments = fixture.fragments.map((fragment) => { const row = captured.fragments.get(`${sedimentreeHex}/${fragment.head}`)!; const fragmentKey = [SUBDUCTION_PREFIX, 'fragments', sedimentreeHex, fragment.head]; const blobKey = [SUBDUCTION_PREFIX, 'fragment-blobs', sedimentreeHex, fragment.head]; entries.push([fragmentKey, row.signed], [blobKey, row.blob]); return { fragmentKey, blobKey, bytes: fromBase64(fragment.bytes), checkpoints: fragment.checkpoints.filter((id) => id !== fragment.head && !fragment.boundary.includes(id)), }; }); await adapter.saveBatch(entries); return { fragments: plantedFragments }; }; /** Heads per sedimentree as a fresh engine over the adapter's rows reports them. */ const headsSeenBy = async (adapter: SqliteStorageAdapter) => { const chunks = await adapter.loadRange([SUBDUCTION_PREFIX]); const storage = memoryStorage(); for (const { key, data } of chunks) { if (!data) { continue; } if (key[1] === 'ids') { storage.ids.add(key[2]); } else if (key[1] === 'commits') { const blob = chunks.find( (chunk) => chunk.key.join('/') === [SUBDUCTION_PREFIX, 'blobs', key[2], key[3]].join('/'), ); storage.commits.set(`${key[2]}/${key[3]}`, { signed: data, blob: blob!.data! }); } else if (key[1] === 'fragments') { const blob = chunks.find( (chunk) => chunk.key.join('/') === [SUBDUCTION_PREFIX, 'fragment-blobs', key[2], key[3]].join('/'), ); storage.fragments.set(`${key[2]}/${key[3]}`, { signed: data, blob: blob!.data! }); } } const engine = new Subduction({ signer: MemorySigner.generate(), storage }); const all = await engine.getAllHeads(); return all.map((entry) => ({ id: entry.id.toString(), heads: entry.heads.map((head) => head.toHexString()) })); }; /** * A sedimentree id for which rewriting the old fragment under an already-loaded tree loses the * tiebreak — shown directly: the engine still reports no head after the rewrite. */ const mineSedimentreeWhereOldFragmentWins = async ({ headBytes, blob }: Omit) => { for (let attempt = 0; attempt < 64; attempt++) { const sedimentreeBytes = padTo32(PublicKey.random().asUint8Array().slice(0, 16)); const storage = memoryStorage(); await storeOldShape(storage, { sedimentreeBytes, headBytes, blob }); const engine = new Subduction({ signer: MemorySigner.generate(), storage }); const sedimentreeId = SedimentreeId.fromBytes(sedimentreeBytes); await engine.getAllHeads(); const fragment = new Fragment(sedimentreeId, CommitId.fromBytes(headBytes), [], [], new BlobMeta(blob)); await engine.storeBuiltBatch(sedimentreeId, [], [new FragmentInput(fragment, blob)]); const [entry] = await engine.getAllHeads(); if (entry.heads.length === 0) { return sedimentreeBytes; } } throw new Error('no sedimentree id where the old fragment wins the tiebreak in 64 attempts'); }; type StoredRow = { signed: Uint8Array; blob: Uint8Array }; type MemoryStorage = SedimentreeStorage & { ids: Set; commits: Map; fragments: Map; }; /** An in-memory storage the engine accepts, keeping the signed rows it writes. */ const memoryStorage = (): MemoryStorage => { const ids = new Set(); const commits = new Map(); const fragments = new Map(); const commitsOf = (sedimentreeId: SedimentreeId) => [...commits.entries()].filter(([key]) => key.startsWith(`${sedimentreeId.toString()}/`)).map(([, row]) => row); const commitWithBlob = (row: StoredRow): CommitWithBlob => new CommitWithBlob(SignedLooseCommit.tryDecode(row.signed), row.blob); const rowKey = (sedimentreeId: SedimentreeId, id: CommitId) => `${sedimentreeId.toString()}/${id.toHexString()}`; const rowsOf = (sedimentreeId: SedimentreeId) => [...fragments.entries()].filter(([key]) => key.startsWith(`${sedimentreeId.toString()}/`)).map(([, row]) => row); const fragmentWithBlob = (row: { signed: Uint8Array; blob: Uint8Array }): FragmentWithBlob => new FragmentWithBlob(SignedFragment.tryDecode(row.signed), row.blob); return { ids, commits, fragments, saveSedimentreeId: async (sedimentreeId) => void ids.add(sedimentreeId.toString()), deleteSedimentreeId: async (sedimentreeId) => void ids.delete(sedimentreeId.toString()), loadAllSedimentreeIds: async () => [...ids].map((hex) => SedimentreeId.fromBytes(hexToBytes(hex))), saveCommit: async (sedimentreeId, commitId, signedCommit, blob) => { commits.set(rowKey(sedimentreeId, commitId), { signed: new Uint8Array(signedCommit.encode()), blob }); }, loadCommit: async (sedimentreeId, commitId) => { const row = commits.get(rowKey(sedimentreeId, commitId)); return row ? commitWithBlob(row) : null; }, listCommitIds: async (sedimentreeId) => [...commits.keys()] .filter((key) => key.startsWith(`${sedimentreeId.toString()}/`)) .map((key) => CommitId.fromHexString(key.split('/')[1])), loadAllCommits: async (sedimentreeId) => commitsOf(sedimentreeId).map(commitWithBlob), deleteCommit: async (sedimentreeId, commitId) => void commits.delete(rowKey(sedimentreeId, commitId)), deleteAllCommits: async () => {}, saveFragment: async (sedimentreeId, head, signedFragment, blob) => { fragments.set(rowKey(sedimentreeId, head), { signed: new Uint8Array(signedFragment.encode()), blob }); }, loadFragment: async (sedimentreeId, head) => { const row = fragments.get(rowKey(sedimentreeId, head)); return row ? fragmentWithBlob(row) : null; }, listFragmentIds: async (sedimentreeId) => [...fragments.keys()] .filter((key) => key.startsWith(`${sedimentreeId.toString()}/`)) .map((key) => CommitId.fromHexString(key.split('/')[1])), loadAllFragments: async (sedimentreeId) => rowsOf(sedimentreeId).map(fragmentWithBlob), deleteFragment: async (sedimentreeId, head) => void fragments.delete(rowKey(sedimentreeId, head)), deleteAllFragments: async (sedimentreeId) => { for (const key of [...fragments.keys()]) { if (key.startsWith(`${sedimentreeId.toString()}/`)) { fragments.delete(key); } } }, saveBatchAll: async (sedimentreeId, commitBatch, fragmentBatch) => { for (const { commitId, signedCommit, blob } of commitBatch) { commits.set(rowKey(sedimentreeId, commitId), { signed: new Uint8Array(signedCommit.encode()), blob }); } for (const { fragmentHead, signedFragment, blob } of fragmentBatch) { fragments.set(rowKey(sedimentreeId, fragmentHead), { signed: new Uint8Array(signedFragment.encode()), blob }); } return commitBatch.length + fragmentBatch.length; }, }; }; const padTo32 = (bytes: Uint8Array): Uint8Array => { const out = new Uint8Array(32); out.set(bytes); return out; }; const bytesToHex = (bytes: Uint8Array): string => Array.from(bytes, (byte) => byte.toString(16).padStart(2, '0')).join(''); const hexToBytes = (hex: string): Uint8Array => Uint8Array.from({ length: hex.length / 2 }, (_, index) => parseInt(hex.slice(index * 2, index * 2 + 2), 16));