@aztec/kv-store 0.0.1-commit.54489865 → 0.0.1-commit.592b9384
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.
- package/dest/indexeddb/index.d.ts +2 -2
- package/dest/indexeddb/index.d.ts.map +1 -1
- package/dest/indexeddb/index.js +3 -6
- package/dest/lmdb/index.d.ts +2 -2
- package/dest/lmdb/index.d.ts.map +1 -1
- package/dest/lmdb/index.js +3 -3
- package/dest/lmdb-v2/factory.js +1 -1
- package/dest/utils.d.ts +9 -6
- package/dest/utils.d.ts.map +1 -1
- package/dest/utils.js +45 -16
- package/package.json +6 -6
- package/src/indexeddb/index.ts +8 -6
- package/src/lmdb/index.ts +8 -3
- package/src/lmdb-v2/factory.ts +1 -1
- package/src/utils.ts +69 -21
|
@@ -2,6 +2,6 @@ import { type Logger } from '@aztec/foundation/log';
|
|
|
2
2
|
import type { DataStoreConfig } from '../config.js';
|
|
3
3
|
import { AztecIndexedDBStore } from './store.js';
|
|
4
4
|
export { AztecIndexedDBStore } from './store.js';
|
|
5
|
-
export declare function createStore(name: string, config: DataStoreConfig, log?: Logger): Promise<AztecIndexedDBStore>;
|
|
5
|
+
export declare function createStore(name: string, config: DataStoreConfig, schemaVersion?: number | undefined, log?: Logger): Promise<AztecIndexedDBStore>;
|
|
6
6
|
export declare function openTmpStore(ephemeral?: boolean): Promise<AztecIndexedDBStore>;
|
|
7
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
7
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9pbmRleGVkZGIvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsT0FBTyxFQUFFLEtBQUssTUFBTSxFQUFnQixNQUFNLHVCQUF1QixDQUFDO0FBRWxFLE9BQU8sS0FBSyxFQUFFLGVBQWUsRUFBRSxNQUFNLGNBQWMsQ0FBQztBQUVwRCxPQUFPLEVBQUUsbUJBQW1CLEVBQUUsTUFBTSxZQUFZLENBQUM7QUFFakQsT0FBTyxFQUFFLG1CQUFtQixFQUFFLE1BQU0sWUFBWSxDQUFDO0FBRWpELHdCQUFzQixXQUFXLENBQy9CLElBQUksRUFBRSxNQUFNLEVBQ1osTUFBTSxFQUFFLGVBQWUsRUFDdkIsYUFBYSxHQUFFLE1BQU0sR0FBRyxTQUFxQixFQUM3QyxHQUFHLEdBQUUsTUFBaUMsZ0NBY3ZDO0FBRUQsd0JBQWdCLFlBQVksQ0FBQyxTQUFTLEdBQUUsT0FBZSxHQUFHLE9BQU8sQ0FBQyxtQkFBbUIsQ0FBQyxDQUVyRiJ9
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/indexeddb/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,MAAM,EAAgB,MAAM,uBAAuB,CAAC;AAElE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAEpD,OAAO,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC;AAEjD,OAAO,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC;AAEjD,wBAAsB,WAAW,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/indexeddb/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,MAAM,EAAgB,MAAM,uBAAuB,CAAC;AAElE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAEpD,OAAO,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC;AAEjD,OAAO,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC;AAEjD,wBAAsB,WAAW,CAC/B,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,eAAe,EACvB,aAAa,GAAE,MAAM,GAAG,SAAqB,EAC7C,GAAG,GAAE,MAAiC,gCAcvC;AAED,wBAAgB,YAAY,CAAC,SAAS,GAAE,OAAe,GAAG,OAAO,CAAC,mBAAmB,CAAC,CAErF"}
|
package/dest/indexeddb/index.js
CHANGED
|
@@ -1,18 +1,15 @@
|
|
|
1
1
|
import { createLogger } from '@aztec/foundation/log';
|
|
2
|
-
import {
|
|
2
|
+
import { initStoreForRollupAndSchemaVersion } from '../utils.js';
|
|
3
3
|
import { AztecIndexedDBStore } from './store.js';
|
|
4
4
|
export { AztecIndexedDBStore } from './store.js';
|
|
5
|
-
export async function createStore(name, config, log = createLogger('kv-store')) {
|
|
5
|
+
export async function createStore(name, config, schemaVersion = undefined, log = createLogger('kv-store')) {
|
|
6
6
|
let { dataDirectory } = config;
|
|
7
7
|
if (typeof dataDirectory !== 'undefined') {
|
|
8
8
|
dataDirectory = `${dataDirectory}/${name}`;
|
|
9
9
|
}
|
|
10
10
|
log.info(dataDirectory ? `Creating ${name} data store at directory ${dataDirectory} with map size ${config.dataStoreMapSizeKb} KB` : `Creating ${name} ephemeral data store with map size ${config.dataStoreMapSizeKb} KB`);
|
|
11
11
|
const store = await AztecIndexedDBStore.open(createLogger('kv-store:indexeddb'), dataDirectory ?? '', false);
|
|
12
|
-
|
|
13
|
-
return initStoreForRollup(store, config.l1Contracts.rollupAddress, log);
|
|
14
|
-
}
|
|
15
|
-
return store;
|
|
12
|
+
return initStoreForRollupAndSchemaVersion(store, schemaVersion, config.l1Contracts?.rollupAddress, log);
|
|
16
13
|
}
|
|
17
14
|
export function openTmpStore(ephemeral = false) {
|
|
18
15
|
return AztecIndexedDBStore.open(createLogger('kv-store:indexeddb'), undefined, ephemeral);
|
package/dest/lmdb/index.d.ts
CHANGED
|
@@ -2,11 +2,11 @@ import { type Logger } from '@aztec/foundation/log';
|
|
|
2
2
|
import type { DataStoreConfig } from '../config.js';
|
|
3
3
|
import { AztecLmdbStore } from './store.js';
|
|
4
4
|
export { AztecLmdbStore } from './store.js';
|
|
5
|
-
export declare function createStore(name: string, config: DataStoreConfig, log?: Logger): AztecLmdbStore | Promise<AztecLmdbStore>;
|
|
5
|
+
export declare function createStore(name: string, config: DataStoreConfig, schemaVersion?: number | undefined, log?: Logger): AztecLmdbStore | Promise<AztecLmdbStore>;
|
|
6
6
|
/**
|
|
7
7
|
* Opens a temporary store for testing purposes.
|
|
8
8
|
* @param ephemeral - true if the store should only exist in memory and not automatically be flushed to disk. Optional
|
|
9
9
|
* @returns A new store
|
|
10
10
|
*/
|
|
11
11
|
export declare function openTmpStore(ephemeral?: boolean): AztecLmdbStore;
|
|
12
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
12
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9sbWRiL2luZGV4LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sRUFBRSxLQUFLLE1BQU0sRUFBZ0IsTUFBTSx1QkFBdUIsQ0FBQztBQUlsRSxPQUFPLEtBQUssRUFBRSxlQUFlLEVBQUUsTUFBTSxjQUFjLENBQUM7QUFFcEQsT0FBTyxFQUFFLGNBQWMsRUFBRSxNQUFNLFlBQVksQ0FBQztBQUU1QyxPQUFPLEVBQUUsY0FBYyxFQUFFLE1BQU0sWUFBWSxDQUFDO0FBRTVDLHdCQUFnQixXQUFXLENBQ3pCLElBQUksRUFBRSxNQUFNLEVBQ1osTUFBTSxFQUFFLGVBQWUsRUFDdkIsYUFBYSxHQUFFLE1BQU0sR0FBRyxTQUFxQixFQUM3QyxHQUFHLEdBQUUsTUFBaUMsNENBa0J2QztBQUNEOzs7O0dBSUc7QUFDSCx3QkFBZ0IsWUFBWSxDQUFDLFNBQVMsR0FBRSxPQUFlLEdBQUcsY0FBYyxDQUd2RSJ9
|
package/dest/lmdb/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/lmdb/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,MAAM,EAAgB,MAAM,uBAAuB,CAAC;AAIlE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAEpD,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAE5C,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAE5C,wBAAgB,WAAW,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/lmdb/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,MAAM,EAAgB,MAAM,uBAAuB,CAAC;AAIlE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAEpD,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAE5C,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAE5C,wBAAgB,WAAW,CACzB,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,eAAe,EACvB,aAAa,GAAE,MAAM,GAAG,SAAqB,EAC7C,GAAG,GAAE,MAAiC,4CAkBvC;AACD;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,SAAS,GAAE,OAAe,GAAG,cAAc,CAGvE"}
|
package/dest/lmdb/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { createLogger } from '@aztec/foundation/log';
|
|
2
2
|
import { join } from 'path';
|
|
3
|
-
import {
|
|
3
|
+
import { initStoreForRollupAndSchemaVersion } from '../utils.js';
|
|
4
4
|
import { AztecLmdbStore } from './store.js';
|
|
5
5
|
export { AztecLmdbStore } from './store.js';
|
|
6
|
-
export function createStore(name, config, log = createLogger('kv-store')) {
|
|
6
|
+
export function createStore(name, config, schemaVersion = undefined, log = createLogger('kv-store')) {
|
|
7
7
|
let { dataDirectory } = config;
|
|
8
8
|
if (typeof dataDirectory !== 'undefined') {
|
|
9
9
|
dataDirectory = join(dataDirectory, name);
|
|
@@ -11,7 +11,7 @@ export function createStore(name, config, log = createLogger('kv-store')) {
|
|
|
11
11
|
log.info(dataDirectory ? `Creating ${name} data store at directory ${dataDirectory} with map size ${config.dataStoreMapSizeKb} KB` : `Creating ${name} ephemeral data store with map size ${config.dataStoreMapSizeKb} KB`);
|
|
12
12
|
const store = AztecLmdbStore.open(dataDirectory, config.dataStoreMapSizeKb, false);
|
|
13
13
|
if (config.l1Contracts?.rollupAddress) {
|
|
14
|
-
return
|
|
14
|
+
return initStoreForRollupAndSchemaVersion(store, schemaVersion, config.l1Contracts.rollupAddress, log);
|
|
15
15
|
}
|
|
16
16
|
return store;
|
|
17
17
|
}
|
package/dest/lmdb-v2/factory.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
2
2
|
import { createLogger } from '@aztec/foundation/log';
|
|
3
|
-
import { DatabaseVersionManager } from '@aztec/stdlib/database-version';
|
|
3
|
+
import { DatabaseVersionManager } from '@aztec/stdlib/database-version/manager';
|
|
4
4
|
import { mkdir, mkdtemp, rm } from 'fs/promises';
|
|
5
5
|
import { tmpdir } from 'os';
|
|
6
6
|
import { join } from 'path';
|
package/dest/utils.d.ts
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
|
-
import
|
|
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
|
-
*
|
|
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
|
-
* @
|
|
11
|
+
* @param log - Optional logger
|
|
12
|
+
* @returns The store (cleared if necessary)
|
|
10
13
|
*/
|
|
11
|
-
export declare function
|
|
12
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
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
|
package/dest/utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
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
|
-
*
|
|
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
|
-
* @
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
const
|
|
15
|
-
|
|
16
|
-
|
|
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
|
|
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.
|
|
3
|
+
"version": "0.0.1-commit.592b9384",
|
|
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.
|
|
29
|
-
"@aztec/ethereum": "0.0.1-commit.
|
|
30
|
-
"@aztec/foundation": "0.0.1-commit.
|
|
31
|
-
"@aztec/native": "0.0.1-commit.
|
|
32
|
-
"@aztec/stdlib": "0.0.1-commit.
|
|
28
|
+
"@aztec/constants": "0.0.1-commit.592b9384",
|
|
29
|
+
"@aztec/ethereum": "0.0.1-commit.592b9384",
|
|
30
|
+
"@aztec/foundation": "0.0.1-commit.592b9384",
|
|
31
|
+
"@aztec/native": "0.0.1-commit.592b9384",
|
|
32
|
+
"@aztec/stdlib": "0.0.1-commit.592b9384",
|
|
33
33
|
"idb": "^8.0.0",
|
|
34
34
|
"lmdb": "^3.2.0",
|
|
35
35
|
"msgpackr": "^1.11.2",
|
package/src/indexeddb/index.ts
CHANGED
|
@@ -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 {
|
|
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(
|
|
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
|
-
|
|
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> {
|
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 {
|
|
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(
|
|
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
|
|
30
|
+
return initStoreForRollupAndSchemaVersion(store, schemaVersion, config.l1Contracts.rollupAddress, log);
|
|
26
31
|
}
|
|
27
32
|
return store;
|
|
28
33
|
}
|
package/src/lmdb-v2/factory.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
2
2
|
import { type LoggerBindings, createLogger } from '@aztec/foundation/log';
|
|
3
|
-
import { DatabaseVersionManager } from '@aztec/stdlib/database-version';
|
|
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';
|
package/src/utils.ts
CHANGED
|
@@ -1,40 +1,88 @@
|
|
|
1
|
-
import
|
|
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
|
-
*
|
|
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
|
-
* @
|
|
16
|
+
* @param log - Optional logger
|
|
17
|
+
* @returns The store (cleared if necessary)
|
|
14
18
|
*/
|
|
15
|
-
export async function
|
|
19
|
+
export async function initStoreForRollupAndSchemaVersion<T extends AztecKVStore | AztecAsyncKVStore>(
|
|
16
20
|
store: T,
|
|
17
|
-
|
|
21
|
+
schemaVersion: number | undefined,
|
|
22
|
+
rollupAddress: EthAddress | undefined,
|
|
18
23
|
log?: Logger,
|
|
19
24
|
): Promise<T> {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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();
|
|
34
35
|
|
|
36
|
+
if (
|
|
37
|
+
doesStoreNeedToBeCleared(
|
|
38
|
+
targetDatabaseVersion,
|
|
39
|
+
storedDatabaseVersion,
|
|
40
|
+
targetSchemaVersion,
|
|
41
|
+
targetRollupAddress,
|
|
42
|
+
log,
|
|
43
|
+
)
|
|
44
|
+
) {
|
|
35
45
|
await store.clear();
|
|
36
46
|
}
|
|
37
47
|
|
|
38
|
-
await
|
|
48
|
+
await dbVersion.set(targetDatabaseVersion.toBuffer().toString('utf-8'));
|
|
49
|
+
|
|
39
50
|
return store;
|
|
40
51
|
}
|
|
52
|
+
|
|
53
|
+
function doesStoreNeedToBeCleared(
|
|
54
|
+
targetDatabaseVersion: DatabaseVersion,
|
|
55
|
+
storedDatabaseVersion: string | undefined,
|
|
56
|
+
targetSchemaVersion: number,
|
|
57
|
+
targetRollupAddress: EthAddress,
|
|
58
|
+
log?: Logger,
|
|
59
|
+
) {
|
|
60
|
+
if (storedDatabaseVersion) {
|
|
61
|
+
try {
|
|
62
|
+
const storedVersion = DatabaseVersion.fromBuffer(Buffer.from(storedDatabaseVersion, 'utf-8'));
|
|
63
|
+
const cmp = storedVersion.cmp(targetDatabaseVersion);
|
|
64
|
+
|
|
65
|
+
if (cmp === undefined) {
|
|
66
|
+
log?.warn('Rollup address changed, clearing database', {
|
|
67
|
+
stored: storedVersion.rollupAddress.toString(),
|
|
68
|
+
current: targetRollupAddress.toString(),
|
|
69
|
+
});
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (cmp !== 0) {
|
|
74
|
+
log?.warn('Schema version changed, clearing database', {
|
|
75
|
+
stored: storedVersion.schemaVersion,
|
|
76
|
+
current: targetSchemaVersion,
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
} catch (err) {
|
|
82
|
+
log?.warn('Failed to parse stored version, clearing database', { err });
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return false;
|
|
88
|
+
}
|