@aztec/archiver 0.86.0 → 0.87.0

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 (57) hide show
  1. package/dest/archiver/archiver.d.ts +62 -5
  2. package/dest/archiver/archiver.d.ts.map +1 -1
  3. package/dest/archiver/archiver.js +362 -91
  4. package/dest/archiver/archiver_store.d.ts +33 -17
  5. package/dest/archiver/archiver_store.d.ts.map +1 -1
  6. package/dest/archiver/archiver_store_test_suite.d.ts.map +1 -1
  7. package/dest/archiver/archiver_store_test_suite.js +364 -62
  8. package/dest/archiver/data_retrieval.d.ts +23 -14
  9. package/dest/archiver/data_retrieval.d.ts.map +1 -1
  10. package/dest/archiver/data_retrieval.js +86 -38
  11. package/dest/archiver/errors.d.ts +8 -0
  12. package/dest/archiver/errors.d.ts.map +1 -1
  13. package/dest/archiver/errors.js +12 -0
  14. package/dest/archiver/instrumentation.d.ts.map +1 -1
  15. package/dest/archiver/kv_archiver_store/block_store.d.ts +4 -1
  16. package/dest/archiver/kv_archiver_store/block_store.d.ts.map +1 -1
  17. package/dest/archiver/kv_archiver_store/block_store.js +43 -6
  18. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts +3 -1
  19. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts.map +1 -1
  20. package/dest/archiver/kv_archiver_store/contract_instance_store.js +17 -3
  21. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts +24 -14
  22. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +1 -1
  23. package/dest/archiver/kv_archiver_store/kv_archiver_store.js +37 -11
  24. package/dest/archiver/kv_archiver_store/log_store.d.ts.map +1 -1
  25. package/dest/archiver/kv_archiver_store/log_store.js +1 -1
  26. package/dest/archiver/kv_archiver_store/message_store.d.ts +21 -15
  27. package/dest/archiver/kv_archiver_store/message_store.d.ts.map +1 -1
  28. package/dest/archiver/kv_archiver_store/message_store.js +150 -48
  29. package/dest/archiver/structs/inbox_message.d.ts +14 -0
  30. package/dest/archiver/structs/inbox_message.d.ts.map +1 -0
  31. package/dest/archiver/structs/inbox_message.js +38 -0
  32. package/dest/rpc/index.d.ts +1 -1
  33. package/dest/test/mock_l2_block_source.d.ts +2 -0
  34. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  35. package/dest/test/mock_l2_block_source.js +8 -1
  36. package/dest/test/mock_structs.d.ts +9 -0
  37. package/dest/test/mock_structs.d.ts.map +1 -0
  38. package/dest/test/mock_structs.js +37 -0
  39. package/package.json +15 -15
  40. package/src/archiver/archiver.ts +431 -108
  41. package/src/archiver/archiver_store.ts +37 -18
  42. package/src/archiver/archiver_store_test_suite.ts +307 -52
  43. package/src/archiver/data_retrieval.ts +130 -53
  44. package/src/archiver/errors.ts +21 -0
  45. package/src/archiver/instrumentation.ts +4 -1
  46. package/src/archiver/kv_archiver_store/block_store.ts +46 -8
  47. package/src/archiver/kv_archiver_store/contract_instance_store.ts +20 -7
  48. package/src/archiver/kv_archiver_store/kv_archiver_store.ts +61 -17
  49. package/src/archiver/kv_archiver_store/log_store.ts +6 -2
  50. package/src/archiver/kv_archiver_store/message_store.ts +209 -53
  51. package/src/archiver/structs/inbox_message.ts +40 -0
  52. package/src/test/mock_l2_block_source.ts +9 -1
  53. package/src/test/mock_structs.ts +49 -0
  54. package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.d.ts +0 -23
  55. package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.d.ts.map +0 -1
  56. package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.js +0 -49
  57. package/src/archiver/memory_archiver_store/l1_to_l2_message_store.ts +0 -61
@@ -1,12 +1,14 @@
1
+ import type { L1BlockId } from '@aztec/ethereum';
1
2
  import type { Fr } from '@aztec/foundation/fields';
2
3
  import { toArray } from '@aztec/foundation/iterable';
3
4
  import { createLogger } from '@aztec/foundation/log';
4
- import type { AztecAsyncKVStore, StoreSize } from '@aztec/kv-store';
5
+ import type { AztecAsyncKVStore, CustomRange, StoreSize } from '@aztec/kv-store';
5
6
  import { FunctionSelector } from '@aztec/stdlib/abi';
6
7
  import type { AztecAddress } from '@aztec/stdlib/aztec-address';
7
8
  import type { L2Block } from '@aztec/stdlib/block';
8
9
  import type {
9
10
  ContractClassPublic,
11
+ ContractDataSource,
10
12
  ContractInstanceUpdateWithAddress,
11
13
  ContractInstanceWithAddress,
12
14
  ExecutablePrivateFunctionWithMembershipProof,
@@ -14,13 +16,12 @@ import type {
14
16
  } from '@aztec/stdlib/contract';
15
17
  import type { GetContractClassLogsResponse, GetPublicLogsResponse } from '@aztec/stdlib/interfaces/client';
16
18
  import { type LogFilter, PrivateLog, type TxScopedL2Log } from '@aztec/stdlib/logs';
17
- import type { InboxLeaf } from '@aztec/stdlib/messaging';
18
19
  import type { BlockHeader, TxHash, TxReceipt } from '@aztec/stdlib/tx';
19
20
 
20
21
  import { join } from 'path';
21
22
 
22
23
  import type { ArchiverDataStore, ArchiverL1SynchPoint } from '../archiver_store.js';
23
- import type { DataRetrieval } from '../structs/data_retrieval.js';
24
+ import type { InboxMessage } from '../structs/inbox_message.js';
24
25
  import type { PublishedL2Block } from '../structs/published.js';
25
26
  import { BlockStore } from './block_store.js';
26
27
  import { ContractClassStore } from './contract_class_store.js';
@@ -28,12 +29,12 @@ import { ContractInstanceStore } from './contract_instance_store.js';
28
29
  import { LogStore } from './log_store.js';
29
30
  import { MessageStore } from './message_store.js';
30
31
 
31
- export const ARCHIVER_DB_VERSION = 1;
32
+ export const ARCHIVER_DB_VERSION = 2;
32
33
 
33
34
  /**
34
35
  * LMDB implementation of the ArchiverDataStore interface.
35
36
  */
36
- export class KVArchiverDataStore implements ArchiverDataStore {
37
+ export class KVArchiverDataStore implements ArchiverDataStore, ContractDataSource {
37
38
  public static readonly SCHEMA_VERSION = ARCHIVER_DB_VERSION;
38
39
 
39
40
  #blockStore: BlockStore;
@@ -45,7 +46,10 @@ export class KVArchiverDataStore implements ArchiverDataStore {
45
46
 
46
47
  #log = createLogger('archiver:data-store');
47
48
 
48
- constructor(private db: AztecAsyncKVStore, logsMaxPageSize: number = 1000) {
49
+ constructor(
50
+ private db: AztecAsyncKVStore,
51
+ logsMaxPageSize: number = 1000,
52
+ ) {
49
53
  this.#blockStore = new BlockStore(db);
50
54
  this.#logStore = new LogStore(db, this.#blockStore, logsMaxPageSize);
51
55
  this.#messageStore = new MessageStore(db);
@@ -53,6 +57,21 @@ export class KVArchiverDataStore implements ArchiverDataStore {
53
57
  this.#contractInstanceStore = new ContractInstanceStore(db);
54
58
  }
55
59
 
60
+ public transactionAsync<T>(callback: () => Promise<T>): Promise<T> {
61
+ return this.db.transactionAsync(callback);
62
+ }
63
+
64
+ public getBlockNumber(): Promise<number> {
65
+ return this.getSynchedL2BlockNumber();
66
+ }
67
+
68
+ public async getContract(
69
+ address: AztecAddress,
70
+ blockNumber?: number,
71
+ ): Promise<ContractInstanceWithAddress | undefined> {
72
+ return this.getContractInstance(address, blockNumber ?? (await this.getBlockNumber()));
73
+ }
74
+
56
75
  public async backupTo(path: string, compress = true): Promise<string> {
57
76
  await this.db.backupTo(path, compress);
58
77
  return join(path, 'data.mdb');
@@ -92,6 +111,10 @@ export class KVArchiverDataStore implements ArchiverDataStore {
92
111
  return this.#contractInstanceStore.getContractInstance(address, blockNumber);
93
112
  }
94
113
 
114
+ getContractInstanceDeploymentBlockNumber(address: AztecAddress): Promise<number | undefined> {
115
+ return this.#contractInstanceStore.getContractInstanceDeploymentBlockNumber(address);
116
+ }
117
+
95
118
  async addContractClasses(
96
119
  data: ContractClassPublic[],
97
120
  bytecodeCommitments: Fr[],
@@ -122,8 +145,10 @@ export class KVArchiverDataStore implements ArchiverDataStore {
122
145
  return this.#contractClassStore.addFunctions(contractClassId, privateFunctions, utilityFunctions);
123
146
  }
124
147
 
125
- async addContractInstances(data: ContractInstanceWithAddress[], _blockNumber: number): Promise<boolean> {
126
- return (await Promise.all(data.map(c => this.#contractInstanceStore.addContractInstance(c)))).every(Boolean);
148
+ async addContractInstances(data: ContractInstanceWithAddress[], blockNumber: number): Promise<boolean> {
149
+ return (await Promise.all(data.map(c => this.#contractInstanceStore.addContractInstance(c, blockNumber)))).every(
150
+ Boolean,
151
+ );
127
152
  }
128
153
 
129
154
  async deleteContractInstances(data: ContractInstanceWithAddress[], _blockNumber: number): Promise<boolean> {
@@ -157,8 +182,8 @@ export class KVArchiverDataStore implements ArchiverDataStore {
157
182
  * @param blocks - The L2 blocks to be added to the store and the last processed L1 block.
158
183
  * @returns True if the operation is successful.
159
184
  */
160
- addBlocks(blocks: PublishedL2Block[]): Promise<boolean> {
161
- return this.#blockStore.addBlocks(blocks);
185
+ addBlocks(blocks: PublishedL2Block[], opts: { force?: boolean } = {}): Promise<boolean> {
186
+ return this.#blockStore.addBlocks(blocks, opts);
162
187
  }
163
188
 
164
189
  /**
@@ -172,6 +197,10 @@ export class KVArchiverDataStore implements ArchiverDataStore {
172
197
  return this.#blockStore.unwindBlocks(from, blocksToUnwind);
173
198
  }
174
199
 
200
+ getPublishedBlock(number: number): Promise<PublishedL2Block | undefined> {
201
+ return this.#blockStore.getBlock(number);
202
+ }
203
+
175
204
  /**
176
205
  * Gets up to `limit` amount of L2 blocks starting from `from`.
177
206
  *
@@ -179,7 +208,7 @@ export class KVArchiverDataStore implements ArchiverDataStore {
179
208
  * @param limit - The number of blocks to return.
180
209
  * @returns The requested L2 blocks
181
210
  */
182
- getBlocks(start: number, limit: number): Promise<PublishedL2Block[]> {
211
+ getPublishedBlocks(start: number, limit: number): Promise<PublishedL2Block[]> {
183
212
  return toArray(this.#blockStore.getBlocks(start, limit));
184
213
  }
185
214
 
@@ -229,12 +258,15 @@ export class KVArchiverDataStore implements ArchiverDataStore {
229
258
  return this.#messageStore.getTotalL1ToL2MessageCount();
230
259
  }
231
260
 
261
+ getLastL1ToL2Message(): Promise<InboxMessage | undefined> {
262
+ return this.#messageStore.getLastMessage();
263
+ }
264
+
232
265
  /**
233
266
  * Append L1 to L2 messages to the store.
234
- * @param messages - The L1 to L2 messages to be added to the store and the last processed L1 block.
235
- * @returns True if the operation is successful.
267
+ * @param messages - The L1 to L2 messages to be added to the store.
236
268
  */
237
- addL1ToL2Messages(messages: DataRetrieval<InboxLeaf>): Promise<boolean> {
269
+ addL1ToL2Messages(messages: InboxMessage[]): Promise<void> {
238
270
  return this.#messageStore.addL1ToL2Messages(messages);
239
271
  }
240
272
 
@@ -326,8 +358,8 @@ export class KVArchiverDataStore implements ArchiverDataStore {
326
358
  await this.#blockStore.setSynchedL1BlockNumber(l1BlockNumber);
327
359
  }
328
360
 
329
- async setMessageSynchedL1BlockNumber(l1BlockNumber: bigint) {
330
- await this.#messageStore.setSynchedL1BlockNumber(l1BlockNumber);
361
+ async setMessageSynchedL1Block(l1Block: L1BlockId) {
362
+ await this.#messageStore.setSynchedL1Block(l1Block);
331
363
  }
332
364
 
333
365
  /**
@@ -336,7 +368,7 @@ export class KVArchiverDataStore implements ArchiverDataStore {
336
368
  async getSynchPoint(): Promise<ArchiverL1SynchPoint> {
337
369
  const [blocksSynchedTo, messagesSynchedTo] = await Promise.all([
338
370
  this.#blockStore.getSynchedL1BlockNumber(),
339
- this.#messageStore.getSynchedL1BlockNumber(),
371
+ this.#messageStore.getSynchedL1Block(),
340
372
  ]);
341
373
  return {
342
374
  blocksSynchedTo,
@@ -347,4 +379,16 @@ export class KVArchiverDataStore implements ArchiverDataStore {
347
379
  public estimateSize(): Promise<StoreSize> {
348
380
  return this.db.estimateSize();
349
381
  }
382
+
383
+ public rollbackL1ToL2MessagesToL2Block(targetBlockNumber: number | bigint): Promise<void> {
384
+ return this.#messageStore.rollbackL1ToL2MessagesToL2Block(BigInt(targetBlockNumber));
385
+ }
386
+
387
+ public iterateL1ToL2Messages(range: CustomRange<bigint> = {}): AsyncIterableIterator<InboxMessage> {
388
+ return this.#messageStore.iterateL1ToL2Messages(range);
389
+ }
390
+
391
+ public removeL1ToL2Messages(startIndex: bigint): Promise<void> {
392
+ return this.#messageStore.removeL1ToL2Messages(startIndex);
393
+ }
350
394
  }
@@ -30,7 +30,11 @@ export class LogStore {
30
30
  #logsMaxPageSize: number;
31
31
  #log = createLogger('archiver:log_store');
32
32
 
33
- constructor(private db: AztecAsyncKVStore, private blockStore: BlockStore, logsMaxPageSize: number = 1000) {
33
+ constructor(
34
+ private db: AztecAsyncKVStore,
35
+ private blockStore: BlockStore,
36
+ logsMaxPageSize: number = 1000,
37
+ ) {
34
38
  this.#logsByTag = db.openMap('archiver_tagged_logs_by_tag');
35
39
  this.#logTagsByBlock = db.openMap('archiver_log_tags_by_block');
36
40
  this.#privateLogsByBlock = db.openMap('archiver_private_logs_by_block');
@@ -59,7 +63,7 @@ export class LogStore {
59
63
  });
60
64
 
61
65
  txEffect.publicLogs.forEach((log, logIndex) => {
62
- const tag = log.log[0];
66
+ const tag = log.fields[0];
63
67
  this.#log.debug(`Found public log with tag ${tag.toString()} in block ${block.number}`);
64
68
 
65
69
  const currentLogs = taggedLogs.get(tag.toString()) ?? [];
@@ -1,71 +1,172 @@
1
- import { L1_TO_L2_MSG_SUBTREE_HEIGHT } from '@aztec/constants';
1
+ import type { L1BlockId } from '@aztec/ethereum';
2
+ import { Buffer16, Buffer32 } from '@aztec/foundation/buffer';
2
3
  import { Fr } from '@aztec/foundation/fields';
4
+ import { toArray } from '@aztec/foundation/iterable';
3
5
  import { createLogger } from '@aztec/foundation/log';
4
- import type { AztecAsyncKVStore, AztecAsyncMap, AztecAsyncSingleton } from '@aztec/kv-store';
6
+ import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
7
+ import {
8
+ type AztecAsyncKVStore,
9
+ type AztecAsyncMap,
10
+ type AztecAsyncSingleton,
11
+ type CustomRange,
12
+ mapRange,
13
+ } from '@aztec/kv-store';
5
14
  import { InboxLeaf } from '@aztec/stdlib/messaging';
6
15
 
7
- import type { DataRetrieval } from '../structs/data_retrieval.js';
16
+ import {
17
+ type InboxMessage,
18
+ deserializeInboxMessage,
19
+ serializeInboxMessage,
20
+ updateRollingHash,
21
+ } from '../structs/inbox_message.js';
22
+
23
+ export class MessageStoreError extends Error {
24
+ constructor(
25
+ message: string,
26
+ public readonly inboxMessage: InboxMessage,
27
+ ) {
28
+ super(message);
29
+ this.name = 'MessageStoreError';
30
+ }
31
+ }
8
32
 
9
- /**
10
- * LMDB implementation of the ArchiverDataStore interface.
11
- */
12
33
  export class MessageStore {
13
- #l1ToL2Messages: AztecAsyncMap<string, Buffer>;
34
+ /** Maps from message index to serialized InboxMessage */
35
+ #l1ToL2Messages: AztecAsyncMap<number, Buffer>;
36
+ /** Maps from hex-stringified message leaf to its index */
14
37
  #l1ToL2MessageIndices: AztecAsyncMap<string, bigint>;
15
- #lastSynchedL1Block: AztecAsyncSingleton<bigint>;
38
+ /** Stores L1 block number and hash of the L1 synchpoint */
39
+ #lastSynchedL1Block: AztecAsyncSingleton<Buffer>;
40
+ /** Stores total messages stored */
16
41
  #totalMessageCount: AztecAsyncSingleton<bigint>;
17
42
 
18
43
  #log = createLogger('archiver:message_store');
19
44
 
20
- #l1ToL2MessagesSubtreeSize = 2 ** L1_TO_L2_MSG_SUBTREE_HEIGHT;
21
-
22
45
  constructor(private db: AztecAsyncKVStore) {
23
46
  this.#l1ToL2Messages = db.openMap('archiver_l1_to_l2_messages');
24
47
  this.#l1ToL2MessageIndices = db.openMap('archiver_l1_to_l2_message_indices');
25
- this.#lastSynchedL1Block = db.openSingleton('archiver_last_l1_block_new_messages');
48
+ this.#lastSynchedL1Block = db.openSingleton('archiver_last_l1_block_id');
26
49
  this.#totalMessageCount = db.openSingleton('archiver_l1_to_l2_message_count');
27
50
  }
28
51
 
29
- async getTotalL1ToL2MessageCount(): Promise<bigint> {
52
+ public async getTotalL1ToL2MessageCount(): Promise<bigint> {
30
53
  return (await this.#totalMessageCount.getAsync()) ?? 0n;
31
54
  }
32
55
 
33
- /**
34
- * Gets the last L1 block number that emitted new messages.
35
- * @returns The last L1 block number processed
36
- */
37
- getSynchedL1BlockNumber(): Promise<bigint | undefined> {
38
- return this.#lastSynchedL1Block.getAsync();
56
+ /** Gets the last L1 block synced. */
57
+ public async getSynchedL1Block(): Promise<L1BlockId | undefined> {
58
+ const buffer = await this.#lastSynchedL1Block.getAsync();
59
+ if (!buffer) {
60
+ return undefined;
61
+ }
62
+
63
+ const reader = BufferReader.asReader(buffer);
64
+ return { l1BlockNumber: reader.readUInt256(), l1BlockHash: Buffer32.fromBuffer(reader.readBytes(Buffer32.SIZE)) };
39
65
  }
40
66
 
41
- async setSynchedL1BlockNumber(l1BlockNumber: bigint): Promise<void> {
42
- await this.#lastSynchedL1Block.set(l1BlockNumber);
67
+ /** Sets the last L1 block synced */
68
+ public async setSynchedL1Block(l1Block: L1BlockId): Promise<void> {
69
+ const buffer = serializeToBuffer([l1Block.l1BlockNumber, l1Block.l1BlockHash]);
70
+ await this.#lastSynchedL1Block.set(buffer);
43
71
  }
44
72
 
45
73
  /**
46
74
  * Append L1 to L2 messages to the store.
47
- * @param messages - The L1 to L2 messages to be added to the store and the last processed L1 block.
48
- * @returns True if the operation is successful.
75
+ * Requires new messages to be in order and strictly after the last message added.
76
+ * Throws if out of order messages are added or if the rolling hash is invalid.
49
77
  */
50
- addL1ToL2Messages(messages: DataRetrieval<InboxLeaf>): Promise<boolean> {
78
+ public addL1ToL2Messages(messages: InboxMessage[]): Promise<void> {
79
+ if (messages.length === 0) {
80
+ return Promise.resolve();
81
+ }
82
+
51
83
  return this.db.transactionAsync(async () => {
52
- const lastL1BlockNumber = (await this.#lastSynchedL1Block.getAsync()) ?? 0n;
53
- if (lastL1BlockNumber >= messages.lastProcessedL1BlockNumber) {
54
- return false;
55
- }
84
+ let lastMessage = await this.getLastMessage();
85
+ let messageCount = 0;
86
+
87
+ for (const message of messages) {
88
+ // Check messages are inserted in increasing order, but allow reinserting messages.
89
+ if (lastMessage && message.index <= lastMessage.index) {
90
+ const existing = await this.#l1ToL2Messages.getAsync(this.indexToKey(message.index));
91
+ if (existing && deserializeInboxMessage(existing).rollingHash.equals(message.rollingHash)) {
92
+ // We reinsert instead of skipping in case the message was re-orged and got added in a different L1 block.
93
+ this.#log.trace(`Reinserting message with index ${message.index} in the store`);
94
+ await this.#l1ToL2Messages.set(this.indexToKey(message.index), serializeInboxMessage(message));
95
+ continue;
96
+ }
56
97
 
57
- await this.#lastSynchedL1Block.set(messages.lastProcessedL1BlockNumber);
98
+ throw new MessageStoreError(
99
+ `Cannot insert L1 to L2 message with index ${message.index} before last message with index ${lastMessage.index}`,
100
+ message,
101
+ );
102
+ }
58
103
 
59
- for (const message of messages.retrievedData) {
60
- const key = `${message.index}`;
61
- await this.#l1ToL2Messages.set(key, message.leaf.toBuffer());
62
- await this.#l1ToL2MessageIndices.set(message.leaf.toString(), message.index);
104
+ // Check rolling hash is valid.
105
+ const previousRollingHash = lastMessage?.rollingHash ?? Buffer16.ZERO;
106
+ const expectedRollingHash = updateRollingHash(previousRollingHash, message.leaf);
107
+ if (!expectedRollingHash.equals(message.rollingHash)) {
108
+ throw new MessageStoreError(
109
+ `Invalid rolling hash for incoming L1 to L2 message ${message.leaf.toString()} ` +
110
+ `with index ${message.index} ` +
111
+ `(expected ${expectedRollingHash.toString()} from previous hash ${previousRollingHash} but got ${message.rollingHash.toString()})`,
112
+ message,
113
+ );
114
+ }
115
+
116
+ // Check index corresponds to the L2 block number.
117
+ const [expectedStart, expectedEnd] = InboxLeaf.indexRangeFromL2Block(message.l2BlockNumber);
118
+ if (message.index < expectedStart || message.index >= expectedEnd) {
119
+ throw new MessageStoreError(
120
+ `Invalid index ${message.index} for incoming L1 to L2 message ${message.leaf.toString()} ` +
121
+ `at block ${message.l2BlockNumber} (expected value in range [${expectedStart}, ${expectedEnd}))`,
122
+ message,
123
+ );
124
+ }
125
+
126
+ // Check there are no gaps in the indices within the same block.
127
+ if (
128
+ lastMessage &&
129
+ message.l2BlockNumber === lastMessage.l2BlockNumber &&
130
+ message.index !== lastMessage.index + 1n
131
+ ) {
132
+ throw new MessageStoreError(
133
+ `Missing prior message for incoming L1 to L2 message ${message.leaf.toString()} ` +
134
+ `with index ${message.index}`,
135
+ message,
136
+ );
137
+ }
138
+
139
+ // Check the first message in a block has the correct index.
140
+ if (
141
+ (!lastMessage || message.l2BlockNumber > lastMessage.l2BlockNumber) &&
142
+ message.index !== InboxLeaf.smallestIndexFromL2Block(message.l2BlockNumber)
143
+ ) {
144
+ throw new MessageStoreError(
145
+ `Message ${message.leaf.toString()} for L2 block ${message.l2BlockNumber} has wrong index ` +
146
+ `${message.index} (expected ${InboxLeaf.smallestIndexFromL2Block(message.l2BlockNumber)})`,
147
+ message,
148
+ );
149
+ }
150
+
151
+ // Perform the insertions.
152
+ await this.#l1ToL2Messages.set(this.indexToKey(message.index), serializeInboxMessage(message));
153
+ await this.#l1ToL2MessageIndices.set(this.leafToIndexKey(message.leaf), message.index);
154
+ messageCount++;
155
+ this.#log.trace(`Inserted L1 to L2 message ${message.leaf} with index ${message.index} into the store`);
156
+ lastMessage = message;
63
157
  }
64
158
 
65
- const lastTotalMessageCount = await this.getTotalL1ToL2MessageCount();
66
- await this.#totalMessageCount.set(lastTotalMessageCount + BigInt(messages.retrievedData.length));
159
+ // Update the L1 sync point to that of the last message added.
160
+ const currentSyncPoint = await this.getSynchedL1Block();
161
+ if (!currentSyncPoint || currentSyncPoint.l1BlockNumber < lastMessage!.l1BlockNumber) {
162
+ await this.setSynchedL1Block({
163
+ l1BlockNumber: lastMessage!.l1BlockNumber,
164
+ l1BlockHash: lastMessage!.l1BlockHash,
165
+ });
166
+ }
67
167
 
68
- return true;
168
+ // Update total message count with the number of inserted messages.
169
+ await this.increaseTotalMessageCount(messageCount);
69
170
  });
70
171
  }
71
172
 
@@ -74,29 +175,84 @@ export class MessageStore {
74
175
  * @param l1ToL2Message - The L1 to L2 message.
75
176
  * @returns The index of the L1 to L2 message in the L1 to L2 message tree (undefined if not found).
76
177
  */
77
- getL1ToL2MessageIndex(l1ToL2Message: Fr): Promise<bigint | undefined> {
78
- return this.#l1ToL2MessageIndices.getAsync(l1ToL2Message.toString());
178
+ public getL1ToL2MessageIndex(l1ToL2Message: Fr): Promise<bigint | undefined> {
179
+ return this.#l1ToL2MessageIndices.getAsync(this.leafToIndexKey(l1ToL2Message));
180
+ }
181
+
182
+ public async getLastMessage(): Promise<InboxMessage | undefined> {
183
+ const [msg] = await toArray(this.#l1ToL2Messages.valuesAsync({ reverse: true, limit: 1 }));
184
+ return msg ? deserializeInboxMessage(msg) : undefined;
79
185
  }
80
186
 
81
- async getL1ToL2Messages(blockNumber: bigint): Promise<Fr[]> {
187
+ public async getL1ToL2Messages(blockNumber: bigint): Promise<Fr[]> {
82
188
  const messages: Fr[] = [];
83
- let undefinedMessageFound = false;
84
- const startIndex = Number(InboxLeaf.smallestIndexFromL2Block(blockNumber));
85
- for (let i = startIndex; i < startIndex + this.#l1ToL2MessagesSubtreeSize; i++) {
86
- // This is inefficient but probably fine for now.
87
- const key = `${i}`;
88
- const message = await this.#l1ToL2Messages.getAsync(key);
89
- if (message) {
90
- if (undefinedMessageFound) {
91
- throw new Error(`L1 to L2 message gap found in block ${blockNumber}`);
92
- }
93
- messages.push(Fr.fromBuffer(message));
94
- } else {
95
- undefinedMessageFound = true;
96
- // We continue iterating over messages here to verify that there are no more messages after the undefined one.
97
- // --> If this was the case this would imply there is some issue with log fetching.
189
+
190
+ const [startIndex, endIndex] = InboxLeaf.indexRangeFromL2Block(blockNumber);
191
+ let lastIndex = startIndex - 1n;
192
+
193
+ for await (const msgBuffer of this.#l1ToL2Messages.valuesAsync({
194
+ start: this.indexToKey(startIndex),
195
+ end: this.indexToKey(endIndex),
196
+ })) {
197
+ const msg = deserializeInboxMessage(msgBuffer);
198
+ if (msg.l2BlockNumber !== blockNumber) {
199
+ throw new Error(`L1 to L2 message with index ${msg.index} has invalid block number ${msg.l2BlockNumber}`);
200
+ } else if (msg.index !== lastIndex + 1n) {
201
+ throw new Error(`Expected L1 to L2 message with index ${lastIndex + 1n} but got ${msg.index}`);
98
202
  }
203
+ lastIndex = msg.index;
204
+ messages.push(msg.leaf);
99
205
  }
206
+
100
207
  return messages;
101
208
  }
209
+
210
+ public async *iterateL1ToL2Messages(range: CustomRange<bigint> = {}): AsyncIterableIterator<InboxMessage> {
211
+ const entriesRange = mapRange(range, this.indexToKey);
212
+ for await (const msgBuffer of this.#l1ToL2Messages.valuesAsync(entriesRange)) {
213
+ yield deserializeInboxMessage(msgBuffer);
214
+ }
215
+ }
216
+
217
+ public removeL1ToL2Messages(startIndex: bigint): Promise<void> {
218
+ this.#log.debug(`Deleting L1 to L2 messages from index ${startIndex}`);
219
+ let deleteCount = 0;
220
+
221
+ return this.db.transactionAsync(async () => {
222
+ for await (const [key, msgBuffer] of this.#l1ToL2Messages.entriesAsync({
223
+ start: this.indexToKey(startIndex),
224
+ })) {
225
+ this.#log.trace(`Deleting L1 to L2 message with index ${key - 1} from the store`);
226
+ await this.#l1ToL2Messages.delete(key);
227
+ await this.#l1ToL2MessageIndices.delete(this.leafToIndexKey(deserializeInboxMessage(msgBuffer).leaf));
228
+ deleteCount++;
229
+ }
230
+ await this.increaseTotalMessageCount(-deleteCount);
231
+ this.#log.warn(`Deleted ${deleteCount} L1 to L2 messages from index ${startIndex} from the store`);
232
+ });
233
+ }
234
+
235
+ public rollbackL1ToL2MessagesToL2Block(targetBlockNumber: bigint): Promise<void> {
236
+ this.#log.debug(`Deleting L1 to L2 messages up to target L2 block ${targetBlockNumber}`);
237
+ const startIndex = InboxLeaf.smallestIndexFromL2Block(targetBlockNumber + 1n);
238
+ return this.removeL1ToL2Messages(startIndex);
239
+ }
240
+
241
+ private indexToKey(index: bigint): number {
242
+ return Number(index);
243
+ }
244
+
245
+ private leafToIndexKey(leaf: Fr): string {
246
+ return leaf.toString();
247
+ }
248
+
249
+ private async increaseTotalMessageCount(count: bigint | number): Promise<void> {
250
+ if (count === 0) {
251
+ return;
252
+ }
253
+ return await this.db.transactionAsync(async () => {
254
+ const lastTotalMessageCount = await this.getTotalL1ToL2MessageCount();
255
+ await this.#totalMessageCount.set(lastTotalMessageCount + BigInt(count));
256
+ });
257
+ }
102
258
  }
@@ -0,0 +1,40 @@
1
+ import { Buffer16, Buffer32 } from '@aztec/foundation/buffer';
2
+ import { keccak256 } from '@aztec/foundation/crypto';
3
+ import { Fr } from '@aztec/foundation/fields';
4
+ import { BufferReader, bigintToUInt64BE, numToUInt32BE, serializeToBuffer } from '@aztec/foundation/serialize';
5
+
6
+ export type InboxMessage = {
7
+ index: bigint;
8
+ leaf: Fr;
9
+ l2BlockNumber: bigint;
10
+ l1BlockNumber: bigint;
11
+ l1BlockHash: Buffer32;
12
+ rollingHash: Buffer16;
13
+ };
14
+
15
+ export function updateRollingHash(currentRollingHash: Buffer16, leaf: Fr): Buffer16 {
16
+ const input = Buffer.concat([currentRollingHash.toBuffer(), leaf.toBuffer()]);
17
+ return Buffer16.fromBuffer(keccak256(input));
18
+ }
19
+
20
+ export function serializeInboxMessage(message: InboxMessage): Buffer {
21
+ return serializeToBuffer([
22
+ bigintToUInt64BE(message.index),
23
+ message.leaf,
24
+ message.l1BlockHash,
25
+ numToUInt32BE(Number(message.l1BlockNumber)),
26
+ numToUInt32BE(Number(message.l2BlockNumber)),
27
+ message.rollingHash,
28
+ ]);
29
+ }
30
+
31
+ export function deserializeInboxMessage(buffer: Buffer): InboxMessage {
32
+ const reader = BufferReader.asReader(buffer);
33
+ const index = reader.readUInt64();
34
+ const leaf = reader.readObject(Fr);
35
+ const l1BlockHash = reader.readObject(Buffer32);
36
+ const l1BlockNumber = BigInt(reader.readNumber());
37
+ const l2BlockNumber = BigInt(reader.readNumber());
38
+ const rollingHash = reader.readObject(Buffer16);
39
+ return { index, leaf, l1BlockHash, l1BlockNumber, l2BlockNumber, rollingHash };
40
+ }
@@ -17,6 +17,7 @@ export class MockL2BlockSource implements L2BlockSource, ContractDataSource {
17
17
  protected l2Blocks: L2Block[] = [];
18
18
 
19
19
  private provenBlockNumber: number = 0;
20
+ private finalizedBlockNumber: number = 0;
20
21
 
21
22
  private log = createLogger('archiver:mock_l2_block_source');
22
23
 
@@ -44,6 +45,13 @@ export class MockL2BlockSource implements L2BlockSource, ContractDataSource {
44
45
  this.provenBlockNumber = provenBlockNumber;
45
46
  }
46
47
 
48
+ public setFinalizedBlockNumber(finalizedBlockNumber: number) {
49
+ if (finalizedBlockNumber > this.provenBlockNumber) {
50
+ this.provenBlockNumber = finalizedBlockNumber;
51
+ }
52
+ this.finalizedBlockNumber = finalizedBlockNumber;
53
+ }
54
+
47
55
  /**
48
56
  * Method to fetch the rollup contract address at the base-layer.
49
57
  * @returns The rollup address.
@@ -174,7 +182,7 @@ export class MockL2BlockSource implements L2BlockSource, ContractDataSource {
174
182
  const [latest, proven, finalized] = [
175
183
  await this.getBlockNumber(),
176
184
  await this.getProvenBlockNumber(),
177
- await this.getProvenBlockNumber(),
185
+ this.finalizedBlockNumber,
178
186
  ] as const;
179
187
 
180
188
  const latestBlock = this.l2Blocks[latest - 1];
@@ -0,0 +1,49 @@
1
+ import { Buffer16, Buffer32 } from '@aztec/foundation/buffer';
2
+ import { randomBigInt } from '@aztec/foundation/crypto';
3
+ import { Fr } from '@aztec/foundation/fields';
4
+ import { InboxLeaf } from '@aztec/stdlib/messaging';
5
+
6
+ import { type InboxMessage, updateRollingHash } from '../archiver/structs/inbox_message.js';
7
+
8
+ export function makeInboxMessage(
9
+ previousRollingHash = Buffer16.ZERO,
10
+ overrides: Partial<InboxMessage> = {},
11
+ ): InboxMessage {
12
+ const { l2BlockNumber = randomBigInt(100n) + 1n } = overrides;
13
+ const { l1BlockNumber = l2BlockNumber } = overrides;
14
+ const { l1BlockHash = Buffer32.random() } = overrides;
15
+ const { leaf = Fr.random() } = overrides;
16
+ const { rollingHash = updateRollingHash(previousRollingHash, leaf) } = overrides;
17
+ const { index = InboxLeaf.smallestIndexFromL2Block(l2BlockNumber) } = overrides;
18
+
19
+ return {
20
+ index,
21
+ leaf,
22
+ l2BlockNumber,
23
+ l1BlockNumber,
24
+ l1BlockHash,
25
+ rollingHash,
26
+ };
27
+ }
28
+
29
+ export function makeInboxMessages(
30
+ count: number,
31
+ opts: {
32
+ initialHash?: Buffer16;
33
+ initialL2BlockNumber?: bigint;
34
+ overrideFn?: (msg: InboxMessage, index: number) => InboxMessage;
35
+ } = {},
36
+ ): InboxMessage[] {
37
+ const { initialHash = Buffer16.ZERO, overrideFn = msg => msg, initialL2BlockNumber = 1n } = opts;
38
+ const messages: InboxMessage[] = [];
39
+ let rollingHash = initialHash;
40
+ for (let i = 0; i < count; i++) {
41
+ const leaf = Fr.random();
42
+ const l2BlockNumber = BigInt(i) + initialL2BlockNumber;
43
+ const message = overrideFn(makeInboxMessage(rollingHash, { leaf, l2BlockNumber }), i);
44
+ rollingHash = message.rollingHash;
45
+ messages.push(message);
46
+ }
47
+
48
+ return messages;
49
+ }
@@ -1,23 +0,0 @@
1
- import type { Fr } from '@aztec/foundation/fields';
2
- import { InboxLeaf } from '@aztec/stdlib/messaging';
3
- /**
4
- * A simple in-memory implementation of an L1 to L2 message store.
5
- */
6
- export declare class L1ToL2MessageStore {
7
- #private;
8
- /**
9
- * A map pointing from a key in a "messageIndex" format to the corresponding L1 to L2 message hash.
10
- */
11
- protected store: Map<string, Fr>;
12
- constructor();
13
- getTotalL1ToL2MessageCount(): bigint;
14
- addMessage(message: InboxLeaf): void;
15
- getMessages(blockNumber: bigint): Fr[];
16
- /**
17
- * Gets the L1 to L2 message index in the L1 to L2 message tree.
18
- * @param l1ToL2Message - The L1 to L2 message.
19
- * @returns The index of the L1 to L2 message in the L1 to L2 message tree (undefined if not found).
20
- */
21
- getMessageIndex(l1ToL2Message: Fr): bigint | undefined;
22
- }
23
- //# sourceMappingURL=l1_to_l2_message_store.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"l1_to_l2_message_store.d.ts","sourceRoot":"","sources":["../../../src/archiver/memory_archiver_store/l1_to_l2_message_store.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,EAAE,EAAE,MAAM,0BAA0B,CAAC;AACnD,OAAO,EAAE,SAAS,EAAE,MAAM,yBAAyB,CAAC;AAEpD;;GAEG;AACH,qBAAa,kBAAkB;;IAC7B;;OAEG;IACH,SAAS,CAAC,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE,EAAE,CAAC,CAAa;;IAM7C,0BAA0B,IAAI,MAAM;IAIpC,UAAU,CAAC,OAAO,EAAE,SAAS;IAI7B,WAAW,CAAC,WAAW,EAAE,MAAM,GAAG,EAAE,EAAE;IAsBtC;;;;OAIG;IACH,eAAe,CAAC,aAAa,EAAE,EAAE,GAAG,MAAM,GAAG,SAAS;CAQvD"}