@aztec/kv-store 0.0.1-commit.9b94fc1 → 0.0.1-commit.9badcec54

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.
Files changed (130) hide show
  1. package/dest/bench/shared_map_bench.d.ts +19 -0
  2. package/dest/bench/shared_map_bench.d.ts.map +1 -0
  3. package/dest/bench/shared_map_bench.js +91 -0
  4. package/dest/indexeddb/array.js +21 -7
  5. package/dest/indexeddb/index.d.ts +3 -3
  6. package/dest/indexeddb/index.d.ts.map +1 -1
  7. package/dest/indexeddb/index.js +3 -6
  8. package/dest/indexeddb/map.d.ts +8 -2
  9. package/dest/indexeddb/map.d.ts.map +1 -1
  10. package/dest/indexeddb/map.js +23 -13
  11. package/dest/indexeddb/multi_map.d.ts +2 -1
  12. package/dest/indexeddb/multi_map.d.ts.map +1 -1
  13. package/dest/indexeddb/multi_map.js +16 -1
  14. package/dest/indexeddb/singleton.js +3 -1
  15. package/dest/indexeddb/store.d.ts +3 -3
  16. package/dest/indexeddb/store.d.ts.map +1 -1
  17. package/dest/indexeddb/store.js +6 -4
  18. package/dest/interfaces/map_test_suite.d.ts +1 -1
  19. package/dest/interfaces/map_test_suite.d.ts.map +1 -1
  20. package/dest/interfaces/map_test_suite.js +48 -2
  21. package/dest/interfaces/multi_map_test_suite.d.ts +1 -1
  22. package/dest/interfaces/multi_map_test_suite.d.ts.map +1 -1
  23. package/dest/interfaces/multi_map_test_suite.js +25 -0
  24. package/dest/interfaces/utils.d.ts +2 -1
  25. package/dest/interfaces/utils.d.ts.map +1 -1
  26. package/dest/interfaces/utils.js +2 -1
  27. package/dest/lmdb/array.js +4 -2
  28. package/dest/lmdb/index.d.ts +3 -3
  29. package/dest/lmdb/index.d.ts.map +1 -1
  30. package/dest/lmdb/index.js +3 -3
  31. package/dest/lmdb/store.d.ts +3 -3
  32. package/dest/lmdb/store.d.ts.map +1 -1
  33. package/dest/lmdb/store.js +13 -9
  34. package/dest/lmdb-v2/array.d.ts +2 -2
  35. package/dest/lmdb-v2/array.d.ts.map +1 -1
  36. package/dest/lmdb-v2/array.js +4 -3
  37. package/dest/lmdb-v2/factory.d.ts +7 -7
  38. package/dest/lmdb-v2/factory.d.ts.map +1 -1
  39. package/dest/lmdb-v2/factory.js +14 -10
  40. package/dest/lmdb-v2/map.d.ts +2 -2
  41. package/dest/lmdb-v2/map.d.ts.map +1 -1
  42. package/dest/lmdb-v2/map.js +1 -2
  43. package/dest/lmdb-v2/multi_map.d.ts +2 -2
  44. package/dest/lmdb-v2/multi_map.d.ts.map +1 -1
  45. package/dest/lmdb-v2/multi_map.js +1 -2
  46. package/dest/lmdb-v2/singleton.d.ts +2 -2
  47. package/dest/lmdb-v2/singleton.d.ts.map +1 -1
  48. package/dest/lmdb-v2/singleton.js +1 -2
  49. package/dest/lmdb-v2/store.d.ts +4 -5
  50. package/dest/lmdb-v2/store.d.ts.map +1 -1
  51. package/dest/lmdb-v2/store.js +3 -25
  52. package/dest/lmdb-v2/tx-helpers.d.ts +6 -0
  53. package/dest/lmdb-v2/tx-helpers.d.ts.map +1 -0
  54. package/dest/lmdb-v2/tx-helpers.js +21 -0
  55. package/dest/lmdb-v2/utils.d.ts +1 -10
  56. package/dest/lmdb-v2/utils.d.ts.map +1 -1
  57. package/dest/lmdb-v2/utils.js +0 -94
  58. package/dest/lmdb-v2/write_transaction.d.ts +1 -1
  59. package/dest/lmdb-v2/write_transaction.d.ts.map +1 -1
  60. package/dest/lmdb-v2/write_transaction.js +9 -5
  61. package/dest/sqlite-opfs/array.d.ts +21 -0
  62. package/dest/sqlite-opfs/array.d.ts.map +1 -0
  63. package/dest/sqlite-opfs/array.js +128 -0
  64. package/dest/sqlite-opfs/index.d.ts +7 -0
  65. package/dest/sqlite-opfs/index.d.ts.map +1 -0
  66. package/dest/sqlite-opfs/index.js +13 -0
  67. package/dest/sqlite-opfs/map.d.ts +35 -0
  68. package/dest/sqlite-opfs/map.d.ts.map +1 -0
  69. package/dest/sqlite-opfs/map.js +163 -0
  70. package/dest/sqlite-opfs/messages.d.ts +58 -0
  71. package/dest/sqlite-opfs/messages.d.ts.map +1 -0
  72. package/dest/sqlite-opfs/messages.js +5 -0
  73. package/dest/sqlite-opfs/multi_map.d.ts +16 -0
  74. package/dest/sqlite-opfs/multi_map.d.ts.map +1 -0
  75. package/dest/sqlite-opfs/multi_map.js +67 -0
  76. package/dest/sqlite-opfs/set.d.ts +13 -0
  77. package/dest/sqlite-opfs/set.d.ts.map +1 -0
  78. package/dest/sqlite-opfs/set.js +19 -0
  79. package/dest/sqlite-opfs/singleton.d.ts +13 -0
  80. package/dest/sqlite-opfs/singleton.d.ts.map +1 -0
  81. package/dest/sqlite-opfs/singleton.js +48 -0
  82. package/dest/sqlite-opfs/store.d.ts +70 -0
  83. package/dest/sqlite-opfs/store.d.ts.map +1 -0
  84. package/dest/sqlite-opfs/store.js +242 -0
  85. package/dest/sqlite-opfs/worker.d.ts +2 -0
  86. package/dest/sqlite-opfs/worker.d.ts.map +1 -0
  87. package/dest/sqlite-opfs/worker.js +189 -0
  88. package/dest/stores/l2_tips_store.d.ts +24 -9
  89. package/dest/stores/l2_tips_store.d.ts.map +1 -1
  90. package/dest/stores/l2_tips_store.js +61 -52
  91. package/dest/utils.d.ts +9 -6
  92. package/dest/utils.d.ts.map +1 -1
  93. package/dest/utils.js +51 -16
  94. package/package.json +22 -16
  95. package/src/bench/shared_map_bench.ts +111 -0
  96. package/src/indexeddb/array.ts +4 -4
  97. package/src/indexeddb/index.ts +9 -7
  98. package/src/indexeddb/map.ts +24 -11
  99. package/src/indexeddb/multi_map.ts +15 -1
  100. package/src/indexeddb/singleton.ts +1 -1
  101. package/src/indexeddb/store.ts +13 -6
  102. package/src/interfaces/map_test_suite.ts +30 -2
  103. package/src/interfaces/multi_map_test_suite.ts +32 -0
  104. package/src/interfaces/utils.ts +1 -0
  105. package/src/lmdb/index.ts +9 -4
  106. package/src/lmdb/store.ts +13 -9
  107. package/src/lmdb-v2/array.ts +2 -2
  108. package/src/lmdb-v2/factory.ts +16 -12
  109. package/src/lmdb-v2/map.ts +2 -2
  110. package/src/lmdb-v2/multi_map.ts +2 -2
  111. package/src/lmdb-v2/singleton.ts +2 -2
  112. package/src/lmdb-v2/store.ts +5 -31
  113. package/src/lmdb-v2/tx-helpers.ts +29 -0
  114. package/src/lmdb-v2/utils.ts +0 -118
  115. package/src/lmdb-v2/write_transaction.ts +9 -8
  116. package/src/sqlite-opfs/array.ts +124 -0
  117. package/src/sqlite-opfs/index.ts +27 -0
  118. package/src/sqlite-opfs/map.ts +163 -0
  119. package/src/sqlite-opfs/messages.ts +28 -0
  120. package/src/sqlite-opfs/multi_map.ts +74 -0
  121. package/src/sqlite-opfs/set.ts +29 -0
  122. package/src/sqlite-opfs/singleton.ts +48 -0
  123. package/src/sqlite-opfs/store.ts +248 -0
  124. package/src/sqlite-opfs/worker.ts +157 -0
  125. package/src/stores/l2_tips_store.ts +65 -56
  126. package/src/utils.ts +79 -21
  127. package/dest/config.d.ts +0 -17
  128. package/dest/config.d.ts.map +0 -1
  129. package/dest/config.js +0 -26
  130. package/src/config.ts +0 -36
@@ -0,0 +1,29 @@
1
+ import type { Key, Range } from '../interfaces/common.js';
2
+ import type { AztecAsyncSet } from '../interfaces/set.js';
3
+ import { SQLiteOPFSAztecMap } from './map.js';
4
+ import type { AztecSQLiteOPFSStore } from './store.js';
5
+
6
+ /** Set backed by SQLite. Composes a Map<K, true>. */
7
+ export class SQLiteOPFSAztecSet<K extends Key> implements AztecAsyncSet<K> {
8
+ readonly #map: SQLiteOPFSAztecMap<K, boolean>;
9
+
10
+ constructor(store: AztecSQLiteOPFSStore, name: string) {
11
+ this.#map = new SQLiteOPFSAztecMap<K, boolean>(store, name);
12
+ }
13
+
14
+ hasAsync(key: K): Promise<boolean> {
15
+ return this.#map.hasAsync(key);
16
+ }
17
+
18
+ add(key: K): Promise<void> {
19
+ return this.#map.set(key, true);
20
+ }
21
+
22
+ delete(key: K): Promise<void> {
23
+ return this.#map.delete(key);
24
+ }
25
+
26
+ async *entriesAsync(range: Range<K> = {}): AsyncIterableIterator<K> {
27
+ yield* this.#map.keysAsync(range);
28
+ }
29
+ }
@@ -0,0 +1,48 @@
1
+ import { Encoder } from 'msgpackr';
2
+ import { hash } from 'ohash';
3
+ import { toBufferKey } from 'ordered-binary';
4
+
5
+ import type { Value } from '../interfaces/common.js';
6
+ import type { AztecAsyncSingleton } from '../interfaces/singleton.js';
7
+ import type { AztecSQLiteOPFSStore } from './store.js';
8
+
9
+ /** Stores a single value identified by `name`. */
10
+ export class SQLiteOPFSAztecSingleton<T extends Value> implements AztecAsyncSingleton<T> {
11
+ readonly #container: string;
12
+ readonly #slot: string;
13
+ readonly #encoder = new Encoder();
14
+
15
+ constructor(
16
+ private readonly store: AztecSQLiteOPFSStore,
17
+ name: string,
18
+ ) {
19
+ this.#container = `singleton:${name}`;
20
+ this.#slot = `singleton:${name}:value`;
21
+ }
22
+
23
+ async getAsync(): Promise<T | undefined> {
24
+ const rows = await this.store.allAsync('SELECT value FROM data WHERE slot = ? LIMIT 1', [this.#slot]);
25
+ if (rows.length === 0) {
26
+ return undefined;
27
+ }
28
+ const raw = rows[0][0];
29
+ if (raw instanceof Uint8Array) {
30
+ return this.#encoder.unpack(raw) as T;
31
+ }
32
+ return undefined;
33
+ }
34
+
35
+ async set(val: T): Promise<boolean> {
36
+ const { changes } = await this.store.runAsync(
37
+ `INSERT OR REPLACE INTO data (slot, container, key, key_count, hash, value)
38
+ VALUES (?, ?, ?, ?, ?, ?)`,
39
+ [this.#slot, this.#container, toBufferKey([this.#slot]), 1, hash(val), this.#encoder.pack(val)],
40
+ );
41
+ return changes > 0;
42
+ }
43
+
44
+ async delete(): Promise<boolean> {
45
+ await this.store.runAsync('DELETE FROM data WHERE slot = ?', [this.#slot]);
46
+ return true;
47
+ }
48
+ }
@@ -0,0 +1,248 @@
1
+ import type { Logger } from '@aztec/foundation/log';
2
+ import { SerialQueue } from '@aztec/foundation/queue';
3
+
4
+ import type { AztecAsyncArray } from '../interfaces/array.js';
5
+ import type { Key, StoreSize, Value } from '../interfaces/common.js';
6
+ import type { AztecAsyncCounter } from '../interfaces/counter.js';
7
+ import type { AztecAsyncMap } from '../interfaces/map.js';
8
+ import type { AztecAsyncMultiMap } from '../interfaces/multi_map.js';
9
+ import type { AztecAsyncSet } from '../interfaces/set.js';
10
+ import type { AztecAsyncSingleton } from '../interfaces/singleton.js';
11
+ import type { AztecAsyncKVStore } from '../interfaces/store.js';
12
+ import { SQLiteOPFSAztecArray } from './array.js';
13
+ import { SQLiteOPFSAztecMap } from './map.js';
14
+ import type { ResultRow, SqlValue, WorkerRequest, WorkerResponse } from './messages.js';
15
+ import { SQLiteOPFSAztecMultiMap } from './multi_map.js';
16
+ import { SQLiteOPFSAztecSet } from './set.js';
17
+ import { SQLiteOPFSAztecSingleton } from './singleton.js';
18
+
19
+ /**
20
+ * Main-thread handle for a SQLite database persisted to OPFS via the `opfs-sahpool`
21
+ * VFS. Owns a dedicated Web Worker (the SAH Pool VFS requires Worker context) and
22
+ * routes every SQL op through it via typed postMessage RPC.
23
+ *
24
+ * Transaction ordering is guaranteed by a `SerialQueue` on the main thread combined
25
+ * with an `#inTx` flag: outside a `transactionAsync` block, each op acquires the
26
+ * queue for its own auto-commit; inside a block, the outer call holds the queue and
27
+ * nested ops bypass it to avoid deadlock.
28
+ */
29
+ export class AztecSQLiteOPFSStore implements AztecAsyncKVStore {
30
+ readonly #worker: Worker;
31
+ readonly #pending = new Map<number, { resolve: (r: WorkerResponse) => void; reject: (err: Error) => void }>();
32
+ readonly #txQueue = new SerialQueue();
33
+ readonly #name: string;
34
+ readonly #log: Logger;
35
+ #nextId = 0;
36
+ #inTx = false;
37
+ #closed = false;
38
+
39
+ private constructor(
40
+ worker: Worker,
41
+ name: string,
42
+ log: Logger,
43
+ public readonly isEphemeral: boolean,
44
+ ) {
45
+ this.#worker = worker;
46
+ this.#name = name;
47
+ this.#log = log;
48
+ this.#worker.onmessage = (ev: MessageEvent<WorkerResponse>) => {
49
+ const { id } = ev.data;
50
+ const handler = this.#pending.get(id);
51
+ if (!handler) {
52
+ this.#log.warn(`SQLite worker: no pending handler for id ${id}`);
53
+ return;
54
+ }
55
+ this.#pending.delete(id);
56
+ handler.resolve(ev.data);
57
+ };
58
+ this.#worker.onerror = ev => {
59
+ this.#log.error(`SQLite worker crashed: ${ev.message}`);
60
+ this.#rejectPending(`SQLite worker crashed: ${ev.message}`);
61
+ };
62
+ this.#txQueue.start();
63
+ }
64
+
65
+ /**
66
+ * Opens (or creates) a SQLite database stored in the OPFS SAH Pool. When `ephemeral`
67
+ * is true the database lives only in memory and is lost when the worker terminates.
68
+ * Pass `poolDirectory` to place the SAH Pool in a non-default OPFS subdirectory —
69
+ * required when multiple stores coexist in the same tab, because the SAH Pool holds
70
+ * an exclusive lock on its directory.
71
+ */
72
+ static async open(
73
+ log: Logger,
74
+ name?: string,
75
+ ephemeral: boolean = false,
76
+ poolDirectory?: string,
77
+ ): Promise<AztecSQLiteOPFSStore> {
78
+ const dbName = name && !ephemeral ? name : `tmp-${globalThis.crypto.getRandomValues(new Uint8Array(8)).join('')}`;
79
+ log.debug(`Opening SQLite-OPFS ${ephemeral ? 'ephemeral ' : ''}database ${dbName}`);
80
+ const worker = new Worker(new URL('./worker.js', import.meta.url), { type: 'module' });
81
+ const store = new AztecSQLiteOPFSStore(worker, dbName, log, ephemeral);
82
+ await store.#sendRequest({ type: 'init', id: store.#allocId(), dbName, ephemeral, poolDirectory });
83
+ return store;
84
+ }
85
+
86
+ openMap<K extends Key, V extends Value>(name: string): AztecAsyncMap<K, V> {
87
+ return new SQLiteOPFSAztecMap<K, V>(this, name);
88
+ }
89
+
90
+ openSet<K extends Key>(name: string): AztecAsyncSet<K> {
91
+ return new SQLiteOPFSAztecSet<K>(this, name);
92
+ }
93
+
94
+ openMultiMap<K extends Key, V extends Value>(name: string): AztecAsyncMultiMap<K, V> {
95
+ return new SQLiteOPFSAztecMultiMap<K, V>(this, name);
96
+ }
97
+
98
+ openCounter<K extends Key>(_name: string): AztecAsyncCounter<K> {
99
+ throw new Error('Method not implemented.');
100
+ }
101
+
102
+ openArray<T extends Value>(name: string): AztecAsyncArray<T> {
103
+ return new SQLiteOPFSAztecArray<T>(this, name);
104
+ }
105
+
106
+ openSingleton<T extends Value>(name: string): AztecAsyncSingleton<T> {
107
+ return new SQLiteOPFSAztecSingleton<T>(this, name);
108
+ }
109
+
110
+ transactionAsync<T>(callback: () => Promise<T>): Promise<T> {
111
+ // Nested calls join the outer transaction — SQLite does not support nested BEGIN,
112
+ // and re-acquiring the SerialQueue while the outer call holds it would deadlock.
113
+ // Errors in the nested callback propagate to the outer catch, which rolls back the
114
+ // whole thing (the standard "nested tx = savepoint-free join" semantic).
115
+ if (this.#inTx) {
116
+ return callback();
117
+ }
118
+ return this.#txQueue.put(async () => {
119
+ this.#inTx = true;
120
+ await this.#sendRequest({ type: 'begin', id: this.#allocId() });
121
+ try {
122
+ const result = await callback();
123
+ await this.#sendRequest({ type: 'commit', id: this.#allocId() });
124
+ return result;
125
+ } catch (err) {
126
+ await this.#sendRequest({ type: 'rollback', id: this.#allocId() }).catch(rollbackErr =>
127
+ this.#log.warn(`SQLite ROLLBACK failed: ${rollbackErr instanceof Error ? rollbackErr.message : rollbackErr}`),
128
+ );
129
+ throw err;
130
+ } finally {
131
+ this.#inTx = false;
132
+ }
133
+ });
134
+ }
135
+
136
+ async clear(): Promise<void> {
137
+ await this.runAsync('DELETE FROM data');
138
+ }
139
+
140
+ async delete(): Promise<void> {
141
+ if (this.#closed) {
142
+ return;
143
+ }
144
+ this.#closed = true;
145
+ await this.#txQueue.end();
146
+ await this.#sendRequest({ type: 'deleteDb', id: this.#allocId(), dbName: this.#name }).catch(err =>
147
+ this.#log.warn(`SQLite deleteDb failed: ${err instanceof Error ? err.message : err}`),
148
+ );
149
+ this.#worker.terminate();
150
+ this.#rejectPending('SQLite store deleted');
151
+ }
152
+
153
+ /**
154
+ * Placeholder — returns zeros to mirror the IndexedDB backend. SQLite exposes real
155
+ * numbers cheaply via `PRAGMA page_count` / `page_size` / `freelist_count` and
156
+ * `SELECT COUNT(*) FROM data`, which would populate `physicalFileSize`, `actualSize`,
157
+ * and `numItems` meaningfully (`mappingSize` stays 0 — it's an LMDB mmap concept).
158
+ * Upgrade when any caller actually consumes these values; all current consumers
159
+ * tolerate zeros.
160
+ */
161
+ estimateSize(): Promise<StoreSize> {
162
+ return Promise.resolve({ mappingSize: 0, physicalFileSize: 0, actualSize: 0, numItems: 0 });
163
+ }
164
+
165
+ async close(): Promise<void> {
166
+ if (this.#closed) {
167
+ return;
168
+ }
169
+ this.#closed = true;
170
+ await this.#txQueue.end();
171
+ await this.#sendRequest({ type: 'close', id: this.#allocId() }).catch(() => {});
172
+ this.#worker.terminate();
173
+ this.#rejectPending('SQLite store closed');
174
+ }
175
+
176
+ backupTo(_dstPath: string, _compact?: boolean): Promise<void> {
177
+ throw new Error('Method not implemented.');
178
+ }
179
+
180
+ /**
181
+ * Returns a raw SQLite image (bytes suitable for writing as a `.sqlite` file and
182
+ * opening in any SQLite tool). Works only for non-ephemeral DBs because the OPFS
183
+ * SAH Pool has to be initialized. Useful for inspection/debugging.
184
+ */
185
+ async exportDb(): Promise<Uint8Array> {
186
+ const resp = await this.#sendRequest({ type: 'export', id: this.#allocId() });
187
+ if (!('bytes' in resp) || !resp.bytes) {
188
+ throw new Error('exportDb: worker returned no bytes');
189
+ }
190
+ return resp.bytes;
191
+ }
192
+
193
+ /**
194
+ * Runs a write statement (INSERT/UPDATE/DELETE/DDL). If called inside a
195
+ * `transactionAsync` block, bypasses the queue; otherwise acquires it so the
196
+ * op runs in its own auto-commit.
197
+ */
198
+ runAsync(sql: string, bind?: SqlValue[]): Promise<{ changes: number }> {
199
+ const send = () =>
200
+ this.#sendRequest({ type: 'run', id: this.#allocId(), sql, bind }).then(r => ({
201
+ changes: 'changes' in r ? (r.changes ?? 0) : 0,
202
+ }));
203
+ return this.#inTx ? send() : this.#txQueue.put(send);
204
+ }
205
+
206
+ /** Runs a SELECT statement and returns rows in array row-mode. */
207
+ allAsync(sql: string, bind?: SqlValue[]): Promise<ResultRow[]> {
208
+ const send = () =>
209
+ this.#sendRequest({ type: 'all', id: this.#allocId(), sql, bind }).then(r => ('rows' in r ? (r.rows ?? []) : []));
210
+ return this.#inTx ? send() : this.#txQueue.put(send);
211
+ }
212
+
213
+ #allocId(): number {
214
+ return ++this.#nextId;
215
+ }
216
+
217
+ /**
218
+ * Reject any in-flight requests with `reason`, so callers awaiting a response to a
219
+ * request sent to a now-terminated worker don't hang forever. Called from
220
+ * close()/delete() and from the worker.onerror handler.
221
+ */
222
+ #rejectPending(reason: string): void {
223
+ if (this.#pending.size === 0) {
224
+ return;
225
+ }
226
+ const err = new Error(reason);
227
+ for (const { reject } of this.#pending.values()) {
228
+ reject(err);
229
+ }
230
+ this.#pending.clear();
231
+ }
232
+
233
+ #sendRequest(req: WorkerRequest): Promise<WorkerResponse> {
234
+ return new Promise<WorkerResponse>((resolve, reject) => {
235
+ this.#pending.set(req.id, {
236
+ resolve: resp => {
237
+ if (resp.type === 'err') {
238
+ reject(new Error(resp.message));
239
+ } else {
240
+ resolve(resp);
241
+ }
242
+ },
243
+ reject,
244
+ });
245
+ this.#worker.postMessage(req);
246
+ });
247
+ }
248
+ }
@@ -0,0 +1,157 @@
1
+ /// <reference lib="webworker" />
2
+ import sqlite3InitModule, { type Database, type SAHPoolUtil, type Sqlite3Static } from '@sqlite.org/sqlite-wasm';
3
+
4
+ import type { ResultRow, SqlValue, WorkerRequest, WorkerResponse } from './messages.js';
5
+
6
+ const SCHEMA_SQL = `
7
+ CREATE TABLE IF NOT EXISTS data (
8
+ slot TEXT NOT NULL PRIMARY KEY,
9
+ container TEXT NOT NULL,
10
+ key BLOB NOT NULL,
11
+ key_count INTEGER NOT NULL,
12
+ hash TEXT NOT NULL,
13
+ value BLOB
14
+ ) WITHOUT ROWID;
15
+
16
+ CREATE INDEX IF NOT EXISTS idx_container_key ON data(container, key);
17
+ CREATE UNIQUE INDEX IF NOT EXISTS idx_container_key_count ON data(container, key, key_count);
18
+ CREATE UNIQUE INDEX IF NOT EXISTS idx_container_key_hash ON data(container, key, hash);
19
+ `;
20
+
21
+ const DEFAULT_SAH_POOL_DIRECTORY = '.aztec-kv';
22
+ const SAH_POOL_VFS_NAME = 'aztec-kv-opfs';
23
+
24
+ let sqlite3: Sqlite3Static | undefined;
25
+ let pool: SAHPoolUtil | undefined;
26
+ let poolDirectory: string | undefined;
27
+ let db: Database | undefined;
28
+ let dbPath: string | undefined;
29
+
30
+ async function ensurePool(directory: string): Promise<SAHPoolUtil> {
31
+ sqlite3 ??= await sqlite3InitModule();
32
+ if (!pool) {
33
+ poolDirectory = directory;
34
+ pool = await sqlite3.installOpfsSAHPoolVfs({
35
+ name: SAH_POOL_VFS_NAME,
36
+ directory,
37
+ initialCapacity: 8,
38
+ });
39
+ }
40
+ return pool;
41
+ }
42
+
43
+ async function handleInit(dbName: string, ephemeral: boolean, directory?: string): Promise<void> {
44
+ sqlite3 ??= await sqlite3InitModule();
45
+ if (ephemeral) {
46
+ db = new sqlite3.oo1.DB(':memory:', 'c');
47
+ } else {
48
+ const p = await ensurePool(directory ?? DEFAULT_SAH_POOL_DIRECTORY);
49
+ dbPath = normalizeDbPath(dbName);
50
+ db = new p.OpfsSAHPoolDb(dbPath);
51
+ }
52
+ runSql(SCHEMA_SQL);
53
+ }
54
+
55
+ function handleClose(): void {
56
+ db?.close();
57
+ db = undefined;
58
+ dbPath = undefined;
59
+ }
60
+
61
+ async function handleExport(): Promise<Uint8Array> {
62
+ if (!db || !dbPath) {
63
+ throw new Error('SQLite worker: no database open to export');
64
+ }
65
+ if (!pool) {
66
+ throw new Error('SQLite worker: no SAH Pool available (ephemeral DBs cannot be exported)');
67
+ }
68
+ return await pool.exportFile(dbPath);
69
+ }
70
+
71
+ async function handleDeleteDb(dbName: string): Promise<void> {
72
+ const path = normalizeDbPath(dbName);
73
+ if (db && dbPath === path) {
74
+ db.close();
75
+ db = undefined;
76
+ dbPath = undefined;
77
+ }
78
+ const p = await ensurePool(poolDirectory ?? DEFAULT_SAH_POOL_DIRECTORY);
79
+ try {
80
+ p.unlink(path);
81
+ } catch {
82
+ // File may not exist; ignore.
83
+ }
84
+ }
85
+
86
+ function requireDb(): Database {
87
+ if (!db) {
88
+ throw new Error('SQLite worker: no database open');
89
+ }
90
+ return db;
91
+ }
92
+
93
+ function runSql(sql: string, bind?: SqlValue[]): { changes: number } {
94
+ const conn = requireDb();
95
+ conn.exec({ sql, bind });
96
+ return { changes: conn.changes() };
97
+ }
98
+
99
+ function selectAll(sql: string, bind?: SqlValue[]): ResultRow[] {
100
+ const conn = requireDb();
101
+ const rows: ResultRow[] = [];
102
+ conn.exec({ sql, bind, rowMode: 'array', resultRows: rows });
103
+ return rows;
104
+ }
105
+
106
+ function normalizeDbPath(dbName: string): string {
107
+ return dbName.startsWith('/') ? dbName : `/${dbName}`;
108
+ }
109
+
110
+ function respond(msg: WorkerResponse): void {
111
+ (self as DedicatedWorkerGlobalScope).postMessage(msg);
112
+ }
113
+
114
+ (self as DedicatedWorkerGlobalScope).onmessage = async (ev: MessageEvent<WorkerRequest>) => {
115
+ const req = ev.data;
116
+ try {
117
+ switch (req.type) {
118
+ case 'init':
119
+ await handleInit(req.dbName, req.ephemeral, req.poolDirectory);
120
+ return respond({ type: 'ok', id: req.id });
121
+ case 'close':
122
+ handleClose();
123
+ return respond({ type: 'ok', id: req.id });
124
+ case 'deleteDb':
125
+ await handleDeleteDb(req.dbName);
126
+ return respond({ type: 'ok', id: req.id });
127
+ case 'run': {
128
+ const { changes } = runSql(req.sql, req.bind);
129
+ return respond({ type: 'ok', id: req.id, changes });
130
+ }
131
+ case 'all': {
132
+ const rows = selectAll(req.sql, req.bind);
133
+ return respond({ type: 'ok', id: req.id, rows });
134
+ }
135
+ case 'export': {
136
+ const bytes = await handleExport();
137
+ return respond({ type: 'ok', id: req.id, bytes });
138
+ }
139
+ case 'begin':
140
+ runSql('BEGIN');
141
+ return respond({ type: 'ok', id: req.id });
142
+ case 'commit':
143
+ runSql('COMMIT');
144
+ return respond({ type: 'ok', id: req.id });
145
+ case 'rollback':
146
+ runSql('ROLLBACK');
147
+ return respond({ type: 'ok', id: req.id });
148
+ default: {
149
+ const _exhaustive: never = req;
150
+ throw new Error(`Unknown request: ${JSON.stringify(_exhaustive)}`);
151
+ }
152
+ }
153
+ } catch (err) {
154
+ const message = err instanceof Error ? err.message : String(err);
155
+ respond({ type: 'err', id: req.id, message });
156
+ }
157
+ };
@@ -1,79 +1,88 @@
1
- import type {
2
- L2BlockId,
3
- L2BlockStreamEvent,
4
- L2BlockStreamEventHandler,
5
- L2BlockStreamLocalDataProvider,
6
- L2BlockTag,
7
- L2Tips,
8
- } from '@aztec/stdlib/block';
1
+ import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
2
+ import { type L2BlockTag, L2TipsStoreBase } from '@aztec/stdlib/block';
3
+ import { PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
9
4
 
10
5
  import type { AztecAsyncMap } from '../interfaces/map.js';
11
6
  import type { AztecAsyncKVStore } from '../interfaces/store.js';
12
7
 
13
- /** Stores currently synced L2 tips and unfinalized block hashes. */
14
- export class L2TipsKVStore implements L2BlockStreamEventHandler, L2BlockStreamLocalDataProvider {
15
- private readonly l2TipsStore: AztecAsyncMap<L2BlockTag, number>;
16
- private readonly l2BlockHashesStore: AztecAsyncMap<number, string>;
8
+ /**
9
+ * Persistent implementation of L2 tips store backed by a KV store.
10
+ * Used by nodes that need to persist chain state across restarts.
11
+ */
12
+ export class L2TipsKVStore extends L2TipsStoreBase {
13
+ private readonly l2TipsStore: AztecAsyncMap<L2BlockTag, BlockNumber>;
14
+ private readonly l2BlockHashesStore: AztecAsyncMap<BlockNumber, string>;
15
+ private readonly l2BlockNumberToCheckpointNumberStore: AztecAsyncMap<BlockNumber, CheckpointNumber>;
16
+ private readonly l2CheckpointStore: AztecAsyncMap<CheckpointNumber, Buffer>;
17
17
 
18
- constructor(store: AztecAsyncKVStore, namespace: string) {
18
+ constructor(
19
+ private store: AztecAsyncKVStore,
20
+ namespace: string,
21
+ ) {
22
+ super();
19
23
  this.l2TipsStore = store.openMap([namespace, 'l2_tips'].join('_'));
20
24
  this.l2BlockHashesStore = store.openMap([namespace, 'l2_block_hashes'].join('_'));
25
+ this.l2BlockNumberToCheckpointNumberStore = store.openMap(
26
+ [namespace, 'l2_block_number_to_checkpoint_number'].join('_'),
27
+ );
28
+ this.l2CheckpointStore = store.openMap([namespace, 'l2_checkpoint_store'].join('_'));
21
29
  }
22
30
 
23
- public getL2BlockHash(number: number): Promise<string | undefined> {
24
- return this.l2BlockHashesStore.getAsync(number);
31
+ protected getTip(tag: L2BlockTag): Promise<BlockNumber | undefined> {
32
+ return this.l2TipsStore.getAsync(tag);
25
33
  }
26
34
 
27
- public async getL2Tips(): Promise<L2Tips> {
28
- return {
29
- latest: await this.getL2Tip('latest'),
30
- finalized: await this.getL2Tip('finalized'),
31
- proven: await this.getL2Tip('proven'),
32
- };
35
+ protected setTip(tag: L2BlockTag, blockNumber: BlockNumber): Promise<void> {
36
+ return this.l2TipsStore.set(tag, blockNumber);
33
37
  }
34
38
 
35
- private async getL2Tip(tag: L2BlockTag): Promise<L2BlockId> {
36
- const blockNumber = await this.l2TipsStore.getAsync(tag);
37
- if (blockNumber === undefined || blockNumber === 0) {
38
- return { number: 0, hash: undefined };
39
- }
40
- const blockHash = await this.l2BlockHashesStore.getAsync(blockNumber);
41
- if (!blockHash) {
42
- throw new Error(`Block hash not found for block number ${blockNumber}`);
39
+ protected getStoredBlockHash(blockNumber: BlockNumber): Promise<string | undefined> {
40
+ return this.l2BlockHashesStore.getAsync(blockNumber);
41
+ }
42
+
43
+ protected setBlockHash(blockNumber: BlockNumber, hash: string): Promise<void> {
44
+ return this.l2BlockHashesStore.set(blockNumber, hash);
45
+ }
46
+
47
+ protected async deleteBlockHashesBefore(blockNumber: BlockNumber): Promise<void> {
48
+ for await (const key of this.l2BlockHashesStore.keysAsync({ end: blockNumber })) {
49
+ await this.l2BlockHashesStore.delete(key);
43
50
  }
51
+ }
52
+
53
+ protected getCheckpointNumberForBlock(blockNumber: BlockNumber): Promise<CheckpointNumber | undefined> {
54
+ return this.l2BlockNumberToCheckpointNumberStore.getAsync(blockNumber);
55
+ }
44
56
 
45
- return { number: blockNumber, hash: blockHash };
57
+ protected setCheckpointNumberForBlock(blockNumber: BlockNumber, checkpointNumber: CheckpointNumber): Promise<void> {
58
+ return this.l2BlockNumberToCheckpointNumberStore.set(blockNumber, checkpointNumber);
46
59
  }
47
60
 
48
- public async handleBlockStreamEvent(event: L2BlockStreamEvent): Promise<void> {
49
- switch (event.type) {
50
- case 'blocks-added': {
51
- const blocks = event.blocks.map(b => b.block);
52
- for (const block of blocks) {
53
- await this.l2BlockHashesStore.set(block.number, (await block.hash()).toString());
54
- }
55
- await this.l2TipsStore.set('latest', blocks.at(-1)!.number);
56
- break;
57
- }
58
- case 'chain-pruned':
59
- await this.saveTag('latest', event.block);
60
- break;
61
- case 'chain-proven':
62
- await this.saveTag('proven', event.block);
63
- break;
64
- case 'chain-finalized':
65
- await this.saveTag('finalized', event.block);
66
- for await (const key of this.l2BlockHashesStore.keysAsync({ end: event.block.number })) {
67
- await this.l2BlockHashesStore.delete(key);
68
- }
69
- break;
61
+ protected async deleteBlockToCheckpointBefore(blockNumber: BlockNumber): Promise<void> {
62
+ for await (const key of this.l2BlockNumberToCheckpointNumberStore.keysAsync({ end: blockNumber })) {
63
+ await this.l2BlockNumberToCheckpointNumberStore.delete(key);
70
64
  }
71
65
  }
72
66
 
73
- private async saveTag(name: L2BlockTag, block: L2BlockId) {
74
- await this.l2TipsStore.set(name, block.number);
75
- if (block.hash) {
76
- await this.l2BlockHashesStore.set(block.number, block.hash);
67
+ protected async getCheckpoint(checkpointNumber: CheckpointNumber): Promise<PublishedCheckpoint | undefined> {
68
+ const buffer = await this.l2CheckpointStore.getAsync(checkpointNumber);
69
+ if (!buffer) {
70
+ return undefined;
77
71
  }
72
+ return PublishedCheckpoint.fromBuffer(buffer);
73
+ }
74
+
75
+ protected saveCheckpointData(checkpoint: PublishedCheckpoint): Promise<void> {
76
+ return this.l2CheckpointStore.set(checkpoint.checkpoint.number, checkpoint.toBuffer());
77
+ }
78
+
79
+ protected async deleteCheckpointsBefore(checkpointNumber: CheckpointNumber): Promise<void> {
80
+ for await (const key of this.l2CheckpointStore.keysAsync({ end: checkpointNumber })) {
81
+ await this.l2CheckpointStore.delete(key);
82
+ }
83
+ }
84
+
85
+ protected runInTransaction<T>(fn: () => Promise<T>): Promise<T> {
86
+ return this.store.transactionAsync(fn);
78
87
  }
79
88
  }