@aztec/kv-store 3.0.3-rc.3 → 4.0.0-devnet.1-patch.0

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 (82) hide show
  1. package/dest/indexeddb/array.js +21 -7
  2. package/dest/indexeddb/index.d.ts +2 -2
  3. package/dest/indexeddb/index.d.ts.map +1 -1
  4. package/dest/indexeddb/index.js +3 -6
  5. package/dest/indexeddb/map.d.ts +2 -2
  6. package/dest/indexeddb/map.d.ts.map +1 -1
  7. package/dest/indexeddb/map.js +11 -11
  8. package/dest/indexeddb/multi_map.d.ts +2 -1
  9. package/dest/indexeddb/multi_map.d.ts.map +1 -1
  10. package/dest/indexeddb/multi_map.js +15 -0
  11. package/dest/indexeddb/singleton.js +3 -1
  12. package/dest/indexeddb/store.d.ts +3 -3
  13. package/dest/indexeddb/store.d.ts.map +1 -1
  14. package/dest/indexeddb/store.js +6 -4
  15. package/dest/interfaces/map_test_suite.d.ts +1 -1
  16. package/dest/interfaces/map_test_suite.d.ts.map +1 -1
  17. package/dest/interfaces/map_test_suite.js +48 -2
  18. package/dest/interfaces/multi_map_test_suite.d.ts +1 -1
  19. package/dest/interfaces/multi_map_test_suite.d.ts.map +1 -1
  20. package/dest/interfaces/multi_map_test_suite.js +25 -0
  21. package/dest/interfaces/utils.d.ts +2 -1
  22. package/dest/interfaces/utils.d.ts.map +1 -1
  23. package/dest/interfaces/utils.js +2 -1
  24. package/dest/lmdb/array.js +4 -2
  25. package/dest/lmdb/index.d.ts +2 -2
  26. package/dest/lmdb/index.d.ts.map +1 -1
  27. package/dest/lmdb/index.js +3 -3
  28. package/dest/lmdb-v2/array.d.ts +2 -2
  29. package/dest/lmdb-v2/array.d.ts.map +1 -1
  30. package/dest/lmdb-v2/array.js +4 -3
  31. package/dest/lmdb-v2/factory.d.ts +6 -6
  32. package/dest/lmdb-v2/factory.d.ts.map +1 -1
  33. package/dest/lmdb-v2/factory.js +14 -10
  34. package/dest/lmdb-v2/map.d.ts +2 -2
  35. package/dest/lmdb-v2/map.d.ts.map +1 -1
  36. package/dest/lmdb-v2/map.js +1 -2
  37. package/dest/lmdb-v2/multi_map.d.ts +2 -2
  38. package/dest/lmdb-v2/multi_map.d.ts.map +1 -1
  39. package/dest/lmdb-v2/multi_map.js +1 -2
  40. package/dest/lmdb-v2/singleton.d.ts +2 -2
  41. package/dest/lmdb-v2/singleton.d.ts.map +1 -1
  42. package/dest/lmdb-v2/singleton.js +1 -2
  43. package/dest/lmdb-v2/store.d.ts +4 -5
  44. package/dest/lmdb-v2/store.d.ts.map +1 -1
  45. package/dest/lmdb-v2/store.js +3 -25
  46. package/dest/lmdb-v2/tx-helpers.d.ts +6 -0
  47. package/dest/lmdb-v2/tx-helpers.d.ts.map +1 -0
  48. package/dest/lmdb-v2/tx-helpers.js +21 -0
  49. package/dest/lmdb-v2/utils.d.ts +1 -10
  50. package/dest/lmdb-v2/utils.d.ts.map +1 -1
  51. package/dest/lmdb-v2/utils.js +0 -94
  52. package/dest/lmdb-v2/write_transaction.d.ts +1 -1
  53. package/dest/lmdb-v2/write_transaction.d.ts.map +1 -1
  54. package/dest/lmdb-v2/write_transaction.js +9 -5
  55. package/dest/stores/l2_tips_store.d.ts +24 -10
  56. package/dest/stores/l2_tips_store.d.ts.map +1 -1
  57. package/dest/stores/l2_tips_store.js +61 -54
  58. package/dest/utils.d.ts +9 -6
  59. package/dest/utils.d.ts.map +1 -1
  60. package/dest/utils.js +51 -16
  61. package/package.json +15 -13
  62. package/src/indexeddb/array.ts +4 -4
  63. package/src/indexeddb/index.ts +8 -6
  64. package/src/indexeddb/map.ts +10 -9
  65. package/src/indexeddb/multi_map.ts +14 -0
  66. package/src/indexeddb/singleton.ts +1 -1
  67. package/src/indexeddb/store.ts +13 -6
  68. package/src/interfaces/map_test_suite.ts +30 -2
  69. package/src/interfaces/multi_map_test_suite.ts +32 -0
  70. package/src/interfaces/utils.ts +1 -0
  71. package/src/lmdb/index.ts +8 -3
  72. package/src/lmdb-v2/array.ts +2 -2
  73. package/src/lmdb-v2/factory.ts +15 -11
  74. package/src/lmdb-v2/map.ts +2 -2
  75. package/src/lmdb-v2/multi_map.ts +2 -2
  76. package/src/lmdb-v2/singleton.ts +2 -2
  77. package/src/lmdb-v2/store.ts +5 -31
  78. package/src/lmdb-v2/tx-helpers.ts +29 -0
  79. package/src/lmdb-v2/utils.ts +0 -118
  80. package/src/lmdb-v2/write_transaction.ts +9 -8
  81. package/src/stores/l2_tips_store.ts +63 -56
  82. package/src/utils.ts +79 -21
@@ -1,9 +1,16 @@
1
- import { GENESIS_BLOCK_HEADER_HASH } from '@aztec/constants';
2
- import { BlockNumber } from '@aztec/foundation/branded-types';
3
- /** Stores currently synced L2 tips and unfinalized block hashes. */ export class L2TipsKVStore {
1
+ import { L2TipsStoreBase } from '@aztec/stdlib/block';
2
+ import { PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
3
+ /**
4
+ * Persistent implementation of L2 tips store backed by a KV store.
5
+ * Used by nodes that need to persist chain state across restarts.
6
+ */ export class L2TipsKVStore extends L2TipsStoreBase {
7
+ store;
4
8
  l2TipsStore;
5
9
  l2BlockHashesStore;
10
+ l2BlockNumberToCheckpointNumberStore;
11
+ l2CheckpointStore;
6
12
  constructor(store, namespace){
13
+ super(), this.store = store;
7
14
  this.l2TipsStore = store.openMap([
8
15
  namespace,
9
16
  'l2_tips'
@@ -12,65 +19,65 @@ import { BlockNumber } from '@aztec/foundation/branded-types';
12
19
  namespace,
13
20
  'l2_block_hashes'
14
21
  ].join('_'));
22
+ this.l2BlockNumberToCheckpointNumberStore = store.openMap([
23
+ namespace,
24
+ 'l2_block_number_to_checkpoint_number'
25
+ ].join('_'));
26
+ this.l2CheckpointStore = store.openMap([
27
+ namespace,
28
+ 'l2_checkpoint_store'
29
+ ].join('_'));
30
+ }
31
+ getTip(tag) {
32
+ return this.l2TipsStore.getAsync(tag);
33
+ }
34
+ setTip(tag, blockNumber) {
35
+ return this.l2TipsStore.set(tag, blockNumber);
15
36
  }
16
- getL2BlockHash(number) {
17
- return this.l2BlockHashesStore.getAsync(number);
37
+ getStoredBlockHash(blockNumber) {
38
+ return this.l2BlockHashesStore.getAsync(blockNumber);
18
39
  }
19
- async getL2Tips() {
20
- return {
21
- latest: await this.getL2Tip('latest'),
22
- finalized: await this.getL2Tip('finalized'),
23
- proven: await this.getL2Tip('proven')
24
- };
40
+ setBlockHash(blockNumber, hash) {
41
+ return this.l2BlockHashesStore.set(blockNumber, hash);
25
42
  }
26
- async getL2Tip(tag) {
27
- const blockNumber = await this.l2TipsStore.getAsync(tag);
28
- if (blockNumber === undefined || blockNumber === 0) {
29
- return {
30
- number: BlockNumber.ZERO,
31
- hash: GENESIS_BLOCK_HEADER_HASH.toString()
32
- };
43
+ async deleteBlockHashesBefore(blockNumber) {
44
+ for await (const key of this.l2BlockHashesStore.keysAsync({
45
+ end: blockNumber
46
+ })){
47
+ await this.l2BlockHashesStore.delete(key);
33
48
  }
34
- const blockHash = await this.l2BlockHashesStore.getAsync(blockNumber);
35
- if (!blockHash) {
36
- throw new Error(`Block hash not found for block number ${blockNumber}`);
49
+ }
50
+ getCheckpointNumberForBlock(blockNumber) {
51
+ return this.l2BlockNumberToCheckpointNumberStore.getAsync(blockNumber);
52
+ }
53
+ setCheckpointNumberForBlock(blockNumber, checkpointNumber) {
54
+ return this.l2BlockNumberToCheckpointNumberStore.set(blockNumber, checkpointNumber);
55
+ }
56
+ async deleteBlockToCheckpointBefore(blockNumber) {
57
+ for await (const key of this.l2BlockNumberToCheckpointNumberStore.keysAsync({
58
+ end: blockNumber
59
+ })){
60
+ await this.l2BlockNumberToCheckpointNumberStore.delete(key);
37
61
  }
38
- return {
39
- number: blockNumber,
40
- hash: blockHash
41
- };
42
62
  }
43
- async handleBlockStreamEvent(event) {
44
- switch(event.type){
45
- case 'blocks-added':
46
- {
47
- const blocks = event.blocks.map((b)=>b.block);
48
- for (const block of blocks){
49
- await this.l2BlockHashesStore.set(block.number, (await block.hash()).toString());
50
- }
51
- await this.l2TipsStore.set('latest', blocks.at(-1).number);
52
- break;
53
- }
54
- case 'chain-pruned':
55
- await this.saveTag('latest', event.block);
56
- break;
57
- case 'chain-proven':
58
- await this.saveTag('proven', event.block);
59
- break;
60
- case 'chain-finalized':
61
- await this.saveTag('finalized', event.block);
62
- for await (const key of this.l2BlockHashesStore.keysAsync({
63
- end: event.block.number
64
- })){
65
- await this.l2BlockHashesStore.delete(key);
66
- }
67
- break;
63
+ async getCheckpoint(checkpointNumber) {
64
+ const buffer = await this.l2CheckpointStore.getAsync(checkpointNumber);
65
+ if (!buffer) {
66
+ return undefined;
68
67
  }
68
+ return PublishedCheckpoint.fromBuffer(buffer);
69
69
  }
70
- async saveTag(name, block) {
71
- await this.l2TipsStore.set(name, block.number);
72
- if (block.hash) {
73
- await this.l2BlockHashesStore.set(block.number, block.hash);
70
+ saveCheckpointData(checkpoint) {
71
+ return this.l2CheckpointStore.set(checkpoint.checkpoint.number, checkpoint.toBuffer());
72
+ }
73
+ async deleteCheckpointsBefore(checkpointNumber) {
74
+ for await (const key of this.l2CheckpointStore.keysAsync({
75
+ end: checkpointNumber
76
+ })){
77
+ await this.l2CheckpointStore.delete(key);
74
78
  }
75
79
  }
80
+ runInTransaction(fn) {
81
+ return this.store.transactionAsync(fn);
82
+ }
76
83
  }
package/dest/utils.d.ts CHANGED
@@ -1,12 +1,15 @@
1
- import type { EthAddress } from '@aztec/foundation/eth-address';
1
+ import { EthAddress } from '@aztec/foundation/eth-address';
2
2
  import type { Logger } from '@aztec/foundation/log';
3
3
  import type { AztecAsyncKVStore, AztecKVStore } from './interfaces/store.js';
4
4
  /**
5
- * Clears the store if the rollup address does not match the one stored in the database.
6
- * This is to prevent data from being accidentally shared between different rollup instances.
5
+ * Clears the store if the schema version or rollup address does not match the one stored in the database.
6
+ * Also clears if migrating from an older store format that didn't track schema version.
7
+ * This is to prevent data from being accidentally mixed up between different rollup instances or schema versions.
7
8
  * @param store - The store to check
9
+ * @param targetSchemaVersion - The current schema version
8
10
  * @param rollupAddress - The ETH address of the rollup contract
9
- * @returns A promise that resolves when the store is cleared, or rejects if the rollup address does not match
11
+ * @param log - Optional logger
12
+ * @returns The store (cleared if necessary)
10
13
  */
11
- export declare function initStoreForRollup<T extends AztecKVStore | AztecAsyncKVStore>(store: T, rollupAddress: EthAddress, log?: Logger): Promise<T>;
12
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidXRpbHMuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3NyYy91dGlscy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEtBQUssRUFBRSxVQUFVLEVBQUUsTUFBTSwrQkFBK0IsQ0FBQztBQUNoRSxPQUFPLEtBQUssRUFBRSxNQUFNLEVBQUUsTUFBTSx1QkFBdUIsQ0FBQztBQUdwRCxPQUFPLEtBQUssRUFBRSxpQkFBaUIsRUFBRSxZQUFZLEVBQUUsTUFBTSx1QkFBdUIsQ0FBQztBQUc3RTs7Ozs7O0dBTUc7QUFDSCx3QkFBc0Isa0JBQWtCLENBQUMsQ0FBQyxTQUFTLFlBQVksR0FBRyxpQkFBaUIsRUFDakYsS0FBSyxFQUFFLENBQUMsRUFDUixhQUFhLEVBQUUsVUFBVSxFQUN6QixHQUFHLENBQUMsRUFBRSxNQUFNLEdBQ1gsT0FBTyxDQUFDLENBQUMsQ0FBQyxDQXFCWiJ9
14
+ export declare function initStoreForRollupAndSchemaVersion<T extends AztecKVStore | AztecAsyncKVStore>(store: T, schemaVersion: number | undefined, rollupAddress: EthAddress | undefined, log?: Logger): Promise<T>;
15
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidXRpbHMuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3NyYy91dGlscy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEVBQUUsVUFBVSxFQUFFLE1BQU0sK0JBQStCLENBQUM7QUFDM0QsT0FBTyxLQUFLLEVBQUUsTUFBTSxFQUFFLE1BQU0sdUJBQXVCLENBQUM7QUFJcEQsT0FBTyxLQUFLLEVBQUUsaUJBQWlCLEVBQUUsWUFBWSxFQUFFLE1BQU0sdUJBQXVCLENBQUM7QUFHN0U7Ozs7Ozs7OztHQVNHO0FBQ0gsd0JBQXNCLGtDQUFrQyxDQUFDLENBQUMsU0FBUyxZQUFZLEdBQUcsaUJBQWlCLEVBQ2pHLEtBQUssRUFBRSxDQUFDLEVBQ1IsYUFBYSxFQUFFLE1BQU0sR0FBRyxTQUFTLEVBQ2pDLGFBQWEsRUFBRSxVQUFVLEdBQUcsU0FBUyxFQUNyQyxHQUFHLENBQUMsRUFBRSxNQUFNLEdBQ1gsT0FBTyxDQUFDLENBQUMsQ0FBQyxDQXFDWiJ9
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,+BAA+B,CAAC;AAChE,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAGpD,OAAO,KAAK,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAG7E;;;;;;GAMG;AACH,wBAAsB,kBAAkB,CAAC,CAAC,SAAS,YAAY,GAAG,iBAAiB,EACjF,KAAK,EAAE,CAAC,EACR,aAAa,EAAE,UAAU,EACzB,GAAG,CAAC,EAAE,MAAM,GACX,OAAO,CAAC,CAAC,CAAC,CAqBZ"}
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,+BAA+B,CAAC;AAC3D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AAIpD,OAAO,KAAK,EAAE,iBAAiB,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAG7E;;;;;;;;;GASG;AACH,wBAAsB,kCAAkC,CAAC,CAAC,SAAS,YAAY,GAAG,iBAAiB,EACjG,KAAK,EAAE,CAAC,EACR,aAAa,EAAE,MAAM,GAAG,SAAS,EACjC,aAAa,EAAE,UAAU,GAAG,SAAS,EACrC,GAAG,CAAC,EAAE,MAAM,GACX,OAAO,CAAC,CAAC,CAAC,CAqCZ"}
package/dest/utils.js CHANGED
@@ -1,24 +1,59 @@
1
+ import { EthAddress } from '@aztec/foundation/eth-address';
2
+ import { DatabaseVersion } from '@aztec/stdlib/database-version/version';
1
3
  import { isSyncStore } from './interfaces/utils.js';
2
4
  /**
3
- * Clears the store if the rollup address does not match the one stored in the database.
4
- * This is to prevent data from being accidentally shared between different rollup instances.
5
+ * Clears the store if the schema version or rollup address does not match the one stored in the database.
6
+ * Also clears if migrating from an older store format that didn't track schema version.
7
+ * This is to prevent data from being accidentally mixed up between different rollup instances or schema versions.
5
8
  * @param store - The store to check
9
+ * @param targetSchemaVersion - The current schema version
6
10
  * @param rollupAddress - The ETH address of the rollup contract
7
- * @returns A promise that resolves when the store is cleared, or rejects if the rollup address does not match
8
- */ export async function initStoreForRollup(store, rollupAddress, log) {
9
- if (!rollupAddress) {
10
- throw new Error('Rollup address is required');
11
- }
12
- const rollupAddressValue = store.openSingleton('rollupAddress');
13
- const rollupAddressString = rollupAddress.toString();
14
- const storedRollupAddressString = isSyncStore(store) ? rollupAddressValue.get() : await rollupAddressValue.getAsync();
15
- if (typeof storedRollupAddressString !== 'undefined' && storedRollupAddressString !== rollupAddressString) {
16
- log?.warn(`Rollup address mismatch. Clearing entire database...`, {
17
- expected: rollupAddressString,
18
- found: storedRollupAddressString
19
- });
11
+ * @param log - Optional logger
12
+ * @returns The store (cleared if necessary)
13
+ */ export async function initStoreForRollupAndSchemaVersion(store, schemaVersion, rollupAddress, log) {
14
+ const targetSchemaVersion = schemaVersion ?? 0;
15
+ const targetRollupAddress = rollupAddress ?? EthAddress.ZERO;
16
+ const targetDatabaseVersion = new DatabaseVersion(targetSchemaVersion, targetRollupAddress);
17
+ // DB version: database schema version + rollup address combined)
18
+ const dbVersion = store.openSingleton('dbVersion');
19
+ const storedDatabaseVersion = isSyncStore(store) ? dbVersion.get() : await dbVersion.getAsync();
20
+ // Check if this is an old format store (has rollupAddress singleton but no dbVersion)
21
+ const oldRollupSingleton = store.openSingleton('rollupAddress');
22
+ const hasOldFormat = isSyncStore(store) ? !storedDatabaseVersion && !!oldRollupSingleton.get() : !storedDatabaseVersion && !!await oldRollupSingleton.getAsync();
23
+ if (hasOldFormat || doesStoreNeedToBeCleared(targetDatabaseVersion, storedDatabaseVersion, targetSchemaVersion, targetRollupAddress, log)) {
24
+ if (hasOldFormat) {
25
+ log?.warn('Detected old store format without dbVersion, clearing database');
26
+ }
20
27
  await store.clear();
21
28
  }
22
- await rollupAddressValue.set(rollupAddressString);
29
+ await dbVersion.set(targetDatabaseVersion.toBuffer().toString('utf-8'));
23
30
  return store;
24
31
  }
32
+ function doesStoreNeedToBeCleared(targetDatabaseVersion, storedDatabaseVersion, targetSchemaVersion, targetRollupAddress, log) {
33
+ if (storedDatabaseVersion) {
34
+ try {
35
+ const storedVersion = DatabaseVersion.fromBuffer(Buffer.from(storedDatabaseVersion, 'utf-8'));
36
+ const cmp = storedVersion.cmp(targetDatabaseVersion);
37
+ if (cmp === undefined) {
38
+ log?.warn('Rollup address changed, clearing database', {
39
+ stored: storedVersion.rollupAddress.toString(),
40
+ current: targetRollupAddress.toString()
41
+ });
42
+ return true;
43
+ }
44
+ if (cmp !== 0) {
45
+ log?.warn('Schema version changed, clearing database', {
46
+ stored: storedVersion.schemaVersion,
47
+ current: targetSchemaVersion
48
+ });
49
+ return true;
50
+ }
51
+ } catch (err) {
52
+ log?.warn('Failed to parse stored version, clearing database', {
53
+ err
54
+ });
55
+ return true;
56
+ }
57
+ }
58
+ return false;
59
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aztec/kv-store",
3
- "version": "3.0.3-rc.3",
3
+ "version": "4.0.0-devnet.1-patch.0",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./dest/interfaces/index.js",
@@ -16,8 +16,8 @@
16
16
  "build:dev": "../scripts/tsc.sh --watch",
17
17
  "clean": "rm -rf ./dest .tsbuildinfo",
18
18
  "test:node": "NODE_NO_WARNINGS=1 mocha --config ./.mocharc.json",
19
- "test:browser": "wtr --config ./web-test-runner.config.mjs",
20
- "test": "yarn test:node",
19
+ "test:browser": "vitest run --config ./vitest.config.ts",
20
+ "test": "yarn test:node && yarn test:browser",
21
21
  "test:jest": "NODE_NO_WARNINGS=1 node --experimental-vm-modules ../node_modules/.bin/jest --passWithNoTests --maxWorkers=${JEST_MAX_WORKERS:-8}"
22
22
  },
23
23
  "inherits": [
@@ -25,11 +25,11 @@
25
25
  "./package.local.json"
26
26
  ],
27
27
  "dependencies": {
28
- "@aztec/constants": "3.0.3-rc.3",
29
- "@aztec/ethereum": "3.0.3-rc.3",
30
- "@aztec/foundation": "3.0.3-rc.3",
31
- "@aztec/native": "3.0.3-rc.3",
32
- "@aztec/stdlib": "3.0.3-rc.3",
28
+ "@aztec/constants": "4.0.0-devnet.1-patch.0",
29
+ "@aztec/ethereum": "4.0.0-devnet.1-patch.0",
30
+ "@aztec/foundation": "4.0.0-devnet.1-patch.0",
31
+ "@aztec/native": "4.0.0-devnet.1-patch.0",
32
+ "@aztec/stdlib": "4.0.0-devnet.1-patch.0",
33
33
  "idb": "^8.0.0",
34
34
  "lmdb": "^3.2.0",
35
35
  "msgpackr": "^1.11.2",
@@ -45,18 +45,20 @@
45
45
  "@types/mocha-each": "^2.0.4",
46
46
  "@types/node": "^22.15.17",
47
47
  "@types/sinon": "^17.0.3",
48
- "@typescript/native-preview": "7.0.0-dev.20251126.1",
49
- "@web/dev-server-esbuild": "^1.0.3",
50
- "@web/test-runner": "^0.19.0",
51
- "@web/test-runner-playwright": "^0.11.0",
48
+ "@typescript/native-preview": "7.0.0-dev.20260113.1",
49
+ "@vitest/browser-playwright": "^4.0.0",
50
+ "buffer": "^6.0.3",
52
51
  "chai": "^5.1.2",
53
52
  "chai-as-promised": "^8.0.1",
54
53
  "jest": "^30.0.0",
55
54
  "mocha": "^10.8.2",
56
55
  "mocha-each": "^2.0.1",
56
+ "playwright": "1.49.0",
57
57
  "sinon": "^19.0.2",
58
58
  "ts-node": "^10.9.1",
59
- "typescript": "^5.3.3"
59
+ "typescript": "^5.3.3",
60
+ "util": "^0.12.5",
61
+ "vitest": "^4.0.0"
60
62
  },
61
63
  "files": [
62
64
  "dest",
@@ -32,7 +32,7 @@ export class IndexedDBAztecArray<T extends Value> implements AztecAsyncArray<T>
32
32
  return (
33
33
  (await this.db
34
34
  .index('key')
35
- .count(IDBKeyRange.bound([this.#container, this.#name], [this.#container, this.#name]))) ?? 0
35
+ .count(IDBKeyRange.bound([this.#container, [this.#name]], [this.#container, [this.#name]]))) ?? 0
36
36
  );
37
37
  }
38
38
 
@@ -43,7 +43,7 @@ export class IndexedDBAztecArray<T extends Value> implements AztecAsyncArray<T>
43
43
  value: val,
44
44
  hash: hash(val),
45
45
  container: this.#container,
46
- key: this.#name,
46
+ key: [this.#name],
47
47
  keyCount: length + 1,
48
48
  slot: this.#slot(length),
49
49
  });
@@ -91,7 +91,7 @@ export class IndexedDBAztecArray<T extends Value> implements AztecAsyncArray<T>
91
91
  value: val,
92
92
  hash: hash(val),
93
93
  container: this.#container,
94
- key: this.#name,
94
+ key: [this.#name],
95
95
  keyCount: index + 1,
96
96
  slot: this.#slot(index),
97
97
  });
@@ -100,7 +100,7 @@ export class IndexedDBAztecArray<T extends Value> implements AztecAsyncArray<T>
100
100
 
101
101
  async *entriesAsync(): AsyncIterableIterator<[number, T]> {
102
102
  const index = this.db.index('key');
103
- const rangeQuery = IDBKeyRange.bound([this.#container, this.#name], [this.#container, this.#name]);
103
+ const rangeQuery = IDBKeyRange.bound([this.#container, [this.#name]], [this.#container, [this.#name]]);
104
104
  for await (const cursor of index.iterate(rangeQuery)) {
105
105
  yield [cursor.value.keyCount - 1, cursor.value.value] as [number, T];
106
106
  }
@@ -1,12 +1,17 @@
1
1
  import { type Logger, createLogger } from '@aztec/foundation/log';
2
2
 
3
3
  import type { DataStoreConfig } from '../config.js';
4
- import { initStoreForRollup } from '../utils.js';
4
+ import { initStoreForRollupAndSchemaVersion } from '../utils.js';
5
5
  import { AztecIndexedDBStore } from './store.js';
6
6
 
7
7
  export { AztecIndexedDBStore } from './store.js';
8
8
 
9
- export async function createStore(name: string, config: DataStoreConfig, log: Logger = createLogger('kv-store')) {
9
+ export async function createStore(
10
+ name: string,
11
+ config: DataStoreConfig,
12
+ schemaVersion: number | undefined = undefined,
13
+ log: Logger = createLogger('kv-store'),
14
+ ) {
10
15
  let { dataDirectory } = config;
11
16
  if (typeof dataDirectory !== 'undefined') {
12
17
  dataDirectory = `${dataDirectory}/${name}`;
@@ -18,10 +23,7 @@ export async function createStore(name: string, config: DataStoreConfig, log: Lo
18
23
  : `Creating ${name} ephemeral data store with map size ${config.dataStoreMapSizeKb} KB`,
19
24
  );
20
25
  const store = await AztecIndexedDBStore.open(createLogger('kv-store:indexeddb'), dataDirectory ?? '', false);
21
- if (config.l1Contracts?.rollupAddress) {
22
- return initStoreForRollup(store, config.l1Contracts.rollupAddress, log);
23
- }
24
- return store;
26
+ return initStoreForRollupAndSchemaVersion(store, schemaVersion, config.l1Contracts?.rollupAddress, log);
25
27
  }
26
28
 
27
29
  export function openTmpStore(ephemeral: boolean = false): Promise<AztecIndexedDBStore> {
@@ -41,7 +41,7 @@ export class IndexedDBAztecMap<K extends Key, V extends Value> implements AztecA
41
41
 
42
42
  async sizeAsync(): Promise<number> {
43
43
  const index = this.db.index('key');
44
- const rangeQuery = IDBKeyRange.bound([this.container, ''], [this.container, '\uffff']);
44
+ const rangeQuery = IDBKeyRange.bound([this.container], [this.container + '\uffff'], true, true);
45
45
  return await index.count(rangeQuery);
46
46
  }
47
47
 
@@ -80,9 +80,12 @@ export class IndexedDBAztecMap<K extends Key, V extends Value> implements AztecA
80
80
 
81
81
  async *entriesAsync(range: Range<K> = {}): AsyncIterableIterator<[K, V]> {
82
82
  const index = this.db.index('key');
83
+ const startKey = range.start ? this.normalizeKey(range.start) : [];
84
+ const endKey = range.end ? this.normalizeKey(range.end) : ['\uffff'];
85
+
83
86
  const rangeQuery = IDBKeyRange.bound(
84
- [this.container, range.start ? this.normalizeKey(range.start) : ''],
85
- [this.container, range.end ? this.normalizeKey(range.end) : '\uffff'],
87
+ [this.container, startKey],
88
+ [this.container, endKey],
86
89
  !!range.reverse,
87
90
  !range.reverse,
88
91
  );
@@ -108,14 +111,12 @@ export class IndexedDBAztecMap<K extends Key, V extends Value> implements AztecA
108
111
  }
109
112
  }
110
113
 
111
- #denormalizeKey(key: string): K {
112
- const denormalizedKey = key.split(',').map(part => (part.startsWith('n_') ? Number(part.slice(2)) : part));
113
- return (denormalizedKey.length > 1 ? denormalizedKey : denormalizedKey[0]) as K;
114
+ #denormalizeKey(key: (string | number | Uint8Array)[]): K {
115
+ return (key.length > 1 ? key : key[0]) as K;
114
116
  }
115
117
 
116
- protected normalizeKey(key: K): string {
117
- const arrayKey = Array.isArray(key) ? key : [key];
118
- return (arrayKey as K[]).map((element: K) => (typeof element === 'number' ? `n_${element}` : element)).join(',');
118
+ protected normalizeKey(key: K): (string | number | Uint8Array)[] {
119
+ return Array.isArray(key) ? key : [key];
119
120
  }
120
121
 
121
122
  protected slot(key: K, index: number = 0): string {
@@ -88,4 +88,18 @@ export class IndexedDBAztecMultiMap<K extends Key, V extends Value>
88
88
  await this.db.delete(fullKey);
89
89
  }
90
90
  }
91
+
92
+ override async delete(key: K): Promise<void> {
93
+ const index = this.db.index('keyCount');
94
+ const rangeQuery = IDBKeyRange.bound(
95
+ [this.container, this.normalizeKey(key), 0],
96
+ [this.container, this.normalizeKey(key), Number.MAX_SAFE_INTEGER],
97
+ false,
98
+ false,
99
+ );
100
+
101
+ for await (const cursor of index.iterate(rangeQuery)) {
102
+ await cursor.delete();
103
+ }
104
+ }
91
105
  }
@@ -37,7 +37,7 @@ export class IndexedDBAztecSingleton<T extends Value> implements AztecAsyncSingl
37
37
  const result = await this.db.put({
38
38
  container: this.#container,
39
39
  slot: this.#slot,
40
- key: this.#slot,
40
+ key: [this.#slot],
41
41
  keyCount: 1,
42
42
  value: val,
43
43
  hash: hash(val),
@@ -20,7 +20,7 @@ import { IndexedDBAztecSingleton } from './singleton.js';
20
20
  export type StoredData<V extends Value> = {
21
21
  value: V;
22
22
  container: string;
23
- key: string;
23
+ key: (string | number | Uint8Array)[];
24
24
  keyCount: number;
25
25
  slot: string;
26
26
  hash: string;
@@ -30,7 +30,12 @@ export interface AztecIDBSchema extends DBSchema {
30
30
  data: {
31
31
  value: StoredData<any>;
32
32
  key: string;
33
- indexes: { container: string; key: string; keyCount: number; hash: string };
33
+ indexes: {
34
+ container: string;
35
+ key: any; // Runtime: (string | number | Uint8Array)[] - idb types don't support arrays
36
+ keyCount: number;
37
+ hash: string;
38
+ };
34
39
  };
35
40
  }
36
41
 
@@ -187,18 +192,20 @@ export class AztecIndexedDBStore implements AztecAsyncKVStore {
187
192
  }
188
193
 
189
194
  /** Deletes this store and removes the database */
190
- delete() {
195
+ async delete() {
191
196
  this.#containers.clear();
197
+ await this.#txQueue.end();
192
198
  this.#rootDB.close();
193
- return deleteDB(this.#name);
199
+ await deleteDB(this.#name);
194
200
  }
195
201
 
196
202
  estimateSize(): Promise<StoreSize> {
197
203
  return Promise.resolve({ mappingSize: 0, physicalFileSize: 0, actualSize: 0, numItems: 0 });
198
204
  }
199
205
 
200
- close(): Promise<void> {
201
- return Promise.resolve();
206
+ async close(): Promise<void> {
207
+ await this.#txQueue.end();
208
+ this.#rootDB.close();
202
209
  }
203
210
 
204
211
  backupTo(_dstPath: string, _compact?: boolean): Promise<void> {
@@ -107,6 +107,33 @@ export function describeAztecMap(
107
107
  expect(await size()).to.equal(1);
108
108
  });
109
109
 
110
+ it('returns 0 for empty map size', async () => {
111
+ expect(await size()).to.equal(0);
112
+ });
113
+
114
+ it('calculates size correctly across multiple operations', async () => {
115
+ expect(await size()).to.equal(0);
116
+
117
+ // Add items
118
+ await map.set('a', 'value1');
119
+ await map.set('b', 'value2');
120
+ await map.set('c', 'value3');
121
+ expect(await size()).to.equal(3);
122
+
123
+ // Update existing (size should not change)
124
+ await map.set('b', 'updated');
125
+ expect(await size()).to.equal(3);
126
+
127
+ // Delete some
128
+ await map.delete('a');
129
+ expect(await size()).to.equal(2);
130
+
131
+ // Delete all
132
+ await map.delete('b');
133
+ await map.delete('c');
134
+ expect(await size()).to.equal(0);
135
+ });
136
+
110
137
  it('should be able to iterate over entries when there are no keys', async () => {
111
138
  expect(await entries()).to.deep.equal([]);
112
139
  });
@@ -145,10 +172,11 @@ export function describeAztecMap(
145
172
  for (const [name, data] of [
146
173
  ['chars', ['a', 'b', 'c', 'd']],
147
174
  ['numbers', [1, 2, 3, 4]],
148
- // disabled because indexeddb sorts lexigographically
149
- // ['negative numbers', [-4, -3, -2, -1]],
175
+ ['negative numbers', [-4, -3, -2, -1]],
150
176
  ['strings', ['aaa', 'bbb', 'ccc', 'ddd']],
151
177
  ['zero-based numbers', [0, 1, 2, 3]],
178
+ ['large numbers', [100, 999, 1000, 1001]],
179
+ ['mixed negative and positive', [-1000, -1, 1, 1000]],
152
180
  ]) {
153
181
  it(`supports range queries over ${name} keys`, async () => {
154
182
  const [a, b, c, d] = data;
@@ -104,6 +104,38 @@ export function describeAztecMultiMap(
104
104
  expect(await size()).to.equal(1);
105
105
  });
106
106
 
107
+ it('returns 0 for empty multimap size', async () => {
108
+ expect(await size()).to.equal(0);
109
+ });
110
+
111
+ it('calculates size correctly with multiple values per key', async () => {
112
+ expect(await size()).to.equal(0);
113
+
114
+ // Add multiple values for same key
115
+ await multiMap.set('key1', 'value1');
116
+ expect(await size()).to.equal(1);
117
+ await multiMap.set('key1', 'value2');
118
+ expect(await size()).to.equal(2);
119
+ await multiMap.set('key1', 'value3');
120
+ expect(await size()).to.equal(3);
121
+
122
+ // Add values for different key
123
+ await multiMap.set('key2', 'value4');
124
+ expect(await size()).to.equal(4);
125
+
126
+ // Delete one value from key1
127
+ await multiMap.deleteValue('key1', 'value2');
128
+ expect(await size()).to.equal(3);
129
+
130
+ // Delete entire key
131
+ await multiMap.delete('key1');
132
+ expect(await size()).to.equal(1);
133
+
134
+ // Delete last key
135
+ await multiMap.delete('key2');
136
+ expect(await size()).to.equal(0);
137
+ });
138
+
107
139
  it('should be able to iterate over entries when there are no keys', async () => {
108
140
  expect(await entries()).to.deep.equal([]);
109
141
  });
@@ -14,6 +14,7 @@ export const mockLogger = {
14
14
  isLevelEnabled: (_level: string) => true,
15
15
  module: 'kv-store:mock-logger',
16
16
  createChild: () => mockLogger,
17
+ getBindings: () => ({}),
17
18
  };
18
19
  /* eslint-enable no-console */
19
20
 
package/src/lmdb/index.ts CHANGED
@@ -3,12 +3,17 @@ import { type Logger, createLogger } from '@aztec/foundation/log';
3
3
  import { join } from 'path';
4
4
 
5
5
  import type { DataStoreConfig } from '../config.js';
6
- import { initStoreForRollup } from '../utils.js';
6
+ import { initStoreForRollupAndSchemaVersion } from '../utils.js';
7
7
  import { AztecLmdbStore } from './store.js';
8
8
 
9
9
  export { AztecLmdbStore } from './store.js';
10
10
 
11
- export function createStore(name: string, config: DataStoreConfig, log: Logger = createLogger('kv-store')) {
11
+ export function createStore(
12
+ name: string,
13
+ config: DataStoreConfig,
14
+ schemaVersion: number | undefined = undefined,
15
+ log: Logger = createLogger('kv-store'),
16
+ ) {
12
17
  let { dataDirectory } = config;
13
18
  if (typeof dataDirectory !== 'undefined') {
14
19
  dataDirectory = join(dataDirectory, name);
@@ -22,7 +27,7 @@ export function createStore(name: string, config: DataStoreConfig, log: Logger =
22
27
 
23
28
  const store = AztecLmdbStore.open(dataDirectory, config.dataStoreMapSizeKb, false);
24
29
  if (config.l1Contracts?.rollupAddress) {
25
- return initStoreForRollup(store, config.l1Contracts.rollupAddress, log);
30
+ return initStoreForRollupAndSchemaVersion(store, schemaVersion, config.l1Contracts.rollupAddress, log);
26
31
  }
27
32
  return store;
28
33
  }
@@ -4,8 +4,8 @@ import type { AztecAsyncArray } from '../interfaces/array.js';
4
4
  import type { Value } from '../interfaces/common.js';
5
5
  import type { AztecAsyncSingleton } from '../interfaces/singleton.js';
6
6
  import type { ReadTransaction } from './read_transaction.js';
7
- // eslint-disable-next-line import/no-cycle
8
- import { AztecLMDBStoreV2, execInReadTx, execInWriteTx } from './store.js';
7
+ import type { AztecLMDBStoreV2 } from './store.js';
8
+ import { execInReadTx, execInWriteTx } from './tx-helpers.js';
9
9
  import { deserializeKey, serializeKey } from './utils.js';
10
10
 
11
11
  export class LMDBArray<T extends Value> implements AztecAsyncArray<T> {