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

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 +8 -2
  6. package/dest/indexeddb/map.d.ts.map +1 -1
  7. package/dest/indexeddb/map.js +23 -13
  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 +16 -1
  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 +24 -11
  65. package/src/indexeddb/multi_map.ts +15 -1
  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
package/src/utils.ts CHANGED
@@ -1,40 +1,98 @@
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
+ import { DatabaseVersion } from '@aztec/stdlib/database-version/version';
3
4
 
4
5
  import type { AztecAsyncSingleton, AztecSingleton } from './interfaces/singleton.js';
5
6
  import type { AztecAsyncKVStore, AztecKVStore } from './interfaces/store.js';
6
7
  import { isSyncStore } from './interfaces/utils.js';
7
8
 
8
9
  /**
9
- * Clears the store if the rollup address does not match the one stored in the database.
10
- * This is to prevent data from being accidentally shared between different rollup instances.
10
+ * Clears the store if the schema version or rollup address does not match the one stored in the database.
11
+ * Also clears if migrating from an older store format that didn't track schema version.
12
+ * This is to prevent data from being accidentally mixed up between different rollup instances or schema versions.
11
13
  * @param store - The store to check
14
+ * @param targetSchemaVersion - The current schema version
12
15
  * @param rollupAddress - The ETH address of the rollup contract
13
- * @returns A promise that resolves when the store is cleared, or rejects if the rollup address does not match
16
+ * @param log - Optional logger
17
+ * @returns The store (cleared if necessary)
14
18
  */
15
- export async function initStoreForRollup<T extends AztecKVStore | AztecAsyncKVStore>(
19
+ export async function initStoreForRollupAndSchemaVersion<T extends AztecKVStore | AztecAsyncKVStore>(
16
20
  store: T,
17
- rollupAddress: EthAddress,
21
+ schemaVersion: number | undefined,
22
+ rollupAddress: EthAddress | undefined,
18
23
  log?: Logger,
19
24
  ): Promise<T> {
20
- if (!rollupAddress) {
21
- throw new Error('Rollup address is required');
22
- }
23
- const rollupAddressValue = store.openSingleton<ReturnType<EthAddress['toString']>>('rollupAddress');
24
- const rollupAddressString = rollupAddress.toString();
25
- const storedRollupAddressString = isSyncStore(store)
26
- ? (rollupAddressValue as AztecSingleton<ReturnType<EthAddress['toString']>>).get()
27
- : await (rollupAddressValue as AztecAsyncSingleton<ReturnType<EthAddress['toString']>>).getAsync();
28
-
29
- if (typeof storedRollupAddressString !== 'undefined' && storedRollupAddressString !== rollupAddressString) {
30
- log?.warn(`Rollup address mismatch. Clearing entire database...`, {
31
- expected: rollupAddressString,
32
- found: storedRollupAddressString,
33
- });
25
+ const targetSchemaVersion = schemaVersion ?? 0;
26
+ const targetRollupAddress = rollupAddress ?? EthAddress.ZERO;
27
+ const targetDatabaseVersion = new DatabaseVersion(targetSchemaVersion, targetRollupAddress);
28
+
29
+ // DB version: database schema version + rollup address combined)
30
+ const dbVersion = store.openSingleton<string>('dbVersion');
31
+
32
+ const storedDatabaseVersion = isSyncStore(store)
33
+ ? (dbVersion as AztecSingleton<string>).get()
34
+ : await (dbVersion as AztecAsyncSingleton<string>).getAsync();
35
+
36
+ // Check if this is an old format store (has rollupAddress singleton but no dbVersion)
37
+ const oldRollupSingleton = store.openSingleton<string>('rollupAddress');
38
+ const hasOldFormat = isSyncStore(store)
39
+ ? !storedDatabaseVersion && !!(oldRollupSingleton as AztecSingleton<string>).get()
40
+ : !storedDatabaseVersion && !!(await (oldRollupSingleton as AztecAsyncSingleton<string>).getAsync());
34
41
 
42
+ if (
43
+ hasOldFormat ||
44
+ doesStoreNeedToBeCleared(
45
+ targetDatabaseVersion,
46
+ storedDatabaseVersion,
47
+ targetSchemaVersion,
48
+ targetRollupAddress,
49
+ log,
50
+ )
51
+ ) {
52
+ if (hasOldFormat) {
53
+ log?.warn('Detected old store format without dbVersion, clearing database');
54
+ }
35
55
  await store.clear();
36
56
  }
37
57
 
38
- await rollupAddressValue.set(rollupAddressString);
58
+ await dbVersion.set(targetDatabaseVersion.toBuffer().toString('utf-8'));
59
+
39
60
  return store;
40
61
  }
62
+
63
+ function doesStoreNeedToBeCleared(
64
+ targetDatabaseVersion: DatabaseVersion,
65
+ storedDatabaseVersion: string | undefined,
66
+ targetSchemaVersion: number,
67
+ targetRollupAddress: EthAddress,
68
+ log?: Logger,
69
+ ) {
70
+ if (storedDatabaseVersion) {
71
+ try {
72
+ const storedVersion = DatabaseVersion.fromBuffer(Buffer.from(storedDatabaseVersion, 'utf-8'));
73
+ const cmp = storedVersion.cmp(targetDatabaseVersion);
74
+
75
+ if (cmp === undefined) {
76
+ log?.warn('Rollup address changed, clearing database', {
77
+ stored: storedVersion.rollupAddress.toString(),
78
+ current: targetRollupAddress.toString(),
79
+ });
80
+ return true;
81
+ }
82
+
83
+ if (cmp !== 0) {
84
+ log?.warn('Schema version changed, clearing database', {
85
+ stored: storedVersion.schemaVersion,
86
+ current: targetSchemaVersion,
87
+ });
88
+
89
+ return true;
90
+ }
91
+ } catch (err) {
92
+ log?.warn('Failed to parse stored version, clearing database', { err });
93
+ return true;
94
+ }
95
+ }
96
+
97
+ return false;
98
+ }