@aztec/kv-store 0.0.1-commit.6230efd → 0.0.1-commit.6b90f3f5

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 (51) hide show
  1. package/dest/indexeddb/array.js +3 -1
  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/interfaces/utils.d.ts +2 -1
  6. package/dest/interfaces/utils.d.ts.map +1 -1
  7. package/dest/interfaces/utils.js +2 -1
  8. package/dest/lmdb/array.js +4 -2
  9. package/dest/lmdb/index.d.ts +2 -2
  10. package/dest/lmdb/index.d.ts.map +1 -1
  11. package/dest/lmdb/index.js +3 -3
  12. package/dest/lmdb-v2/array.d.ts +2 -2
  13. package/dest/lmdb-v2/array.d.ts.map +1 -1
  14. package/dest/lmdb-v2/array.js +4 -3
  15. package/dest/lmdb-v2/factory.d.ts +6 -6
  16. package/dest/lmdb-v2/factory.d.ts.map +1 -1
  17. package/dest/lmdb-v2/factory.js +14 -10
  18. package/dest/lmdb-v2/map.d.ts +2 -2
  19. package/dest/lmdb-v2/map.d.ts.map +1 -1
  20. package/dest/lmdb-v2/map.js +1 -2
  21. package/dest/lmdb-v2/multi_map.d.ts +2 -2
  22. package/dest/lmdb-v2/multi_map.d.ts.map +1 -1
  23. package/dest/lmdb-v2/multi_map.js +1 -2
  24. package/dest/lmdb-v2/singleton.d.ts +2 -2
  25. package/dest/lmdb-v2/singleton.d.ts.map +1 -1
  26. package/dest/lmdb-v2/singleton.js +1 -2
  27. package/dest/lmdb-v2/store.d.ts +4 -5
  28. package/dest/lmdb-v2/store.d.ts.map +1 -1
  29. package/dest/lmdb-v2/store.js +3 -25
  30. package/dest/lmdb-v2/tx-helpers.d.ts +6 -0
  31. package/dest/lmdb-v2/tx-helpers.d.ts.map +1 -0
  32. package/dest/lmdb-v2/tx-helpers.js +21 -0
  33. package/dest/stores/l2_tips_store.d.ts +24 -10
  34. package/dest/stores/l2_tips_store.d.ts.map +1 -1
  35. package/dest/stores/l2_tips_store.js +61 -54
  36. package/dest/utils.d.ts +9 -6
  37. package/dest/utils.d.ts.map +1 -1
  38. package/dest/utils.js +45 -16
  39. package/package.json +7 -7
  40. package/src/indexeddb/index.ts +8 -6
  41. package/src/interfaces/utils.ts +1 -0
  42. package/src/lmdb/index.ts +8 -3
  43. package/src/lmdb-v2/array.ts +2 -2
  44. package/src/lmdb-v2/factory.ts +15 -11
  45. package/src/lmdb-v2/map.ts +2 -2
  46. package/src/lmdb-v2/multi_map.ts +2 -2
  47. package/src/lmdb-v2/singleton.ts +2 -2
  48. package/src/lmdb-v2/store.ts +5 -31
  49. package/src/lmdb-v2/tx-helpers.ts +29 -0
  50. package/src/stores/l2_tips_store.ts +63 -56
  51. package/src/utils.ts +69 -21
@@ -1,15 +1,29 @@
1
- import { BlockNumber } from '@aztec/foundation/branded-types';
2
- import type { L2BlockStreamEvent, L2BlockStreamEventHandler, L2BlockStreamLocalDataProvider, L2Tips } 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';
3
4
  import type { AztecAsyncKVStore } from '../interfaces/store.js';
4
- /** Stores currently synced L2 tips and unfinalized block hashes. */
5
- export declare class L2TipsKVStore implements L2BlockStreamEventHandler, L2BlockStreamLocalDataProvider {
5
+ /**
6
+ * Persistent implementation of L2 tips store backed by a KV store.
7
+ * Used by nodes that need to persist chain state across restarts.
8
+ */
9
+ export declare class L2TipsKVStore extends L2TipsStoreBase {
10
+ private store;
6
11
  private readonly l2TipsStore;
7
12
  private readonly l2BlockHashesStore;
13
+ private readonly l2BlockNumberToCheckpointNumberStore;
14
+ private readonly l2CheckpointStore;
8
15
  constructor(store: AztecAsyncKVStore, namespace: string);
9
- getL2BlockHash(number: BlockNumber): Promise<string | undefined>;
10
- getL2Tips(): Promise<L2Tips>;
11
- private getL2Tip;
12
- handleBlockStreamEvent(event: L2BlockStreamEvent): Promise<void>;
13
- private saveTag;
16
+ protected getTip(tag: L2BlockTag): Promise<BlockNumber | undefined>;
17
+ protected setTip(tag: L2BlockTag, blockNumber: BlockNumber): Promise<void>;
18
+ protected getStoredBlockHash(blockNumber: BlockNumber): Promise<string | undefined>;
19
+ protected setBlockHash(blockNumber: BlockNumber, hash: string): Promise<void>;
20
+ protected deleteBlockHashesBefore(blockNumber: BlockNumber): Promise<void>;
21
+ protected getCheckpointNumberForBlock(blockNumber: BlockNumber): Promise<CheckpointNumber | undefined>;
22
+ protected setCheckpointNumberForBlock(blockNumber: BlockNumber, checkpointNumber: CheckpointNumber): Promise<void>;
23
+ protected deleteBlockToCheckpointBefore(blockNumber: BlockNumber): Promise<void>;
24
+ protected getCheckpoint(checkpointNumber: CheckpointNumber): Promise<PublishedCheckpoint | undefined>;
25
+ protected saveCheckpointData(checkpoint: PublishedCheckpoint): Promise<void>;
26
+ protected deleteCheckpointsBefore(checkpointNumber: CheckpointNumber): Promise<void>;
27
+ protected runInTransaction<T>(fn: () => Promise<T>): Promise<T>;
14
28
  }
15
- //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibDJfdGlwc19zdG9yZS5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL3N0b3Jlcy9sMl90aXBzX3N0b3JlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUNBLE9BQU8sRUFBRSxXQUFXLEVBQUUsTUFBTSxpQ0FBaUMsQ0FBQztBQUM5RCxPQUFPLEtBQUssRUFFVixrQkFBa0IsRUFDbEIseUJBQXlCLEVBQ3pCLDhCQUE4QixFQUU5QixNQUFNLEVBQ1AsTUFBTSxxQkFBcUIsQ0FBQztBQUc3QixPQUFPLEtBQUssRUFBRSxpQkFBa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AAS,CAAC,CAElF;IAED,SAAS,CAAC,YAAY,CAAC,WAAW,EAAE,WAAW,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAE5E;IAED,UAAgB,uBAAuB,CAAC,WAAW,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC,CAI/E;IAED,SAAS,CAAC,2BAA2B,CAAC,WAAW,EAAE,WAAW,GAAG,OAAO,CAAC,gBAAgB,GAAG,SAAS,CAAC,CAErG;IAED,SAAS,CAAC,2BAA2B,CAAC,WAAW,EAAE,WAAW,EAAE,gBAAgB,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC,CAEjH;IAED,UAAgB,6BAA6B,CAAC,WAAW,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC,CAIrF;IAED,UAAgB,aAAa,CAAC,gBAAgB,EAAE,gBAAgB,GAAG,OAAO,CAAC,mBAAmB,GAAG,SAAS,CAAC,CAM1G;IAED,SAAS,CAAC,kBAAkB,CAAC,UAAU,EAAE,mBAAmB,GAAG,OAAO,CAAC,IAAI,CAAC,CAE3E;IAED,UAAgB,uBAAuB,CAAC,gBAAgB,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC,CAIzF;IAED,SAAS,CAAC,gBAAgB,CAAC,CAAC,EAAE,EAAE,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAE9D;CACF"}
@@ -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,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidXRpbHMuZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3NyYy91dGlscy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEVBQUUsVUFBVSxFQUFFLE1BQU0sK0JBQStCLENBQUM7QUFDM0QsT0FBTyxLQUFLLEVBQUUsTUFBTSxFQUFFLE1BQU0sdUJBQXVCLENBQUM7QUFJcEQsT0FBTyxLQUFLLEVBQUUsaUJBQWlCLEVBQUUsWUFBWSxFQUFFLE1BQU0sdUJBQXVCLENBQUM7QUFHN0U7Ozs7Ozs7OztHQVNHO0FBQ0gsd0JBQXNCLGtDQUFrQyxDQUFDLENBQUMsU0FBUyxZQUFZLEdBQUcsaUJBQWlCLEVBQ2pHLEtBQUssRUFBRSxDQUFDLEVBQ1IsYUFBYSxFQUFFLE1BQU0sR0FBRyxTQUFTLEVBQ2pDLGFBQWEsRUFBRSxVQUFVLEdBQUcsU0FBUyxFQUNyQyxHQUFHLENBQUMsRUFBRSxNQUFNLEdBQ1gsT0FBTyxDQUFDLENBQUMsQ0FBQyxDQTJCWiJ9
@@ -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,CA2BZ"}
package/dest/utils.js CHANGED
@@ -1,24 +1,53 @@
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
+ if (doesStoreNeedToBeCleared(targetDatabaseVersion, storedDatabaseVersion, targetSchemaVersion, targetRollupAddress, log)) {
20
21
  await store.clear();
21
22
  }
22
- await rollupAddressValue.set(rollupAddressString);
23
+ await dbVersion.set(targetDatabaseVersion.toBuffer().toString('utf-8'));
23
24
  return store;
24
25
  }
26
+ function doesStoreNeedToBeCleared(targetDatabaseVersion, storedDatabaseVersion, targetSchemaVersion, targetRollupAddress, log) {
27
+ if (storedDatabaseVersion) {
28
+ try {
29
+ const storedVersion = DatabaseVersion.fromBuffer(Buffer.from(storedDatabaseVersion, 'utf-8'));
30
+ const cmp = storedVersion.cmp(targetDatabaseVersion);
31
+ if (cmp === undefined) {
32
+ log?.warn('Rollup address changed, clearing database', {
33
+ stored: storedVersion.rollupAddress.toString(),
34
+ current: targetRollupAddress.toString()
35
+ });
36
+ return true;
37
+ }
38
+ if (cmp !== 0) {
39
+ log?.warn('Schema version changed, clearing database', {
40
+ stored: storedVersion.schemaVersion,
41
+ current: targetSchemaVersion
42
+ });
43
+ return true;
44
+ }
45
+ } catch (err) {
46
+ log?.warn('Failed to parse stored version, clearing database', {
47
+ err
48
+ });
49
+ return true;
50
+ }
51
+ }
52
+ return false;
53
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aztec/kv-store",
3
- "version": "0.0.1-commit.6230efd",
3
+ "version": "0.0.1-commit.6b90f3f5",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./dest/interfaces/index.js",
@@ -25,11 +25,11 @@
25
25
  "./package.local.json"
26
26
  ],
27
27
  "dependencies": {
28
- "@aztec/constants": "0.0.1-commit.6230efd",
29
- "@aztec/ethereum": "0.0.1-commit.6230efd",
30
- "@aztec/foundation": "0.0.1-commit.6230efd",
31
- "@aztec/native": "0.0.1-commit.6230efd",
32
- "@aztec/stdlib": "0.0.1-commit.6230efd",
28
+ "@aztec/constants": "0.0.1-commit.6b90f3f5",
29
+ "@aztec/ethereum": "0.0.1-commit.6b90f3f5",
30
+ "@aztec/foundation": "0.0.1-commit.6b90f3f5",
31
+ "@aztec/native": "0.0.1-commit.6b90f3f5",
32
+ "@aztec/stdlib": "0.0.1-commit.6b90f3f5",
33
33
  "idb": "^8.0.0",
34
34
  "lmdb": "^3.2.0",
35
35
  "msgpackr": "^1.11.2",
@@ -45,7 +45,7 @@
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",
48
+ "@typescript/native-preview": "7.0.0-dev.20260113.1",
49
49
  "@web/dev-server-esbuild": "^1.0.3",
50
50
  "@web/test-runner": "^0.19.0",
51
51
  "@web/test-runner-playwright": "^0.11.0",
@@ -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> {
@@ -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> {
@@ -1,6 +1,6 @@
1
1
  import { EthAddress } from '@aztec/foundation/eth-address';
2
- import { type Logger, createLogger } from '@aztec/foundation/log';
3
- import { DatabaseVersionManager } from '@aztec/stdlib/database-version';
2
+ import { type LoggerBindings, createLogger } from '@aztec/foundation/log';
3
+ import { DatabaseVersionManager } from '@aztec/stdlib/database-version/manager';
4
4
 
5
5
  import { mkdir, mkdtemp, rm } from 'fs/promises';
6
6
  import { tmpdir } from 'os';
@@ -15,8 +15,9 @@ export async function createStore(
15
15
  name: string,
16
16
  schemaVersion: number,
17
17
  config: DataStoreConfig,
18
- log: Logger = createLogger('kv-store:lmdb-v2:' + name),
18
+ bindings?: LoggerBindings,
19
19
  ): Promise<AztecLMDBStoreV2> {
20
+ const log = createLogger('kv-store:lmdb-v2:' + name, bindings);
20
21
  const { dataDirectory, l1Contracts } = config;
21
22
 
22
23
  let store: AztecLMDBStoreV2;
@@ -33,7 +34,7 @@ export async function createStore(
33
34
  rollupAddress,
34
35
  dataDirectory: subDir,
35
36
  onOpen: dbDirectory =>
36
- AztecLMDBStoreV2.new(dbDirectory, config.dataStoreMapSizeKb, MAX_READERS, () => Promise.resolve(), log),
37
+ AztecLMDBStoreV2.new(dbDirectory, config.dataStoreMapSizeKb, MAX_READERS, () => Promise.resolve(), bindings),
37
38
  });
38
39
 
39
40
  log.info(
@@ -41,7 +42,7 @@ export async function createStore(
41
42
  );
42
43
  [store] = await versionManager.open();
43
44
  } else {
44
- store = await openTmpStore(name, true, config.dataStoreMapSizeKb, MAX_READERS, log);
45
+ store = await openTmpStore(name, true, config.dataStoreMapSizeKb, MAX_READERS, bindings);
45
46
  }
46
47
 
47
48
  return store;
@@ -52,8 +53,9 @@ export async function openTmpStore(
52
53
  ephemeral: boolean = true,
53
54
  dbMapSizeKb = 10 * 1_024 * 1_024, // 10GB
54
55
  maxReaders = MAX_READERS,
55
- log: Logger = createLogger('kv-store:lmdb-v2:' + name),
56
+ bindings?: LoggerBindings,
56
57
  ): Promise<AztecLMDBStoreV2> {
58
+ const log = createLogger('kv-store:lmdb-v2:' + name, bindings);
57
59
  const dataDir = await mkdtemp(join(tmpdir(), name + '-'));
58
60
  log.debug(`Created temporary data store at: ${dataDir} with size: ${dbMapSizeKb} KB (LMDB v2)`);
59
61
 
@@ -73,17 +75,18 @@ export async function openTmpStore(
73
75
 
74
76
  // For temporary stores, we don't need to worry about versioning
75
77
  // as they are ephemeral and get cleaned up after use
76
- return AztecLMDBStoreV2.new(dataDir, dbMapSizeKb, maxReaders, cleanup, log);
78
+ return AztecLMDBStoreV2.new(dataDir, dbMapSizeKb, maxReaders, cleanup, bindings);
77
79
  }
78
80
 
79
81
  export async function openStoreAt(
80
82
  dataDir: string,
81
83
  dbMapSizeKb = 10 * 1_024 * 1_024, // 10GB
82
84
  maxReaders = MAX_READERS,
83
- log: Logger = createLogger('kv-store:lmdb-v2'),
85
+ bindings?: LoggerBindings,
84
86
  ): Promise<AztecLMDBStoreV2> {
87
+ const log = createLogger('kv-store:lmdb-v2', bindings);
85
88
  log.debug(`Opening data store at: ${dataDir} with size: ${dbMapSizeKb} KB (LMDB v2)`);
86
- return await AztecLMDBStoreV2.new(dataDir, dbMapSizeKb, maxReaders, undefined, log);
89
+ return await AztecLMDBStoreV2.new(dataDir, dbMapSizeKb, maxReaders, undefined, bindings);
87
90
  }
88
91
 
89
92
  export async function openVersionedStoreAt(
@@ -92,14 +95,15 @@ export async function openVersionedStoreAt(
92
95
  rollupAddress: EthAddress,
93
96
  dbMapSizeKb = 10 * 1_024 * 1_024, // 10GB
94
97
  maxReaders = MAX_READERS,
95
- log: Logger = createLogger('kv-store:lmdb-v2'),
98
+ bindings?: LoggerBindings,
96
99
  ): Promise<AztecLMDBStoreV2> {
100
+ const log = createLogger('kv-store:lmdb-v2', bindings);
97
101
  log.debug(`Opening data store at: ${dataDirectory} with size: ${dbMapSizeKb} KB (LMDB v2)`);
98
102
  const [store] = await new DatabaseVersionManager({
99
103
  schemaVersion,
100
104
  rollupAddress,
101
105
  dataDirectory,
102
- onOpen: dataDir => AztecLMDBStoreV2.new(dataDir, dbMapSizeKb, maxReaders, undefined, log),
106
+ onOpen: dataDir => AztecLMDBStoreV2.new(dataDir, dbMapSizeKb, maxReaders, undefined, bindings),
103
107
  }).open();
104
108
  return store;
105
109
  }
@@ -3,8 +3,8 @@ import { Encoder } from 'msgpackr';
3
3
  import type { Key, Range, Value } from '../interfaces/common.js';
4
4
  import type { AztecAsyncMap } from '../interfaces/map.js';
5
5
  import type { ReadTransaction } from './read_transaction.js';
6
- // eslint-disable-next-line import/no-cycle
7
- import { type AztecLMDBStoreV2, execInReadTx, execInWriteTx } from './store.js';
6
+ import type { AztecLMDBStoreV2 } from './store.js';
7
+ import { execInReadTx, execInWriteTx } from './tx-helpers.js';
8
8
  import { deserializeKey, maxKey, minKey, serializeKey } from './utils.js';
9
9
 
10
10
  export class LMDBMap<K extends Key, V extends Value> implements AztecAsyncMap<K, V> {
@@ -4,8 +4,8 @@ import { MAXIMUM_KEY, toBufferKey } from 'ordered-binary';
4
4
  import type { Key, Range, Value } from '../interfaces/common.js';
5
5
  import type { AztecAsyncMultiMap } from '../interfaces/multi_map.js';
6
6
  import type { ReadTransaction } from './read_transaction.js';
7
- // eslint-disable-next-line import/no-cycle
8
- import { type 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, maxKey, minKey, serializeKey } from './utils.js';
10
10
 
11
11
  export class LMDBMultiMap<K extends Key, V extends Value> implements AztecAsyncMultiMap<K, V> {
@@ -1,8 +1,8 @@
1
1
  import { Encoder } from 'msgpackr';
2
2
 
3
3
  import type { AztecAsyncSingleton } from '../interfaces/singleton.js';
4
- // eslint-disable-next-line import/no-cycle
5
- import { type AztecLMDBStoreV2, execInReadTx, execInWriteTx } from './store.js';
4
+ import type { AztecLMDBStoreV2 } from './store.js';
5
+ import { execInReadTx, execInWriteTx } from './tx-helpers.js';
6
6
  import { serializeKey } from './utils.js';
7
7
 
8
8
  export class LMDBSingleValue<T> implements AztecAsyncSingleton<T> {
@@ -1,4 +1,4 @@
1
- import { type Logger, createLogger } from '@aztec/foundation/log';
1
+ import { type Logger, type LoggerBindings, createLogger } from '@aztec/foundation/log';
2
2
  import { Semaphore, SerialQueue } from '@aztec/foundation/queue';
3
3
  import { MsgpackChannel, NativeLMDBStore } from '@aztec/native';
4
4
 
@@ -13,9 +13,7 @@ import type { AztecAsyncMultiMap } from '../interfaces/multi_map.js';
13
13
  import type { AztecAsyncSet } from '../interfaces/set.js';
14
14
  import type { AztecAsyncSingleton } from '../interfaces/singleton.js';
15
15
  import type { AztecAsyncKVStore } from '../interfaces/store.js';
16
- // eslint-disable-next-line import/no-cycle
17
16
  import { LMDBArray } from './array.js';
18
- // eslint-disable-next-line import/no-cycle
19
17
  import { LMDBMap } from './map.js';
20
18
  import {
21
19
  Database,
@@ -27,10 +25,11 @@ import {
27
25
  import { LMDBMultiMap } from './multi_map.js';
28
26
  import { ReadTransaction } from './read_transaction.js';
29
27
  import { LMDBSet } from './set.js';
30
- // eslint-disable-next-line import/no-cycle
31
28
  import { LMDBSingleValue } from './singleton.js';
32
29
  import { WriteTransaction } from './write_transaction.js';
33
30
 
31
+ export { execInReadTx, execInWriteTx } from './tx-helpers.js';
32
+
34
33
  export class AztecLMDBStoreV2 implements AztecAsyncKVStore, LMDBMessageChannel {
35
34
  private open = false;
36
35
  private channel: MsgpackChannel<LMDBMessageType, LMDBRequestBody, LMDBResponseBody>;
@@ -76,8 +75,9 @@ export class AztecLMDBStoreV2 implements AztecAsyncKVStore, LMDBMessageChannel {
76
75
  dbMapSizeKb: number = 10 * 1024 * 1024,
77
76
  maxReaders: number = 16,
78
77
  cleanup?: () => Promise<void>,
79
- log = createLogger('kv-store:lmdb-v2'),
78
+ bindings?: LoggerBindings,
80
79
  ) {
80
+ const log = createLogger('kv-store:lmdb-v2', bindings);
81
81
  const db = new AztecLMDBStoreV2(dataDir, dbMapSizeKb, maxReaders, log, cleanup);
82
82
  await db.start();
83
83
  return db;
@@ -217,29 +217,3 @@ export class AztecLMDBStoreV2 implements AztecAsyncKVStore, LMDBMessageChannel {
217
217
  };
218
218
  }
219
219
  }
220
-
221
- export function execInWriteTx<T>(store: AztecLMDBStoreV2, fn: (tx: WriteTransaction) => Promise<T>): Promise<T> {
222
- const currentWrite = store.getCurrentWriteTx();
223
- if (currentWrite) {
224
- return fn(currentWrite);
225
- } else {
226
- return store.transactionAsync(fn);
227
- }
228
- }
229
-
230
- export async function execInReadTx<T>(
231
- store: AztecLMDBStoreV2,
232
- fn: (tx: ReadTransaction) => T | Promise<T>,
233
- ): Promise<T> {
234
- const currentWrite = store.getCurrentWriteTx();
235
- if (currentWrite) {
236
- return await fn(currentWrite);
237
- } else {
238
- const tx = store.getReadTx();
239
- try {
240
- return await fn(tx);
241
- } finally {
242
- tx.close();
243
- }
244
- }
245
- }
@@ -0,0 +1,29 @@
1
+ import type { ReadTransaction } from './read_transaction.js';
2
+ import type { AztecLMDBStoreV2 } from './store.js';
3
+ import type { WriteTransaction } from './write_transaction.js';
4
+
5
+ export function execInWriteTx<T>(store: AztecLMDBStoreV2, fn: (tx: WriteTransaction) => Promise<T>): Promise<T> {
6
+ const currentWrite = store.getCurrentWriteTx();
7
+ if (currentWrite) {
8
+ return fn(currentWrite);
9
+ } else {
10
+ return store.transactionAsync(fn);
11
+ }
12
+ }
13
+
14
+ export async function execInReadTx<T>(
15
+ store: AztecLMDBStoreV2,
16
+ fn: (tx: ReadTransaction) => T | Promise<T>,
17
+ ): Promise<T> {
18
+ const currentWrite = store.getCurrentWriteTx();
19
+ if (currentWrite) {
20
+ return await fn(currentWrite);
21
+ } else {
22
+ const tx = store.getReadTx();
23
+ try {
24
+ return await fn(tx);
25
+ } finally {
26
+ tx.close();
27
+ }
28
+ }
29
+ }