@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.
- package/dest/indexeddb/array.js +21 -7
- 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/indexeddb/map.d.ts +8 -2
- package/dest/indexeddb/map.d.ts.map +1 -1
- package/dest/indexeddb/map.js +23 -13
- package/dest/indexeddb/multi_map.d.ts +2 -1
- package/dest/indexeddb/multi_map.d.ts.map +1 -1
- package/dest/indexeddb/multi_map.js +16 -1
- package/dest/indexeddb/singleton.js +3 -1
- package/dest/indexeddb/store.d.ts +3 -3
- package/dest/indexeddb/store.d.ts.map +1 -1
- package/dest/indexeddb/store.js +6 -4
- package/dest/interfaces/map_test_suite.d.ts +1 -1
- package/dest/interfaces/map_test_suite.d.ts.map +1 -1
- package/dest/interfaces/map_test_suite.js +48 -2
- package/dest/interfaces/multi_map_test_suite.d.ts +1 -1
- package/dest/interfaces/multi_map_test_suite.d.ts.map +1 -1
- package/dest/interfaces/multi_map_test_suite.js +25 -0
- package/dest/interfaces/utils.d.ts +2 -1
- package/dest/interfaces/utils.d.ts.map +1 -1
- package/dest/interfaces/utils.js +2 -1
- package/dest/lmdb/array.js +4 -2
- 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/array.d.ts +2 -2
- package/dest/lmdb-v2/array.d.ts.map +1 -1
- package/dest/lmdb-v2/array.js +4 -3
- package/dest/lmdb-v2/factory.d.ts +6 -6
- package/dest/lmdb-v2/factory.d.ts.map +1 -1
- package/dest/lmdb-v2/factory.js +14 -10
- package/dest/lmdb-v2/map.d.ts +2 -2
- package/dest/lmdb-v2/map.d.ts.map +1 -1
- package/dest/lmdb-v2/map.js +1 -2
- package/dest/lmdb-v2/multi_map.d.ts +2 -2
- package/dest/lmdb-v2/multi_map.d.ts.map +1 -1
- package/dest/lmdb-v2/multi_map.js +1 -2
- package/dest/lmdb-v2/singleton.d.ts +2 -2
- package/dest/lmdb-v2/singleton.d.ts.map +1 -1
- package/dest/lmdb-v2/singleton.js +1 -2
- package/dest/lmdb-v2/store.d.ts +4 -5
- package/dest/lmdb-v2/store.d.ts.map +1 -1
- package/dest/lmdb-v2/store.js +3 -25
- package/dest/lmdb-v2/tx-helpers.d.ts +6 -0
- package/dest/lmdb-v2/tx-helpers.d.ts.map +1 -0
- package/dest/lmdb-v2/tx-helpers.js +21 -0
- package/dest/lmdb-v2/utils.d.ts +1 -10
- package/dest/lmdb-v2/utils.d.ts.map +1 -1
- package/dest/lmdb-v2/utils.js +0 -94
- package/dest/lmdb-v2/write_transaction.d.ts +1 -1
- package/dest/lmdb-v2/write_transaction.d.ts.map +1 -1
- package/dest/lmdb-v2/write_transaction.js +9 -5
- package/dest/stores/l2_tips_store.d.ts +24 -10
- package/dest/stores/l2_tips_store.d.ts.map +1 -1
- package/dest/stores/l2_tips_store.js +61 -54
- package/dest/utils.d.ts +9 -6
- package/dest/utils.d.ts.map +1 -1
- package/dest/utils.js +51 -16
- package/package.json +15 -13
- package/src/indexeddb/array.ts +4 -4
- package/src/indexeddb/index.ts +8 -6
- package/src/indexeddb/map.ts +24 -11
- package/src/indexeddb/multi_map.ts +15 -1
- package/src/indexeddb/singleton.ts +1 -1
- package/src/indexeddb/store.ts +13 -6
- package/src/interfaces/map_test_suite.ts +30 -2
- package/src/interfaces/multi_map_test_suite.ts +32 -0
- package/src/interfaces/utils.ts +1 -0
- package/src/lmdb/index.ts +8 -3
- package/src/lmdb-v2/array.ts +2 -2
- package/src/lmdb-v2/factory.ts +15 -11
- package/src/lmdb-v2/map.ts +2 -2
- package/src/lmdb-v2/multi_map.ts +2 -2
- package/src/lmdb-v2/singleton.ts +2 -2
- package/src/lmdb-v2/store.ts +5 -31
- package/src/lmdb-v2/tx-helpers.ts +29 -0
- package/src/lmdb-v2/utils.ts +0 -118
- package/src/lmdb-v2/write_transaction.ts +9 -8
- package/src/stores/l2_tips_store.ts +63 -56
- package/src/utils.ts +79 -21
|
@@ -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
|
});
|
package/src/interfaces/utils.ts
CHANGED
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/array.ts
CHANGED
|
@@ -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
|
-
|
|
8
|
-
import {
|
|
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> {
|
package/src/lmdb-v2/factory.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { EthAddress } from '@aztec/foundation/eth-address';
|
|
2
|
-
import { type
|
|
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
|
-
|
|
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(),
|
|
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,
|
|
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
|
-
|
|
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,
|
|
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
|
-
|
|
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,
|
|
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
|
-
|
|
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,
|
|
106
|
+
onOpen: dataDir => AztecLMDBStoreV2.new(dataDir, dbMapSizeKb, maxReaders, undefined, bindings),
|
|
103
107
|
}).open();
|
|
104
108
|
return store;
|
|
105
109
|
}
|
package/src/lmdb-v2/map.ts
CHANGED
|
@@ -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
|
-
|
|
7
|
-
import {
|
|
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> {
|
package/src/lmdb-v2/multi_map.ts
CHANGED
|
@@ -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
|
-
|
|
8
|
-
import {
|
|
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> {
|
package/src/lmdb-v2/singleton.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Encoder } from 'msgpackr';
|
|
2
2
|
|
|
3
3
|
import type { AztecAsyncSingleton } from '../interfaces/singleton.js';
|
|
4
|
-
|
|
5
|
-
import {
|
|
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> {
|
package/src/lmdb-v2/store.ts
CHANGED
|
@@ -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
|
-
|
|
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
|
+
}
|
package/src/lmdb-v2/utils.ts
CHANGED
|
@@ -2,124 +2,6 @@ import { MAXIMUM_KEY, fromBufferKey, toBufferKey } from 'ordered-binary';
|
|
|
2
2
|
|
|
3
3
|
import type { Key } from '../interfaces/common.js';
|
|
4
4
|
|
|
5
|
-
type Cmp<T> = (a: T, b: T) => -1 | 0 | 1;
|
|
6
|
-
|
|
7
|
-
export function dedupeSortedArray<T>(arr: T[], cmp: Cmp<T>): void {
|
|
8
|
-
for (let i = 0; i < arr.length; i++) {
|
|
9
|
-
let j = i + 1;
|
|
10
|
-
for (; j < arr.length; j++) {
|
|
11
|
-
const res = cmp(arr[i], arr[j]);
|
|
12
|
-
if (res === 0) {
|
|
13
|
-
continue;
|
|
14
|
-
} else if (res < 0) {
|
|
15
|
-
break;
|
|
16
|
-
} else {
|
|
17
|
-
throw new Error('Array not sorted');
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
if (j - i > 1) {
|
|
22
|
-
arr.splice(i + 1, j - i - 1);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export function insertIntoSortedArray<T>(arr: T[], item: T, cmp: (a: T, b: T) => number): void {
|
|
28
|
-
let left = 0;
|
|
29
|
-
let right = arr.length;
|
|
30
|
-
|
|
31
|
-
while (left < right) {
|
|
32
|
-
const mid = (left + right) >> 1;
|
|
33
|
-
const comparison = cmp(arr[mid], item);
|
|
34
|
-
|
|
35
|
-
if (comparison < 0) {
|
|
36
|
-
left = mid + 1;
|
|
37
|
-
} else {
|
|
38
|
-
right = mid;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
arr.splice(left, 0, item);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
export function findIndexInSortedArray<T, N>(values: T[], needle: N, cmp: (a: T, b: N) => number): number {
|
|
46
|
-
let start = 0;
|
|
47
|
-
let end = values.length - 1;
|
|
48
|
-
|
|
49
|
-
while (start <= end) {
|
|
50
|
-
const mid = start + (((end - start) / 2) | 0);
|
|
51
|
-
const res = cmp(values[mid], needle);
|
|
52
|
-
if (res === 0) {
|
|
53
|
-
return mid;
|
|
54
|
-
} else if (res > 0) {
|
|
55
|
-
end = mid - 1;
|
|
56
|
-
} else {
|
|
57
|
-
start = mid + 1;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
return -1;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export function findInSortedArray<T, N>(values: T[], needle: N, cmp: (a: T, b: N) => number): T | undefined {
|
|
65
|
-
const idx = findIndexInSortedArray(values, needle, cmp);
|
|
66
|
-
return idx > -1 ? values[idx] : undefined;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
export function removeAnyOf<T, N>(arr: T[], vals: N[], cmp: (a: T, b: N) => -1 | 0 | 1): void {
|
|
70
|
-
let writeIdx = 0;
|
|
71
|
-
let readIdx = 0;
|
|
72
|
-
let valIdx = 0;
|
|
73
|
-
|
|
74
|
-
while (readIdx < arr.length && valIdx < vals.length) {
|
|
75
|
-
const comparison = cmp(arr[readIdx], vals[valIdx]);
|
|
76
|
-
|
|
77
|
-
if (comparison < 0) {
|
|
78
|
-
arr[writeIdx++] = arr[readIdx++];
|
|
79
|
-
} else if (comparison > 0) {
|
|
80
|
-
valIdx++;
|
|
81
|
-
} else {
|
|
82
|
-
readIdx++;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
while (readIdx < arr.length) {
|
|
87
|
-
arr[writeIdx++] = arr[readIdx++];
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
arr.length = writeIdx;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
export function removeFromSortedArray<T, N>(arr: T[], val: N, cmp: (a: T, b: N) => -1 | 0 | 1) {
|
|
94
|
-
const idx = findIndexInSortedArray(arr, val, cmp);
|
|
95
|
-
if (idx > -1) {
|
|
96
|
-
arr.splice(idx, 1);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
export function merge<T>(arr: T[], toInsert: T[], cmp: (a: T, b: T) => -1 | 0 | 1): void {
|
|
101
|
-
const result = new Array<T>(arr.length + toInsert.length);
|
|
102
|
-
let i = 0,
|
|
103
|
-
j = 0,
|
|
104
|
-
k = 0;
|
|
105
|
-
|
|
106
|
-
while (i < arr.length && j < toInsert.length) {
|
|
107
|
-
result[k++] = cmp(arr[i], toInsert[j]) <= 0 ? arr[i++] : toInsert[j++];
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
while (i < arr.length) {
|
|
111
|
-
result[k++] = arr[i++];
|
|
112
|
-
}
|
|
113
|
-
while (j < toInsert.length) {
|
|
114
|
-
result[k++] = toInsert[j++];
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
for (i = 0; i < result.length; i++) {
|
|
118
|
-
arr[i] = result[i];
|
|
119
|
-
}
|
|
120
|
-
arr.length = result.length;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
5
|
export function keyCmp(a: [Uint8Array, Uint8Array[] | null], b: [Uint8Array, Uint8Array[] | null]): -1 | 0 | 1 {
|
|
124
6
|
return Buffer.compare(a[0], b[0]);
|
|
125
7
|
}
|
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import { type Batch, Database, LMDBMessageType } from './message.js';
|
|
2
|
-
import { ReadTransaction } from './read_transaction.js';
|
|
3
1
|
import {
|
|
4
2
|
dedupeSortedArray,
|
|
5
3
|
findInSortedArray,
|
|
6
4
|
findIndexInSortedArray,
|
|
7
5
|
insertIntoSortedArray,
|
|
8
|
-
keyCmp,
|
|
9
6
|
merge,
|
|
10
7
|
removeAnyOf,
|
|
11
8
|
removeFromSortedArray,
|
|
12
|
-
|
|
13
|
-
|
|
9
|
+
} from '@aztec/foundation/array';
|
|
10
|
+
|
|
11
|
+
import { type Batch, Database, LMDBMessageType } from './message.js';
|
|
12
|
+
import { ReadTransaction } from './read_transaction.js';
|
|
13
|
+
import { keyCmp, singleKeyCmp } from './utils.js';
|
|
14
14
|
|
|
15
15
|
export class WriteTransaction extends ReadTransaction {
|
|
16
16
|
// exposed for tests
|
|
@@ -44,7 +44,7 @@ export class WriteTransaction extends ReadTransaction {
|
|
|
44
44
|
remove(key: Uint8Array): Promise<void> {
|
|
45
45
|
const removeEntryIndex = findIndexInSortedArray(this.dataBatch.removeEntries, key, singleKeyCmp);
|
|
46
46
|
if (removeEntryIndex === -1) {
|
|
47
|
-
this.dataBatch.removeEntries
|
|
47
|
+
insertIntoSortedArray(this.dataBatch.removeEntries, [key, null], keyCmp);
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
const addEntryIndex = findIndexInSortedArray(this.dataBatch.addEntries, key, singleKeyCmp);
|
|
@@ -218,8 +218,9 @@ export class WriteTransaction extends ReadTransaction {
|
|
|
218
218
|
): AsyncIterable<[Uint8Array, T]> {
|
|
219
219
|
this.assertIsOpen();
|
|
220
220
|
|
|
221
|
-
//
|
|
221
|
+
// Snapshot both add and remove entries at the start of iteration to ensure consistency
|
|
222
222
|
const uncommittedEntries = [...batch.addEntries];
|
|
223
|
+
const removeEntries = [...batch.removeEntries];
|
|
223
224
|
// used to check we're in the right order when comparing between a key and uncommittedEntries
|
|
224
225
|
let cmpDirection = -1;
|
|
225
226
|
if (reverse) {
|
|
@@ -262,7 +263,7 @@ export class WriteTransaction extends ReadTransaction {
|
|
|
262
263
|
break;
|
|
263
264
|
}
|
|
264
265
|
|
|
265
|
-
const toRemove = findInSortedArray(
|
|
266
|
+
const toRemove = findInSortedArray(removeEntries, key, singleKeyCmp);
|
|
266
267
|
|
|
267
268
|
// at this point we've either exhausted all uncommitted entries,
|
|
268
269
|
// we reached a key strictly greater/smaller than `key`
|
|
@@ -1,81 +1,88 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
L2BlockId,
|
|
5
|
-
L2BlockStreamEvent,
|
|
6
|
-
L2BlockStreamEventHandler,
|
|
7
|
-
L2BlockStreamLocalDataProvider,
|
|
8
|
-
L2BlockTag,
|
|
9
|
-
L2Tips,
|
|
10
|
-
} 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';
|
|
11
4
|
|
|
12
5
|
import type { AztecAsyncMap } from '../interfaces/map.js';
|
|
13
6
|
import type { AztecAsyncKVStore } from '../interfaces/store.js';
|
|
14
7
|
|
|
15
|
-
/**
|
|
16
|
-
|
|
8
|
+
/**
|
|
9
|
+
* Persistent implementation of L2 tips store backed by a KV store.
|
|
10
|
+
* Used by nodes that need to persist chain state across restarts.
|
|
11
|
+
*/
|
|
12
|
+
export class L2TipsKVStore extends L2TipsStoreBase {
|
|
17
13
|
private readonly l2TipsStore: AztecAsyncMap<L2BlockTag, BlockNumber>;
|
|
18
14
|
private readonly l2BlockHashesStore: AztecAsyncMap<BlockNumber, string>;
|
|
15
|
+
private readonly l2BlockNumberToCheckpointNumberStore: AztecAsyncMap<BlockNumber, CheckpointNumber>;
|
|
16
|
+
private readonly l2CheckpointStore: AztecAsyncMap<CheckpointNumber, Buffer>;
|
|
19
17
|
|
|
20
|
-
constructor(
|
|
18
|
+
constructor(
|
|
19
|
+
private store: AztecAsyncKVStore,
|
|
20
|
+
namespace: string,
|
|
21
|
+
) {
|
|
22
|
+
super();
|
|
21
23
|
this.l2TipsStore = store.openMap([namespace, 'l2_tips'].join('_'));
|
|
22
24
|
this.l2BlockHashesStore = store.openMap([namespace, 'l2_block_hashes'].join('_'));
|
|
25
|
+
this.l2BlockNumberToCheckpointNumberStore = store.openMap(
|
|
26
|
+
[namespace, 'l2_block_number_to_checkpoint_number'].join('_'),
|
|
27
|
+
);
|
|
28
|
+
this.l2CheckpointStore = store.openMap([namespace, 'l2_checkpoint_store'].join('_'));
|
|
23
29
|
}
|
|
24
30
|
|
|
25
|
-
|
|
26
|
-
return this.
|
|
31
|
+
protected getTip(tag: L2BlockTag): Promise<BlockNumber | undefined> {
|
|
32
|
+
return this.l2TipsStore.getAsync(tag);
|
|
27
33
|
}
|
|
28
34
|
|
|
29
|
-
|
|
30
|
-
return
|
|
31
|
-
latest: await this.getL2Tip('latest'),
|
|
32
|
-
finalized: await this.getL2Tip('finalized'),
|
|
33
|
-
proven: await this.getL2Tip('proven'),
|
|
34
|
-
};
|
|
35
|
+
protected setTip(tag: L2BlockTag, blockNumber: BlockNumber): Promise<void> {
|
|
36
|
+
return this.l2TipsStore.set(tag, blockNumber);
|
|
35
37
|
}
|
|
36
38
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
39
|
+
protected getStoredBlockHash(blockNumber: BlockNumber): Promise<string | undefined> {
|
|
40
|
+
return this.l2BlockHashesStore.getAsync(blockNumber);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
protected setBlockHash(blockNumber: BlockNumber, hash: string): Promise<void> {
|
|
44
|
+
return this.l2BlockHashesStore.set(blockNumber, hash);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
protected async deleteBlockHashesBefore(blockNumber: BlockNumber): Promise<void> {
|
|
48
|
+
for await (const key of this.l2BlockHashesStore.keysAsync({ end: blockNumber })) {
|
|
49
|
+
await this.l2BlockHashesStore.delete(key);
|
|
45
50
|
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
protected getCheckpointNumberForBlock(blockNumber: BlockNumber): Promise<CheckpointNumber | undefined> {
|
|
54
|
+
return this.l2BlockNumberToCheckpointNumberStore.getAsync(blockNumber);
|
|
55
|
+
}
|
|
46
56
|
|
|
47
|
-
|
|
57
|
+
protected setCheckpointNumberForBlock(blockNumber: BlockNumber, checkpointNumber: CheckpointNumber): Promise<void> {
|
|
58
|
+
return this.l2BlockNumberToCheckpointNumberStore.set(blockNumber, checkpointNumber);
|
|
48
59
|
}
|
|
49
60
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
const blocks = event.blocks.map(b => b.block);
|
|
54
|
-
for (const block of blocks) {
|
|
55
|
-
await this.l2BlockHashesStore.set(block.number, (await block.hash()).toString());
|
|
56
|
-
}
|
|
57
|
-
await this.l2TipsStore.set('latest', blocks.at(-1)!.number);
|
|
58
|
-
break;
|
|
59
|
-
}
|
|
60
|
-
case 'chain-pruned':
|
|
61
|
-
await this.saveTag('latest', event.block);
|
|
62
|
-
break;
|
|
63
|
-
case 'chain-proven':
|
|
64
|
-
await this.saveTag('proven', event.block);
|
|
65
|
-
break;
|
|
66
|
-
case 'chain-finalized':
|
|
67
|
-
await this.saveTag('finalized', event.block);
|
|
68
|
-
for await (const key of this.l2BlockHashesStore.keysAsync({ end: event.block.number })) {
|
|
69
|
-
await this.l2BlockHashesStore.delete(key);
|
|
70
|
-
}
|
|
71
|
-
break;
|
|
61
|
+
protected async deleteBlockToCheckpointBefore(blockNumber: BlockNumber): Promise<void> {
|
|
62
|
+
for await (const key of this.l2BlockNumberToCheckpointNumberStore.keysAsync({ end: blockNumber })) {
|
|
63
|
+
await this.l2BlockNumberToCheckpointNumberStore.delete(key);
|
|
72
64
|
}
|
|
73
65
|
}
|
|
74
66
|
|
|
75
|
-
|
|
76
|
-
await this.
|
|
77
|
-
if (
|
|
78
|
-
|
|
67
|
+
protected async getCheckpoint(checkpointNumber: CheckpointNumber): Promise<PublishedCheckpoint | undefined> {
|
|
68
|
+
const buffer = await this.l2CheckpointStore.getAsync(checkpointNumber);
|
|
69
|
+
if (!buffer) {
|
|
70
|
+
return undefined;
|
|
79
71
|
}
|
|
72
|
+
return PublishedCheckpoint.fromBuffer(buffer);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
protected saveCheckpointData(checkpoint: PublishedCheckpoint): Promise<void> {
|
|
76
|
+
return this.l2CheckpointStore.set(checkpoint.checkpoint.number, checkpoint.toBuffer());
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
protected async deleteCheckpointsBefore(checkpointNumber: CheckpointNumber): Promise<void> {
|
|
80
|
+
for await (const key of this.l2CheckpointStore.keysAsync({ end: checkpointNumber })) {
|
|
81
|
+
await this.l2CheckpointStore.delete(key);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
protected runInTransaction<T>(fn: () => Promise<T>): Promise<T> {
|
|
86
|
+
return this.store.transactionAsync(fn);
|
|
80
87
|
}
|
|
81
88
|
}
|