@aztec/kv-store 0.0.1-commit.fce3e4f → 0.0.1-commit.fffb133c

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 (41) hide show
  1. package/dest/indexeddb/array.js +3 -1
  2. package/dest/lmdb/array.js +4 -2
  3. package/dest/lmdb/store.js +1 -1
  4. package/dest/lmdb-v2/array.d.ts +2 -2
  5. package/dest/lmdb-v2/array.d.ts.map +1 -1
  6. package/dest/lmdb-v2/array.js +4 -3
  7. package/dest/lmdb-v2/map.d.ts +2 -2
  8. package/dest/lmdb-v2/map.d.ts.map +1 -1
  9. package/dest/lmdb-v2/map.js +1 -2
  10. package/dest/lmdb-v2/multi_map.d.ts +2 -2
  11. package/dest/lmdb-v2/multi_map.d.ts.map +1 -1
  12. package/dest/lmdb-v2/multi_map.js +1 -2
  13. package/dest/lmdb-v2/singleton.d.ts +2 -2
  14. package/dest/lmdb-v2/singleton.d.ts.map +1 -1
  15. package/dest/lmdb-v2/singleton.js +1 -2
  16. package/dest/lmdb-v2/store.d.ts +2 -3
  17. package/dest/lmdb-v2/store.d.ts.map +1 -1
  18. package/dest/lmdb-v2/store.js +1 -24
  19. package/dest/lmdb-v2/tx-helpers.d.ts +6 -0
  20. package/dest/lmdb-v2/tx-helpers.d.ts.map +1 -0
  21. package/dest/lmdb-v2/tx-helpers.js +21 -0
  22. package/dest/lmdb-v2/utils.d.ts +1 -10
  23. package/dest/lmdb-v2/utils.d.ts.map +1 -1
  24. package/dest/lmdb-v2/utils.js +0 -94
  25. package/dest/lmdb-v2/write_transaction.d.ts +1 -1
  26. package/dest/lmdb-v2/write_transaction.d.ts.map +1 -1
  27. package/dest/lmdb-v2/write_transaction.js +9 -5
  28. package/dest/stores/l2_tips_store.d.ts +24 -9
  29. package/dest/stores/l2_tips_store.d.ts.map +1 -1
  30. package/dest/stores/l2_tips_store.js +61 -52
  31. package/package.json +10 -8
  32. package/src/lmdb/store.ts +1 -1
  33. package/src/lmdb-v2/array.ts +2 -2
  34. package/src/lmdb-v2/map.ts +2 -2
  35. package/src/lmdb-v2/multi_map.ts +2 -2
  36. package/src/lmdb-v2/singleton.ts +2 -2
  37. package/src/lmdb-v2/store.ts +2 -29
  38. package/src/lmdb-v2/tx-helpers.ts +29 -0
  39. package/src/lmdb-v2/utils.ts +0 -118
  40. package/src/lmdb-v2/write_transaction.ts +9 -8
  41. package/src/stores/l2_tips_store.ts +65 -56
@@ -1,7 +1,16 @@
1
- /** 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;
2
8
  l2TipsStore;
3
9
  l2BlockHashesStore;
10
+ l2BlockNumberToCheckpointNumberStore;
11
+ l2CheckpointStore;
4
12
  constructor(store, namespace){
13
+ super(), this.store = store;
5
14
  this.l2TipsStore = store.openMap([
6
15
  namespace,
7
16
  'l2_tips'
@@ -10,65 +19,65 @@
10
19
  namespace,
11
20
  'l2_block_hashes'
12
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);
13
36
  }
14
- getL2BlockHash(number) {
15
- return this.l2BlockHashesStore.getAsync(number);
37
+ getStoredBlockHash(blockNumber) {
38
+ return this.l2BlockHashesStore.getAsync(blockNumber);
16
39
  }
17
- async getL2Tips() {
18
- return {
19
- latest: await this.getL2Tip('latest'),
20
- finalized: await this.getL2Tip('finalized'),
21
- proven: await this.getL2Tip('proven')
22
- };
40
+ setBlockHash(blockNumber, hash) {
41
+ return this.l2BlockHashesStore.set(blockNumber, hash);
23
42
  }
24
- async getL2Tip(tag) {
25
- const blockNumber = await this.l2TipsStore.getAsync(tag);
26
- if (blockNumber === undefined || blockNumber === 0) {
27
- return {
28
- number: 0,
29
- hash: undefined
30
- };
43
+ async deleteBlockHashesBefore(blockNumber) {
44
+ for await (const key of this.l2BlockHashesStore.keysAsync({
45
+ end: blockNumber
46
+ })){
47
+ await this.l2BlockHashesStore.delete(key);
31
48
  }
32
- const blockHash = await this.l2BlockHashesStore.getAsync(blockNumber);
33
- if (!blockHash) {
34
- 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);
35
61
  }
36
- return {
37
- number: blockNumber,
38
- hash: blockHash
39
- };
40
62
  }
41
- async handleBlockStreamEvent(event) {
42
- switch(event.type){
43
- case 'blocks-added':
44
- {
45
- const blocks = event.blocks.map((b)=>b.block);
46
- for (const block of blocks){
47
- await this.l2BlockHashesStore.set(block.number, (await block.hash()).toString());
48
- }
49
- await this.l2TipsStore.set('latest', blocks.at(-1).number);
50
- break;
51
- }
52
- case 'chain-pruned':
53
- await this.saveTag('latest', event.block);
54
- break;
55
- case 'chain-proven':
56
- await this.saveTag('proven', event.block);
57
- break;
58
- case 'chain-finalized':
59
- await this.saveTag('finalized', event.block);
60
- for await (const key of this.l2BlockHashesStore.keysAsync({
61
- end: event.block.number
62
- })){
63
- await this.l2BlockHashesStore.delete(key);
64
- }
65
- break;
63
+ async getCheckpoint(checkpointNumber) {
64
+ const buffer = await this.l2CheckpointStore.getAsync(checkpointNumber);
65
+ if (!buffer) {
66
+ return undefined;
66
67
  }
68
+ return PublishedCheckpoint.fromBuffer(buffer);
67
69
  }
68
- async saveTag(name, block) {
69
- await this.l2TipsStore.set(name, block.number);
70
- if (block.hash) {
71
- 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);
72
78
  }
73
79
  }
80
+ runInTransaction(fn) {
81
+ return this.store.transactionAsync(fn);
82
+ }
74
83
  }
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "@aztec/kv-store",
3
- "version": "0.0.1-commit.fce3e4f",
3
+ "version": "0.0.1-commit.fffb133c",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": "./dest/interfaces/index.js",
7
+ "./interfaces": "./dest/interfaces/index.js",
7
8
  "./lmdb": "./dest/lmdb/index.js",
8
9
  "./lmdb-v2": "./dest/lmdb-v2/index.js",
9
10
  "./indexeddb": "./dest/indexeddb/index.js",
@@ -11,8 +12,8 @@
11
12
  "./config": "./dest/config.js"
12
13
  },
13
14
  "scripts": {
14
- "build": "yarn clean && tsgo -b",
15
- "build:dev": "tsgo -b --watch",
15
+ "build": "yarn clean && ../scripts/tsc.sh",
16
+ "build:dev": "../scripts/tsc.sh --watch",
16
17
  "clean": "rm -rf ./dest .tsbuildinfo",
17
18
  "test:node": "NODE_NO_WARNINGS=1 mocha --config ./.mocharc.json",
18
19
  "test:browser": "wtr --config ./web-test-runner.config.mjs",
@@ -24,10 +25,11 @@
24
25
  "./package.local.json"
25
26
  ],
26
27
  "dependencies": {
27
- "@aztec/ethereum": "0.0.1-commit.fce3e4f",
28
- "@aztec/foundation": "0.0.1-commit.fce3e4f",
29
- "@aztec/native": "0.0.1-commit.fce3e4f",
30
- "@aztec/stdlib": "0.0.1-commit.fce3e4f",
28
+ "@aztec/constants": "0.0.1-commit.fffb133c",
29
+ "@aztec/ethereum": "0.0.1-commit.fffb133c",
30
+ "@aztec/foundation": "0.0.1-commit.fffb133c",
31
+ "@aztec/native": "0.0.1-commit.fffb133c",
32
+ "@aztec/stdlib": "0.0.1-commit.fffb133c",
31
33
  "idb": "^8.0.0",
32
34
  "lmdb": "^3.2.0",
33
35
  "msgpackr": "^1.11.2",
@@ -43,7 +45,7 @@
43
45
  "@types/mocha-each": "^2.0.4",
44
46
  "@types/node": "^22.15.17",
45
47
  "@types/sinon": "^17.0.3",
46
- "@typescript/native-preview": "7.0.0-dev.20251126.1",
48
+ "@typescript/native-preview": "7.0.0-dev.20260113.1",
47
49
  "@web/dev-server-esbuild": "^1.0.3",
48
50
  "@web/test-runner": "^0.19.0",
49
51
  "@web/test-runner-playwright": "^0.11.0",
package/src/lmdb/store.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { randomBytes } from '@aztec/foundation/crypto';
1
+ import { randomBytes } from '@aztec/foundation/crypto/random';
2
2
  import { createLogger } from '@aztec/foundation/log';
3
3
 
4
4
  import { promises as fs, mkdirSync } from 'fs';
@@ -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> {
@@ -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> {
@@ -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>;
@@ -217,29 +216,3 @@ export class AztecLMDBStoreV2 implements AztecAsyncKVStore, LMDBMessageChannel {
217
216
  };
218
217
  }
219
218
  }
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
+ }
@@ -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
- singleKeyCmp,
13
- } from './utils.js';
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.push([key, null]);
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
- // make a copy of this in case we're running in reverse
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(batch.removeEntries, key, singleKeyCmp);
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,79 +1,88 @@
1
- import type {
2
- L2BlockId,
3
- L2BlockStreamEvent,
4
- L2BlockStreamEventHandler,
5
- L2BlockStreamLocalDataProvider,
6
- L2BlockTag,
7
- L2Tips,
8
- } 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';
9
4
 
10
5
  import type { AztecAsyncMap } from '../interfaces/map.js';
11
6
  import type { AztecAsyncKVStore } from '../interfaces/store.js';
12
7
 
13
- /** Stores currently synced L2 tips and unfinalized block hashes. */
14
- export class L2TipsKVStore implements L2BlockStreamEventHandler, L2BlockStreamLocalDataProvider {
15
- private readonly l2TipsStore: AztecAsyncMap<L2BlockTag, number>;
16
- private readonly l2BlockHashesStore: AztecAsyncMap<number, string>;
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 {
13
+ private readonly l2TipsStore: AztecAsyncMap<L2BlockTag, BlockNumber>;
14
+ private readonly l2BlockHashesStore: AztecAsyncMap<BlockNumber, string>;
15
+ private readonly l2BlockNumberToCheckpointNumberStore: AztecAsyncMap<BlockNumber, CheckpointNumber>;
16
+ private readonly l2CheckpointStore: AztecAsyncMap<CheckpointNumber, Buffer>;
17
17
 
18
- constructor(store: AztecAsyncKVStore, namespace: string) {
18
+ constructor(
19
+ private store: AztecAsyncKVStore,
20
+ namespace: string,
21
+ ) {
22
+ super();
19
23
  this.l2TipsStore = store.openMap([namespace, 'l2_tips'].join('_'));
20
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('_'));
21
29
  }
22
30
 
23
- public getL2BlockHash(number: number): Promise<string | undefined> {
24
- return this.l2BlockHashesStore.getAsync(number);
31
+ protected getTip(tag: L2BlockTag): Promise<BlockNumber | undefined> {
32
+ return this.l2TipsStore.getAsync(tag);
25
33
  }
26
34
 
27
- public async getL2Tips(): Promise<L2Tips> {
28
- return {
29
- latest: await this.getL2Tip('latest'),
30
- finalized: await this.getL2Tip('finalized'),
31
- proven: await this.getL2Tip('proven'),
32
- };
35
+ protected setTip(tag: L2BlockTag, blockNumber: BlockNumber): Promise<void> {
36
+ return this.l2TipsStore.set(tag, blockNumber);
33
37
  }
34
38
 
35
- private async getL2Tip(tag: L2BlockTag): Promise<L2BlockId> {
36
- const blockNumber = await this.l2TipsStore.getAsync(tag);
37
- if (blockNumber === undefined || blockNumber === 0) {
38
- return { number: 0, hash: undefined };
39
- }
40
- const blockHash = await this.l2BlockHashesStore.getAsync(blockNumber);
41
- if (!blockHash) {
42
- throw new Error(`Block hash not found for block number ${blockNumber}`);
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);
43
50
  }
51
+ }
52
+
53
+ protected getCheckpointNumberForBlock(blockNumber: BlockNumber): Promise<CheckpointNumber | undefined> {
54
+ return this.l2BlockNumberToCheckpointNumberStore.getAsync(blockNumber);
55
+ }
44
56
 
45
- return { number: blockNumber, hash: blockHash };
57
+ protected setCheckpointNumberForBlock(blockNumber: BlockNumber, checkpointNumber: CheckpointNumber): Promise<void> {
58
+ return this.l2BlockNumberToCheckpointNumberStore.set(blockNumber, checkpointNumber);
46
59
  }
47
60
 
48
- public async handleBlockStreamEvent(event: L2BlockStreamEvent): Promise<void> {
49
- switch (event.type) {
50
- case 'blocks-added': {
51
- const blocks = event.blocks.map(b => b.block);
52
- for (const block of blocks) {
53
- await this.l2BlockHashesStore.set(block.number, (await block.hash()).toString());
54
- }
55
- await this.l2TipsStore.set('latest', blocks.at(-1)!.number);
56
- break;
57
- }
58
- case 'chain-pruned':
59
- await this.saveTag('latest', event.block);
60
- break;
61
- case 'chain-proven':
62
- await this.saveTag('proven', event.block);
63
- break;
64
- case 'chain-finalized':
65
- await this.saveTag('finalized', event.block);
66
- for await (const key of this.l2BlockHashesStore.keysAsync({ end: event.block.number })) {
67
- await this.l2BlockHashesStore.delete(key);
68
- }
69
- 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);
70
64
  }
71
65
  }
72
66
 
73
- private async saveTag(name: L2BlockTag, block: L2BlockId) {
74
- await this.l2TipsStore.set(name, block.number);
75
- if (block.hash) {
76
- await this.l2BlockHashesStore.set(block.number, block.hash);
67
+ protected async getCheckpoint(checkpointNumber: CheckpointNumber): Promise<PublishedCheckpoint | undefined> {
68
+ const buffer = await this.l2CheckpointStore.getAsync(checkpointNumber);
69
+ if (!buffer) {
70
+ return undefined;
77
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);
78
87
  }
79
88
  }